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
);
69 typedef struct Shortcut
{
70 struct Shortcut
*next
;
78 static Shortcut
*shortcutList
= NULL
;
84 * "Title" EXEC command_to_exec -params
86 * "Title" EXEC command_to_exec -params
88 * "Workspaces" WORKSPACE_MENU
89 * "Title" built_in_command
91 * "Quick Quit" EXIT QUICK
94 * Commands may be preceded by SHORTCUT key
98 * INFO_PANEL - shows the Info Panel
99 * LEGAL_PANEL - shows the Legal info panel
100 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
101 * REFRESH - forces the desktop to be repainted
102 * EXIT [QUICK] - exit the window manager [without confirmation]
103 * EXEC <program> - execute an external program
104 * SHEXEC <command> - execute a shell command
105 * WORKSPACE_MENU - places the workspace submenu
107 * RESTART [<window manager>] - restarts the window manager
108 * SHOW_ALL - unhide all windows on workspace
109 * HIDE_OTHERS - hides all windows excep the focused one
110 * OPEN_MENU file - read menu data from file which must be a valid menu file.
111 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
112 * - read menu data from directory(ies) and
113 * eventually precede each with a command.
114 * OPEN_MENU | command
115 * - opens command and uses its stdout to construct and insert
116 * the resulting menu in current position. The output of
117 * command must be a valid menu description.
118 * The space between '|' and command is optional.
119 * || will do the same, but will not cache the contents.
120 * SAVE_SESSION - saves the current state of the desktop, which include
121 * all running applications, all their hints (geometry,
122 * position on screen, workspace they live on, the dock
123 * or clip from where they were launched, and
124 * if minimized, shaded or hidden. Also saves the current
125 * workspace the user is on. All will be restored on every
126 * start of windowmaker until another SAVE_SESSION or
127 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
128 * WindowMaker domain file, then saving is automatically
129 * done on every windowmaker exit, overwriting any
130 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
132 * CLEAR_SESSION - clears any previous saved session. This will not have
133 * any effect if SaveSessionOnExit is True.
141 static void execCommand(WMenu
* menu
, WMenuEntry
* entry
)
145 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char *)entry
->clientdata
);
147 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
148 GrabModeAsync
, GrabModeAsync
, None
, wCursor
[WCUR_WAIT
], CurrentTime
);
152 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
155 XUngrabPointer(dpy
, CurrentTime
);
159 static void exitCommand(WMenu
* menu
, WMenuEntry
* entry
)
161 static int inside
= 0;
164 /* prevent reentrant calls */
174 if ((long)entry
->clientdata
== M_QUICK
) {
177 int r
, oldSaveSessionFlag
;
179 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
180 r
= wExitDialog(menu
->frame
->screen_ptr
, _("Exit"),
181 _("Exit window manager?"), _("Exit"), _("Cancel"), NULL
);
183 if (r
== WAPRDefault
) {
185 } else if (r
== WAPRAlternate
) {
186 /* Don't modify the "save session on exit" flag if the
187 * user canceled the operation. */
188 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
191 if (result
== R_EXIT
)
192 Shutdown(WSExitMode
);
199 static void shutdownCommand(WMenu
* menu
, WMenuEntry
* entry
)
201 static int inside
= 0;
204 /* prevent reentrant calls */
214 if ((long)entry
->clientdata
== M_QUICK
)
217 int r
, oldSaveSessionFlag
;
219 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
221 r
= wExitDialog(menu
->frame
->screen_ptr
,
223 _("Kill Window System session?\n"
224 "(all applications will be closed)"), _("Kill"), _("Cancel"), NULL
);
225 if (r
== WAPRDefault
) {
227 } else if (r
== WAPRAlternate
) {
228 /* Don't modify the "save session on exit" flag if the
229 * user canceled the operation. */
230 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
234 if (result
!= R_CANCEL
) {
235 Shutdown(WSKillMode
);
243 static void restartCommand(WMenu
* menu
, WMenuEntry
* entry
)
245 Shutdown(WSRestartPreparationMode
);
246 Restart((char *)entry
->clientdata
, False
);
250 static void refreshCommand(WMenu
* menu
, WMenuEntry
* entry
)
252 wRefreshDesktop(menu
->frame
->screen_ptr
);
255 static void arrangeIconsCommand(WMenu
* menu
, WMenuEntry
* entry
)
257 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
260 static void showAllCommand(WMenu
* menu
, WMenuEntry
* entry
)
262 wShowAllWindows(menu
->frame
->screen_ptr
);
265 static void hideOthersCommand(WMenu
* menu
, WMenuEntry
* entry
)
267 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
270 static void saveSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
272 if (!wPreferences
.save_session_on_exit
)
273 wSessionSaveState(menu
->frame
->screen_ptr
);
275 wScreenSaveState(menu
->frame
->screen_ptr
);
278 static void clearSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
280 wSessionClearState(menu
->frame
->screen_ptr
);
281 wScreenSaveState(menu
->frame
->screen_ptr
);
284 static void infoPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
286 wShowInfoPanel(menu
->frame
->screen_ptr
);
289 static void legalPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
291 wShowLegalPanel(menu
->frame
->screen_ptr
);
294 /********************************************************************/
296 static char * getLocalizedMenuFile(char *menu
)
298 char *buffer
, *ptr
, *locale
;
304 len
= strlen(menu
) + strlen(Locale
) + 8;
305 buffer
= wmalloc(len
);
307 /* try menu.locale_name */
308 snprintf(buffer
, len
, "%s.%s", menu
, Locale
);
309 if (access(buffer
, F_OK
) == 0)
312 /* position of locale in our buffer */
313 locale
= buffer
+ strlen(menu
) + 1;
315 /* check if it is in the form aa_bb.encoding and check for aa_bb */
316 ptr
= strchr(locale
, '.');
319 if (access(buffer
, F_OK
) == 0)
323 /* now check for aa */
324 ptr
= strchr(locale
, '_');
327 if (access(buffer
, F_OK
) == 0)
336 Bool
wRootMenuPerformShortcut(XEvent
* event
)
338 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
343 /* ignore CapsLock */
344 modifiers
= event
->xkey
.state
& ValidModMask
;
346 for (ptr
= shortcutList
; ptr
!= NULL
; ptr
= ptr
->next
) {
347 if (ptr
->keycode
== 0 || ptr
->menu
->menu
->screen_ptr
!= scr
)
350 if (ptr
->keycode
== event
->xkey
.keycode
&& ptr
->modifier
== modifiers
) {
351 (*ptr
->entry
->callback
) (ptr
->menu
, ptr
->entry
);
359 void wRootMenuBindShortcuts(Window window
)
365 if (ptr
->modifier
!= AnyModifier
) {
366 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
| LockMask
,
367 window
, True
, GrabModeAsync
, GrabModeAsync
);
369 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
372 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
377 static void rebindKeygrabs(WScreen
* scr
)
381 wwin
= scr
->focused_window
;
383 while (wwin
!= NULL
) {
384 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
386 if (!WFLAGP(wwin
, no_bind_keys
)) {
387 wWindowSetKeyGrabs(wwin
);
393 static void removeShortcutsForMenu(WMenu
* menu
)
396 Shortcut
*newList
= NULL
;
399 while (ptr
!= NULL
) {
401 if (ptr
->menu
== menu
) {
409 shortcutList
= newList
;
410 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
413 static Bool
addShortcut(const char *file
, char *shortcutDefinition
, WMenu
* menu
, WMenuEntry
* entry
)
418 char buf
[MAX_SHORTCUT_LENGTH
], *b
;
420 ptr
= wmalloc(sizeof(Shortcut
));
422 wstrlcpy(buf
, shortcutDefinition
, MAX_SHORTCUT_LENGTH
);
427 while ((k
= strchr(b
, '+')) != NULL
) {
431 mod
= wXModifierFromKey(b
);
433 wwarning(_("%s: invalid key modifier \"%s\""), file
, b
);
437 ptr
->modifier
|= mod
;
443 ksym
= XStringToKeysym(b
);
445 if (ksym
== NoSymbol
) {
446 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
447 file
, shortcutDefinition
, entry
->text
);
452 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
453 if (ptr
->keycode
== 0) {
454 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
455 shortcutDefinition
, entry
->text
);
463 ptr
->next
= shortcutList
;
466 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
471 static char *next_token(char *line
, char **next
)
477 while (*line
== ' ' || *line
== '\t')
485 while (*tmp
!= 0 && *tmp
!= '"')
488 wwarning(_("%s: unmatched '\"' in menu file"), line
);
499 } while (*tmp
!= 0 && *tmp
!= ' ' && *tmp
!= '\t');
513 while (*tmp
== ' ' || *tmp
== '\t')
522 static void separateCommand(char *line
, char ***file
, char **command
)
524 char *token
, *tmp
= line
;
525 WMArray
*array
= WMCreateArray(4);
531 token
= next_token(tmp
, &tmp
);
533 if (strcmp(token
, "WITH") == 0) {
534 if (tmp
!= NULL
&& *tmp
!= 0)
535 *command
= wstrdup(tmp
);
537 wwarning(_("%s: missing command"), line
);
540 WMAddToArray(array
, token
);
542 } while (token
!= NULL
&& tmp
!= NULL
);
544 count
= WMGetArrayItemCount(array
);
546 *file
= wmalloc(sizeof(char *) * (count
+ 1));
547 (*file
)[count
] = NULL
;
548 for (i
= 0; i
< count
; i
++) {
549 (*file
)[i
] = WMGetFromArray(array
, i
);
555 static WMenu
*constructPLMenu(WScreen
*screen
, char *path
)
557 WMPropList
*pl
= NULL
;
563 pl
= WMReadPropListFromFile(path
);
567 menu
= configureMenu(screen
, pl
, False
);
571 menu
->on_destroy
= removeShortcutsForMenu
;
575 static void constructMenu(WMenu
* menu
, WMenuEntry
* entry
)
578 struct stat stat_buf
;
585 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
586 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
587 wwarning(_("invalid OPEN_MENU specification: %s"), (char *)entry
->clientdata
);
593 if (path
[0][0] == '|') {
596 if (!menu
->cascades
[entry
->cascade
] || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
599 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
601 if (submenu
!= NULL
) {
602 if (path
[0][1] == '|')
603 submenu
->timestamp
= 0;
605 submenu
->timestamp
= 1; /* there's no automatic reloading */
613 /* try interpreting path as a proplist file */
614 submenu
= constructPLMenu(menu
->frame
->screen_ptr
, path
[0]);
615 /* if unsuccessful, try it as an old-style file */
619 while (path
[i
] != NULL
) {
622 if (strcmp(path
[i
], "-noext") == 0) {
627 tmp
= wexpandpath(path
[i
]);
629 lpath
= getLocalizedMenuFile(tmp
);
638 if (stat(path
[i
], &stat_buf
) == 0) {
639 if (last
< stat_buf
.st_mtime
)
640 last
= stat_buf
.st_mtime
;
644 werror(_("%s:could not stat menu"), path
[i
]);
652 werror(_("%s:could not stat menu:%s"), "OPEN_MENU", (char *)entry
->clientdata
);
655 stat(path
[first
], &stat_buf
);
656 if (!menu
->cascades
[entry
->cascade
]
657 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
659 if (S_ISDIR(stat_buf
.st_mode
)) {
661 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
, entry
->text
, path
, cmd
);
663 submenu
->timestamp
= last
;
664 } else if (S_ISREG(stat_buf
.st_mode
)) {
668 wwarning(_("too many parameters in OPEN_MENU: %s"),
669 (char *)entry
->clientdata
);
671 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
673 submenu
->timestamp
= stat_buf
.st_mtime
;
684 wMenuEntryRemoveCascade(menu
, entry
);
685 wMenuEntrySetCascade(menu
, entry
, submenu
);
690 while (path
[i
] != NULL
)
697 static void cleanupWorkspaceMenu(WMenu
* menu
)
699 if (menu
->frame
->screen_ptr
->workspace_menu
== menu
)
700 menu
->frame
->screen_ptr
->workspace_menu
= NULL
;
703 static WMenuEntry
*addWorkspaceMenu(WScreen
* scr
, WMenu
* menu
, char *title
)
708 if (scr
->flags
.added_workspace_menu
) {
710 ("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
713 scr
->flags
.added_workspace_menu
= 1;
715 wsmenu
= wWorkspaceMenuMake(scr
, True
);
716 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
718 scr
->workspace_menu
= wsmenu
;
719 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
720 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
722 wWorkspaceMenuUpdate(scr
, wsmenu
);
727 static void cleanupWindowsMenu(WMenu
* menu
)
729 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
730 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
733 static WMenuEntry
*addWindowsMenu(WScreen
* scr
, WMenu
* menu
, char *title
)
739 if (scr
->flags
.added_windows_menu
) {
741 ("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
744 scr
->flags
.added_windows_menu
= 1;
746 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
747 wwmenu
->on_destroy
= cleanupWindowsMenu
;
748 scr
->switch_menu
= wwmenu
;
749 wwin
= scr
->focused_window
;
751 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
755 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
756 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
761 static WMenuEntry
*addMenuEntry(WMenu
* menu
, char *title
, char *shortcut
, char *command
,
762 char *params
, const char *file_name
)
765 WMenuEntry
*entry
= NULL
;
766 Bool shortcutOk
= False
;
770 scr
= menu
->frame
->screen_ptr
;
771 if (strcmp(command
, "OPEN_MENU") == 0) {
773 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
778 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
780 path
= wstrdup(params
);
782 dummy
= wMenuCreate(scr
, title
, False
);
783 dummy
->on_destroy
= removeShortcutsForMenu
;
784 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
785 entry
->free_cdata
= wfree
;
786 wMenuEntrySetCascade(menu
, entry
, dummy
);
788 } else if (strcmp(command
, "EXEC") == 0) {
790 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
792 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrconcat("exec ", params
));
793 entry
->free_cdata
= wfree
;
796 } else if (strcmp(command
, "SHEXEC") == 0) {
798 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
800 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrdup(params
));
801 entry
->free_cdata
= wfree
;
804 } else if (strcmp(command
, "EXIT") == 0) {
806 if (params
&& strcmp(params
, "QUICK") == 0)
807 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void *)M_QUICK
);
809 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
812 } else if (strcmp(command
, "SHUTDOWN") == 0) {
814 if (params
&& strcmp(params
, "QUICK") == 0)
815 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, (void *)M_QUICK
);
817 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
820 } else if (strcmp(command
, "REFRESH") == 0) {
821 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
824 } else if (strcmp(command
, "WORKSPACE_MENU") == 0) {
825 entry
= addWorkspaceMenu(scr
, menu
, title
);
828 } else if (strcmp(command
, "WINDOWS_MENU") == 0) {
829 entry
= addWindowsMenu(scr
, menu
, title
);
832 } else if (strcmp(command
, "ARRANGE_ICONS") == 0) {
833 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
836 } else if (strcmp(command
, "HIDE_OTHERS") == 0) {
837 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
840 } else if (strcmp(command
, "SHOW_ALL") == 0) {
841 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
844 } else if (strcmp(command
, "RESTART") == 0) {
845 entry
= wMenuAddCallback(menu
, title
, restartCommand
, params
? wstrdup(params
) : NULL
);
846 entry
->free_cdata
= wfree
;
848 } else if (strcmp(command
, "SAVE_SESSION") == 0) {
849 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
852 } else if (strcmp(command
, "CLEAR_SESSION") == 0) {
853 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
855 } else if (strcmp(command
, "INFO_PANEL") == 0) {
856 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
858 } else if (strcmp(command
, "LEGAL_PANEL") == 0) {
859 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
862 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
, command
);
867 if (shortcut
&& entry
) {
869 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
, title
);
871 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
873 entry
->rtext
= GetShortcutString(shortcut
);
875 entry->rtext = wstrdup(shortcut);
884 /******************* Menu Configuration From File *******************/
886 static void freeline(char *title
, char *command
, char *parameter
, char *shortcut
)
894 static WMenu
*parseCascade(WScreen
* scr
, WMenu
* menu
, WMenuParser parser
)
896 char *command
, *params
, *shortcut
, *title
;
898 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
900 if (command
== NULL
|| !command
[0]) {
901 WMenuParserError(parser
, _("missing command in menu config") );
902 freeline(title
, command
, params
, shortcut
);
906 if (strcasecmp(command
, "MENU") == 0) {
911 cascade
= wMenuCreate(scr
, M_(title
), False
);
912 cascade
->on_destroy
= removeShortcutsForMenu
;
913 if (!parseCascade(scr
, cascade
, parser
)) {
914 wMenuDestroy(cascade
, True
);
916 wMenuEntrySetCascade(menu
, wMenuAddCallback(menu
, M_(title
), NULL
, NULL
), cascade
);
918 } else if (strcasecmp(command
, "END") == 0) {
920 freeline(title
, command
, params
, shortcut
);
924 addMenuEntry(menu
, M_(title
), shortcut
, command
, params
, WMenuParserGetFilename(parser
));
926 freeline(title
, command
, params
, shortcut
);
929 WMenuParserError(parser
, _("syntax error in menu file: END declaration missing") );
935 static WMenu
*readMenuFile(WScreen
* scr
, char *file_name
)
940 char *command
, *params
, *shortcut
, *title
;
948 if (!wPreferences
.flags
.nocpp
) {
949 args
= MakeCPPArgs(file_name
);
951 wwarning(_("could not make arguments for menu file preprocessor"));
953 snprintf(cmd
, sizeof(cmd
), "%s %s %s", CPP_PATH
, args
, file_name
);
955 file
= popen(cmd
, "r");
957 werror(_("%s:could not open/preprocess menu file"), file_name
);
966 file
= fopen(file_name
, "rb");
968 werror(_("%s:could not open menu file"), file_name
);
972 parser
= WMenuParserCreate(file_name
, file
);
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
);
981 if (strcasecmp(command
, "MENU") == 0) {
982 menu
= wMenuCreate(scr
, M_(title
), True
);
983 menu
->on_destroy
= removeShortcutsForMenu
;
984 if (!parseCascade(scr
, menu
, parser
)) {
985 wMenuDestroy(menu
, True
);
988 freeline(title
, command
, params
, shortcut
);
991 WMenuParserError(parser
, _("invalid menu file, MENU command is missing") );
992 freeline(title
, command
, params
, shortcut
);
995 freeline(title
, command
, params
, shortcut
);
998 WMenuParserDelete(parser
);
1001 if (pclose(file
) == -1) {
1002 werror(_("error reading preprocessed menu data"));
1014 /************ Menu Configuration From Pipe *************/
1016 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
)
1021 char *command
, *params
, *shortcut
, *title
;
1023 char flat_file
[MAXLINE
];
1030 flat_file
[0] = '\0';
1032 for (i
= 0; file_name
[i
] != NULL
; i
++) {
1033 strcat(flat_file
, file_name
[i
]);
1034 strcat(flat_file
, " ");
1036 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1039 if (!wPreferences
.flags
.nocpp
) {
1040 args
= MakeCPPArgs(filename
);
1042 wwarning(_("could not make arguments for menu file preprocessor"));
1044 snprintf(cmd
, sizeof(cmd
), "%s | %s %s", filename
, CPP_PATH
, args
);
1047 file
= popen(cmd
, "r");
1049 werror(_("%s:could not open/preprocess menu file"), filename
);
1056 file
= popen(filename
, "rb");
1059 werror(_("%s:could not open menu file"), filename
);
1063 parser
= WMenuParserCreate(flat_file
, file
);
1065 while (WMenuParserGetLine(parser
, &title
, &command
, ¶ms
, &shortcut
)) {
1067 if (command
== NULL
|| !command
[0]) {
1068 WMenuParserError(parser
, _("missing command in menu config") );
1069 freeline(title
, command
, params
, shortcut
);
1072 if (strcasecmp(command
, "MENU") == 0) {
1073 menu
= wMenuCreate(scr
, M_(title
), True
);
1074 menu
->on_destroy
= removeShortcutsForMenu
;
1075 if (!parseCascade(scr
, menu
, parser
)) {
1076 wMenuDestroy(menu
, True
);
1079 freeline(title
, command
, params
, shortcut
);
1082 WMenuParserError(parser
, _("no title given for the root menu") );
1083 freeline(title
, command
, params
, shortcut
);
1087 freeline(title
, command
, params
, shortcut
);
1090 WMenuParserDelete(parser
);
1101 static int myCompare(const void *d1
, const void *d2
)
1103 dir_data
*p1
= *(dir_data
**) d1
;
1104 dir_data
*p2
= *(dir_data
**) d2
;
1106 return strcmp(p1
->name
, p2
->name
);
1109 /************ Menu Configuration From Directory *************/
1111 static Bool
isFilePackage(char *file
)
1115 /* check if the extension indicates this file is a
1116 * file package. For now, only recognize .themed */
1120 if (l
> 7 && strcmp(&(file
[l
- 7]), ".themed") == 0) {
1127 static WMenu
*readMenuDirectory(WScreen
* scr
, char *title
, char **path
, char *command
)
1130 struct dirent
*dentry
;
1131 struct stat stat_buf
;
1134 WMArray
*dirs
= NULL
, *files
= NULL
;
1135 WMArrayIterator iter
;
1136 int length
, i
, have_space
= 0;
1138 int stripExtension
= 0;
1140 dirs
= WMCreateArray(16);
1141 files
= WMCreateArray(16);
1144 while (path
[i
] != NULL
) {
1145 if (strcmp(path
[i
], "-noext") == 0) {
1151 dir
= opendir(path
[i
]);
1157 while ((dentry
= readdir(dir
))) {
1159 if (strcmp(dentry
->d_name
, ".") == 0 || strcmp(dentry
->d_name
, "..") == 0)
1162 if (dentry
->d_name
[0] == '.')
1165 buffer
= malloc(strlen(path
[i
]) + strlen(dentry
->d_name
) + 4);
1167 werror(_("out of memory while constructing directory menu %s"), path
[i
]);
1171 strcpy(buffer
, path
[i
]);
1172 strcat(buffer
, "/");
1173 strcat(buffer
, dentry
->d_name
);
1175 if (stat(buffer
, &stat_buf
) != 0) {
1176 werror(_("%s:could not stat file \"%s\" in menu directory"),
1177 path
[i
], dentry
->d_name
);
1179 Bool isFilePack
= False
;
1182 if (S_ISDIR(stat_buf
.st_mode
)
1183 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1185 /* access always returns success for user root */
1186 if (access(buffer
, X_OK
) == 0) {
1187 /* Directory is accesible. Add to directory list */
1189 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1190 data
->name
= wstrdup(dentry
->d_name
);
1193 WMAddToArray(dirs
, data
);
1195 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1196 /* Hack because access always returns X_OK success for user root */
1197 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1198 if ((command
!= NULL
&& access(buffer
, R_OK
) == 0) ||
1199 (command
== NULL
&& access(buffer
, X_OK
) == 0 &&
1200 (stat_buf
.st_mode
& S_IXANY
))) {
1202 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1203 data
->name
= wstrdup(dentry
->d_name
);
1206 WMAddToArray(files
, data
);
1217 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1223 WMSortArray(dirs
, myCompare
);
1224 WMSortArray(files
, myCompare
);
1226 menu
= wMenuCreate(scr
, M_(title
), False
);
1227 menu
->on_destroy
= removeShortcutsForMenu
;
1229 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1230 /* New directory. Use same OPEN_MENU command that was used
1231 * for the current directory. */
1232 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1236 length
+= strlen(command
) + 6;
1237 buffer
= malloc(length
);
1239 werror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1245 strcat(buffer
, "-noext ");
1247 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1250 strcat(buffer
, "\"");
1251 strcat(buffer
, path
[data
->index
]);
1253 strcat(buffer
, "/");
1254 strcat(buffer
, data
->name
);
1256 strcat(buffer
, "\"");
1258 strcat(buffer
, " WITH ");
1259 strcat(buffer
, command
);
1262 addMenuEntry(menu
, M_(data
->name
), NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1270 WM_ITERATE_ARRAY(files
, data
, iter
) {
1271 /* executable: add as entry */
1272 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1274 length
+= strlen(command
);
1276 buffer
= malloc(length
);
1278 werror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1282 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1283 if (command
!= NULL
) {
1284 strcpy(buffer
, command
);
1285 strcat(buffer
, " ");
1287 strcat(buffer
, "\"");
1288 strcat(buffer
, path
[data
->index
]);
1293 strcat(buffer
, path
[data
->index
]);
1295 strcpy(buffer
, path
[data
->index
]);
1298 strcat(buffer
, "/");
1299 strcat(buffer
, data
->name
);
1301 strcat(buffer
, "\"");
1303 if (stripExtension
) {
1304 char *ptr
= strrchr(data
->name
, '.');
1305 if (ptr
&& ptr
!= data
->name
)
1308 addMenuEntry(menu
, M_(data
->name
), NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1322 /************ Menu Configuration From WMRootMenu *************/
1324 static WMenu
*makeDefaultMenu(WScreen
* scr
)
1328 menu
= wMenuCreate(scr
, _("Commands"), True
);
1329 wMenuAddCallback(menu
, M_("XTerm"), execCommand
, "xterm");
1330 wMenuAddCallback(menu
, M_("rxvt"), execCommand
, "rxvt");
1331 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1332 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1337 *----------------------------------------------------------------------
1339 * Reads root menu configuration from defaults database.
1341 *----------------------------------------------------------------------
1343 static WMenu
*configureMenu(WScreen
* scr
, WMPropList
* definition
, Bool includeGlobals
)
1348 WMPropList
*title
, *command
, *params
;
1351 if (WMIsPLString(definition
)) {
1352 struct stat stat_buf
;
1354 Bool menu_is_default
= False
;
1356 /* menu definition is a string. Probably a path, so parse the file */
1358 tmp
= wexpandpath(WMGetFromPLString(definition
));
1360 path
= getLocalizedMenuFile(tmp
);
1363 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1366 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1367 menu_is_default
= True
;
1371 werror(_("could not find menu file \"%s\" referenced in WMRootMenu"), tmp
);
1376 if (stat(path
, &stat_buf
) < 0) {
1377 werror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1383 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1384 /* if the pointer in WMRootMenu has changed */
1385 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1387 if (menu_is_default
) {
1389 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1393 menu
= readMenuFile(scr
, path
);
1395 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1405 count
= WMGetPropListItemCount(definition
);
1409 elem
= WMGetFromPLArray(definition
, 0);
1410 if (!WMIsPLString(elem
)) {
1411 tmp
= WMGetPropListDescription(elem
, False
);
1412 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1416 mtitle
= WMGetFromPLString(elem
);
1418 menu
= wMenuCreate(scr
, M_(mtitle
), False
);
1419 menu
->on_destroy
= removeShortcutsForMenu
;
1421 #ifdef GLOBAL_SUBMENU_FILE
1422 if (includeGlobals
) {
1426 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1429 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1430 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1435 for (i
= 1; i
< count
; i
++) {
1436 elem
= WMGetFromPLArray(definition
, i
);
1438 if (WMIsPLString(elem
)) {
1441 file
= WMGetFromPLString(elem
);
1445 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1448 if (WMIsPLArray(WMGetFromPLArray(elem
, 1))) {
1453 submenu
= configureMenu(scr
, elem
, True
);
1455 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1456 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1460 WMPropList
*shortcut
;
1463 title
= WMGetFromPLArray(elem
, idx
++);
1464 shortcut
= WMGetFromPLArray(elem
, idx
++);
1465 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT") == 0) {
1466 shortcut
= WMGetFromPLArray(elem
, idx
++);
1467 command
= WMGetFromPLArray(elem
, idx
++);
1472 params
= WMGetFromPLArray(elem
, idx
++);
1474 if (!title
|| !command
)
1477 addMenuEntry(menu
, M_(WMGetFromPLString(title
)),
1478 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1479 WMGetFromPLString(command
),
1480 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1485 tmp
= WMGetPropListDescription(elem
, False
);
1486 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1494 *----------------------------------------------------------------------
1496 * Opens the root menu, parsing the menu configuration from the
1497 * defaults database.
1498 * If the menu is already mapped and is not sticked to the
1499 * root window, it will be unmapped.
1502 * The menu may be remade.
1505 * Construction of OPEN_MENU entries are delayed to the moment the
1507 *----------------------------------------------------------------------
1509 void OpenRootMenu(WScreen
* scr
, int x
, int y
, int keyboard
)
1512 WMPropList
*definition
;
1514 static WMPropList *domain=NULL;
1517 domain = WMCreatePLString("WMRootMenu");
1521 scr
->flags
.root_menu_changed_shortcuts
= 0;
1522 scr
->flags
.added_workspace_menu
= 0;
1523 scr
->flags
.added_windows_menu
= 0;
1525 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1526 menu
= scr
->root_menu
;
1527 if (!menu
->flags
.buttoned
) {
1530 wRaiseFrame(menu
->frame
->core
);
1533 wMenuMapAt(menu
, 0, 0, True
);
1535 wMenuMapCopyAt(menu
, x
- menu
->frame
->core
->width
/ 2, y
);
1540 definition
= WDRootMenu
->dictionary
;
1543 definition = PLGetDomain(domain);
1546 if (WMIsPLArray(definition
)) {
1547 if (!scr
->root_menu
|| WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1548 menu
= configureMenu(scr
, definition
, True
);
1550 menu
->timestamp
= WDRootMenu
->timestamp
;
1555 menu
= configureMenu(scr
, definition
, True
);
1560 /* menu hasn't changed or could not be read */
1561 if (!scr
->root_menu
) {
1562 wMessageDialog(scr
, _("Error"),
1563 _("The applications menu could not be loaded. "
1564 "Look at the console output for a detailed "
1565 "description of the errors."), _("OK"), NULL
, NULL
);
1567 menu
= makeDefaultMenu(scr
);
1568 scr
->root_menu
= menu
;
1570 menu
= scr
->root_menu
;
1573 if (scr
->root_menu
) {
1574 wMenuDestroy(scr
->root_menu
, True
);
1576 scr
->root_menu
= menu
;
1581 if (keyboard
&& x
== 0 && y
== 0) {
1583 } else if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
1584 newx
= x
- menu
->frame
->core
->width
/ 2;
1585 newy
= y
- menu
->frame
->core
->height
/ 2;
1587 newx
= x
- menu
->frame
->core
->width
/ 2;
1590 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1593 if (scr
->flags
.root_menu_changed_shortcuts
)
1594 rebindKeygrabs(scr
);