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 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 WMenu
*configureMenu(WScreen
*scr
, WMPropList
*definition
);
70 static void menu_parser_register_macros(WMenuParser parser
);
72 typedef struct Shortcut
{
73 struct Shortcut
*next
;
81 static Shortcut
*shortcutList
= NULL
;
87 * "Title" EXEC command_to_exec -params
89 * "Title" EXEC command_to_exec -params
91 * "Workspaces" WORKSPACE_MENU
92 * "Title" built_in_command
94 * "Quick Quit" EXIT QUICK
97 * Commands may be preceded by SHORTCUT key
101 * INFO_PANEL - shows the Info Panel
102 * LEGAL_PANEL - shows the Legal info panel
103 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
104 * REFRESH - forces the desktop to be repainted
105 * EXIT [QUICK] - exit the window manager [without confirmation]
106 * EXEC <program> - execute an external program
107 * SHEXEC <command> - execute a shell command
108 * WORKSPACE_MENU - places the workspace submenu
110 * RESTART [<window manager>] - restarts the window manager
111 * SHOW_ALL - unhide all windows on workspace
112 * HIDE_OTHERS - hides all windows excep the focused one
113 * OPEN_MENU file - read menu data from file which must be a valid menu file.
114 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
115 * - read menu data from directory(ies) and
116 * eventually precede each with a command.
117 * OPEN_MENU | command
118 * - opens command and uses its stdout to construct and insert
119 * the resulting menu in current position. The output of
120 * command must be a valid menu description.
121 * The space between '|' and command is optional.
122 * || will do the same, but will not cache the contents.
123 * OPEN_PLMENU | command
124 * - opens command and uses its stdout which must be in proplist
125 * fromat to construct and insert the resulting menu in current
127 * The space between '|' and command is optional.
128 * || will do the same, but will not cache the contents.
129 * SAVE_SESSION - saves the current state of the desktop, which include
130 * all running applications, all their hints (geometry,
131 * position on screen, workspace they live on, the dock
132 * or clip from where they were launched, and
133 * if minimized, shaded or hidden. Also saves the current
134 * workspace the user is on. All will be restored on every
135 * start of windowmaker until another SAVE_SESSION or
136 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
137 * WindowMaker domain file, then saving is automatically
138 * done on every windowmaker exit, overwriting any
139 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
141 * CLEAR_SESSION - clears any previous saved session. This will not have
142 * any effect if SaveSessionOnExit is True.
150 static void execCommand(WMenu
* menu
, WMenuEntry
* entry
)
154 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char *)entry
->clientdata
);
156 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
157 GrabModeAsync
, GrabModeAsync
, None
, wPreferences
.cursor
[WCUR_WAIT
], CurrentTime
);
161 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
164 XUngrabPointer(dpy
, CurrentTime
);
168 static void exitCommand(WMenu
* menu
, WMenuEntry
* entry
)
170 static int inside
= 0;
173 /* prevent reentrant calls */
183 if ((long)entry
->clientdata
== M_QUICK
) {
186 int r
, oldSaveSessionFlag
;
188 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
189 r
= wExitDialog(menu
->frame
->screen_ptr
, _("Exit"),
190 _("Exit window manager?"), _("Exit"), _("Cancel"), NULL
);
192 if (r
== WAPRDefault
) {
194 } else if (r
== WAPRAlternate
) {
195 /* Don't modify the "save session on exit" flag if the
196 * user canceled the operation. */
197 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
200 if (result
== R_EXIT
)
201 Shutdown(WSExitMode
);
208 static void shutdownCommand(WMenu
* menu
, WMenuEntry
* entry
)
210 static int inside
= 0;
213 /* prevent reentrant calls */
223 if ((long)entry
->clientdata
== M_QUICK
)
226 int r
, oldSaveSessionFlag
;
228 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
230 r
= wExitDialog(menu
->frame
->screen_ptr
,
232 _("Kill Window System session?\n"
233 "(all applications will be closed)"), _("Kill"), _("Cancel"), NULL
);
234 if (r
== WAPRDefault
) {
236 } else if (r
== WAPRAlternate
) {
237 /* Don't modify the "save session on exit" flag if the
238 * user canceled the operation. */
239 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
243 if (result
!= R_CANCEL
) {
244 Shutdown(WSKillMode
);
252 static void restartCommand(WMenu
* menu
, WMenuEntry
* entry
)
254 /* Parameter not used, but tell the compiler that it is ok */
258 Shutdown(WSRestartPreparationMode
);
259 Restart((char *)entry
->clientdata
, False
);
263 static void refreshCommand(WMenu
* menu
, WMenuEntry
* entry
)
265 /* Parameter not used, but tell the compiler that it is ok */
268 wRefreshDesktop(menu
->frame
->screen_ptr
);
271 static void arrangeIconsCommand(WMenu
* menu
, WMenuEntry
* entry
)
273 /* Parameter not used, but tell the compiler that it is ok */
276 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
279 static void showAllCommand(WMenu
* menu
, WMenuEntry
* entry
)
281 /* Parameter not used, but tell the compiler that it is ok */
284 wShowAllWindows(menu
->frame
->screen_ptr
);
287 static void hideOthersCommand(WMenu
* menu
, WMenuEntry
* entry
)
289 /* Parameter not used, but tell the compiler that it is ok */
292 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
295 static void saveSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
297 /* Parameter not used, but tell the compiler that it is ok */
300 if (!wPreferences
.save_session_on_exit
)
301 wSessionSaveState(menu
->frame
->screen_ptr
);
303 wScreenSaveState(menu
->frame
->screen_ptr
);
306 static void clearSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
308 /* Parameter not used, but tell the compiler that it is ok */
311 wSessionClearState(menu
->frame
->screen_ptr
);
312 wScreenSaveState(menu
->frame
->screen_ptr
);
315 static void infoPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
317 /* Parameter not used, but tell the compiler that it is ok */
320 wShowInfoPanel(menu
->frame
->screen_ptr
);
323 static void legalPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
325 /* Parameter not used, but tell the compiler that it is ok */
328 wShowLegalPanel(menu
->frame
->screen_ptr
);
331 /********************************************************************/
333 static char *getLocalizedMenuFile(const char *menu
)
335 char *buffer
, *ptr
, *locale
;
338 if (!w_global
.locale
)
341 len
= strlen(menu
) + strlen(w_global
.locale
) + 8;
342 buffer
= wmalloc(len
);
344 /* try menu.locale_name */
345 snprintf(buffer
, len
, "%s.%s", menu
, w_global
.locale
);
346 if (access(buffer
, F_OK
) == 0)
349 /* position of locale in our buffer */
350 locale
= buffer
+ strlen(menu
) + 1;
352 /* check if it is in the form aa_bb.encoding and check for aa_bb */
353 ptr
= strchr(locale
, '.');
356 if (access(buffer
, F_OK
) == 0)
360 /* now check for aa */
361 ptr
= strchr(locale
, '_');
364 if (access(buffer
, F_OK
) == 0)
373 Bool
wRootMenuPerformShortcut(XEvent
* event
)
375 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
380 /* ignore CapsLock */
381 modifiers
= event
->xkey
.state
& w_global
.shortcut
.modifiers_mask
;
383 for (ptr
= shortcutList
; ptr
!= NULL
; ptr
= ptr
->next
) {
384 if (ptr
->keycode
== 0 || ptr
->menu
->menu
->screen_ptr
!= scr
)
387 if (ptr
->keycode
== event
->xkey
.keycode
&& ptr
->modifier
== modifiers
) {
388 (*ptr
->entry
->callback
) (ptr
->menu
, ptr
->entry
);
396 void wRootMenuBindShortcuts(Window window
)
402 if (ptr
->modifier
!= AnyModifier
) {
403 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
| LockMask
,
404 window
, True
, GrabModeAsync
, GrabModeAsync
);
406 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
409 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
414 static void rebindKeygrabs(WScreen
* scr
)
418 wwin
= scr
->focused_window
;
420 while (wwin
!= NULL
) {
421 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
423 if (!WFLAGP(wwin
, no_bind_keys
)) {
424 wWindowSetKeyGrabs(wwin
);
430 static void removeShortcutsForMenu(WMenu
* menu
)
433 Shortcut
*newList
= NULL
;
436 while (ptr
!= NULL
) {
438 if (ptr
->menu
== menu
) {
446 shortcutList
= newList
;
447 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
450 static Bool
addShortcut(const char *file
, const char *shortcutDefinition
, WMenu
*menu
, WMenuEntry
*entry
)
455 char buf
[MAX_SHORTCUT_LENGTH
], *b
;
457 ptr
= wmalloc(sizeof(Shortcut
));
459 wstrlcpy(buf
, shortcutDefinition
, MAX_SHORTCUT_LENGTH
);
464 while ((k
= strchr(b
, '+')) != NULL
) {
468 mod
= wXModifierFromKey(b
);
470 wwarning(_("%s: invalid key modifier \"%s\""), file
, b
);
474 ptr
->modifier
|= mod
;
480 ksym
= XStringToKeysym(b
);
482 if (ksym
== NoSymbol
) {
483 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
484 file
, shortcutDefinition
, entry
->text
);
489 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
490 if (ptr
->keycode
== 0) {
491 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
492 shortcutDefinition
, entry
->text
);
500 ptr
->next
= shortcutList
;
503 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
508 static char *next_token(char *line
, char **next
)
514 while (*line
== ' ' || *line
== '\t')
522 while (*tmp
!= 0 && *tmp
!= '"')
525 wwarning(_("%s: unmatched '\"' in menu file"), line
);
536 } while (*tmp
!= 0 && *tmp
!= ' ' && *tmp
!= '\t');
550 while (*tmp
== ' ' || *tmp
== '\t')
559 static void separateCommand(char *line
, char ***file
, char **command
)
561 char *token
, *tmp
= line
;
562 WMArray
*array
= WMCreateArray(4);
568 token
= next_token(tmp
, &tmp
);
570 if (strcmp(token
, "WITH") == 0) {
571 if (tmp
!= NULL
&& *tmp
!= 0)
572 *command
= wstrdup(tmp
);
574 wwarning(_("%s: missing command"), line
);
578 WMAddToArray(array
, token
);
580 } while (token
!= NULL
&& tmp
!= NULL
);
582 count
= WMGetArrayItemCount(array
);
584 *file
= wmalloc(sizeof(char *) * (count
+ 1));
585 (*file
)[count
] = NULL
;
586 for (i
= 0; i
< count
; i
++) {
587 (*file
)[i
] = WMGetFromArray(array
, i
);
593 static WMenu
*constructPLMenu(WScreen
*screen
, const char *path
)
595 WMPropList
*pl
= NULL
;
601 pl
= WMReadPropListFromFile(path
);
605 menu
= configureMenu(screen
, pl
);
607 WMReleasePropList(pl
);
612 menu
->on_destroy
= removeShortcutsForMenu
;
618 static void constructMenu(WMenu
* menu
, WMenuEntry
* entry
)
621 struct stat stat_buf
;
628 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
629 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
630 wwarning(_("invalid OPEN_MENU specification: %s"), (char *)entry
->clientdata
);
632 for (i
= 0; path
[i
] != NULL
; i
++)
641 if (path
[0][0] == '|') {
644 if (!menu
->cascades
[entry
->cascade
] || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
647 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
649 if (submenu
!= NULL
) {
650 if (path
[0][1] == '|')
651 submenu
->timestamp
= 0;
653 submenu
->timestamp
= 1; /* there's no automatic reloading */
661 /* try interpreting path as a proplist file */
662 submenu
= constructPLMenu(menu
->frame
->screen_ptr
, path
[0]);
663 /* if unsuccessful, try it as an old-style file */
667 while (path
[i
] != NULL
) {
670 if (strcmp(path
[i
], "-noext") == 0) {
675 tmp
= wexpandpath(path
[i
]);
677 lpath
= getLocalizedMenuFile(tmp
);
686 if (stat(path
[i
], &stat_buf
) == 0) {
687 if (last
< stat_buf
.st_mtime
)
688 last
= stat_buf
.st_mtime
;
692 werror(_("%s:could not stat menu"), path
[i
]);
700 werror(_("%s:could not stat menu:%s"), "OPEN_MENU", (char *)entry
->clientdata
);
703 stat(path
[first
], &stat_buf
);
704 if (!menu
->cascades
[entry
->cascade
]
705 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
707 if (S_ISDIR(stat_buf
.st_mode
)) {
709 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
, entry
->text
, path
, cmd
);
711 submenu
->timestamp
= last
;
712 } else if (S_ISREG(stat_buf
.st_mode
)) {
716 wwarning(_("too many parameters in OPEN_MENU: %s"),
717 (char *)entry
->clientdata
);
719 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
721 submenu
->timestamp
= stat_buf
.st_mtime
;
732 wMenuEntryRemoveCascade(menu
, entry
);
733 wMenuEntrySetCascade(menu
, entry
, submenu
);
738 while (path
[i
] != NULL
)
745 static void constructPLMenuFromPipe(WMenu
* menu
, WMenuEntry
* entry
)
747 WMenu
*submenu
= NULL
;
752 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
753 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
754 wwarning(_("invalid OPEN_PLMENU specification: %s"),
755 (char *)entry
->clientdata
);
757 for (i
= 0; path
[i
] != NULL
; i
++)
766 if (path
[0][0] == '|') {
769 if (!menu
->cascades
[entry
->cascade
]
770 || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
772 submenu
= readPLMenuPipe(menu
->frame
->screen_ptr
, path
);
774 if (submenu
!= NULL
) {
775 if (path
[0][1] == '|')
776 submenu
->timestamp
= 0;
778 submenu
->timestamp
= 1; /* there's no automatic reloading */
784 wMenuEntryRemoveCascade(menu
, entry
);
785 wMenuEntrySetCascade(menu
, entry
, submenu
);
789 while (path
[i
] != NULL
)
797 static void cleanupWorkspaceMenu(WMenu
*menu
)
799 if (menu
->frame
->screen_ptr
->workspace_menu
== menu
)
800 menu
->frame
->screen_ptr
->workspace_menu
= NULL
;
803 static WMenuEntry
*addWorkspaceMenu(WScreen
*scr
, WMenu
*menu
, const char *title
)
808 if (scr
->flags
.added_workspace_menu
) {
810 ("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
813 scr
->flags
.added_workspace_menu
= 1;
815 wsmenu
= wWorkspaceMenuMake(scr
, True
);
816 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
818 scr
->workspace_menu
= wsmenu
;
819 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
820 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
822 wWorkspaceMenuUpdate(scr
, wsmenu
);
827 static void cleanupWindowsMenu(WMenu
* menu
)
829 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
830 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
833 static WMenuEntry
*addWindowsMenu(WScreen
*scr
, WMenu
*menu
, const char *title
)
839 if (scr
->flags
.added_windows_menu
) {
841 ("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
844 scr
->flags
.added_windows_menu
= 1;
846 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
847 wwmenu
->on_destroy
= cleanupWindowsMenu
;
848 scr
->switch_menu
= wwmenu
;
849 wwin
= scr
->focused_window
;
851 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
855 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
856 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
861 static WMenuEntry
*addMenuEntry(WMenu
*menu
, const char *title
, const char *shortcut
, const char *command
,
862 const char *params
, const char *file_name
)
865 WMenuEntry
*entry
= NULL
;
866 Bool shortcutOk
= False
;
870 scr
= menu
->frame
->screen_ptr
;
871 if (strcmp(command
, "OPEN_MENU") == 0) {
873 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
878 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
880 path
= wstrdup(params
);
882 dummy
= wMenuCreate(scr
, title
, False
);
883 dummy
->on_destroy
= removeShortcutsForMenu
;
884 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
885 entry
->free_cdata
= wfree
;
886 wMenuEntrySetCascade(menu
, entry
, dummy
);
888 } else if (strcmp(command
, "OPEN_PLMENU") == 0) {
890 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
895 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
897 path
= wstrdup(params
);
899 dummy
= wMenuCreate(scr
, title
, False
);
900 dummy
->on_destroy
= removeShortcutsForMenu
;
901 entry
= wMenuAddCallback(menu
, title
, constructPLMenuFromPipe
, path
);
902 entry
->free_cdata
= wfree
;
903 wMenuEntrySetCascade(menu
, entry
, dummy
);
905 } else if (strcmp(command
, "EXEC") == 0) {
907 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
909 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrconcat("exec ", params
));
910 entry
->free_cdata
= wfree
;
913 } else if (strcmp(command
, "SHEXEC") == 0) {
915 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
917 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrdup(params
));
918 entry
->free_cdata
= wfree
;
921 } else if (strcmp(command
, "EXIT") == 0) {
923 if (params
&& strcmp(params
, "QUICK") == 0)
924 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void *)M_QUICK
);
926 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
929 } else if (strcmp(command
, "SHUTDOWN") == 0) {
931 if (params
&& strcmp(params
, "QUICK") == 0)
932 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, (void *)M_QUICK
);
934 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
937 } else if (strcmp(command
, "REFRESH") == 0) {
938 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
941 } else if (strcmp(command
, "WORKSPACE_MENU") == 0) {
942 entry
= addWorkspaceMenu(scr
, menu
, title
);
945 } else if (strcmp(command
, "WINDOWS_MENU") == 0) {
946 entry
= addWindowsMenu(scr
, menu
, title
);
949 } else if (strcmp(command
, "ARRANGE_ICONS") == 0) {
950 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
953 } else if (strcmp(command
, "HIDE_OTHERS") == 0) {
954 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
957 } else if (strcmp(command
, "SHOW_ALL") == 0) {
958 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
961 } else if (strcmp(command
, "RESTART") == 0) {
962 entry
= wMenuAddCallback(menu
, title
, restartCommand
, params
? wstrdup(params
) : NULL
);
963 entry
->free_cdata
= wfree
;
965 } else if (strcmp(command
, "SAVE_SESSION") == 0) {
966 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
969 } else if (strcmp(command
, "CLEAR_SESSION") == 0) {
970 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
972 } else if (strcmp(command
, "INFO_PANEL") == 0) {
973 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
975 } else if (strcmp(command
, "LEGAL_PANEL") == 0) {
976 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
979 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
, command
);
984 if (shortcut
&& entry
) {
986 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
, title
);
988 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
990 entry
->rtext
= GetShortcutString(shortcut
);
992 entry->rtext = wstrdup(shortcut);
1001 /******************* Menu Configuration From File *******************/
1003 static void freeline(char *title
, char *command
, char *parameter
, char *shortcut
)
1011 static WMenu
*parseCascade(WScreen
* scr
, WMenu
* menu
, WMenuParser parser
)
1013 char *command
, *params
, *shortcut
, *title
;
1015 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
1017 if (command
== NULL
|| !command
[0]) {
1018 WMenuParserError(parser
, _("missing command in menu config") );
1019 freeline(title
, command
, params
, shortcut
);
1023 if (strcasecmp(command
, "MENU") == 0) {
1028 cascade
= wMenuCreate(scr
, M_(title
), False
);
1029 cascade
->on_destroy
= removeShortcutsForMenu
;
1030 if (!parseCascade(scr
, cascade
, parser
)) {
1031 wMenuDestroy(cascade
, True
);
1033 wMenuEntrySetCascade(menu
, wMenuAddCallback(menu
, M_(title
), NULL
, NULL
), cascade
);
1035 } else if (strcasecmp(command
, "END") == 0) {
1037 freeline(title
, command
, params
, shortcut
);
1041 addMenuEntry(menu
, M_(title
), shortcut
, command
, params
, WMenuParserGetFilename(parser
));
1043 freeline(title
, command
, params
, shortcut
);
1046 WMenuParserError(parser
, _("syntax error in menu file: END declaration missing") );
1052 static WMenu
*readMenu(WScreen
*scr
, const char *flat_file
, FILE *file
)
1056 char *title
, *command
, *params
, *shortcut
;
1058 parser
= WMenuParserCreate(flat_file
, file
, DEF_CONFIG_PATHS
);
1059 menu_parser_register_macros(parser
);
1061 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
1063 if (command
== NULL
|| !command
[0]) {
1064 WMenuParserError(parser
, _("missing command in menu config") );
1065 freeline(title
, command
, params
, shortcut
);
1068 if (strcasecmp(command
, "MENU") == 0) {
1069 menu
= wMenuCreate(scr
, M_(title
), True
);
1070 menu
->on_destroy
= removeShortcutsForMenu
;
1071 if (!parseCascade(scr
, menu
, parser
)) {
1072 wMenuDestroy(menu
, True
);
1075 freeline(title
, command
, params
, shortcut
);
1078 WMenuParserError(parser
, _("invalid menu, no menu title given") );
1079 freeline(title
, command
, params
, shortcut
);
1083 freeline(title
, command
, params
, shortcut
);
1086 WMenuParserDelete(parser
);
1090 static WMenu
*readMenuFile(WScreen
*scr
, const char *file_name
)
1095 file
= fopen(file_name
, "rb");
1097 werror(_("could not open menu file \"%s\": %s"), file_name
, strerror(errno
));
1100 menu
= readMenu(scr
, file_name
, file
);
1106 static inline int generate_command_from_list(char *buffer
, size_t buffer_size
, char **command_elements
)
1113 for (i
= 0; command_elements
[i
] != NULL
; i
++) {
1116 if (wr_idx
< buffer_size
- 1)
1117 buffer
[wr_idx
++] = ' ';
1119 for (rd
= command_elements
[i
]; *rd
!= '\0'; rd
++) {
1120 if (wr_idx
< buffer_size
- 1)
1121 buffer
[wr_idx
++] = *rd
;
1126 buffer
[wr_idx
] = '\0';
1130 /************ Menu Configuration From Pipe *************/
1131 static WMenu
*readPLMenuPipe(WScreen
* scr
, char **file_name
)
1133 WMPropList
*plist
= NULL
;
1136 char flat_file
[MAXLINE
];
1138 if (generate_command_from_list(flat_file
, sizeof(flat_file
), file_name
)) {
1139 werror(_("could not open menu file \"%s\": %s"),
1140 file_name
[0], _("pipe command for PropertyList is too long"));
1143 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1145 plist
= WMReadPropListFromPipe(filename
);
1150 menu
= configureMenu(scr
, plist
);
1152 WMReleasePropList(plist
);
1157 menu
->on_destroy
= removeShortcutsForMenu
;
1161 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
)
1166 char flat_file
[MAXLINE
];
1168 if (generate_command_from_list(flat_file
, sizeof(flat_file
), file_name
)) {
1169 werror(_("could not open menu file \"%s\": %s"),
1170 file_name
[0], _("pipe command is too long"));
1173 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1176 * In case of memory problem, 'popen' will not set the errno, so we initialise it
1177 * to be able to display a meaningful message. For other problems, 'popen' will
1178 * properly set errno, so we'll still get a good message
1181 file
= popen(filename
, "r");
1183 werror(_("could not open menu file \"%s\": %s"), filename
, strerror(errno
));
1186 menu
= readMenu(scr
, flat_file
, file
);
1197 static int myCompare(const void *d1
, const void *d2
)
1199 dir_data
*p1
= *(dir_data
**) d1
;
1200 dir_data
*p2
= *(dir_data
**) d2
;
1202 return strcmp(p1
->name
, p2
->name
);
1205 /***** Preset some macro for file parser *****/
1206 static void menu_parser_register_macros(WMenuParser parser
)
1211 // Used to return CPP verion, now returns wmaker's version
1212 WMenuParserRegisterSimpleMacro(parser
, "__VERSION__", VERSION
);
1214 // All macros below were historically defined by WindowMaker
1215 visual
= DefaultVisual(dpy
, DefaultScreen(dpy
));
1216 snprintf(buf
, sizeof(buf
), "%d", visual
->class);
1217 WMenuParserRegisterSimpleMacro(parser
, "VISUAL", buf
);
1219 snprintf(buf
, sizeof(buf
), "%d", DefaultDepth(dpy
, DefaultScreen(dpy
)) );
1220 WMenuParserRegisterSimpleMacro(parser
, "DEPTH", buf
);
1222 snprintf(buf
, sizeof(buf
), "%d", WidthOfScreen(DefaultScreenOfDisplay(dpy
)) );
1223 WMenuParserRegisterSimpleMacro(parser
, "SCR_WIDTH", buf
);
1225 snprintf(buf
, sizeof(buf
), "%d", HeightOfScreen(DefaultScreenOfDisplay(dpy
)) );
1226 WMenuParserRegisterSimpleMacro(parser
, "SCR_HEIGHT", buf
);
1228 WMenuParserRegisterSimpleMacro(parser
, "DISPLAY", XDisplayName(DisplayString(dpy
)) );
1230 WMenuParserRegisterSimpleMacro(parser
, "WM_VERSION", "\"" VERSION
"\"");
1233 /************ Menu Configuration From Directory *************/
1235 static Bool
isFilePackage(const char *file
)
1239 /* check if the extension indicates this file is a
1240 * file package. For now, only recognize .themed */
1244 if (l
> 7 && strcmp(&(file
[l
- 7]), ".themed") == 0) {
1251 static WMenu
*readMenuDirectory(WScreen
*scr
, const char *title
, char **path
, const char *command
)
1254 struct dirent
*dentry
;
1255 struct stat stat_buf
;
1258 WMArray
*dirs
= NULL
, *files
= NULL
;
1259 WMArrayIterator iter
;
1260 int length
, i
, have_space
= 0;
1262 int stripExtension
= 0;
1264 dirs
= WMCreateArray(16);
1265 files
= WMCreateArray(16);
1268 while (path
[i
] != NULL
) {
1269 if (strcmp(path
[i
], "-noext") == 0) {
1275 dir
= opendir(path
[i
]);
1281 while ((dentry
= readdir(dir
))) {
1283 if (strcmp(dentry
->d_name
, ".") == 0 || strcmp(dentry
->d_name
, "..") == 0)
1286 if (dentry
->d_name
[0] == '.')
1289 buffer
= malloc(strlen(path
[i
]) + strlen(dentry
->d_name
) + 4);
1291 werror(_("out of memory while constructing directory menu %s"), path
[i
]);
1295 strcpy(buffer
, path
[i
]);
1296 strcat(buffer
, "/");
1297 strcat(buffer
, dentry
->d_name
);
1299 if (stat(buffer
, &stat_buf
) != 0) {
1300 werror(_("%s:could not stat file \"%s\" in menu directory"),
1301 path
[i
], dentry
->d_name
);
1303 Bool isFilePack
= False
;
1306 if (S_ISDIR(stat_buf
.st_mode
)
1307 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1309 /* access always returns success for user root */
1310 if (access(buffer
, X_OK
) == 0) {
1311 /* Directory is accesible. Add to directory list */
1313 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1314 data
->name
= wstrdup(dentry
->d_name
);
1317 WMAddToArray(dirs
, data
);
1319 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1320 /* Hack because access always returns X_OK success for user root */
1321 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1322 if ((command
!= NULL
&& access(buffer
, R_OK
) == 0) ||
1323 (command
== NULL
&& access(buffer
, X_OK
) == 0 &&
1324 (stat_buf
.st_mode
& S_IXANY
))) {
1326 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1327 data
->name
= wstrdup(dentry
->d_name
);
1330 WMAddToArray(files
, data
);
1341 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1347 WMSortArray(dirs
, myCompare
);
1348 WMSortArray(files
, myCompare
);
1350 menu
= wMenuCreate(scr
, M_(title
), False
);
1351 menu
->on_destroy
= removeShortcutsForMenu
;
1353 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1354 /* New directory. Use same OPEN_MENU command that was used
1355 * for the current directory. */
1356 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1360 length
+= strlen(command
) + 6;
1361 buffer
= malloc(length
);
1363 werror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1369 strcat(buffer
, "-noext ");
1371 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1374 strcat(buffer
, "\"");
1375 strcat(buffer
, path
[data
->index
]);
1377 strcat(buffer
, "/");
1378 strcat(buffer
, data
->name
);
1380 strcat(buffer
, "\"");
1382 strcat(buffer
, " WITH ");
1383 strcat(buffer
, command
);
1386 addMenuEntry(menu
, M_(data
->name
), NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1393 WM_ITERATE_ARRAY(files
, data
, iter
) {
1394 /* executable: add as entry */
1395 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1397 length
+= strlen(command
);
1399 buffer
= malloc(length
);
1401 werror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1405 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1406 if (command
!= NULL
) {
1407 strcpy(buffer
, command
);
1408 strcat(buffer
, " ");
1410 strcat(buffer
, "\"");
1411 strcat(buffer
, path
[data
->index
]);
1416 strcat(buffer
, path
[data
->index
]);
1418 strcpy(buffer
, path
[data
->index
]);
1421 strcat(buffer
, "/");
1422 strcat(buffer
, data
->name
);
1424 strcat(buffer
, "\"");
1426 if (stripExtension
) {
1427 char *ptr
= strrchr(data
->name
, '.');
1428 if (ptr
&& ptr
!= data
->name
)
1431 addMenuEntry(menu
, M_(data
->name
), NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1444 /************ Menu Configuration From WMRootMenu *************/
1446 static WMenu
*makeDefaultMenu(WScreen
* scr
)
1450 menu
= wMenuCreate(scr
, _("Commands"), True
);
1451 wMenuAddCallback(menu
, M_("XTerm"), execCommand
, "xterm");
1452 wMenuAddCallback(menu
, M_("rxvt"), execCommand
, "rxvt");
1453 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1454 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1459 *----------------------------------------------------------------------
1461 * Reads root menu configuration from defaults database.
1463 *----------------------------------------------------------------------
1465 static WMenu
*configureMenu(WScreen
*scr
, WMPropList
*definition
)
1470 WMPropList
*title
, *command
, *params
;
1473 if (WMIsPLString(definition
)) {
1474 struct stat stat_buf
;
1476 Bool menu_is_default
= False
;
1478 /* menu definition is a string. Probably a path, so parse the file */
1480 tmp
= wexpandpath(WMGetFromPLString(definition
));
1482 path
= getLocalizedMenuFile(tmp
);
1485 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1488 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1489 menu_is_default
= True
;
1493 werror(_("could not find menu file \"%s\" referenced in WMRootMenu"), tmp
);
1498 if (stat(path
, &stat_buf
) < 0) {
1499 werror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1505 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1506 /* if the pointer in WMRootMenu has changed */
1507 || w_global
.domain
.root_menu
->timestamp
> scr
->root_menu
->timestamp
) {
1509 if (menu_is_default
) {
1511 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1515 menu
= readMenuFile(scr
, path
);
1517 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, w_global
.domain
.root_menu
->timestamp
);
1527 count
= WMGetPropListItemCount(definition
);
1531 elem
= WMGetFromPLArray(definition
, 0);
1532 if (!WMIsPLString(elem
)) {
1533 tmp
= WMGetPropListDescription(elem
, False
);
1534 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1538 mtitle
= WMGetFromPLString(elem
);
1540 menu
= wMenuCreate(scr
, M_(mtitle
), False
);
1541 menu
->on_destroy
= removeShortcutsForMenu
;
1543 for (i
= 1; i
< count
; i
++) {
1544 elem
= WMGetFromPLArray(definition
, i
);
1546 if (WMIsPLString(elem
)) {
1549 file
= WMGetFromPLString(elem
);
1553 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1556 if (WMIsPLArray(WMGetFromPLArray(elem
, 1))) {
1561 submenu
= configureMenu(scr
, elem
);
1563 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1564 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1568 WMPropList
*shortcut
;
1571 title
= WMGetFromPLArray(elem
, idx
++);
1572 shortcut
= WMGetFromPLArray(elem
, idx
++);
1573 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT") == 0) {
1574 shortcut
= WMGetFromPLArray(elem
, idx
++);
1575 command
= WMGetFromPLArray(elem
, idx
++);
1580 params
= WMGetFromPLArray(elem
, idx
++);
1582 if (!title
|| !command
)
1585 addMenuEntry(menu
, M_(WMGetFromPLString(title
)),
1586 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1587 WMGetFromPLString(command
),
1588 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1593 tmp
= WMGetPropListDescription(elem
, False
);
1594 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1602 *----------------------------------------------------------------------
1604 * Opens the root menu, parsing the menu configuration from the
1605 * defaults database.
1606 * If the menu is already mapped and is not sticked to the
1607 * root window, it will be unmapped.
1610 * The menu may be remade.
1613 * Construction of OPEN_MENU entries are delayed to the moment the
1615 *----------------------------------------------------------------------
1617 void OpenRootMenu(WScreen
* scr
, int x
, int y
, int keyboard
)
1620 WMPropList
*definition
;
1622 static WMPropList *domain=NULL;
1625 domain = WMCreatePLString("WMRootMenu");
1629 scr
->flags
.root_menu_changed_shortcuts
= 0;
1630 scr
->flags
.added_workspace_menu
= 0;
1631 scr
->flags
.added_windows_menu
= 0;
1633 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1634 menu
= scr
->root_menu
;
1635 if (!menu
->flags
.buttoned
) {
1638 wRaiseFrame(menu
->frame
->core
);
1641 wMenuMapAt(menu
, 0, 0, True
);
1643 wMenuMapCopyAt(menu
, x
- menu
->frame
->core
->width
/ 2, y
);
1648 definition
= w_global
.domain
.root_menu
->dictionary
;
1651 definition = PLGetDomain(domain);
1654 if (WMIsPLArray(definition
)) {
1655 if (!scr
->root_menu
|| w_global
.domain
.root_menu
->timestamp
> scr
->root_menu
->timestamp
) {
1656 menu
= configureMenu(scr
, definition
);
1658 menu
->timestamp
= w_global
.domain
.root_menu
->timestamp
;
1663 menu
= configureMenu(scr
, definition
);
1668 /* menu hasn't changed or could not be read */
1669 if (!scr
->root_menu
) {
1670 wMessageDialog(scr
, _("Error"),
1671 _("The applications menu could not be loaded. "
1672 "Look at the console output for a detailed "
1673 "description of the errors."), _("OK"), NULL
, NULL
);
1675 menu
= makeDefaultMenu(scr
);
1676 scr
->root_menu
= menu
;
1678 menu
= scr
->root_menu
;
1681 if (scr
->root_menu
) {
1682 wMenuDestroy(scr
->root_menu
, True
);
1684 scr
->root_menu
= menu
;
1689 if (keyboard
&& x
== 0 && y
== 0) {
1691 } else if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
1692 newx
= x
- menu
->frame
->core
->width
/ 2;
1693 newy
= y
- menu
->frame
->core
->height
/ 2;
1695 newx
= x
- menu
->frame
->core
->width
/ 2;
1698 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1701 if (scr
->flags
.root_menu_changed_shortcuts
)
1702 rebindKeygrabs(scr
);