WINGs: fix possible NULL pointer dereference (Coverity #50197)
[wmaker-crm.git] / src / rootmenu.c
blobca7750c9df55676a832e11d5ff72e40f66b8d1ac
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.
23 #include "wconfig.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include <sys/types.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <ctype.h>
34 #include <time.h>
35 #include <dirent.h>
37 #include <X11/Xlib.h>
38 #include <X11/Xutil.h>
39 #include <X11/Xatom.h>
41 #include "WindowMaker.h"
42 #include "actions.h"
43 #include "menu.h"
44 #include "misc.h"
45 #include "main.h"
46 #include "dialog.h"
47 #include "keybind.h"
48 #include "stacking.h"
49 #include "workspace.h"
50 #include "defaults.h"
51 #include "framewin.h"
52 #include "session.h"
53 #include "shutdown.h"
54 #include "xmodifier.h"
55 #include "rootmenu.h"
56 #include "startup.h"
57 #include "switchmenu.h"
59 #include <WINGs/WUtil.h>
61 #define MAX_SHORTCUT_LENGTH 32
64 static WMenu *readMenuPipe(WScreen * scr, char **file_name);
65 static WMenu *readPLMenuPipe(WScreen * scr, char **file_name);
66 static WMenu *readMenuFile(WScreen *scr, const char *file_name);
67 static WMenu *readMenuDirectory(WScreen *scr, const char *title, char **file_name, const char *command);
68 static WMenu *configureMenu(WScreen * scr, WMPropList * definition, Bool includeGlobals);
69 static void menu_parser_register_macros(WMenuParser parser);
71 typedef struct Shortcut {
72 struct Shortcut *next;
74 int modifier;
75 KeyCode keycode;
76 WMenuEntry *entry;
77 WMenu *menu;
78 } Shortcut;
80 static Shortcut *shortcutList = NULL;
83 * Syntax:
84 * # main menu
85 * "Menu Name" MENU
86 * "Title" EXEC command_to_exec -params
87 * "Submenu" MENU
88 * "Title" EXEC command_to_exec -params
89 * "Submenu" END
90 * "Workspaces" WORKSPACE_MENU
91 * "Title" built_in_command
92 * "Quit" EXIT
93 * "Quick Quit" EXIT QUICK
94 * "Menu Name" END
96 * Commands may be preceded by SHORTCUT key
98 * Built-in commands:
100 * INFO_PANEL - shows the Info Panel
101 * LEGAL_PANEL - shows the Legal info panel
102 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
103 * REFRESH - forces the desktop to be repainted
104 * EXIT [QUICK] - exit the window manager [without confirmation]
105 * EXEC <program> - execute an external program
106 * SHEXEC <command> - execute a shell command
107 * WORKSPACE_MENU - places the workspace submenu
108 * ARRANGE_ICONS
109 * RESTART [<window manager>] - restarts the window manager
110 * SHOW_ALL - unhide all windows on workspace
111 * HIDE_OTHERS - hides all windows excep the focused one
112 * OPEN_MENU file - read menu data from file which must be a valid menu file.
113 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
114 * - read menu data from directory(ies) and
115 * eventually precede each with a command.
116 * OPEN_MENU | command
117 * - opens command and uses its stdout to construct and insert
118 * the resulting menu in current position. The output of
119 * command must be a valid menu description.
120 * The space between '|' and command is optional.
121 * || will do the same, but will not cache the contents.
122 * OPEN_PLMENU | command
123 * - opens command and uses its stdout which must be in proplist
124 * fromat to construct and insert the resulting menu in current
125 * position.
126 * The space between '|' and command is optional.
127 * || will do the same, but will not cache the contents.
128 * SAVE_SESSION - saves the current state of the desktop, which include
129 * all running applications, all their hints (geometry,
130 * position on screen, workspace they live on, the dock
131 * or clip from where they were launched, and
132 * if minimized, shaded or hidden. Also saves the current
133 * workspace the user is on. All will be restored on every
134 * start of windowmaker until another SAVE_SESSION or
135 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
136 * WindowMaker domain file, then saving is automatically
137 * done on every windowmaker exit, overwriting any
138 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
139 * dock state now.
140 * CLEAR_SESSION - clears any previous saved session. This will not have
141 * any effect if SaveSessionOnExit is True.
145 #define M_QUICK 1
147 /* menu commands */
149 static void execCommand(WMenu * menu, WMenuEntry * entry)
151 char *cmdline;
153 cmdline = ExpandOptions(menu->frame->screen_ptr, (char *)entry->clientdata);
155 XGrabPointer(dpy, menu->frame->screen_ptr->root_win, True, 0,
156 GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_WAIT], CurrentTime);
157 XSync(dpy, 0);
159 if (cmdline) {
160 ExecuteShellCommand(menu->frame->screen_ptr, cmdline);
161 wfree(cmdline);
163 XUngrabPointer(dpy, CurrentTime);
164 XSync(dpy, 0);
167 static void exitCommand(WMenu * menu, WMenuEntry * entry)
169 static int inside = 0;
170 int result;
172 /* prevent reentrant calls */
173 if (inside)
174 return;
175 inside = 1;
177 #define R_CANCEL 0
178 #define R_EXIT 1
180 result = R_CANCEL;
182 if ((long)entry->clientdata == M_QUICK) {
183 result = R_EXIT;
184 } else {
185 int r, oldSaveSessionFlag;
187 oldSaveSessionFlag = wPreferences.save_session_on_exit;
188 r = wExitDialog(menu->frame->screen_ptr, _("Exit"),
189 _("Exit window manager?"), _("Exit"), _("Cancel"), NULL);
191 if (r == WAPRDefault) {
192 result = R_EXIT;
193 } else if (r == WAPRAlternate) {
194 /* Don't modify the "save session on exit" flag if the
195 * user canceled the operation. */
196 wPreferences.save_session_on_exit = oldSaveSessionFlag;
199 if (result == R_EXIT)
200 Shutdown(WSExitMode);
202 #undef R_EXIT
203 #undef R_CANCEL
204 inside = 0;
207 static void shutdownCommand(WMenu * menu, WMenuEntry * entry)
209 static int inside = 0;
210 int result;
212 /* prevent reentrant calls */
213 if (inside)
214 return;
215 inside = 1;
217 #define R_CANCEL 0
218 #define R_CLOSE 1
219 #define R_KILL 2
221 result = R_CANCEL;
222 if ((long)entry->clientdata == M_QUICK)
223 result = R_CLOSE;
224 else {
225 int r, oldSaveSessionFlag;
227 oldSaveSessionFlag = wPreferences.save_session_on_exit;
229 r = wExitDialog(menu->frame->screen_ptr,
230 _("Kill X session"),
231 _("Kill Window System session?\n"
232 "(all applications will be closed)"), _("Kill"), _("Cancel"), NULL);
233 if (r == WAPRDefault) {
234 result = R_KILL;
235 } else if (r == WAPRAlternate) {
236 /* Don't modify the "save session on exit" flag if the
237 * user canceled the operation. */
238 wPreferences.save_session_on_exit = oldSaveSessionFlag;
242 if (result != R_CANCEL) {
243 Shutdown(WSKillMode);
245 #undef R_CLOSE
246 #undef R_CANCEL
247 #undef R_KILL
248 inside = 0;
251 static void restartCommand(WMenu * menu, WMenuEntry * entry)
253 /* Parameter not used, but tell the compiler that it is ok */
254 (void) menu;
255 (void) entry;
257 Shutdown(WSRestartPreparationMode);
258 Restart((char *)entry->clientdata, False);
259 Restart(NULL, True);
262 static void refreshCommand(WMenu * menu, WMenuEntry * entry)
264 /* Parameter not used, but tell the compiler that it is ok */
265 (void) entry;
267 wRefreshDesktop(menu->frame->screen_ptr);
270 static void arrangeIconsCommand(WMenu * menu, WMenuEntry * entry)
272 /* Parameter not used, but tell the compiler that it is ok */
273 (void) entry;
275 wArrangeIcons(menu->frame->screen_ptr, True);
278 static void showAllCommand(WMenu * menu, WMenuEntry * entry)
280 /* Parameter not used, but tell the compiler that it is ok */
281 (void) entry;
283 wShowAllWindows(menu->frame->screen_ptr);
286 static void hideOthersCommand(WMenu * menu, WMenuEntry * entry)
288 /* Parameter not used, but tell the compiler that it is ok */
289 (void) entry;
291 wHideOtherApplications(menu->frame->screen_ptr->focused_window);
294 static void saveSessionCommand(WMenu * menu, WMenuEntry * entry)
296 /* Parameter not used, but tell the compiler that it is ok */
297 (void) entry;
299 if (!wPreferences.save_session_on_exit)
300 wSessionSaveState(menu->frame->screen_ptr);
302 wScreenSaveState(menu->frame->screen_ptr);
305 static void clearSessionCommand(WMenu *menu, WMenuEntry *entry)
307 /* Parameter not used, but tell the compiler that it is ok */
308 (void) entry;
310 wSessionClearState();
311 wScreenSaveState(menu->frame->screen_ptr);
314 static void infoPanelCommand(WMenu * menu, WMenuEntry * entry)
316 /* Parameter not used, but tell the compiler that it is ok */
317 (void) entry;
319 wShowInfoPanel(menu->frame->screen_ptr);
322 static void legalPanelCommand(WMenu * menu, WMenuEntry * entry)
324 /* Parameter not used, but tell the compiler that it is ok */
325 (void) entry;
327 wShowLegalPanel(menu->frame->screen_ptr);
330 /********************************************************************/
332 static char *getLocalizedMenuFile(const char *menu)
334 char *buffer, *ptr, *locale;
335 int len;
337 if (!w_global.locale)
338 return NULL;
340 len = strlen(menu) + strlen(w_global.locale) + 8;
341 buffer = wmalloc(len);
343 /* try menu.locale_name */
344 snprintf(buffer, len, "%s.%s", menu, w_global.locale);
345 if (access(buffer, F_OK) == 0)
346 return buffer;
348 /* position of locale in our buffer */
349 locale = buffer + strlen(menu) + 1;
351 /* check if it is in the form aa_bb.encoding and check for aa_bb */
352 ptr = strchr(locale, '.');
353 if (ptr) {
354 *ptr = 0;
355 if (access(buffer, F_OK) == 0)
356 return buffer;
359 /* now check for aa */
360 ptr = strchr(locale, '_');
361 if (ptr) {
362 *ptr = 0;
363 if (access(buffer, F_OK) == 0)
364 return buffer;
367 wfree(buffer);
369 return NULL;
372 Bool wRootMenuPerformShortcut(XEvent * event)
374 WScreen *scr = wScreenForRootWindow(event->xkey.root);
375 Shortcut *ptr;
376 int modifiers;
377 int done = 0;
379 /* ignore CapsLock */
380 modifiers = event->xkey.state & w_global.shortcut.modifiers_mask;
382 for (ptr = shortcutList; ptr != NULL; ptr = ptr->next) {
383 if (ptr->keycode == 0 || ptr->menu->menu->screen_ptr != scr)
384 continue;
386 if (ptr->keycode == event->xkey.keycode && ptr->modifier == modifiers) {
387 (*ptr->entry->callback) (ptr->menu, ptr->entry);
388 done = True;
392 return done;
395 void wRootMenuBindShortcuts(Window window)
397 Shortcut *ptr;
399 ptr = shortcutList;
400 while (ptr) {
401 if (ptr->modifier != AnyModifier) {
402 XGrabKey(dpy, ptr->keycode, ptr->modifier | LockMask,
403 window, True, GrabModeAsync, GrabModeAsync);
404 #ifdef NUMLOCK_HACK
405 wHackedGrabKey(ptr->keycode, ptr->modifier, window, True, GrabModeAsync, GrabModeAsync);
406 #endif
408 XGrabKey(dpy, ptr->keycode, ptr->modifier, window, True, GrabModeAsync, GrabModeAsync);
409 ptr = ptr->next;
413 static void rebindKeygrabs(WScreen * scr)
415 WWindow *wwin;
417 wwin = scr->focused_window;
419 while (wwin != NULL) {
420 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->frame->core->window);
422 if (!WFLAGP(wwin, no_bind_keys)) {
423 wWindowSetKeyGrabs(wwin);
425 wwin = wwin->prev;
429 static void removeShortcutsForMenu(WMenu * menu)
431 Shortcut *ptr, *tmp;
432 Shortcut *newList = NULL;
434 ptr = shortcutList;
435 while (ptr != NULL) {
436 tmp = ptr->next;
437 if (ptr->menu == menu) {
438 wfree(ptr);
439 } else {
440 ptr->next = newList;
441 newList = ptr;
443 ptr = tmp;
445 shortcutList = newList;
446 menu->menu->screen_ptr->flags.root_menu_changed_shortcuts = 1;
449 static Bool addShortcut(const char *file, const char *shortcutDefinition, WMenu *menu, WMenuEntry *entry)
451 Shortcut *ptr;
452 KeySym ksym;
453 char *k;
454 char buf[MAX_SHORTCUT_LENGTH], *b;
456 ptr = wmalloc(sizeof(Shortcut));
458 wstrlcpy(buf, shortcutDefinition, MAX_SHORTCUT_LENGTH);
459 b = (char *)buf;
461 /* get modifiers */
462 ptr->modifier = 0;
463 while ((k = strchr(b, '+')) != NULL) {
464 int mod;
466 *k = 0;
467 mod = wXModifierFromKey(b);
468 if (mod < 0) {
469 wwarning(_("%s: invalid key modifier \"%s\""), file, b);
470 wfree(ptr);
471 return False;
473 ptr->modifier |= mod;
475 b = k + 1;
478 /* get key */
479 ksym = XStringToKeysym(b);
481 if (ksym == NoSymbol) {
482 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
483 file, shortcutDefinition, entry->text);
484 wfree(ptr);
485 return False;
488 ptr->keycode = XKeysymToKeycode(dpy, ksym);
489 if (ptr->keycode == 0) {
490 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file,
491 shortcutDefinition, entry->text);
492 wfree(ptr);
493 return False;
496 ptr->menu = menu;
497 ptr->entry = entry;
499 ptr->next = shortcutList;
500 shortcutList = ptr;
502 menu->menu->screen_ptr->flags.root_menu_changed_shortcuts = 1;
504 return True;
507 static char *next_token(char *line, char **next)
509 char *tmp, c;
510 char *ret;
512 *next = NULL;
513 while (*line == ' ' || *line == '\t')
514 line++;
516 tmp = line;
518 if (*tmp == '"') {
519 tmp++;
520 line++;
521 while (*tmp != 0 && *tmp != '"')
522 tmp++;
523 if (*tmp != '"') {
524 wwarning(_("%s: unmatched '\"' in menu file"), line);
525 return NULL;
527 } else {
528 do {
529 if (*tmp == '\\')
530 tmp++;
532 if (*tmp != 0)
533 tmp++;
535 } while (*tmp != 0 && *tmp != ' ' && *tmp != '\t');
538 c = *tmp;
539 *tmp = 0;
540 ret = wstrdup(line);
541 *tmp = c;
543 if (c == 0)
544 return ret;
545 else
546 tmp++;
548 /* skip blanks */
549 while (*tmp == ' ' || *tmp == '\t')
550 tmp++;
552 if (*tmp != 0)
553 *next = tmp;
555 return ret;
558 static void separateCommand(char *line, char ***file, char **command)
560 char *token, *tmp = line;
561 WMArray *array = WMCreateArray(4);
562 int count, i;
564 *file = NULL;
565 *command = NULL;
566 do {
567 token = next_token(tmp, &tmp);
568 if (token) {
569 if (strcmp(token, "WITH") == 0) {
570 if (tmp != NULL && *tmp != 0)
571 *command = wstrdup(tmp);
572 else
573 wwarning(_("%s: missing command"), line);
574 break;
576 WMAddToArray(array, token);
578 } while (token != NULL && tmp != NULL);
580 count = WMGetArrayItemCount(array);
581 if (count > 0) {
582 *file = wmalloc(sizeof(char *) * (count + 1));
583 (*file)[count] = NULL;
584 for (i = 0; i < count; i++) {
585 (*file)[i] = WMGetFromArray(array, i);
588 WMFreeArray(array);
591 static WMenu *constructPLMenu(WScreen *screen, const char *path)
593 WMPropList *pl = NULL;
594 WMenu *menu = NULL;
596 if (!path)
597 return NULL;
599 pl = WMReadPropListFromFile(path);
600 if (!pl)
601 return NULL;
603 menu = configureMenu(screen, pl, False);
605 WMReleasePropList(pl);
607 if (!menu)
608 return NULL;
610 menu->on_destroy = removeShortcutsForMenu;
611 return menu;
616 static void constructMenu(WMenu * menu, WMenuEntry * entry)
618 WMenu *submenu;
619 struct stat stat_buf;
620 char **path;
621 char *cmd;
622 char *lpath = NULL;
623 int i, first = -1;
624 time_t last = 0;
626 separateCommand((char *)entry->clientdata, &path, &cmd);
627 if (path == NULL || *path == NULL || **path == 0) {
628 wwarning(_("invalid OPEN_MENU specification: %s"), (char *)entry->clientdata);
629 if (cmd)
630 wfree(cmd);
631 return;
634 if (path[0][0] == '|') {
635 /* pipe menu */
637 if (!menu->cascades[entry->cascade] || menu->cascades[entry->cascade]->timestamp == 0) {
638 /* parse pipe */
640 submenu = readMenuPipe(menu->frame->screen_ptr, path);
642 if (submenu != NULL) {
643 if (path[0][1] == '|')
644 submenu->timestamp = 0;
645 else
646 submenu->timestamp = 1; /* there's no automatic reloading */
648 } else {
649 submenu = NULL;
652 } else {
654 /* try interpreting path as a proplist file */
655 submenu = constructPLMenu(menu->frame->screen_ptr, path[0]);
656 /* if unsuccessful, try it as an old-style file */
657 if (!submenu) {
659 i = 0;
660 while (path[i] != NULL) {
661 char *tmp;
663 if (strcmp(path[i], "-noext") == 0) {
664 i++;
665 continue;
668 tmp = wexpandpath(path[i]);
669 wfree(path[i]);
670 lpath = getLocalizedMenuFile(tmp);
671 if (lpath) {
672 wfree(tmp);
673 path[i] = lpath;
674 lpath = NULL;
675 } else {
676 path[i] = tmp;
679 if (stat(path[i], &stat_buf) == 0) {
680 if (last < stat_buf.st_mtime)
681 last = stat_buf.st_mtime;
682 if (first < 0)
683 first = i;
684 } else {
685 werror(_("%s:could not stat menu"), path[i]);
686 /*goto finish; */
689 i++;
692 if (first < 0) {
693 werror(_("%s:could not stat menu:%s"), "OPEN_MENU", (char *)entry->clientdata);
694 goto finish;
696 stat(path[first], &stat_buf);
697 if (!menu->cascades[entry->cascade]
698 || menu->cascades[entry->cascade]->timestamp < last) {
700 if (S_ISDIR(stat_buf.st_mode)) {
701 /* menu directory */
702 submenu = readMenuDirectory(menu->frame->screen_ptr, entry->text, path, cmd);
703 if (submenu)
704 submenu->timestamp = last;
705 } else if (S_ISREG(stat_buf.st_mode)) {
706 /* menu file */
708 if (cmd || path[1])
709 wwarning(_("too many parameters in OPEN_MENU: %s"),
710 (char *)entry->clientdata);
712 submenu = readMenuFile(menu->frame->screen_ptr, path[first]);
713 if (submenu)
714 submenu->timestamp = stat_buf.st_mtime;
715 } else {
716 submenu = NULL;
718 } else {
719 submenu = NULL;
724 if (submenu) {
725 wMenuEntryRemoveCascade(menu, entry);
726 wMenuEntrySetCascade(menu, entry, submenu);
729 finish:
730 i = 0;
731 while (path[i] != NULL)
732 wfree(path[i++]);
733 wfree(path);
734 if (cmd)
735 wfree(cmd);
738 static void constructPLMenuFromPipe(WMenu * menu, WMenuEntry * entry)
740 WMenu *submenu = NULL;
741 char **path;
742 char *cmd;
743 int i;
745 separateCommand((char *)entry->clientdata, &path, &cmd);
746 if (path == NULL || *path == NULL || **path == 0) {
747 wwarning(_("invalid OPEN_PLMENU specification: %s"),
748 (char *)entry->clientdata);
749 if (cmd)
750 wfree(cmd);
751 return;
754 if (path[0][0] == '|') {
755 /* pipe menu */
757 if (!menu->cascades[entry->cascade]
758 || menu->cascades[entry->cascade]->timestamp == 0) {
759 /* parse pipe */
760 submenu = readPLMenuPipe(menu->frame->screen_ptr, path);
762 if (submenu != NULL) {
763 if (path[0][1] == '|')
764 submenu->timestamp = 0;
765 else
766 submenu->timestamp = 1; /* there's no automatic reloading */
771 if (submenu) {
772 wMenuEntryRemoveCascade(menu, entry);
773 wMenuEntrySetCascade(menu, entry, submenu);
776 i = 0;
777 while (path[i] != NULL)
778 wfree(path[i++]);
780 wfree(path);
781 if (cmd)
782 wfree(cmd);
785 static void cleanupWorkspaceMenu(WMenu *menu)
787 if (w_global.workspace.menu == menu)
788 w_global.workspace.menu = NULL;
791 static WMenuEntry *addWorkspaceMenu(WScreen *scr, WMenu *menu, const char *title)
793 WMenu *wsmenu;
794 WMenuEntry *entry;
796 if (scr->flags.added_workspace_menu) {
797 wwarning(_
798 ("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
799 return NULL;
800 } else {
801 scr->flags.added_workspace_menu = 1;
803 wsmenu = wWorkspaceMenuMake(scr, True);
804 wsmenu->on_destroy = cleanupWorkspaceMenu;
806 w_global.workspace.menu = wsmenu;
807 entry = wMenuAddCallback(menu, title, NULL, NULL);
808 wMenuEntrySetCascade(menu, entry, wsmenu);
810 wWorkspaceMenuUpdate(wsmenu);
812 return entry;
815 static void cleanupWindowsMenu(WMenu * menu)
817 if (menu->frame->screen_ptr->switch_menu == menu)
818 menu->frame->screen_ptr->switch_menu = NULL;
821 static WMenuEntry *addWindowsMenu(WScreen *scr, WMenu *menu, const char *title)
823 WMenu *wwmenu;
824 WWindow *wwin;
825 WMenuEntry *entry;
827 if (scr->flags.added_windows_menu) {
828 wwarning(_
829 ("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
830 return NULL;
831 } else {
832 scr->flags.added_windows_menu = 1;
834 wwmenu = wMenuCreate(scr, _("Window List"), False);
835 wwmenu->on_destroy = cleanupWindowsMenu;
836 scr->switch_menu = wwmenu;
837 wwin = scr->focused_window;
838 while (wwin) {
839 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
841 wwin = wwin->prev;
843 entry = wMenuAddCallback(menu, title, NULL, NULL);
844 wMenuEntrySetCascade(menu, entry, wwmenu);
846 return entry;
849 static WMenuEntry *addMenuEntry(WMenu *menu, const char *title, const char *shortcut, const char *command,
850 const char *params, const char *file_name)
852 WScreen *scr;
853 WMenuEntry *entry = NULL;
854 Bool shortcutOk = False;
856 if (!menu)
857 return NULL;
858 scr = menu->frame->screen_ptr;
859 if (strcmp(command, "OPEN_MENU") == 0) {
860 if (!params) {
861 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name, command);
862 } else {
863 WMenu *dummy;
864 char *path;
866 path = wfindfile(DEF_CONFIG_PATHS, params);
867 if (!path) {
868 path = wstrdup(params);
870 dummy = wMenuCreate(scr, title, False);
871 dummy->on_destroy = removeShortcutsForMenu;
872 entry = wMenuAddCallback(menu, title, constructMenu, path);
873 entry->free_cdata = wfree;
874 wMenuEntrySetCascade(menu, entry, dummy);
876 } else if (strcmp(command, "OPEN_PLMENU") == 0) {
877 if (!params) {
878 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name, command);
879 } else {
880 WMenu *dummy;
881 char *path;
883 path = wfindfile(DEF_CONFIG_PATHS, params);
884 if (!path)
885 path = wstrdup(params);
887 dummy = wMenuCreate(scr, title, False);
888 dummy->on_destroy = removeShortcutsForMenu;
889 entry = wMenuAddCallback(menu, title, constructPLMenuFromPipe, path);
890 entry->free_cdata = wfree;
891 wMenuEntrySetCascade(menu, entry, dummy);
893 } else if (strcmp(command, "EXEC") == 0) {
894 if (!params)
895 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name, command);
896 else {
897 entry = wMenuAddCallback(menu, title, execCommand, wstrconcat("exec ", params));
898 entry->free_cdata = wfree;
899 shortcutOk = True;
901 } else if (strcmp(command, "SHEXEC") == 0) {
902 if (!params)
903 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name, command);
904 else {
905 entry = wMenuAddCallback(menu, title, execCommand, wstrdup(params));
906 entry->free_cdata = wfree;
907 shortcutOk = True;
909 } else if (strcmp(command, "EXIT") == 0) {
911 if (params && strcmp(params, "QUICK") == 0)
912 entry = wMenuAddCallback(menu, title, exitCommand, (void *)M_QUICK);
913 else
914 entry = wMenuAddCallback(menu, title, exitCommand, NULL);
916 shortcutOk = True;
917 } else if (strcmp(command, "SHUTDOWN") == 0) {
919 if (params && strcmp(params, "QUICK") == 0)
920 entry = wMenuAddCallback(menu, title, shutdownCommand, (void *)M_QUICK);
921 else
922 entry = wMenuAddCallback(menu, title, shutdownCommand, NULL);
924 shortcutOk = True;
925 } else if (strcmp(command, "REFRESH") == 0) {
926 entry = wMenuAddCallback(menu, title, refreshCommand, NULL);
928 shortcutOk = True;
929 } else if (strcmp(command, "WORKSPACE_MENU") == 0) {
930 entry = addWorkspaceMenu(scr, menu, title);
932 shortcutOk = True;
933 } else if (strcmp(command, "WINDOWS_MENU") == 0) {
934 entry = addWindowsMenu(scr, menu, title);
936 shortcutOk = True;
937 } else if (strcmp(command, "ARRANGE_ICONS") == 0) {
938 entry = wMenuAddCallback(menu, title, arrangeIconsCommand, NULL);
940 shortcutOk = True;
941 } else if (strcmp(command, "HIDE_OTHERS") == 0) {
942 entry = wMenuAddCallback(menu, title, hideOthersCommand, NULL);
944 shortcutOk = True;
945 } else if (strcmp(command, "SHOW_ALL") == 0) {
946 entry = wMenuAddCallback(menu, title, showAllCommand, NULL);
948 shortcutOk = True;
949 } else if (strcmp(command, "RESTART") == 0) {
950 entry = wMenuAddCallback(menu, title, restartCommand, params ? wstrdup(params) : NULL);
951 entry->free_cdata = wfree;
952 shortcutOk = True;
953 } else if (strcmp(command, "SAVE_SESSION") == 0) {
954 entry = wMenuAddCallback(menu, title, saveSessionCommand, NULL);
956 shortcutOk = True;
957 } else if (strcmp(command, "CLEAR_SESSION") == 0) {
958 entry = wMenuAddCallback(menu, title, clearSessionCommand, NULL);
959 shortcutOk = True;
960 } else if (strcmp(command, "INFO_PANEL") == 0) {
961 entry = wMenuAddCallback(menu, title, infoPanelCommand, NULL);
962 shortcutOk = True;
963 } else if (strcmp(command, "LEGAL_PANEL") == 0) {
964 entry = wMenuAddCallback(menu, title, legalPanelCommand, NULL);
965 shortcutOk = True;
966 } else {
967 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name, command);
969 return NULL;
972 if (shortcut && entry) {
973 if (!shortcutOk) {
974 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name, title);
975 } else {
976 if (addShortcut(file_name, shortcut, menu, entry)) {
978 entry->rtext = GetShortcutString(shortcut);
980 entry->rtext = wstrdup(shortcut);
986 return entry;
989 /******************* Menu Configuration From File *******************/
991 static void freeline(char *title, char *command, char *parameter, char *shortcut)
993 wfree(title);
994 wfree(command);
995 wfree(parameter);
996 wfree(shortcut);
999 static WMenu *parseCascade(WScreen * scr, WMenu * menu, WMenuParser parser)
1001 char *command, *params, *shortcut, *title;
1003 while (WMenuParserGetLine(parser, &title, &command, &params, &shortcut)) {
1005 if (command == NULL || !command[0]) {
1006 WMenuParserError(parser, _("missing command in menu config") );
1007 freeline(title, command, params, shortcut);
1008 goto error;
1011 if (strcasecmp(command, "MENU") == 0) {
1012 WMenu *cascade;
1014 /* start submenu */
1016 cascade = wMenuCreate(scr, M_(title), False);
1017 cascade->on_destroy = removeShortcutsForMenu;
1018 if (!parseCascade(scr, cascade, parser)) {
1019 wMenuDestroy(cascade, True);
1020 } else {
1021 wMenuEntrySetCascade(menu, wMenuAddCallback(menu, M_(title), NULL, NULL), cascade);
1023 } else if (strcasecmp(command, "END") == 0) {
1024 /* end of menu */
1025 freeline(title, command, params, shortcut);
1026 return menu;
1027 } else {
1028 /* normal items */
1029 addMenuEntry(menu, M_(title), shortcut, command, params, WMenuParserGetFilename(parser));
1031 freeline(title, command, params, shortcut);
1034 WMenuParserError(parser, _("syntax error in menu file: END declaration missing") );
1036 error:
1037 return NULL;
1040 static WMenu *readMenuFile(WScreen *scr, const char *file_name)
1042 WMenu *menu = NULL;
1043 FILE *file = NULL;
1044 WMenuParser parser;
1045 char *command, *params, *shortcut, *title;
1047 file = fopen(file_name, "rb");
1048 if (!file) {
1049 werror(_("%s:could not open menu file"), file_name);
1050 return NULL;
1052 parser = WMenuParserCreate(file_name, file, DEF_CONFIG_PATHS);
1053 menu_parser_register_macros(parser);
1055 while (WMenuParserGetLine(parser, &title, &command, &params, &shortcut)) {
1057 if (command == NULL || !command[0]) {
1058 WMenuParserError(parser, _("missing command in menu config") );
1059 freeline(title, command, params, shortcut);
1060 break;
1062 if (strcasecmp(command, "MENU") == 0) {
1063 menu = wMenuCreate(scr, M_(title), True);
1064 menu->on_destroy = removeShortcutsForMenu;
1065 if (!parseCascade(scr, menu, parser)) {
1066 wMenuDestroy(menu, True);
1067 menu = NULL;
1069 freeline(title, command, params, shortcut);
1070 break;
1071 } else {
1072 WMenuParserError(parser, _("invalid menu file, MENU command is missing") );
1073 freeline(title, command, params, shortcut);
1074 break;
1076 freeline(title, command, params, shortcut);
1079 WMenuParserDelete(parser);
1080 fclose(file);
1082 return menu;
1085 /************ Menu Configuration From Pipe *************/
1086 static WMenu *readPLMenuPipe(WScreen * scr, char **file_name)
1088 WMPropList *plist = NULL;
1089 WMenu *menu = NULL;
1090 char *filename;
1091 char flat_file[MAXLINE];
1092 int i;
1094 flat_file[0] = '\0';
1096 for (i = 0; file_name[i] != NULL; i++) {
1097 strcat(flat_file, file_name[i]);
1098 strcat(flat_file, " ");
1100 filename = flat_file + (flat_file[1] == '|' ? 2 : 1);
1102 plist = WMReadPropListFromPipe(filename);
1104 if (!plist)
1105 return NULL;
1107 menu = configureMenu(scr, plist, False);
1109 WMReleasePropList(plist);
1111 if (!menu)
1112 return NULL;
1114 menu->on_destroy = removeShortcutsForMenu;
1115 return menu;
1118 static WMenu *readMenuPipe(WScreen * scr, char **file_name)
1120 WMenu *menu = NULL;
1121 FILE *file = NULL;
1122 WMenuParser parser;
1123 char *command, *params, *shortcut, *title;
1124 char *filename;
1125 char flat_file[MAXLINE];
1126 int i;
1128 flat_file[0] = '\0';
1130 for (i = 0; file_name[i] != NULL; i++) {
1131 strcat(flat_file, file_name[i]);
1132 strcat(flat_file, " ");
1134 filename = flat_file + (flat_file[1] == '|' ? 2 : 1);
1136 file = popen(filename, "r");
1137 if (!file) {
1138 werror(_("%s:could not open menu file"), filename);
1139 return NULL;
1141 parser = WMenuParserCreate(flat_file, file, DEF_CONFIG_PATHS);
1142 menu_parser_register_macros(parser);
1144 while (WMenuParserGetLine(parser, &title, &command, &params, &shortcut)) {
1146 if (command == NULL || !command[0]) {
1147 WMenuParserError(parser, _("missing command in menu config") );
1148 freeline(title, command, params, shortcut);
1149 break;
1151 if (strcasecmp(command, "MENU") == 0) {
1152 menu = wMenuCreate(scr, M_(title), True);
1153 menu->on_destroy = removeShortcutsForMenu;
1154 if (!parseCascade(scr, menu, parser)) {
1155 wMenuDestroy(menu, True);
1156 menu = NULL;
1158 freeline(title, command, params, shortcut);
1159 break;
1160 } else {
1161 WMenuParserError(parser, _("no title given for the root menu") );
1162 freeline(title, command, params, shortcut);
1163 break;
1166 freeline(title, command, params, shortcut);
1169 WMenuParserDelete(parser);
1170 pclose(file);
1172 return menu;
1175 typedef struct {
1176 char *name;
1177 int index;
1178 } dir_data;
1180 static int myCompare(const void *d1, const void *d2)
1182 dir_data *p1 = *(dir_data **) d1;
1183 dir_data *p2 = *(dir_data **) d2;
1185 return strcmp(p1->name, p2->name);
1188 /***** Preset some macro for file parser *****/
1189 static void menu_parser_register_macros(WMenuParser parser)
1191 Visual *visual;
1192 char buf[32];
1194 // Used to return CPP verion, now returns wmaker's version
1195 WMenuParserRegisterSimpleMacro(parser, "__VERSION__", VERSION);
1197 // All macros below were historically defined by WindowMaker
1198 visual = DefaultVisual(dpy, DefaultScreen(dpy));
1199 snprintf(buf, sizeof(buf), "%d", visual->class);
1200 WMenuParserRegisterSimpleMacro(parser, "VISUAL", buf);
1202 snprintf(buf, sizeof(buf), "%d", DefaultDepth(dpy, DefaultScreen(dpy)) );
1203 WMenuParserRegisterSimpleMacro(parser, "DEPTH", buf);
1205 snprintf(buf, sizeof(buf), "%d", WidthOfScreen(DefaultScreenOfDisplay(dpy)) );
1206 WMenuParserRegisterSimpleMacro(parser, "SCR_WIDTH", buf);
1208 snprintf(buf, sizeof(buf), "%d", HeightOfScreen(DefaultScreenOfDisplay(dpy)) );
1209 WMenuParserRegisterSimpleMacro(parser, "SCR_HEIGHT", buf);
1211 WMenuParserRegisterSimpleMacro(parser, "DISPLAY", XDisplayName(DisplayString(dpy)) );
1213 WMenuParserRegisterSimpleMacro(parser, "WM_VERSION", "\"" VERSION "\"");
1216 /************ Menu Configuration From Directory *************/
1218 static Bool isFilePackage(const char *file)
1220 int l;
1222 /* check if the extension indicates this file is a
1223 * file package. For now, only recognize .themed */
1225 l = strlen(file);
1227 if (l > 7 && strcmp(&(file[l - 7]), ".themed") == 0) {
1228 return True;
1229 } else {
1230 return False;
1234 static WMenu *readMenuDirectory(WScreen *scr, const char *title, char **path, const char *command)
1236 DIR *dir;
1237 struct dirent *dentry;
1238 struct stat stat_buf;
1239 WMenu *menu = NULL;
1240 char *buffer;
1241 WMArray *dirs = NULL, *files = NULL;
1242 WMArrayIterator iter;
1243 int length, i, have_space = 0;
1244 dir_data *data;
1245 int stripExtension = 0;
1247 dirs = WMCreateArray(16);
1248 files = WMCreateArray(16);
1250 i = 0;
1251 while (path[i] != NULL) {
1252 if (strcmp(path[i], "-noext") == 0) {
1253 stripExtension = 1;
1254 i++;
1255 continue;
1258 dir = opendir(path[i]);
1259 if (!dir) {
1260 i++;
1261 continue;
1264 while ((dentry = readdir(dir))) {
1266 if (strcmp(dentry->d_name, ".") == 0 || strcmp(dentry->d_name, "..") == 0)
1267 continue;
1269 if (dentry->d_name[0] == '.')
1270 continue;
1272 buffer = malloc(strlen(path[i]) + strlen(dentry->d_name) + 4);
1273 if (!buffer) {
1274 werror(_("out of memory while constructing directory menu %s"), path[i]);
1275 break;
1278 strcpy(buffer, path[i]);
1279 strcat(buffer, "/");
1280 strcat(buffer, dentry->d_name);
1282 if (stat(buffer, &stat_buf) != 0) {
1283 werror(_("%s:could not stat file \"%s\" in menu directory"),
1284 path[i], dentry->d_name);
1285 } else {
1286 Bool isFilePack = False;
1288 data = NULL;
1289 if (S_ISDIR(stat_buf.st_mode)
1290 && !(isFilePack = isFilePackage(dentry->d_name))) {
1292 /* access always returns success for user root */
1293 if (access(buffer, X_OK) == 0) {
1294 /* Directory is accesible. Add to directory list */
1296 data = (dir_data *) wmalloc(sizeof(dir_data));
1297 data->name = wstrdup(dentry->d_name);
1298 data->index = i;
1300 WMAddToArray(dirs, data);
1302 } else if (S_ISREG(stat_buf.st_mode) || isFilePack) {
1303 /* Hack because access always returns X_OK success for user root */
1304 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1305 if ((command != NULL && access(buffer, R_OK) == 0) ||
1306 (command == NULL && access(buffer, X_OK) == 0 &&
1307 (stat_buf.st_mode & S_IXANY))) {
1309 data = (dir_data *) wmalloc(sizeof(dir_data));
1310 data->name = wstrdup(dentry->d_name);
1311 data->index = i;
1313 WMAddToArray(files, data);
1317 free(buffer);
1320 closedir(dir);
1321 i++;
1324 if (!WMGetArrayItemCount(dirs) && !WMGetArrayItemCount(files)) {
1325 WMFreeArray(dirs);
1326 WMFreeArray(files);
1327 return NULL;
1330 WMSortArray(dirs, myCompare);
1331 WMSortArray(files, myCompare);
1333 menu = wMenuCreate(scr, M_(title), False);
1334 menu->on_destroy = removeShortcutsForMenu;
1336 WM_ITERATE_ARRAY(dirs, data, iter) {
1337 /* New directory. Use same OPEN_MENU command that was used
1338 * for the current directory. */
1339 length = strlen(path[data->index]) + strlen(data->name) + 6;
1340 if (stripExtension)
1341 length += 7;
1342 if (command)
1343 length += strlen(command) + 6;
1344 buffer = malloc(length);
1345 if (!buffer) {
1346 werror(_("out of memory while constructing directory menu %s"), path[data->index]);
1347 break;
1350 buffer[0] = '\0';
1351 if (stripExtension)
1352 strcat(buffer, "-noext ");
1354 have_space = strchr(path[data->index], ' ') != NULL || strchr(data->name, ' ') != NULL;
1356 if (have_space)
1357 strcat(buffer, "\"");
1358 strcat(buffer, path[data->index]);
1360 strcat(buffer, "/");
1361 strcat(buffer, data->name);
1362 if (have_space)
1363 strcat(buffer, "\"");
1364 if (command) {
1365 strcat(buffer, " WITH ");
1366 strcat(buffer, command);
1369 addMenuEntry(menu, M_(data->name), NULL, "OPEN_MENU", buffer, path[data->index]);
1371 wfree(buffer);
1372 if (data->name)
1373 wfree(data->name);
1374 wfree(data);
1377 WM_ITERATE_ARRAY(files, data, iter) {
1378 /* executable: add as entry */
1379 length = strlen(path[data->index]) + strlen(data->name) + 6;
1380 if (command)
1381 length += strlen(command);
1383 buffer = malloc(length);
1384 if (!buffer) {
1385 werror(_("out of memory while constructing directory menu %s"), path[data->index]);
1386 break;
1389 have_space = strchr(path[data->index], ' ') != NULL || strchr(data->name, ' ') != NULL;
1390 if (command != NULL) {
1391 strcpy(buffer, command);
1392 strcat(buffer, " ");
1393 if (have_space)
1394 strcat(buffer, "\"");
1395 strcat(buffer, path[data->index]);
1396 } else {
1397 if (have_space) {
1398 buffer[0] = '"';
1399 buffer[1] = 0;
1400 strcat(buffer, path[data->index]);
1401 } else {
1402 strcpy(buffer, path[data->index]);
1405 strcat(buffer, "/");
1406 strcat(buffer, data->name);
1407 if (have_space)
1408 strcat(buffer, "\"");
1410 if (stripExtension) {
1411 char *ptr = strrchr(data->name, '.');
1412 if (ptr && ptr != data->name)
1413 *ptr = 0;
1415 addMenuEntry(menu, M_(data->name), NULL, "SHEXEC", buffer, path[data->index]);
1417 wfree(buffer);
1418 if (data->name)
1419 wfree(data->name);
1420 wfree(data);
1423 WMFreeArray(files);
1424 WMFreeArray(dirs);
1426 return menu;
1429 /************ Menu Configuration From WMRootMenu *************/
1431 static WMenu *makeDefaultMenu(WScreen * scr)
1433 WMenu *menu = NULL;
1435 menu = wMenuCreate(scr, _("Commands"), True);
1436 wMenuAddCallback(menu, M_("XTerm"), execCommand, "xterm");
1437 wMenuAddCallback(menu, M_("rxvt"), execCommand, "rxvt");
1438 wMenuAddCallback(menu, _("Restart"), restartCommand, NULL);
1439 wMenuAddCallback(menu, _("Exit..."), exitCommand, NULL);
1440 return menu;
1444 *----------------------------------------------------------------------
1445 * configureMenu--
1446 * Reads root menu configuration from defaults database.
1448 *----------------------------------------------------------------------
1450 static WMenu *configureMenu(WScreen * scr, WMPropList * definition, Bool includeGlobals)
1452 WMenu *menu = NULL;
1453 WMPropList *elem;
1454 int i, count;
1455 WMPropList *title, *command, *params;
1456 char *tmp, *mtitle;
1458 if (WMIsPLString(definition)) {
1459 struct stat stat_buf;
1460 char *path = NULL;
1461 Bool menu_is_default = False;
1463 /* menu definition is a string. Probably a path, so parse the file */
1465 tmp = wexpandpath(WMGetFromPLString(definition));
1467 path = getLocalizedMenuFile(tmp);
1469 if (!path)
1470 path = wfindfile(DEF_CONFIG_PATHS, tmp);
1472 if (!path) {
1473 path = wfindfile(DEF_CONFIG_PATHS, DEF_MENU_FILE);
1474 menu_is_default = True;
1477 if (!path) {
1478 werror(_("could not find menu file \"%s\" referenced in WMRootMenu"), tmp);
1479 wfree(tmp);
1480 return NULL;
1483 if (stat(path, &stat_buf) < 0) {
1484 werror(_("could not access menu \"%s\" referenced in WMRootMenu"), path);
1485 wfree(path);
1486 wfree(tmp);
1487 return NULL;
1490 if (!scr->root_menu || stat_buf.st_mtime > scr->root_menu->timestamp
1491 /* if the pointer in WMRootMenu has changed */
1492 || w_global.domain.root_menu->timestamp > scr->root_menu->timestamp) {
1494 if (menu_is_default) {
1495 wwarning(_
1496 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1497 path);
1500 menu = readMenuFile(scr, path);
1501 if (menu)
1502 menu->timestamp = WMAX(stat_buf.st_mtime, w_global.domain.root_menu->timestamp);
1503 } else {
1504 menu = NULL;
1506 wfree(path);
1507 wfree(tmp);
1509 return menu;
1512 count = WMGetPropListItemCount(definition);
1513 if (count == 0)
1514 return NULL;
1516 elem = WMGetFromPLArray(definition, 0);
1517 if (!WMIsPLString(elem)) {
1518 tmp = WMGetPropListDescription(elem, False);
1519 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp);
1520 wfree(tmp);
1521 return NULL;
1523 mtitle = WMGetFromPLString(elem);
1525 menu = wMenuCreate(scr, M_(mtitle), False);
1526 menu->on_destroy = removeShortcutsForMenu;
1528 #ifdef GLOBAL_SUBMENU_FILE
1529 if (includeGlobals) {
1530 WMenu *submenu;
1531 WMenuEntry *mentry;
1533 submenu = readMenuFile(scr, GLOBAL_SUBMENU_FILE);
1535 if (submenu) {
1536 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL, NULL);
1537 wMenuEntrySetCascade(menu, mentry, submenu);
1540 #else
1541 /* Parameter not used, but tell the compiler that it is ok */
1542 (void) includeGlobals;
1543 #endif
1545 for (i = 1; i < count; i++) {
1546 elem = WMGetFromPLArray(definition, i);
1547 #if 0
1548 if (WMIsPLString(elem)) {
1549 char *file;
1551 file = WMGetFromPLString(elem);
1554 #endif
1555 if (!WMIsPLArray(elem) || WMGetPropListItemCount(elem) < 2)
1556 goto error;
1558 if (WMIsPLArray(WMGetFromPLArray(elem, 1))) {
1559 WMenu *submenu;
1560 WMenuEntry *mentry;
1562 /* submenu */
1563 submenu = configureMenu(scr, elem, True);
1564 if (submenu) {
1565 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL, NULL);
1566 wMenuEntrySetCascade(menu, mentry, submenu);
1568 } else {
1569 int idx = 0;
1570 WMPropList *shortcut;
1571 /* normal entry */
1573 title = WMGetFromPLArray(elem, idx++);
1574 shortcut = WMGetFromPLArray(elem, idx++);
1575 if (strcmp(WMGetFromPLString(shortcut), "SHORTCUT") == 0) {
1576 shortcut = WMGetFromPLArray(elem, idx++);
1577 command = WMGetFromPLArray(elem, idx++);
1578 } else {
1579 command = shortcut;
1580 shortcut = NULL;
1582 params = WMGetFromPLArray(elem, idx++);
1584 if (!title || !command)
1585 goto error;
1587 addMenuEntry(menu, M_(WMGetFromPLString(title)),
1588 shortcut ? WMGetFromPLString(shortcut) : NULL,
1589 WMGetFromPLString(command),
1590 params ? WMGetFromPLString(params) : NULL, "WMRootMenu");
1592 continue;
1594 error:
1595 tmp = WMGetPropListDescription(elem, False);
1596 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp);
1597 wfree(tmp);
1600 return menu;
1604 *----------------------------------------------------------------------
1605 * OpenRootMenu--
1606 * Opens the root menu, parsing the menu configuration from the
1607 * defaults database.
1608 * If the menu is already mapped and is not sticked to the
1609 * root window, it will be unmapped.
1611 * Side effects:
1612 * The menu may be remade.
1614 * Notes:
1615 * Construction of OPEN_MENU entries are delayed to the moment the
1616 * user map's them.
1617 *----------------------------------------------------------------------
1619 void OpenRootMenu(WScreen * scr, int x, int y, int keyboard)
1621 WMenu *menu = NULL;
1622 WMPropList *definition;
1624 static WMPropList *domain=NULL;
1626 if (!domain) {
1627 domain = WMCreatePLString("WMRootMenu");
1631 scr->flags.root_menu_changed_shortcuts = 0;
1632 scr->flags.added_workspace_menu = 0;
1633 scr->flags.added_windows_menu = 0;
1635 if (scr->root_menu && scr->root_menu->flags.mapped) {
1636 menu = scr->root_menu;
1637 if (!menu->flags.buttoned) {
1638 wMenuUnmap(menu);
1639 } else {
1640 wRaiseFrame(menu->frame->core);
1642 if (keyboard)
1643 wMenuMapAt(menu, 0, 0, True);
1644 else
1645 wMenuMapCopyAt(menu, x - menu->frame->core->width / 2, y);
1647 return;
1650 definition = w_global.domain.root_menu->dictionary;
1653 definition = PLGetDomain(domain);
1655 if (definition) {
1656 if (WMIsPLArray(definition)) {
1657 if (!scr->root_menu || w_global.domain.root_menu->timestamp > scr->root_menu->timestamp) {
1658 menu = configureMenu(scr, definition, True);
1659 if (menu)
1660 menu->timestamp = w_global.domain.root_menu->timestamp;
1662 } else
1663 menu = NULL;
1664 } else {
1665 menu = configureMenu(scr, definition, True);
1669 if (!menu) {
1670 /* menu hasn't changed or could not be read */
1671 if (!scr->root_menu) {
1672 wMessageDialog(scr, _("Error"),
1673 _("The applications menu could not be loaded. "
1674 "Look at the console output for a detailed "
1675 "description of the errors."), _("OK"), NULL, NULL);
1677 menu = makeDefaultMenu(scr);
1678 scr->root_menu = menu;
1680 menu = scr->root_menu;
1681 } else {
1682 /* new root menu */
1683 if (scr->root_menu) {
1684 wMenuDestroy(scr->root_menu, True);
1686 scr->root_menu = menu;
1688 if (menu) {
1689 int newx, newy;
1691 if (keyboard && x == 0 && y == 0) {
1692 newx = newy = 0;
1693 } else if (keyboard && x == scr->scr_width / 2 && y == scr->scr_height / 2) {
1694 newx = x - menu->frame->core->width / 2;
1695 newy = y - menu->frame->core->height / 2;
1696 } else {
1697 newx = x - menu->frame->core->width / 2;
1698 newy = y;
1700 wMenuMapAt(menu, newx, newy, keyboard);
1703 if (scr->flags.root_menu_changed_shortcuts)
1704 rebindKeygrabs(scr);