wAppIconPaint paint argument
[wmaker-crm.git] / src / rootmenu.c
blob2fcd0ae99d99f4e6d21e9baf6976676cce8c41d6
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 "main.h"
46 #include "dialog.h"
47 #include "keybind.h"
48 #include "stacking.h"
49 #include "workspace.h"
50 #include "defaults.h"
51 #include "framewin.h"
52 #include "session.h"
53 #include "xmodifier.h"
55 #include <WINGs/WUtil.h>
57 #define MAX_SHORTCUT_LENGTH 32
59 extern char *Locale;
60 extern WDDomain *WDRootMenu;
61 extern Cursor wCursor[WCUR_LAST];
62 extern WPreferences wPreferences;
64 static WMenu *readMenuPipe(WScreen * scr, char **file_name);
65 static WMenu *readMenuFile(WScreen * scr, char *file_name);
66 static WMenu *readMenuDirectory(WScreen * scr, char *title, char **file_name, char *command);
67 static WMenu *configureMenu(WScreen * scr, WMPropList * definition, Bool includeGlobals);
68 static void menu_parser_register_macros(WMenuParser parser);
70 typedef struct Shortcut {
71 struct Shortcut *next;
73 int modifier;
74 KeyCode keycode;
75 WMenuEntry *entry;
76 WMenu *menu;
77 } Shortcut;
79 static Shortcut *shortcutList = NULL;
82 * Syntax:
83 * # main menu
84 * "Menu Name" MENU
85 * "Title" EXEC command_to_exec -params
86 * "Submenu" MENU
87 * "Title" EXEC command_to_exec -params
88 * "Submenu" END
89 * "Workspaces" WORKSPACE_MENU
90 * "Title" built_in_command
91 * "Quit" EXIT
92 * "Quick Quit" EXIT QUICK
93 * "Menu Name" END
95 * Commands may be preceded by SHORTCUT key
97 * Built-in commands:
99 * INFO_PANEL - shows the Info Panel
100 * LEGAL_PANEL - shows the Legal info panel
101 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
102 * REFRESH - forces the desktop to be repainted
103 * EXIT [QUICK] - exit the window manager [without confirmation]
104 * EXEC <program> - execute an external program
105 * SHEXEC <command> - execute a shell command
106 * WORKSPACE_MENU - places the workspace submenu
107 * ARRANGE_ICONS
108 * RESTART [<window manager>] - restarts the window manager
109 * SHOW_ALL - unhide all windows on workspace
110 * HIDE_OTHERS - hides all windows excep the focused one
111 * OPEN_MENU file - read menu data from file which must be a valid menu file.
112 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
113 * - read menu data from directory(ies) and
114 * eventually precede each with a command.
115 * OPEN_MENU | command
116 * - opens command and uses its stdout to construct and insert
117 * the resulting menu in current position. The output of
118 * command must be a valid menu description.
119 * The space between '|' and command is optional.
120 * || will do the same, but will not cache the contents.
121 * SAVE_SESSION - saves the current state of the desktop, which include
122 * all running applications, all their hints (geometry,
123 * position on screen, workspace they live on, the dock
124 * or clip from where they were launched, and
125 * if minimized, shaded or hidden. Also saves the current
126 * workspace the user is on. All will be restored on every
127 * start of windowmaker until another SAVE_SESSION or
128 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
129 * WindowMaker domain file, then saving is automatically
130 * done on every windowmaker exit, overwriting any
131 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
132 * dock state now.
133 * CLEAR_SESSION - clears any previous saved session. This will not have
134 * any effect if SaveSessionOnExit is True.
138 #define M_QUICK 1
140 /* menu commands */
142 static void execCommand(WMenu * menu, WMenuEntry * entry)
144 char *cmdline;
146 cmdline = ExpandOptions(menu->frame->screen_ptr, (char *)entry->clientdata);
148 XGrabPointer(dpy, menu->frame->screen_ptr->root_win, True, 0,
149 GrabModeAsync, GrabModeAsync, None, wCursor[WCUR_WAIT], CurrentTime);
150 XSync(dpy, 0);
152 if (cmdline) {
153 ExecuteShellCommand(menu->frame->screen_ptr, cmdline);
154 wfree(cmdline);
156 XUngrabPointer(dpy, CurrentTime);
157 XSync(dpy, 0);
160 static void exitCommand(WMenu * menu, WMenuEntry * entry)
162 static int inside = 0;
163 int result;
165 /* prevent reentrant calls */
166 if (inside)
167 return;
168 inside = 1;
170 #define R_CANCEL 0
171 #define R_EXIT 1
173 result = R_CANCEL;
175 if ((long)entry->clientdata == M_QUICK) {
176 result = R_EXIT;
177 } else {
178 int r, oldSaveSessionFlag;
180 oldSaveSessionFlag = wPreferences.save_session_on_exit;
181 r = wExitDialog(menu->frame->screen_ptr, _("Exit"),
182 _("Exit window manager?"), _("Exit"), _("Cancel"), NULL);
184 if (r == WAPRDefault) {
185 result = R_EXIT;
186 } else if (r == WAPRAlternate) {
187 /* Don't modify the "save session on exit" flag if the
188 * user canceled the operation. */
189 wPreferences.save_session_on_exit = oldSaveSessionFlag;
192 if (result == R_EXIT)
193 Shutdown(WSExitMode);
195 #undef R_EXIT
196 #undef R_CANCEL
197 inside = 0;
200 static void shutdownCommand(WMenu * menu, WMenuEntry * entry)
202 static int inside = 0;
203 int result;
205 /* prevent reentrant calls */
206 if (inside)
207 return;
208 inside = 1;
210 #define R_CANCEL 0
211 #define R_CLOSE 1
212 #define R_KILL 2
214 result = R_CANCEL;
215 if ((long)entry->clientdata == M_QUICK)
216 result = R_CLOSE;
217 else {
218 int r, oldSaveSessionFlag;
220 oldSaveSessionFlag = wPreferences.save_session_on_exit;
222 r = wExitDialog(menu->frame->screen_ptr,
223 _("Kill X session"),
224 _("Kill Window System session?\n"
225 "(all applications will be closed)"), _("Kill"), _("Cancel"), NULL);
226 if (r == WAPRDefault) {
227 result = R_KILL;
228 } else if (r == WAPRAlternate) {
229 /* Don't modify the "save session on exit" flag if the
230 * user canceled the operation. */
231 wPreferences.save_session_on_exit = oldSaveSessionFlag;
235 if (result != R_CANCEL) {
236 Shutdown(WSKillMode);
238 #undef R_CLOSE
239 #undef R_CANCEL
240 #undef R_KILL
241 inside = 0;
244 static void restartCommand(WMenu * menu, WMenuEntry * entry)
246 Shutdown(WSRestartPreparationMode);
247 Restart((char *)entry->clientdata, False);
248 Restart(NULL, True);
251 static void refreshCommand(WMenu * menu, WMenuEntry * entry)
253 wRefreshDesktop(menu->frame->screen_ptr);
256 static void arrangeIconsCommand(WMenu * menu, WMenuEntry * entry)
258 wArrangeIcons(menu->frame->screen_ptr, True);
261 static void showAllCommand(WMenu * menu, WMenuEntry * entry)
263 wShowAllWindows(menu->frame->screen_ptr);
266 static void hideOthersCommand(WMenu * menu, WMenuEntry * entry)
268 wHideOtherApplications(menu->frame->screen_ptr->focused_window);
271 static void saveSessionCommand(WMenu * menu, WMenuEntry * entry)
273 if (!wPreferences.save_session_on_exit)
274 wSessionSaveState(menu->frame->screen_ptr);
276 wScreenSaveState(menu->frame->screen_ptr);
279 static void clearSessionCommand(WMenu * menu, WMenuEntry * entry)
281 wSessionClearState(menu->frame->screen_ptr);
282 wScreenSaveState(menu->frame->screen_ptr);
285 static void infoPanelCommand(WMenu * menu, WMenuEntry * entry)
287 wShowInfoPanel(menu->frame->screen_ptr);
290 static void legalPanelCommand(WMenu * menu, WMenuEntry * entry)
292 wShowLegalPanel(menu->frame->screen_ptr);
295 /********************************************************************/
297 static char * getLocalizedMenuFile(char *menu)
299 char *buffer, *ptr, *locale;
300 int len;
302 if (!Locale)
303 return NULL;
305 len = strlen(menu) + strlen(Locale) + 8;
306 buffer = wmalloc(len);
308 /* try menu.locale_name */
309 snprintf(buffer, len, "%s.%s", menu, Locale);
310 if (access(buffer, F_OK) == 0)
311 return buffer;
313 /* position of locale in our buffer */
314 locale = buffer + strlen(menu) + 1;
316 /* check if it is in the form aa_bb.encoding and check for aa_bb */
317 ptr = strchr(locale, '.');
318 if (ptr) {
319 *ptr = 0;
320 if (access(buffer, F_OK) == 0)
321 return buffer;
324 /* now check for aa */
325 ptr = strchr(locale, '_');
326 if (ptr) {
327 *ptr = 0;
328 if (access(buffer, F_OK) == 0)
329 return buffer;
332 wfree(buffer);
334 return NULL;
337 Bool wRootMenuPerformShortcut(XEvent * event)
339 WScreen *scr = wScreenForRootWindow(event->xkey.root);
340 Shortcut *ptr;
341 int modifiers;
342 int done = 0;
344 /* ignore CapsLock */
345 modifiers = event->xkey.state & ValidModMask;
347 for (ptr = shortcutList; ptr != NULL; ptr = ptr->next) {
348 if (ptr->keycode == 0 || ptr->menu->menu->screen_ptr != scr)
349 continue;
351 if (ptr->keycode == event->xkey.keycode && ptr->modifier == modifiers) {
352 (*ptr->entry->callback) (ptr->menu, ptr->entry);
353 done = True;
357 return done;
360 void wRootMenuBindShortcuts(Window window)
362 Shortcut *ptr;
364 ptr = shortcutList;
365 while (ptr) {
366 if (ptr->modifier != AnyModifier) {
367 XGrabKey(dpy, ptr->keycode, ptr->modifier | LockMask,
368 window, True, GrabModeAsync, GrabModeAsync);
369 #ifdef NUMLOCK_HACK
370 wHackedGrabKey(ptr->keycode, ptr->modifier, window, True, GrabModeAsync, GrabModeAsync);
371 #endif
373 XGrabKey(dpy, ptr->keycode, ptr->modifier, window, True, GrabModeAsync, GrabModeAsync);
374 ptr = ptr->next;
378 static void rebindKeygrabs(WScreen * scr)
380 WWindow *wwin;
382 wwin = scr->focused_window;
384 while (wwin != NULL) {
385 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->frame->core->window);
387 if (!WFLAGP(wwin, no_bind_keys)) {
388 wWindowSetKeyGrabs(wwin);
390 wwin = wwin->prev;
394 static void removeShortcutsForMenu(WMenu * menu)
396 Shortcut *ptr, *tmp;
397 Shortcut *newList = NULL;
399 ptr = shortcutList;
400 while (ptr != NULL) {
401 tmp = ptr->next;
402 if (ptr->menu == menu) {
403 wfree(ptr);
404 } else {
405 ptr->next = newList;
406 newList = ptr;
408 ptr = tmp;
410 shortcutList = newList;
411 menu->menu->screen_ptr->flags.root_menu_changed_shortcuts = 1;
414 static Bool addShortcut(const char *file, char *shortcutDefinition, WMenu * menu, WMenuEntry * entry)
416 Shortcut *ptr;
417 KeySym ksym;
418 char *k;
419 char buf[MAX_SHORTCUT_LENGTH], *b;
421 ptr = wmalloc(sizeof(Shortcut));
423 wstrlcpy(buf, shortcutDefinition, MAX_SHORTCUT_LENGTH);
424 b = (char *)buf;
426 /* get modifiers */
427 ptr->modifier = 0;
428 while ((k = strchr(b, '+')) != NULL) {
429 int mod;
431 *k = 0;
432 mod = wXModifierFromKey(b);
433 if (mod < 0) {
434 wwarning(_("%s: invalid key modifier \"%s\""), file, b);
435 wfree(ptr);
436 return False;
438 ptr->modifier |= mod;
440 b = k + 1;
443 /* get key */
444 ksym = XStringToKeysym(b);
446 if (ksym == NoSymbol) {
447 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
448 file, shortcutDefinition, entry->text);
449 wfree(ptr);
450 return False;
453 ptr->keycode = XKeysymToKeycode(dpy, ksym);
454 if (ptr->keycode == 0) {
455 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file,
456 shortcutDefinition, entry->text);
457 wfree(ptr);
458 return False;
461 ptr->menu = menu;
462 ptr->entry = entry;
464 ptr->next = shortcutList;
465 shortcutList = ptr;
467 menu->menu->screen_ptr->flags.root_menu_changed_shortcuts = 1;
469 return True;
472 static char *next_token(char *line, char **next)
474 char *tmp, c;
475 char *ret;
477 *next = NULL;
478 while (*line == ' ' || *line == '\t')
479 line++;
481 tmp = line;
483 if (*tmp == '"') {
484 tmp++;
485 line++;
486 while (*tmp != 0 && *tmp != '"')
487 tmp++;
488 if (*tmp != '"') {
489 wwarning(_("%s: unmatched '\"' in menu file"), line);
490 return NULL;
492 } else {
493 do {
494 if (*tmp == '\\')
495 tmp++;
497 if (*tmp != 0)
498 tmp++;
500 } while (*tmp != 0 && *tmp != ' ' && *tmp != '\t');
503 c = *tmp;
504 *tmp = 0;
505 ret = wstrdup(line);
506 *tmp = c;
508 if (c == 0)
509 return ret;
510 else
511 tmp++;
513 /* skip blanks */
514 while (*tmp == ' ' || *tmp == '\t')
515 tmp++;
517 if (*tmp != 0)
518 *next = tmp;
520 return ret;
523 static void separateCommand(char *line, char ***file, char **command)
525 char *token, *tmp = line;
526 WMArray *array = WMCreateArray(4);
527 int count, i;
529 *file = NULL;
530 *command = NULL;
531 do {
532 token = next_token(tmp, &tmp);
533 if (token) {
534 if (strcmp(token, "WITH") == 0) {
535 if (tmp != NULL && *tmp != 0)
536 *command = wstrdup(tmp);
537 else
538 wwarning(_("%s: missing command"), line);
539 break;
541 WMAddToArray(array, token);
543 } while (token != NULL && tmp != NULL);
545 count = WMGetArrayItemCount(array);
546 if (count > 0) {
547 *file = wmalloc(sizeof(char *) * (count + 1));
548 (*file)[count] = NULL;
549 for (i = 0; i < count; i++) {
550 (*file)[i] = WMGetFromArray(array, i);
553 WMFreeArray(array);
556 static WMenu *constructPLMenu(WScreen *screen, char *path)
558 WMPropList *pl = NULL;
559 WMenu *menu = NULL;
561 if (!path)
562 return NULL;
564 pl = WMReadPropListFromFile(path);
565 if (!pl)
566 return NULL;
568 menu = configureMenu(screen, pl, False);
569 if (!menu)
570 return NULL;
572 menu->on_destroy = removeShortcutsForMenu;
573 return menu;
576 static void constructMenu(WMenu * menu, WMenuEntry * entry)
578 WMenu *submenu;
579 struct stat stat_buf;
580 char **path;
581 char *cmd;
582 char *lpath = NULL;
583 int i, first = -1;
584 time_t last = 0;
586 separateCommand((char *)entry->clientdata, &path, &cmd);
587 if (path == NULL || *path == NULL || **path == 0) {
588 wwarning(_("invalid OPEN_MENU specification: %s"), (char *)entry->clientdata);
589 if (cmd)
590 wfree(cmd);
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, const 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 = wfree;
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 = wfree;
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 = wfree;
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 = wfree;
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 freeline(char *title, char *command, char *parameter, char *shortcut)
889 wfree(title);
890 wfree(command);
891 wfree(parameter);
892 wfree(shortcut);
895 static WMenu *parseCascade(WScreen * scr, WMenu * menu, WMenuParser parser)
897 char *command, *params, *shortcut, *title;
899 while (WMenuParserGetLine(parser, &title, &command, &params, &shortcut)) {
901 if (command == NULL || !command[0]) {
902 WMenuParserError(parser, _("missing command in menu config") );
903 freeline(title, command, params, shortcut);
904 goto error;
907 if (strcasecmp(command, "MENU") == 0) {
908 WMenu *cascade;
910 /* start submenu */
912 cascade = wMenuCreate(scr, M_(title), False);
913 cascade->on_destroy = removeShortcutsForMenu;
914 if (!parseCascade(scr, cascade, parser)) {
915 wMenuDestroy(cascade, True);
916 } else {
917 wMenuEntrySetCascade(menu, wMenuAddCallback(menu, M_(title), NULL, NULL), cascade);
919 } else if (strcasecmp(command, "END") == 0) {
920 /* end of menu */
921 freeline(title, command, params, shortcut);
922 return menu;
923 } else {
924 /* normal items */
925 addMenuEntry(menu, M_(title), shortcut, command, params, WMenuParserGetFilename(parser));
927 freeline(title, command, params, shortcut);
930 WMenuParserError(parser, _("syntax error in menu file: END declaration missing") );
932 error:
933 return NULL;
936 static WMenu *readMenuFile(WScreen * scr, char *file_name)
938 WMenu *menu = NULL;
939 FILE *file = NULL;
940 WMenuParser parser;
941 char *command, *params, *shortcut, *title;
943 file = fopen(file_name, "rb");
944 if (!file) {
945 werror(_("%s:could not open menu file"), file_name);
946 return NULL;
948 parser = WMenuParserCreate(file_name, file, DEF_CONFIG_PATHS);
949 menu_parser_register_macros(parser);
951 while (WMenuParserGetLine(parser, &title, &command, &params, &shortcut)) {
953 if (command == NULL || !command[0]) {
954 WMenuParserError(parser, _("missing command in menu config") );
955 freeline(title, command, params, shortcut);
956 break;
958 if (strcasecmp(command, "MENU") == 0) {
959 menu = wMenuCreate(scr, M_(title), True);
960 menu->on_destroy = removeShortcutsForMenu;
961 if (!parseCascade(scr, menu, parser)) {
962 wMenuDestroy(menu, True);
963 menu = NULL;
965 freeline(title, command, params, shortcut);
966 break;
967 } else {
968 WMenuParserError(parser, _("invalid menu file, MENU command is missing") );
969 freeline(title, command, params, shortcut);
970 break;
972 freeline(title, command, params, shortcut);
975 WMenuParserDelete(parser);
976 fclose(file);
978 return menu;
981 /************ Menu Configuration From Pipe *************/
983 static WMenu *readMenuPipe(WScreen * scr, char **file_name)
985 WMenu *menu = NULL;
986 FILE *file = NULL;
987 WMenuParser parser;
988 char *command, *params, *shortcut, *title;
989 char *filename;
990 char flat_file[MAXLINE];
991 int i;
993 flat_file[0] = '\0';
995 for (i = 0; file_name[i] != NULL; i++) {
996 strcat(flat_file, file_name[i]);
997 strcat(flat_file, " ");
999 filename = flat_file + (flat_file[1] == '|' ? 2 : 1);
1001 file = popen(filename, "r");
1002 if (!file) {
1003 werror(_("%s:could not open menu file"), filename);
1004 return NULL;
1006 parser = WMenuParserCreate(flat_file, file, DEF_CONFIG_PATHS);
1007 menu_parser_register_macros(parser);
1009 while (WMenuParserGetLine(parser, &title, &command, &params, &shortcut)) {
1011 if (command == NULL || !command[0]) {
1012 WMenuParserError(parser, _("missing command in menu config") );
1013 freeline(title, command, params, shortcut);
1014 break;
1016 if (strcasecmp(command, "MENU") == 0) {
1017 menu = wMenuCreate(scr, M_(title), True);
1018 menu->on_destroy = removeShortcutsForMenu;
1019 if (!parseCascade(scr, menu, parser)) {
1020 wMenuDestroy(menu, True);
1021 menu = NULL;
1023 freeline(title, command, params, shortcut);
1024 break;
1025 } else {
1026 WMenuParserError(parser, _("no title given for the root menu") );
1027 freeline(title, command, params, shortcut);
1028 break;
1031 freeline(title, command, params, shortcut);
1034 WMenuParserDelete(parser);
1035 pclose(file);
1037 return menu;
1040 typedef struct {
1041 char *name;
1042 int index;
1043 } dir_data;
1045 static int myCompare(const void *d1, const void *d2)
1047 dir_data *p1 = *(dir_data **) d1;
1048 dir_data *p2 = *(dir_data **) d2;
1050 return strcmp(p1->name, p2->name);
1053 /***** Preset some macro for file parser *****/
1054 static void menu_parser_register_macros(WMenuParser parser)
1056 Visual *visual;
1057 char buf[32];
1059 // Used to return CPP verion, now returns wmaker's version
1060 WMenuParserRegisterSimpleMacro(parser, "__VERSION__", VERSION);
1062 // All macros below were historically defined by WindowMaker
1063 visual = DefaultVisual(dpy, DefaultScreen(dpy));
1064 snprintf(buf, sizeof(buf), "%d", visual->class);
1065 WMenuParserRegisterSimpleMacro(parser, "VISUAL", buf);
1067 snprintf(buf, sizeof(buf), "%d", DefaultDepth(dpy, DefaultScreen(dpy)) );
1068 WMenuParserRegisterSimpleMacro(parser, "DEPTH", buf);
1070 snprintf(buf, sizeof(buf), "%d", WidthOfScreen(DefaultScreenOfDisplay(dpy)) );
1071 WMenuParserRegisterSimpleMacro(parser, "SCR_WIDTH", buf);
1073 snprintf(buf, sizeof(buf), "%d", HeightOfScreen(DefaultScreenOfDisplay(dpy)) );
1074 WMenuParserRegisterSimpleMacro(parser, "SCR_HEIGHT", buf);
1076 WMenuParserRegisterSimpleMacro(parser, "DISPLAY", XDisplayName(DisplayString(dpy)) );
1078 WMenuParserRegisterSimpleMacro(parser, "WM_VERSION", "\"" VERSION "\"");
1081 /************ Menu Configuration From Directory *************/
1083 static Bool isFilePackage(char *file)
1085 int l;
1087 /* check if the extension indicates this file is a
1088 * file package. For now, only recognize .themed */
1090 l = strlen(file);
1092 if (l > 7 && strcmp(&(file[l - 7]), ".themed") == 0) {
1093 return True;
1094 } else {
1095 return False;
1099 static WMenu *readMenuDirectory(WScreen * scr, char *title, char **path, char *command)
1101 DIR *dir;
1102 struct dirent *dentry;
1103 struct stat stat_buf;
1104 WMenu *menu = NULL;
1105 char *buffer;
1106 WMArray *dirs = NULL, *files = NULL;
1107 WMArrayIterator iter;
1108 int length, i, have_space = 0;
1109 dir_data *data;
1110 int stripExtension = 0;
1112 dirs = WMCreateArray(16);
1113 files = WMCreateArray(16);
1115 i = 0;
1116 while (path[i] != NULL) {
1117 if (strcmp(path[i], "-noext") == 0) {
1118 stripExtension = 1;
1119 i++;
1120 continue;
1123 dir = opendir(path[i]);
1124 if (!dir) {
1125 i++;
1126 continue;
1129 while ((dentry = readdir(dir))) {
1131 if (strcmp(dentry->d_name, ".") == 0 || strcmp(dentry->d_name, "..") == 0)
1132 continue;
1134 if (dentry->d_name[0] == '.')
1135 continue;
1137 buffer = malloc(strlen(path[i]) + strlen(dentry->d_name) + 4);
1138 if (!buffer) {
1139 werror(_("out of memory while constructing directory menu %s"), path[i]);
1140 break;
1143 strcpy(buffer, path[i]);
1144 strcat(buffer, "/");
1145 strcat(buffer, dentry->d_name);
1147 if (stat(buffer, &stat_buf) != 0) {
1148 werror(_("%s:could not stat file \"%s\" in menu directory"),
1149 path[i], dentry->d_name);
1150 } else {
1151 Bool isFilePack = False;
1153 data = NULL;
1154 if (S_ISDIR(stat_buf.st_mode)
1155 && !(isFilePack = isFilePackage(dentry->d_name))) {
1157 /* access always returns success for user root */
1158 if (access(buffer, X_OK) == 0) {
1159 /* Directory is accesible. Add to directory list */
1161 data = (dir_data *) wmalloc(sizeof(dir_data));
1162 data->name = wstrdup(dentry->d_name);
1163 data->index = i;
1165 WMAddToArray(dirs, data);
1167 } else if (S_ISREG(stat_buf.st_mode) || isFilePack) {
1168 /* Hack because access always returns X_OK success for user root */
1169 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1170 if ((command != NULL && access(buffer, R_OK) == 0) ||
1171 (command == NULL && access(buffer, X_OK) == 0 &&
1172 (stat_buf.st_mode & S_IXANY))) {
1174 data = (dir_data *) wmalloc(sizeof(dir_data));
1175 data->name = wstrdup(dentry->d_name);
1176 data->index = i;
1178 WMAddToArray(files, data);
1182 free(buffer);
1185 closedir(dir);
1186 i++;
1189 if (!WMGetArrayItemCount(dirs) && !WMGetArrayItemCount(files)) {
1190 WMFreeArray(dirs);
1191 WMFreeArray(files);
1192 return NULL;
1195 WMSortArray(dirs, myCompare);
1196 WMSortArray(files, myCompare);
1198 menu = wMenuCreate(scr, M_(title), False);
1199 menu->on_destroy = removeShortcutsForMenu;
1201 WM_ITERATE_ARRAY(dirs, data, iter) {
1202 /* New directory. Use same OPEN_MENU command that was used
1203 * for the current directory. */
1204 length = strlen(path[data->index]) + strlen(data->name) + 6;
1205 if (stripExtension)
1206 length += 7;
1207 if (command)
1208 length += strlen(command) + 6;
1209 buffer = malloc(length);
1210 if (!buffer) {
1211 werror(_("out of memory while constructing directory menu %s"), path[data->index]);
1212 break;
1215 buffer[0] = '\0';
1216 if (stripExtension)
1217 strcat(buffer, "-noext ");
1219 have_space = strchr(path[data->index], ' ') != NULL || strchr(data->name, ' ') != NULL;
1221 if (have_space)
1222 strcat(buffer, "\"");
1223 strcat(buffer, path[data->index]);
1225 strcat(buffer, "/");
1226 strcat(buffer, data->name);
1227 if (have_space)
1228 strcat(buffer, "\"");
1229 if (command) {
1230 strcat(buffer, " WITH ");
1231 strcat(buffer, command);
1234 addMenuEntry(menu, M_(data->name), NULL, "OPEN_MENU", buffer, path[data->index]);
1236 wfree(buffer);
1237 if (data->name)
1238 wfree(data->name);
1239 wfree(data);
1242 WM_ITERATE_ARRAY(files, data, iter) {
1243 /* executable: add as entry */
1244 length = strlen(path[data->index]) + strlen(data->name) + 6;
1245 if (command)
1246 length += strlen(command);
1248 buffer = malloc(length);
1249 if (!buffer) {
1250 werror(_("out of memory while constructing directory menu %s"), path[data->index]);
1251 break;
1254 have_space = strchr(path[data->index], ' ') != NULL || strchr(data->name, ' ') != NULL;
1255 if (command != NULL) {
1256 strcpy(buffer, command);
1257 strcat(buffer, " ");
1258 if (have_space)
1259 strcat(buffer, "\"");
1260 strcat(buffer, path[data->index]);
1261 } else {
1262 if (have_space) {
1263 buffer[0] = '"';
1264 buffer[1] = 0;
1265 strcat(buffer, path[data->index]);
1266 } else {
1267 strcpy(buffer, path[data->index]);
1270 strcat(buffer, "/");
1271 strcat(buffer, data->name);
1272 if (have_space)
1273 strcat(buffer, "\"");
1275 if (stripExtension) {
1276 char *ptr = strrchr(data->name, '.');
1277 if (ptr && ptr != data->name)
1278 *ptr = 0;
1280 addMenuEntry(menu, M_(data->name), NULL, "SHEXEC", buffer, path[data->index]);
1282 wfree(buffer);
1283 if (data->name)
1284 wfree(data->name);
1285 wfree(data);
1288 WMFreeArray(files);
1289 WMFreeArray(dirs);
1291 return menu;
1294 /************ Menu Configuration From WMRootMenu *************/
1296 static WMenu *makeDefaultMenu(WScreen * scr)
1298 WMenu *menu = NULL;
1300 menu = wMenuCreate(scr, _("Commands"), True);
1301 wMenuAddCallback(menu, M_("XTerm"), execCommand, "xterm");
1302 wMenuAddCallback(menu, M_("rxvt"), execCommand, "rxvt");
1303 wMenuAddCallback(menu, _("Restart"), restartCommand, NULL);
1304 wMenuAddCallback(menu, _("Exit..."), exitCommand, NULL);
1305 return menu;
1309 *----------------------------------------------------------------------
1310 * configureMenu--
1311 * Reads root menu configuration from defaults database.
1313 *----------------------------------------------------------------------
1315 static WMenu *configureMenu(WScreen * scr, WMPropList * definition, Bool includeGlobals)
1317 WMenu *menu = NULL;
1318 WMPropList *elem;
1319 int i, count;
1320 WMPropList *title, *command, *params;
1321 char *tmp, *mtitle;
1323 if (WMIsPLString(definition)) {
1324 struct stat stat_buf;
1325 char *path = NULL;
1326 Bool menu_is_default = False;
1328 /* menu definition is a string. Probably a path, so parse the file */
1330 tmp = wexpandpath(WMGetFromPLString(definition));
1332 path = getLocalizedMenuFile(tmp);
1334 if (!path)
1335 path = wfindfile(DEF_CONFIG_PATHS, tmp);
1337 if (!path) {
1338 path = wfindfile(DEF_CONFIG_PATHS, DEF_MENU_FILE);
1339 menu_is_default = True;
1342 if (!path) {
1343 werror(_("could not find menu file \"%s\" referenced in WMRootMenu"), tmp);
1344 wfree(tmp);
1345 return NULL;
1348 if (stat(path, &stat_buf) < 0) {
1349 werror(_("could not access menu \"%s\" referenced in WMRootMenu"), path);
1350 wfree(path);
1351 wfree(tmp);
1352 return NULL;
1355 if (!scr->root_menu || stat_buf.st_mtime > scr->root_menu->timestamp
1356 /* if the pointer in WMRootMenu has changed */
1357 || WDRootMenu->timestamp > scr->root_menu->timestamp) {
1359 if (menu_is_default) {
1360 wwarning(_
1361 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1362 path);
1365 menu = readMenuFile(scr, path);
1366 if (menu)
1367 menu->timestamp = WMAX(stat_buf.st_mtime, WDRootMenu->timestamp);
1368 } else {
1369 menu = NULL;
1371 wfree(path);
1372 wfree(tmp);
1374 return menu;
1377 count = WMGetPropListItemCount(definition);
1378 if (count == 0)
1379 return NULL;
1381 elem = WMGetFromPLArray(definition, 0);
1382 if (!WMIsPLString(elem)) {
1383 tmp = WMGetPropListDescription(elem, False);
1384 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp);
1385 wfree(tmp);
1386 return NULL;
1388 mtitle = WMGetFromPLString(elem);
1390 menu = wMenuCreate(scr, M_(mtitle), False);
1391 menu->on_destroy = removeShortcutsForMenu;
1393 #ifdef GLOBAL_SUBMENU_FILE
1394 if (includeGlobals) {
1395 WMenu *submenu;
1396 WMenuEntry *mentry;
1398 submenu = readMenuFile(scr, GLOBAL_SUBMENU_FILE);
1400 if (submenu) {
1401 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL, NULL);
1402 wMenuEntrySetCascade(menu, mentry, submenu);
1405 #endif
1407 for (i = 1; i < count; i++) {
1408 elem = WMGetFromPLArray(definition, i);
1409 #if 0
1410 if (WMIsPLString(elem)) {
1411 char *file;
1413 file = WMGetFromPLString(elem);
1416 #endif
1417 if (!WMIsPLArray(elem) || WMGetPropListItemCount(elem) < 2)
1418 goto error;
1420 if (WMIsPLArray(WMGetFromPLArray(elem, 1))) {
1421 WMenu *submenu;
1422 WMenuEntry *mentry;
1424 /* submenu */
1425 submenu = configureMenu(scr, elem, True);
1426 if (submenu) {
1427 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL, NULL);
1428 wMenuEntrySetCascade(menu, mentry, submenu);
1430 } else {
1431 int idx = 0;
1432 WMPropList *shortcut;
1433 /* normal entry */
1435 title = WMGetFromPLArray(elem, idx++);
1436 shortcut = WMGetFromPLArray(elem, idx++);
1437 if (strcmp(WMGetFromPLString(shortcut), "SHORTCUT") == 0) {
1438 shortcut = WMGetFromPLArray(elem, idx++);
1439 command = WMGetFromPLArray(elem, idx++);
1440 } else {
1441 command = shortcut;
1442 shortcut = NULL;
1444 params = WMGetFromPLArray(elem, idx++);
1446 if (!title || !command)
1447 goto error;
1449 addMenuEntry(menu, M_(WMGetFromPLString(title)),
1450 shortcut ? WMGetFromPLString(shortcut) : NULL,
1451 WMGetFromPLString(command),
1452 params ? WMGetFromPLString(params) : NULL, "WMRootMenu");
1454 continue;
1456 error:
1457 tmp = WMGetPropListDescription(elem, False);
1458 wwarning(_("%s:format error in root menu configuration \"%s\""), "WMRootMenu", tmp);
1459 wfree(tmp);
1462 return menu;
1466 *----------------------------------------------------------------------
1467 * OpenRootMenu--
1468 * Opens the root menu, parsing the menu configuration from the
1469 * defaults database.
1470 * If the menu is already mapped and is not sticked to the
1471 * root window, it will be unmapped.
1473 * Side effects:
1474 * The menu may be remade.
1476 * Notes:
1477 * Construction of OPEN_MENU entries are delayed to the moment the
1478 * user map's them.
1479 *----------------------------------------------------------------------
1481 void OpenRootMenu(WScreen * scr, int x, int y, int keyboard)
1483 WMenu *menu = NULL;
1484 WMPropList *definition;
1486 static WMPropList *domain=NULL;
1488 if (!domain) {
1489 domain = WMCreatePLString("WMRootMenu");
1493 scr->flags.root_menu_changed_shortcuts = 0;
1494 scr->flags.added_workspace_menu = 0;
1495 scr->flags.added_windows_menu = 0;
1497 if (scr->root_menu && scr->root_menu->flags.mapped) {
1498 menu = scr->root_menu;
1499 if (!menu->flags.buttoned) {
1500 wMenuUnmap(menu);
1501 } else {
1502 wRaiseFrame(menu->frame->core);
1504 if (keyboard)
1505 wMenuMapAt(menu, 0, 0, True);
1506 else
1507 wMenuMapCopyAt(menu, x - menu->frame->core->width / 2, y);
1509 return;
1512 definition = WDRootMenu->dictionary;
1515 definition = PLGetDomain(domain);
1517 if (definition) {
1518 if (WMIsPLArray(definition)) {
1519 if (!scr->root_menu || WDRootMenu->timestamp > scr->root_menu->timestamp) {
1520 menu = configureMenu(scr, definition, True);
1521 if (menu)
1522 menu->timestamp = WDRootMenu->timestamp;
1524 } else
1525 menu = NULL;
1526 } else {
1527 menu = configureMenu(scr, definition, True);
1531 if (!menu) {
1532 /* menu hasn't changed or could not be read */
1533 if (!scr->root_menu) {
1534 wMessageDialog(scr, _("Error"),
1535 _("The applications menu could not be loaded. "
1536 "Look at the console output for a detailed "
1537 "description of the errors."), _("OK"), NULL, NULL);
1539 menu = makeDefaultMenu(scr);
1540 scr->root_menu = menu;
1542 menu = scr->root_menu;
1543 } else {
1544 /* new root menu */
1545 if (scr->root_menu) {
1546 wMenuDestroy(scr->root_menu, True);
1548 scr->root_menu = menu;
1550 if (menu) {
1551 int newx, newy;
1553 if (keyboard && x == 0 && y == 0) {
1554 newx = newy = 0;
1555 } else if (keyboard && x == scr->scr_width / 2 && y == scr->scr_height / 2) {
1556 newx = x - menu->frame->core->width / 2;
1557 newy = y - menu->frame->core->height / 2;
1558 } else {
1559 newx = x - menu->frame->core->width / 2;
1560 newy = y;
1562 wMenuMapAt(menu, newx, newy, keyboard);
1565 if (scr->flags.root_menu_changed_shortcuts)
1566 rebindKeygrabs(scr);