1 /* rootmenu.c- user defined menu
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
31 #include <sys/types.h>
38 #include <X11/Xutil.h>
39 #include <X11/Xatom.h>
41 #include "WindowMaker.h"
48 #include "workspace.h"
52 #include "xmodifier.h"
54 #include <WINGs/WUtil.h>
56 #define MAX_SHORTCUT_LENGTH 32
60 extern WDDomain
*WDRootMenu
;
62 extern Cursor wCursor
[WCUR_LAST
];
64 extern Time LastTimestamp
;
66 extern WPreferences wPreferences
;
68 extern int wScreenCount
;
70 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
);
71 static WMenu
*readMenuFile(WScreen
* scr
, char *file_name
);
72 static WMenu
*readMenuDirectory(WScreen
* scr
, char *title
, char **file_name
, char *command
);
74 typedef struct Shortcut
{
75 struct Shortcut
*next
;
83 static Shortcut
*shortcutList
= NULL
;
89 * "Title" EXEC command_to_exec -params
91 * "Title" EXEC command_to_exec -params
93 * "Workspaces" WORKSPACE_MENU
94 * "Title" built_in_command
96 * "Quick Quit" EXIT QUICK
99 * Commands may be preceded by SHORTCUT key
103 * INFO_PANEL - shows the Info Panel
104 * LEGAL_PANEL - shows the Legal info panel
105 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
106 * REFRESH - forces the desktop to be repainted
107 * EXIT [QUICK] - exit the window manager [without confirmation]
108 * EXEC <program> - execute an external program
109 * SHEXEC <command> - execute a shell command
110 * WORKSPACE_MENU - places the workspace submenu
112 * RESTART [<window manager>] - restarts the window manager
113 * SHOW_ALL - unhide all windows on workspace
114 * HIDE_OTHERS - hides all windows excep the focused one
115 * OPEN_MENU file - read menu data from file which must be a valid menu file.
116 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
117 * - read menu data from directory(ies) and
118 * eventually precede each with a command.
119 * OPEN_MENU | command
120 * - opens command and uses its stdout to construct and insert
121 * the resulting menu in current position. The output of
122 * command must be a valid menu description.
123 * The space between '|' and command is optional.
124 * || will do the same, but will not cache the contents.
125 * SAVE_SESSION - saves the current state of the desktop, which include
126 * all running applications, all their hints (geometry,
127 * position on screen, workspace they live on, the dock
128 * or clip from where they were launched, and
129 * if minimized, shaded or hidden. Also saves the current
130 * workspace the user is on. All will be restored on every
131 * start of windowmaker until another SAVE_SESSION or
132 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
133 * WindowMaker domain file, then saving is automatically
134 * done on every windowmaker exit, overwriting any
135 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
137 * CLEAR_SESSION - clears any previous saved session. This will not have
138 * any effect if SaveSessionOnExit is True.
146 static void execCommand(WMenu
* menu
, WMenuEntry
* entry
)
150 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char *)entry
->clientdata
);
152 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
153 GrabModeAsync
, GrabModeAsync
, None
, wCursor
[WCUR_WAIT
], CurrentTime
);
157 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
160 XUngrabPointer(dpy
, CurrentTime
);
164 static void exitCommand(WMenu
* menu
, WMenuEntry
* entry
)
166 static int inside
= 0;
169 /* prevent reentrant calls */
179 if ((long)entry
->clientdata
== M_QUICK
) {
182 int r
, oldSaveSessionFlag
;
184 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
185 r
= wExitDialog(menu
->frame
->screen_ptr
, _("Exit"),
186 _("Exit window manager?"), _("Exit"), _("Cancel"), NULL
);
188 if (r
== WAPRDefault
) {
190 } else if (r
== WAPRAlternate
) {
191 /* Don't modify the "save session on exit" flag if the
192 * user canceled the operation. */
193 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
196 if (result
== R_EXIT
) {
198 printf("Exiting WindowMaker.\n");
200 Shutdown(WSExitMode
);
207 static void shutdownCommand(WMenu
* menu
, WMenuEntry
* entry
)
209 static int inside
= 0;
212 /* prevent reentrant calls */
222 if ((long)entry
->clientdata
== M_QUICK
)
226 if (wSessionIsManaged()) {
229 r
= wMessageDialog(menu
->frame
->screen_ptr
,
230 _("Close X session"),
231 _("Close Window System session?\n"
232 "Kill might close applications with unsaved data."),
233 _("Close"), _("Kill"), _("Cancel"));
234 if (r
== WAPRDefault
)
236 else if (r
== WAPRAlternate
)
241 int r
, oldSaveSessionFlag
;
243 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
245 r
= wExitDialog(menu
->frame
->screen_ptr
,
247 _("Kill Window System session?\n"
248 "(all applications will be closed)"), _("Kill"), _("Cancel"), NULL
);
249 if (r
== WAPRDefault
) {
251 } else if (r
== WAPRAlternate
) {
252 /* Don't modify the "save session on exit" flag if the
253 * user canceled the operation. */
254 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
259 if (result
!= R_CANCEL
) {
261 if (result
== R_CLOSE
) {
262 Shutdown(WSLogoutMode
);
264 #endif /* XSMP_ENABLED */
266 Shutdown(WSKillMode
);
275 static void restartCommand(WMenu
* menu
, WMenuEntry
* entry
)
277 Shutdown(WSRestartPreparationMode
);
278 Restart((char *)entry
->clientdata
, False
);
282 static void refreshCommand(WMenu
* menu
, WMenuEntry
* entry
)
284 wRefreshDesktop(menu
->frame
->screen_ptr
);
287 static void arrangeIconsCommand(WMenu
* menu
, WMenuEntry
* entry
)
289 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
292 static void showAllCommand(WMenu
* menu
, WMenuEntry
* entry
)
294 wShowAllWindows(menu
->frame
->screen_ptr
);
297 static void hideOthersCommand(WMenu
* menu
, WMenuEntry
* entry
)
299 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
302 static void saveSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
304 if (!wPreferences
.save_session_on_exit
)
305 wSessionSaveState(menu
->frame
->screen_ptr
);
307 wScreenSaveState(menu
->frame
->screen_ptr
);
310 static void clearSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
312 wSessionClearState(menu
->frame
->screen_ptr
);
313 wScreenSaveState(menu
->frame
->screen_ptr
);
316 static void infoPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
318 wShowInfoPanel(menu
->frame
->screen_ptr
);
321 static void legalPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
323 wShowLegalPanel(menu
->frame
->screen_ptr
);
326 /********************************************************************/
328 static char *getLocalizedMenuFile(char *menu
)
330 char *buffer
, *ptr
, *locale
;
336 len
= strlen(menu
) + strlen(Locale
) + 8;
337 buffer
= wmalloc(len
);
339 /* try menu.locale_name */
340 snprintf(buffer
, len
, "%s.%s", menu
, Locale
);
341 if (access(buffer
, F_OK
) == 0) {
345 /* position of locale in our buffer */
346 locale
= buffer
+ strlen(menu
) + 1;
348 /* check if it is in the form aa_bb.encoding and check for aa_bb */
349 ptr
= strchr(locale
, '.');
352 if (access(buffer
, F_OK
) == 0) {
356 /* now check for aa */
357 ptr
= strchr(locale
, '_');
360 if (access(buffer
, F_OK
) == 0) {
370 static void raiseMenus(WMenu
* menu
)
374 if (menu
->flags
.mapped
) {
375 wRaiseFrame(menu
->frame
->core
);
377 for (i
= 0; i
< menu
->cascade_no
; i
++) {
378 if (menu
->cascades
[i
])
379 raiseMenus(menu
->cascades
[i
]);
383 Bool
wRootMenuPerformShortcut(XEvent
* event
)
385 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
390 /* ignore CapsLock */
391 modifiers
= event
->xkey
.state
& ValidModMask
;
393 for (ptr
= shortcutList
; ptr
!= NULL
; ptr
= ptr
->next
) {
394 if (ptr
->keycode
== 0 || ptr
->menu
->menu
->screen_ptr
!= scr
)
397 if (ptr
->keycode
== event
->xkey
.keycode
&& ptr
->modifier
== modifiers
) {
398 (*ptr
->entry
->callback
) (ptr
->menu
, ptr
->entry
);
406 void wRootMenuBindShortcuts(Window window
)
412 if (ptr
->modifier
!= AnyModifier
) {
413 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
| LockMask
,
414 window
, True
, GrabModeAsync
, GrabModeAsync
);
416 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
419 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
424 static void rebindKeygrabs(WScreen
* scr
)
428 wwin
= scr
->focused_window
;
430 while (wwin
!= NULL
) {
431 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
433 if (!WFLAGP(wwin
, no_bind_keys
)) {
434 wWindowSetKeyGrabs(wwin
);
440 static void removeShortcutsForMenu(WMenu
* menu
)
443 Shortcut
*newList
= NULL
;
446 while (ptr
!= NULL
) {
448 if (ptr
->menu
== menu
) {
456 shortcutList
= newList
;
457 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
460 static Bool
addShortcut(char *file
, char *shortcutDefinition
, WMenu
* menu
, WMenuEntry
* entry
)
465 char buf
[MAX_SHORTCUT_LENGTH
], *b
;
467 ptr
= wmalloc(sizeof(Shortcut
));
469 strncpy(buf
, shortcutDefinition
, MAX_SHORTCUT_LENGTH
);
474 while ((k
= strchr(b
, '+')) != NULL
) {
478 mod
= wXModifierFromKey(b
);
480 wwarning(_("%s: invalid key modifier \"%s\""), file
, b
);
484 ptr
->modifier
|= mod
;
490 ksym
= XStringToKeysym(b
);
492 if (ksym
== NoSymbol
) {
493 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
494 file
, shortcutDefinition
, entry
->text
);
499 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
500 if (ptr
->keycode
== 0) {
501 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
502 shortcutDefinition
, entry
->text
);
510 ptr
->next
= shortcutList
;
513 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
518 /*******************************/
520 static char *cropline(char *line
)
524 if (strlen(line
) == 0)
527 end
= &(line
[strlen(line
)]) - 1;
528 while (isspace(*line
) && *line
!= 0)
530 while (end
> line
&& isspace(*end
)) {
537 static char *next_token(char *line
, char **next
)
543 while (*line
== ' ' || *line
== '\t')
551 while (*tmp
!= 0 && *tmp
!= '"')
554 wwarning(_("%s: unmatched '\"' in menu file"), line
);
565 } while (*tmp
!= 0 && *tmp
!= ' ' && *tmp
!= '\t');
579 while (*tmp
== ' ' || *tmp
== '\t')
588 static void separateCommand(char *line
, char ***file
, char **command
)
590 char *token
, *tmp
= line
;
591 WMArray
*array
= WMCreateArray(4);
597 token
= next_token(tmp
, &tmp
);
599 if (strcmp(token
, "WITH") == 0) {
600 if (tmp
!= NULL
&& *tmp
!= 0)
601 *command
= wstrdup(tmp
);
603 wwarning(_("%s: missing command"), line
);
606 WMAddToArray(array
, token
);
608 } while (token
!= NULL
&& tmp
!= NULL
);
610 count
= WMGetArrayItemCount(array
);
612 *file
= wmalloc(sizeof(char *) * (count
+ 1));
613 (*file
)[count
] = NULL
;
614 for (i
= 0; i
< count
; i
++) {
615 (*file
)[i
] = WMGetFromArray(array
, i
);
621 static void constructMenu(WMenu
* menu
, WMenuEntry
* entry
)
624 struct stat stat_buf
;
631 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
632 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
633 wwarning(_("invalid OPEN_MENU specification: %s"), (char *)entry
->clientdata
);
637 if (path
[0][0] == '|') {
640 if (!menu
->cascades
[entry
->cascade
] || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
643 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
645 if (submenu
!= NULL
) {
646 if (path
[0][1] == '|')
647 submenu
->timestamp
= 0;
649 submenu
->timestamp
= 1; /* there's no automatic reloading */
657 while (path
[i
] != NULL
) {
660 if (strcmp(path
[i
], "-noext") == 0) {
665 tmp
= wexpandpath(path
[i
]);
667 lpath
= getLocalizedMenuFile(tmp
);
676 if (stat(path
[i
], &stat_buf
) == 0) {
677 if (last
< stat_buf
.st_mtime
)
678 last
= stat_buf
.st_mtime
;
682 wsyserror(_("%s:could not stat menu"), path
[i
]);
690 wsyserror(_("%s:could not stat menu:%s"), "OPEN_MENU", (char *)entry
->clientdata
);
693 stat(path
[first
], &stat_buf
);
694 if (!menu
->cascades
[entry
->cascade
]
695 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
697 if (S_ISDIR(stat_buf
.st_mode
)) {
699 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
, entry
->text
, path
, cmd
);
701 submenu
->timestamp
= last
;
702 } else if (S_ISREG(stat_buf
.st_mode
)) {
706 wwarning(_("too many parameters in OPEN_MENU: %s"),
707 (char *)entry
->clientdata
);
709 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
711 submenu
->timestamp
= stat_buf
.st_mtime
;
721 wMenuEntryRemoveCascade(menu
, entry
);
722 wMenuEntrySetCascade(menu
, entry
, submenu
);
727 while (path
[i
] != NULL
)
734 static void cleanupWorkspaceMenu(WMenu
* menu
)
736 if (menu
->frame
->screen_ptr
->workspace_menu
== menu
)
737 menu
->frame
->screen_ptr
->workspace_menu
= NULL
;
740 static WMenuEntry
*addWorkspaceMenu(WScreen
* scr
, WMenu
* menu
, char *title
)
745 if (scr
->flags
.added_workspace_menu
) {
747 ("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
750 scr
->flags
.added_workspace_menu
= 1;
752 wsmenu
= wWorkspaceMenuMake(scr
, True
);
753 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
755 scr
->workspace_menu
= wsmenu
;
756 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
757 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
759 wWorkspaceMenuUpdate(scr
, wsmenu
);
764 static void cleanupWindowsMenu(WMenu
* menu
)
766 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
767 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
770 static WMenuEntry
*addWindowsMenu(WScreen
* scr
, WMenu
* menu
, char *title
)
776 if (scr
->flags
.added_windows_menu
) {
778 ("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
781 scr
->flags
.added_windows_menu
= 1;
783 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
784 wwmenu
->on_destroy
= cleanupWindowsMenu
;
785 scr
->switch_menu
= wwmenu
;
786 wwin
= scr
->focused_window
;
788 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
792 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
793 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
798 static WMenuEntry
*addMenuEntry(WMenu
* menu
, char *title
, char *shortcut
, char *command
,
799 char *params
, char *file_name
)
802 WMenuEntry
*entry
= NULL
;
803 Bool shortcutOk
= False
;
807 scr
= menu
->frame
->screen_ptr
;
808 if (strcmp(command
, "OPEN_MENU") == 0) {
810 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
815 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
817 path
= wstrdup(params
);
819 dummy
= wMenuCreate(scr
, title
, False
);
820 dummy
->on_destroy
= removeShortcutsForMenu
;
821 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
822 entry
->free_cdata
= free
;
823 wMenuEntrySetCascade(menu
, entry
, dummy
);
825 } else if (strcmp(command
, "EXEC") == 0) {
827 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
829 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrconcat("exec ", params
));
830 entry
->free_cdata
= free
;
833 } else if (strcmp(command
, "SHEXEC") == 0) {
835 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
837 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrdup(params
));
838 entry
->free_cdata
= free
;
841 } else if (strcmp(command
, "EXIT") == 0) {
843 if (params
&& strcmp(params
, "QUICK") == 0)
844 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void *)M_QUICK
);
846 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
849 } else if (strcmp(command
, "SHUTDOWN") == 0) {
851 if (params
&& strcmp(params
, "QUICK") == 0)
852 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, (void *)M_QUICK
);
854 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
857 } else if (strcmp(command
, "REFRESH") == 0) {
858 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
861 } else if (strcmp(command
, "WORKSPACE_MENU") == 0) {
862 entry
= addWorkspaceMenu(scr
, menu
, title
);
865 } else if (strcmp(command
, "WINDOWS_MENU") == 0) {
866 entry
= addWindowsMenu(scr
, menu
, title
);
869 } else if (strcmp(command
, "ARRANGE_ICONS") == 0) {
870 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
873 } else if (strcmp(command
, "HIDE_OTHERS") == 0) {
874 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
877 } else if (strcmp(command
, "SHOW_ALL") == 0) {
878 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
881 } else if (strcmp(command
, "RESTART") == 0) {
882 entry
= wMenuAddCallback(menu
, title
, restartCommand
, params
? wstrdup(params
) : NULL
);
883 entry
->free_cdata
= free
;
885 } else if (strcmp(command
, "SAVE_SESSION") == 0) {
886 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
889 } else if (strcmp(command
, "CLEAR_SESSION") == 0) {
890 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
892 } else if (strcmp(command
, "INFO_PANEL") == 0) {
893 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
895 } else if (strcmp(command
, "LEGAL_PANEL") == 0) {
896 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
899 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
, command
);
904 if (shortcut
&& entry
) {
906 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
, title
);
908 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
910 entry
->rtext
= GetShortcutString(shortcut
);
912 entry->rtext = wstrdup(shortcut);
921 /******************* Menu Configuration From File *******************/
923 static void separateline(char *line
, char *title
, char *command
, char *parameter
, char *shortcut
)
934 while (isspace(*line
) && (*line
!= 0))
939 while (line
[i
] != '"' && (line
[i
] != 0))
945 while (!isspace(line
[i
]) && (line
[i
] != 0))
948 strncpy(title
, line
, i
);
952 /* get the command or shortcut keyword */
953 while (isspace(*line
) && (*line
!= 0))
958 while (!isspace(line
[i
]) && (line
[i
] != 0))
960 strncpy(command
, line
, i
);
964 if (strcmp(command
, "SHORTCUT") == 0) {
965 /* get the shortcut key */
966 while (isspace(*line
) && (*line
!= 0))
971 while (line
[i
] != '"' && (line
[i
] != 0))
977 while (!isspace(line
[i
]) && (line
[i
] != 0))
980 strncpy(shortcut
, line
, i
);
986 /* get the command */
987 while (isspace(*line
) && (*line
!= 0))
992 while (!isspace(line
[i
]) && (line
[i
] != 0))
994 strncpy(command
, line
, i
);
999 /* get the parameters */
1000 while (isspace(*line
) && (*line
!= 0))
1008 while (line
[l
] != 0 && line
[l
] != '"') {
1009 parameter
[l
] = line
[l
];
1017 while (isspace(line
[l
]) && (l
> 0))
1019 strncpy(parameter
, line
, l
);
1023 static WMenu
*parseCascade(WScreen
* scr
, WMenu
* menu
, FILE * file
, char *file_name
)
1025 char linebuf
[MAXLINE
];
1026 char elinebuf
[MAXLINE
];
1027 char title
[MAXLINE
];
1028 char command
[MAXLINE
];
1029 char shortcut
[MAXLINE
];
1030 char params
[MAXLINE
];
1033 while (!feof(file
)) {
1037 fgets(linebuf
, MAXLINE
, file
);
1038 line
= cropline(linebuf
);
1039 lsize
= strlen(line
);
1041 if (line
[lsize
- 1] == '\\') {
1044 fgets(elinebuf
, MAXLINE
, file
);
1045 line2
= cropline(elinebuf
);
1046 lsize2
= strlen(line2
);
1047 if (lsize2
+ lsize
> MAXLINE
) {
1048 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1052 line
[lsize
- 1] = 0;
1053 lsize
+= lsize2
- 1;
1054 strcat(line
, line2
);
1059 } while (!ok
&& !feof(file
));
1063 if (line
[0] == 0 || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
1066 separateline(line
, title
, command
, params
, shortcut
);
1069 wwarning(_("%s:missing command in menu config: %s"), file_name
, line
);
1073 if (strcasecmp(command
, "MENU") == 0) {
1078 cascade
= wMenuCreate(scr
, title
, False
);
1079 cascade
->on_destroy
= removeShortcutsForMenu
;
1080 if (parseCascade(scr
, cascade
, file
, file_name
) == NULL
) {
1081 wMenuDestroy(cascade
, True
);
1083 wMenuEntrySetCascade(menu
, wMenuAddCallback(menu
, title
, NULL
, NULL
), cascade
);
1085 } else if (strcasecmp(command
, "END") == 0) {
1091 addMenuEntry(menu
, title
, shortcut
[0] ? shortcut
: NULL
, command
,
1092 params
[0] ? params
: NULL
, file_name
);
1096 wwarning(_("%s:syntax error in menu file:END declaration missing"), file_name
);
1103 static WMenu
*readMenuFile(WScreen
* scr
, char *file_name
)
1107 char linebuf
[MAXLINE
];
1108 char title
[MAXLINE
];
1109 char shortcut
[MAXLINE
];
1110 char command
[MAXLINE
];
1111 char params
[MAXLINE
];
1119 if (!wPreferences
.flags
.nocpp
) {
1120 args
= MakeCPPArgs(file_name
);
1122 wwarning(_("could not make arguments for menu file preprocessor"));
1124 snprintf(command
, sizeof(command
), "%s %s %s", CPP_PATH
, args
, file_name
);
1126 file
= popen(command
, "r");
1128 wsyserror(_("%s:could not open/preprocess menu file"), file_name
);
1137 file
= fopen(file_name
, "rb");
1139 wsyserror(_("%s:could not open menu file"), file_name
);
1144 while (!feof(file
)) {
1145 if (!fgets(linebuf
, MAXLINE
, file
))
1147 line
= cropline(linebuf
);
1148 if (line
[0] == 0 || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
1151 separateline(line
, title
, command
, params
, shortcut
);
1154 wwarning(_("%s:missing command in menu config: %s"), file_name
, line
);
1157 if (strcasecmp(command
, "MENU") == 0) {
1158 menu
= wMenuCreate(scr
, title
, True
);
1159 menu
->on_destroy
= removeShortcutsForMenu
;
1160 if (!parseCascade(scr
, menu
, file
, file_name
)) {
1161 wMenuDestroy(menu
, True
);
1165 wwarning(_("%s:invalid menu file. MENU command is missing"), file_name
);
1172 if (pclose(file
) == -1) {
1173 wsyserror(_("error reading preprocessed menu data"));
1185 /************ Menu Configuration From Pipe *************/
1187 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
)
1191 char linebuf
[MAXLINE
];
1192 char title
[MAXLINE
];
1193 char command
[MAXLINE
];
1194 char params
[MAXLINE
];
1195 char shortcut
[MAXLINE
];
1198 char flat_file
[MAXLINE
];
1205 flat_file
[0] = '\0';
1207 for (i
= 0; file_name
[i
] != NULL
; i
++) {
1208 strcat(flat_file
, file_name
[i
]);
1209 strcat(flat_file
, " ");
1211 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1214 if (!wPreferences
.flags
.nocpp
) {
1215 args
= MakeCPPArgs(filename
);
1217 wwarning(_("could not make arguments for menu file preprocessor"));
1219 snprintf(command
, sizeof(command
), "%s | %s %s", filename
, CPP_PATH
, args
);
1222 file
= popen(command
, "r");
1224 wsyserror(_("%s:could not open/preprocess menu file"), filename
);
1233 file
= popen(filename
, "rb");
1236 wsyserror(_("%s:could not open menu file"), filename
);
1241 while (!feof(file
)) {
1242 if (!fgets(linebuf
, MAXLINE
, file
))
1244 line
= cropline(linebuf
);
1245 if (line
[0] == 0 || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
1248 separateline(line
, title
, command
, params
, shortcut
);
1251 wwarning(_("%s:missing command in menu config: %s"), file_name
, line
);
1254 if (strcasecmp(command
, "MENU") == 0) {
1255 menu
= wMenuCreate(scr
, title
, True
);
1256 menu
->on_destroy
= removeShortcutsForMenu
;
1257 if (!parseCascade(scr
, menu
, file
, filename
)) {
1258 wMenuDestroy(menu
, True
);
1262 wwarning(_("%s:no title given for the root menu"), filename
);
1277 static int myCompare(const void *d1
, const void *d2
)
1279 dir_data
*p1
= *(dir_data
**) d1
;
1280 dir_data
*p2
= *(dir_data
**) d2
;
1282 return strcmp(p1
->name
, p2
->name
);
1285 /************ Menu Configuration From Directory *************/
1287 static Bool
isFilePackage(char *file
)
1291 /* check if the extension indicates this file is a
1292 * file package. For now, only recognize .themed */
1296 if (l
> 7 && strcmp(&(file
[l
- 7]), ".themed") == 0) {
1303 static WMenu
*readMenuDirectory(WScreen
* scr
, char *title
, char **path
, char *command
)
1306 struct dirent
*dentry
;
1307 struct stat stat_buf
;
1310 WMArray
*dirs
= NULL
, *files
= NULL
;
1311 WMArrayIterator iter
;
1312 int length
, i
, have_space
= 0;
1314 int stripExtension
= 0;
1316 dirs
= WMCreateArray(16);
1317 files
= WMCreateArray(16);
1320 while (path
[i
] != NULL
) {
1321 if (strcmp(path
[i
], "-noext") == 0) {
1327 dir
= opendir(path
[i
]);
1333 while ((dentry
= readdir(dir
))) {
1335 if (strcmp(dentry
->d_name
, ".") == 0 || strcmp(dentry
->d_name
, "..") == 0)
1338 if (dentry
->d_name
[0] == '.')
1341 buffer
= malloc(strlen(path
[i
]) + strlen(dentry
->d_name
) + 4);
1343 wsyserror(_("out of memory while constructing directory menu %s"), path
[i
]);
1347 strcpy(buffer
, path
[i
]);
1348 strcat(buffer
, "/");
1349 strcat(buffer
, dentry
->d_name
);
1351 if (stat(buffer
, &stat_buf
) != 0) {
1352 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1353 path
[i
], dentry
->d_name
);
1355 Bool isFilePack
= False
;
1358 if (S_ISDIR(stat_buf
.st_mode
)
1359 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1361 /* access always returns success for user root */
1362 if (access(buffer
, X_OK
) == 0) {
1363 /* Directory is accesible. Add to directory list */
1365 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1366 data
->name
= wstrdup(dentry
->d_name
);
1369 WMAddToArray(dirs
, data
);
1371 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1372 /* Hack because access always returns X_OK success for user root */
1373 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1374 if ((command
!= NULL
&& access(buffer
, R_OK
) == 0) ||
1375 (command
== NULL
&& access(buffer
, X_OK
) == 0 &&
1376 (stat_buf
.st_mode
& S_IXANY
))) {
1378 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1379 data
->name
= wstrdup(dentry
->d_name
);
1382 WMAddToArray(files
, data
);
1393 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1399 WMSortArray(dirs
, myCompare
);
1400 WMSortArray(files
, myCompare
);
1402 menu
= wMenuCreate(scr
, title
, False
);
1403 menu
->on_destroy
= removeShortcutsForMenu
;
1405 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1406 /* New directory. Use same OPEN_MENU command that was used
1407 * for the current directory. */
1408 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1412 length
+= strlen(command
) + 6;
1413 buffer
= malloc(length
);
1415 wsyserror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1421 strcat(buffer
, "-noext ");
1423 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1426 strcat(buffer
, "\"");
1427 strcat(buffer
, path
[data
->index
]);
1429 strcat(buffer
, "/");
1430 strcat(buffer
, data
->name
);
1432 strcat(buffer
, "\"");
1434 strcat(buffer
, " WITH ");
1435 strcat(buffer
, command
);
1438 addMenuEntry(menu
, data
->name
, NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1446 WM_ITERATE_ARRAY(files
, data
, iter
) {
1447 /* executable: add as entry */
1448 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1450 length
+= strlen(command
);
1452 buffer
= malloc(length
);
1454 wsyserror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1458 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1459 if (command
!= NULL
) {
1460 strcpy(buffer
, command
);
1461 strcat(buffer
, " ");
1463 strcat(buffer
, "\"");
1464 strcat(buffer
, path
[data
->index
]);
1469 strcat(buffer
, path
[data
->index
]);
1471 strcpy(buffer
, path
[data
->index
]);
1474 strcat(buffer
, "/");
1475 strcat(buffer
, data
->name
);
1477 strcat(buffer
, "\"");
1479 if (stripExtension
) {
1480 char *ptr
= strrchr(data
->name
, '.');
1481 if (ptr
&& ptr
!= data
->name
)
1484 addMenuEntry(menu
, data
->name
, NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1498 /************ Menu Configuration From WMRootMenu *************/
1500 static WMenu
*makeDefaultMenu(WScreen
* scr
)
1504 menu
= wMenuCreate(scr
, _("Commands"), True
);
1505 wMenuAddCallback(menu
, "XTerm", execCommand
, "xterm");
1506 wMenuAddCallback(menu
, "rxvt", execCommand
, "rxvt");
1507 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1508 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1513 *----------------------------------------------------------------------
1515 * Reads root menu configuration from defaults database.
1517 *----------------------------------------------------------------------
1519 static WMenu
*configureMenu(WScreen
* scr
, WMPropList
* definition
)
1524 WMPropList
*title
, *command
, *params
;
1527 if (WMIsPLString(definition
)) {
1528 struct stat stat_buf
;
1530 Bool menu_is_default
= False
;
1532 /* menu definition is a string. Probably a path, so parse the file */
1534 tmp
= wexpandpath(WMGetFromPLString(definition
));
1536 path
= getLocalizedMenuFile(tmp
);
1539 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1542 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1543 menu_is_default
= True
;
1547 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"), tmp
);
1552 if (stat(path
, &stat_buf
) < 0) {
1553 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1559 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1560 /* if the pointer in WMRootMenu has changed */
1561 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1563 if (menu_is_default
) {
1565 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1569 menu
= readMenuFile(scr
, path
);
1571 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1581 count
= WMGetPropListItemCount(definition
);
1585 elem
= WMGetFromPLArray(definition
, 0);
1586 if (!WMIsPLString(elem
)) {
1587 tmp
= WMGetPropListDescription(elem
, False
);
1588 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1592 mtitle
= WMGetFromPLString(elem
);
1594 menu
= wMenuCreate(scr
, mtitle
, False
);
1595 menu
->on_destroy
= removeShortcutsForMenu
;
1597 #ifdef GLOBAL_SUBMENU_FILE
1602 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1605 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1606 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1611 for (i
= 1; i
< count
; i
++) {
1612 elem
= WMGetFromPLArray(definition
, i
);
1614 if (WMIsPLString(elem
)) {
1617 file
= WMGetFromPLString(elem
);
1621 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1624 if (WMIsPLArray(WMGetFromPLArray(elem
, 1))) {
1629 submenu
= configureMenu(scr
, elem
);
1631 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1632 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1636 WMPropList
*shortcut
;
1639 title
= WMGetFromPLArray(elem
, idx
++);
1640 shortcut
= WMGetFromPLArray(elem
, idx
++);
1641 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT") == 0) {
1642 shortcut
= WMGetFromPLArray(elem
, idx
++);
1643 command
= WMGetFromPLArray(elem
, idx
++);
1648 params
= WMGetFromPLArray(elem
, idx
++);
1650 if (!title
|| !command
)
1653 addMenuEntry(menu
, WMGetFromPLString(title
),
1654 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1655 WMGetFromPLString(command
),
1656 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1661 tmp
= WMGetPropListDescription(elem
, False
);
1662 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1670 *----------------------------------------------------------------------
1672 * Opens the root menu, parsing the menu configuration from the
1673 * defaults database.
1674 * If the menu is already mapped and is not sticked to the
1675 * root window, it will be unmapped.
1678 * The menu may be remade.
1681 * Construction of OPEN_MENU entries are delayed to the moment the
1683 *----------------------------------------------------------------------
1685 void OpenRootMenu(WScreen
* scr
, int x
, int y
, int keyboard
)
1688 WMPropList
*definition
;
1690 static WMPropList *domain=NULL;
1693 domain = WMCreatePLString("WMRootMenu");
1697 scr
->flags
.root_menu_changed_shortcuts
= 0;
1698 scr
->flags
.added_workspace_menu
= 0;
1699 scr
->flags
.added_windows_menu
= 0;
1701 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1702 menu
= scr
->root_menu
;
1703 if (!menu
->flags
.buttoned
) {
1706 wRaiseFrame(menu
->frame
->core
);
1709 wMenuMapAt(menu
, 0, 0, True
);
1711 wMenuMapCopyAt(menu
, x
- menu
->frame
->core
->width
/ 2, y
);
1716 definition
= WDRootMenu
->dictionary
;
1719 definition = PLGetDomain(domain);
1722 if (WMIsPLArray(definition
)) {
1723 if (!scr
->root_menu
|| WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1724 menu
= configureMenu(scr
, definition
);
1726 menu
->timestamp
= WDRootMenu
->timestamp
;
1731 menu
= configureMenu(scr
, definition
);
1736 /* menu hasn't changed or could not be read */
1737 if (!scr
->root_menu
) {
1738 wMessageDialog(scr
, _("Error"),
1739 _("The applications menu could not be loaded. "
1740 "Look at the console output for a detailed "
1741 "description of the errors."), _("OK"), NULL
, NULL
);
1743 menu
= makeDefaultMenu(scr
);
1744 scr
->root_menu
= menu
;
1746 menu
= scr
->root_menu
;
1749 if (scr
->root_menu
) {
1750 wMenuDestroy(scr
->root_menu
, True
);
1752 scr
->root_menu
= menu
;
1757 if (keyboard
&& x
== 0 && y
== 0) {
1759 } else if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
1760 newx
= x
- menu
->frame
->core
->width
/ 2;
1761 newy
= y
- menu
->frame
->core
->height
/ 2;
1763 newx
= x
- menu
->frame
->core
->width
/ 2;
1766 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1769 if (scr
->flags
.root_menu_changed_shortcuts
)
1770 rebindKeygrabs(scr
);