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 along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/types.h>
38 #include <X11/Xutil.h>
39 #include <X11/Xatom.h>
41 #include "WindowMaker.h"
49 #include "workspace.h"
54 #include "xmodifier.h"
57 #include "switchmenu.h"
59 #include <WINGs/WUtil.h>
61 #define MAX_SHORTCUT_LENGTH 32
64 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
);
65 static WMenu
*readPLMenuPipe(WScreen
* scr
, char **file_name
);
66 static WMenu
*readMenuFile(WScreen
*scr
, const char *file_name
);
67 static WMenu
*readMenuDirectory(WScreen
*scr
, const char *title
, char **file_name
, const char *command
);
68 static WMenu
*configureMenu(WScreen
* scr
, WMPropList
* definition
, Bool includeGlobals
);
69 static void menu_parser_register_macros(WMenuParser parser
);
71 typedef struct Shortcut
{
72 struct Shortcut
*next
;
80 static Shortcut
*shortcutList
= NULL
;
86 * "Title" EXEC command_to_exec -params
88 * "Title" EXEC command_to_exec -params
90 * "Workspaces" WORKSPACE_MENU
91 * "Title" built_in_command
93 * "Quick Quit" EXIT QUICK
96 * Commands may be preceded by SHORTCUT key
100 * INFO_PANEL - shows the Info Panel
101 * LEGAL_PANEL - shows the Legal info panel
102 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
103 * REFRESH - forces the desktop to be repainted
104 * EXIT [QUICK] - exit the window manager [without confirmation]
105 * EXEC <program> - execute an external program
106 * SHEXEC <command> - execute a shell command
107 * WORKSPACE_MENU - places the workspace submenu
109 * RESTART [<window manager>] - restarts the window manager
110 * SHOW_ALL - unhide all windows on workspace
111 * HIDE_OTHERS - hides all windows excep the focused one
112 * OPEN_MENU file - read menu data from file which must be a valid menu file.
113 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
114 * - read menu data from directory(ies) and
115 * eventually precede each with a command.
116 * OPEN_MENU | command
117 * - opens command and uses its stdout to construct and insert
118 * the resulting menu in current position. The output of
119 * command must be a valid menu description.
120 * The space between '|' and command is optional.
121 * || will do the same, but will not cache the contents.
122 * OPEN_PLMENU | command
123 * - opens command and uses its stdout which must be in proplist
124 * fromat to construct and insert the resulting menu in current
126 * The space between '|' and command is optional.
127 * || will do the same, but will not cache the contents.
128 * SAVE_SESSION - saves the current state of the desktop, which include
129 * all running applications, all their hints (geometry,
130 * position on screen, workspace they live on, the dock
131 * or clip from where they were launched, and
132 * if minimized, shaded or hidden. Also saves the current
133 * workspace the user is on. All will be restored on every
134 * start of windowmaker until another SAVE_SESSION or
135 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
136 * WindowMaker domain file, then saving is automatically
137 * done on every windowmaker exit, overwriting any
138 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
140 * CLEAR_SESSION - clears any previous saved session. This will not have
141 * any effect if SaveSessionOnExit is True.
149 static void execCommand(WMenu
* menu
, WMenuEntry
* entry
)
153 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char *)entry
->clientdata
);
155 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
156 GrabModeAsync
, GrabModeAsync
, None
, wPreferences
.cursor
[WCUR_WAIT
], CurrentTime
);
160 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
163 XUngrabPointer(dpy
, CurrentTime
);
167 static void exitCommand(WMenu
* menu
, WMenuEntry
* entry
)
169 static int inside
= 0;
172 /* prevent reentrant calls */
182 if ((long)entry
->clientdata
== M_QUICK
) {
185 int r
, oldSaveSessionFlag
;
187 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
188 r
= wExitDialog(menu
->frame
->screen_ptr
, _("Exit"),
189 _("Exit window manager?"), _("Exit"), _("Cancel"), NULL
);
191 if (r
== WAPRDefault
) {
193 } else if (r
== WAPRAlternate
) {
194 /* Don't modify the "save session on exit" flag if the
195 * user canceled the operation. */
196 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
199 if (result
== R_EXIT
)
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
)
225 int r
, oldSaveSessionFlag
;
227 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
229 r
= wExitDialog(menu
->frame
->screen_ptr
,
231 _("Kill Window System session?\n"
232 "(all applications will be closed)"), _("Kill"), _("Cancel"), NULL
);
233 if (r
== WAPRDefault
) {
235 } else if (r
== WAPRAlternate
) {
236 /* Don't modify the "save session on exit" flag if the
237 * user canceled the operation. */
238 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
242 if (result
!= R_CANCEL
) {
243 Shutdown(WSKillMode
);
251 static void restartCommand(WMenu
* menu
, WMenuEntry
* entry
)
253 /* Parameter not used, but tell the compiler that it is ok */
257 Shutdown(WSRestartPreparationMode
);
258 Restart((char *)entry
->clientdata
, False
);
262 static void refreshCommand(WMenu
* menu
, WMenuEntry
* entry
)
264 /* Parameter not used, but tell the compiler that it is ok */
267 wRefreshDesktop(menu
->frame
->screen_ptr
);
270 static void arrangeIconsCommand(WMenu
* menu
, WMenuEntry
* entry
)
272 /* Parameter not used, but tell the compiler that it is ok */
275 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
278 static void showAllCommand(WMenu
* menu
, WMenuEntry
* entry
)
280 /* Parameter not used, but tell the compiler that it is ok */
283 wShowAllWindows(menu
->frame
->screen_ptr
);
286 static void hideOthersCommand(WMenu
* menu
, WMenuEntry
* entry
)
288 /* Parameter not used, but tell the compiler that it is ok */
291 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
294 static void saveSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
296 /* Parameter not used, but tell the compiler that it is ok */
299 if (!wPreferences
.save_session_on_exit
)
300 wSessionSaveState(menu
->frame
->screen_ptr
);
302 wScreenSaveState(menu
->frame
->screen_ptr
);
305 static void clearSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
307 /* Parameter not used, but tell the compiler that it is ok */
310 wSessionClearState();
311 wScreenSaveState(menu
->frame
->screen_ptr
);
314 static void infoPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
316 /* Parameter not used, but tell the compiler that it is ok */
319 wShowInfoPanel(menu
->frame
->screen_ptr
);
322 static void legalPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
324 /* Parameter not used, but tell the compiler that it is ok */
327 wShowLegalPanel(menu
->frame
->screen_ptr
);
330 /********************************************************************/
332 static char *getLocalizedMenuFile(const char *menu
)
334 char *buffer
, *ptr
, *locale
;
337 if (!w_global
.locale
)
340 len
= strlen(menu
) + strlen(w_global
.locale
) + 8;
341 buffer
= wmalloc(len
);
343 /* try menu.locale_name */
344 snprintf(buffer
, len
, "%s.%s", menu
, w_global
.locale
);
345 if (access(buffer
, F_OK
) == 0)
348 /* position of locale in our buffer */
349 locale
= buffer
+ strlen(menu
) + 1;
351 /* check if it is in the form aa_bb.encoding and check for aa_bb */
352 ptr
= strchr(locale
, '.');
355 if (access(buffer
, F_OK
) == 0)
359 /* now check for aa */
360 ptr
= strchr(locale
, '_');
363 if (access(buffer
, F_OK
) == 0)
372 Bool
wRootMenuPerformShortcut(XEvent
* event
)
374 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
379 /* ignore CapsLock */
380 modifiers
= event
->xkey
.state
& w_global
.shortcut
.modifiers_mask
;
382 for (ptr
= shortcutList
; ptr
!= NULL
; ptr
= ptr
->next
) {
383 if (ptr
->keycode
== 0 || ptr
->menu
->menu
->screen_ptr
!= scr
)
386 if (ptr
->keycode
== event
->xkey
.keycode
&& ptr
->modifier
== modifiers
) {
387 (*ptr
->entry
->callback
) (ptr
->menu
, ptr
->entry
);
395 void wRootMenuBindShortcuts(Window window
)
401 if (ptr
->modifier
!= AnyModifier
) {
402 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
| LockMask
,
403 window
, True
, GrabModeAsync
, GrabModeAsync
);
405 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
408 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
413 static void rebindKeygrabs(WScreen
* scr
)
417 wwin
= scr
->focused_window
;
419 while (wwin
!= NULL
) {
420 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
422 if (!WFLAGP(wwin
, no_bind_keys
)) {
423 wWindowSetKeyGrabs(wwin
);
429 static void removeShortcutsForMenu(WMenu
* menu
)
432 Shortcut
*newList
= NULL
;
435 while (ptr
!= NULL
) {
437 if (ptr
->menu
== menu
) {
445 shortcutList
= newList
;
446 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
449 static Bool
addShortcut(const char *file
, const char *shortcutDefinition
, WMenu
*menu
, WMenuEntry
*entry
)
454 char buf
[MAX_SHORTCUT_LENGTH
], *b
;
456 ptr
= wmalloc(sizeof(Shortcut
));
458 wstrlcpy(buf
, shortcutDefinition
, MAX_SHORTCUT_LENGTH
);
463 while ((k
= strchr(b
, '+')) != NULL
) {
467 mod
= wXModifierFromKey(b
);
469 wwarning(_("%s: invalid key modifier \"%s\""), file
, b
);
473 ptr
->modifier
|= mod
;
479 ksym
= XStringToKeysym(b
);
481 if (ksym
== NoSymbol
) {
482 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
483 file
, shortcutDefinition
, entry
->text
);
488 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
489 if (ptr
->keycode
== 0) {
490 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
491 shortcutDefinition
, entry
->text
);
499 ptr
->next
= shortcutList
;
502 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
507 static char *next_token(char *line
, char **next
)
513 while (*line
== ' ' || *line
== '\t')
521 while (*tmp
!= 0 && *tmp
!= '"')
524 wwarning(_("%s: unmatched '\"' in menu file"), line
);
535 } while (*tmp
!= 0 && *tmp
!= ' ' && *tmp
!= '\t');
549 while (*tmp
== ' ' || *tmp
== '\t')
558 static void separateCommand(char *line
, char ***file
, char **command
)
560 char *token
, *tmp
= line
;
561 WMArray
*array
= WMCreateArray(4);
567 token
= next_token(tmp
, &tmp
);
569 if (strcmp(token
, "WITH") == 0) {
570 if (tmp
!= NULL
&& *tmp
!= 0)
571 *command
= wstrdup(tmp
);
573 wwarning(_("%s: missing command"), line
);
576 WMAddToArray(array
, token
);
578 } while (token
!= NULL
&& tmp
!= NULL
);
580 count
= WMGetArrayItemCount(array
);
582 *file
= wmalloc(sizeof(char *) * (count
+ 1));
583 (*file
)[count
] = NULL
;
584 for (i
= 0; i
< count
; i
++) {
585 (*file
)[i
] = WMGetFromArray(array
, i
);
591 static WMenu
*constructPLMenu(WScreen
*screen
, const char *path
)
593 WMPropList
*pl
= NULL
;
599 pl
= WMReadPropListFromFile(path
);
603 menu
= configureMenu(screen
, pl
, False
);
605 WMReleasePropList(pl
);
610 menu
->on_destroy
= removeShortcutsForMenu
;
616 static void constructMenu(WMenu
* menu
, WMenuEntry
* entry
)
619 struct stat stat_buf
;
626 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
627 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
628 wwarning(_("invalid OPEN_MENU specification: %s"), (char *)entry
->clientdata
);
634 if (path
[0][0] == '|') {
637 if (!menu
->cascades
[entry
->cascade
] || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
640 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
642 if (submenu
!= NULL
) {
643 if (path
[0][1] == '|')
644 submenu
->timestamp
= 0;
646 submenu
->timestamp
= 1; /* there's no automatic reloading */
654 /* try interpreting path as a proplist file */
655 submenu
= constructPLMenu(menu
->frame
->screen_ptr
, path
[0]);
656 /* if unsuccessful, try it as an old-style file */
660 while (path
[i
] != NULL
) {
663 if (strcmp(path
[i
], "-noext") == 0) {
668 tmp
= wexpandpath(path
[i
]);
670 lpath
= getLocalizedMenuFile(tmp
);
679 if (stat(path
[i
], &stat_buf
) == 0) {
680 if (last
< stat_buf
.st_mtime
)
681 last
= stat_buf
.st_mtime
;
685 werror(_("%s:could not stat menu"), path
[i
]);
693 werror(_("%s:could not stat menu:%s"), "OPEN_MENU", (char *)entry
->clientdata
);
696 stat(path
[first
], &stat_buf
);
697 if (!menu
->cascades
[entry
->cascade
]
698 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
700 if (S_ISDIR(stat_buf
.st_mode
)) {
702 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
, entry
->text
, path
, cmd
);
704 submenu
->timestamp
= last
;
705 } else if (S_ISREG(stat_buf
.st_mode
)) {
709 wwarning(_("too many parameters in OPEN_MENU: %s"),
710 (char *)entry
->clientdata
);
712 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
714 submenu
->timestamp
= stat_buf
.st_mtime
;
725 wMenuEntryRemoveCascade(menu
, entry
);
726 wMenuEntrySetCascade(menu
, entry
, submenu
);
731 while (path
[i
] != NULL
)
738 static void constructPLMenuFromPipe(WMenu
* menu
, WMenuEntry
* entry
)
740 WMenu
*submenu
= NULL
;
745 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
746 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
747 wwarning(_("invalid OPEN_PLMENU specification: %s"),
748 (char *)entry
->clientdata
);
754 if (path
[0][0] == '|') {
757 if (!menu
->cascades
[entry
->cascade
]
758 || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
760 submenu
= readPLMenuPipe(menu
->frame
->screen_ptr
, path
);
762 if (submenu
!= NULL
) {
763 if (path
[0][1] == '|')
764 submenu
->timestamp
= 0;
766 submenu
->timestamp
= 1; /* there's no automatic reloading */
772 wMenuEntryRemoveCascade(menu
, entry
);
773 wMenuEntrySetCascade(menu
, entry
, submenu
);
777 while (path
[i
] != NULL
)
785 static void cleanupWorkspaceMenu(WMenu
*menu
)
787 if (w_global
.workspace
.menu
== menu
)
788 w_global
.workspace
.menu
= NULL
;
791 static WMenuEntry
*addWorkspaceMenu(WScreen
*scr
, WMenu
*menu
, const char *title
)
796 if (scr
->flags
.added_workspace_menu
) {
798 ("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
801 scr
->flags
.added_workspace_menu
= 1;
803 wsmenu
= wWorkspaceMenuMake(scr
, True
);
804 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
806 w_global
.workspace
.menu
= wsmenu
;
807 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
808 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
810 wWorkspaceMenuUpdate(wsmenu
);
815 static void cleanupWindowsMenu(WMenu
* menu
)
817 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
818 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
821 static WMenuEntry
*addWindowsMenu(WScreen
*scr
, WMenu
*menu
, const char *title
)
827 if (scr
->flags
.added_windows_menu
) {
829 ("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
832 scr
->flags
.added_windows_menu
= 1;
834 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
835 wwmenu
->on_destroy
= cleanupWindowsMenu
;
836 scr
->switch_menu
= wwmenu
;
837 wwin
= scr
->focused_window
;
839 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
843 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
844 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
849 static WMenuEntry
*addMenuEntry(WMenu
*menu
, const char *title
, const char *shortcut
, const char *command
,
850 const char *params
, const char *file_name
)
853 WMenuEntry
*entry
= NULL
;
854 Bool shortcutOk
= False
;
858 scr
= menu
->frame
->screen_ptr
;
859 if (strcmp(command
, "OPEN_MENU") == 0) {
861 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
866 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
868 path
= wstrdup(params
);
870 dummy
= wMenuCreate(scr
, title
, False
);
871 dummy
->on_destroy
= removeShortcutsForMenu
;
872 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
873 entry
->free_cdata
= wfree
;
874 wMenuEntrySetCascade(menu
, entry
, dummy
);
876 } else if (strcmp(command
, "OPEN_PLMENU") == 0) {
878 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
883 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
885 path
= wstrdup(params
);
887 dummy
= wMenuCreate(scr
, title
, False
);
888 dummy
->on_destroy
= removeShortcutsForMenu
;
889 entry
= wMenuAddCallback(menu
, title
, constructPLMenuFromPipe
, path
);
890 entry
->free_cdata
= wfree
;
891 wMenuEntrySetCascade(menu
, entry
, dummy
);
893 } else if (strcmp(command
, "EXEC") == 0) {
895 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
897 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrconcat("exec ", params
));
898 entry
->free_cdata
= wfree
;
901 } else if (strcmp(command
, "SHEXEC") == 0) {
903 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
905 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrdup(params
));
906 entry
->free_cdata
= wfree
;
909 } else if (strcmp(command
, "EXIT") == 0) {
911 if (params
&& strcmp(params
, "QUICK") == 0)
912 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void *)M_QUICK
);
914 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
917 } else if (strcmp(command
, "SHUTDOWN") == 0) {
919 if (params
&& strcmp(params
, "QUICK") == 0)
920 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, (void *)M_QUICK
);
922 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
925 } else if (strcmp(command
, "REFRESH") == 0) {
926 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
929 } else if (strcmp(command
, "WORKSPACE_MENU") == 0) {
930 entry
= addWorkspaceMenu(scr
, menu
, title
);
933 } else if (strcmp(command
, "WINDOWS_MENU") == 0) {
934 entry
= addWindowsMenu(scr
, menu
, title
);
937 } else if (strcmp(command
, "ARRANGE_ICONS") == 0) {
938 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
941 } else if (strcmp(command
, "HIDE_OTHERS") == 0) {
942 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
945 } else if (strcmp(command
, "SHOW_ALL") == 0) {
946 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
949 } else if (strcmp(command
, "RESTART") == 0) {
950 entry
= wMenuAddCallback(menu
, title
, restartCommand
, params
? wstrdup(params
) : NULL
);
951 entry
->free_cdata
= wfree
;
953 } else if (strcmp(command
, "SAVE_SESSION") == 0) {
954 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
957 } else if (strcmp(command
, "CLEAR_SESSION") == 0) {
958 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
960 } else if (strcmp(command
, "INFO_PANEL") == 0) {
961 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
963 } else if (strcmp(command
, "LEGAL_PANEL") == 0) {
964 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
967 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
, command
);
972 if (shortcut
&& entry
) {
974 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
, title
);
976 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
978 entry
->rtext
= GetShortcutString(shortcut
);
980 entry->rtext = wstrdup(shortcut);
989 /******************* Menu Configuration From File *******************/
991 static void freeline(char *title
, char *command
, char *parameter
, char *shortcut
)
999 static WMenu
*parseCascade(WScreen
* scr
, WMenu
* menu
, WMenuParser parser
)
1001 char *command
, *params
, *shortcut
, *title
;
1003 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
1005 if (command
== NULL
|| !command
[0]) {
1006 WMenuParserError(parser
, _("missing command in menu config") );
1007 freeline(title
, command
, params
, shortcut
);
1011 if (strcasecmp(command
, "MENU") == 0) {
1016 cascade
= wMenuCreate(scr
, M_(title
), False
);
1017 cascade
->on_destroy
= removeShortcutsForMenu
;
1018 if (!parseCascade(scr
, cascade
, parser
)) {
1019 wMenuDestroy(cascade
, True
);
1021 wMenuEntrySetCascade(menu
, wMenuAddCallback(menu
, M_(title
), NULL
, NULL
), cascade
);
1023 } else if (strcasecmp(command
, "END") == 0) {
1025 freeline(title
, command
, params
, shortcut
);
1029 addMenuEntry(menu
, M_(title
), shortcut
, command
, params
, WMenuParserGetFilename(parser
));
1031 freeline(title
, command
, params
, shortcut
);
1034 WMenuParserError(parser
, _("syntax error in menu file: END declaration missing") );
1040 static WMenu
*readMenuFile(WScreen
*scr
, const char *file_name
)
1045 char *command
, *params
, *shortcut
, *title
;
1047 file
= fopen(file_name
, "rb");
1049 werror(_("%s:could not open menu file"), file_name
);
1052 parser
= WMenuParserCreate(file_name
, file
, DEF_CONFIG_PATHS
);
1053 menu_parser_register_macros(parser
);
1055 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
1057 if (command
== NULL
|| !command
[0]) {
1058 WMenuParserError(parser
, _("missing command in menu config") );
1059 freeline(title
, command
, params
, shortcut
);
1062 if (strcasecmp(command
, "MENU") == 0) {
1063 menu
= wMenuCreate(scr
, M_(title
), True
);
1064 menu
->on_destroy
= removeShortcutsForMenu
;
1065 if (!parseCascade(scr
, menu
, parser
)) {
1066 wMenuDestroy(menu
, True
);
1069 freeline(title
, command
, params
, shortcut
);
1072 WMenuParserError(parser
, _("invalid menu file, MENU command is missing") );
1073 freeline(title
, command
, params
, shortcut
);
1076 freeline(title
, command
, params
, shortcut
);
1079 WMenuParserDelete(parser
);
1085 /************ Menu Configuration From Pipe *************/
1086 static WMenu
*readPLMenuPipe(WScreen
* scr
, char **file_name
)
1088 WMPropList
*plist
= NULL
;
1091 char flat_file
[MAXLINE
];
1094 flat_file
[0] = '\0';
1096 for (i
= 0; file_name
[i
] != NULL
; i
++) {
1097 strcat(flat_file
, file_name
[i
]);
1098 strcat(flat_file
, " ");
1100 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1102 plist
= WMReadPropListFromPipe(filename
);
1107 menu
= configureMenu(scr
, plist
, False
);
1109 WMReleasePropList(plist
);
1114 menu
->on_destroy
= removeShortcutsForMenu
;
1118 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
)
1123 char *command
, *params
, *shortcut
, *title
;
1125 char flat_file
[MAXLINE
];
1128 flat_file
[0] = '\0';
1130 for (i
= 0; file_name
[i
] != NULL
; i
++) {
1131 strcat(flat_file
, file_name
[i
]);
1132 strcat(flat_file
, " ");
1134 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1136 file
= popen(filename
, "r");
1138 werror(_("%s:could not open menu file"), filename
);
1141 parser
= WMenuParserCreate(flat_file
, file
, DEF_CONFIG_PATHS
);
1142 menu_parser_register_macros(parser
);
1144 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
1146 if (command
== NULL
|| !command
[0]) {
1147 WMenuParserError(parser
, _("missing command in menu config") );
1148 freeline(title
, command
, params
, shortcut
);
1151 if (strcasecmp(command
, "MENU") == 0) {
1152 menu
= wMenuCreate(scr
, M_(title
), True
);
1153 menu
->on_destroy
= removeShortcutsForMenu
;
1154 if (!parseCascade(scr
, menu
, parser
)) {
1155 wMenuDestroy(menu
, True
);
1158 freeline(title
, command
, params
, shortcut
);
1161 WMenuParserError(parser
, _("no title given for the root menu") );
1162 freeline(title
, command
, params
, shortcut
);
1166 freeline(title
, command
, params
, shortcut
);
1169 WMenuParserDelete(parser
);
1180 static int myCompare(const void *d1
, const void *d2
)
1182 dir_data
*p1
= *(dir_data
**) d1
;
1183 dir_data
*p2
= *(dir_data
**) d2
;
1185 return strcmp(p1
->name
, p2
->name
);
1188 /***** Preset some macro for file parser *****/
1189 static void menu_parser_register_macros(WMenuParser parser
)
1194 // Used to return CPP verion, now returns wmaker's version
1195 WMenuParserRegisterSimpleMacro(parser
, "__VERSION__", VERSION
);
1197 // All macros below were historically defined by WindowMaker
1198 visual
= DefaultVisual(dpy
, DefaultScreen(dpy
));
1199 snprintf(buf
, sizeof(buf
), "%d", visual
->class);
1200 WMenuParserRegisterSimpleMacro(parser
, "VISUAL", buf
);
1202 snprintf(buf
, sizeof(buf
), "%d", DefaultDepth(dpy
, DefaultScreen(dpy
)) );
1203 WMenuParserRegisterSimpleMacro(parser
, "DEPTH", buf
);
1205 snprintf(buf
, sizeof(buf
), "%d", WidthOfScreen(DefaultScreenOfDisplay(dpy
)) );
1206 WMenuParserRegisterSimpleMacro(parser
, "SCR_WIDTH", buf
);
1208 snprintf(buf
, sizeof(buf
), "%d", HeightOfScreen(DefaultScreenOfDisplay(dpy
)) );
1209 WMenuParserRegisterSimpleMacro(parser
, "SCR_HEIGHT", buf
);
1211 WMenuParserRegisterSimpleMacro(parser
, "DISPLAY", XDisplayName(DisplayString(dpy
)) );
1213 WMenuParserRegisterSimpleMacro(parser
, "WM_VERSION", "\"" VERSION
"\"");
1216 /************ Menu Configuration From Directory *************/
1218 static Bool
isFilePackage(const char *file
)
1222 /* check if the extension indicates this file is a
1223 * file package. For now, only recognize .themed */
1227 if (l
> 7 && strcmp(&(file
[l
- 7]), ".themed") == 0) {
1234 static WMenu
*readMenuDirectory(WScreen
*scr
, const char *title
, char **path
, const char *command
)
1237 struct dirent
*dentry
;
1238 struct stat stat_buf
;
1241 WMArray
*dirs
= NULL
, *files
= NULL
;
1242 WMArrayIterator iter
;
1243 int length
, i
, have_space
= 0;
1245 int stripExtension
= 0;
1247 dirs
= WMCreateArray(16);
1248 files
= WMCreateArray(16);
1251 while (path
[i
] != NULL
) {
1252 if (strcmp(path
[i
], "-noext") == 0) {
1258 dir
= opendir(path
[i
]);
1264 while ((dentry
= readdir(dir
))) {
1266 if (strcmp(dentry
->d_name
, ".") == 0 || strcmp(dentry
->d_name
, "..") == 0)
1269 if (dentry
->d_name
[0] == '.')
1272 buffer
= malloc(strlen(path
[i
]) + strlen(dentry
->d_name
) + 4);
1274 werror(_("out of memory while constructing directory menu %s"), path
[i
]);
1278 strcpy(buffer
, path
[i
]);
1279 strcat(buffer
, "/");
1280 strcat(buffer
, dentry
->d_name
);
1282 if (stat(buffer
, &stat_buf
) != 0) {
1283 werror(_("%s:could not stat file \"%s\" in menu directory"),
1284 path
[i
], dentry
->d_name
);
1286 Bool isFilePack
= False
;
1289 if (S_ISDIR(stat_buf
.st_mode
)
1290 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1292 /* access always returns success for user root */
1293 if (access(buffer
, X_OK
) == 0) {
1294 /* Directory is accesible. Add to directory list */
1296 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1297 data
->name
= wstrdup(dentry
->d_name
);
1300 WMAddToArray(dirs
, data
);
1302 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1303 /* Hack because access always returns X_OK success for user root */
1304 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1305 if ((command
!= NULL
&& access(buffer
, R_OK
) == 0) ||
1306 (command
== NULL
&& access(buffer
, X_OK
) == 0 &&
1307 (stat_buf
.st_mode
& S_IXANY
))) {
1309 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1310 data
->name
= wstrdup(dentry
->d_name
);
1313 WMAddToArray(files
, data
);
1324 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1330 WMSortArray(dirs
, myCompare
);
1331 WMSortArray(files
, myCompare
);
1333 menu
= wMenuCreate(scr
, M_(title
), False
);
1334 menu
->on_destroy
= removeShortcutsForMenu
;
1336 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1337 /* New directory. Use same OPEN_MENU command that was used
1338 * for the current directory. */
1339 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1343 length
+= strlen(command
) + 6;
1344 buffer
= malloc(length
);
1346 werror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1352 strcat(buffer
, "-noext ");
1354 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1357 strcat(buffer
, "\"");
1358 strcat(buffer
, path
[data
->index
]);
1360 strcat(buffer
, "/");
1361 strcat(buffer
, data
->name
);
1363 strcat(buffer
, "\"");
1365 strcat(buffer
, " WITH ");
1366 strcat(buffer
, command
);
1369 addMenuEntry(menu
, M_(data
->name
), NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1377 WM_ITERATE_ARRAY(files
, data
, iter
) {
1378 /* executable: add as entry */
1379 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1381 length
+= strlen(command
);
1383 buffer
= malloc(length
);
1385 werror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1389 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1390 if (command
!= NULL
) {
1391 strcpy(buffer
, command
);
1392 strcat(buffer
, " ");
1394 strcat(buffer
, "\"");
1395 strcat(buffer
, path
[data
->index
]);
1400 strcat(buffer
, path
[data
->index
]);
1402 strcpy(buffer
, path
[data
->index
]);
1405 strcat(buffer
, "/");
1406 strcat(buffer
, data
->name
);
1408 strcat(buffer
, "\"");
1410 if (stripExtension
) {
1411 char *ptr
= strrchr(data
->name
, '.');
1412 if (ptr
&& ptr
!= data
->name
)
1415 addMenuEntry(menu
, M_(data
->name
), NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1429 /************ Menu Configuration From WMRootMenu *************/
1431 static WMenu
*makeDefaultMenu(WScreen
* scr
)
1435 menu
= wMenuCreate(scr
, _("Commands"), True
);
1436 wMenuAddCallback(menu
, M_("XTerm"), execCommand
, "xterm");
1437 wMenuAddCallback(menu
, M_("rxvt"), execCommand
, "rxvt");
1438 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1439 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1444 *----------------------------------------------------------------------
1446 * Reads root menu configuration from defaults database.
1448 *----------------------------------------------------------------------
1450 static WMenu
*configureMenu(WScreen
* scr
, WMPropList
* definition
, Bool includeGlobals
)
1455 WMPropList
*title
, *command
, *params
;
1458 if (WMIsPLString(definition
)) {
1459 struct stat stat_buf
;
1461 Bool menu_is_default
= False
;
1463 /* menu definition is a string. Probably a path, so parse the file */
1465 tmp
= wexpandpath(WMGetFromPLString(definition
));
1467 path
= getLocalizedMenuFile(tmp
);
1470 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1473 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1474 menu_is_default
= True
;
1478 werror(_("could not find menu file \"%s\" referenced in WMRootMenu"), tmp
);
1483 if (stat(path
, &stat_buf
) < 0) {
1484 werror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1490 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1491 /* if the pointer in WMRootMenu has changed */
1492 || w_global
.domain
.root_menu
->timestamp
> scr
->root_menu
->timestamp
) {
1494 if (menu_is_default
) {
1496 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1500 menu
= readMenuFile(scr
, path
);
1502 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, w_global
.domain
.root_menu
->timestamp
);
1512 count
= WMGetPropListItemCount(definition
);
1516 elem
= WMGetFromPLArray(definition
, 0);
1517 if (!WMIsPLString(elem
)) {
1518 tmp
= WMGetPropListDescription(elem
, False
);
1519 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1523 mtitle
= WMGetFromPLString(elem
);
1525 menu
= wMenuCreate(scr
, M_(mtitle
), False
);
1526 menu
->on_destroy
= removeShortcutsForMenu
;
1528 #ifdef GLOBAL_SUBMENU_FILE
1529 if (includeGlobals
) {
1533 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1536 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1537 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1541 /* Parameter not used, but tell the compiler that it is ok */
1542 (void) includeGlobals
;
1545 for (i
= 1; i
< count
; i
++) {
1546 elem
= WMGetFromPLArray(definition
, i
);
1548 if (WMIsPLString(elem
)) {
1551 file
= WMGetFromPLString(elem
);
1555 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1558 if (WMIsPLArray(WMGetFromPLArray(elem
, 1))) {
1563 submenu
= configureMenu(scr
, elem
, True
);
1565 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1566 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1570 WMPropList
*shortcut
;
1573 title
= WMGetFromPLArray(elem
, idx
++);
1574 shortcut
= WMGetFromPLArray(elem
, idx
++);
1575 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT") == 0) {
1576 shortcut
= WMGetFromPLArray(elem
, idx
++);
1577 command
= WMGetFromPLArray(elem
, idx
++);
1582 params
= WMGetFromPLArray(elem
, idx
++);
1584 if (!title
|| !command
)
1587 addMenuEntry(menu
, M_(WMGetFromPLString(title
)),
1588 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1589 WMGetFromPLString(command
),
1590 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1595 tmp
= WMGetPropListDescription(elem
, False
);
1596 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1604 *----------------------------------------------------------------------
1606 * Opens the root menu, parsing the menu configuration from the
1607 * defaults database.
1608 * If the menu is already mapped and is not sticked to the
1609 * root window, it will be unmapped.
1612 * The menu may be remade.
1615 * Construction of OPEN_MENU entries are delayed to the moment the
1617 *----------------------------------------------------------------------
1619 void OpenRootMenu(WScreen
* scr
, int x
, int y
, int keyboard
)
1622 WMPropList
*definition
;
1624 static WMPropList *domain=NULL;
1627 domain = WMCreatePLString("WMRootMenu");
1631 scr
->flags
.root_menu_changed_shortcuts
= 0;
1632 scr
->flags
.added_workspace_menu
= 0;
1633 scr
->flags
.added_windows_menu
= 0;
1635 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1636 menu
= scr
->root_menu
;
1637 if (!menu
->flags
.buttoned
) {
1640 wRaiseFrame(menu
->frame
->core
);
1643 wMenuMapAt(menu
, 0, 0, True
);
1645 wMenuMapCopyAt(menu
, x
- menu
->frame
->core
->width
/ 2, y
);
1650 definition
= w_global
.domain
.root_menu
->dictionary
;
1653 definition = PLGetDomain(domain);
1656 if (WMIsPLArray(definition
)) {
1657 if (!scr
->root_menu
|| w_global
.domain
.root_menu
->timestamp
> scr
->root_menu
->timestamp
) {
1658 menu
= configureMenu(scr
, definition
, True
);
1660 menu
->timestamp
= w_global
.domain
.root_menu
->timestamp
;
1665 menu
= configureMenu(scr
, definition
, True
);
1670 /* menu hasn't changed or could not be read */
1671 if (!scr
->root_menu
) {
1672 wMessageDialog(scr
, _("Error"),
1673 _("The applications menu could not be loaded. "
1674 "Look at the console output for a detailed "
1675 "description of the errors."), _("OK"), NULL
, NULL
);
1677 menu
= makeDefaultMenu(scr
);
1678 scr
->root_menu
= menu
;
1680 menu
= scr
->root_menu
;
1683 if (scr
->root_menu
) {
1684 wMenuDestroy(scr
->root_menu
, True
);
1686 scr
->root_menu
= menu
;
1691 if (keyboard
&& x
== 0 && y
== 0) {
1693 } else if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
1694 newx
= x
- menu
->frame
->core
->width
/ 2;
1695 newy
= y
- menu
->frame
->core
->height
/ 2;
1697 newx
= x
- menu
->frame
->core
->width
/ 2;
1700 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1703 if (scr
->flags
.root_menu_changed_shortcuts
)
1704 rebindKeygrabs(scr
);