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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
31 #include <sys/types.h>
38 #include <X11/Xutil.h>
39 #include <X11/Xatom.h>
41 #include "WindowMaker.h"
48 #include "workspace.h"
52 #include "xmodifier.h"
54 #include <WINGs/WUtil.h>
56 #define MAX_SHORTCUT_LENGTH 32
60 extern WDDomain
*WDRootMenu
;
62 extern Cursor wCursor
[WCUR_LAST
];
64 extern Time LastTimestamp
;
66 extern WPreferences wPreferences
;
68 extern int wScreenCount
;
70 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
);
71 static WMenu
*readMenuFile(WScreen
* scr
, char *file_name
);
72 static WMenu
*readMenuDirectory(WScreen
* scr
, char *title
, char **file_name
, char *command
);
74 typedef struct Shortcut
{
75 struct Shortcut
*next
;
83 static Shortcut
*shortcutList
= NULL
;
89 * "Title" EXEC command_to_exec -params
91 * "Title" EXEC command_to_exec -params
93 * "Workspaces" WORKSPACE_MENU
94 * "Title" built_in_command
96 * "Quick Quit" EXIT QUICK
99 * Commands may be preceded by SHORTCUT key
103 * INFO_PANEL - shows the Info Panel
104 * LEGAL_PANEL - shows the Legal info panel
105 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
106 * REFRESH - forces the desktop to be repainted
107 * EXIT [QUICK] - exit the window manager [without confirmation]
108 * EXEC <program> - execute an external program
109 * SHEXEC <command> - execute a shell command
110 * WORKSPACE_MENU - places the workspace submenu
112 * RESTART [<window manager>] - restarts the window manager
113 * SHOW_ALL - unhide all windows on workspace
114 * HIDE_OTHERS - hides all windows excep the focused one
115 * OPEN_MENU file - read menu data from file which must be a valid menu file.
116 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
117 * - read menu data from directory(ies) and
118 * eventually precede each with a command.
119 * OPEN_MENU | command
120 * - opens command and uses its stdout to construct and insert
121 * the resulting menu in current position. The output of
122 * command must be a valid menu description.
123 * The space between '|' and command is optional.
124 * || will do the same, but will not cache the contents.
125 * SAVE_SESSION - saves the current state of the desktop, which include
126 * all running applications, all their hints (geometry,
127 * position on screen, workspace they live on, the dock
128 * or clip from where they were launched, and
129 * if minimized, shaded or hidden. Also saves the current
130 * workspace the user is on. All will be restored on every
131 * start of windowmaker until another SAVE_SESSION or
132 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
133 * WindowMaker domain file, then saving is automatically
134 * done on every windowmaker exit, overwriting any
135 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
137 * CLEAR_SESSION - clears any previous saved session. This will not have
138 * any effect if SaveSessionOnExit is True.
146 static void execCommand(WMenu
* menu
, WMenuEntry
* entry
)
150 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char *)entry
->clientdata
);
152 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
153 GrabModeAsync
, GrabModeAsync
, None
, wCursor
[WCUR_WAIT
], CurrentTime
);
157 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
160 XUngrabPointer(dpy
, CurrentTime
);
164 static void exitCommand(WMenu
* menu
, WMenuEntry
* entry
)
166 static int inside
= 0;
169 /* prevent reentrant calls */
179 if ((long)entry
->clientdata
== M_QUICK
) {
182 int r
, oldSaveSessionFlag
;
184 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
185 r
= wExitDialog(menu
->frame
->screen_ptr
, _("Exit"),
186 _("Exit window manager?"), _("Exit"), _("Cancel"), NULL
);
188 if (r
== WAPRDefault
) {
190 } else if (r
== WAPRAlternate
) {
191 /* Don't modify the "save session on exit" flag if the
192 * user canceled the operation. */
193 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
196 if (result
== R_EXIT
)
197 Shutdown(WSExitMode
);
204 static void shutdownCommand(WMenu
* menu
, WMenuEntry
* entry
)
206 static int inside
= 0;
209 /* prevent reentrant calls */
219 if ((long)entry
->clientdata
== M_QUICK
)
223 int r
, oldSaveSessionFlag
;
225 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
227 r
= wExitDialog(menu
->frame
->screen_ptr
,
229 _("Kill Window System session?\n"
230 "(all applications will be closed)"), _("Kill"), _("Cancel"), NULL
);
231 if (r
== WAPRDefault
) {
233 } else if (r
== WAPRAlternate
) {
234 /* Don't modify the "save session on exit" flag if the
235 * user canceled the operation. */
236 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
241 if (result
!= R_CANCEL
) {
243 Shutdown(WSKillMode
);
252 static void restartCommand(WMenu
* menu
, WMenuEntry
* entry
)
254 Shutdown(WSRestartPreparationMode
);
255 Restart((char *)entry
->clientdata
, False
);
259 static void refreshCommand(WMenu
* menu
, WMenuEntry
* entry
)
261 wRefreshDesktop(menu
->frame
->screen_ptr
);
264 static void arrangeIconsCommand(WMenu
* menu
, WMenuEntry
* entry
)
266 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
269 static void showAllCommand(WMenu
* menu
, WMenuEntry
* entry
)
271 wShowAllWindows(menu
->frame
->screen_ptr
);
274 static void hideOthersCommand(WMenu
* menu
, WMenuEntry
* entry
)
276 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
279 static void saveSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
281 if (!wPreferences
.save_session_on_exit
)
282 wSessionSaveState(menu
->frame
->screen_ptr
);
284 wScreenSaveState(menu
->frame
->screen_ptr
);
287 static void clearSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
289 wSessionClearState(menu
->frame
->screen_ptr
);
290 wScreenSaveState(menu
->frame
->screen_ptr
);
293 static void infoPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
295 wShowInfoPanel(menu
->frame
->screen_ptr
);
298 static void legalPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
300 wShowLegalPanel(menu
->frame
->screen_ptr
);
303 /********************************************************************/
305 static char *getLocalizedMenuFile(char *menu
)
307 char *buffer
, *ptr
, *locale
;
313 len
= strlen(menu
) + strlen(Locale
) + 8;
314 buffer
= wmalloc(len
);
316 /* try menu.locale_name */
317 snprintf(buffer
, len
, "%s.%s", menu
, Locale
);
318 if (access(buffer
, F_OK
) == 0) {
322 /* position of locale in our buffer */
323 locale
= buffer
+ strlen(menu
) + 1;
325 /* check if it is in the form aa_bb.encoding and check for aa_bb */
326 ptr
= strchr(locale
, '.');
329 if (access(buffer
, F_OK
) == 0) {
333 /* now check for aa */
334 ptr
= strchr(locale
, '_');
337 if (access(buffer
, F_OK
) == 0) {
347 Bool
wRootMenuPerformShortcut(XEvent
* event
)
349 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
354 /* ignore CapsLock */
355 modifiers
= event
->xkey
.state
& ValidModMask
;
357 for (ptr
= shortcutList
; ptr
!= NULL
; ptr
= ptr
->next
) {
358 if (ptr
->keycode
== 0 || ptr
->menu
->menu
->screen_ptr
!= scr
)
361 if (ptr
->keycode
== event
->xkey
.keycode
&& ptr
->modifier
== modifiers
) {
362 (*ptr
->entry
->callback
) (ptr
->menu
, ptr
->entry
);
370 void wRootMenuBindShortcuts(Window window
)
376 if (ptr
->modifier
!= AnyModifier
) {
377 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
| LockMask
,
378 window
, True
, GrabModeAsync
, GrabModeAsync
);
380 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
383 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
388 static void rebindKeygrabs(WScreen
* scr
)
392 wwin
= scr
->focused_window
;
394 while (wwin
!= NULL
) {
395 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
397 if (!WFLAGP(wwin
, no_bind_keys
)) {
398 wWindowSetKeyGrabs(wwin
);
404 static void removeShortcutsForMenu(WMenu
* menu
)
407 Shortcut
*newList
= NULL
;
410 while (ptr
!= NULL
) {
412 if (ptr
->menu
== menu
) {
420 shortcutList
= newList
;
421 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
424 static Bool
addShortcut(char *file
, char *shortcutDefinition
, WMenu
* menu
, WMenuEntry
* entry
)
429 char buf
[MAX_SHORTCUT_LENGTH
], *b
;
431 ptr
= wmalloc(sizeof(Shortcut
));
433 strncpy(buf
, shortcutDefinition
, MAX_SHORTCUT_LENGTH
);
438 while ((k
= strchr(b
, '+')) != NULL
) {
442 mod
= wXModifierFromKey(b
);
444 wwarning(_("%s: invalid key modifier \"%s\""), file
, b
);
448 ptr
->modifier
|= mod
;
454 ksym
= XStringToKeysym(b
);
456 if (ksym
== NoSymbol
) {
457 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
458 file
, shortcutDefinition
, entry
->text
);
463 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
464 if (ptr
->keycode
== 0) {
465 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
466 shortcutDefinition
, entry
->text
);
474 ptr
->next
= shortcutList
;
477 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
482 /*******************************/
484 static char *cropline(char *line
)
488 if (strlen(line
) == 0)
491 end
= &(line
[strlen(line
)]) - 1;
492 while (isspace(*line
) && *line
!= 0)
494 while (end
> line
&& isspace(*end
)) {
501 static char *next_token(char *line
, char **next
)
507 while (*line
== ' ' || *line
== '\t')
515 while (*tmp
!= 0 && *tmp
!= '"')
518 wwarning(_("%s: unmatched '\"' in menu file"), line
);
529 } while (*tmp
!= 0 && *tmp
!= ' ' && *tmp
!= '\t');
543 while (*tmp
== ' ' || *tmp
== '\t')
552 static void separateCommand(char *line
, char ***file
, char **command
)
554 char *token
, *tmp
= line
;
555 WMArray
*array
= WMCreateArray(4);
561 token
= next_token(tmp
, &tmp
);
563 if (strcmp(token
, "WITH") == 0) {
564 if (tmp
!= NULL
&& *tmp
!= 0)
565 *command
= wstrdup(tmp
);
567 wwarning(_("%s: missing command"), line
);
570 WMAddToArray(array
, token
);
572 } while (token
!= NULL
&& tmp
!= NULL
);
574 count
= WMGetArrayItemCount(array
);
576 *file
= wmalloc(sizeof(char *) * (count
+ 1));
577 (*file
)[count
] = NULL
;
578 for (i
= 0; i
< count
; i
++) {
579 (*file
)[i
] = WMGetFromArray(array
, i
);
585 static void constructMenu(WMenu
* menu
, WMenuEntry
* entry
)
588 struct stat stat_buf
;
595 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
596 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
597 wwarning(_("invalid OPEN_MENU specification: %s"), (char *)entry
->clientdata
);
601 if (path
[0][0] == '|') {
604 if (!menu
->cascades
[entry
->cascade
] || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
607 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
609 if (submenu
!= NULL
) {
610 if (path
[0][1] == '|')
611 submenu
->timestamp
= 0;
613 submenu
->timestamp
= 1; /* there's no automatic reloading */
621 while (path
[i
] != NULL
) {
624 if (strcmp(path
[i
], "-noext") == 0) {
629 tmp
= wexpandpath(path
[i
]);
631 lpath
= getLocalizedMenuFile(tmp
);
640 if (stat(path
[i
], &stat_buf
) == 0) {
641 if (last
< stat_buf
.st_mtime
)
642 last
= stat_buf
.st_mtime
;
646 wsyserror(_("%s:could not stat menu"), path
[i
]);
654 wsyserror(_("%s:could not stat menu:%s"), "OPEN_MENU", (char *)entry
->clientdata
);
657 stat(path
[first
], &stat_buf
);
658 if (!menu
->cascades
[entry
->cascade
]
659 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
661 if (S_ISDIR(stat_buf
.st_mode
)) {
663 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
, entry
->text
, path
, cmd
);
665 submenu
->timestamp
= last
;
666 } else if (S_ISREG(stat_buf
.st_mode
)) {
670 wwarning(_("too many parameters in OPEN_MENU: %s"),
671 (char *)entry
->clientdata
);
673 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
675 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
, 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
= free
;
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
= free
;
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
= free
;
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
= free
;
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 separateline(char *line
, char *title
, char *command
, char *parameter
, char *shortcut
)
898 while (isspace(*line
) && (*line
!= 0))
903 while (line
[i
] != '"' && (line
[i
] != 0))
909 while (!isspace(line
[i
]) && (line
[i
] != 0))
912 strncpy(title
, line
, i
);
916 /* get the command or shortcut keyword */
917 while (isspace(*line
) && (*line
!= 0))
922 while (!isspace(line
[i
]) && (line
[i
] != 0))
924 strncpy(command
, line
, i
);
928 if (strcmp(command
, "SHORTCUT") == 0) {
929 /* get the shortcut key */
930 while (isspace(*line
) && (*line
!= 0))
935 while (line
[i
] != '"' && (line
[i
] != 0))
941 while (!isspace(line
[i
]) && (line
[i
] != 0))
944 strncpy(shortcut
, line
, i
);
950 /* get the command */
951 while (isspace(*line
) && (*line
!= 0))
956 while (!isspace(line
[i
]) && (line
[i
] != 0))
958 strncpy(command
, line
, i
);
963 /* get the parameters */
964 while (isspace(*line
) && (*line
!= 0))
972 while (line
[l
] != 0 && line
[l
] != '"') {
973 parameter
[l
] = line
[l
];
981 while (isspace(line
[l
]) && (l
> 0))
983 strncpy(parameter
, line
, l
);
987 static WMenu
*parseCascade(WScreen
* scr
, WMenu
* menu
, FILE * file
, char *file_name
)
989 char linebuf
[MAXLINE
];
990 char elinebuf
[MAXLINE
];
992 char command
[MAXLINE
];
993 char shortcut
[MAXLINE
];
994 char params
[MAXLINE
];
997 while (!feof(file
)) {
1001 fgets(linebuf
, MAXLINE
, file
);
1002 line
= cropline(linebuf
);
1003 lsize
= strlen(line
);
1005 if (line
[lsize
- 1] == '\\') {
1008 fgets(elinebuf
, MAXLINE
, file
);
1009 line2
= cropline(elinebuf
);
1010 lsize2
= strlen(line2
);
1011 if (lsize2
+ lsize
> MAXLINE
) {
1012 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1016 line
[lsize
- 1] = 0;
1017 lsize
+= lsize2
- 1;
1018 strcat(line
, line2
);
1023 } while (!ok
&& !feof(file
));
1027 if (line
[0] == 0 || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
1030 separateline(line
, title
, command
, params
, shortcut
);
1033 wwarning(_("%s:missing command in menu config: %s"), file_name
, line
);
1037 if (strcasecmp(command
, "MENU") == 0) {
1042 cascade
= wMenuCreate(scr
, title
, False
);
1043 cascade
->on_destroy
= removeShortcutsForMenu
;
1044 if (parseCascade(scr
, cascade
, file
, file_name
) == NULL
) {
1045 wMenuDestroy(cascade
, True
);
1047 wMenuEntrySetCascade(menu
, wMenuAddCallback(menu
, title
, NULL
, NULL
), cascade
);
1049 } else if (strcasecmp(command
, "END") == 0) {
1055 addMenuEntry(menu
, title
, shortcut
[0] ? shortcut
: NULL
, command
,
1056 params
[0] ? params
: NULL
, file_name
);
1060 wwarning(_("%s:syntax error in menu file:END declaration missing"), file_name
);
1067 static WMenu
*readMenuFile(WScreen
* scr
, char *file_name
)
1071 char linebuf
[MAXLINE
];
1072 char title
[MAXLINE
];
1073 char shortcut
[MAXLINE
];
1074 char command
[MAXLINE
];
1075 char params
[MAXLINE
];
1083 if (!wPreferences
.flags
.nocpp
) {
1084 args
= MakeCPPArgs(file_name
);
1086 wwarning(_("could not make arguments for menu file preprocessor"));
1088 snprintf(command
, sizeof(command
), "%s %s %s", CPP_PATH
, args
, file_name
);
1090 file
= popen(command
, "r");
1092 wsyserror(_("%s:could not open/preprocess menu file"), file_name
);
1101 file
= fopen(file_name
, "rb");
1103 wsyserror(_("%s:could not open menu file"), file_name
);
1108 while (!feof(file
)) {
1109 if (!fgets(linebuf
, MAXLINE
, file
))
1111 line
= cropline(linebuf
);
1112 if (line
[0] == 0 || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
1115 separateline(line
, title
, command
, params
, shortcut
);
1118 wwarning(_("%s:missing command in menu config: %s"), file_name
, line
);
1121 if (strcasecmp(command
, "MENU") == 0) {
1122 menu
= wMenuCreate(scr
, title
, True
);
1123 menu
->on_destroy
= removeShortcutsForMenu
;
1124 if (!parseCascade(scr
, menu
, file
, file_name
)) {
1125 wMenuDestroy(menu
, True
);
1129 wwarning(_("%s:invalid menu file. MENU command is missing"), file_name
);
1136 if (pclose(file
) == -1) {
1137 wsyserror(_("error reading preprocessed menu data"));
1149 /************ Menu Configuration From Pipe *************/
1151 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
)
1155 char linebuf
[MAXLINE
];
1156 char title
[MAXLINE
];
1157 char command
[MAXLINE
];
1158 char params
[MAXLINE
];
1159 char shortcut
[MAXLINE
];
1162 char flat_file
[MAXLINE
];
1169 flat_file
[0] = '\0';
1171 for (i
= 0; file_name
[i
] != NULL
; i
++) {
1172 strcat(flat_file
, file_name
[i
]);
1173 strcat(flat_file
, " ");
1175 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1178 if (!wPreferences
.flags
.nocpp
) {
1179 args
= MakeCPPArgs(filename
);
1181 wwarning(_("could not make arguments for menu file preprocessor"));
1183 snprintf(command
, sizeof(command
), "%s | %s %s", filename
, CPP_PATH
, args
);
1186 file
= popen(command
, "r");
1188 wsyserror(_("%s:could not open/preprocess menu file"), filename
);
1197 file
= popen(filename
, "rb");
1200 wsyserror(_("%s:could not open menu file"), filename
);
1205 while (!feof(file
)) {
1206 if (!fgets(linebuf
, MAXLINE
, file
))
1208 line
= cropline(linebuf
);
1209 if (line
[0] == 0 || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
1212 separateline(line
, title
, command
, params
, shortcut
);
1215 wwarning(_("%s:missing command in menu config: %s"), filename
, line
);
1218 if (strcasecmp(command
, "MENU") == 0) {
1219 menu
= wMenuCreate(scr
, title
, True
);
1220 menu
->on_destroy
= removeShortcutsForMenu
;
1221 if (!parseCascade(scr
, menu
, file
, filename
)) {
1222 wMenuDestroy(menu
, True
);
1226 wwarning(_("%s:no title given for the root menu"), filename
);
1241 static int myCompare(const void *d1
, const void *d2
)
1243 dir_data
*p1
= *(dir_data
**) d1
;
1244 dir_data
*p2
= *(dir_data
**) d2
;
1246 return strcmp(p1
->name
, p2
->name
);
1249 /************ Menu Configuration From Directory *************/
1251 static Bool
isFilePackage(char *file
)
1255 /* check if the extension indicates this file is a
1256 * file package. For now, only recognize .themed */
1260 if (l
> 7 && strcmp(&(file
[l
- 7]), ".themed") == 0) {
1267 static WMenu
*readMenuDirectory(WScreen
* scr
, char *title
, char **path
, char *command
)
1270 struct dirent
*dentry
;
1271 struct stat stat_buf
;
1274 WMArray
*dirs
= NULL
, *files
= NULL
;
1275 WMArrayIterator iter
;
1276 int length
, i
, have_space
= 0;
1278 int stripExtension
= 0;
1280 dirs
= WMCreateArray(16);
1281 files
= WMCreateArray(16);
1284 while (path
[i
] != NULL
) {
1285 if (strcmp(path
[i
], "-noext") == 0) {
1291 dir
= opendir(path
[i
]);
1297 while ((dentry
= readdir(dir
))) {
1299 if (strcmp(dentry
->d_name
, ".") == 0 || strcmp(dentry
->d_name
, "..") == 0)
1302 if (dentry
->d_name
[0] == '.')
1305 buffer
= malloc(strlen(path
[i
]) + strlen(dentry
->d_name
) + 4);
1307 wsyserror(_("out of memory while constructing directory menu %s"), path
[i
]);
1311 strcpy(buffer
, path
[i
]);
1312 strcat(buffer
, "/");
1313 strcat(buffer
, dentry
->d_name
);
1315 if (stat(buffer
, &stat_buf
) != 0) {
1316 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1317 path
[i
], dentry
->d_name
);
1319 Bool isFilePack
= False
;
1322 if (S_ISDIR(stat_buf
.st_mode
)
1323 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1325 /* access always returns success for user root */
1326 if (access(buffer
, X_OK
) == 0) {
1327 /* Directory is accesible. Add to directory list */
1329 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1330 data
->name
= wstrdup(dentry
->d_name
);
1333 WMAddToArray(dirs
, data
);
1335 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1336 /* Hack because access always returns X_OK success for user root */
1337 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1338 if ((command
!= NULL
&& access(buffer
, R_OK
) == 0) ||
1339 (command
== NULL
&& access(buffer
, X_OK
) == 0 &&
1340 (stat_buf
.st_mode
& S_IXANY
))) {
1342 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1343 data
->name
= wstrdup(dentry
->d_name
);
1346 WMAddToArray(files
, data
);
1357 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1363 WMSortArray(dirs
, myCompare
);
1364 WMSortArray(files
, myCompare
);
1366 menu
= wMenuCreate(scr
, title
, False
);
1367 menu
->on_destroy
= removeShortcutsForMenu
;
1369 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1370 /* New directory. Use same OPEN_MENU command that was used
1371 * for the current directory. */
1372 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1376 length
+= strlen(command
) + 6;
1377 buffer
= malloc(length
);
1379 wsyserror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1385 strcat(buffer
, "-noext ");
1387 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1390 strcat(buffer
, "\"");
1391 strcat(buffer
, path
[data
->index
]);
1393 strcat(buffer
, "/");
1394 strcat(buffer
, data
->name
);
1396 strcat(buffer
, "\"");
1398 strcat(buffer
, " WITH ");
1399 strcat(buffer
, command
);
1402 addMenuEntry(menu
, data
->name
, NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1410 WM_ITERATE_ARRAY(files
, data
, iter
) {
1411 /* executable: add as entry */
1412 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1414 length
+= strlen(command
);
1416 buffer
= malloc(length
);
1418 wsyserror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1422 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1423 if (command
!= NULL
) {
1424 strcpy(buffer
, command
);
1425 strcat(buffer
, " ");
1427 strcat(buffer
, "\"");
1428 strcat(buffer
, path
[data
->index
]);
1433 strcat(buffer
, path
[data
->index
]);
1435 strcpy(buffer
, path
[data
->index
]);
1438 strcat(buffer
, "/");
1439 strcat(buffer
, data
->name
);
1441 strcat(buffer
, "\"");
1443 if (stripExtension
) {
1444 char *ptr
= strrchr(data
->name
, '.');
1445 if (ptr
&& ptr
!= data
->name
)
1448 addMenuEntry(menu
, data
->name
, NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1462 /************ Menu Configuration From WMRootMenu *************/
1464 static WMenu
*makeDefaultMenu(WScreen
* scr
)
1468 menu
= wMenuCreate(scr
, _("Commands"), True
);
1469 wMenuAddCallback(menu
, "XTerm", execCommand
, "xterm");
1470 wMenuAddCallback(menu
, "rxvt", execCommand
, "rxvt");
1471 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1472 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1477 *----------------------------------------------------------------------
1479 * Reads root menu configuration from defaults database.
1481 *----------------------------------------------------------------------
1483 static WMenu
*configureMenu(WScreen
* scr
, WMPropList
* definition
)
1488 WMPropList
*title
, *command
, *params
;
1491 if (WMIsPLString(definition
)) {
1492 struct stat stat_buf
;
1494 Bool menu_is_default
= False
;
1496 /* menu definition is a string. Probably a path, so parse the file */
1498 tmp
= wexpandpath(WMGetFromPLString(definition
));
1500 path
= getLocalizedMenuFile(tmp
);
1503 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1506 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1507 menu_is_default
= True
;
1511 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"), tmp
);
1516 if (stat(path
, &stat_buf
) < 0) {
1517 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1523 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1524 /* if the pointer in WMRootMenu has changed */
1525 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1527 if (menu_is_default
) {
1529 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1533 menu
= readMenuFile(scr
, path
);
1535 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1545 count
= WMGetPropListItemCount(definition
);
1549 elem
= WMGetFromPLArray(definition
, 0);
1550 if (!WMIsPLString(elem
)) {
1551 tmp
= WMGetPropListDescription(elem
, False
);
1552 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1556 mtitle
= WMGetFromPLString(elem
);
1558 menu
= wMenuCreate(scr
, mtitle
, False
);
1559 menu
->on_destroy
= removeShortcutsForMenu
;
1561 #ifdef GLOBAL_SUBMENU_FILE
1566 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1569 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1570 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1575 for (i
= 1; i
< count
; i
++) {
1576 elem
= WMGetFromPLArray(definition
, i
);
1578 if (WMIsPLString(elem
)) {
1581 file
= WMGetFromPLString(elem
);
1585 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1588 if (WMIsPLArray(WMGetFromPLArray(elem
, 1))) {
1593 submenu
= configureMenu(scr
, elem
);
1595 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1596 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1600 WMPropList
*shortcut
;
1603 title
= WMGetFromPLArray(elem
, idx
++);
1604 shortcut
= WMGetFromPLArray(elem
, idx
++);
1605 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT") == 0) {
1606 shortcut
= WMGetFromPLArray(elem
, idx
++);
1607 command
= WMGetFromPLArray(elem
, idx
++);
1612 params
= WMGetFromPLArray(elem
, idx
++);
1614 if (!title
|| !command
)
1617 addMenuEntry(menu
, WMGetFromPLString(title
),
1618 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1619 WMGetFromPLString(command
),
1620 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1625 tmp
= WMGetPropListDescription(elem
, False
);
1626 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1634 *----------------------------------------------------------------------
1636 * Opens the root menu, parsing the menu configuration from the
1637 * defaults database.
1638 * If the menu is already mapped and is not sticked to the
1639 * root window, it will be unmapped.
1642 * The menu may be remade.
1645 * Construction of OPEN_MENU entries are delayed to the moment the
1647 *----------------------------------------------------------------------
1649 void OpenRootMenu(WScreen
* scr
, int x
, int y
, int keyboard
)
1652 WMPropList
*definition
;
1654 static WMPropList *domain=NULL;
1657 domain = WMCreatePLString("WMRootMenu");
1661 scr
->flags
.root_menu_changed_shortcuts
= 0;
1662 scr
->flags
.added_workspace_menu
= 0;
1663 scr
->flags
.added_windows_menu
= 0;
1665 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1666 menu
= scr
->root_menu
;
1667 if (!menu
->flags
.buttoned
) {
1670 wRaiseFrame(menu
->frame
->core
);
1673 wMenuMapAt(menu
, 0, 0, True
);
1675 wMenuMapCopyAt(menu
, x
- menu
->frame
->core
->width
/ 2, y
);
1680 definition
= WDRootMenu
->dictionary
;
1683 definition = PLGetDomain(domain);
1686 if (WMIsPLArray(definition
)) {
1687 if (!scr
->root_menu
|| WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1688 menu
= configureMenu(scr
, definition
);
1690 menu
->timestamp
= WDRootMenu
->timestamp
;
1695 menu
= configureMenu(scr
, definition
);
1700 /* menu hasn't changed or could not be read */
1701 if (!scr
->root_menu
) {
1702 wMessageDialog(scr
, _("Error"),
1703 _("The applications menu could not be loaded. "
1704 "Look at the console output for a detailed "
1705 "description of the errors."), _("OK"), NULL
, NULL
);
1707 menu
= makeDefaultMenu(scr
);
1708 scr
->root_menu
= menu
;
1710 menu
= scr
->root_menu
;
1713 if (scr
->root_menu
) {
1714 wMenuDestroy(scr
->root_menu
, True
);
1716 scr
->root_menu
= menu
;
1721 if (keyboard
&& x
== 0 && y
== 0) {
1723 } else if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
1724 newx
= x
- menu
->frame
->core
->width
/ 2;
1725 newy
= y
- menu
->frame
->core
->height
/ 2;
1727 newx
= x
- menu
->frame
->core
->width
/ 2;
1730 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1733 if (scr
->flags
.root_menu_changed_shortcuts
)
1734 rebindKeygrabs(scr
);