- Really fixed problem with keyboard shortcuts executed an every screen for
[wmaker-crm.git] / src / rootmenu.c
blob3eaad07a523908459cfeeeeadd9f41dbf7350022
1 /* rootmenu.c- user defined menu
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998 Dan Pascu
7 *
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,
21 * USA.
24 #include "wconfig.h"
26 #ifndef LITE
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <sys/wait.h>
33 #include <sys/types.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <time.h>
37 #include <dirent.h>
39 #include <X11/Xlib.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xatom.h>
43 #include "WindowMaker.h"
44 #include "actions.h"
45 #include "menu.h"
46 #include "funcs.h"
47 #include "dialog.h"
48 #include "keybind.h"
49 #include "stacking.h"
50 #include "workspace.h"
51 #include "defaults.h"
52 #include "framewin.h"
53 #include "session.h"
54 #include "xmodifier.h"
56 #include <WINGs/WUtil.h>
60 extern char *Locale;
62 extern WDDomain *WDRootMenu;
64 extern Cursor wCursor[WCUR_LAST];
66 extern Time LastTimestamp;
68 extern WPreferences wPreferences;
70 extern int wScreenCount;
72 static WMenu *readMenuPipe(WScreen *scr, char **file_name);
73 static WMenu *readMenuFile(WScreen *scr, char *file_name);
74 static WMenu *readMenuDirectory(WScreen *scr, char *title, char **file_name,
75 char *command);
78 typedef struct Shortcut {
79 struct Shortcut *next;
81 int modifier;
82 KeyCode keycode;
83 WMenuEntry *entry;
84 WMenu *menu;
85 } Shortcut;
89 static Shortcut *shortcutList = NULL;
93 * Syntax:
94 * # main menu
95 * "Menu Name" MENU
96 * "Title" EXEC command_to_exec -params
97 * "Submenu" MENU
98 * "Title" EXEC command_to_exec -params
99 * "Submenu" END
100 * "Workspaces" WORKSPACE_MENU
101 * "Title" built_in_command
102 * "Quit" EXIT
103 * "Quick Quit" EXIT QUICK
104 * "Menu Name" END
106 * Commands may be preceded by SHORTCUT key
108 * Built-in commands:
110 * INFO_PANEL - shows the Info Panel
111 * LEGAL_PANEL - shows the Legal info panel
112 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
113 * REFRESH - forces the desktop to be repainted
114 * EXIT [QUICK] - exit the window manager [without confirmation]
115 * EXEC <program> - execute an external program
116 * SHEXEC <command> - execute a shell command
117 * WORKSPACE_MENU - places the workspace submenu
118 * ARRANGE_ICONS
119 * RESTART [<window manager>] - restarts the window manager
120 * SHOW_ALL - unhide all windows on workspace
121 * HIDE_OTHERS - hides all windows excep the focused one
122 * OPEN_MENU file - read menu data from file which must be a valid menu file.
123 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
124 * - read menu data from directory(ies) and
125 * eventually precede each with a command.
126 * OPEN_MENU | command
127 * - opens command and uses its stdout to construct and insert
128 * the resulting menu in current position. The output of
129 * command must be a valid menu description.
130 * The space between '|' and command is optional.
131 * || will do the same, but will not cache the contents.
132 * SAVE_SESSION - saves the current state of the desktop, which include
133 * all running applications, all their hints (geometry,
134 * position on screen, workspace they live on, the dock
135 * or clip from where they were launched, and
136 * if minimized, shaded or hidden. Also saves the current
137 * workspace the user is on. All will be restored on every
138 * start of windowmaker until another SAVE_SESSION or
139 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
140 * WindowMaker domain file, then saving is automatically
141 * done on every windowmaker exit, overwriting any
142 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
143 * dock state now.
144 * CLEAR_SESSION - clears any previous saved session. This will not have
145 * any effect if SaveSessionOnExit is True.
149 #define M_QUICK 1
151 /* menu commands */
153 static void
154 execCommand(WMenu *menu, WMenuEntry *entry)
156 char *cmdline;
158 cmdline = ExpandOptions(menu->frame->screen_ptr, (char*)entry->clientdata);
160 XGrabPointer(dpy, menu->frame->screen_ptr->root_win, True, 0,
161 GrabModeAsync, GrabModeAsync, None, wCursor[WCUR_WAIT],
162 CurrentTime);
163 XSync(dpy, 0);
165 if (cmdline) {
166 ExecuteShellCommand(menu->frame->screen_ptr, cmdline);
167 wfree(cmdline);
169 XUngrabPointer(dpy, CurrentTime);
170 XSync(dpy, 0);
174 static void
175 exitCommand(WMenu *menu, WMenuEntry *entry)
177 static int inside = 0;
178 int result;
180 /* prevent reentrant calls */
181 if (inside)
182 return;
183 inside = 1;
185 #define R_CANCEL 0
186 #define R_EXIT 1
188 result = R_CANCEL;
190 if ((long)entry->clientdata==M_QUICK) {
191 result = R_EXIT;
192 } else {
193 int r, oldSaveSessionFlag;
195 oldSaveSessionFlag = wPreferences.save_session_on_exit;
196 r = wExitDialog(menu->frame->screen_ptr, _("Exit"),
197 _("Exit window manager?"),
198 _("Exit"), _("Cancel"), NULL);
200 if (r==WAPRDefault) {
201 result = R_EXIT;
202 } else if (r==WAPRAlternate) {
203 /* Don't modify the "save session on exit" flag if the
204 * user canceled the operation. */
205 wPreferences.save_session_on_exit = oldSaveSessionFlag;
208 if (result==R_EXIT) {
209 #ifdef DEBUG
210 printf("Exiting WindowMaker.\n");
211 #endif
212 Shutdown(WSExitMode);
214 #undef R_EXIT
215 #undef R_CANCEL
216 inside = 0;
220 static void
221 shutdownCommand(WMenu *menu, WMenuEntry *entry)
223 static int inside = 0;
224 int result;
226 /* prevent reentrant calls */
227 if (inside)
228 return;
229 inside = 1;
231 #define R_CANCEL 0
232 #define R_CLOSE 1
233 #define R_KILL 2
236 result = R_CANCEL;
237 if ((long)entry->clientdata==M_QUICK)
238 result = R_CLOSE;
239 else {
240 #ifdef XSMP_ENABLED
241 if (wSessionIsManaged()) {
242 int r;
244 r = wMessageDialog(menu->frame->screen_ptr,
245 _("Close X session"),
246 _("Close Window System session?\n"
247 "Kill might close applications with unsaved data."),
248 _("Close"), _("Kill"), _("Cancel"));
249 if (r==WAPRDefault)
250 result = R_CLOSE;
251 else if (r==WAPRAlternate)
252 result = R_KILL;
253 } else
254 #endif
256 int r, oldSaveSessionFlag;
258 oldSaveSessionFlag = wPreferences.save_session_on_exit;
260 r = wExitDialog(menu->frame->screen_ptr,
261 _("Kill X session"),
262 _("Kill Window System session?\n"
263 "(all applications will be closed)"),
264 _("Kill"), _("Cancel"), NULL);
265 if (r==WAPRDefault) {
266 result = R_KILL;
267 } else if (r==WAPRAlternate) {
268 /* Don't modify the "save session on exit" flag if the
269 * user canceled the operation. */
270 wPreferences.save_session_on_exit = oldSaveSessionFlag;
275 if (result!=R_CANCEL) {
276 #ifdef XSMP_ENABLED
277 if (result == R_CLOSE) {
278 Shutdown(WSLogoutMode);
279 } else
280 #endif /* XSMP_ENABLED */
282 Shutdown(WSKillMode);
285 #undef R_CLOSE
286 #undef R_CANCEL
287 #undef R_KILL
288 inside = 0;
292 static void
293 restartCommand(WMenu *menu, WMenuEntry *entry)
295 Shutdown(WSRestartPreparationMode);
296 Restart((char*)entry->clientdata, False);
297 Restart(NULL, True);
301 static void
302 refreshCommand(WMenu *menu, WMenuEntry *entry)
304 wRefreshDesktop(menu->frame->screen_ptr);
308 static void
309 arrangeIconsCommand(WMenu *menu, WMenuEntry *entry)
311 wArrangeIcons(menu->frame->screen_ptr, True);
314 static void
315 showAllCommand(WMenu *menu, WMenuEntry *entry)
317 wShowAllWindows(menu->frame->screen_ptr);
320 static void
321 hideOthersCommand(WMenu *menu, WMenuEntry *entry)
323 wHideOtherApplications(menu->frame->screen_ptr->focused_window);
327 static void
328 saveSessionCommand(WMenu *menu, WMenuEntry *entry)
330 if (!wPreferences.save_session_on_exit)
331 wSessionSaveState(menu->frame->screen_ptr);
333 wScreenSaveState(menu->frame->screen_ptr);
337 static void
338 clearSessionCommand(WMenu *menu, WMenuEntry *entry)
340 wSessionClearState(menu->frame->screen_ptr);
344 static void
345 infoPanelCommand(WMenu *menu, WMenuEntry *entry)
347 wShowInfoPanel(menu->frame->screen_ptr);
351 static void
352 legalPanelCommand(WMenu *menu, WMenuEntry *entry)
354 wShowLegalPanel(menu->frame->screen_ptr);
358 /********************************************************************/
361 static char*
362 getLocalizedMenuFile(char *menu)
364 char *buffer;
365 char *ptr;
366 int len;
368 if (!Locale)
369 return NULL;
371 len = strlen(menu)+strlen(Locale)+8;
372 buffer = wmalloc(len);
374 /* try menu.locale_name */
375 snprintf(buffer, len, "%s.%s", menu, Locale);
376 if (access(buffer, F_OK)==0) {
377 return buffer;
379 /* check if it is in the form aa_bb.encoding and check for aa_bb */
380 ptr = strchr(Locale, '.');
381 if (ptr) {
382 *ptr = 0;
383 if (access(buffer, F_OK)==0) {
384 return buffer;
387 /* now check for aa */
388 ptr = strchr(buffer, '_');
389 if (ptr) {
390 *ptr = 0;
391 if (access(buffer, F_OK)==0) {
392 return buffer;
396 return NULL;
400 static void
401 raiseMenus(WMenu *menu)
403 int i;
405 if (menu->flags.mapped) {
406 wRaiseFrame(menu->frame->core);
408 for (i=0; i<menu->cascade_no; i++) {
409 if (menu->cascades[i])
410 raiseMenus(menu->cascades[i]);
416 Bool
417 wRootMenuPerformShortcut(XEvent *event)
419 WScreen *scr = wScreenForRootWindow(event->xkey.root);
420 Shortcut *ptr;
421 int modifiers;
422 int done = 0;
423 Window dummy;
424 int foo;
426 /* ignore CapsLock */
427 modifiers = event->xkey.state & ValidModMask;
429 for (ptr = shortcutList; ptr!=NULL; ptr = ptr->next) {
430 if (ptr->keycode==0 || ptr->menu->menu->screen_ptr!=scr)
431 continue;
433 if (ptr->keycode==event->xkey.keycode && ptr->modifier==modifiers) {
434 (*ptr->entry->callback)(ptr->menu, ptr->entry);
435 done = True;
439 return done;
443 void
444 wRootMenuBindShortcuts(Window window)
446 Shortcut *ptr;
448 ptr = shortcutList;
449 while (ptr) {
450 if (ptr->modifier!=AnyModifier) {
451 XGrabKey(dpy, ptr->keycode, ptr->modifier|LockMask,
452 window, True, GrabModeAsync, GrabModeAsync);
453 #ifdef NUMLOCK_HACK
454 wHackedGrabKey(ptr->keycode, ptr->modifier,
455 window, True, GrabModeAsync, GrabModeAsync);
456 #endif
458 XGrabKey(dpy, ptr->keycode, ptr->modifier, window, True,
459 GrabModeAsync, GrabModeAsync);
460 ptr = ptr->next;
465 static void
466 rebindKeygrabs(WScreen *scr)
468 WWindow *wwin;
470 wwin = scr->focused_window;
472 while (wwin!=NULL) {
473 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->frame->core->window);
475 if (!WFLAGP(wwin, no_bind_keys)) {
476 wWindowSetKeyGrabs(wwin);
478 wwin = wwin->prev;
483 static void
484 removeShortcutsForMenu(WMenu *menu)
486 Shortcut *ptr, *tmp;
487 Shortcut *newList = NULL;
489 ptr = shortcutList;
490 while (ptr!=NULL) {
491 tmp = ptr->next;
492 if (ptr->menu == menu) {
493 wfree(ptr);
494 } else {
495 ptr->next = newList;
496 newList = ptr;
498 ptr = tmp;
500 shortcutList = newList;
501 menu->menu->screen_ptr->flags.root_menu_changed_shortcuts = 1;
505 static Bool
506 addShortcut(char *file, char *shortcutDefinition, WMenu *menu,
507 WMenuEntry *entry)
509 Shortcut *ptr;
510 KeySym ksym;
511 char *k;
512 char buf[128], *b;
514 ptr = wmalloc(sizeof(Shortcut));
516 strcpy(buf, shortcutDefinition);
517 b = (char*)buf;
519 /* get modifiers */
520 ptr->modifier = 0;
521 while ((k = strchr(b, '+'))!=NULL) {
522 int mod;
524 *k = 0;
525 mod = wXModifierFromKey(b);
526 if (mod<0) {
527 wwarning(_("%s:invalid key modifier \"%s\""), file, b);
528 wfree(ptr);
529 return False;
531 ptr->modifier |= mod;
533 b = k+1;
536 /* get key */
537 ksym = XStringToKeysym(b);
539 if (ksym==NoSymbol) {
540 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
541 file, shortcutDefinition, entry->text);
542 wfree(ptr);
543 return False;
546 ptr->keycode = XKeysymToKeycode(dpy, ksym);
547 if (ptr->keycode==0) {
548 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file,
549 shortcutDefinition, entry->text);
550 wfree(ptr);
551 return False;
554 ptr->menu = menu;
555 ptr->entry = entry;
557 ptr->next = shortcutList;
558 shortcutList = ptr;
560 menu->menu->screen_ptr->flags.root_menu_changed_shortcuts = 1;
562 return True;
566 /*******************************/
568 static char*
569 cropline(char *line)
571 char *end;
573 if (strlen(line)==0)
574 return line;
576 end = &(line[strlen(line)])-1;
577 while (isspace(*line) && *line!=0) line++;
578 while (end>line && isspace(*end)) {
579 *end=0;
580 end--;
582 return line;
586 static char*
587 next_token(char *line, char **next)
589 char *tmp, c;
590 char *ret;
592 *next = NULL;
593 while (*line==' ' || *line=='\t') line++;
595 tmp = line;
597 if (*tmp=='"') {
598 tmp++; line++;
599 while (*tmp!=0 && *tmp!='"') tmp++;
600 if (*tmp!='"') {
601 wwarning(_("%s: unmatched '\"' in menu file"), line);
602 return NULL;
604 } else {
605 do {
606 if (*tmp=='\\')
607 tmp++;
609 if (*tmp!=0)
610 tmp++;
612 } while (*tmp!=0 && *tmp!=' ' && *tmp!='\t');
615 c = *tmp;
616 *tmp = 0;
617 ret = wstrdup(line);
618 *tmp = c;
620 if (c==0)
621 return ret;
622 else
623 tmp++;
625 /* skip blanks */
626 while (*tmp==' ' || *tmp=='\t') tmp++;
628 if (*tmp!=0)
629 *next = tmp;
631 return ret;
635 static void
636 separateCommand(char *line, char ***file, char **command)
638 char *token, *tmp = line;
639 WMArray *array = WMCreateArray(4);
640 int count, i;
642 *file = NULL;
643 *command = NULL;
644 do {
645 token = next_token(tmp, &tmp);
646 if (token) {
647 if (strcmp(token, "WITH")==0) {
648 if (tmp!=NULL && *tmp!=0)
649 *command = wstrdup(tmp);
650 else
651 wwarning(_("%s: missing command"), line);
652 break;
654 WMAddToArray(array, token);
656 } while (token!=NULL && tmp!=NULL);
658 count = WMGetArrayItemCount(array);
659 if (count>0) {
660 *file = wmalloc(sizeof(char*)*(count+1));
661 (*file)[count] = NULL;
662 for (i = 0; i < count; i++) {
663 (*file)[i] = WMGetFromArray(array, i);
666 WMFreeArray(array);
670 static void
671 constructMenu(WMenu *menu, WMenuEntry *entry)
673 WMenu *submenu;
674 struct stat stat_buf;
675 char **path;
676 char *cmd;
677 char *lpath = NULL;
678 int i, first=-1;
679 time_t last=0;
681 separateCommand((char*)entry->clientdata, &path, &cmd);
682 if (path == NULL || *path==NULL || **path==0) {
683 wwarning(_("invalid OPEN_MENU specification: %s"),
684 (char*)entry->clientdata);
685 return;
688 if (path[0][0]=='|') {
689 /* pipe menu */
691 if (!menu->cascades[entry->cascade] ||
692 menu->cascades[entry->cascade]->timestamp == 0) {
693 /* parse pipe */
695 submenu = readMenuPipe(menu->frame->screen_ptr, path);
697 if(submenu != NULL) {
698 if (path[0][1] == '|')
699 submenu->timestamp = 0;
700 else
701 submenu->timestamp = 1; /* there's no automatic reloading */
703 } else {
704 submenu = NULL;
707 } else {
708 i=0;
709 while(path[i] != NULL) {
710 char *tmp;
712 if (strcmp(path[i], "-noext")==0) {
713 i++;
714 continue;
717 tmp = wexpandpath(path[i]);
718 wfree(path[i]);
719 lpath = getLocalizedMenuFile(tmp);
720 if (lpath) {
721 wfree(tmp);
722 path[i] = lpath;
723 lpath = NULL;
724 } else {
725 path[i] = tmp;
728 if (stat(path[i], &stat_buf)==0) {
729 if (last < stat_buf.st_mtime)
730 last = stat_buf.st_mtime;
731 if (first<0)
732 first=i;
733 } else {
734 wsyserror(_("%s:could not stat menu"), path[i]);
735 /*goto finish;*/
738 i++;
741 if (first < 0) {
742 wsyserror(_("%s:could not stat menu:%s"), "OPEN_MENU",
743 (char*)entry->clientdata);
744 goto finish;
746 stat(path[first], &stat_buf);
747 if (!menu->cascades[entry->cascade]
748 || menu->cascades[entry->cascade]->timestamp < last) {
750 if (S_ISDIR(stat_buf.st_mode)) {
751 /* menu directory */
752 submenu = readMenuDirectory(menu->frame->screen_ptr,
753 entry->text, path, cmd);
754 if (submenu)
755 submenu->timestamp = last;
756 } else if (S_ISREG(stat_buf.st_mode)) {
757 /* menu file */
759 if (cmd || path[1])
760 wwarning(_("too many parameters in OPEN_MENU: %s"),
761 (char*)entry->clientdata);
763 submenu = readMenuFile(menu->frame->screen_ptr, path[first]);
764 if (submenu)
765 submenu->timestamp = stat_buf.st_mtime;
766 } else {
767 submenu = NULL;
769 } else {
770 submenu = NULL;
774 if (submenu) {
775 wMenuEntryRemoveCascade(menu, entry);
776 wMenuEntrySetCascade(menu, entry, submenu);
779 finish:
780 i = 0;
781 while (path[i]!=NULL)
782 wfree(path[i++]);
783 wfree(path);
784 if (cmd)
785 wfree(cmd);
789 static void
790 cleanupWorkspaceMenu(WMenu *menu)
792 if (menu->frame->screen_ptr->workspace_menu == menu)
793 menu->frame->screen_ptr->workspace_menu = NULL;
797 static WMenuEntry*
798 addWorkspaceMenu(WScreen *scr, WMenu *menu, char *title)
800 WMenu *wsmenu;
801 WMenuEntry *entry;
803 if (scr->flags.added_workspace_menu) {
804 wwarning(_("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
805 return NULL;
806 } else {
807 scr->flags.added_workspace_menu = 1;
809 wsmenu = wWorkspaceMenuMake(scr, True);
810 wsmenu->on_destroy = cleanupWorkspaceMenu;
812 scr->workspace_menu = wsmenu;
813 entry = wMenuAddCallback(menu, title, NULL, NULL);
814 wMenuEntrySetCascade(menu, entry, wsmenu);
816 wWorkspaceMenuUpdate(scr, wsmenu);
818 return entry;
822 static void
823 cleanupWindowsMenu(WMenu *menu)
825 if (menu->frame->screen_ptr->switch_menu == menu)
826 menu->frame->screen_ptr->switch_menu = NULL;
830 static WMenuEntry*
831 addWindowsMenu(WScreen *scr, WMenu *menu, char *title)
833 WMenu *wwmenu;
834 WWindow *wwin;
835 WMenuEntry *entry;
837 if (scr->flags.added_windows_menu) {
838 wwarning(_("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
839 return NULL;
840 } else {
841 scr->flags.added_windows_menu = 1;
843 wwmenu = wMenuCreate(scr, _("Window List"), False);
844 wwmenu->on_destroy = cleanupWindowsMenu;
845 scr->switch_menu = wwmenu;
846 wwin = scr->focused_window;
847 while (wwin) {
848 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
850 wwin = wwin->prev;
852 entry = wMenuAddCallback(menu, title, NULL, NULL);
853 wMenuEntrySetCascade(menu, entry, wwmenu);
855 return entry;
859 static WMenuEntry*
860 addMenuEntry(WMenu *menu, char *title, char *shortcut, char *command,
861 char *params, char *file_name)
863 WScreen *scr;
864 WMenuEntry *entry = NULL;
865 Bool shortcutOk = False;
867 if (!menu)
868 return NULL;
869 scr = menu->frame->screen_ptr;
870 if (strcmp(command, "OPEN_MENU")==0) {
871 if (!params) {
872 wwarning(_("%s:missing parameter for menu command \"%s\""),
873 file_name, command);
874 } else {
875 WMenu *dummy;
876 char *path;
878 path = wfindfile(DEF_CONFIG_PATHS, params);
879 if (!path) {
880 path = wstrdup(params);
882 dummy = wMenuCreate(scr, title, False);
883 dummy->on_destroy = removeShortcutsForMenu;
884 entry = wMenuAddCallback(menu, title, constructMenu, path);
885 entry->free_cdata = free;
886 wMenuEntrySetCascade(menu, entry, dummy);
888 } else if (strcmp(command, "EXEC")==0) {
889 if (!params)
890 wwarning(_("%s:missing parameter for menu command \"%s\""),
891 file_name, command);
892 else {
893 entry = wMenuAddCallback(menu, title, execCommand,
894 wstrconcat("exec ", params));
895 entry->free_cdata = free;
896 shortcutOk = True;
898 } else if (strcmp(command, "SHEXEC")==0) {
899 if (!params)
900 wwarning(_("%s:missing parameter for menu command \"%s\""),
901 file_name, command);
902 else {
903 entry = wMenuAddCallback(menu, title, execCommand,
904 wstrdup(params));
905 entry->free_cdata = free;
906 shortcutOk = True;
908 } else if (strcmp(command, "EXIT")==0) {
910 if (params && strcmp(params, "QUICK")==0)
911 entry = wMenuAddCallback(menu, title, exitCommand, (void*)M_QUICK);
912 else
913 entry = wMenuAddCallback(menu, title, exitCommand, NULL);
915 shortcutOk = True;
916 } else if (strcmp(command, "SHUTDOWN")==0) {
918 if (params && strcmp(params, "QUICK")==0)
919 entry = wMenuAddCallback(menu, title, shutdownCommand,
920 (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,
951 params ? wstrdup(params) : NULL);
952 entry->free_cdata = free;
953 shortcutOk = True;
954 } else if (strcmp(command, "SAVE_SESSION")==0) {
955 entry = wMenuAddCallback(menu, title, saveSessionCommand, NULL);
957 shortcutOk = True;
958 } else if (strcmp(command, "CLEAR_SESSION")==0) {
959 entry = wMenuAddCallback(menu, title, clearSessionCommand, NULL);
960 shortcutOk = True;
961 } else if (strcmp(command, "INFO_PANEL")==0) {
962 entry = wMenuAddCallback(menu, title, infoPanelCommand, NULL);
963 shortcutOk = True;
964 } else if (strcmp(command, "LEGAL_PANEL")==0) {
965 entry = wMenuAddCallback(menu, title, legalPanelCommand, NULL);
966 shortcutOk = True;
967 } else {
968 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name,
969 command);
971 return NULL;
974 if (shortcut && entry) {
975 if (!shortcutOk) {
976 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name,
977 title);
978 } else {
979 if (addShortcut(file_name, shortcut, menu, entry)) {
981 entry->rtext = GetShortcutString(shortcut);
983 entry->rtext = wstrdup(shortcut);
989 return entry;
994 /******************* Menu Configuration From File *******************/
996 static void
997 separateline(char *line, char *title, char *command, char *parameter,
998 char *shortcut)
1000 int l, i;
1002 l = strlen(line);
1004 *title = 0;
1005 *command = 0;
1006 *parameter = 0;
1007 *shortcut = 0;
1008 /* get the title */
1009 while (isspace(*line) && (*line!=0)) line++;
1010 if (*line=='"') {
1011 line++;
1012 i=0;
1013 while (line[i]!='"' && (line[i]!=0)) i++;
1014 if (line[i]!='"')
1015 return;
1016 } else {
1017 i=0;
1018 while (!isspace(line[i]) && (line[i]!=0)) i++;
1020 strncpy(title, line, i);
1021 title[i++]=0;
1022 line+=i;
1024 /* get the command or shortcut keyword */
1025 while (isspace(*line) && (*line!=0)) line++;
1026 if (*line==0)
1027 return;
1028 i=0;
1029 while (!isspace(line[i]) && (line[i]!=0)) i++;
1030 strncpy(command, line, i);
1031 command[i++]=0;
1032 line+=i;
1034 if (strcmp(command, "SHORTCUT")==0) {
1035 /* get the shortcut key */
1036 while (isspace(*line) && (*line!=0)) line++;
1037 if (*line=='"') {
1038 line++;
1039 i=0;
1040 while (line[i]!='"' && (line[i]!=0)) i++;
1041 if (line[i]!='"')
1042 return;
1043 } else {
1044 i=0;
1045 while (!isspace(line[i]) && (line[i]!=0)) i++;
1047 strncpy(shortcut, line, i);
1048 shortcut[i++]=0;
1049 line+=i;
1051 *command=0;
1053 /* get the command */
1054 while (isspace(*line) && (*line!=0)) line++;
1055 if (*line==0)
1056 return;
1057 i=0;
1058 while (!isspace(line[i]) && (line[i]!=0)) i++;
1059 strncpy(command, line, i);
1060 command[i++]=0;
1061 line+=i;
1064 /* get the parameters */
1065 while (isspace(*line) && (*line!=0)) line++;
1066 if (*line==0)
1067 return;
1069 if (*line=='"') {
1070 line++;
1071 l = 0;
1072 while (line[l]!=0 && line[l]!='"') {
1073 parameter[l] = line[l];
1074 l++;
1076 parameter[l] = 0;
1077 return;
1080 l = strlen(line);
1081 while (isspace(line[l]) && (l>0)) l--;
1082 strncpy(parameter, line, l);
1083 parameter[l]=0;
1087 static WMenu*
1088 parseCascade(WScreen *scr, WMenu *menu, FILE *file, char *file_name)
1090 char linebuf[MAXLINE];
1091 char elinebuf[MAXLINE];
1092 char title[MAXLINE];
1093 char command[MAXLINE];
1094 char shortcut[MAXLINE];
1095 char params[MAXLINE];
1096 char *line;
1098 while (!IsEof(file)) {
1099 int lsize, ok;
1101 ok = 0;
1102 fgets(linebuf, MAXLINE, file);
1103 line = cropline(linebuf);
1104 lsize = strlen(line);
1105 do {
1106 if (line[lsize-1]=='\\') {
1107 char *line2;
1108 int lsize2;
1109 fgets(elinebuf, MAXLINE, file);
1110 line2=cropline(elinebuf);
1111 lsize2=strlen(line2);
1112 if (lsize2+lsize>MAXLINE) {
1113 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1114 file_name, line);
1115 ok=2;
1116 } else {
1117 line[lsize-1]=0;
1118 lsize+=lsize2-1;
1119 strcat(line, line2);
1121 } else {
1122 ok=1;
1124 } while (!ok && !IsEof(file));
1125 if (ok==2)
1126 continue;
1128 if (line[0]==0 || line[0]=='#' || (line[0]=='/' && line[1]=='/'))
1129 continue;
1132 separateline(line, title, command, params, shortcut);
1134 if (!command[0]) {
1135 wwarning(_("%s:missing command in menu config: %s"), file_name,
1136 line);
1137 goto error;
1140 if (strcasecmp(command, "MENU")==0) {
1141 WMenu *cascade;
1143 /* start submenu */
1145 cascade = wMenuCreate(scr, title, False);
1146 cascade->on_destroy = removeShortcutsForMenu;
1147 if (parseCascade(scr, cascade, file, file_name)==NULL) {
1148 wMenuDestroy(cascade, True);
1149 } else {
1150 wMenuEntrySetCascade(menu,
1151 wMenuAddCallback(menu, title, NULL, NULL),
1152 cascade);
1154 } else if (strcasecmp(command, "END")==0) {
1155 /* end of menu */
1156 return menu;
1158 } else {
1159 /* normal items */
1160 addMenuEntry(menu, title, shortcut[0] ? shortcut : NULL, command,
1161 params[0] ? params : NULL, file_name);
1165 wwarning(_("%s:syntax error in menu file:END declaration missing"),
1166 file_name);
1167 return menu;
1169 error:
1170 return menu;
1174 static WMenu*
1175 readMenuFile(WScreen *scr, char *file_name)
1177 WMenu *menu=NULL;
1178 FILE *file = NULL;
1179 char linebuf[MAXLINE];
1180 char title[MAXLINE];
1181 char shortcut[MAXLINE];
1182 char command[MAXLINE];
1183 char params[MAXLINE];
1184 char *line;
1185 #ifdef USECPP
1186 char *args;
1187 int cpp = 0;
1188 #endif
1190 #ifdef USECPP
1191 if (!wPreferences.flags.nocpp) {
1192 args = MakeCPPArgs(file_name);
1193 if (!args) {
1194 wwarning(_("could not make arguments for menu file preprocessor"));
1195 } else {
1196 snprintf(command, sizeof(command), "%s %s %s",
1197 CPP_PATH, args, file_name);
1198 wfree(args);
1199 file = popen(command, "r");
1200 if (!file) {
1201 wsyserror(_("%s:could not open/preprocess menu file"),
1202 file_name);
1203 } else {
1204 cpp = 1;
1208 #endif /* USECPP */
1210 if (!file) {
1211 file = fopen(file_name, "r");
1212 if (!file) {
1213 wsyserror(_("%s:could not open menu file"), file_name);
1214 return NULL;
1218 while (!IsEof(file)) {
1219 if (!fgets(linebuf, MAXLINE, file))
1220 break;
1221 line = cropline(linebuf);
1222 if (line[0]==0 || line[0]=='#' || (line[0]=='/' && line[1]=='/'))
1223 continue;
1225 separateline(line, title, command, params, shortcut);
1227 if (!command[0]) {
1228 wwarning(_("%s:missing command in menu config: %s"), file_name,
1229 line);
1230 break;
1232 if (strcasecmp(command, "MENU")==0) {
1233 menu = wMenuCreate(scr, title, True);
1234 menu->on_destroy = removeShortcutsForMenu;
1235 if (!parseCascade(scr, menu, file, file_name)) {
1236 wMenuDestroy(menu, True);
1238 break;
1239 } else {
1240 wwarning(_("%s:invalid menu file. MENU command is missing"),
1241 file_name);
1242 break;
1246 #ifdef CPP
1247 if (cpp) {
1248 if (pclose(file)==-1) {
1249 wsyserror(_("error reading preprocessed menu data"));
1251 } else {
1252 fclose(file);
1254 #else
1255 fclose(file);
1256 #endif
1258 return menu;
1261 /************ Menu Configuration From Pipe *************/
1263 static WMenu*
1264 readMenuPipe(WScreen *scr, char **file_name)
1266 WMenu *menu=NULL;
1267 FILE *file = NULL;
1268 char linebuf[MAXLINE];
1269 char title[MAXLINE];
1270 char command[MAXLINE];
1271 char params[MAXLINE];
1272 char shortcut[MAXLINE];
1273 char *line;
1274 char *filename;
1275 char flat_file[MAXLINE];
1276 int i;
1277 #ifdef USECPP
1278 char *args;
1279 int cpp = 0;
1280 #endif
1282 flat_file[0] = '\0';
1284 for(i=0; file_name[i]!=NULL; i++) {
1285 strcat(flat_file, file_name[i]);
1286 strcat(flat_file, " ");
1288 filename = flat_file+1;
1290 #ifdef USECPP
1291 if (!wPreferences.flags.nocpp) {
1292 args = MakeCPPArgs(filename);
1293 if (!args) {
1294 wwarning(_("could not make arguments for menu file preprocessor"));
1295 } else {
1296 snprintf(command, sizeof(command), "%s | %s %s",
1297 filename, CPP_PATH, args);
1299 wfree(args);
1300 file = popen(command, "r");
1301 if (!file) {
1302 wsyserror(_("%s:could not open/preprocess menu file"), filename);
1303 } else {
1304 cpp = 1;
1309 #endif /* USECPP */
1311 if (!file) {
1312 file = popen(filename, "r");
1314 if (!file) {
1315 wsyserror(_("%s:could not open menu file"), filename);
1316 return NULL;
1320 while (!IsEof(file)) {
1321 if (!fgets(linebuf, MAXLINE, file))
1322 break;
1323 line = cropline(linebuf);
1324 if (line[0]==0 || line[0]=='#' || (line[0]=='/' && line[1]=='/'))
1325 continue;
1327 separateline(line, title, command, params, shortcut);
1329 if (!command[0]) {
1330 wwarning(_("%s:missing command in menu config: %s"), file_name,
1331 line);
1332 break;
1334 if (strcasecmp(command, "MENU")==0) {
1335 menu = wMenuCreate(scr, title, True);
1336 menu->on_destroy = removeShortcutsForMenu;
1337 if (!parseCascade(scr, menu, file, filename)) {
1338 wMenuDestroy(menu, True);
1340 break;
1341 } else {
1342 wwarning(_("%s:no title given for the root menu"), filename);
1343 break;
1347 pclose(file);
1349 return menu;
1354 typedef struct {
1355 char *name;
1356 int index;
1357 } dir_data;
1360 static int
1361 myCompare(const void *d1, const void *d2)
1363 dir_data *p1 = *(dir_data**)d1;
1364 dir_data *p2 = *(dir_data**)d2;
1366 return strcmp(p1->name, p2->name);
1370 /************ Menu Configuration From Directory *************/
1372 static Bool
1373 isFilePackage(char *file)
1375 int l;
1377 /* check if the extension indicates this file is a
1378 * file package. For now, only recognize .themed */
1380 l = strlen(file);
1382 if (l > 7 && strcmp(&(file[l-7]), ".themed")==0) {
1383 return True;
1384 } else {
1385 return False;
1390 static WMenu*
1391 readMenuDirectory(WScreen *scr, char *title, char **path, char *command)
1393 DIR *dir;
1394 struct dirent *dentry;
1395 struct stat stat_buf;
1396 WMenu *menu=NULL;
1397 char *buffer;
1398 WMArray *dirs = NULL, *files = NULL;
1399 WMArrayIterator iter;
1400 int length, i, have_space=0;
1401 dir_data *data;
1402 int stripExtension = 0;
1405 dirs = WMCreateArray(16);
1406 files = WMCreateArray(16);
1408 i=0;
1409 while (path[i]!=NULL) {
1410 if (strcmp(path[i], "-noext")==0) {
1411 stripExtension = 1;
1412 i++;
1413 continue;
1416 dir = opendir(path[i]);
1417 if (!dir) {
1418 i++;
1419 continue;
1422 while ((dentry = readdir(dir))) {
1424 if (strcmp(dentry->d_name, ".")==0 ||
1425 strcmp(dentry->d_name, "..")==0)
1426 continue;
1428 if (dentry->d_name[0] == '.')
1429 continue;
1431 buffer = malloc(strlen(path[i])+strlen(dentry->d_name)+4);
1432 if (!buffer) {
1433 wsyserror(_("out of memory while constructing directory menu %s"),
1434 path[i]);
1435 break;
1438 strcpy(buffer, path[i]);
1439 strcat(buffer, "/");
1440 strcat(buffer, dentry->d_name);
1442 if (stat(buffer, &stat_buf)!=0) {
1443 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1444 path[i], dentry->d_name);
1445 } else {
1446 Bool isFilePack = False;
1448 data = NULL;
1449 if (S_ISDIR(stat_buf.st_mode)
1450 && !(isFilePack = isFilePackage(dentry->d_name))) {
1452 /* access always returns success for user root */
1453 if (access(buffer, X_OK)==0) {
1454 /* Directory is accesible. Add to directory list */
1456 data = (dir_data*) wmalloc(sizeof(dir_data));
1457 data->name = wstrdup(dentry->d_name);
1458 data->index = i;
1460 WMAddToArray(dirs, data);
1462 } else if (S_ISREG(stat_buf.st_mode) || isFilePack) {
1463 /* Hack because access always returns X_OK success for user root */
1464 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1465 if ((command!=NULL && access(buffer, R_OK)==0) ||
1466 (command==NULL && access(buffer, X_OK)==0 &&
1467 (stat_buf.st_mode & S_IXANY))) {
1469 data = (dir_data*) wmalloc(sizeof(dir_data));
1470 data->name = wstrdup(dentry->d_name);
1471 data->index = i;
1473 WMAddToArray(files, data);
1477 wfree(buffer);
1480 closedir(dir);
1481 i++;
1484 if (!WMGetArrayItemCount(dirs) && !WMGetArrayItemCount(files)) {
1485 WMFreeArray(dirs);
1486 WMFreeArray(files);
1487 return NULL;
1490 WMSortArray(dirs, myCompare);
1491 WMSortArray(files, myCompare);
1493 menu = wMenuCreate(scr, title, False);
1494 menu->on_destroy = removeShortcutsForMenu;
1496 WM_ITERATE_ARRAY(dirs, data, iter) {
1497 /* New directory. Use same OPEN_MENU command that was used
1498 * for the current directory. */
1499 length = strlen(path[data->index])+strlen(data->name)+6;
1500 if (stripExtension)
1501 length += 7;
1502 if (command)
1503 length += strlen(command) + 6;
1504 buffer = malloc(length);
1505 if (!buffer) {
1506 wsyserror(_("out of memory while constructing directory menu %s"),
1507 path[data->index]);
1508 break;
1511 buffer[0] = '\0';
1512 if (stripExtension)
1513 strcat(buffer, "-noext ");
1515 have_space = strchr(path[data->index], ' ')!=NULL ||
1516 strchr(data->name, ' ')!=NULL;
1518 if (have_space)
1519 strcat(buffer, "\"");
1520 strcat(buffer, path[data->index]);
1522 strcat(buffer, "/");
1523 strcat(buffer, data->name);
1524 if (have_space)
1525 strcat(buffer, "\"");
1526 if (command) {
1527 strcat(buffer, " WITH ");
1528 strcat(buffer, command);
1531 addMenuEntry(menu, data->name, NULL, "OPEN_MENU", buffer, path[data->index]);
1533 wfree(buffer);
1534 if (data->name)
1535 wfree(data->name);
1536 wfree(data);
1539 WM_ITERATE_ARRAY(files, data, iter) {
1540 /* executable: add as entry */
1541 length = strlen(path[data->index])+strlen(data->name)+6;
1542 if (command)
1543 length += strlen(command);
1545 buffer = malloc(length);
1546 if (!buffer) {
1547 wsyserror(_("out of memory while constructing directory menu %s"),
1548 path[data->index]);
1549 break;
1552 have_space = strchr(path[data->index], ' ')!=NULL ||
1553 strchr(data->name, ' ')!=NULL;
1554 if (command!=NULL) {
1555 strcpy(buffer, command);
1556 strcat(buffer, " ");
1557 if (have_space)
1558 strcat(buffer, "\"");
1559 strcat(buffer, path[data->index]);
1560 } else {
1561 if (have_space) {
1562 buffer[0] = '"';
1563 buffer[1] = 0;
1564 strcat(buffer, path[data->index]);
1565 } else {
1566 strcpy(buffer, path[data->index]);
1569 strcat(buffer, "/");
1570 strcat(buffer, data->name);
1571 if (have_space)
1572 strcat(buffer, "\"");
1574 if (stripExtension) {
1575 char *ptr = strrchr(data->name, '.');
1576 if (ptr && ptr!=data->name)
1577 *ptr = 0;
1579 addMenuEntry(menu, data->name, NULL, "SHEXEC", buffer, path[data->index]);
1581 wfree(buffer);
1582 if (data->name)
1583 wfree(data->name);
1584 wfree(data);
1587 WMFreeArray(files);
1588 WMFreeArray(dirs);
1590 return menu;
1594 /************ Menu Configuration From WMRootMenu *************/
1596 static WMenu*
1597 makeDefaultMenu(WScreen *scr)
1599 WMenu *menu=NULL;
1601 menu = wMenuCreate(scr, _("Commands"), True);
1602 wMenuAddCallback(menu, "XTerm", execCommand, "xterm");
1603 wMenuAddCallback(menu, "rxvt", execCommand, "rxvt");
1604 wMenuAddCallback(menu, _("Restart"), restartCommand, NULL);
1605 wMenuAddCallback(menu, _("Exit..."), exitCommand, NULL);
1606 return menu;
1614 *----------------------------------------------------------------------
1615 * configureMenu--
1616 * Reads root menu configuration from defaults database.
1618 *----------------------------------------------------------------------
1620 static WMenu*
1621 configureMenu(WScreen *scr, WMPropList *definition)
1623 WMenu *menu = NULL;
1624 WMPropList *elem;
1625 int i, count;
1626 WMPropList *title, *command, *params;
1627 char *tmp, *mtitle;
1630 if (WMIsPLString(definition)) {
1631 struct stat stat_buf;
1632 char *path = NULL;
1633 Bool menu_is_default = False;
1635 /* menu definition is a string. Probably a path, so parse the file */
1637 tmp = wexpandpath(WMGetFromPLString(definition));
1639 path = getLocalizedMenuFile(tmp);
1641 if (!path)
1642 path = wfindfile(DEF_CONFIG_PATHS, tmp);
1644 if (!path) {
1645 path = wfindfile(DEF_CONFIG_PATHS, DEF_MENU_FILE);
1646 menu_is_default = True;
1649 if (!path) {
1650 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"),
1651 tmp);
1652 wfree(tmp);
1653 return NULL;
1656 if (stat(path, &stat_buf)<0) {
1657 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path);
1658 wfree(path);
1659 wfree(tmp);
1660 return NULL;
1663 if (!scr->root_menu || stat_buf.st_mtime > scr->root_menu->timestamp
1664 /* if the pointer in WMRootMenu has changed */
1665 || WDRootMenu->timestamp > scr->root_menu->timestamp) {
1667 if (menu_is_default) {
1668 wwarning(_("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1669 path);
1672 menu = readMenuFile(scr, path);
1673 if (menu)
1674 menu->timestamp = WMAX(stat_buf.st_mtime, WDRootMenu->timestamp);
1675 } else {
1676 menu = NULL;
1678 wfree(path);
1679 wfree(tmp);
1681 return menu;
1684 count = WMGetPropListItemCount(definition);
1685 if (count==0)
1686 return NULL;
1688 elem = WMGetFromPLArray(definition, 0);
1689 if (!WMIsPLString(elem)) {
1690 tmp = WMGetPropListDescription(elem, False);
1691 wwarning(_("%s:format error in root menu configuration \"%s\""),
1692 "WMRootMenu", tmp);
1693 wfree(tmp);
1694 return NULL;
1696 mtitle = WMGetFromPLString(elem);
1698 menu = wMenuCreate(scr, mtitle, False);
1699 menu->on_destroy = removeShortcutsForMenu;
1701 #ifdef GLOBAL_SUBMENU_FILE
1703 WMenu *submenu;
1704 WMenuEntry *mentry;
1706 submenu = readMenuFile(scr, GLOBAL_SUBMENU_FILE);
1708 if (submenu) {
1709 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL, NULL);
1710 wMenuEntrySetCascade(menu, mentry, submenu);
1713 #endif
1715 for (i=1; i<count; i++) {
1716 elem = WMGetFromPLArray(definition, i);
1717 #if 0
1718 if (WMIsPLString(elem)) {
1719 char *file;
1721 file = WMGetFromPLString(elem);
1724 #endif
1725 if (!WMIsPLArray(elem) || WMGetPropListItemCount(elem) < 2)
1726 goto error;
1728 if (WMIsPLArray(WMGetFromPLArray(elem,1))) {
1729 WMenu *submenu;
1730 WMenuEntry *mentry;
1732 /* submenu */
1733 submenu = configureMenu(scr, elem);
1734 if (submenu) {
1735 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL,
1736 NULL);
1737 wMenuEntrySetCascade(menu, mentry, submenu);
1739 } else {
1740 int idx = 0;
1741 WMPropList *shortcut;
1742 /* normal entry */
1744 title = WMGetFromPLArray(elem, idx++);
1745 shortcut = WMGetFromPLArray(elem, idx++);
1746 if (strcmp(WMGetFromPLString(shortcut), "SHORTCUT")==0) {
1747 shortcut = WMGetFromPLArray(elem, idx++);
1748 command = WMGetFromPLArray(elem, idx++);
1749 } else {
1750 command = shortcut;
1751 shortcut = NULL;
1753 params = WMGetFromPLArray(elem, idx++);
1755 if (!title || !command)
1756 goto error;
1758 addMenuEntry(menu, WMGetFromPLString(title),
1759 shortcut ? WMGetFromPLString(shortcut) : NULL,
1760 WMGetFromPLString(command),
1761 params ? WMGetFromPLString(params) : NULL, "WMRootMenu");
1763 continue;
1765 error:
1766 tmp = WMGetPropListDescription(elem, False);
1767 wwarning(_("%s:format error in root menu configuration \"%s\""),
1768 "WMRootMenu", tmp);
1769 wfree(tmp);
1772 return menu;
1783 *----------------------------------------------------------------------
1784 * OpenRootMenu--
1785 * Opens the root menu, parsing the menu configuration from the
1786 * defaults database.
1787 * If the menu is already mapped and is not sticked to the
1788 * root window, it will be unmapped.
1790 * Side effects:
1791 * The menu may be remade.
1793 * Notes:
1794 * Construction of OPEN_MENU entries are delayed to the moment the
1795 * user map's them.
1796 *----------------------------------------------------------------------
1798 void
1799 OpenRootMenu(WScreen *scr, int x, int y, int keyboard)
1801 WMenu *menu=NULL;
1802 WMPropList *definition;
1804 static WMPropList *domain=NULL;
1806 if (!domain) {
1807 domain = WMCreatePLString("WMRootMenu");
1811 scr->flags.root_menu_changed_shortcuts = 0;
1812 scr->flags.added_workspace_menu = 0;
1813 scr->flags.added_windows_menu = 0;
1815 if (scr->root_menu && scr->root_menu->flags.mapped) {
1816 menu = scr->root_menu;
1817 if (!menu->flags.buttoned) {
1818 wMenuUnmap(menu);
1819 } else {
1820 wRaiseFrame(menu->frame->core);
1822 if (keyboard)
1823 wMenuMapAt(menu, 0, 0, True);
1824 else
1825 wMenuMapCopyAt(menu, x-menu->frame->core->width/2, y);
1827 return;
1831 definition = WDRootMenu->dictionary;
1834 definition = PLGetDomain(domain);
1836 if (definition) {
1837 if (WMIsPLArray(definition)) {
1838 if (!scr->root_menu
1839 || WDRootMenu->timestamp > scr->root_menu->timestamp) {
1840 menu = configureMenu(scr, definition);
1841 if (menu)
1842 menu->timestamp = WDRootMenu->timestamp;
1844 } else
1845 menu = NULL;
1846 } else {
1847 menu = configureMenu(scr, definition);
1851 if (!menu) {
1852 /* menu hasn't changed or could not be read */
1853 if (!scr->root_menu) {
1854 wMessageDialog(scr, _("Error"),
1855 _("The applications menu could not be loaded. "
1856 "Look at the console output for a detailed "
1857 "description of the errors."),
1858 _("OK"), NULL, NULL);
1860 menu = makeDefaultMenu(scr);
1861 scr->root_menu = menu;
1863 menu = scr->root_menu;
1864 } else {
1865 /* new root menu */
1866 if (scr->root_menu) {
1867 wMenuDestroy(scr->root_menu, True);
1869 scr->root_menu = menu;
1871 if (menu) {
1872 int newx, newy;
1874 if (keyboard && x==0 && y==0) {
1875 newx = newy = 0;
1876 } else if (keyboard && x==scr->scr_width/2 && y==scr->scr_height/2) {
1877 newx = x - menu->frame->core->width/2;
1878 newy = y - menu->frame->core->height/2;
1879 } else {
1880 newx = x - menu->frame->core->width/2;
1881 newy = y;
1883 wMenuMapAt(menu, newx, newy, keyboard);
1886 if (scr->flags.root_menu_changed_shortcuts)
1887 rebindKeygrabs(scr);
1890 #endif /* !LITE */