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"
53 #include "xmodifier.h"
55 #include <WINGs/WUtil.h>
57 #define MAX_SHORTCUT_LENGTH 32
60 extern WDDomain
*WDRootMenu
;
61 extern Cursor wCursor
[WCUR_LAST
];
62 extern WPreferences wPreferences
;
64 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
);
65 static WMenu
*readMenuFile(WScreen
* scr
, char *file_name
);
66 static WMenu
*readMenuDirectory(WScreen
* scr
, char *title
, char **file_name
, char *command
);
67 static WMenu
*configureMenu(WScreen
* scr
, WMPropList
* definition
, Bool includeGlobals
);
68 static void menu_parser_register_macros(WMenuParser parser
);
70 typedef struct Shortcut
{
71 struct Shortcut
*next
;
79 static Shortcut
*shortcutList
= NULL
;
85 * "Title" EXEC command_to_exec -params
87 * "Title" EXEC command_to_exec -params
89 * "Workspaces" WORKSPACE_MENU
90 * "Title" built_in_command
92 * "Quick Quit" EXIT QUICK
95 * Commands may be preceded by SHORTCUT key
99 * INFO_PANEL - shows the Info Panel
100 * LEGAL_PANEL - shows the Legal info panel
101 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
102 * REFRESH - forces the desktop to be repainted
103 * EXIT [QUICK] - exit the window manager [without confirmation]
104 * EXEC <program> - execute an external program
105 * SHEXEC <command> - execute a shell command
106 * WORKSPACE_MENU - places the workspace submenu
108 * RESTART [<window manager>] - restarts the window manager
109 * SHOW_ALL - unhide all windows on workspace
110 * HIDE_OTHERS - hides all windows excep the focused one
111 * OPEN_MENU file - read menu data from file which must be a valid menu file.
112 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
113 * - read menu data from directory(ies) and
114 * eventually precede each with a command.
115 * OPEN_MENU | command
116 * - opens command and uses its stdout to construct and insert
117 * the resulting menu in current position. The output of
118 * command must be a valid menu description.
119 * The space between '|' and command is optional.
120 * || will do the same, but will not cache the contents.
121 * SAVE_SESSION - saves the current state of the desktop, which include
122 * all running applications, all their hints (geometry,
123 * position on screen, workspace they live on, the dock
124 * or clip from where they were launched, and
125 * if minimized, shaded or hidden. Also saves the current
126 * workspace the user is on. All will be restored on every
127 * start of windowmaker until another SAVE_SESSION or
128 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
129 * WindowMaker domain file, then saving is automatically
130 * done on every windowmaker exit, overwriting any
131 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
133 * CLEAR_SESSION - clears any previous saved session. This will not have
134 * any effect if SaveSessionOnExit is True.
142 static void execCommand(WMenu
* menu
, WMenuEntry
* entry
)
146 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char *)entry
->clientdata
);
148 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
149 GrabModeAsync
, GrabModeAsync
, None
, wCursor
[WCUR_WAIT
], CurrentTime
);
153 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
156 XUngrabPointer(dpy
, CurrentTime
);
160 static void exitCommand(WMenu
* menu
, WMenuEntry
* entry
)
162 static int inside
= 0;
165 /* prevent reentrant calls */
175 if ((long)entry
->clientdata
== M_QUICK
) {
178 int r
, oldSaveSessionFlag
;
180 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
181 r
= wExitDialog(menu
->frame
->screen_ptr
, _("Exit"),
182 _("Exit window manager?"), _("Exit"), _("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
;
192 if (result
== R_EXIT
)
193 Shutdown(WSExitMode
);
200 static void shutdownCommand(WMenu
* menu
, WMenuEntry
* entry
)
202 static int inside
= 0;
205 /* prevent reentrant calls */
215 if ((long)entry
->clientdata
== M_QUICK
)
218 int r
, oldSaveSessionFlag
;
220 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
222 r
= wExitDialog(menu
->frame
->screen_ptr
,
224 _("Kill Window System session?\n"
225 "(all applications will be closed)"), _("Kill"), _("Cancel"), NULL
);
226 if (r
== WAPRDefault
) {
228 } else if (r
== WAPRAlternate
) {
229 /* Don't modify the "save session on exit" flag if the
230 * user canceled the operation. */
231 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
235 if (result
!= R_CANCEL
) {
236 Shutdown(WSKillMode
);
244 static void restartCommand(WMenu
* menu
, WMenuEntry
* entry
)
246 Shutdown(WSRestartPreparationMode
);
247 Restart((char *)entry
->clientdata
, False
);
251 static void refreshCommand(WMenu
* menu
, WMenuEntry
* entry
)
253 wRefreshDesktop(menu
->frame
->screen_ptr
);
256 static void arrangeIconsCommand(WMenu
* menu
, WMenuEntry
* entry
)
258 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
261 static void showAllCommand(WMenu
* menu
, WMenuEntry
* entry
)
263 wShowAllWindows(menu
->frame
->screen_ptr
);
266 static void hideOthersCommand(WMenu
* menu
, WMenuEntry
* entry
)
268 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
271 static void saveSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
273 if (!wPreferences
.save_session_on_exit
)
274 wSessionSaveState(menu
->frame
->screen_ptr
);
276 wScreenSaveState(menu
->frame
->screen_ptr
);
279 static void clearSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
281 wSessionClearState(menu
->frame
->screen_ptr
);
282 wScreenSaveState(menu
->frame
->screen_ptr
);
285 static void infoPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
287 wShowInfoPanel(menu
->frame
->screen_ptr
);
290 static void legalPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
292 wShowLegalPanel(menu
->frame
->screen_ptr
);
295 /********************************************************************/
297 static char * getLocalizedMenuFile(char *menu
)
299 char *buffer
, *ptr
, *locale
;
305 len
= strlen(menu
) + strlen(Locale
) + 8;
306 buffer
= wmalloc(len
);
308 /* try menu.locale_name */
309 snprintf(buffer
, len
, "%s.%s", menu
, Locale
);
310 if (access(buffer
, F_OK
) == 0)
313 /* position of locale in our buffer */
314 locale
= buffer
+ strlen(menu
) + 1;
316 /* check if it is in the form aa_bb.encoding and check for aa_bb */
317 ptr
= strchr(locale
, '.');
320 if (access(buffer
, F_OK
) == 0)
324 /* now check for aa */
325 ptr
= strchr(locale
, '_');
328 if (access(buffer
, F_OK
) == 0)
337 Bool
wRootMenuPerformShortcut(XEvent
* event
)
339 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
344 /* ignore CapsLock */
345 modifiers
= event
->xkey
.state
& ValidModMask
;
347 for (ptr
= shortcutList
; ptr
!= NULL
; ptr
= ptr
->next
) {
348 if (ptr
->keycode
== 0 || ptr
->menu
->menu
->screen_ptr
!= scr
)
351 if (ptr
->keycode
== event
->xkey
.keycode
&& ptr
->modifier
== modifiers
) {
352 (*ptr
->entry
->callback
) (ptr
->menu
, ptr
->entry
);
360 void wRootMenuBindShortcuts(Window window
)
366 if (ptr
->modifier
!= AnyModifier
) {
367 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
| LockMask
,
368 window
, True
, GrabModeAsync
, GrabModeAsync
);
370 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
373 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
378 static void rebindKeygrabs(WScreen
* scr
)
382 wwin
= scr
->focused_window
;
384 while (wwin
!= NULL
) {
385 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
387 if (!WFLAGP(wwin
, no_bind_keys
)) {
388 wWindowSetKeyGrabs(wwin
);
394 static void removeShortcutsForMenu(WMenu
* menu
)
397 Shortcut
*newList
= NULL
;
400 while (ptr
!= NULL
) {
402 if (ptr
->menu
== menu
) {
410 shortcutList
= newList
;
411 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
414 static Bool
addShortcut(const char *file
, char *shortcutDefinition
, WMenu
* menu
, WMenuEntry
* entry
)
419 char buf
[MAX_SHORTCUT_LENGTH
], *b
;
421 ptr
= wmalloc(sizeof(Shortcut
));
423 wstrlcpy(buf
, shortcutDefinition
, MAX_SHORTCUT_LENGTH
);
428 while ((k
= strchr(b
, '+')) != NULL
) {
432 mod
= wXModifierFromKey(b
);
434 wwarning(_("%s: invalid key modifier \"%s\""), file
, b
);
438 ptr
->modifier
|= mod
;
444 ksym
= XStringToKeysym(b
);
446 if (ksym
== NoSymbol
) {
447 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
448 file
, shortcutDefinition
, entry
->text
);
453 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
454 if (ptr
->keycode
== 0) {
455 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
456 shortcutDefinition
, entry
->text
);
464 ptr
->next
= shortcutList
;
467 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
472 static char *next_token(char *line
, char **next
)
478 while (*line
== ' ' || *line
== '\t')
486 while (*tmp
!= 0 && *tmp
!= '"')
489 wwarning(_("%s: unmatched '\"' in menu file"), line
);
500 } while (*tmp
!= 0 && *tmp
!= ' ' && *tmp
!= '\t');
514 while (*tmp
== ' ' || *tmp
== '\t')
523 static void separateCommand(char *line
, char ***file
, char **command
)
525 char *token
, *tmp
= line
;
526 WMArray
*array
= WMCreateArray(4);
532 token
= next_token(tmp
, &tmp
);
534 if (strcmp(token
, "WITH") == 0) {
535 if (tmp
!= NULL
&& *tmp
!= 0)
536 *command
= wstrdup(tmp
);
538 wwarning(_("%s: missing command"), line
);
541 WMAddToArray(array
, token
);
543 } while (token
!= NULL
&& tmp
!= NULL
);
545 count
= WMGetArrayItemCount(array
);
547 *file
= wmalloc(sizeof(char *) * (count
+ 1));
548 (*file
)[count
] = NULL
;
549 for (i
= 0; i
< count
; i
++) {
550 (*file
)[i
] = WMGetFromArray(array
, i
);
556 static WMenu
*constructPLMenu(WScreen
*screen
, char *path
)
558 WMPropList
*pl
= NULL
;
564 pl
= WMReadPropListFromFile(path
);
568 menu
= configureMenu(screen
, pl
, False
);
572 menu
->on_destroy
= removeShortcutsForMenu
;
576 static void constructMenu(WMenu
* menu
, WMenuEntry
* entry
)
579 struct stat stat_buf
;
586 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
587 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
588 wwarning(_("invalid OPEN_MENU specification: %s"), (char *)entry
->clientdata
);
594 if (path
[0][0] == '|') {
597 if (!menu
->cascades
[entry
->cascade
] || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
600 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
602 if (submenu
!= NULL
) {
603 if (path
[0][1] == '|')
604 submenu
->timestamp
= 0;
606 submenu
->timestamp
= 1; /* there's no automatic reloading */
614 /* try interpreting path as a proplist file */
615 submenu
= constructPLMenu(menu
->frame
->screen_ptr
, path
[0]);
616 /* if unsuccessful, try it as an old-style file */
620 while (path
[i
] != NULL
) {
623 if (strcmp(path
[i
], "-noext") == 0) {
628 tmp
= wexpandpath(path
[i
]);
630 lpath
= getLocalizedMenuFile(tmp
);
639 if (stat(path
[i
], &stat_buf
) == 0) {
640 if (last
< stat_buf
.st_mtime
)
641 last
= stat_buf
.st_mtime
;
645 werror(_("%s:could not stat menu"), path
[i
]);
653 werror(_("%s:could not stat menu:%s"), "OPEN_MENU", (char *)entry
->clientdata
);
656 stat(path
[first
], &stat_buf
);
657 if (!menu
->cascades
[entry
->cascade
]
658 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
660 if (S_ISDIR(stat_buf
.st_mode
)) {
662 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
, entry
->text
, path
, cmd
);
664 submenu
->timestamp
= last
;
665 } else if (S_ISREG(stat_buf
.st_mode
)) {
669 wwarning(_("too many parameters in OPEN_MENU: %s"),
670 (char *)entry
->clientdata
);
672 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
674 submenu
->timestamp
= stat_buf
.st_mtime
;
685 wMenuEntryRemoveCascade(menu
, entry
);
686 wMenuEntrySetCascade(menu
, entry
, submenu
);
691 while (path
[i
] != NULL
)
698 static void cleanupWorkspaceMenu(WMenu
* menu
)
700 if (menu
->frame
->screen_ptr
->workspace_menu
== menu
)
701 menu
->frame
->screen_ptr
->workspace_menu
= NULL
;
704 static WMenuEntry
*addWorkspaceMenu(WScreen
* scr
, WMenu
* menu
, char *title
)
709 if (scr
->flags
.added_workspace_menu
) {
711 ("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
714 scr
->flags
.added_workspace_menu
= 1;
716 wsmenu
= wWorkspaceMenuMake(scr
, True
);
717 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
719 scr
->workspace_menu
= wsmenu
;
720 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
721 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
723 wWorkspaceMenuUpdate(scr
, wsmenu
);
728 static void cleanupWindowsMenu(WMenu
* menu
)
730 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
731 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
734 static WMenuEntry
*addWindowsMenu(WScreen
* scr
, WMenu
* menu
, char *title
)
740 if (scr
->flags
.added_windows_menu
) {
742 ("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
745 scr
->flags
.added_windows_menu
= 1;
747 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
748 wwmenu
->on_destroy
= cleanupWindowsMenu
;
749 scr
->switch_menu
= wwmenu
;
750 wwin
= scr
->focused_window
;
752 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
756 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
757 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
762 static WMenuEntry
*addMenuEntry(WMenu
* menu
, char *title
, char *shortcut
, char *command
,
763 char *params
, const char *file_name
)
766 WMenuEntry
*entry
= NULL
;
767 Bool shortcutOk
= False
;
771 scr
= menu
->frame
->screen_ptr
;
772 if (strcmp(command
, "OPEN_MENU") == 0) {
774 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
779 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
781 path
= wstrdup(params
);
783 dummy
= wMenuCreate(scr
, title
, False
);
784 dummy
->on_destroy
= removeShortcutsForMenu
;
785 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
786 entry
->free_cdata
= wfree
;
787 wMenuEntrySetCascade(menu
, entry
, dummy
);
789 } else if (strcmp(command
, "EXEC") == 0) {
791 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
793 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrconcat("exec ", params
));
794 entry
->free_cdata
= wfree
;
797 } else if (strcmp(command
, "SHEXEC") == 0) {
799 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
801 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrdup(params
));
802 entry
->free_cdata
= wfree
;
805 } else if (strcmp(command
, "EXIT") == 0) {
807 if (params
&& strcmp(params
, "QUICK") == 0)
808 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void *)M_QUICK
);
810 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
813 } else if (strcmp(command
, "SHUTDOWN") == 0) {
815 if (params
&& strcmp(params
, "QUICK") == 0)
816 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, (void *)M_QUICK
);
818 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
821 } else if (strcmp(command
, "REFRESH") == 0) {
822 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
825 } else if (strcmp(command
, "WORKSPACE_MENU") == 0) {
826 entry
= addWorkspaceMenu(scr
, menu
, title
);
829 } else if (strcmp(command
, "WINDOWS_MENU") == 0) {
830 entry
= addWindowsMenu(scr
, menu
, title
);
833 } else if (strcmp(command
, "ARRANGE_ICONS") == 0) {
834 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
837 } else if (strcmp(command
, "HIDE_OTHERS") == 0) {
838 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
841 } else if (strcmp(command
, "SHOW_ALL") == 0) {
842 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
845 } else if (strcmp(command
, "RESTART") == 0) {
846 entry
= wMenuAddCallback(menu
, title
, restartCommand
, params
? wstrdup(params
) : NULL
);
847 entry
->free_cdata
= wfree
;
849 } else if (strcmp(command
, "SAVE_SESSION") == 0) {
850 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
853 } else if (strcmp(command
, "CLEAR_SESSION") == 0) {
854 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
856 } else if (strcmp(command
, "INFO_PANEL") == 0) {
857 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
859 } else if (strcmp(command
, "LEGAL_PANEL") == 0) {
860 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
863 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
, command
);
868 if (shortcut
&& entry
) {
870 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
, title
);
872 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
874 entry
->rtext
= GetShortcutString(shortcut
);
876 entry->rtext = wstrdup(shortcut);
885 /******************* Menu Configuration From File *******************/
887 static void freeline(char *title
, char *command
, char *parameter
, char *shortcut
)
895 static WMenu
*parseCascade(WScreen
* scr
, WMenu
* menu
, WMenuParser parser
)
897 char *command
, *params
, *shortcut
, *title
;
899 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
901 if (command
== NULL
|| !command
[0]) {
902 WMenuParserError(parser
, _("missing command in menu config") );
903 freeline(title
, command
, params
, shortcut
);
907 if (strcasecmp(command
, "MENU") == 0) {
912 cascade
= wMenuCreate(scr
, M_(title
), False
);
913 cascade
->on_destroy
= removeShortcutsForMenu
;
914 if (!parseCascade(scr
, cascade
, parser
)) {
915 wMenuDestroy(cascade
, True
);
917 wMenuEntrySetCascade(menu
, wMenuAddCallback(menu
, M_(title
), NULL
, NULL
), cascade
);
919 } else if (strcasecmp(command
, "END") == 0) {
921 freeline(title
, command
, params
, shortcut
);
925 addMenuEntry(menu
, M_(title
), shortcut
, command
, params
, WMenuParserGetFilename(parser
));
927 freeline(title
, command
, params
, shortcut
);
930 WMenuParserError(parser
, _("syntax error in menu file: END declaration missing") );
936 static WMenu
*readMenuFile(WScreen
* scr
, char *file_name
)
941 char *command
, *params
, *shortcut
, *title
;
943 file
= fopen(file_name
, "rb");
945 werror(_("%s:could not open menu file"), file_name
);
948 parser
= WMenuParserCreate(file_name
, file
, DEF_CONFIG_PATHS
);
949 menu_parser_register_macros(parser
);
951 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
953 if (command
== NULL
|| !command
[0]) {
954 WMenuParserError(parser
, _("missing command in menu config") );
955 freeline(title
, command
, params
, shortcut
);
958 if (strcasecmp(command
, "MENU") == 0) {
959 menu
= wMenuCreate(scr
, M_(title
), True
);
960 menu
->on_destroy
= removeShortcutsForMenu
;
961 if (!parseCascade(scr
, menu
, parser
)) {
962 wMenuDestroy(menu
, True
);
965 freeline(title
, command
, params
, shortcut
);
968 WMenuParserError(parser
, _("invalid menu file, MENU command is missing") );
969 freeline(title
, command
, params
, shortcut
);
972 freeline(title
, command
, params
, shortcut
);
975 WMenuParserDelete(parser
);
981 /************ Menu Configuration From Pipe *************/
983 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
)
988 char *command
, *params
, *shortcut
, *title
;
990 char flat_file
[MAXLINE
];
995 for (i
= 0; file_name
[i
] != NULL
; i
++) {
996 strcat(flat_file
, file_name
[i
]);
997 strcat(flat_file
, " ");
999 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1001 file
= popen(filename
, "r");
1003 werror(_("%s:could not open menu file"), filename
);
1006 parser
= WMenuParserCreate(flat_file
, file
, DEF_CONFIG_PATHS
);
1007 menu_parser_register_macros(parser
);
1009 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
1011 if (command
== NULL
|| !command
[0]) {
1012 WMenuParserError(parser
, _("missing command in menu config") );
1013 freeline(title
, command
, params
, shortcut
);
1016 if (strcasecmp(command
, "MENU") == 0) {
1017 menu
= wMenuCreate(scr
, M_(title
), True
);
1018 menu
->on_destroy
= removeShortcutsForMenu
;
1019 if (!parseCascade(scr
, menu
, parser
)) {
1020 wMenuDestroy(menu
, True
);
1023 freeline(title
, command
, params
, shortcut
);
1026 WMenuParserError(parser
, _("no title given for the root menu") );
1027 freeline(title
, command
, params
, shortcut
);
1031 freeline(title
, command
, params
, shortcut
);
1034 WMenuParserDelete(parser
);
1045 static int myCompare(const void *d1
, const void *d2
)
1047 dir_data
*p1
= *(dir_data
**) d1
;
1048 dir_data
*p2
= *(dir_data
**) d2
;
1050 return strcmp(p1
->name
, p2
->name
);
1053 /***** Preset some macro for file parser *****/
1054 static void menu_parser_register_macros(WMenuParser parser
)
1059 // Used to return CPP verion, now returns wmaker's version
1060 WMenuParserRegisterSimpleMacro(parser
, "__VERSION__", VERSION
);
1062 // All macros below were historically defined by WindowMaker
1063 visual
= DefaultVisual(dpy
, DefaultScreen(dpy
));
1064 snprintf(buf
, sizeof(buf
), "%d", visual
->class);
1065 WMenuParserRegisterSimpleMacro(parser
, "VISUAL", buf
);
1067 snprintf(buf
, sizeof(buf
), "%d", DefaultDepth(dpy
, DefaultScreen(dpy
)) );
1068 WMenuParserRegisterSimpleMacro(parser
, "DEPTH", buf
);
1070 snprintf(buf
, sizeof(buf
), "%d", WidthOfScreen(DefaultScreenOfDisplay(dpy
)) );
1071 WMenuParserRegisterSimpleMacro(parser
, "SCR_WIDTH", buf
);
1073 snprintf(buf
, sizeof(buf
), "%d", HeightOfScreen(DefaultScreenOfDisplay(dpy
)) );
1074 WMenuParserRegisterSimpleMacro(parser
, "SCR_HEIGHT", buf
);
1076 WMenuParserRegisterSimpleMacro(parser
, "DISPLAY", XDisplayName(DisplayString(dpy
)) );
1078 WMenuParserRegisterSimpleMacro(parser
, "WM_VERSION", "\"" VERSION
"\"");
1081 /************ Menu Configuration From Directory *************/
1083 static Bool
isFilePackage(char *file
)
1087 /* check if the extension indicates this file is a
1088 * file package. For now, only recognize .themed */
1092 if (l
> 7 && strcmp(&(file
[l
- 7]), ".themed") == 0) {
1099 static WMenu
*readMenuDirectory(WScreen
* scr
, char *title
, char **path
, char *command
)
1102 struct dirent
*dentry
;
1103 struct stat stat_buf
;
1106 WMArray
*dirs
= NULL
, *files
= NULL
;
1107 WMArrayIterator iter
;
1108 int length
, i
, have_space
= 0;
1110 int stripExtension
= 0;
1112 dirs
= WMCreateArray(16);
1113 files
= WMCreateArray(16);
1116 while (path
[i
] != NULL
) {
1117 if (strcmp(path
[i
], "-noext") == 0) {
1123 dir
= opendir(path
[i
]);
1129 while ((dentry
= readdir(dir
))) {
1131 if (strcmp(dentry
->d_name
, ".") == 0 || strcmp(dentry
->d_name
, "..") == 0)
1134 if (dentry
->d_name
[0] == '.')
1137 buffer
= malloc(strlen(path
[i
]) + strlen(dentry
->d_name
) + 4);
1139 werror(_("out of memory while constructing directory menu %s"), path
[i
]);
1143 strcpy(buffer
, path
[i
]);
1144 strcat(buffer
, "/");
1145 strcat(buffer
, dentry
->d_name
);
1147 if (stat(buffer
, &stat_buf
) != 0) {
1148 werror(_("%s:could not stat file \"%s\" in menu directory"),
1149 path
[i
], dentry
->d_name
);
1151 Bool isFilePack
= False
;
1154 if (S_ISDIR(stat_buf
.st_mode
)
1155 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1157 /* access always returns success for user root */
1158 if (access(buffer
, X_OK
) == 0) {
1159 /* Directory is accesible. Add to directory list */
1161 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1162 data
->name
= wstrdup(dentry
->d_name
);
1165 WMAddToArray(dirs
, data
);
1167 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1168 /* Hack because access always returns X_OK success for user root */
1169 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1170 if ((command
!= NULL
&& access(buffer
, R_OK
) == 0) ||
1171 (command
== NULL
&& access(buffer
, X_OK
) == 0 &&
1172 (stat_buf
.st_mode
& S_IXANY
))) {
1174 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1175 data
->name
= wstrdup(dentry
->d_name
);
1178 WMAddToArray(files
, data
);
1189 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1195 WMSortArray(dirs
, myCompare
);
1196 WMSortArray(files
, myCompare
);
1198 menu
= wMenuCreate(scr
, M_(title
), False
);
1199 menu
->on_destroy
= removeShortcutsForMenu
;
1201 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1202 /* New directory. Use same OPEN_MENU command that was used
1203 * for the current directory. */
1204 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1208 length
+= strlen(command
) + 6;
1209 buffer
= malloc(length
);
1211 werror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1217 strcat(buffer
, "-noext ");
1219 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1222 strcat(buffer
, "\"");
1223 strcat(buffer
, path
[data
->index
]);
1225 strcat(buffer
, "/");
1226 strcat(buffer
, data
->name
);
1228 strcat(buffer
, "\"");
1230 strcat(buffer
, " WITH ");
1231 strcat(buffer
, command
);
1234 addMenuEntry(menu
, M_(data
->name
), NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1242 WM_ITERATE_ARRAY(files
, data
, iter
) {
1243 /* executable: add as entry */
1244 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1246 length
+= strlen(command
);
1248 buffer
= malloc(length
);
1250 werror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1254 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1255 if (command
!= NULL
) {
1256 strcpy(buffer
, command
);
1257 strcat(buffer
, " ");
1259 strcat(buffer
, "\"");
1260 strcat(buffer
, path
[data
->index
]);
1265 strcat(buffer
, path
[data
->index
]);
1267 strcpy(buffer
, path
[data
->index
]);
1270 strcat(buffer
, "/");
1271 strcat(buffer
, data
->name
);
1273 strcat(buffer
, "\"");
1275 if (stripExtension
) {
1276 char *ptr
= strrchr(data
->name
, '.');
1277 if (ptr
&& ptr
!= data
->name
)
1280 addMenuEntry(menu
, M_(data
->name
), NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1294 /************ Menu Configuration From WMRootMenu *************/
1296 static WMenu
*makeDefaultMenu(WScreen
* scr
)
1300 menu
= wMenuCreate(scr
, _("Commands"), True
);
1301 wMenuAddCallback(menu
, M_("XTerm"), execCommand
, "xterm");
1302 wMenuAddCallback(menu
, M_("rxvt"), execCommand
, "rxvt");
1303 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1304 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1309 *----------------------------------------------------------------------
1311 * Reads root menu configuration from defaults database.
1313 *----------------------------------------------------------------------
1315 static WMenu
*configureMenu(WScreen
* scr
, WMPropList
* definition
, Bool includeGlobals
)
1320 WMPropList
*title
, *command
, *params
;
1323 if (WMIsPLString(definition
)) {
1324 struct stat stat_buf
;
1326 Bool menu_is_default
= False
;
1328 /* menu definition is a string. Probably a path, so parse the file */
1330 tmp
= wexpandpath(WMGetFromPLString(definition
));
1332 path
= getLocalizedMenuFile(tmp
);
1335 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1338 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1339 menu_is_default
= True
;
1343 werror(_("could not find menu file \"%s\" referenced in WMRootMenu"), tmp
);
1348 if (stat(path
, &stat_buf
) < 0) {
1349 werror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1355 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1356 /* if the pointer in WMRootMenu has changed */
1357 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1359 if (menu_is_default
) {
1361 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1365 menu
= readMenuFile(scr
, path
);
1367 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1377 count
= WMGetPropListItemCount(definition
);
1381 elem
= WMGetFromPLArray(definition
, 0);
1382 if (!WMIsPLString(elem
)) {
1383 tmp
= WMGetPropListDescription(elem
, False
);
1384 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1388 mtitle
= WMGetFromPLString(elem
);
1390 menu
= wMenuCreate(scr
, M_(mtitle
), False
);
1391 menu
->on_destroy
= removeShortcutsForMenu
;
1393 #ifdef GLOBAL_SUBMENU_FILE
1394 if (includeGlobals
) {
1398 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1401 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1402 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1407 for (i
= 1; i
< count
; i
++) {
1408 elem
= WMGetFromPLArray(definition
, i
);
1410 if (WMIsPLString(elem
)) {
1413 file
= WMGetFromPLString(elem
);
1417 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1420 if (WMIsPLArray(WMGetFromPLArray(elem
, 1))) {
1425 submenu
= configureMenu(scr
, elem
, True
);
1427 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1428 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1432 WMPropList
*shortcut
;
1435 title
= WMGetFromPLArray(elem
, idx
++);
1436 shortcut
= WMGetFromPLArray(elem
, idx
++);
1437 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT") == 0) {
1438 shortcut
= WMGetFromPLArray(elem
, idx
++);
1439 command
= WMGetFromPLArray(elem
, idx
++);
1444 params
= WMGetFromPLArray(elem
, idx
++);
1446 if (!title
|| !command
)
1449 addMenuEntry(menu
, M_(WMGetFromPLString(title
)),
1450 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1451 WMGetFromPLString(command
),
1452 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1457 tmp
= WMGetPropListDescription(elem
, False
);
1458 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1466 *----------------------------------------------------------------------
1468 * Opens the root menu, parsing the menu configuration from the
1469 * defaults database.
1470 * If the menu is already mapped and is not sticked to the
1471 * root window, it will be unmapped.
1474 * The menu may be remade.
1477 * Construction of OPEN_MENU entries are delayed to the moment the
1479 *----------------------------------------------------------------------
1481 void OpenRootMenu(WScreen
* scr
, int x
, int y
, int keyboard
)
1484 WMPropList
*definition
;
1486 static WMPropList *domain=NULL;
1489 domain = WMCreatePLString("WMRootMenu");
1493 scr
->flags
.root_menu_changed_shortcuts
= 0;
1494 scr
->flags
.added_workspace_menu
= 0;
1495 scr
->flags
.added_windows_menu
= 0;
1497 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1498 menu
= scr
->root_menu
;
1499 if (!menu
->flags
.buttoned
) {
1502 wRaiseFrame(menu
->frame
->core
);
1505 wMenuMapAt(menu
, 0, 0, True
);
1507 wMenuMapCopyAt(menu
, x
- menu
->frame
->core
->width
/ 2, y
);
1512 definition
= WDRootMenu
->dictionary
;
1515 definition = PLGetDomain(domain);
1518 if (WMIsPLArray(definition
)) {
1519 if (!scr
->root_menu
|| WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1520 menu
= configureMenu(scr
, definition
, True
);
1522 menu
->timestamp
= WDRootMenu
->timestamp
;
1527 menu
= configureMenu(scr
, definition
, True
);
1532 /* menu hasn't changed or could not be read */
1533 if (!scr
->root_menu
) {
1534 wMessageDialog(scr
, _("Error"),
1535 _("The applications menu could not be loaded. "
1536 "Look at the console output for a detailed "
1537 "description of the errors."), _("OK"), NULL
, NULL
);
1539 menu
= makeDefaultMenu(scr
);
1540 scr
->root_menu
= menu
;
1542 menu
= scr
->root_menu
;
1545 if (scr
->root_menu
) {
1546 wMenuDestroy(scr
->root_menu
, True
);
1548 scr
->root_menu
= menu
;
1553 if (keyboard
&& x
== 0 && y
== 0) {
1555 } else if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
1556 newx
= x
- menu
->frame
->core
->width
/ 2;
1557 newy
= y
- menu
->frame
->core
->height
/ 2;
1559 newx
= x
- menu
->frame
->core
->width
/ 2;
1562 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1565 if (scr
->flags
.root_menu_changed_shortcuts
)
1566 rebindKeygrabs(scr
);