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
) {
198 printf("Exiting WindowMaker.\n");
200 Shutdown(WSExitMode
);
207 static void shutdownCommand(WMenu
* menu
, WMenuEntry
* entry
)
209 static int inside
= 0;
212 /* prevent reentrant calls */
222 if ((long)entry
->clientdata
== M_QUICK
)
226 int r
, oldSaveSessionFlag
;
228 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
230 r
= wExitDialog(menu
->frame
->screen_ptr
,
232 _("Kill Window System session?\n"
233 "(all applications will be closed)"), _("Kill"), _("Cancel"), NULL
);
234 if (r
== WAPRDefault
) {
236 } else if (r
== WAPRAlternate
) {
237 /* Don't modify the "save session on exit" flag if the
238 * user canceled the operation. */
239 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
244 if (result
!= R_CANCEL
) {
246 Shutdown(WSKillMode
);
255 static void restartCommand(WMenu
* menu
, WMenuEntry
* entry
)
257 Shutdown(WSRestartPreparationMode
);
258 Restart((char *)entry
->clientdata
, False
);
262 static void refreshCommand(WMenu
* menu
, WMenuEntry
* entry
)
264 wRefreshDesktop(menu
->frame
->screen_ptr
);
267 static void arrangeIconsCommand(WMenu
* menu
, WMenuEntry
* entry
)
269 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
272 static void showAllCommand(WMenu
* menu
, WMenuEntry
* entry
)
274 wShowAllWindows(menu
->frame
->screen_ptr
);
277 static void hideOthersCommand(WMenu
* menu
, WMenuEntry
* entry
)
279 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
282 static void saveSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
284 if (!wPreferences
.save_session_on_exit
)
285 wSessionSaveState(menu
->frame
->screen_ptr
);
287 wScreenSaveState(menu
->frame
->screen_ptr
);
290 static void clearSessionCommand(WMenu
* menu
, WMenuEntry
* entry
)
292 wSessionClearState(menu
->frame
->screen_ptr
);
293 wScreenSaveState(menu
->frame
->screen_ptr
);
296 static void infoPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
298 wShowInfoPanel(menu
->frame
->screen_ptr
);
301 static void legalPanelCommand(WMenu
* menu
, WMenuEntry
* entry
)
303 wShowLegalPanel(menu
->frame
->screen_ptr
);
306 /********************************************************************/
308 static char *getLocalizedMenuFile(char *menu
)
310 char *buffer
, *ptr
, *locale
;
316 len
= strlen(menu
) + strlen(Locale
) + 8;
317 buffer
= wmalloc(len
);
319 /* try menu.locale_name */
320 snprintf(buffer
, len
, "%s.%s", menu
, Locale
);
321 if (access(buffer
, F_OK
) == 0) {
325 /* position of locale in our buffer */
326 locale
= buffer
+ strlen(menu
) + 1;
328 /* check if it is in the form aa_bb.encoding and check for aa_bb */
329 ptr
= strchr(locale
, '.');
332 if (access(buffer
, F_OK
) == 0) {
336 /* now check for aa */
337 ptr
= strchr(locale
, '_');
340 if (access(buffer
, F_OK
) == 0) {
350 Bool
wRootMenuPerformShortcut(XEvent
* event
)
352 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
357 /* ignore CapsLock */
358 modifiers
= event
->xkey
.state
& ValidModMask
;
360 for (ptr
= shortcutList
; ptr
!= NULL
; ptr
= ptr
->next
) {
361 if (ptr
->keycode
== 0 || ptr
->menu
->menu
->screen_ptr
!= scr
)
364 if (ptr
->keycode
== event
->xkey
.keycode
&& ptr
->modifier
== modifiers
) {
365 (*ptr
->entry
->callback
) (ptr
->menu
, ptr
->entry
);
373 void wRootMenuBindShortcuts(Window window
)
379 if (ptr
->modifier
!= AnyModifier
) {
380 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
| LockMask
,
381 window
, True
, GrabModeAsync
, GrabModeAsync
);
383 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
386 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
, GrabModeAsync
, GrabModeAsync
);
391 static void rebindKeygrabs(WScreen
* scr
)
395 wwin
= scr
->focused_window
;
397 while (wwin
!= NULL
) {
398 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
400 if (!WFLAGP(wwin
, no_bind_keys
)) {
401 wWindowSetKeyGrabs(wwin
);
407 static void removeShortcutsForMenu(WMenu
* menu
)
410 Shortcut
*newList
= NULL
;
413 while (ptr
!= NULL
) {
415 if (ptr
->menu
== menu
) {
423 shortcutList
= newList
;
424 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
427 static Bool
addShortcut(char *file
, char *shortcutDefinition
, WMenu
* menu
, WMenuEntry
* entry
)
432 char buf
[MAX_SHORTCUT_LENGTH
], *b
;
434 ptr
= wmalloc(sizeof(Shortcut
));
436 strncpy(buf
, shortcutDefinition
, MAX_SHORTCUT_LENGTH
);
441 while ((k
= strchr(b
, '+')) != NULL
) {
445 mod
= wXModifierFromKey(b
);
447 wwarning(_("%s: invalid key modifier \"%s\""), file
, b
);
451 ptr
->modifier
|= mod
;
457 ksym
= XStringToKeysym(b
);
459 if (ksym
== NoSymbol
) {
460 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
461 file
, shortcutDefinition
, entry
->text
);
466 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
467 if (ptr
->keycode
== 0) {
468 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
469 shortcutDefinition
, entry
->text
);
477 ptr
->next
= shortcutList
;
480 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
485 /*******************************/
487 static char *cropline(char *line
)
491 if (strlen(line
) == 0)
494 end
= &(line
[strlen(line
)]) - 1;
495 while (isspace(*line
) && *line
!= 0)
497 while (end
> line
&& isspace(*end
)) {
504 static char *next_token(char *line
, char **next
)
510 while (*line
== ' ' || *line
== '\t')
518 while (*tmp
!= 0 && *tmp
!= '"')
521 wwarning(_("%s: unmatched '\"' in menu file"), line
);
532 } while (*tmp
!= 0 && *tmp
!= ' ' && *tmp
!= '\t');
546 while (*tmp
== ' ' || *tmp
== '\t')
555 static void separateCommand(char *line
, char ***file
, char **command
)
557 char *token
, *tmp
= line
;
558 WMArray
*array
= WMCreateArray(4);
564 token
= next_token(tmp
, &tmp
);
566 if (strcmp(token
, "WITH") == 0) {
567 if (tmp
!= NULL
&& *tmp
!= 0)
568 *command
= wstrdup(tmp
);
570 wwarning(_("%s: missing command"), line
);
573 WMAddToArray(array
, token
);
575 } while (token
!= NULL
&& tmp
!= NULL
);
577 count
= WMGetArrayItemCount(array
);
579 *file
= wmalloc(sizeof(char *) * (count
+ 1));
580 (*file
)[count
] = NULL
;
581 for (i
= 0; i
< count
; i
++) {
582 (*file
)[i
] = WMGetFromArray(array
, i
);
588 static void constructMenu(WMenu
* menu
, WMenuEntry
* entry
)
591 struct stat stat_buf
;
598 separateCommand((char *)entry
->clientdata
, &path
, &cmd
);
599 if (path
== NULL
|| *path
== NULL
|| **path
== 0) {
600 wwarning(_("invalid OPEN_MENU specification: %s"), (char *)entry
->clientdata
);
604 if (path
[0][0] == '|') {
607 if (!menu
->cascades
[entry
->cascade
] || menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
610 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
612 if (submenu
!= NULL
) {
613 if (path
[0][1] == '|')
614 submenu
->timestamp
= 0;
616 submenu
->timestamp
= 1; /* there's no automatic reloading */
624 while (path
[i
] != NULL
) {
627 if (strcmp(path
[i
], "-noext") == 0) {
632 tmp
= wexpandpath(path
[i
]);
634 lpath
= getLocalizedMenuFile(tmp
);
643 if (stat(path
[i
], &stat_buf
) == 0) {
644 if (last
< stat_buf
.st_mtime
)
645 last
= stat_buf
.st_mtime
;
649 wsyserror(_("%s:could not stat menu"), path
[i
]);
657 wsyserror(_("%s:could not stat menu:%s"), "OPEN_MENU", (char *)entry
->clientdata
);
660 stat(path
[first
], &stat_buf
);
661 if (!menu
->cascades
[entry
->cascade
]
662 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
664 if (S_ISDIR(stat_buf
.st_mode
)) {
666 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
, entry
->text
, path
, cmd
);
668 submenu
->timestamp
= last
;
669 } else if (S_ISREG(stat_buf
.st_mode
)) {
673 wwarning(_("too many parameters in OPEN_MENU: %s"),
674 (char *)entry
->clientdata
);
676 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
678 submenu
->timestamp
= stat_buf
.st_mtime
;
688 wMenuEntryRemoveCascade(menu
, entry
);
689 wMenuEntrySetCascade(menu
, entry
, submenu
);
694 while (path
[i
] != NULL
)
701 static void cleanupWorkspaceMenu(WMenu
* menu
)
703 if (menu
->frame
->screen_ptr
->workspace_menu
== menu
)
704 menu
->frame
->screen_ptr
->workspace_menu
= NULL
;
707 static WMenuEntry
*addWorkspaceMenu(WScreen
* scr
, WMenu
* menu
, char *title
)
712 if (scr
->flags
.added_workspace_menu
) {
714 ("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
717 scr
->flags
.added_workspace_menu
= 1;
719 wsmenu
= wWorkspaceMenuMake(scr
, True
);
720 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
722 scr
->workspace_menu
= wsmenu
;
723 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
724 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
726 wWorkspaceMenuUpdate(scr
, wsmenu
);
731 static void cleanupWindowsMenu(WMenu
* menu
)
733 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
734 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
737 static WMenuEntry
*addWindowsMenu(WScreen
* scr
, WMenu
* menu
, char *title
)
743 if (scr
->flags
.added_windows_menu
) {
745 ("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
748 scr
->flags
.added_windows_menu
= 1;
750 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
751 wwmenu
->on_destroy
= cleanupWindowsMenu
;
752 scr
->switch_menu
= wwmenu
;
753 wwin
= scr
->focused_window
;
755 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
759 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
760 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
765 static WMenuEntry
*addMenuEntry(WMenu
* menu
, char *title
, char *shortcut
, char *command
,
766 char *params
, char *file_name
)
769 WMenuEntry
*entry
= NULL
;
770 Bool shortcutOk
= False
;
774 scr
= menu
->frame
->screen_ptr
;
775 if (strcmp(command
, "OPEN_MENU") == 0) {
777 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
782 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
784 path
= wstrdup(params
);
786 dummy
= wMenuCreate(scr
, title
, False
);
787 dummy
->on_destroy
= removeShortcutsForMenu
;
788 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
789 entry
->free_cdata
= free
;
790 wMenuEntrySetCascade(menu
, entry
, dummy
);
792 } else if (strcmp(command
, "EXEC") == 0) {
794 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
796 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrconcat("exec ", params
));
797 entry
->free_cdata
= free
;
800 } else if (strcmp(command
, "SHEXEC") == 0) {
802 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name
, command
);
804 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrdup(params
));
805 entry
->free_cdata
= free
;
808 } else if (strcmp(command
, "EXIT") == 0) {
810 if (params
&& strcmp(params
, "QUICK") == 0)
811 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void *)M_QUICK
);
813 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
816 } else if (strcmp(command
, "SHUTDOWN") == 0) {
818 if (params
&& strcmp(params
, "QUICK") == 0)
819 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, (void *)M_QUICK
);
821 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
824 } else if (strcmp(command
, "REFRESH") == 0) {
825 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
828 } else if (strcmp(command
, "WORKSPACE_MENU") == 0) {
829 entry
= addWorkspaceMenu(scr
, menu
, title
);
832 } else if (strcmp(command
, "WINDOWS_MENU") == 0) {
833 entry
= addWindowsMenu(scr
, menu
, title
);
836 } else if (strcmp(command
, "ARRANGE_ICONS") == 0) {
837 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
840 } else if (strcmp(command
, "HIDE_OTHERS") == 0) {
841 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
844 } else if (strcmp(command
, "SHOW_ALL") == 0) {
845 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
848 } else if (strcmp(command
, "RESTART") == 0) {
849 entry
= wMenuAddCallback(menu
, title
, restartCommand
, params
? wstrdup(params
) : NULL
);
850 entry
->free_cdata
= free
;
852 } else if (strcmp(command
, "SAVE_SESSION") == 0) {
853 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
856 } else if (strcmp(command
, "CLEAR_SESSION") == 0) {
857 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
859 } else if (strcmp(command
, "INFO_PANEL") == 0) {
860 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
862 } else if (strcmp(command
, "LEGAL_PANEL") == 0) {
863 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
866 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
, command
);
871 if (shortcut
&& entry
) {
873 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
, title
);
875 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
877 entry
->rtext
= GetShortcutString(shortcut
);
879 entry->rtext = wstrdup(shortcut);
888 /******************* Menu Configuration From File *******************/
890 static void separateline(char *line
, char *title
, char *command
, char *parameter
, char *shortcut
)
901 while (isspace(*line
) && (*line
!= 0))
906 while (line
[i
] != '"' && (line
[i
] != 0))
912 while (!isspace(line
[i
]) && (line
[i
] != 0))
915 strncpy(title
, line
, i
);
919 /* get the command or shortcut keyword */
920 while (isspace(*line
) && (*line
!= 0))
925 while (!isspace(line
[i
]) && (line
[i
] != 0))
927 strncpy(command
, line
, i
);
931 if (strcmp(command
, "SHORTCUT") == 0) {
932 /* get the shortcut key */
933 while (isspace(*line
) && (*line
!= 0))
938 while (line
[i
] != '"' && (line
[i
] != 0))
944 while (!isspace(line
[i
]) && (line
[i
] != 0))
947 strncpy(shortcut
, line
, i
);
953 /* get the command */
954 while (isspace(*line
) && (*line
!= 0))
959 while (!isspace(line
[i
]) && (line
[i
] != 0))
961 strncpy(command
, line
, i
);
966 /* get the parameters */
967 while (isspace(*line
) && (*line
!= 0))
975 while (line
[l
] != 0 && line
[l
] != '"') {
976 parameter
[l
] = line
[l
];
984 while (isspace(line
[l
]) && (l
> 0))
986 strncpy(parameter
, line
, l
);
990 static WMenu
*parseCascade(WScreen
* scr
, WMenu
* menu
, FILE * file
, char *file_name
)
992 char linebuf
[MAXLINE
];
993 char elinebuf
[MAXLINE
];
995 char command
[MAXLINE
];
996 char shortcut
[MAXLINE
];
997 char params
[MAXLINE
];
1000 while (!feof(file
)) {
1004 fgets(linebuf
, MAXLINE
, file
);
1005 line
= cropline(linebuf
);
1006 lsize
= strlen(line
);
1008 if (line
[lsize
- 1] == '\\') {
1011 fgets(elinebuf
, MAXLINE
, file
);
1012 line2
= cropline(elinebuf
);
1013 lsize2
= strlen(line2
);
1014 if (lsize2
+ lsize
> MAXLINE
) {
1015 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1019 line
[lsize
- 1] = 0;
1020 lsize
+= lsize2
- 1;
1021 strcat(line
, line2
);
1026 } while (!ok
&& !feof(file
));
1030 if (line
[0] == 0 || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
1033 separateline(line
, title
, command
, params
, shortcut
);
1036 wwarning(_("%s:missing command in menu config: %s"), file_name
, line
);
1040 if (strcasecmp(command
, "MENU") == 0) {
1045 cascade
= wMenuCreate(scr
, title
, False
);
1046 cascade
->on_destroy
= removeShortcutsForMenu
;
1047 if (parseCascade(scr
, cascade
, file
, file_name
) == NULL
) {
1048 wMenuDestroy(cascade
, True
);
1050 wMenuEntrySetCascade(menu
, wMenuAddCallback(menu
, title
, NULL
, NULL
), cascade
);
1052 } else if (strcasecmp(command
, "END") == 0) {
1058 addMenuEntry(menu
, title
, shortcut
[0] ? shortcut
: NULL
, command
,
1059 params
[0] ? params
: NULL
, file_name
);
1063 wwarning(_("%s:syntax error in menu file:END declaration missing"), file_name
);
1070 static WMenu
*readMenuFile(WScreen
* scr
, char *file_name
)
1074 char linebuf
[MAXLINE
];
1075 char title
[MAXLINE
];
1076 char shortcut
[MAXLINE
];
1077 char command
[MAXLINE
];
1078 char params
[MAXLINE
];
1086 if (!wPreferences
.flags
.nocpp
) {
1087 args
= MakeCPPArgs(file_name
);
1089 wwarning(_("could not make arguments for menu file preprocessor"));
1091 snprintf(command
, sizeof(command
), "%s %s %s", CPP_PATH
, args
, file_name
);
1093 file
= popen(command
, "r");
1095 wsyserror(_("%s:could not open/preprocess menu file"), file_name
);
1104 file
= fopen(file_name
, "rb");
1106 wsyserror(_("%s:could not open menu file"), file_name
);
1111 while (!feof(file
)) {
1112 if (!fgets(linebuf
, MAXLINE
, file
))
1114 line
= cropline(linebuf
);
1115 if (line
[0] == 0 || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
1118 separateline(line
, title
, command
, params
, shortcut
);
1121 wwarning(_("%s:missing command in menu config: %s"), file_name
, line
);
1124 if (strcasecmp(command
, "MENU") == 0) {
1125 menu
= wMenuCreate(scr
, title
, True
);
1126 menu
->on_destroy
= removeShortcutsForMenu
;
1127 if (!parseCascade(scr
, menu
, file
, file_name
)) {
1128 wMenuDestroy(menu
, True
);
1132 wwarning(_("%s:invalid menu file. MENU command is missing"), file_name
);
1139 if (pclose(file
) == -1) {
1140 wsyserror(_("error reading preprocessed menu data"));
1152 /************ Menu Configuration From Pipe *************/
1154 static WMenu
*readMenuPipe(WScreen
* scr
, char **file_name
)
1158 char linebuf
[MAXLINE
];
1159 char title
[MAXLINE
];
1160 char command
[MAXLINE
];
1161 char params
[MAXLINE
];
1162 char shortcut
[MAXLINE
];
1165 char flat_file
[MAXLINE
];
1172 flat_file
[0] = '\0';
1174 for (i
= 0; file_name
[i
] != NULL
; i
++) {
1175 strcat(flat_file
, file_name
[i
]);
1176 strcat(flat_file
, " ");
1178 filename
= flat_file
+ (flat_file
[1] == '|' ? 2 : 1);
1181 if (!wPreferences
.flags
.nocpp
) {
1182 args
= MakeCPPArgs(filename
);
1184 wwarning(_("could not make arguments for menu file preprocessor"));
1186 snprintf(command
, sizeof(command
), "%s | %s %s", filename
, CPP_PATH
, args
);
1189 file
= popen(command
, "r");
1191 wsyserror(_("%s:could not open/preprocess menu file"), filename
);
1200 file
= popen(filename
, "rb");
1203 wsyserror(_("%s:could not open menu file"), filename
);
1208 while (!feof(file
)) {
1209 if (!fgets(linebuf
, MAXLINE
, file
))
1211 line
= cropline(linebuf
);
1212 if (line
[0] == 0 || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
1215 separateline(line
, title
, command
, params
, shortcut
);
1218 wwarning(_("%s:missing command in menu config: %s"), file_name
, line
);
1221 if (strcasecmp(command
, "MENU") == 0) {
1222 menu
= wMenuCreate(scr
, title
, True
);
1223 menu
->on_destroy
= removeShortcutsForMenu
;
1224 if (!parseCascade(scr
, menu
, file
, filename
)) {
1225 wMenuDestroy(menu
, True
);
1229 wwarning(_("%s:no title given for the root menu"), filename
);
1244 static int myCompare(const void *d1
, const void *d2
)
1246 dir_data
*p1
= *(dir_data
**) d1
;
1247 dir_data
*p2
= *(dir_data
**) d2
;
1249 return strcmp(p1
->name
, p2
->name
);
1252 /************ Menu Configuration From Directory *************/
1254 static Bool
isFilePackage(char *file
)
1258 /* check if the extension indicates this file is a
1259 * file package. For now, only recognize .themed */
1263 if (l
> 7 && strcmp(&(file
[l
- 7]), ".themed") == 0) {
1270 static WMenu
*readMenuDirectory(WScreen
* scr
, char *title
, char **path
, char *command
)
1273 struct dirent
*dentry
;
1274 struct stat stat_buf
;
1277 WMArray
*dirs
= NULL
, *files
= NULL
;
1278 WMArrayIterator iter
;
1279 int length
, i
, have_space
= 0;
1281 int stripExtension
= 0;
1283 dirs
= WMCreateArray(16);
1284 files
= WMCreateArray(16);
1287 while (path
[i
] != NULL
) {
1288 if (strcmp(path
[i
], "-noext") == 0) {
1294 dir
= opendir(path
[i
]);
1300 while ((dentry
= readdir(dir
))) {
1302 if (strcmp(dentry
->d_name
, ".") == 0 || strcmp(dentry
->d_name
, "..") == 0)
1305 if (dentry
->d_name
[0] == '.')
1308 buffer
= malloc(strlen(path
[i
]) + strlen(dentry
->d_name
) + 4);
1310 wsyserror(_("out of memory while constructing directory menu %s"), path
[i
]);
1314 strcpy(buffer
, path
[i
]);
1315 strcat(buffer
, "/");
1316 strcat(buffer
, dentry
->d_name
);
1318 if (stat(buffer
, &stat_buf
) != 0) {
1319 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1320 path
[i
], dentry
->d_name
);
1322 Bool isFilePack
= False
;
1325 if (S_ISDIR(stat_buf
.st_mode
)
1326 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1328 /* access always returns success for user root */
1329 if (access(buffer
, X_OK
) == 0) {
1330 /* Directory is accesible. Add to directory list */
1332 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1333 data
->name
= wstrdup(dentry
->d_name
);
1336 WMAddToArray(dirs
, data
);
1338 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1339 /* Hack because access always returns X_OK success for user root */
1340 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1341 if ((command
!= NULL
&& access(buffer
, R_OK
) == 0) ||
1342 (command
== NULL
&& access(buffer
, X_OK
) == 0 &&
1343 (stat_buf
.st_mode
& S_IXANY
))) {
1345 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1346 data
->name
= wstrdup(dentry
->d_name
);
1349 WMAddToArray(files
, data
);
1360 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1366 WMSortArray(dirs
, myCompare
);
1367 WMSortArray(files
, myCompare
);
1369 menu
= wMenuCreate(scr
, title
, False
);
1370 menu
->on_destroy
= removeShortcutsForMenu
;
1372 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1373 /* New directory. Use same OPEN_MENU command that was used
1374 * for the current directory. */
1375 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1379 length
+= strlen(command
) + 6;
1380 buffer
= malloc(length
);
1382 wsyserror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1388 strcat(buffer
, "-noext ");
1390 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1393 strcat(buffer
, "\"");
1394 strcat(buffer
, path
[data
->index
]);
1396 strcat(buffer
, "/");
1397 strcat(buffer
, data
->name
);
1399 strcat(buffer
, "\"");
1401 strcat(buffer
, " WITH ");
1402 strcat(buffer
, command
);
1405 addMenuEntry(menu
, data
->name
, NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1413 WM_ITERATE_ARRAY(files
, data
, iter
) {
1414 /* executable: add as entry */
1415 length
= strlen(path
[data
->index
]) + strlen(data
->name
) + 6;
1417 length
+= strlen(command
);
1419 buffer
= malloc(length
);
1421 wsyserror(_("out of memory while constructing directory menu %s"), path
[data
->index
]);
1425 have_space
= strchr(path
[data
->index
], ' ') != NULL
|| strchr(data
->name
, ' ') != NULL
;
1426 if (command
!= NULL
) {
1427 strcpy(buffer
, command
);
1428 strcat(buffer
, " ");
1430 strcat(buffer
, "\"");
1431 strcat(buffer
, path
[data
->index
]);
1436 strcat(buffer
, path
[data
->index
]);
1438 strcpy(buffer
, path
[data
->index
]);
1441 strcat(buffer
, "/");
1442 strcat(buffer
, data
->name
);
1444 strcat(buffer
, "\"");
1446 if (stripExtension
) {
1447 char *ptr
= strrchr(data
->name
, '.');
1448 if (ptr
&& ptr
!= data
->name
)
1451 addMenuEntry(menu
, data
->name
, NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1465 /************ Menu Configuration From WMRootMenu *************/
1467 static WMenu
*makeDefaultMenu(WScreen
* scr
)
1471 menu
= wMenuCreate(scr
, _("Commands"), True
);
1472 wMenuAddCallback(menu
, "XTerm", execCommand
, "xterm");
1473 wMenuAddCallback(menu
, "rxvt", execCommand
, "rxvt");
1474 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1475 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1480 *----------------------------------------------------------------------
1482 * Reads root menu configuration from defaults database.
1484 *----------------------------------------------------------------------
1486 static WMenu
*configureMenu(WScreen
* scr
, WMPropList
* definition
)
1491 WMPropList
*title
, *command
, *params
;
1494 if (WMIsPLString(definition
)) {
1495 struct stat stat_buf
;
1497 Bool menu_is_default
= False
;
1499 /* menu definition is a string. Probably a path, so parse the file */
1501 tmp
= wexpandpath(WMGetFromPLString(definition
));
1503 path
= getLocalizedMenuFile(tmp
);
1506 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1509 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1510 menu_is_default
= True
;
1514 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"), tmp
);
1519 if (stat(path
, &stat_buf
) < 0) {
1520 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1526 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1527 /* if the pointer in WMRootMenu has changed */
1528 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1530 if (menu_is_default
) {
1532 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1536 menu
= readMenuFile(scr
, path
);
1538 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1548 count
= WMGetPropListItemCount(definition
);
1552 elem
= WMGetFromPLArray(definition
, 0);
1553 if (!WMIsPLString(elem
)) {
1554 tmp
= WMGetPropListDescription(elem
, False
);
1555 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1559 mtitle
= WMGetFromPLString(elem
);
1561 menu
= wMenuCreate(scr
, mtitle
, False
);
1562 menu
->on_destroy
= removeShortcutsForMenu
;
1564 #ifdef GLOBAL_SUBMENU_FILE
1569 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1572 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1573 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1578 for (i
= 1; i
< count
; i
++) {
1579 elem
= WMGetFromPLArray(definition
, i
);
1581 if (WMIsPLString(elem
)) {
1584 file
= WMGetFromPLString(elem
);
1588 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1591 if (WMIsPLArray(WMGetFromPLArray(elem
, 1))) {
1596 submenu
= configureMenu(scr
, elem
);
1598 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1599 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1603 WMPropList
*shortcut
;
1606 title
= WMGetFromPLArray(elem
, idx
++);
1607 shortcut
= WMGetFromPLArray(elem
, idx
++);
1608 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT") == 0) {
1609 shortcut
= WMGetFromPLArray(elem
, idx
++);
1610 command
= WMGetFromPLArray(elem
, idx
++);
1615 params
= WMGetFromPLArray(elem
, idx
++);
1617 if (!title
|| !command
)
1620 addMenuEntry(menu
, WMGetFromPLString(title
),
1621 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1622 WMGetFromPLString(command
),
1623 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1628 tmp
= WMGetPropListDescription(elem
, False
);
1629 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp
);
1637 *----------------------------------------------------------------------
1639 * Opens the root menu, parsing the menu configuration from the
1640 * defaults database.
1641 * If the menu is already mapped and is not sticked to the
1642 * root window, it will be unmapped.
1645 * The menu may be remade.
1648 * Construction of OPEN_MENU entries are delayed to the moment the
1650 *----------------------------------------------------------------------
1652 void OpenRootMenu(WScreen
* scr
, int x
, int y
, int keyboard
)
1655 WMPropList
*definition
;
1657 static WMPropList *domain=NULL;
1660 domain = WMCreatePLString("WMRootMenu");
1664 scr
->flags
.root_menu_changed_shortcuts
= 0;
1665 scr
->flags
.added_workspace_menu
= 0;
1666 scr
->flags
.added_windows_menu
= 0;
1668 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1669 menu
= scr
->root_menu
;
1670 if (!menu
->flags
.buttoned
) {
1673 wRaiseFrame(menu
->frame
->core
);
1676 wMenuMapAt(menu
, 0, 0, True
);
1678 wMenuMapCopyAt(menu
, x
- menu
->frame
->core
->width
/ 2, y
);
1683 definition
= WDRootMenu
->dictionary
;
1686 definition = PLGetDomain(domain);
1689 if (WMIsPLArray(definition
)) {
1690 if (!scr
->root_menu
|| WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1691 menu
= configureMenu(scr
, definition
);
1693 menu
->timestamp
= WDRootMenu
->timestamp
;
1698 menu
= configureMenu(scr
, definition
);
1703 /* menu hasn't changed or could not be read */
1704 if (!scr
->root_menu
) {
1705 wMessageDialog(scr
, _("Error"),
1706 _("The applications menu could not be loaded. "
1707 "Look at the console output for a detailed "
1708 "description of the errors."), _("OK"), NULL
, NULL
);
1710 menu
= makeDefaultMenu(scr
);
1711 scr
->root_menu
= menu
;
1713 menu
= scr
->root_menu
;
1716 if (scr
->root_menu
) {
1717 wMenuDestroy(scr
->root_menu
, True
);
1719 scr
->root_menu
= menu
;
1724 if (keyboard
&& x
== 0 && y
== 0) {
1726 } else if (keyboard
&& x
== scr
->scr_width
/ 2 && y
== scr
->scr_height
/ 2) {
1727 newx
= x
- menu
->frame
->core
->width
/ 2;
1728 newy
= y
- menu
->frame
->core
->height
/ 2;
1730 newx
= x
- menu
->frame
->core
->width
/ 2;
1733 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1736 if (scr
->flags
.root_menu_changed_shortcuts
)
1737 rebindKeygrabs(scr
);