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 extern WDDomain
*WDRootMenu
;
65 extern Cursor wCursor
[WCUR_LAST
];
67 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
);
68 static WMenu
*readPLMenuPipe(WScreen
* scr
, char **file_name
);
69 static WMenu
*readMenuFile(WScreen
*scr
, const char *file_name
);
70 static WMenu
*readMenuDirectory(WScreen
*scr
, const char *title
, char **file_name
, const char *command
);
71 static WMenu
*configureMenu(WScreen
* scr
, WMPropList
* definition
, Bool includeGlobals
);
72 static void menu_parser_register_macros(WMenuParser parser
);
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 * OPEN_PLMENU | command
126 * - opens command and uses its stdout which must be in proplist
127 * fromat to construct and insert the resulting menu in current
129 * The space between '|' and command is optional.
130 * || will do the same, but will not cache the contents.
131 * SAVE_SESSION - saves the current state of the desktop, which include
132 * all running applications, all their hints (geometry,
133 * position on screen, workspace they live on, the dock
134 * or clip from where they were launched, and
135 * if minimized, shaded or hidden. Also saves the current
136 * workspace the user is on. All will be restored on every
137 * start of windowmaker until another SAVE_SESSION or
138 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
139 * WindowMaker domain file, then saving is automatically
140 * done on every windowmaker exit, overwriting any
141 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
143 * CLEAR_SESSION - clears any previous saved session. This will not have
144 * any effect if SaveSessionOnExit is True.
152 static void execCommand(WMenu
* menu
, WMenuEntry
* entry
)
156 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char *)entry
->clientdata
);
158 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
159 GrabModeAsync
, GrabModeAsync
, None
, wCursor
[WCUR_WAIT
], CurrentTime
);
163 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
166 XUngrabPointer(dpy
, CurrentTime
);
170 static void exitCommand(WMenu
* menu
, WMenuEntry
* entry
)
172 static int inside
= 0;
175 /* prevent reentrant calls */
185 if ((long)entry
->clientdata
== M_QUICK
) {
188 int r
, oldSaveSessionFlag
;
190 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
191 r
= wExitDialog(menu
->frame
->screen_ptr
, _("Exit"),
192 _("Exit window manager?"), _("Exit"), _("Cancel"), NULL
);
194 if (r
== WAPRDefault
) {
196 } else if (r
== WAPRAlternate
) {
197 /* Don't modify the "save session on exit" flag if the
198 * user canceled the operation. */
199 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
202 if (result
== R_EXIT
)
203 Shutdown(WSExitMode
);
210 static void shutdownCommand(WMenu
* menu
, WMenuEntry
* entry
)
212 static int inside
= 0;
215 /* prevent reentrant calls */
225 if ((long)entry
->clientdata
== M_QUICK
)
228 int r
, oldSaveSessionFlag
;
230 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
232 r
= wExitDialog(menu
->frame
->screen_ptr
,
234 _("Kill Window System session?\n"
235 "(all applications will be closed)"), _("Kill"), _("Cancel"), NULL
);
236 if (r
== WAPRDefault
) {
238 } else if (r
== WAPRAlternate
) {
239 /* Don't modify the "save session on exit" flag if the
240 * user canceled the operation. */
241 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
245 if (result
!= R_CANCEL
) {
246 Shutdown(WSKillMode
);
254 static void restartCommand(WMenu
* menu
, WMenuEntry
* entry
)
256 Shutdown(WSRestartPreparationMode
);
257 Restart((char *)entry
->clientdata
, False
);
261 static void refreshCommand(WMenu
* menu
, WMenuEntry
* entry
)
263 wRefreshDesktop(menu
->frame
->screen_ptr
);
266 static void arrangeIconsCommand(WMenu
* menu
, WMenuEntry
* entry
)
268 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
271 static void showAllCommand(WMenu
* menu
, WMenuEntry
* entry
)
273 wShowAllWindows(menu
->frame
->screen_ptr
);
276 static void hideOthersCommand(WMenu
* menu
, WMenuEntry
* entry
)
278 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
281 static void saveSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
283 if (!wPreferences
.save_session_on_exit
)
284 wSessionSaveState(menu
->frame
->screen_ptr
);
286 wScreenSaveState(menu
->frame
->screen_ptr
);
289 static void clearSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
291 wSessionClearState(menu
->frame
->screen_ptr
);
292 wScreenSaveState(menu
->frame
->screen_ptr
);
295 static void infoPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
297 wShowInfoPanel(menu
->frame
->screen_ptr
);
300 static void legalPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
302 wShowLegalPanel(menu
->frame
->screen_ptr
);
305 /********************************************************************/
307 static char *getLocalizedMenuFile(const char *menu
)
309 char *buffer
, *ptr
, *locale
;
315 len
= strlen(menu
) + strlen(Locale
) + 8;
316 buffer
= wmalloc(len
);
318 /* try menu.locale_name */
319 snprintf(buffer
, len
, "%s.%s", menu
, Locale
);
320 if (access(buffer
, F_OK
) == 0)
323 /* position of locale in our buffer */
324 locale
= buffer
+ strlen(menu
) + 1;
326 /* check if it is in the form aa_bb.encoding and check for aa_bb */
327 ptr
= strchr(locale
, '.');
330 if (access(buffer
, F_OK
) == 0)
334 /* now check for aa */
335 ptr
= strchr(locale
, '_');
338 if (access(buffer
, F_OK
) == 0)
347 Bool
wRootMenuPerformShortcut(XEvent
* event
)
349 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
354 /* ignore CapsLock */
355 modifiers
= event
->xkey
.state
& ValidModMask
;
357 for (ptr
= shortcutList
; ptr
!= NULL
; ptr
= ptr
->next
) {
358 if (ptr
->keycode
== 0 || ptr
->menu
->menu
->screen_ptr
!= scr
)
361 if (ptr
->keycode
== event
->xkey
.keycode
&& ptr
->modifier
== modifiers
) {
362 (*ptr
->entry
->callback
) (ptr
->menu
, ptr
->entry
);
370 void wRootMenuBindShortcuts(Window window
)
376 if (ptr
->modifier
!= AnyModifier
) {
377 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
| LockMask
,
378 window
, True
, GrabModeAsync
, GrabModeAsync
);
380 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
383 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
388 static void rebindKeygrabs(WScreen
* scr
)
392 wwin
= scr
->focused_window
;
394 while (wwin
!= NULL
) {
395 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
397 if (!WFLAGP(wwin
, no_bind_keys
)) {
398 wWindowSetKeyGrabs(wwin
);
404 static void removeShortcutsForMenu(WMenu
* menu
)
407 Shortcut
*newList
= NULL
;
410 while (ptr
!= NULL
) {
412 if (ptr
->menu
== menu
) {
420 shortcutList
= newList
;
421 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
424 static Bool
addShortcut(const char *file
, const char *shortcutDefinition
, WMenu
*menu
, WMenuEntry
*entry
)
429 char buf
[MAX_SHORTCUT_LENGTH
], *b
;
431 ptr
= wmalloc(sizeof(Shortcut
));
433 wstrlcpy(buf
, shortcutDefinition
, MAX_SHORTCUT_LENGTH
);
438 while ((k
= strchr(b
, '+')) != NULL
) {
442 mod
= wXModifierFromKey(b
);
444 wwarning(_("%s: invalid key modifier \"%s\""), file
, b
);
448 ptr
->modifier
|= mod
;
454 ksym
= XStringToKeysym(b
);
456 if (ksym
== NoSymbol
) {
457 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
458 file
, shortcutDefinition
, entry
->text
);
463 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
464 if (ptr
->keycode
== 0) {
465 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
466 shortcutDefinition
, entry
->text
);
474 ptr
->next
= shortcutList
;
477 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
482 static char *next_token(char *line
, char **next
)
488 while (*line
== ' ' || *line
== '\t')
496 while (*tmp
!= 0 && *tmp
!= '"')
499 wwarning(_("%s: unmatched '\"' in menu file"), line
);
510 } while (*tmp
!= 0 && *tmp
!= ' ' && *tmp
!= '\t');
524 while (*tmp
== ' ' || *tmp
== '\t')
533 static void separateCommand(char *line
, char ***file
, char **command
)
535 char *token
, *tmp
= line
;
536 WMArray
*array
= WMCreateArray(4);
542 token
= next_token(tmp
, &tmp
);
544 if (strcmp(token
, "WITH") == 0) {
545 if (tmp
!= NULL
&& *tmp
!= 0)
546 *command
= wstrdup(tmp
);
548 wwarning(_("%s: missing command"), line
);
551 WMAddToArray(array
, token
);
553 } while (token
!= NULL
&& tmp
!= NULL
);
555 count
= WMGetArrayItemCount(array
);
557 *file
= wmalloc(sizeof(char *) * (count
+ 1));
558 (*file
)[count
] = NULL
;
559 for (i
= 0; i
< count
; i
++) {
560 (*file
)[i
] = WMGetFromArray(array
, i
);
566 static WMenu
*constructPLMenu(WScreen
*screen
, const char *path
)
568 WMPropList
*pl
= NULL
;
574 pl
= WMReadPropListFromFile(path
);
578 menu
= configureMenu(screen
, pl
, False
);
580 WMReleasePropList(pl
);
585 menu
->on_destroy
= removeShortcutsForMenu
;
591 static void constructMenu(WMenu
* menu
, WMenuEntry
* entry
)
594 struct stat stat_buf
;
601 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
602 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
603 wwarning(_("invalid OPEN_MENU specification: %s"), (char *)entry
->clientdata
);
609 if (path
[0][0] == '|') {
612 if (!menu
->cascades
[entry
->cascade
] || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
615 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
617 if (submenu
!= NULL
) {
618 if (path
[0][1] == '|')
619 submenu
->timestamp
= 0;
621 submenu
->timestamp
= 1; /* there's no automatic reloading */
629 /* try interpreting path as a proplist file */
630 submenu
= constructPLMenu(menu
->frame
->screen_ptr
, path
[0]);
631 /* if unsuccessful, try it as an old-style file */
635 while (path
[i
] != NULL
) {
638 if (strcmp(path
[i
], "-noext") == 0) {
643 tmp
= wexpandpath(path
[i
]);
645 lpath
= getLocalizedMenuFile(tmp
);
654 if (stat(path
[i
], &stat_buf
) == 0) {
655 if (last
< stat_buf
.st_mtime
)
656 last
= stat_buf
.st_mtime
;
660 werror(_("%s:could not stat menu"), path
[i
]);
668 werror(_("%s:could not stat menu:%s"), "OPEN_MENU", (char *)entry
->clientdata
);
671 stat(path
[first
], &stat_buf
);
672 if (!menu
->cascades
[entry
->cascade
]
673 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
675 if (S_ISDIR(stat_buf
.st_mode
)) {
677 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
, entry
->text
, path
, cmd
);
679 submenu
->timestamp
= last
;
680 } else if (S_ISREG(stat_buf
.st_mode
)) {
684 wwarning(_("too many parameters in OPEN_MENU: %s"),
685 (char *)entry
->clientdata
);
687 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
689 submenu
->timestamp
= stat_buf
.st_mtime
;
700 wMenuEntryRemoveCascade(menu
, entry
);
701 wMenuEntrySetCascade(menu
, entry
, submenu
);
706 while (path
[i
] != NULL
)
713 static void constructPLMenuFromPipe(WMenu
* menu
, WMenuEntry
* entry
)
715 WMenu
*submenu
= NULL
;
720 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
721 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
722 wwarning(_("invalid OPEN_PLMENU specification: %s"),
723 (char *)entry
->clientdata
);
729 if (path
[0][0] == '|') {
732 if (!menu
->cascades
[entry
->cascade
]
733 || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
735 submenu
= readPLMenuPipe(menu
->frame
->screen_ptr
, path
);
737 if (submenu
!= NULL
) {
738 if (path
[0][1] == '|')
739 submenu
->timestamp
= 0;
741 submenu
->timestamp
= 1; /* there's no automatic reloading */
747 wMenuEntryRemoveCascade(menu
, entry
);
748 wMenuEntrySetCascade(menu
, entry
, submenu
);
752 while (path
[i
] != NULL
)
760 static void cleanupWorkspaceMenu(WMenu
* menu
)
762 if (menu
->frame
->screen_ptr
->workspace_menu
== menu
)
763 menu
->frame
->screen_ptr
->workspace_menu
= NULL
;
766 static WMenuEntry
*addWorkspaceMenu(WScreen
*scr
, WMenu
*menu
, const char *title
)
771 if (scr
->flags
.added_workspace_menu
) {
773 ("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
776 scr
->flags
.added_workspace_menu
= 1;
778 wsmenu
= wWorkspaceMenuMake(scr
, True
);
779 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
781 scr
->workspace_menu
= wsmenu
;
782 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
783 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
785 wWorkspaceMenuUpdate(scr
, wsmenu
);
790 static void cleanupWindowsMenu(WMenu
* menu
)
792 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
793 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
796 static WMenuEntry
*addWindowsMenu(WScreen
*scr
, WMenu
*menu
, const char *title
)
802 if (scr
->flags
.added_windows_menu
) {
804 ("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
807 scr
->flags
.added_windows_menu
= 1;
809 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
810 wwmenu
->on_destroy
= cleanupWindowsMenu
;
811 scr
->switch_menu
= wwmenu
;
812 wwin
= scr
->focused_window
;
814 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
818 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
819 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
824 static WMenuEntry
*addMenuEntry(WMenu
*menu
, const char *title
, const char *shortcut
, const char *command
,
825 const char *params
, const char *file_name
)
828 WMenuEntry
*entry
= NULL
;
829 Bool shortcutOk
= False
;
833 scr
= menu
->frame
->screen_ptr
;
834 if (strcmp(command
, "OPEN_MENU") == 0) {
836 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
841 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
843 path
= wstrdup(params
);
845 dummy
= wMenuCreate(scr
, title
, False
);
846 dummy
->on_destroy
= removeShortcutsForMenu
;
847 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
848 entry
->free_cdata
= wfree
;
849 wMenuEntrySetCascade(menu
, entry
, dummy
);
851 } else if (strcmp(command
, "OPEN_PLMENU") == 0) {
853 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
858 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
860 path
= wstrdup(params
);
862 dummy
= wMenuCreate(scr
, title
, False
);
863 dummy
->on_destroy
= removeShortcutsForMenu
;
864 entry
= wMenuAddCallback(menu
, title
, constructPLMenuFromPipe
, path
);
865 entry
->free_cdata
= wfree
;
866 wMenuEntrySetCascade(menu
, entry
, dummy
);
868 } else if (strcmp(command
, "EXEC") == 0) {
870 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
872 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrconcat("exec ", params
));
873 entry
->free_cdata
= wfree
;
876 } else if (strcmp(command
, "SHEXEC") == 0) {
878 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
880 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrdup(params
));
881 entry
->free_cdata
= wfree
;
884 } else if (strcmp(command
, "EXIT") == 0) {
886 if (params
&& strcmp(params
, "QUICK") == 0)
887 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void *)M_QUICK
);
889 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
892 } else if (strcmp(command
, "SHUTDOWN") == 0) {
894 if (params
&& strcmp(params
, "QUICK") == 0)
895 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, (void *)M_QUICK
);
897 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
900 } else if (strcmp(command
, "REFRESH") == 0) {
901 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
904 } else if (strcmp(command
, "WORKSPACE_MENU") == 0) {
905 entry
= addWorkspaceMenu(scr
, menu
, title
);
908 } else if (strcmp(command
, "WINDOWS_MENU") == 0) {
909 entry
= addWindowsMenu(scr
, menu
, title
);
912 } else if (strcmp(command
, "ARRANGE_ICONS") == 0) {
913 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
916 } else if (strcmp(command
, "HIDE_OTHERS") == 0) {
917 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
920 } else if (strcmp(command
, "SHOW_ALL") == 0) {
921 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
924 } else if (strcmp(command
, "RESTART") == 0) {
925 entry
= wMenuAddCallback(menu
, title
, restartCommand
, params
? wstrdup(params
) : NULL
);
926 entry
->free_cdata
= wfree
;
928 } else if (strcmp(command
, "SAVE_SESSION") == 0) {
929 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
932 } else if (strcmp(command
, "CLEAR_SESSION") == 0) {
933 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
935 } else if (strcmp(command
, "INFO_PANEL") == 0) {
936 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
938 } else if (strcmp(command
, "LEGAL_PANEL") == 0) {
939 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
942 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
, command
);
947 if (shortcut
&& entry
) {
949 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
, title
);
951 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
953 entry
->rtext
= GetShortcutString(shortcut
);
955 entry->rtext = wstrdup(shortcut);
964 /******************* Menu Configuration From File *******************/
966 static void freeline(char *title
, char *command
, char *parameter
, char *shortcut
)
974 static WMenu
*parseCascade(WScreen
* scr
, WMenu
* menu
, WMenuParser parser
)
976 char *command
, *params
, *shortcut
, *title
;
978 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
980 if (command
== NULL
|| !command
[0]) {
981 WMenuParserError(parser
, _("missing command in menu config") );
982 freeline(title
, command
, params
, shortcut
);
986 if (strcasecmp(command
, "MENU") == 0) {
991 cascade
= wMenuCreate(scr
, M_(title
), False
);
992 cascade
->on_destroy
= removeShortcutsForMenu
;
993 if (!parseCascade(scr
, cascade
, parser
)) {
994 wMenuDestroy(cascade
, True
);
996 wMenuEntrySetCascade(menu
, wMenuAddCallback(menu
, M_(title
), NULL
, NULL
), cascade
);
998 } else if (strcasecmp(command
, "END") == 0) {
1000 freeline(title
, command
, params
, shortcut
);
1004 addMenuEntry(menu
, M_(title
), shortcut
, command
, params
, WMenuParserGetFilename(parser
));
1006 freeline(title
, command
, params
, shortcut
);
1009 WMenuParserError(parser
, _("syntax error in menu file: END declaration missing") );
1015 static WMenu
*readMenuFile(WScreen
*scr
, const char *file_name
)
1020 char *command
, *params
, *shortcut
, *title
;
1022 file
= fopen(file_name
, "rb");
1024 werror(_("%s:could not open menu file"), file_name
);
1027 parser
= WMenuParserCreate(file_name
, file
, DEF_CONFIG_PATHS
);
1028 menu_parser_register_macros(parser
);
1030 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
1032 if (command
== NULL
|| !command
[0]) {
1033 WMenuParserError(parser
, _("missing command in menu config") );
1034 freeline(title
, command
, params
, shortcut
);
1037 if (strcasecmp(command
, "MENU") == 0) {
1038 menu
= wMenuCreate(scr
, M_(title
), True
);
1039 menu
->on_destroy
= removeShortcutsForMenu
;
1040 if (!parseCascade(scr
, menu
, parser
)) {
1041 wMenuDestroy(menu
, True
);
1044 freeline(title
, command
, params
, shortcut
);
1047 WMenuParserError(parser
, _("invalid menu file, MENU command is missing") );
1048 freeline(title
, command
, params
, shortcut
);
1051 freeline(title
, command
, params
, shortcut
);
1054 WMenuParserDelete(parser
);
1060 /************ Menu Configuration From Pipe *************/
1061 static WMenu
*readPLMenuPipe(WScreen
* scr
, char **file_name
)
1063 WMPropList
*plist
= NULL
;
1066 char flat_file
[MAXLINE
];
1069 flat_file
[0] = '\0';
1071 for (i
= 0; file_name
[i
] != NULL
; i
++) {
1072 strcat(flat_file
, file_name
[i
]);
1073 strcat(flat_file
, " ");
1075 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1077 plist
= WMReadPropListFromPipe(filename
);
1082 menu
= configureMenu(scr
, plist
, False
);
1084 WMReleasePropList(plist
);
1089 menu
->on_destroy
= removeShortcutsForMenu
;
1093 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
)
1098 char *command
, *params
, *shortcut
, *title
;
1100 char flat_file
[MAXLINE
];
1103 flat_file
[0] = '\0';
1105 for (i
= 0; file_name
[i
] != NULL
; i
++) {
1106 strcat(flat_file
, file_name
[i
]);
1107 strcat(flat_file
, " ");
1109 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1111 file
= popen(filename
, "r");
1113 werror(_("%s:could not open menu file"), filename
);
1116 parser
= WMenuParserCreate(flat_file
, file
, DEF_CONFIG_PATHS
);
1117 menu_parser_register_macros(parser
);
1119 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
1121 if (command
== NULL
|| !command
[0]) {
1122 WMenuParserError(parser
, _("missing command in menu config") );
1123 freeline(title
, command
, params
, shortcut
);
1126 if (strcasecmp(command
, "MENU") == 0) {
1127 menu
= wMenuCreate(scr
, M_(title
), True
);
1128 menu
->on_destroy
= removeShortcutsForMenu
;
1129 if (!parseCascade(scr
, menu
, parser
)) {
1130 wMenuDestroy(menu
, True
);
1133 freeline(title
, command
, params
, shortcut
);
1136 WMenuParserError(parser
, _("no title given for the root menu") );
1137 freeline(title
, command
, params
, shortcut
);
1141 freeline(title
, command
, params
, shortcut
);
1144 WMenuParserDelete(parser
);
1155 static int myCompare(const void *d1
, const void *d2
)
1157 dir_data
*p1
= *(dir_data
**) d1
;
1158 dir_data
*p2
= *(dir_data
**) d2
;
1160 return strcmp(p1
->name
, p2
->name
);
1163 /***** Preset some macro for file parser *****/
1164 static void menu_parser_register_macros(WMenuParser parser
)
1169 // Used to return CPP verion, now returns wmaker's version
1170 WMenuParserRegisterSimpleMacro(parser
, "__VERSION__", VERSION
);
1172 // All macros below were historically defined by WindowMaker
1173 visual
= DefaultVisual(dpy
, DefaultScreen(dpy
));
1174 snprintf(buf
, sizeof(buf
), "%d", visual
->class);
1175 WMenuParserRegisterSimpleMacro(parser
, "VISUAL", buf
);
1177 snprintf(buf
, sizeof(buf
), "%d", DefaultDepth(dpy
, DefaultScreen(dpy
)) );
1178 WMenuParserRegisterSimpleMacro(parser
, "DEPTH", buf
);
1180 snprintf(buf
, sizeof(buf
), "%d", WidthOfScreen(DefaultScreenOfDisplay(dpy
)) );
1181 WMenuParserRegisterSimpleMacro(parser
, "SCR_WIDTH", buf
);
1183 snprintf(buf
, sizeof(buf
), "%d", HeightOfScreen(DefaultScreenOfDisplay(dpy
)) );
1184 WMenuParserRegisterSimpleMacro(parser
, "SCR_HEIGHT", buf
);
1186 WMenuParserRegisterSimpleMacro(parser
, "DISPLAY", XDisplayName(DisplayString(dpy
)) );
1188 WMenuParserRegisterSimpleMacro(parser
, "WM_VERSION", "\"" VERSION
"\"");
1191 /************ Menu Configuration From Directory *************/
1193 static Bool
isFilePackage(const char *file
)
1197 /* check if the extension indicates this file is a
1198 * file package. For now, only recognize .themed */
1202 if (l
> 7 && strcmp(&(file
[l
- 7]), ".themed") == 0) {
1209 static WMenu
*readMenuDirectory(WScreen
*scr
, const char *title
, char **path
, const char *command
)
1212 struct dirent
*dentry
;
1213 struct stat stat_buf
;
1216 WMArray
*dirs
= NULL
, *files
= NULL
;
1217 WMArrayIterator iter
;
1218 int length
, i
, have_space
= 0;
1220 int stripExtension
= 0;
1222 dirs
= WMCreateArray(16);
1223 files
= WMCreateArray(16);
1226 while (path
[i
] != NULL
) {
1227 if (strcmp(path
[i
], "-noext") == 0) {
1233 dir
= opendir(path
[i
]);
1239 while ((dentry
= readdir(dir
))) {
1241 if (strcmp(dentry
->d_name
, ".") == 0 || strcmp(dentry
->d_name
, "..") == 0)
1244 if (dentry
->d_name
[0] == '.')
1247 buffer
= malloc(strlen(path
[i
]) + strlen(dentry
->d_name
) + 4);
1249 werror(_("out of memory while constructing directory menu %s"), path
[i
]);
1253 strcpy(buffer
, path
[i
]);
1254 strcat(buffer
, "/");
1255 strcat(buffer
, dentry
->d_name
);
1257 if (stat(buffer
, &stat_buf
) != 0) {
1258 werror(_("%s:could not stat file \"%s\" in menu directory"),
1259 path
[i
], dentry
->d_name
);
1261 Bool isFilePack
= False
;
1264 if (S_ISDIR(stat_buf
.st_mode
)
1265 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1267 /* access always returns success for user root */
1268 if (access(buffer
, X_OK
) == 0) {
1269 /* Directory is accesible. Add to directory list */
1271 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1272 data
->name
= wstrdup(dentry
->d_name
);
1275 WMAddToArray(dirs
, data
);
1277 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1278 /* Hack because access always returns X_OK success for user root */
1279 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1280 if ((command
!= NULL
&& access(buffer
, R_OK
) == 0) ||
1281 (command
== NULL
&& access(buffer
, X_OK
) == 0 &&
1282 (stat_buf
.st_mode
& S_IXANY
))) {
1284 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1285 data
->name
= wstrdup(dentry
->d_name
);
1288 WMAddToArray(files
, data
);
1299 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1305 WMSortArray(dirs
, myCompare
);
1306 WMSortArray(files
, myCompare
);
1308 menu
= wMenuCreate(scr
, M_(title
), False
);
1309 menu
->on_destroy
= removeShortcutsForMenu
;
1311 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1312 /* New directory. Use same OPEN_MENU command that was used
1313 * for the current directory. */
1314 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1318 length
+= strlen(command
) + 6;
1319 buffer
= malloc(length
);
1321 werror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1327 strcat(buffer
, "-noext ");
1329 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1332 strcat(buffer
, "\"");
1333 strcat(buffer
, path
[data
->index
]);
1335 strcat(buffer
, "/");
1336 strcat(buffer
, data
->name
);
1338 strcat(buffer
, "\"");
1340 strcat(buffer
, " WITH ");
1341 strcat(buffer
, command
);
1344 addMenuEntry(menu
, M_(data
->name
), NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1352 WM_ITERATE_ARRAY(files
, data
, iter
) {
1353 /* executable: add as entry */
1354 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1356 length
+= strlen(command
);
1358 buffer
= malloc(length
);
1360 werror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1364 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1365 if (command
!= NULL
) {
1366 strcpy(buffer
, command
);
1367 strcat(buffer
, " ");
1369 strcat(buffer
, "\"");
1370 strcat(buffer
, path
[data
->index
]);
1375 strcat(buffer
, path
[data
->index
]);
1377 strcpy(buffer
, path
[data
->index
]);
1380 strcat(buffer
, "/");
1381 strcat(buffer
, data
->name
);
1383 strcat(buffer
, "\"");
1385 if (stripExtension
) {
1386 char *ptr
= strrchr(data
->name
, '.');
1387 if (ptr
&& ptr
!= data
->name
)
1390 addMenuEntry(menu
, M_(data
->name
), NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1404 /************ Menu Configuration From WMRootMenu *************/
1406 static WMenu
*makeDefaultMenu(WScreen
* scr
)
1410 menu
= wMenuCreate(scr
, _("Commands"), True
);
1411 wMenuAddCallback(menu
, M_("XTerm"), execCommand
, "xterm");
1412 wMenuAddCallback(menu
, M_("rxvt"), execCommand
, "rxvt");
1413 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1414 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1419 *----------------------------------------------------------------------
1421 * Reads root menu configuration from defaults database.
1423 *----------------------------------------------------------------------
1425 static WMenu
*configureMenu(WScreen
* scr
, WMPropList
* definition
, Bool includeGlobals
)
1430 WMPropList
*title
, *command
, *params
;
1433 if (WMIsPLString(definition
)) {
1434 struct stat stat_buf
;
1436 Bool menu_is_default
= False
;
1438 /* menu definition is a string. Probably a path, so parse the file */
1440 tmp
= wexpandpath(WMGetFromPLString(definition
));
1442 path
= getLocalizedMenuFile(tmp
);
1445 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1448 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1449 menu_is_default
= True
;
1453 werror(_("could not find menu file \"%s\" referenced in WMRootMenu"), tmp
);
1458 if (stat(path
, &stat_buf
) < 0) {
1459 werror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1465 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1466 /* if the pointer in WMRootMenu has changed */
1467 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1469 if (menu_is_default
) {
1471 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1475 menu
= readMenuFile(scr
, path
);
1477 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1487 count
= WMGetPropListItemCount(definition
);
1491 elem
= WMGetFromPLArray(definition
, 0);
1492 if (!WMIsPLString(elem
)) {
1493 tmp
= WMGetPropListDescription(elem
, False
);
1494 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1498 mtitle
= WMGetFromPLString(elem
);
1500 menu
= wMenuCreate(scr
, M_(mtitle
), False
);
1501 menu
->on_destroy
= removeShortcutsForMenu
;
1503 #ifdef GLOBAL_SUBMENU_FILE
1504 if (includeGlobals
) {
1508 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1511 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1512 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1517 for (i
= 1; i
< count
; i
++) {
1518 elem
= WMGetFromPLArray(definition
, i
);
1520 if (WMIsPLString(elem
)) {
1523 file
= WMGetFromPLString(elem
);
1527 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1530 if (WMIsPLArray(WMGetFromPLArray(elem
, 1))) {
1535 submenu
= configureMenu(scr
, elem
, True
);
1537 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1538 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1542 WMPropList
*shortcut
;
1545 title
= WMGetFromPLArray(elem
, idx
++);
1546 shortcut
= WMGetFromPLArray(elem
, idx
++);
1547 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT") == 0) {
1548 shortcut
= WMGetFromPLArray(elem
, idx
++);
1549 command
= WMGetFromPLArray(elem
, idx
++);
1554 params
= WMGetFromPLArray(elem
, idx
++);
1556 if (!title
|| !command
)
1559 addMenuEntry(menu
, M_(WMGetFromPLString(title
)),
1560 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1561 WMGetFromPLString(command
),
1562 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1567 tmp
= WMGetPropListDescription(elem
, False
);
1568 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1576 *----------------------------------------------------------------------
1578 * Opens the root menu, parsing the menu configuration from the
1579 * defaults database.
1580 * If the menu is already mapped and is not sticked to the
1581 * root window, it will be unmapped.
1584 * The menu may be remade.
1587 * Construction of OPEN_MENU entries are delayed to the moment the
1589 *----------------------------------------------------------------------
1591 void OpenRootMenu(WScreen
* scr
, int x
, int y
, int keyboard
)
1594 WMPropList
*definition
;
1596 static WMPropList *domain=NULL;
1599 domain = WMCreatePLString("WMRootMenu");
1603 scr
->flags
.root_menu_changed_shortcuts
= 0;
1604 scr
->flags
.added_workspace_menu
= 0;
1605 scr
->flags
.added_windows_menu
= 0;
1607 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1608 menu
= scr
->root_menu
;
1609 if (!menu
->flags
.buttoned
) {
1612 wRaiseFrame(menu
->frame
->core
);
1615 wMenuMapAt(menu
, 0, 0, True
);
1617 wMenuMapCopyAt(menu
, x
- menu
->frame
->core
->width
/ 2, y
);
1622 definition
= WDRootMenu
->dictionary
;
1625 definition = PLGetDomain(domain);
1628 if (WMIsPLArray(definition
)) {
1629 if (!scr
->root_menu
|| WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1630 menu
= configureMenu(scr
, definition
, True
);
1632 menu
->timestamp
= WDRootMenu
->timestamp
;
1637 menu
= configureMenu(scr
, definition
, True
);
1642 /* menu hasn't changed or could not be read */
1643 if (!scr
->root_menu
) {
1644 wMessageDialog(scr
, _("Error"),
1645 _("The applications menu could not be loaded. "
1646 "Look at the console output for a detailed "
1647 "description of the errors."), _("OK"), NULL
, NULL
);
1649 menu
= makeDefaultMenu(scr
);
1650 scr
->root_menu
= menu
;
1652 menu
= scr
->root_menu
;
1655 if (scr
->root_menu
) {
1656 wMenuDestroy(scr
->root_menu
, True
);
1658 scr
->root_menu
= menu
;
1663 if (keyboard
&& x
== 0 && y
== 0) {
1665 } else if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
1666 newx
= x
- menu
->frame
->core
->width
/ 2;
1667 newy
= y
- menu
->frame
->core
->height
/ 2;
1669 newx
= x
- menu
->frame
->core
->width
/ 2;
1672 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1675 if (scr
->flags
.root_menu_changed_shortcuts
)
1676 rebindKeygrabs(scr
);