fix for soemthing wrong with piped menus
[wmaker-crm.git] / src / rootmenu.c
blob2f242174bec317c2ae5d9a4b2855cc1d58502005
1 /* rootmenu.c- user defined menu
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997-2002 Alfredo K. Kojima
6 * Copyright (c) 1998-2002 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);
341 wScreenSaveState(menu->frame->screen_ptr);
345 static void
346 infoPanelCommand(WMenu *menu, WMenuEntry *entry)
348 wShowInfoPanel(menu->frame->screen_ptr);
352 static void
353 legalPanelCommand(WMenu *menu, WMenuEntry *entry)
355 wShowLegalPanel(menu->frame->screen_ptr);
359 /********************************************************************/
362 static char*
363 getLocalizedMenuFile(char *menu)
365 char *buffer;
366 char *ptr;
367 int len;
369 if (!Locale)
370 return NULL;
372 len = strlen(menu)+strlen(Locale)+8;
373 buffer = wmalloc(len);
375 /* try menu.locale_name */
376 snprintf(buffer, len, "%s.%s", menu, Locale);
377 if (access(buffer, F_OK)==0) {
378 return buffer;
380 /* check if it is in the form aa_bb.encoding and check for aa_bb */
381 ptr = strchr(Locale, '.');
382 if (ptr) {
383 *ptr = 0;
384 if (access(buffer, F_OK)==0) {
385 return buffer;
388 /* now check for aa */
389 ptr = strchr(buffer, '_');
390 if (ptr) {
391 *ptr = 0;
392 if (access(buffer, F_OK)==0) {
393 return buffer;
397 return NULL;
401 static void
402 raiseMenus(WMenu *menu)
404 int i;
406 if (menu->flags.mapped) {
407 wRaiseFrame(menu->frame->core);
409 for (i=0; i<menu->cascade_no; i++) {
410 if (menu->cascades[i])
411 raiseMenus(menu->cascades[i]);
417 Bool
418 wRootMenuPerformShortcut(XEvent *event)
420 WScreen *scr = wScreenForRootWindow(event->xkey.root);
421 Shortcut *ptr;
422 int modifiers;
423 int done = 0;
425 /* ignore CapsLock */
426 modifiers = event->xkey.state & ValidModMask;
428 for (ptr = shortcutList; ptr!=NULL; ptr = ptr->next) {
429 if (ptr->keycode==0 || ptr->menu->menu->screen_ptr!=scr)
430 continue;
432 if (ptr->keycode==event->xkey.keycode && ptr->modifier==modifiers) {
433 (*ptr->entry->callback)(ptr->menu, ptr->entry);
434 done = True;
438 return done;
442 void
443 wRootMenuBindShortcuts(Window window)
445 Shortcut *ptr;
447 ptr = shortcutList;
448 while (ptr) {
449 if (ptr->modifier!=AnyModifier) {
450 XGrabKey(dpy, ptr->keycode, ptr->modifier|LockMask,
451 window, True, GrabModeAsync, GrabModeAsync);
452 #ifdef NUMLOCK_HACK
453 wHackedGrabKey(ptr->keycode, ptr->modifier,
454 window, True, GrabModeAsync, GrabModeAsync);
455 #endif
457 XGrabKey(dpy, ptr->keycode, ptr->modifier, window, True,
458 GrabModeAsync, GrabModeAsync);
459 ptr = ptr->next;
464 static void
465 rebindKeygrabs(WScreen *scr)
467 WWindow *wwin;
469 wwin = scr->focused_window;
471 while (wwin!=NULL) {
472 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->frame->core->window);
474 if (!WFLAGP(wwin, no_bind_keys)) {
475 wWindowSetKeyGrabs(wwin);
477 wwin = wwin->prev;
482 static void
483 removeShortcutsForMenu(WMenu *menu)
485 Shortcut *ptr, *tmp;
486 Shortcut *newList = NULL;
488 ptr = shortcutList;
489 while (ptr!=NULL) {
490 tmp = ptr->next;
491 if (ptr->menu == menu) {
492 wfree(ptr);
493 } else {
494 ptr->next = newList;
495 newList = ptr;
497 ptr = tmp;
499 shortcutList = newList;
500 menu->menu->screen_ptr->flags.root_menu_changed_shortcuts = 1;
504 static Bool
505 addShortcut(char *file, char *shortcutDefinition, WMenu *menu,
506 WMenuEntry *entry)
508 Shortcut *ptr;
509 KeySym ksym;
510 char *k;
511 char buf[128], *b;
513 ptr = wmalloc(sizeof(Shortcut));
515 strcpy(buf, shortcutDefinition);
516 b = (char*)buf;
518 /* get modifiers */
519 ptr->modifier = 0;
520 while ((k = strchr(b, '+'))!=NULL) {
521 int mod;
523 *k = 0;
524 mod = wXModifierFromKey(b);
525 if (mod<0) {
526 wwarning(_("%s:invalid key modifier \"%s\""), file, b);
527 wfree(ptr);
528 return False;
530 ptr->modifier |= mod;
532 b = k+1;
535 /* get key */
536 ksym = XStringToKeysym(b);
538 if (ksym==NoSymbol) {
539 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
540 file, shortcutDefinition, entry->text);
541 wfree(ptr);
542 return False;
545 ptr->keycode = XKeysymToKeycode(dpy, ksym);
546 if (ptr->keycode==0) {
547 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file,
548 shortcutDefinition, entry->text);
549 wfree(ptr);
550 return False;
553 ptr->menu = menu;
554 ptr->entry = entry;
556 ptr->next = shortcutList;
557 shortcutList = ptr;
559 menu->menu->screen_ptr->flags.root_menu_changed_shortcuts = 1;
561 return True;
565 /*******************************/
567 static char*
568 cropline(char *line)
570 char *end;
572 if (strlen(line)==0)
573 return line;
575 end = &(line[strlen(line)])-1;
576 while (isspace(*line) && *line!=0) line++;
577 while (end>line && isspace(*end)) {
578 *end=0;
579 end--;
581 return line;
585 static char*
586 next_token(char *line, char **next)
588 char *tmp, c;
589 char *ret;
591 *next = NULL;
592 while (*line==' ' || *line=='\t') line++;
594 tmp = line;
596 if (*tmp=='"') {
597 tmp++; line++;
598 while (*tmp!=0 && *tmp!='"') tmp++;
599 if (*tmp!='"') {
600 wwarning(_("%s: unmatched '\"' in menu file"), line);
601 return NULL;
603 } else {
604 do {
605 if (*tmp=='\\')
606 tmp++;
608 if (*tmp!=0)
609 tmp++;
611 } while (*tmp!=0 && *tmp!=' ' && *tmp!='\t');
614 c = *tmp;
615 *tmp = 0;
616 ret = wstrdup(line);
617 *tmp = c;
619 if (c==0)
620 return ret;
621 else
622 tmp++;
624 /* skip blanks */
625 while (*tmp==' ' || *tmp=='\t') tmp++;
627 if (*tmp!=0)
628 *next = tmp;
630 return ret;
634 static void
635 separateCommand(char *line, char ***file, char **command)
637 char *token, *tmp = line;
638 WMArray *array = WMCreateArray(4);
639 int count, i;
641 *file = NULL;
642 *command = NULL;
643 do {
644 token = next_token(tmp, &tmp);
645 if (token) {
646 if (strcmp(token, "WITH")==0) {
647 if (tmp!=NULL && *tmp!=0)
648 *command = wstrdup(tmp);
649 else
650 wwarning(_("%s: missing command"), line);
651 break;
653 WMAddToArray(array, token);
655 } while (token!=NULL && tmp!=NULL);
657 count = WMGetArrayItemCount(array);
658 if (count>0) {
659 *file = wmalloc(sizeof(char*)*(count+1));
660 (*file)[count] = NULL;
661 for (i = 0; i < count; i++) {
662 (*file)[i] = WMGetFromArray(array, i);
665 WMFreeArray(array);
669 static void
670 constructMenu(WMenu *menu, WMenuEntry *entry)
672 WMenu *submenu;
673 struct stat stat_buf;
674 char **path;
675 char *cmd;
676 char *lpath = NULL;
677 int i, first=-1;
678 time_t last=0;
680 separateCommand((char*)entry->clientdata, &path, &cmd);
681 if (path == NULL || *path==NULL || **path==0) {
682 wwarning(_("invalid OPEN_MENU specification: %s"),
683 (char*)entry->clientdata);
684 return;
687 if (path[0][0]=='|') {
688 /* pipe menu */
690 if (!menu->cascades[entry->cascade] ||
691 menu->cascades[entry->cascade]->timestamp == 0) {
692 /* parse pipe */
694 submenu = readMenuPipe(menu->frame->screen_ptr, path);
696 if(submenu != NULL) {
697 if (path[0][1] == '|')
698 submenu->timestamp = 0;
699 else
700 submenu->timestamp = 1; /* there's no automatic reloading */
702 } else {
703 submenu = NULL;
706 } else {
707 i=0;
708 while(path[i] != NULL) {
709 char *tmp;
711 if (strcmp(path[i], "-noext")==0) {
712 i++;
713 continue;
716 tmp = wexpandpath(path[i]);
717 wfree(path[i]);
718 lpath = getLocalizedMenuFile(tmp);
719 if (lpath) {
720 wfree(tmp);
721 path[i] = lpath;
722 lpath = NULL;
723 } else {
724 path[i] = tmp;
727 if (stat(path[i], &stat_buf)==0) {
728 if (last < stat_buf.st_mtime)
729 last = stat_buf.st_mtime;
730 if (first<0)
731 first=i;
732 } else {
733 wsyserror(_("%s:could not stat menu"), path[i]);
734 /*goto finish;*/
737 i++;
740 if (first < 0) {
741 wsyserror(_("%s:could not stat menu:%s"), "OPEN_MENU",
742 (char*)entry->clientdata);
743 goto finish;
745 stat(path[first], &stat_buf);
746 if (!menu->cascades[entry->cascade]
747 || menu->cascades[entry->cascade]->timestamp < last) {
749 if (S_ISDIR(stat_buf.st_mode)) {
750 /* menu directory */
751 submenu = readMenuDirectory(menu->frame->screen_ptr,
752 entry->text, path, cmd);
753 if (submenu)
754 submenu->timestamp = last;
755 } else if (S_ISREG(stat_buf.st_mode)) {
756 /* menu file */
758 if (cmd || path[1])
759 wwarning(_("too many parameters in OPEN_MENU: %s"),
760 (char*)entry->clientdata);
762 submenu = readMenuFile(menu->frame->screen_ptr, path[first]);
763 if (submenu)
764 submenu->timestamp = stat_buf.st_mtime;
765 } else {
766 submenu = NULL;
768 } else {
769 submenu = NULL;
773 if (submenu) {
774 wMenuEntryRemoveCascade(menu, entry);
775 wMenuEntrySetCascade(menu, entry, submenu);
778 finish:
779 i = 0;
780 while (path[i]!=NULL)
781 wfree(path[i++]);
782 wfree(path);
783 if (cmd)
784 wfree(cmd);
788 static void
789 cleanupWorkspaceMenu(WMenu *menu)
791 if (menu->frame->screen_ptr->workspace_menu == menu)
792 menu->frame->screen_ptr->workspace_menu = NULL;
796 static WMenuEntry*
797 addWorkspaceMenu(WScreen *scr, WMenu *menu, char *title)
799 WMenu *wsmenu;
800 WMenuEntry *entry;
802 if (scr->flags.added_workspace_menu) {
803 wwarning(_("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
804 return NULL;
805 } else {
806 scr->flags.added_workspace_menu = 1;
808 wsmenu = wWorkspaceMenuMake(scr, True);
809 wsmenu->on_destroy = cleanupWorkspaceMenu;
811 scr->workspace_menu = wsmenu;
812 entry = wMenuAddCallback(menu, title, NULL, NULL);
813 wMenuEntrySetCascade(menu, entry, wsmenu);
815 wWorkspaceMenuUpdate(scr, wsmenu);
817 return entry;
821 static void
822 cleanupWindowsMenu(WMenu *menu)
824 if (menu->frame->screen_ptr->switch_menu == menu)
825 menu->frame->screen_ptr->switch_menu = NULL;
829 static WMenuEntry*
830 addWindowsMenu(WScreen *scr, WMenu *menu, char *title)
832 WMenu *wwmenu;
833 WWindow *wwin;
834 WMenuEntry *entry;
836 if (scr->flags.added_windows_menu) {
837 wwarning(_("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
838 return NULL;
839 } else {
840 scr->flags.added_windows_menu = 1;
842 wwmenu = wMenuCreate(scr, _("Window List"), False);
843 wwmenu->on_destroy = cleanupWindowsMenu;
844 scr->switch_menu = wwmenu;
845 wwin = scr->focused_window;
846 while (wwin) {
847 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
849 wwin = wwin->prev;
851 entry = wMenuAddCallback(menu, title, NULL, NULL);
852 wMenuEntrySetCascade(menu, entry, wwmenu);
854 return entry;
858 static WMenuEntry*
859 addMenuEntry(WMenu *menu, char *title, char *shortcut, char *command,
860 char *params, char *file_name)
862 WScreen *scr;
863 WMenuEntry *entry = NULL;
864 Bool shortcutOk = False;
866 if (!menu)
867 return NULL;
868 scr = menu->frame->screen_ptr;
869 if (strcmp(command, "OPEN_MENU")==0) {
870 if (!params) {
871 wwarning(_("%s:missing parameter for menu command \"%s\""),
872 file_name, command);
873 } else {
874 WMenu *dummy;
875 char *path;
877 path = wfindfile(DEF_CONFIG_PATHS, params);
878 if (!path) {
879 path = wstrdup(params);
881 dummy = wMenuCreate(scr, title, False);
882 dummy->on_destroy = removeShortcutsForMenu;
883 entry = wMenuAddCallback(menu, title, constructMenu, path);
884 entry->free_cdata = free;
885 wMenuEntrySetCascade(menu, entry, dummy);
887 } else if (strcmp(command, "EXEC")==0) {
888 if (!params)
889 wwarning(_("%s:missing parameter for menu command \"%s\""),
890 file_name, command);
891 else {
892 entry = wMenuAddCallback(menu, title, execCommand,
893 wstrconcat("exec ", params));
894 entry->free_cdata = free;
895 shortcutOk = True;
897 } else if (strcmp(command, "SHEXEC")==0) {
898 if (!params)
899 wwarning(_("%s:missing parameter for menu command \"%s\""),
900 file_name, command);
901 else {
902 entry = wMenuAddCallback(menu, title, execCommand,
903 wstrdup(params));
904 entry->free_cdata = free;
905 shortcutOk = True;
907 } else if (strcmp(command, "EXIT")==0) {
909 if (params && strcmp(params, "QUICK")==0)
910 entry = wMenuAddCallback(menu, title, exitCommand, (void*)M_QUICK);
911 else
912 entry = wMenuAddCallback(menu, title, exitCommand, NULL);
914 shortcutOk = True;
915 } else if (strcmp(command, "SHUTDOWN")==0) {
917 if (params && strcmp(params, "QUICK")==0)
918 entry = wMenuAddCallback(menu, title, shutdownCommand,
919 (void*)M_QUICK);
920 else
921 entry = wMenuAddCallback(menu, title, shutdownCommand, NULL);
923 shortcutOk = True;
924 } else if (strcmp(command, "REFRESH")==0) {
925 entry = wMenuAddCallback(menu, title, refreshCommand, NULL);
927 shortcutOk = True;
928 } else if (strcmp(command, "WORKSPACE_MENU")==0) {
929 entry = addWorkspaceMenu(scr, menu, title);
931 shortcutOk = True;
932 } else if (strcmp(command, "WINDOWS_MENU")==0) {
933 entry = addWindowsMenu(scr, menu, title);
935 shortcutOk = True;
936 } else if (strcmp(command, "ARRANGE_ICONS")==0) {
937 entry = wMenuAddCallback(menu, title, arrangeIconsCommand, NULL);
939 shortcutOk = True;
940 } else if (strcmp(command, "HIDE_OTHERS")==0) {
941 entry = wMenuAddCallback(menu, title, hideOthersCommand, NULL);
943 shortcutOk = True;
944 } else if (strcmp(command, "SHOW_ALL")==0) {
945 entry = wMenuAddCallback(menu, title, showAllCommand, NULL);
947 shortcutOk = True;
948 } else if (strcmp(command, "RESTART")==0) {
949 entry = wMenuAddCallback(menu, title, restartCommand,
950 params ? wstrdup(params) : NULL);
951 entry->free_cdata = free;
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,
968 command);
970 return NULL;
973 if (shortcut && entry) {
974 if (!shortcutOk) {
975 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name,
976 title);
977 } else {
978 if (addShortcut(file_name, shortcut, menu, entry)) {
980 entry->rtext = GetShortcutString(shortcut);
982 entry->rtext = wstrdup(shortcut);
988 return entry;
993 /******************* Menu Configuration From File *******************/
995 static void
996 separateline(char *line, char *title, char *command, char *parameter,
997 char *shortcut)
999 int l, i;
1001 l = strlen(line);
1003 *title = 0;
1004 *command = 0;
1005 *parameter = 0;
1006 *shortcut = 0;
1007 /* get the title */
1008 while (isspace(*line) && (*line!=0)) line++;
1009 if (*line=='"') {
1010 line++;
1011 i=0;
1012 while (line[i]!='"' && (line[i]!=0)) i++;
1013 if (line[i]!='"')
1014 return;
1015 } else {
1016 i=0;
1017 while (!isspace(line[i]) && (line[i]!=0)) i++;
1019 strncpy(title, line, i);
1020 title[i++]=0;
1021 line+=i;
1023 /* get the command or shortcut keyword */
1024 while (isspace(*line) && (*line!=0)) line++;
1025 if (*line==0)
1026 return;
1027 i=0;
1028 while (!isspace(line[i]) && (line[i]!=0)) i++;
1029 strncpy(command, line, i);
1030 command[i++]=0;
1031 line+=i;
1033 if (strcmp(command, "SHORTCUT")==0) {
1034 /* get the shortcut key */
1035 while (isspace(*line) && (*line!=0)) line++;
1036 if (*line=='"') {
1037 line++;
1038 i=0;
1039 while (line[i]!='"' && (line[i]!=0)) i++;
1040 if (line[i]!='"')
1041 return;
1042 } else {
1043 i=0;
1044 while (!isspace(line[i]) && (line[i]!=0)) i++;
1046 strncpy(shortcut, line, i);
1047 shortcut[i++]=0;
1048 line+=i;
1050 *command=0;
1052 /* get the command */
1053 while (isspace(*line) && (*line!=0)) line++;
1054 if (*line==0)
1055 return;
1056 i=0;
1057 while (!isspace(line[i]) && (line[i]!=0)) i++;
1058 strncpy(command, line, i);
1059 command[i++]=0;
1060 line+=i;
1063 /* get the parameters */
1064 while (isspace(*line) && (*line!=0)) line++;
1065 if (*line==0)
1066 return;
1068 if (*line=='"') {
1069 line++;
1070 l = 0;
1071 while (line[l]!=0 && line[l]!='"') {
1072 parameter[l] = line[l];
1073 l++;
1075 parameter[l] = 0;
1076 return;
1079 l = strlen(line);
1080 while (isspace(line[l]) && (l>0)) l--;
1081 strncpy(parameter, line, l);
1082 parameter[l]=0;
1086 static WMenu*
1087 parseCascade(WScreen *scr, WMenu *menu, FILE *file, char *file_name)
1089 char linebuf[MAXLINE];
1090 char elinebuf[MAXLINE];
1091 char title[MAXLINE];
1092 char command[MAXLINE];
1093 char shortcut[MAXLINE];
1094 char params[MAXLINE];
1095 char *line;
1097 while (!IsEof(file)) {
1098 int lsize, ok;
1100 ok = 0;
1101 fgets(linebuf, MAXLINE, file);
1102 line = cropline(linebuf);
1103 lsize = strlen(line);
1104 do {
1105 if (line[lsize-1]=='\\') {
1106 char *line2;
1107 int lsize2;
1108 fgets(elinebuf, MAXLINE, file);
1109 line2=cropline(elinebuf);
1110 lsize2=strlen(line2);
1111 if (lsize2+lsize>MAXLINE) {
1112 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1113 file_name, line);
1114 ok=2;
1115 } else {
1116 line[lsize-1]=0;
1117 lsize+=lsize2-1;
1118 strcat(line, line2);
1120 } else {
1121 ok=1;
1123 } while (!ok && !IsEof(file));
1124 if (ok==2)
1125 continue;
1127 if (line[0]==0 || line[0]=='#' || (line[0]=='/' && line[1]=='/'))
1128 continue;
1131 separateline(line, title, command, params, shortcut);
1133 if (!command[0]) {
1134 wwarning(_("%s:missing command in menu config: %s"), file_name,
1135 line);
1136 goto error;
1139 if (strcasecmp(command, "MENU")==0) {
1140 WMenu *cascade;
1142 /* start submenu */
1144 cascade = wMenuCreate(scr, title, False);
1145 cascade->on_destroy = removeShortcutsForMenu;
1146 if (parseCascade(scr, cascade, file, file_name)==NULL) {
1147 wMenuDestroy(cascade, True);
1148 } else {
1149 wMenuEntrySetCascade(menu,
1150 wMenuAddCallback(menu, title, NULL, NULL),
1151 cascade);
1153 } else if (strcasecmp(command, "END")==0) {
1154 /* end of menu */
1155 return menu;
1157 } else {
1158 /* normal items */
1159 addMenuEntry(menu, title, shortcut[0] ? shortcut : NULL, command,
1160 params[0] ? params : NULL, file_name);
1164 wwarning(_("%s:syntax error in menu file:END declaration missing"),
1165 file_name);
1166 return menu;
1168 error:
1169 return menu;
1173 static WMenu*
1174 readMenuFile(WScreen *scr, char *file_name)
1176 WMenu *menu=NULL;
1177 FILE *file = NULL;
1178 char linebuf[MAXLINE];
1179 char title[MAXLINE];
1180 char shortcut[MAXLINE];
1181 char command[MAXLINE];
1182 char params[MAXLINE];
1183 char *line;
1184 #ifdef USECPP
1185 char *args;
1186 int cpp = 0;
1187 #endif
1189 #ifdef USECPP
1190 if (!wPreferences.flags.nocpp) {
1191 args = MakeCPPArgs(file_name);
1192 if (!args) {
1193 wwarning(_("could not make arguments for menu file preprocessor"));
1194 } else {
1195 snprintf(command, sizeof(command), "%s %s %s",
1196 CPP_PATH, args, file_name);
1197 wfree(args);
1198 file = popen(command, "r");
1199 if (!file) {
1200 wsyserror(_("%s:could not open/preprocess menu file"),
1201 file_name);
1202 } else {
1203 cpp = 1;
1207 #endif /* USECPP */
1209 if (!file) {
1210 file = fopen(file_name, "r");
1211 if (!file) {
1212 wsyserror(_("%s:could not open menu file"), file_name);
1213 return NULL;
1217 while (!IsEof(file)) {
1218 if (!fgets(linebuf, MAXLINE, file))
1219 break;
1220 line = cropline(linebuf);
1221 if (line[0]==0 || line[0]=='#' || (line[0]=='/' && line[1]=='/'))
1222 continue;
1224 separateline(line, title, command, params, shortcut);
1226 if (!command[0]) {
1227 wwarning(_("%s:missing command in menu config: %s"), file_name,
1228 line);
1229 break;
1231 if (strcasecmp(command, "MENU")==0) {
1232 menu = wMenuCreate(scr, title, True);
1233 menu->on_destroy = removeShortcutsForMenu;
1234 if (!parseCascade(scr, menu, file, file_name)) {
1235 wMenuDestroy(menu, True);
1237 break;
1238 } else {
1239 wwarning(_("%s:invalid menu file. MENU command is missing"),
1240 file_name);
1241 break;
1245 #ifdef CPP
1246 if (cpp) {
1247 if (pclose(file)==-1) {
1248 wsyserror(_("error reading preprocessed menu data"));
1250 } else {
1251 fclose(file);
1253 #else
1254 fclose(file);
1255 #endif
1257 return menu;
1260 /************ Menu Configuration From Pipe *************/
1262 static WMenu*
1263 readMenuPipe(WScreen *scr, char **file_name)
1265 WMenu *menu=NULL;
1266 FILE *file = NULL;
1267 char linebuf[MAXLINE];
1268 char title[MAXLINE];
1269 char command[MAXLINE];
1270 char params[MAXLINE];
1271 char shortcut[MAXLINE];
1272 char *line;
1273 char *filename;
1274 char flat_file[MAXLINE];
1275 int i;
1276 #ifdef USECPP
1277 char *args;
1278 int cpp = 0;
1279 #endif
1281 flat_file[0] = '\0';
1283 for(i=0; file_name[i]!=NULL; i++) {
1284 strcat(flat_file, file_name[i]);
1285 strcat(flat_file, " ");
1287 filename = flat_file + (flat_file[1]=='|'?2: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 */