Revert "Inotify: Reload keyboard shortcut definitions when configuration changes"
[wmaker-crm.git] / src / rootmenu.c
blobcca29aeb3c7cf09c293f5732abfabad267a0cd40
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 "funcs.h"
45 #include "dialog.h"
46 #include "keybind.h"
47 #include "stacking.h"
48 #include "workspace.h"
49 #include "defaults.h"
50 #include "framewin.h"
51 #include "session.h"
52 #include "xmodifier.h"
54 #include <WINGs/WUtil.h>
56 #define MAX_SHORTCUT_LENGTH 32
58 extern char *Locale;
59 extern WDDomain *WDRootMenu;
60 extern Cursor wCursor[WCUR_LAST];
61 extern WPreferences wPreferences;
63 static WMenu *readMenuPipe(WScreen * scr, char **file_name);
64 static WMenu *readMenuFile(WScreen * scr, char *file_name);
65 static WMenu *readMenuDirectory(WScreen * scr, char *title, char **file_name, char *command);
66 static WMenu *configureMenu(WScreen * scr, WMPropList * definition, Bool includeGlobals);
68 typedef struct Shortcut {
69 struct Shortcut *next;
71 int modifier;
72 KeyCode keycode;
73 WMenuEntry *entry;
74 WMenu *menu;
75 } Shortcut;
77 static Shortcut *shortcutList = NULL;
80 * Syntax:
81 * # main menu
82 * "Menu Name" MENU
83 * "Title" EXEC command_to_exec -params
84 * "Submenu" MENU
85 * "Title" EXEC command_to_exec -params
86 * "Submenu" END
87 * "Workspaces" WORKSPACE_MENU
88 * "Title" built_in_command
89 * "Quit" EXIT
90 * "Quick Quit" EXIT QUICK
91 * "Menu Name" END
93 * Commands may be preceded by SHORTCUT key
95 * Built-in commands:
97 * INFO_PANEL - shows the Info Panel
98 * LEGAL_PANEL - shows the Legal info panel
99 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
100 * REFRESH - forces the desktop to be repainted
101 * EXIT [QUICK] - exit the window manager [without confirmation]
102 * EXEC <program> - execute an external program
103 * SHEXEC <command> - execute a shell command
104 * WORKSPACE_MENU - places the workspace submenu
105 * ARRANGE_ICONS
106 * RESTART [<window manager>] - restarts the window manager
107 * SHOW_ALL - unhide all windows on workspace
108 * HIDE_OTHERS - hides all windows excep the focused one
109 * OPEN_MENU file - read menu data from file which must be a valid menu file.
110 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
111 * - read menu data from directory(ies) and
112 * eventually precede each with a command.
113 * OPEN_MENU | command
114 * - opens command and uses its stdout to construct and insert
115 * the resulting menu in current position. The output of
116 * command must be a valid menu description.
117 * The space between '|' and command is optional.
118 * || will do the same, but will not cache the contents.
119 * SAVE_SESSION - saves the current state of the desktop, which include
120 * all running applications, all their hints (geometry,
121 * position on screen, workspace they live on, the dock
122 * or clip from where they were launched, and
123 * if minimized, shaded or hidden. Also saves the current
124 * workspace the user is on. All will be restored on every
125 * start of windowmaker until another SAVE_SESSION or
126 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
127 * WindowMaker domain file, then saving is automatically
128 * done on every windowmaker exit, overwriting any
129 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
130 * dock state now.
131 * CLEAR_SESSION - clears any previous saved session. This will not have
132 * any effect if SaveSessionOnExit is True.
136 #define M_QUICK 1
138 /* menu commands */
140 static void execCommand(WMenu * menu, WMenuEntry * entry)
142 char *cmdline;
144 cmdline = ExpandOptions(menu->frame->screen_ptr, (char *)entry->clientdata);
146 XGrabPointer(dpy, menu->frame->screen_ptr->root_win, True, 0,
147 GrabModeAsync, GrabModeAsync, None, wCursor[WCUR_WAIT], CurrentTime);
148 XSync(dpy, 0);
150 if (cmdline) {
151 ExecuteShellCommand(menu->frame->screen_ptr, cmdline);
152 wfree(cmdline);
154 XUngrabPointer(dpy, CurrentTime);
155 XSync(dpy, 0);
158 static void exitCommand(WMenu * menu, WMenuEntry * entry)
160 static int inside = 0;
161 int result;
163 /* prevent reentrant calls */
164 if (inside)
165 return;
166 inside = 1;
168 #define R_CANCEL 0
169 #define R_EXIT 1
171 result = R_CANCEL;
173 if ((long)entry->clientdata == M_QUICK) {
174 result = R_EXIT;
175 } else {
176 int r, oldSaveSessionFlag;
178 oldSaveSessionFlag = wPreferences.save_session_on_exit;
179 r = wExitDialog(menu->frame->screen_ptr, _("Exit"),
180 _("Exit window manager?"), _("Exit"), _("Cancel"), NULL);
182 if (r == WAPRDefault) {
183 result = R_EXIT;
184 } else if (r == WAPRAlternate) {
185 /* Don't modify the "save session on exit" flag if the
186 * user canceled the operation. */
187 wPreferences.save_session_on_exit = oldSaveSessionFlag;
190 if (result == R_EXIT)
191 Shutdown(WSExitMode);
193 #undef R_EXIT
194 #undef R_CANCEL
195 inside = 0;
198 static void shutdownCommand(WMenu * menu, WMenuEntry * entry)
200 static int inside = 0;
201 int result;
203 /* prevent reentrant calls */
204 if (inside)
205 return;
206 inside = 1;
208 #define R_CANCEL 0
209 #define R_CLOSE 1
210 #define R_KILL 2
212 result = R_CANCEL;
213 if ((long)entry->clientdata == M_QUICK)
214 result = R_CLOSE;
215 else {
217 int r, oldSaveSessionFlag;
219 oldSaveSessionFlag = wPreferences.save_session_on_exit;
221 r = wExitDialog(menu->frame->screen_ptr,
222 _("Kill X session"),
223 _("Kill Window System session?\n"
224 "(all applications will be closed)"), _("Kill"), _("Cancel"), NULL);
225 if (r == WAPRDefault) {
226 result = R_KILL;
227 } else if (r == WAPRAlternate) {
228 /* Don't modify the "save session on exit" flag if the
229 * user canceled the operation. */
230 wPreferences.save_session_on_exit = oldSaveSessionFlag;
235 if (result != R_CANCEL) {
237 Shutdown(WSKillMode);
240 #undef R_CLOSE
241 #undef R_CANCEL
242 #undef R_KILL
243 inside = 0;
246 static void restartCommand(WMenu * menu, WMenuEntry * entry)
248 Shutdown(WSRestartPreparationMode);
249 Restart((char *)entry->clientdata, False);
250 Restart(NULL, True);
253 static void refreshCommand(WMenu * menu, WMenuEntry * entry)
255 wRefreshDesktop(menu->frame->screen_ptr);
258 static void arrangeIconsCommand(WMenu * menu, WMenuEntry * entry)
260 wArrangeIcons(menu->frame->screen_ptr, True);
263 static void showAllCommand(WMenu * menu, WMenuEntry * entry)
265 wShowAllWindows(menu->frame->screen_ptr);
268 static void hideOthersCommand(WMenu * menu, WMenuEntry * entry)
270 wHideOtherApplications(menu->frame->screen_ptr->focused_window);
273 static void saveSessionCommand(WMenu * menu, WMenuEntry * entry)
275 if (!wPreferences.save_session_on_exit)
276 wSessionSaveState(menu->frame->screen_ptr);
278 wScreenSaveState(menu->frame->screen_ptr);
281 static void clearSessionCommand(WMenu * menu, WMenuEntry * entry)
283 wSessionClearState(menu->frame->screen_ptr);
284 wScreenSaveState(menu->frame->screen_ptr);
287 static void infoPanelCommand(WMenu * menu, WMenuEntry * entry)
289 wShowInfoPanel(menu->frame->screen_ptr);
292 static void legalPanelCommand(WMenu * menu, WMenuEntry * entry)
294 wShowLegalPanel(menu->frame->screen_ptr);
297 /********************************************************************/
299 static char * getLocalizedMenuFile(char *menu)
301 char *buffer, *ptr, *locale;
302 int len;
304 if (!Locale)
305 return NULL;
307 len = strlen(menu) + strlen(Locale) + 8;
308 buffer = wmalloc(len);
310 /* try menu.locale_name */
311 snprintf(buffer, len, "%s.%s", menu, Locale);
312 if (access(buffer, F_OK) == 0)
313 return buffer;
315 /* position of locale in our buffer */
316 locale = buffer + strlen(menu) + 1;
318 /* check if it is in the form aa_bb.encoding and check for aa_bb */
319 ptr = strchr(locale, '.');
320 if (ptr) {
321 *ptr = 0;
322 if (access(buffer, F_OK) == 0)
323 return buffer;
326 /* now check for aa */
327 ptr = strchr(locale, '_');
328 if (ptr) {
329 *ptr = 0;
330 if (access(buffer, F_OK) == 0)
331 return buffer;
334 wfree(buffer);
336 return NULL;
339 Bool wRootMenuPerformShortcut(XEvent * event)
341 WScreen *scr = wScreenForRootWindow(event->xkey.root);
342 Shortcut *ptr;
343 int modifiers;
344 int done = 0;
346 /* ignore CapsLock */
347 modifiers = event->xkey.state & ValidModMask;
349 for (ptr = shortcutList; ptr != NULL; ptr = ptr->next) {
350 if (ptr->keycode == 0 || ptr->menu->menu->screen_ptr != scr)
351 continue;
353 if (ptr->keycode == event->xkey.keycode && ptr->modifier == modifiers) {
354 (*ptr->entry->callback) (ptr->menu, ptr->entry);
355 done = True;
359 return done;
362 void wRootMenuBindShortcuts(Window window)
364 Shortcut *ptr;
366 ptr = shortcutList;
367 while (ptr) {
368 if (ptr->modifier != AnyModifier) {
369 XGrabKey(dpy, ptr->keycode, ptr->modifier | LockMask,
370 window, True, GrabModeAsync, GrabModeAsync);
371 #ifdef NUMLOCK_HACK
372 wHackedGrabKey(ptr->keycode, ptr->modifier, window, True, GrabModeAsync, GrabModeAsync);
373 #endif
375 XGrabKey(dpy, ptr->keycode, ptr->modifier, window, True, GrabModeAsync, GrabModeAsync);
376 ptr = ptr->next;
380 static void rebindKeygrabs(WScreen * scr)
382 WWindow *wwin;
384 wwin = scr->focused_window;
386 while (wwin != NULL) {
387 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->frame->core->window);
389 if (!WFLAGP(wwin, no_bind_keys)) {
390 wWindowSetKeyGrabs(wwin);
392 wwin = wwin->prev;
396 static void removeShortcutsForMenu(WMenu * menu)
398 Shortcut *ptr, *tmp;
399 Shortcut *newList = NULL;
401 ptr = shortcutList;
402 while (ptr != NULL) {
403 tmp = ptr->next;
404 if (ptr->menu == menu) {
405 wfree(ptr);
406 } else {
407 ptr->next = newList;
408 newList = ptr;
410 ptr = tmp;
412 shortcutList = newList;
413 menu->menu->screen_ptr->flags.root_menu_changed_shortcuts = 1;
416 static Bool addShortcut(char *file, char *shortcutDefinition, WMenu * menu, WMenuEntry * entry)
418 Shortcut *ptr;
419 KeySym ksym;
420 char *k;
421 char buf[MAX_SHORTCUT_LENGTH], *b;
423 ptr = wmalloc(sizeof(Shortcut));
425 wstrlcpy(buf, shortcutDefinition, MAX_SHORTCUT_LENGTH);
426 b = (char *)buf;
428 /* get modifiers */
429 ptr->modifier = 0;
430 while ((k = strchr(b, '+')) != NULL) {
431 int mod;
433 *k = 0;
434 mod = wXModifierFromKey(b);
435 if (mod < 0) {
436 wwarning(_("%s: invalid key modifier \"%s\""), file, b);
437 wfree(ptr);
438 return False;
440 ptr->modifier |= mod;
442 b = k + 1;
445 /* get key */
446 ksym = XStringToKeysym(b);
448 if (ksym == NoSymbol) {
449 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
450 file, shortcutDefinition, entry->text);
451 wfree(ptr);
452 return False;
455 ptr->keycode = XKeysymToKeycode(dpy, ksym);
456 if (ptr->keycode == 0) {
457 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file,
458 shortcutDefinition, entry->text);
459 wfree(ptr);
460 return False;
463 ptr->menu = menu;
464 ptr->entry = entry;
466 ptr->next = shortcutList;
467 shortcutList = ptr;
469 menu->menu->screen_ptr->flags.root_menu_changed_shortcuts = 1;
471 return True;
474 static char *next_token(char *line, char **next)
476 char *tmp, c;
477 char *ret;
479 *next = NULL;
480 while (*line == ' ' || *line == '\t')
481 line++;
483 tmp = line;
485 if (*tmp == '"') {
486 tmp++;
487 line++;
488 while (*tmp != 0 && *tmp != '"')
489 tmp++;
490 if (*tmp != '"') {
491 wwarning(_("%s: unmatched '\"' in menu file"), line);
492 return NULL;
494 } else {
495 do {
496 if (*tmp == '\\')
497 tmp++;
499 if (*tmp != 0)
500 tmp++;
502 } while (*tmp != 0 && *tmp != ' ' && *tmp != '\t');
505 c = *tmp;
506 *tmp = 0;
507 ret = wstrdup(line);
508 *tmp = c;
510 if (c == 0)
511 return ret;
512 else
513 tmp++;
515 /* skip blanks */
516 while (*tmp == ' ' || *tmp == '\t')
517 tmp++;
519 if (*tmp != 0)
520 *next = tmp;
522 return ret;
525 static void separateCommand(char *line, char ***file, char **command)
527 char *token, *tmp = line;
528 WMArray *array = WMCreateArray(4);
529 int count, i;
531 *file = NULL;
532 *command = NULL;
533 do {
534 token = next_token(tmp, &tmp);
535 if (token) {
536 if (strcmp(token, "WITH") == 0) {
537 if (tmp != NULL && *tmp != 0)
538 *command = wstrdup(tmp);
539 else
540 wwarning(_("%s: missing command"), line);
541 break;
543 WMAddToArray(array, token);
545 } while (token != NULL && tmp != NULL);
547 count = WMGetArrayItemCount(array);
548 if (count > 0) {
549 *file = wmalloc(sizeof(char *) * (count + 1));
550 (*file)[count] = NULL;
551 for (i = 0; i < count; i++) {
552 (*file)[i] = WMGetFromArray(array, i);
555 WMFreeArray(array);
558 static WMenu *constructPLMenu(WScreen *screen, char *path)
560 WMPropList *pl = NULL;
561 WMenu *menu = NULL;
563 if (!path)
564 return NULL;
566 pl = WMReadPropListFromFile(path);
567 if (!pl)
568 return NULL;
570 menu = configureMenu(screen, pl, False);
571 if (!menu)
572 return NULL;
574 menu->on_destroy = removeShortcutsForMenu;
575 return menu;
578 static void constructMenu(WMenu * menu, WMenuEntry * entry)
580 WMenu *submenu;
581 struct stat stat_buf;
582 char **path;
583 char *cmd;
584 char *lpath = NULL;
585 int i, first = -1;
586 time_t last = 0;
588 separateCommand((char *)entry->clientdata, &path, &cmd);
589 if (path == NULL || *path == NULL || **path == 0) {
590 wwarning(_("invalid OPEN_MENU specification: %s"), (char *)entry->clientdata);
591 return;
594 if (path[0][0] == '|') {
595 /* pipe menu */
597 if (!menu->cascades[entry->cascade] || menu->cascades[entry->cascade]->timestamp == 0) {
598 /* parse pipe */
600 submenu = readMenuPipe(menu->frame->screen_ptr, path);
602 if (submenu != NULL) {
603 if (path[0][1] == '|')
604 submenu->timestamp = 0;
605 else
606 submenu->timestamp = 1; /* there's no automatic reloading */
608 } else {
609 submenu = NULL;
612 } else {
614 /* try interpreting path as a proplist file */
615 submenu = constructPLMenu(menu->frame->screen_ptr, path[0]);
616 /* if unsuccessful, try it as an old-style file */
617 if (!submenu) {
619 i = 0;
620 while (path[i] != NULL) {
621 char *tmp;
623 if (strcmp(path[i], "-noext") == 0) {
624 i++;
625 continue;
628 tmp = wexpandpath(path[i]);
629 wfree(path[i]);
630 lpath = getLocalizedMenuFile(tmp);
631 if (lpath) {
632 wfree(tmp);
633 path[i] = lpath;
634 lpath = NULL;
635 } else {
636 path[i] = tmp;
639 if (stat(path[i], &stat_buf) == 0) {
640 if (last < stat_buf.st_mtime)
641 last = stat_buf.st_mtime;
642 if (first < 0)
643 first = i;
644 } else {
645 werror(_("%s:could not stat menu"), path[i]);
646 /*goto finish; */
649 i++;
652 if (first < 0) {
653 werror(_("%s:could not stat menu:%s"), "OPEN_MENU", (char *)entry->clientdata);
654 goto finish;
656 stat(path[first], &stat_buf);
657 if (!menu->cascades[entry->cascade]
658 || menu->cascades[entry->cascade]->timestamp < last) {
660 if (S_ISDIR(stat_buf.st_mode)) {
661 /* menu directory */
662 submenu = readMenuDirectory(menu->frame->screen_ptr, entry->text, path, cmd);
663 if (submenu)
664 submenu->timestamp = last;
665 } else if (S_ISREG(stat_buf.st_mode)) {
666 /* menu file */
668 if (cmd || path[1])
669 wwarning(_("too many parameters in OPEN_MENU: %s"),
670 (char *)entry->clientdata);
672 submenu = readMenuFile(menu->frame->screen_ptr, path[first]);
673 if (submenu)
674 submenu->timestamp = stat_buf.st_mtime;
675 } else {
676 submenu = NULL;
678 } else {
679 submenu = NULL;
684 if (submenu) {
685 wMenuEntryRemoveCascade(menu, entry);
686 wMenuEntrySetCascade(menu, entry, submenu);
689 finish:
690 i = 0;
691 while (path[i] != NULL)
692 wfree(path[i++]);
693 wfree(path);
694 if (cmd)
695 wfree(cmd);
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)
706 WMenu *wsmenu;
707 WMenuEntry *entry;
709 if (scr->flags.added_workspace_menu) {
710 wwarning(_
711 ("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
712 return NULL;
713 } else {
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);
725 return entry;
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)
736 WMenu *wwmenu;
737 WWindow *wwin;
738 WMenuEntry *entry;
740 if (scr->flags.added_windows_menu) {
741 wwarning(_
742 ("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
743 return NULL;
744 } else {
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;
751 while (wwin) {
752 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
754 wwin = wwin->prev;
756 entry = wMenuAddCallback(menu, title, NULL, NULL);
757 wMenuEntrySetCascade(menu, entry, wwmenu);
759 return entry;
762 static WMenuEntry *addMenuEntry(WMenu * menu, char *title, char *shortcut, char *command,
763 char *params, char *file_name)
765 WScreen *scr;
766 WMenuEntry *entry = NULL;
767 Bool shortcutOk = False;
769 if (!menu)
770 return NULL;
771 scr = menu->frame->screen_ptr;
772 if (strcmp(command, "OPEN_MENU") == 0) {
773 if (!params) {
774 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name, command);
775 } else {
776 WMenu *dummy;
777 char *path;
779 path = wfindfile(DEF_CONFIG_PATHS, params);
780 if (!path) {
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) {
790 if (!params)
791 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name, command);
792 else {
793 entry = wMenuAddCallback(menu, title, execCommand, wstrconcat("exec ", params));
794 entry->free_cdata = free;
795 shortcutOk = True;
797 } else if (strcmp(command, "SHEXEC") == 0) {
798 if (!params)
799 wwarning(_("%s:missing parameter for menu command \"%s\""), file_name, command);
800 else {
801 entry = wMenuAddCallback(menu, title, execCommand, wstrdup(params));
802 entry->free_cdata = free;
803 shortcutOk = True;
805 } else if (strcmp(command, "EXIT") == 0) {
807 if (params && strcmp(params, "QUICK") == 0)
808 entry = wMenuAddCallback(menu, title, exitCommand, (void *)M_QUICK);
809 else
810 entry = wMenuAddCallback(menu, title, exitCommand, NULL);
812 shortcutOk = True;
813 } else if (strcmp(command, "SHUTDOWN") == 0) {
815 if (params && strcmp(params, "QUICK") == 0)
816 entry = wMenuAddCallback(menu, title, shutdownCommand, (void *)M_QUICK);
817 else
818 entry = wMenuAddCallback(menu, title, shutdownCommand, NULL);
820 shortcutOk = True;
821 } else if (strcmp(command, "REFRESH") == 0) {
822 entry = wMenuAddCallback(menu, title, refreshCommand, NULL);
824 shortcutOk = True;
825 } else if (strcmp(command, "WORKSPACE_MENU") == 0) {
826 entry = addWorkspaceMenu(scr, menu, title);
828 shortcutOk = True;
829 } else if (strcmp(command, "WINDOWS_MENU") == 0) {
830 entry = addWindowsMenu(scr, menu, title);
832 shortcutOk = True;
833 } else if (strcmp(command, "ARRANGE_ICONS") == 0) {
834 entry = wMenuAddCallback(menu, title, arrangeIconsCommand, NULL);
836 shortcutOk = True;
837 } else if (strcmp(command, "HIDE_OTHERS") == 0) {
838 entry = wMenuAddCallback(menu, title, hideOthersCommand, NULL);
840 shortcutOk = True;
841 } else if (strcmp(command, "SHOW_ALL") == 0) {
842 entry = wMenuAddCallback(menu, title, showAllCommand, NULL);
844 shortcutOk = True;
845 } else if (strcmp(command, "RESTART") == 0) {
846 entry = wMenuAddCallback(menu, title, restartCommand, params ? wstrdup(params) : NULL);
847 entry->free_cdata = free;
848 shortcutOk = True;
849 } else if (strcmp(command, "SAVE_SESSION") == 0) {
850 entry = wMenuAddCallback(menu, title, saveSessionCommand, NULL);
852 shortcutOk = True;
853 } else if (strcmp(command, "CLEAR_SESSION") == 0) {
854 entry = wMenuAddCallback(menu, title, clearSessionCommand, NULL);
855 shortcutOk = True;
856 } else if (strcmp(command, "INFO_PANEL") == 0) {
857 entry = wMenuAddCallback(menu, title, infoPanelCommand, NULL);
858 shortcutOk = True;
859 } else if (strcmp(command, "LEGAL_PANEL") == 0) {
860 entry = wMenuAddCallback(menu, title, legalPanelCommand, NULL);
861 shortcutOk = True;
862 } else {
863 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name, command);
865 return NULL;
868 if (shortcut && entry) {
869 if (!shortcutOk) {
870 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name, title);
871 } else {
872 if (addShortcut(file_name, shortcut, menu, entry)) {
874 entry->rtext = GetShortcutString(shortcut);
876 entry->rtext = wstrdup(shortcut);
882 return entry;
885 /******************* Menu Configuration From File *******************/
887 static void separateline(char *line, char *title, char *command, char *parameter, char *shortcut)
889 int l, i;
891 l = strlen(line);
893 *title = 0;
894 *command = 0;
895 *parameter = 0;
896 *shortcut = 0;
897 /* get the title */
898 while (isspace(*line) && (*line != 0))
899 line++;
900 if (*line == '"') {
901 line++;
902 i = 0;
903 while (line[i] != '"' && (line[i] != 0))
904 i++;
905 if (line[i] != '"')
906 return;
907 } else {
908 i = 0;
909 while (!isspace(line[i]) && (line[i] != 0))
910 i++;
912 strncpy(title, line, i);
913 title[i++] = 0;
914 line += i;
916 /* get the command or shortcut keyword */
917 while (isspace(*line) && (*line != 0))
918 line++;
919 if (*line == 0)
920 return;
921 i = 0;
922 while (!isspace(line[i]) && (line[i] != 0))
923 i++;
924 strncpy(command, line, i);
925 command[i++] = 0;
926 line += i;
928 if (strcmp(command, "SHORTCUT") == 0) {
929 /* get the shortcut key */
930 while (isspace(*line) && (*line != 0))
931 line++;
932 if (*line == '"') {
933 line++;
934 i = 0;
935 while (line[i] != '"' && (line[i] != 0))
936 i++;
937 if (line[i] != '"')
938 return;
939 } else {
940 i = 0;
941 while (!isspace(line[i]) && (line[i] != 0))
942 i++;
944 strncpy(shortcut, line, i);
945 shortcut[i++] = 0;
946 line += i;
948 *command = 0;
950 /* get the command */
951 while (isspace(*line) && (*line != 0))
952 line++;
953 if (*line == 0)
954 return;
955 i = 0;
956 while (!isspace(line[i]) && (line[i] != 0))
957 i++;
958 strncpy(command, line, i);
959 command[i++] = 0;
960 line += i;
963 /* get the parameters */
964 while (isspace(*line) && (*line != 0))
965 line++;
966 if (*line == 0)
967 return;
969 if (*line == '"') {
970 line++;
971 l = 0;
972 while (line[l] != 0 && line[l] != '"') {
973 parameter[l] = line[l];
974 l++;
976 parameter[l] = 0;
977 return;
980 l = strlen(line);
981 while (isspace(line[l]) && (l > 0))
982 l--;
983 strncpy(parameter, line, l);
984 parameter[l] = 0;
987 static WMenu *parseCascade(WScreen * scr, WMenu * menu, FILE * file, char *file_name)
989 char linebuf[MAXLINE];
990 char elinebuf[MAXLINE];
991 char title[MAXLINE];
992 char command[MAXLINE];
993 char shortcut[MAXLINE];
994 char params[MAXLINE];
995 char *line;
997 while (!feof(file)) {
998 int lsize, ok;
1000 ok = 0;
1001 fgets(linebuf, MAXLINE, file);
1002 line = wtrimspace(linebuf);
1003 lsize = strlen(line);
1004 do {
1005 if (line[lsize - 1] == '\\') {
1006 char *line2;
1007 int lsize2;
1008 fgets(elinebuf, MAXLINE, file);
1009 line2 = wtrimspace(elinebuf);
1010 lsize2 = strlen(line2);
1011 if (lsize2 + lsize > MAXLINE) {
1012 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1013 file_name, line);
1014 ok = 2;
1015 } else {
1016 line[lsize - 1] = 0;
1017 lsize += lsize2 - 1;
1018 strcat(line, line2);
1020 } else {
1021 ok = 1;
1023 } while (!ok && !feof(file));
1024 if (ok == 2)
1025 continue;
1027 if (line[0] == 0 || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
1028 continue;
1030 separateline(line, title, command, params, shortcut);
1032 if (!command[0]) {
1033 wwarning(_("%s:missing command in menu config: %s"), file_name, line);
1034 goto error;
1037 if (strcasecmp(command, "MENU") == 0) {
1038 WMenu *cascade;
1040 /* start submenu */
1042 cascade = wMenuCreate(scr, M_(title), False);
1043 cascade->on_destroy = removeShortcutsForMenu;
1044 if (parseCascade(scr, cascade, file, file_name) == NULL) {
1045 wMenuDestroy(cascade, True);
1046 } else {
1047 wMenuEntrySetCascade(menu, wMenuAddCallback(menu, M_(title), NULL, NULL), cascade);
1049 } else if (strcasecmp(command, "END") == 0) {
1050 /* end of menu */
1051 return menu;
1053 } else {
1054 /* normal items */
1055 addMenuEntry(menu, M_(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);
1061 return menu;
1063 error:
1064 return menu;
1067 static WMenu *readMenuFile(WScreen * scr, char *file_name)
1069 WMenu *menu = NULL;
1070 FILE *file = NULL;
1071 char linebuf[MAXLINE];
1072 char title[MAXLINE];
1073 char shortcut[MAXLINE];
1074 char command[MAXLINE];
1075 char params[MAXLINE];
1076 char *line;
1077 #ifdef USECPP
1078 char *args;
1079 int cpp = 0;
1080 #endif
1082 #ifdef USECPP
1083 if (!wPreferences.flags.nocpp) {
1084 args = MakeCPPArgs(file_name);
1085 if (!args) {
1086 wwarning(_("could not make arguments for menu file preprocessor"));
1087 } else {
1088 snprintf(command, sizeof(command), "%s %s %s", CPP_PATH, args, file_name);
1089 wfree(args);
1090 file = popen(command, "r");
1091 if (!file) {
1092 werror(_("%s:could not open/preprocess menu file"), file_name);
1093 } else {
1094 cpp = 1;
1098 #endif /* USECPP */
1100 if (!file) {
1101 file = fopen(file_name, "rb");
1102 if (!file) {
1103 werror(_("%s:could not open menu file"), file_name);
1104 return NULL;
1108 while (!feof(file)) {
1109 if (!fgets(linebuf, MAXLINE, file))
1110 break;
1111 line = wtrimspace(linebuf);
1112 if (line[0] == 0 || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
1113 continue;
1115 separateline(line, title, command, params, shortcut);
1117 if (!command[0]) {
1118 wwarning(_("%s:missing command in menu config: %s"), file_name, line);
1119 break;
1121 if (strcasecmp(command, "MENU") == 0) {
1122 menu = wMenuCreate(scr, M_(title), True);
1123 menu->on_destroy = removeShortcutsForMenu;
1124 if (!parseCascade(scr, menu, file, file_name)) {
1125 wMenuDestroy(menu, True);
1127 break;
1128 } else {
1129 wwarning(_("%s:invalid menu file. MENU command is missing"), file_name);
1130 break;
1134 #ifdef USECPP
1135 if (cpp) {
1136 if (pclose(file) == -1) {
1137 werror(_("error reading preprocessed menu data"));
1139 } else {
1140 fclose(file);
1142 #else
1143 fclose(file);
1144 #endif
1146 return menu;
1149 /************ Menu Configuration From Pipe *************/
1151 static WMenu *readMenuPipe(WScreen * scr, char **file_name)
1153 WMenu *menu = NULL;
1154 FILE *file = NULL;
1155 char linebuf[MAXLINE];
1156 char title[MAXLINE];
1157 char command[MAXLINE];
1158 char params[MAXLINE];
1159 char shortcut[MAXLINE];
1160 char *line;
1161 char *filename;
1162 char flat_file[MAXLINE];
1163 int i;
1164 #ifdef USECPP
1165 char *args;
1166 #endif
1168 flat_file[0] = '\0';
1170 for (i = 0; file_name[i] != NULL; i++) {
1171 strcat(flat_file, file_name[i]);
1172 strcat(flat_file, " ");
1174 filename = flat_file + (flat_file[1] == '|' ? 2 : 1);
1176 #ifdef USECPP
1177 if (!wPreferences.flags.nocpp) {
1178 args = MakeCPPArgs(filename);
1179 if (!args) {
1180 wwarning(_("could not make arguments for menu file preprocessor"));
1181 } else {
1182 snprintf(command, sizeof(command), "%s | %s %s", filename, CPP_PATH, args);
1184 wfree(args);
1185 file = popen(command, "r");
1186 if (!file) {
1187 werror(_("%s:could not open/preprocess menu file"), filename);
1191 #endif /* USECPP */
1193 if (!file) {
1194 file = popen(filename, "rb");
1196 if (!file) {
1197 werror(_("%s:could not open menu file"), filename);
1198 return NULL;
1202 while (!feof(file)) {
1203 if (!fgets(linebuf, MAXLINE, file))
1204 break;
1205 line = wtrimspace(linebuf);
1206 if (line[0] == 0 || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
1207 continue;
1209 separateline(line, title, command, params, shortcut);
1211 if (!command[0]) {
1212 wwarning(_("%s:missing command in menu config: %s"), filename, line);
1213 break;
1215 if (strcasecmp(command, "MENU") == 0) {
1216 menu = wMenuCreate(scr, M_(title), True);
1217 menu->on_destroy = removeShortcutsForMenu;
1218 if (!parseCascade(scr, menu, file, filename)) {
1219 wMenuDestroy(menu, True);
1221 break;
1222 } else {
1223 wwarning(_("%s:no title given for the root menu"), filename);
1224 break;
1228 pclose(file);
1230 return menu;
1233 typedef struct {
1234 char *name;
1235 int index;
1236 } dir_data;
1238 static int myCompare(const void *d1, const void *d2)
1240 dir_data *p1 = *(dir_data **) d1;
1241 dir_data *p2 = *(dir_data **) d2;
1243 return strcmp(p1->name, p2->name);
1246 /************ Menu Configuration From Directory *************/
1248 static Bool isFilePackage(char *file)
1250 int l;
1252 /* check if the extension indicates this file is a
1253 * file package. For now, only recognize .themed */
1255 l = strlen(file);
1257 if (l > 7 && strcmp(&(file[l - 7]), ".themed") == 0) {
1258 return True;
1259 } else {
1260 return False;
1264 static WMenu *readMenuDirectory(WScreen * scr, char *title, char **path, char *command)
1266 DIR *dir;
1267 struct dirent *dentry;
1268 struct stat stat_buf;
1269 WMenu *menu = NULL;
1270 char *buffer;
1271 WMArray *dirs = NULL, *files = NULL;
1272 WMArrayIterator iter;
1273 int length, i, have_space = 0;
1274 dir_data *data;
1275 int stripExtension = 0;
1277 dirs = WMCreateArray(16);
1278 files = WMCreateArray(16);
1280 i = 0;
1281 while (path[i] != NULL) {
1282 if (strcmp(path[i], "-noext") == 0) {
1283 stripExtension = 1;
1284 i++;
1285 continue;
1288 dir = opendir(path[i]);
1289 if (!dir) {
1290 i++;
1291 continue;
1294 while ((dentry = readdir(dir))) {
1296 if (strcmp(dentry->d_name, ".") == 0 || strcmp(dentry->d_name, "..") == 0)
1297 continue;
1299 if (dentry->d_name[0] == '.')
1300 continue;
1302 buffer = malloc(strlen(path[i]) + strlen(dentry->d_name) + 4);
1303 if (!buffer) {
1304 werror(_("out of memory while constructing directory menu %s"), path[i]);
1305 break;
1308 strcpy(buffer, path[i]);
1309 strcat(buffer, "/");
1310 strcat(buffer, dentry->d_name);
1312 if (stat(buffer, &stat_buf) != 0) {
1313 werror(_("%s:could not stat file \"%s\" in menu directory"),
1314 path[i], dentry->d_name);
1315 } else {
1316 Bool isFilePack = False;
1318 data = NULL;
1319 if (S_ISDIR(stat_buf.st_mode)
1320 && !(isFilePack = isFilePackage(dentry->d_name))) {
1322 /* access always returns success for user root */
1323 if (access(buffer, X_OK) == 0) {
1324 /* Directory is accesible. Add to directory list */
1326 data = (dir_data *) wmalloc(sizeof(dir_data));
1327 data->name = wstrdup(dentry->d_name);
1328 data->index = i;
1330 WMAddToArray(dirs, data);
1332 } else if (S_ISREG(stat_buf.st_mode) || isFilePack) {
1333 /* Hack because access always returns X_OK success for user root */
1334 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1335 if ((command != NULL && access(buffer, R_OK) == 0) ||
1336 (command == NULL && access(buffer, X_OK) == 0 &&
1337 (stat_buf.st_mode & S_IXANY))) {
1339 data = (dir_data *) wmalloc(sizeof(dir_data));
1340 data->name = wstrdup(dentry->d_name);
1341 data->index = i;
1343 WMAddToArray(files, data);
1347 wfree(buffer);
1350 closedir(dir);
1351 i++;
1354 if (!WMGetArrayItemCount(dirs) && !WMGetArrayItemCount(files)) {
1355 WMFreeArray(dirs);
1356 WMFreeArray(files);
1357 return NULL;
1360 WMSortArray(dirs, myCompare);
1361 WMSortArray(files, myCompare);
1363 menu = wMenuCreate(scr, M_(title), False);
1364 menu->on_destroy = removeShortcutsForMenu;
1366 WM_ITERATE_ARRAY(dirs, data, iter) {
1367 /* New directory. Use same OPEN_MENU command that was used
1368 * for the current directory. */
1369 length = strlen(path[data->index]) + strlen(data->name) + 6;
1370 if (stripExtension)
1371 length += 7;
1372 if (command)
1373 length += strlen(command) + 6;
1374 buffer = malloc(length);
1375 if (!buffer) {
1376 werror(_("out of memory while constructing directory menu %s"), path[data->index]);
1377 break;
1380 buffer[0] = '\0';
1381 if (stripExtension)
1382 strcat(buffer, "-noext ");
1384 have_space = strchr(path[data->index], ' ') != NULL || strchr(data->name, ' ') != NULL;
1386 if (have_space)
1387 strcat(buffer, "\"");
1388 strcat(buffer, path[data->index]);
1390 strcat(buffer, "/");
1391 strcat(buffer, data->name);
1392 if (have_space)
1393 strcat(buffer, "\"");
1394 if (command) {
1395 strcat(buffer, " WITH ");
1396 strcat(buffer, command);
1399 addMenuEntry(menu, M_(data->name), NULL, "OPEN_MENU", buffer, path[data->index]);
1401 wfree(buffer);
1402 if (data->name)
1403 wfree(data->name);
1404 wfree(data);
1407 WM_ITERATE_ARRAY(files, data, iter) {
1408 /* executable: add as entry */
1409 length = strlen(path[data->index]) + strlen(data->name) + 6;
1410 if (command)
1411 length += strlen(command);
1413 buffer = malloc(length);
1414 if (!buffer) {
1415 werror(_("out of memory while constructing directory menu %s"), path[data->index]);
1416 break;
1419 have_space = strchr(path[data->index], ' ') != NULL || strchr(data->name, ' ') != NULL;
1420 if (command != NULL) {
1421 strcpy(buffer, command);
1422 strcat(buffer, " ");
1423 if (have_space)
1424 strcat(buffer, "\"");
1425 strcat(buffer, path[data->index]);
1426 } else {
1427 if (have_space) {
1428 buffer[0] = '"';
1429 buffer[1] = 0;
1430 strcat(buffer, path[data->index]);
1431 } else {
1432 strcpy(buffer, path[data->index]);
1435 strcat(buffer, "/");
1436 strcat(buffer, data->name);
1437 if (have_space)
1438 strcat(buffer, "\"");
1440 if (stripExtension) {
1441 char *ptr = strrchr(data->name, '.');
1442 if (ptr && ptr != data->name)
1443 *ptr = 0;
1445 addMenuEntry(menu, M_(data->name), NULL, "SHEXEC", buffer, path[data->index]);
1447 wfree(buffer);
1448 if (data->name)
1449 wfree(data->name);
1450 wfree(data);
1453 WMFreeArray(files);
1454 WMFreeArray(dirs);
1456 return menu;
1459 /************ Menu Configuration From WMRootMenu *************/
1461 static WMenu *makeDefaultMenu(WScreen * scr)
1463 WMenu *menu = NULL;
1465 menu = wMenuCreate(scr, _("Commands"), True);
1466 wMenuAddCallback(menu, M_("XTerm"), execCommand, "xterm");
1467 wMenuAddCallback(menu, M_("rxvt"), execCommand, "rxvt");
1468 wMenuAddCallback(menu, _("Restart"), restartCommand, NULL);
1469 wMenuAddCallback(menu, _("Exit..."), exitCommand, NULL);
1470 return menu;
1474 *----------------------------------------------------------------------
1475 * configureMenu--
1476 * Reads root menu configuration from defaults database.
1478 *----------------------------------------------------------------------
1480 static WMenu *configureMenu(WScreen * scr, WMPropList * definition, Bool includeGlobals)
1482 WMenu *menu = NULL;
1483 WMPropList *elem;
1484 int i, count;
1485 WMPropList *title, *command, *params;
1486 char *tmp, *mtitle;
1488 if (WMIsPLString(definition)) {
1489 struct stat stat_buf;
1490 char *path = NULL;
1491 Bool menu_is_default = False;
1493 /* menu definition is a string. Probably a path, so parse the file */
1495 tmp = wexpandpath(WMGetFromPLString(definition));
1497 path = getLocalizedMenuFile(tmp);
1499 if (!path)
1500 path = wfindfile(DEF_CONFIG_PATHS, tmp);
1502 if (!path) {
1503 path = wfindfile(DEF_CONFIG_PATHS, DEF_MENU_FILE);
1504 menu_is_default = True;
1507 if (!path) {
1508 werror(_("could not find menu file \"%s\" referenced in WMRootMenu"), tmp);
1509 wfree(tmp);
1510 return NULL;
1513 if (stat(path, &stat_buf) < 0) {
1514 werror(_("could not access menu \"%s\" referenced in WMRootMenu"), path);
1515 wfree(path);
1516 wfree(tmp);
1517 return NULL;
1520 if (!scr->root_menu || stat_buf.st_mtime > scr->root_menu->timestamp
1521 /* if the pointer in WMRootMenu has changed */
1522 || WDRootMenu->timestamp > scr->root_menu->timestamp) {
1524 if (menu_is_default) {
1525 wwarning(_
1526 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1527 path);
1530 menu = readMenuFile(scr, path);
1531 if (menu)
1532 menu->timestamp = WMAX(stat_buf.st_mtime, WDRootMenu->timestamp);
1533 } else {
1534 menu = NULL;
1536 wfree(path);
1537 wfree(tmp);
1539 return menu;
1542 count = WMGetPropListItemCount(definition);
1543 if (count == 0)
1544 return NULL;
1546 elem = WMGetFromPLArray(definition, 0);
1547 if (!WMIsPLString(elem)) {
1548 tmp = WMGetPropListDescription(elem, False);
1549 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp);
1550 wfree(tmp);
1551 return NULL;
1553 mtitle = WMGetFromPLString(elem);
1555 menu = wMenuCreate(scr, M_(mtitle), False);
1556 menu->on_destroy = removeShortcutsForMenu;
1558 #ifdef GLOBAL_SUBMENU_FILE
1559 if (includeGlobals) {
1560 WMenu *submenu;
1561 WMenuEntry *mentry;
1563 submenu = readMenuFile(scr, GLOBAL_SUBMENU_FILE);
1565 if (submenu) {
1566 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL, NULL);
1567 wMenuEntrySetCascade(menu, mentry, submenu);
1570 #endif
1572 for (i = 1; i < count; i++) {
1573 elem = WMGetFromPLArray(definition, i);
1574 #if 0
1575 if (WMIsPLString(elem)) {
1576 char *file;
1578 file = WMGetFromPLString(elem);
1581 #endif
1582 if (!WMIsPLArray(elem) || WMGetPropListItemCount(elem) < 2)
1583 goto error;
1585 if (WMIsPLArray(WMGetFromPLArray(elem, 1))) {
1586 WMenu *submenu;
1587 WMenuEntry *mentry;
1589 /* submenu */
1590 submenu = configureMenu(scr, elem, True);
1591 if (submenu) {
1592 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL, NULL);
1593 wMenuEntrySetCascade(menu, mentry, submenu);
1595 } else {
1596 int idx = 0;
1597 WMPropList *shortcut;
1598 /* normal entry */
1600 title = WMGetFromPLArray(elem, idx++);
1601 shortcut = WMGetFromPLArray(elem, idx++);
1602 if (strcmp(WMGetFromPLString(shortcut), "SHORTCUT") == 0) {
1603 shortcut = WMGetFromPLArray(elem, idx++);
1604 command = WMGetFromPLArray(elem, idx++);
1605 } else {
1606 command = shortcut;
1607 shortcut = NULL;
1609 params = WMGetFromPLArray(elem, idx++);
1611 if (!title || !command)
1612 goto error;
1614 addMenuEntry(menu, M_(WMGetFromPLString(title)),
1615 shortcut ? WMGetFromPLString(shortcut) : NULL,
1616 WMGetFromPLString(command),
1617 params ? WMGetFromPLString(params) : NULL, "WMRootMenu");
1619 continue;
1621 error:
1622 tmp = WMGetPropListDescription(elem, False);
1623 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp);
1624 wfree(tmp);
1627 return menu;
1631 *----------------------------------------------------------------------
1632 * OpenRootMenu--
1633 * Opens the root menu, parsing the menu configuration from the
1634 * defaults database.
1635 * If the menu is already mapped and is not sticked to the
1636 * root window, it will be unmapped.
1638 * Side effects:
1639 * The menu may be remade.
1641 * Notes:
1642 * Construction of OPEN_MENU entries are delayed to the moment the
1643 * user map's them.
1644 *----------------------------------------------------------------------
1646 void OpenRootMenu(WScreen * scr, int x, int y, int keyboard)
1648 WMenu *menu = NULL;
1649 WMPropList *definition;
1651 static WMPropList *domain=NULL;
1653 if (!domain) {
1654 domain = WMCreatePLString("WMRootMenu");
1658 scr->flags.root_menu_changed_shortcuts = 0;
1659 scr->flags.added_workspace_menu = 0;
1660 scr->flags.added_windows_menu = 0;
1662 if (scr->root_menu && scr->root_menu->flags.mapped) {
1663 menu = scr->root_menu;
1664 if (!menu->flags.buttoned) {
1665 wMenuUnmap(menu);
1666 } else {
1667 wRaiseFrame(menu->frame->core);
1669 if (keyboard)
1670 wMenuMapAt(menu, 0, 0, True);
1671 else
1672 wMenuMapCopyAt(menu, x - menu->frame->core->width / 2, y);
1674 return;
1677 definition = WDRootMenu->dictionary;
1680 definition = PLGetDomain(domain);
1682 if (definition) {
1683 if (WMIsPLArray(definition)) {
1684 if (!scr->root_menu || WDRootMenu->timestamp > scr->root_menu->timestamp) {
1685 menu = configureMenu(scr, definition, True);
1686 if (menu)
1687 menu->timestamp = WDRootMenu->timestamp;
1689 } else
1690 menu = NULL;
1691 } else {
1692 menu = configureMenu(scr, definition, True);
1696 if (!menu) {
1697 /* menu hasn't changed or could not be read */
1698 if (!scr->root_menu) {
1699 wMessageDialog(scr, _("Error"),
1700 _("The applications menu could not be loaded. "
1701 "Look at the console output for a detailed "
1702 "description of the errors."), _("OK"), NULL, NULL);
1704 menu = makeDefaultMenu(scr);
1705 scr->root_menu = menu;
1707 menu = scr->root_menu;
1708 } else {
1709 /* new root menu */
1710 if (scr->root_menu) {
1711 wMenuDestroy(scr->root_menu, True);
1713 scr->root_menu = menu;
1715 if (menu) {
1716 int newx, newy;
1718 if (keyboard && x == 0 && y == 0) {
1719 newx = newy = 0;
1720 } else if (keyboard && x == scr->scr_width / 2 && y == scr->scr_height / 2) {
1721 newx = x - menu->frame->core->width / 2;
1722 newy = y - menu->frame->core->height / 2;
1723 } else {
1724 newx = x - menu->frame->core->width / 2;
1725 newy = y;
1727 wMenuMapAt(menu, newx, newy, keyboard);
1730 if (scr->flags.root_menu_changed_shortcuts)
1731 rebindKeygrabs(scr);