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
7 * Copyright (c) 2014-2023 Window Maker Team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/types.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xatom.h>
43 #include "WindowMaker.h"
51 #include "workspace.h"
56 #include "xmodifier.h"
59 #include "switchmenu.h"
61 #include <WINGs/WUtil.h>
63 #define MAX_SHORTCUT_LENGTH 32
65 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
);
66 static WMenu
*readPLMenuPipe(WScreen
* scr
, char **file_name
);
67 static WMenu
*readMenuFile(WScreen
*scr
, const char *file_name
);
68 static WMenu
*readMenuDirectory(WScreen
*scr
, const char *title
, char **file_name
, const char *command
);
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.
147 static void execCommand(WMenu
* menu
, WMenuEntry
* entry
)
149 ExecuteInputCommand(menu
->frame
->screen_ptr
, (char *)entry
->clientdata
);
152 static void exitCommand(WMenu
* menu
, WMenuEntry
* entry
)
154 ExecuteExitCommand(menu
->frame
->screen_ptr
, (long)entry
->clientdata
);
158 static void shutdownCommand(WMenu
* menu
, WMenuEntry
* entry
)
160 static int inside
= 0;
163 /* prevent reentrant calls */
173 if ((long)entry
->clientdata
== M_QUICK
)
176 int r
, oldSaveSessionFlag
;
178 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
180 r
= wExitDialog(menu
->frame
->screen_ptr
,
182 _("Kill Window System session?\n"
183 "(all applications will be closed)"), _("Kill"), _("Cancel"), NULL
);
184 if (r
== WAPRDefault
) {
186 } else if (r
== WAPRAlternate
) {
187 /* Don't modify the "save session on exit" flag if the
188 * user canceled the operation. */
189 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
193 if (result
!= R_CANCEL
) {
194 Shutdown(WSKillMode
);
202 static void restartCommand(WMenu
* menu
, WMenuEntry
* entry
)
204 /* Parameter not used, but tell the compiler that it is ok */
208 Shutdown(WSRestartPreparationMode
);
209 Restart((char *)entry
->clientdata
, False
);
213 static void refreshCommand(WMenu
* menu
, WMenuEntry
* entry
)
215 /* Parameter not used, but tell the compiler that it is ok */
218 wRefreshDesktop(menu
->frame
->screen_ptr
);
221 static void arrangeIconsCommand(WMenu
* menu
, WMenuEntry
* entry
)
223 /* Parameter not used, but tell the compiler that it is ok */
226 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
229 static void showAllCommand(WMenu
* menu
, WMenuEntry
* entry
)
231 /* Parameter not used, but tell the compiler that it is ok */
234 wShowAllWindows(menu
->frame
->screen_ptr
);
237 static void hideOthersCommand(WMenu
* menu
, WMenuEntry
* entry
)
239 /* Parameter not used, but tell the compiler that it is ok */
242 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
245 static void saveSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
247 /* Parameter not used, but tell the compiler that it is ok */
250 if (!wPreferences
.save_session_on_exit
)
251 wSessionSaveState(menu
->frame
->screen_ptr
);
253 wScreenSaveState(menu
->frame
->screen_ptr
);
256 static void clearSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
258 /* Parameter not used, but tell the compiler that it is ok */
261 wSessionClearState(menu
->frame
->screen_ptr
);
262 wScreenSaveState(menu
->frame
->screen_ptr
);
265 static void infoPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
267 /* Parameter not used, but tell the compiler that it is ok */
270 wShowInfoPanel(menu
->frame
->screen_ptr
);
273 static void legalPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
275 /* Parameter not used, but tell the compiler that it is ok */
278 wShowLegalPanel(menu
->frame
->screen_ptr
);
281 /********************************************************************/
283 static char *getLocalizedMenuFile(const char *menu
)
285 char *buffer
, *ptr
, *locale
;
288 if (!w_global
.locale
)
291 len
= strlen(menu
) + strlen(w_global
.locale
) + 8;
292 buffer
= wmalloc(len
);
294 /* try menu.locale_name */
295 snprintf(buffer
, len
, "%s.%s", menu
, w_global
.locale
);
296 if (access(buffer
, F_OK
) == 0)
299 /* position of locale in our buffer */
300 locale
= buffer
+ strlen(menu
) + 1;
302 /* check if it is in the form aa_bb.encoding and check for aa_bb */
303 ptr
= strchr(locale
, '.');
306 if (access(buffer
, F_OK
) == 0)
310 /* now check for aa */
311 ptr
= strchr(locale
, '_');
314 if (access(buffer
, F_OK
) == 0)
323 Bool
wRootMenuPerformShortcut(XEvent
* event
)
325 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
330 /* ignore CapsLock */
331 modifiers
= event
->xkey
.state
& w_global
.shortcut
.modifiers_mask
;
333 for (ptr
= shortcutList
; ptr
!= NULL
; ptr
= ptr
->next
) {
334 if (ptr
->keycode
== 0 || ptr
->menu
->menu
->screen_ptr
!= scr
)
337 if (ptr
->keycode
== event
->xkey
.keycode
&& ptr
->modifier
== modifiers
) {
338 (*ptr
->entry
->callback
) (ptr
->menu
, ptr
->entry
);
346 void wRootMenuBindShortcuts(Window window
)
352 if (ptr
->modifier
!= AnyModifier
) {
353 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
| LockMask
,
354 window
, True
, GrabModeAsync
, GrabModeAsync
);
356 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
359 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
364 static void rebindKeygrabs(WScreen
* scr
)
368 wwin
= scr
->focused_window
;
370 while (wwin
!= NULL
) {
371 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
373 if (!WFLAGP(wwin
, no_bind_keys
)) {
374 wWindowSetKeyGrabs(wwin
);
380 static void removeShortcutsForMenu(WMenu
* menu
)
383 Shortcut
*newList
= NULL
;
386 while (ptr
!= NULL
) {
388 if (ptr
->menu
== menu
) {
396 shortcutList
= newList
;
397 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
400 static Bool
addShortcut(const char *file
, const char *shortcutDefinition
, WMenu
*menu
, WMenuEntry
*entry
)
405 char buf
[MAX_SHORTCUT_LENGTH
], *b
;
407 ptr
= wmalloc(sizeof(Shortcut
));
409 wstrlcpy(buf
, shortcutDefinition
, MAX_SHORTCUT_LENGTH
);
414 while ((k
= strchr(b
, '+')) != NULL
) {
418 mod
= wXModifierFromKey(b
);
420 wwarning(_("%s: invalid key modifier \"%s\""), file
, b
);
424 ptr
->modifier
|= mod
;
430 ksym
= XStringToKeysym(b
);
432 if (ksym
== NoSymbol
) {
433 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
434 file
, shortcutDefinition
, entry
->text
);
439 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
440 if (ptr
->keycode
== 0) {
441 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
442 shortcutDefinition
, entry
->text
);
450 ptr
->next
= shortcutList
;
453 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
458 static char *next_token(char *line
, char **next
)
464 while (*line
== ' ' || *line
== '\t')
472 while (*tmp
!= 0 && *tmp
!= '"')
475 wwarning(_("%s: unmatched '\"' in menu file"), line
);
486 } while (*tmp
!= 0 && *tmp
!= ' ' && *tmp
!= '\t');
500 while (*tmp
== ' ' || *tmp
== '\t')
509 static void separateCommand(char *line
, char ***file
, char **command
)
511 char *token
, *tmp
= line
;
512 WMArray
*array
= WMCreateArray(4);
518 token
= next_token(tmp
, &tmp
);
520 if (strcmp(token
, "WITH") == 0) {
521 if (tmp
!= NULL
&& *tmp
!= 0)
522 *command
= wstrdup(tmp
);
524 wwarning(_("%s: missing command"), line
);
528 WMAddToArray(array
, token
);
530 } while (token
!= NULL
&& tmp
!= NULL
);
532 count
= WMGetArrayItemCount(array
);
534 *file
= wmalloc(sizeof(char *) * (count
+ 1));
535 (*file
)[count
] = NULL
;
536 for (i
= 0; i
< count
; i
++) {
537 (*file
)[i
] = WMGetFromArray(array
, i
);
543 static WMenu
*constructPLMenu(WScreen
*screen
, const char *path
)
545 WMPropList
*pl
= NULL
;
551 pl
= WMReadPropListFromFile(path
);
555 menu
= configureMenu(screen
, pl
);
557 WMReleasePropList(pl
);
562 menu
->on_destroy
= removeShortcutsForMenu
;
568 static void constructMenu(WMenu
* menu
, WMenuEntry
* entry
)
571 struct stat stat_buf
;
578 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
579 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
580 wwarning(_("invalid OPEN_MENU specification: %s"), (char *)entry
->clientdata
);
582 for (i
= 0; path
[i
] != NULL
; i
++)
591 if (path
[0][0] == '|') {
594 if (!menu
->cascades
[entry
->cascade
] || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
597 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
599 if (submenu
!= NULL
) {
600 if (path
[0][1] == '|')
601 submenu
->timestamp
= 0;
603 submenu
->timestamp
= 1; /* there's no automatic reloading */
611 /* try interpreting path as a proplist file */
612 submenu
= constructPLMenu(menu
->frame
->screen_ptr
, path
[0]);
613 /* if unsuccessful, try it as an old-style file */
617 while (path
[i
] != NULL
) {
620 if (strcmp(path
[i
], "-noext") == 0) {
625 tmp
= wexpandpath(path
[i
]);
627 if (strstr(tmp
, "#usergnusteppath#") == tmp
) {
630 tmp
= wstrconcat(wusergnusteppath(),
636 lpath
= getLocalizedMenuFile(tmp
);
645 if (stat(path
[i
], &stat_buf
) == 0) {
646 if (last
< stat_buf
.st_mtime
)
647 last
= stat_buf
.st_mtime
;
651 werror(_("%s:could not stat menu"), path
[i
]);
659 werror(_("%s:could not stat menu:%s"), "OPEN_MENU", (char *)entry
->clientdata
);
662 stat(path
[first
], &stat_buf
);
663 if (!menu
->cascades
[entry
->cascade
]
664 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
666 if (S_ISDIR(stat_buf
.st_mode
)) {
668 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
, entry
->text
, path
, cmd
);
670 submenu
->timestamp
= last
;
671 } else if (S_ISREG(stat_buf
.st_mode
)) {
675 wwarning(_("too many parameters in OPEN_MENU: %s"),
676 (char *)entry
->clientdata
);
678 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
680 submenu
->timestamp
= stat_buf
.st_mtime
;
691 wMenuEntryRemoveCascade(menu
, entry
);
692 wMenuEntrySetCascade(menu
, entry
, submenu
);
697 while (path
[i
] != NULL
)
704 static void constructPLMenuFromPipe(WMenu
* menu
, WMenuEntry
* entry
)
706 WMenu
*submenu
= NULL
;
711 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
712 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
713 wwarning(_("invalid OPEN_PLMENU specification: %s"),
714 (char *)entry
->clientdata
);
716 for (i
= 0; path
[i
] != NULL
; i
++)
725 if (path
[0][0] == '|') {
728 if (!menu
->cascades
[entry
->cascade
]
729 || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
731 submenu
= readPLMenuPipe(menu
->frame
->screen_ptr
, path
);
733 if (submenu
!= NULL
) {
734 if (path
[0][1] == '|')
735 submenu
->timestamp
= 0;
737 submenu
->timestamp
= 1; /* there's no automatic reloading */
743 wMenuEntryRemoveCascade(menu
, entry
);
744 wMenuEntrySetCascade(menu
, entry
, submenu
);
748 while (path
[i
] != NULL
)
756 static void cleanupWorkspaceMenu(WMenu
*menu
)
758 if (menu
->frame
->screen_ptr
->workspace_menu
== menu
)
759 menu
->frame
->screen_ptr
->workspace_menu
= NULL
;
762 static WMenuEntry
*addWorkspaceMenu(WScreen
*scr
, WMenu
*menu
, const char *title
)
767 if (scr
->flags
.added_workspace_menu
) {
769 ("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
772 scr
->flags
.added_workspace_menu
= 1;
774 wsmenu
= wWorkspaceMenuMake(scr
, True
);
775 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
777 scr
->workspace_menu
= wsmenu
;
778 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
779 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
781 wWorkspaceMenuUpdate(scr
, wsmenu
);
786 static void cleanupWindowsMenu(WMenu
* menu
)
788 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
789 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
792 static WMenuEntry
*addWindowsMenu(WScreen
*scr
, WMenu
*menu
, const char *title
)
798 if (scr
->flags
.added_windows_menu
) {
800 ("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
803 scr
->flags
.added_windows_menu
= 1;
805 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
806 wwmenu
->on_destroy
= cleanupWindowsMenu
;
807 scr
->switch_menu
= wwmenu
;
808 wwin
= scr
->focused_window
;
810 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
814 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
815 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
820 static WMenuEntry
*addMenuEntry(WMenu
*menu
, const char *title
, const char *shortcut
, const char *command
,
821 const char *params
, const char *file_name
)
824 WMenuEntry
*entry
= NULL
;
825 Bool shortcutOk
= False
;
829 scr
= menu
->frame
->screen_ptr
;
830 if (strcmp(command
, "OPEN_MENU") == 0) {
832 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
837 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
839 path
= wstrdup(params
);
841 dummy
= wMenuCreate(scr
, title
, False
);
842 dummy
->on_destroy
= removeShortcutsForMenu
;
843 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
844 entry
->free_cdata
= wfree
;
845 wMenuEntrySetCascade(menu
, entry
, dummy
);
847 } else if (strcmp(command
, "OPEN_PLMENU") == 0) {
849 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
854 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
856 path
= wstrdup(params
);
858 dummy
= wMenuCreate(scr
, title
, False
);
859 dummy
->on_destroy
= removeShortcutsForMenu
;
860 entry
= wMenuAddCallback(menu
, title
, constructPLMenuFromPipe
, path
);
861 entry
->free_cdata
= wfree
;
862 wMenuEntrySetCascade(menu
, entry
, dummy
);
864 } else if (strcmp(command
, "EXEC") == 0) {
866 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
868 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrconcat("exec ", params
));
869 entry
->free_cdata
= wfree
;
872 } else if (strcmp(command
, "SHEXEC") == 0) {
874 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
876 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrdup(params
));
877 entry
->free_cdata
= wfree
;
880 } else if (strcmp(command
, "EXIT") == 0) {
882 if (params
&& strcmp(params
, "QUICK") == 0)
883 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void *)M_QUICK
);
885 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
888 } else if (strcmp(command
, "SHUTDOWN") == 0) {
890 if (params
&& strcmp(params
, "QUICK") == 0)
891 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, (void *)M_QUICK
);
893 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
896 } else if (strcmp(command
, "REFRESH") == 0) {
897 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
900 } else if (strcmp(command
, "WORKSPACE_MENU") == 0) {
901 entry
= addWorkspaceMenu(scr
, menu
, title
);
904 } else if (strcmp(command
, "WINDOWS_MENU") == 0) {
905 entry
= addWindowsMenu(scr
, menu
, title
);
908 } else if (strcmp(command
, "ARRANGE_ICONS") == 0) {
909 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
912 } else if (strcmp(command
, "HIDE_OTHERS") == 0) {
913 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
916 } else if (strcmp(command
, "SHOW_ALL") == 0) {
917 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
920 } else if (strcmp(command
, "RESTART") == 0) {
921 entry
= wMenuAddCallback(menu
, title
, restartCommand
, params
? wstrdup(params
) : NULL
);
922 entry
->free_cdata
= wfree
;
924 } else if (strcmp(command
, "SAVE_SESSION") == 0) {
925 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
928 } else if (strcmp(command
, "CLEAR_SESSION") == 0) {
929 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
931 } else if (strcmp(command
, "INFO_PANEL") == 0) {
932 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
934 } else if (strcmp(command
, "LEGAL_PANEL") == 0) {
935 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
938 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
, command
);
943 if (shortcut
&& entry
) {
945 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
, title
);
947 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
949 entry
->rtext
= GetShortcutString(shortcut
);
951 entry->rtext = wstrdup(shortcut);
960 /******************* Menu Configuration From File *******************/
962 static void freeline(char *title
, char *command
, char *parameter
, char *shortcut
)
970 static WMenu
*parseCascade(WScreen
* scr
, WMenu
* menu
, WMenuParser parser
)
972 char *command
, *params
, *shortcut
, *title
;
974 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
976 if (command
== NULL
|| !command
[0]) {
977 WMenuParserError(parser
, _("missing command in menu config") );
978 freeline(title
, command
, params
, shortcut
);
982 if (strcasecmp(command
, "MENU") == 0) {
987 cascade
= wMenuCreate(scr
, M_(title
), False
);
988 cascade
->on_destroy
= removeShortcutsForMenu
;
989 if (!parseCascade(scr
, cascade
, parser
)) {
990 wMenuDestroy(cascade
, True
);
992 wMenuEntrySetCascade(menu
, wMenuAddCallback(menu
, M_(title
), NULL
, NULL
), cascade
);
994 } else if (strcasecmp(command
, "END") == 0) {
996 freeline(title
, command
, params
, shortcut
);
1000 addMenuEntry(menu
, M_(title
), shortcut
, command
, params
, WMenuParserGetFilename(parser
));
1002 freeline(title
, command
, params
, shortcut
);
1005 WMenuParserError(parser
, _("syntax error in menu file: END declaration missing") );
1011 static WMenu
*readMenu(WScreen
*scr
, const char *flat_file
, FILE *file
)
1015 char *title
, *command
, *params
, *shortcut
;
1017 parser
= WMenuParserCreate(flat_file
, file
, DEF_CONFIG_PATHS
);
1018 menu_parser_register_macros(parser
);
1020 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
1022 if (command
== NULL
|| !command
[0]) {
1023 WMenuParserError(parser
, _("missing command in menu config") );
1024 freeline(title
, command
, params
, shortcut
);
1027 if (strcasecmp(command
, "MENU") == 0) {
1028 menu
= wMenuCreate(scr
, M_(title
), True
);
1029 menu
->on_destroy
= removeShortcutsForMenu
;
1030 if (!parseCascade(scr
, menu
, parser
)) {
1031 wMenuDestroy(menu
, True
);
1034 freeline(title
, command
, params
, shortcut
);
1037 WMenuParserError(parser
, _("invalid menu, no menu title given") );
1038 freeline(title
, command
, params
, shortcut
);
1043 WMenuParserDelete(parser
);
1047 static WMenu
*readMenuFile(WScreen
*scr
, const char *file_name
)
1052 file
= fopen(file_name
, "rb");
1054 werror(_("could not open menu file \"%s\": %s"), file_name
, strerror(errno
));
1057 menu
= readMenu(scr
, file_name
, file
);
1063 static inline int generate_command_from_list(char *buffer
, size_t buffer_size
, char **command_elements
)
1070 for (i
= 0; command_elements
[i
] != NULL
; i
++) {
1073 if (wr_idx
< buffer_size
- 1)
1074 buffer
[wr_idx
++] = ' ';
1076 for (rd
= command_elements
[i
]; *rd
!= '\0'; rd
++) {
1077 if (wr_idx
< buffer_size
- 1)
1078 buffer
[wr_idx
++] = *rd
;
1083 buffer
[wr_idx
] = '\0';
1087 /************ Menu Configuration From Pipe *************/
1088 static WMenu
*readPLMenuPipe(WScreen
* scr
, char **file_name
)
1090 WMPropList
*plist
= NULL
;
1093 char flat_file
[MAXLINE
];
1095 if (generate_command_from_list(flat_file
, sizeof(flat_file
), file_name
)) {
1096 werror(_("could not open menu file \"%s\": %s"),
1097 file_name
[0], _("pipe command for PropertyList is too long"));
1100 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1102 plist
= WMReadPropListFromPipe(filename
);
1107 menu
= configureMenu(scr
, plist
);
1109 WMReleasePropList(plist
);
1114 menu
->on_destroy
= removeShortcutsForMenu
;
1118 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
)
1123 char flat_file
[MAXLINE
];
1125 if (generate_command_from_list(flat_file
, sizeof(flat_file
), file_name
)) {
1126 werror(_("could not open menu file \"%s\": %s"),
1127 file_name
[0], _("pipe command is too long"));
1130 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1133 * In case of memory problem, 'popen' will not set the errno, so we initialise it
1134 * to be able to display a meaningful message. For other problems, 'popen' will
1135 * properly set errno, so we'll still get a good message
1138 file
= popen(filename
, "r");
1140 werror(_("could not open menu file \"%s\": %s"), filename
, strerror(errno
));
1143 menu
= readMenu(scr
, flat_file
, file
);
1154 static int myCompare(const void *d1
, const void *d2
)
1156 dir_data
*p1
= *(dir_data
**) d1
;
1157 dir_data
*p2
= *(dir_data
**) d2
;
1159 return strcmp(p1
->name
, p2
->name
);
1162 /***** Preset some macro for file parser *****/
1163 static void menu_parser_register_macros(WMenuParser parser
)
1168 // Used to return CPP verion, now returns wmaker's version
1169 WMenuParserRegisterSimpleMacro(parser
, "__VERSION__", VERSION
);
1171 // All macros below were historically defined by WindowMaker
1172 visual
= DefaultVisual(dpy
, DefaultScreen(dpy
));
1173 snprintf(buf
, sizeof(buf
), "%d", visual
->class);
1174 WMenuParserRegisterSimpleMacro(parser
, "VISUAL", buf
);
1176 snprintf(buf
, sizeof(buf
), "%d", DefaultDepth(dpy
, DefaultScreen(dpy
)) );
1177 WMenuParserRegisterSimpleMacro(parser
, "DEPTH", buf
);
1179 snprintf(buf
, sizeof(buf
), "%d", WidthOfScreen(DefaultScreenOfDisplay(dpy
)) );
1180 WMenuParserRegisterSimpleMacro(parser
, "SCR_WIDTH", buf
);
1182 snprintf(buf
, sizeof(buf
), "%d", HeightOfScreen(DefaultScreenOfDisplay(dpy
)) );
1183 WMenuParserRegisterSimpleMacro(parser
, "SCR_HEIGHT", buf
);
1185 WMenuParserRegisterSimpleMacro(parser
, "DISPLAY", XDisplayName(DisplayString(dpy
)) );
1187 WMenuParserRegisterSimpleMacro(parser
, "WM_VERSION", "\"" VERSION
"\"");
1190 /************ Menu Configuration From Directory *************/
1192 static Bool
isFilePackage(const char *file
)
1196 /* check if the extension indicates this file is a
1197 * file package. For now, only recognize .themed */
1201 if (l
> 7 && strcmp(&(file
[l
- 7]), ".themed") == 0) {
1208 static WMenu
*readMenuDirectory(WScreen
*scr
, const char *title
, char **path
, const char *command
)
1211 struct dirent
*dentry
;
1212 struct stat stat_buf
;
1215 WMArray
*dirs
= NULL
, *files
= NULL
;
1216 WMArrayIterator iter
;
1217 int length
, i
, have_space
= 0;
1219 int stripExtension
= 0;
1221 dirs
= WMCreateArray(16);
1222 files
= WMCreateArray(16);
1225 while (path
[i
] != NULL
) {
1226 if (strcmp(path
[i
], "-noext") == 0) {
1232 dir
= opendir(path
[i
]);
1238 while ((dentry
= readdir(dir
))) {
1240 if (strcmp(dentry
->d_name
, ".") == 0 || strcmp(dentry
->d_name
, "..") == 0)
1243 if (dentry
->d_name
[0] == '.')
1246 buffer
= malloc(strlen(path
[i
]) + strlen(dentry
->d_name
) + 4);
1248 werror(_("out of memory while constructing directory menu %s"), path
[i
]);
1252 strcpy(buffer
, path
[i
]);
1253 strcat(buffer
, "/");
1254 strcat(buffer
, dentry
->d_name
);
1256 if (stat(buffer
, &stat_buf
) != 0) {
1257 werror(_("%s:could not stat file \"%s\" in menu directory"),
1258 path
[i
], dentry
->d_name
);
1260 Bool isFilePack
= False
;
1263 if (S_ISDIR(stat_buf
.st_mode
)
1264 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1266 /* access always returns success for user root */
1267 if (access(buffer
, X_OK
) == 0) {
1268 /* Directory is accesible. Add to directory list */
1270 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1271 data
->name
= wstrdup(dentry
->d_name
);
1274 WMAddToArray(dirs
, data
);
1276 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1277 /* Hack because access always returns X_OK success for user root */
1278 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1279 if ((command
!= NULL
&& access(buffer
, R_OK
) == 0) ||
1280 (command
== NULL
&& access(buffer
, X_OK
) == 0 &&
1281 (stat_buf
.st_mode
& S_IXANY
))) {
1283 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1284 data
->name
= wstrdup(dentry
->d_name
);
1287 WMAddToArray(files
, data
);
1298 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1304 WMSortArray(dirs
, myCompare
);
1305 WMSortArray(files
, myCompare
);
1307 menu
= wMenuCreate(scr
, M_(title
), False
);
1308 menu
->on_destroy
= removeShortcutsForMenu
;
1310 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1311 /* New directory. Use same OPEN_MENU command that was used
1312 * for the current directory. */
1313 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1317 length
+= strlen(command
) + 6;
1318 buffer
= malloc(length
);
1320 werror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1326 strcat(buffer
, "-noext ");
1328 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1331 strcat(buffer
, "\"");
1332 strcat(buffer
, path
[data
->index
]);
1334 strcat(buffer
, "/");
1335 strcat(buffer
, data
->name
);
1337 strcat(buffer
, "\"");
1339 strcat(buffer
, " WITH ");
1340 strcat(buffer
, command
);
1343 addMenuEntry(menu
, M_(data
->name
), NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1350 WM_ITERATE_ARRAY(files
, data
, iter
) {
1351 /* executable: add as entry */
1352 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1354 length
+= strlen(command
);
1356 buffer
= malloc(length
);
1358 werror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1362 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1363 if (command
!= NULL
) {
1364 strcpy(buffer
, command
);
1365 strcat(buffer
, " ");
1367 strcat(buffer
, "\"");
1368 strcat(buffer
, path
[data
->index
]);
1373 strcat(buffer
, path
[data
->index
]);
1375 strcpy(buffer
, path
[data
->index
]);
1378 strcat(buffer
, "/");
1379 strcat(buffer
, data
->name
);
1381 strcat(buffer
, "\"");
1383 if (stripExtension
) {
1384 char *ptr
= strrchr(data
->name
, '.');
1385 if (ptr
&& ptr
!= data
->name
)
1388 addMenuEntry(menu
, M_(data
->name
), NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1401 /************ Menu Configuration From WMRootMenu *************/
1403 static WMenu
*makeDefaultMenu(WScreen
* scr
)
1407 menu
= wMenuCreate(scr
, _("Commands"), True
);
1408 wMenuAddCallback(menu
, M_("XTerm"), execCommand
, "xterm");
1409 wMenuAddCallback(menu
, M_("rxvt"), execCommand
, "rxvt");
1410 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1411 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1416 *----------------------------------------------------------------------
1418 * Reads root menu configuration from defaults database.
1420 *----------------------------------------------------------------------
1422 WMenu
*configureMenu(WScreen
*scr
, WMPropList
*definition
)
1427 WMPropList
*title
, *command
, *params
;
1430 if (WMIsPLString(definition
)) {
1431 struct stat stat_buf
;
1433 Bool menu_is_default
= False
;
1435 /* menu definition is a string. Probably a path, so parse the file */
1437 tmp
= wexpandpath(WMGetFromPLString(definition
));
1439 path
= getLocalizedMenuFile(tmp
);
1442 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1445 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1446 menu_is_default
= True
;
1450 werror(_("could not find menu file \"%s\" referenced in WMRootMenu"), tmp
);
1455 if (stat(path
, &stat_buf
) < 0) {
1456 werror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1462 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1463 /* if the pointer in WMRootMenu has changed */
1464 || w_global
.domain
.root_menu
->timestamp
> scr
->root_menu
->timestamp
) {
1465 WMPropList
*menu_from_file
= NULL
;
1467 if (menu_is_default
) {
1469 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1473 menu_from_file
= WMReadPropListFromFile(path
);
1474 if (menu_from_file
== NULL
) { /* old style menu */
1475 menu
= readMenuFile(scr
, path
);
1477 menu
= configureMenu(scr
, menu_from_file
);
1478 WMReleasePropList(menu_from_file
);
1482 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, w_global
.domain
.root_menu
->timestamp
);
1492 count
= WMGetPropListItemCount(definition
);
1496 elem
= WMGetFromPLArray(definition
, 0);
1497 if (!WMIsPLString(elem
)) {
1498 tmp
= WMGetPropListDescription(elem
, False
);
1499 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1503 mtitle
= WMGetFromPLString(elem
);
1505 menu
= wMenuCreate(scr
, M_(mtitle
), False
);
1506 menu
->on_destroy
= removeShortcutsForMenu
;
1508 for (i
= 1; i
< count
; i
++) {
1509 elem
= WMGetFromPLArray(definition
, i
);
1511 if (WMIsPLString(elem
)) {
1514 file
= WMGetFromPLString(elem
);
1518 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1521 if (WMIsPLArray(WMGetFromPLArray(elem
, 1))) {
1526 submenu
= configureMenu(scr
, elem
);
1528 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1529 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1533 WMPropList
*shortcut
;
1536 title
= WMGetFromPLArray(elem
, idx
++);
1537 shortcut
= WMGetFromPLArray(elem
, idx
++);
1538 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT") == 0) {
1539 shortcut
= WMGetFromPLArray(elem
, idx
++);
1540 command
= WMGetFromPLArray(elem
, idx
++);
1545 params
= WMGetFromPLArray(elem
, idx
++);
1547 if (!title
|| !command
)
1550 addMenuEntry(menu
, M_(WMGetFromPLString(title
)),
1551 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1552 WMGetFromPLString(command
),
1553 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1558 tmp
= WMGetPropListDescription(elem
, False
);
1559 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1567 *----------------------------------------------------------------------
1569 * Opens the root menu, parsing the menu configuration from the
1570 * defaults database.
1571 * If the menu is already mapped and is not sticked to the
1572 * root window, it will be unmapped.
1575 * The menu may be remade.
1578 * Construction of OPEN_MENU entries are delayed to the moment the
1580 *----------------------------------------------------------------------
1582 void OpenRootMenu(WScreen
* scr
, int x
, int y
, int keyboard
)
1585 WMPropList
*definition
;
1587 static WMPropList *domain=NULL;
1590 domain = WMCreatePLString("WMRootMenu");
1594 scr
->flags
.root_menu_changed_shortcuts
= 0;
1595 scr
->flags
.added_workspace_menu
= 0;
1596 scr
->flags
.added_windows_menu
= 0;
1598 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1599 menu
= scr
->root_menu
;
1600 if (!menu
->flags
.buttoned
) {
1603 wRaiseFrame(menu
->frame
->core
);
1606 wMenuMapAt(menu
, 0, 0, True
);
1608 wMenuMapCopyAt(menu
, x
- menu
->frame
->core
->width
/ 2, y
);
1613 definition
= w_global
.domain
.root_menu
->dictionary
;
1616 definition = PLGetDomain(domain);
1619 if (WMIsPLArray(definition
)) {
1620 if (!scr
->root_menu
|| w_global
.domain
.root_menu
->timestamp
> scr
->root_menu
->timestamp
) {
1621 menu
= configureMenu(scr
, definition
);
1623 menu
->timestamp
= w_global
.domain
.root_menu
->timestamp
;
1628 menu
= configureMenu(scr
, definition
);
1633 /* menu hasn't changed or could not be read */
1634 if (!scr
->root_menu
) {
1635 wMessageDialog(scr
, _("Error"),
1636 _("The applications menu could not be loaded. "
1637 "Look at the console output for a detailed "
1638 "description of the errors."), _("OK"), NULL
, NULL
);
1640 menu
= makeDefaultMenu(scr
);
1641 scr
->root_menu
= menu
;
1643 menu
= scr
->root_menu
;
1646 if (scr
->root_menu
) {
1647 wMenuDestroy(scr
->root_menu
, True
);
1649 scr
->root_menu
= menu
;
1654 if (keyboard
&& x
== 0 && y
== 0) {
1656 } else if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
1657 newx
= x
- menu
->frame
->core
->width
/ 2;
1658 newy
= y
- menu
->frame
->core
->height
/ 2;
1660 newx
= x
- menu
->frame
->core
->width
/ 2;
1663 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1666 if (scr
->flags
.root_menu_changed_shortcuts
)
1667 rebindKeygrabs(scr
);