fixed some compilation warnings.
[wmaker-crm.git] / src / rootmenu.c
blob68963402817e790f61548bda75e5fbae6615b451
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 <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/wait.h>
34 #include <sys/types.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <time.h>
38 #include <dirent.h>
40 #include <X11/Xlib.h>
41 #include <X11/Xutil.h>
42 #include <X11/Xatom.h>
44 #include "WindowMaker.h"
45 #include "actions.h"
46 #include "menu.h"
47 #include "funcs.h"
48 #include "dialog.h"
49 #include "keybind.h"
50 #include "stacking.h"
51 #include "workspace.h"
52 #include "defaults.h"
53 #include "framewin.h"
54 #include "session.h"
55 #include "xmodifier.h"
56 #include <proplist.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 * SAVE_SESSION - saves the current state of the desktop, which include
132 * all running applications, all their hints (geometry,
133 * position on screen, workspace they live on, the dock
134 * or clip from where they were launched, and
135 * if minimized, shaded or hidden. Also saves the current
136 * workspace the user is on. All will be restored on every
137 * start of windowmaker until another SAVE_SESSION or
138 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
139 * WindowMaker domain file, then saving is automatically
140 * done on every windowmaker exit, overwriting any
141 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
142 * dock state now.
143 * CLEAR_SESSION - clears any previous saved session. This will not have
144 * any effect if SaveSessionOnExit is True.
148 #define MAX(a,b) ((a)>(b) ? (a) : (b))
151 #define M_QUICK 1
153 /* menu commands */
155 static void
156 execCommand(WMenu *menu, WMenuEntry *entry)
158 char *cmdline;
160 cmdline = ExpandOptions(menu->frame->screen_ptr, (char*)entry->clientdata);
162 XGrabPointer(dpy, menu->frame->screen_ptr->root_win, True, 0,
163 GrabModeAsync, GrabModeAsync, None, wCursor[WCUR_WAIT],
164 CurrentTime);
165 XSync(dpy, 0);
167 if (cmdline) {
168 ExecuteShellCommand(menu->frame->screen_ptr, cmdline);
169 free(cmdline);
171 XUngrabPointer(dpy, CurrentTime);
172 XSync(dpy, 0);
176 static void
177 exitCommand(WMenu *menu, WMenuEntry *entry)
179 static int inside = 0;
181 /* prevent reentrant calls */
182 if (inside)
183 return;
184 inside = 1;
186 if ((long)entry->clientdata==M_QUICK
187 || wMessageDialog(menu->frame->screen_ptr, _("Exit"),
188 _("Exit window manager?"),
189 _("Exit"), _("Cancel"), NULL)==WAPRDefault) {
190 #ifdef DEBUG
191 printf("Exiting WindowMaker.\n");
192 #endif
193 Shutdown(WSExitMode);
195 inside = 0;
199 static void
200 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
215 result = R_CANCEL;
216 if ((long)entry->clientdata==M_QUICK)
217 result = R_CLOSE;
218 else {
219 #ifdef XSMP_ENABLED
220 if (wSessionIsManaged()) {
221 int r;
223 r = wMessageDialog(menu->frame->screen_ptr,
224 _("Close X session"),
225 _("Close Window System session?\n"
226 "Kill might close applications with unsaved data."),
227 _("Close"), _("Kill"), _("Cancel"));
228 if (r==WAPRDefault)
229 result = R_CLOSE;
230 else if (r==WAPRAlternate)
231 result = R_KILL;
232 } else
233 #endif
235 int r;
237 r = wMessageDialog(menu->frame->screen_ptr,
238 _("Kill X session"),
239 _("Kill Window System session?\n"
240 "(all applications will be closed)"),
241 _("Kill"), _("Cancel"), NULL);
242 if (r==WAPRDefault)
243 result = R_KILL;
247 if (result!=R_CANCEL) {
248 #ifdef XSMP_ENABLED
249 if (result == R_CLOSE) {
250 Shutdown(WSLogoutMode);
251 } else
252 #endif /* XSMP_ENABLED */
254 Shutdown(WSKillMode);
257 #undef R_CLOSE
258 #undef R_CANCEL
259 #undef R_KILL
260 inside = 0;
264 static void
265 restartCommand(WMenu *menu, WMenuEntry *entry)
267 Shutdown(WSRestartPreparationMode);
268 Restart((char*)entry->clientdata, False);
269 Restart(NULL, True);
273 static void
274 refreshCommand(WMenu *menu, WMenuEntry *entry)
276 wRefreshDesktop(menu->frame->screen_ptr);
280 static void
281 arrangeIconsCommand(WMenu *menu, WMenuEntry *entry)
283 wArrangeIcons(menu->frame->screen_ptr, True);
286 static void
287 showAllCommand(WMenu *menu, WMenuEntry *entry)
289 wShowAllWindows(menu->frame->screen_ptr);
292 static void
293 hideOthersCommand(WMenu *menu, WMenuEntry *entry)
295 wHideOtherApplications(menu->frame->screen_ptr->focused_window);
299 static void
300 saveSessionCommand(WMenu *menu, WMenuEntry *entry)
302 if (!wPreferences.save_session_on_exit)
303 wSessionSaveState(menu->frame->screen_ptr);
305 wScreenSaveState(menu->frame->screen_ptr);
309 static void
310 clearSessionCommand(WMenu *menu, WMenuEntry *entry)
312 wSessionClearState(menu->frame->screen_ptr);
316 static void
317 infoPanelCommand(WMenu *menu, WMenuEntry *entry)
319 wShowInfoPanel(menu->frame->screen_ptr);
323 static void
324 legalPanelCommand(WMenu *menu, WMenuEntry *entry)
326 wShowLegalPanel(menu->frame->screen_ptr);
330 /********************************************************************/
333 static char*
334 getLocalizedMenuFile(char *menu)
336 char *buffer;
337 char *ptr;
339 if (!Locale)
340 return NULL;
342 buffer = wmalloc(strlen(menu)+32);
344 /* try menu.locale_name */
345 sprintf(buffer, "%s.%s", menu, Locale);
346 if (access(buffer, F_OK)==0) {
347 return buffer;
349 /* check if it is in the form aa_bb.encoding and check for aa_bb */
350 ptr = strchr(Locale, '.');
351 if (ptr) {
352 *ptr = 0;
353 if (access(buffer, F_OK)==0) {
354 return buffer;
357 /* now check for aa */
358 ptr = strchr(buffer, '_');
359 if (ptr) {
360 *ptr = 0;
361 if (access(buffer, F_OK)==0) {
362 return buffer;
366 return NULL;
370 static void
371 raiseMenus(WMenu *menu)
373 int i;
375 if (menu->flags.mapped) {
376 wRaiseFrame(menu->frame->core);
378 for (i=0; i<menu->cascade_no; i++) {
379 if (menu->cascades[i])
380 raiseMenus(menu->cascades[i]);
386 Bool
387 wRootMenuPerformShortcut(XEvent *event)
389 Shortcut *ptr;
390 int modifiers;
391 int done = 0;
393 /* ignore CapsLock */
394 modifiers = event->xkey.state & ValidModMask;
396 for (ptr = shortcutList; ptr!=NULL; ptr = ptr->next) {
397 if (ptr->keycode==0)
398 continue;
400 if (ptr->keycode==event->xkey.keycode && (ptr->modifier==modifiers)) {
401 (*ptr->entry->callback)(ptr->menu, ptr->entry);
402 done = True;
405 return done;
409 void
410 wRootMenuBindShortcuts(Window window)
412 Shortcut *ptr;
414 ptr = shortcutList;
415 while (ptr) {
416 if (ptr->modifier!=AnyModifier) {
417 XGrabKey(dpy, ptr->keycode, ptr->modifier|LockMask,
418 window, True, GrabModeAsync, GrabModeAsync);
419 #ifdef NUMLOCK_HACK
420 wHackedGrabKey(ptr->keycode, ptr->modifier,
421 window, True, GrabModeAsync, GrabModeAsync);
422 #endif
424 XGrabKey(dpy, ptr->keycode, ptr->modifier, window, True,
425 GrabModeAsync, GrabModeAsync);
426 ptr = ptr->next;
431 static void
432 rebindKeygrabs(WScreen *scr)
434 WWindow *wwin;
436 wwin = scr->focused_window;
438 while (wwin!=NULL) {
439 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->frame->core->window);
441 if (!WFLAGP(wwin, no_bind_keys)) {
442 wWindowSetKeyGrabs(wwin);
444 wwin = wwin->prev;
449 static void
450 removeShortcutsForMenu(WMenu *menu)
452 Shortcut *ptr, *tmp;
453 Shortcut *newList = NULL;
455 ptr = shortcutList;
456 while (ptr!=NULL) {
457 tmp = ptr->next;
458 if (ptr->menu == menu) {
459 free(ptr);
460 } else {
461 ptr->next = newList;
462 newList = ptr;
464 ptr = tmp;
466 shortcutList = newList;
467 menu->menu->screen_ptr->flags.root_menu_changed_shortcuts = 1;
471 static Bool
472 addShortcut(char *file, char *shortcutDefinition, WMenu *menu,
473 WMenuEntry *entry)
475 Shortcut *ptr;
476 KeySym ksym;
477 char *k;
478 char buf[128], *b;
480 ptr = wmalloc(sizeof(Shortcut));
482 strcpy(buf, shortcutDefinition);
483 b = (char*)buf;
485 /* get modifiers */
486 ptr->modifier = 0;
487 while ((k = strchr(b, '+'))!=NULL) {
488 int mod;
490 *k = 0;
491 mod = wXModifierFromKey(b);
492 if (mod<0) {
493 wwarning(_("%s:invalid key modifier \"%s\""), file, b);
494 free(ptr);
495 return False;
497 ptr->modifier |= mod;
499 b = k+1;
502 /* get key */
503 ksym = XStringToKeysym(b);
505 if (ksym==NoSymbol) {
506 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
507 file, shortcutDefinition, entry->text);
508 free(ptr);
509 return False;
512 ptr->keycode = XKeysymToKeycode(dpy, ksym);
513 if (ptr->keycode==0) {
514 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file,
515 shortcutDefinition, entry->text);
516 free(ptr);
517 return False;
520 ptr->menu = menu;
521 ptr->entry = entry;
523 ptr->next = shortcutList;
524 shortcutList = ptr;
526 menu->menu->screen_ptr->flags.root_menu_changed_shortcuts = 1;
528 return True;
532 /*******************************/
534 static char*
535 cropline(char *line)
537 char *end;
539 if (strlen(line)==0)
540 return line;
542 end = &(line[strlen(line)])-1;
543 while (isspace(*line) && *line!=0) line++;
544 while (end>line && isspace(*end)) {
545 *end=0;
546 end--;
548 return line;
552 static char*
553 next_token(char *line, char **next)
555 char *tmp, c;
556 char *ret;
558 *next = NULL;
559 while (*line==' ' || *line=='\t') line++;
561 tmp = line;
563 if (*tmp=='"') {
564 tmp++; line++;
565 while (*tmp!=0 && *tmp!='"') tmp++;
566 if (*tmp!='"') {
567 wwarning(_("%s: unmatched '\"' in menu file"), line);
568 return NULL;
570 } else {
571 do {
572 if (*tmp=='\\')
573 tmp++;
575 if (*tmp!=0)
576 tmp++;
578 } while (*tmp!=0 && *tmp!=' ' && *tmp!='\t');
581 c = *tmp;
582 *tmp = 0;
583 ret = wstrdup(line);
584 *tmp = c;
586 if (c==0)
587 return ret;
588 else
589 tmp++;
591 /* skip blanks */
592 while (*tmp==' ' || *tmp=='\t') tmp++;
594 if (*tmp!=0)
595 *next = tmp;
597 return ret;
601 static void
602 separateCommand(char *line, char ***file, char **command)
604 char *token, *tmp = line;
605 WMBag *bag = WMCreateBag(4);
606 int count, i;
608 *file = NULL;
609 *command = NULL;
610 do {
611 token = next_token(tmp, &tmp);
612 if (token) {
613 if (strcmp(token, "WITH")==0) {
614 if (tmp!=NULL && *tmp!=0)
615 *command = wstrdup(tmp);
616 else
617 wwarning(_("%s: missing command"), line);
618 break;
620 WMPutInBag(bag, token);
622 } while (token!=NULL && tmp!=NULL);
624 count = WMGetBagItemCount(bag);
625 if (count>0) {
626 int j;
627 *file = wmalloc(sizeof(char*)*(count+1));
628 i = count;
629 (*file)[count] = NULL;
630 for (j = 0; j < count; j++) {
631 (*file)[--i] = WMGetFromBag(bag, j);
634 WMFreeBag(bag);
638 static void
639 constructMenu(WMenu *menu, WMenuEntry *entry)
641 WMenu *submenu;
642 struct stat stat_buf;
643 char **path;
644 char *cmd;
645 char *lpath = NULL;
646 int i, first=-1;
647 time_t last=0;
649 separateCommand((char*)entry->clientdata, &path, &cmd);
650 if (!path || *path==NULL || **path==0) {
651 wwarning(_("invalid OPEN_MENU specification: %s"),
652 (char*)entry->clientdata);
653 return;
656 if (path[0][0]=='|') {
657 /* pipe menu */
659 if (!menu->cascades[entry->cascade] ||
660 menu->cascades[entry->cascade]->timestamp == 0) {
661 /* parse pipe */
663 submenu = readMenuPipe(menu->frame->screen_ptr, path);
665 /* there's no automatic reloading */
666 if(submenu != NULL)
667 submenu->timestamp = 1;
668 } else {
669 submenu = NULL;
672 } else {
673 i=0;
674 while(path[i] != NULL) {
675 char *tmp;
677 if (strcmp(path[i], "-noext")==0) {
678 i++;
679 continue;
682 tmp = wexpandpath(path[i]);
683 free(path[i]);
684 lpath = getLocalizedMenuFile(tmp);
685 if (lpath) {
686 free(tmp);
687 path[i] = lpath;
688 lpath = NULL;
689 } else {
690 path[i] = tmp;
693 if (stat(path[i], &stat_buf)==0) {
694 if (last < stat_buf.st_mtime)
695 last = stat_buf.st_mtime;
696 if (first<0)
697 first=i;
698 } else {
699 wsyserror(_("%s:could not stat menu"), path[i]);
700 /*goto finish;*/
703 i++;
706 if (first < 0) {
707 wsyserror(_("%s:could not stat menu:%s"), "OPEN_MENU",
708 (char*)entry->clientdata);
709 goto finish;
711 stat(path[first], &stat_buf);
712 if (!menu->cascades[entry->cascade]
713 || menu->cascades[entry->cascade]->timestamp < last) {
715 if (S_ISDIR(stat_buf.st_mode)) {
716 /* menu directory */
717 submenu = readMenuDirectory(menu->frame->screen_ptr,
718 entry->text, path, cmd);
719 if (submenu)
720 submenu->timestamp = last;
721 } else if (S_ISREG(stat_buf.st_mode)) {
722 /* menu file */
724 if (cmd || path[1])
725 wwarning(_("too many parameters in OPEN_MENU: %s"),
726 (char*)entry->clientdata);
728 submenu = readMenuFile(menu->frame->screen_ptr, path[first]);
729 if (submenu)
730 submenu->timestamp = stat_buf.st_mtime;
731 } else {
732 submenu = NULL;
734 } else {
735 submenu = NULL;
739 if (submenu) {
740 wMenuEntryRemoveCascade(menu, entry);
741 wMenuEntrySetCascade(menu, entry, submenu);
744 finish:
745 i = 0;
746 while (path[i]!=NULL)
747 free(path[i++]);
748 free(path);
749 if (cmd)
750 free(cmd);
754 static WMenuEntry*
755 addWorkspaceMenu(WScreen *scr, WMenu *menu, char *title)
757 WMenu *wsmenu;
758 WMenuEntry *entry;
760 if (scr->flags.added_workspace_menu) {
761 wwarning(_("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
762 return NULL;
763 } else {
764 scr->flags.added_workspace_menu = 1;
766 wsmenu = wWorkspaceMenuMake(scr, True);
767 scr->workspace_menu = wsmenu;
768 entry = wMenuAddCallback(menu, title, NULL, NULL);
769 wMenuEntrySetCascade(menu, entry, wsmenu);
771 wWorkspaceMenuUpdate(scr, wsmenu);
773 return entry;
777 static WMenuEntry*
778 addMenuEntry(WMenu *menu, char *title, char *shortcut, char *command,
779 char *params, char *file_name)
781 WScreen *scr;
782 WMenuEntry *entry = NULL;
783 Bool shortcutOk = False;
785 if (!menu)
786 return NULL;
787 scr = menu->frame->screen_ptr;
788 if (strcmp(command, "OPEN_MENU")==0) {
789 if (!params) {
790 wwarning(_("%s:missing parameter for menu command \"%s\""),
791 file_name, command);
792 } else {
793 WMenu *dummy;
794 char *path;
796 path = wfindfile(DEF_CONFIG_PATHS, params);
797 if (!path) {
798 path = wstrdup(params);
800 dummy = wMenuCreate(scr, title, False);
801 dummy->on_destroy = removeShortcutsForMenu;
802 entry = wMenuAddCallback(menu, title, constructMenu, path);
803 entry->free_cdata = free;
804 wMenuEntrySetCascade(menu, entry, dummy);
806 } else if (strcmp(command, "EXEC")==0) {
807 if (!params)
808 wwarning(_("%s:missing parameter for menu command \"%s\""),
809 file_name, command);
810 else {
811 entry = wMenuAddCallback(menu, title, execCommand,
812 wstrappend("exec ", params));
813 entry->free_cdata = free;
814 shortcutOk = True;
816 } else if (strcmp(command, "SHEXEC")==0) {
817 if (!params)
818 wwarning(_("%s:missing parameter for menu command \"%s\""),
819 file_name, command);
820 else {
821 entry = wMenuAddCallback(menu, title, execCommand,
822 wstrdup(params));
823 entry->free_cdata = free;
824 shortcutOk = True;
826 } else if (strcmp(command, "EXIT")==0) {
828 if (params && strcmp(params, "QUICK")==0)
829 entry = wMenuAddCallback(menu, title, exitCommand, (void*)M_QUICK);
830 else
831 entry = wMenuAddCallback(menu, title, exitCommand, NULL);
833 shortcutOk = True;
834 } else if (strcmp(command, "SHUTDOWN")==0) {
836 if (params && strcmp(params, "QUICK")==0)
837 entry = wMenuAddCallback(menu, title, shutdownCommand,
838 (void*)M_QUICK);
839 else
840 entry = wMenuAddCallback(menu, title, shutdownCommand, NULL);
842 shortcutOk = True;
843 } else if (strcmp(command, "REFRESH")==0) {
844 entry = wMenuAddCallback(menu, title, refreshCommand, NULL);
846 shortcutOk = True;
847 } else if (strcmp(command, "WORKSPACE_MENU")==0) {
848 entry = addWorkspaceMenu(scr, menu, title);
850 shortcutOk = True;
851 } else if (strcmp(command, "ARRANGE_ICONS")==0) {
852 entry = wMenuAddCallback(menu, title, arrangeIconsCommand, NULL);
854 shortcutOk = True;
855 } else if (strcmp(command, "HIDE_OTHERS")==0) {
856 entry = wMenuAddCallback(menu, title, hideOthersCommand, NULL);
858 shortcutOk = True;
859 } else if (strcmp(command, "SHOW_ALL")==0) {
860 entry = wMenuAddCallback(menu, title, showAllCommand, NULL);
862 shortcutOk = True;
863 } else if (strcmp(command, "RESTART")==0) {
864 entry = wMenuAddCallback(menu, title, restartCommand,
865 params ? wstrdup(params) : NULL);
866 entry->free_cdata = free;
867 shortcutOk = True;
868 } else if (strcmp(command, "SAVE_SESSION")==0) {
869 entry = wMenuAddCallback(menu, title, saveSessionCommand, NULL);
871 shortcutOk = True;
872 } else if (strcmp(command, "CLEAR_SESSION")==0) {
873 entry = wMenuAddCallback(menu, title, clearSessionCommand, NULL);
874 shortcutOk = True;
875 } else if (strcmp(command, "INFO_PANEL")==0) {
876 entry = wMenuAddCallback(menu, title, infoPanelCommand, NULL);
877 shortcutOk = True;
878 } else if (strcmp(command, "LEGAL_PANEL")==0) {
879 entry = wMenuAddCallback(menu, title, legalPanelCommand, NULL);
880 shortcutOk = True;
881 } else {
882 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name,
883 command);
885 return NULL;
888 if (shortcut && entry) {
889 if (!shortcutOk) {
890 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name,
891 title);
892 } else {
893 if (addShortcut(file_name, shortcut, menu, entry)) {
895 entry->rtext = GetShortcutString(shortcut);
897 entry->rtext = wstrdup(shortcut);
903 return entry;
908 /******************* Menu Configuration From File *******************/
910 static void
911 separateline(char *line, char *title, char *command, char *parameter,
912 char *shortcut)
914 int l, i;
916 l = strlen(line);
918 *title = 0;
919 *command = 0;
920 *parameter = 0;
921 *shortcut = 0;
922 /* get the title */
923 while (isspace(*line) && (*line!=0)) line++;
924 if (*line=='"') {
925 line++;
926 i=0;
927 while (line[i]!='"' && (line[i]!=0)) i++;
928 if (line[i]!='"')
929 return;
930 } else {
931 i=0;
932 while (!isspace(line[i]) && (line[i]!=0)) i++;
934 strncpy(title, line, i);
935 title[i++]=0;
936 line+=i;
938 /* get the command or shortcut keyword */
939 while (isspace(*line) && (*line!=0)) line++;
940 if (*line==0)
941 return;
942 i=0;
943 while (!isspace(line[i]) && (line[i]!=0)) i++;
944 strncpy(command, line, i);
945 command[i++]=0;
946 line+=i;
948 if (strcmp(command, "SHORTCUT")==0) {
949 /* get the shortcut key */
950 while (isspace(*line) && (*line!=0)) line++;
951 if (*line=='"') {
952 line++;
953 i=0;
954 while (line[i]!='"' && (line[i]!=0)) i++;
955 if (line[i]!='"')
956 return;
957 } else {
958 i=0;
959 while (!isspace(line[i]) && (line[i]!=0)) i++;
961 strncpy(shortcut, line, i);
962 shortcut[i++]=0;
963 line+=i;
965 *command=0;
967 /* get the command */
968 while (isspace(*line) && (*line!=0)) line++;
969 if (*line==0)
970 return;
971 i=0;
972 while (!isspace(line[i]) && (line[i]!=0)) i++;
973 strncpy(command, line, i);
974 command[i++]=0;
975 line+=i;
978 /* get the parameters */
979 while (isspace(*line) && (*line!=0)) line++;
980 if (*line==0)
981 return;
983 if (*line=='"') {
984 line++;
985 l = 0;
986 while (line[l]!=0 && line[l]!='"') {
987 parameter[l] = line[l];
988 l++;
990 parameter[l] = 0;
991 return;
994 l = strlen(line);
995 while (isspace(line[l]) && (l>0)) l--;
996 strncpy(parameter, line, l);
997 parameter[l]=0;
1001 static WMenu*
1002 parseCascade(WScreen *scr, WMenu *menu, FILE *file, char *file_name)
1004 char linebuf[MAXLINE];
1005 char elinebuf[MAXLINE];
1006 char title[MAXLINE];
1007 char command[MAXLINE];
1008 char shortcut[MAXLINE];
1009 char params[MAXLINE];
1010 char *line;
1012 while (!IsEof(file)) {
1013 int lsize, ok;
1015 ok = 0;
1016 fgets(linebuf, MAXLINE, file);
1017 line = cropline(linebuf);
1018 lsize = strlen(line);
1019 do {
1020 if (line[lsize-1]=='\\') {
1021 char *line2;
1022 int lsize2;
1023 fgets(elinebuf, MAXLINE, file);
1024 line2=cropline(elinebuf);
1025 lsize2=strlen(line2);
1026 if (lsize2+lsize>MAXLINE) {
1027 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1028 file_name, line);
1029 ok=2;
1030 } else {
1031 line[lsize-1]=0;
1032 lsize+=lsize2-1;
1033 strcat(line, line2);
1035 } else {
1036 ok=1;
1038 } while (!ok && !IsEof(file));
1039 if (ok==2)
1040 continue;
1042 if (line[0]==0 || line[0]=='#' || (line[0]=='/' && line[1]=='/'))
1043 continue;
1046 separateline(line, title, command, params, shortcut);
1048 if (!command[0]) {
1049 wwarning(_("%s:missing command in menu config: %s"), file_name,
1050 line);
1051 goto error;
1054 if (strcasecmp(command, "MENU")==0) {
1055 WMenu *cascade;
1057 /* start submenu */
1059 cascade = wMenuCreate(scr, title, False);
1060 cascade->on_destroy = removeShortcutsForMenu;
1061 if (parseCascade(scr, cascade, file, file_name)==NULL) {
1062 wMenuDestroy(cascade, True);
1063 } else {
1064 wMenuEntrySetCascade(menu,
1065 wMenuAddCallback(menu, title, NULL, NULL),
1066 cascade);
1068 } else if (strcasecmp(command, "END")==0) {
1069 /* end of menu */
1070 return menu;
1072 } else {
1073 /* normal items */
1074 addMenuEntry(menu, title, shortcut[0] ? shortcut : NULL, command,
1075 params[0] ? params : NULL, file_name);
1079 wwarning(_("%s:syntax error in menu file:END declaration missing"),
1080 file_name);
1081 return menu;
1083 error:
1084 return menu;
1088 static WMenu*
1089 readMenuFile(WScreen *scr, char *file_name)
1091 WMenu *menu=NULL;
1092 FILE *file = NULL;
1093 char linebuf[MAXLINE];
1094 char title[MAXLINE];
1095 char shortcut[MAXLINE];
1096 char command[MAXLINE];
1097 char params[MAXLINE];
1098 char *line;
1099 #ifdef USECPP
1100 char *args;
1101 int cpp = 0;
1102 #endif
1104 #ifdef USECPP
1105 if (!wPreferences.flags.nocpp) {
1106 args = MakeCPPArgs(file_name);
1107 if (!args) {
1108 wwarning(_("could not make arguments for menu file preprocessor"));
1109 } else {
1110 sprintf(command, "%s %s %s", CPP_PATH, args, file_name);
1111 free(args);
1112 file = popen(command, "r");
1113 if (!file) {
1114 wsyserror(_("%s:could not open/preprocess menu file"),
1115 file_name);
1116 } else {
1117 cpp = 1;
1121 #endif /* USECPP */
1123 if (!file) {
1124 file = fopen(file_name, "r");
1125 if (!file) {
1126 wsyserror(_("%s:could not open menu file"), file_name);
1127 return NULL;
1131 while (!IsEof(file)) {
1132 if (!fgets(linebuf, MAXLINE, file))
1133 break;
1134 line = cropline(linebuf);
1135 if (line[0]==0 || line[0]=='#' || (line[0]=='/' && line[1]=='/'))
1136 continue;
1138 separateline(line, title, command, params, shortcut);
1140 if (!command[0]) {
1141 wwarning(_("%s:missing command in menu config: %s"), file_name,
1142 line);
1143 break;
1145 if (strcasecmp(command, "MENU")==0) {
1146 menu = wMenuCreate(scr, title, True);
1147 menu->on_destroy = removeShortcutsForMenu;
1148 if (!parseCascade(scr, menu, file, file_name)) {
1149 wMenuDestroy(menu, True);
1151 break;
1152 } else {
1153 wwarning(_("%s:invalid menu file. MENU command is missing"),
1154 file_name);
1155 break;
1159 #ifdef CPP
1160 if (cpp) {
1161 if (pclose(file)==-1) {
1162 wsyserror(_("error reading preprocessed menu data"));
1164 } else {
1165 fclose(file);
1167 #else
1168 fclose(file);
1169 #endif
1171 return menu;
1174 /************ Menu Configuration From Pipe *************/
1176 static WMenu*
1177 readMenuPipe(WScreen *scr, char **file_name)
1179 WMenu *menu=NULL;
1180 FILE *file = NULL;
1181 char linebuf[MAXLINE];
1182 char title[MAXLINE];
1183 char command[MAXLINE];
1184 char params[MAXLINE];
1185 char shortcut[MAXLINE];
1186 char *line;
1187 char * filename;
1188 char flat_file[MAXLINE];
1189 int i;
1190 #ifdef USECPP
1191 char *args;
1192 int cpp = 0;
1193 #endif
1195 flat_file[0] = '\0';
1197 for(i = 0 ; file_name[i] != NULL ; i++) {
1198 strcat(flat_file, file_name[i]);
1199 strcat(flat_file, " ");
1201 filename = flat_file+1;
1203 #ifdef USECPP
1204 if (!wPreferences.flags.nocpp) {
1205 args = MakeCPPArgs(filename);
1206 if (!args) {
1207 wwarning(_("could not make arguments for menu file preprocessor"));
1208 } else {
1209 sprintf(command, "%s | %s %s", filename, CPP_PATH, args);
1211 free(args);
1212 file = popen(command, "r");
1213 if (!file) {
1214 wsyserror(_("%s:could not open/preprocess menu file"), filename);
1215 } else {
1216 cpp = 1;
1221 #endif /* USECPP */
1223 if (!file) {
1224 file = popen(filename, "r");
1226 if (!file) {
1227 wsyserror(_("%s:could not open menu file"), filename);
1228 return NULL;
1232 while (!IsEof(file)) {
1233 if (!fgets(linebuf, MAXLINE, file))
1234 break;
1235 line = cropline(linebuf);
1236 if (line[0]==0 || line[0]=='#' || (line[0]=='/' && line[1]=='/'))
1237 continue;
1239 separateline(line, title, command, params, shortcut);
1241 if (!command[0]) {
1242 wwarning(_("%s:missing command in menu config: %s"), file_name,
1243 line);
1244 break;
1246 if (strcasecmp(command, "MENU")==0) {
1247 menu = wMenuCreate(scr, title, True);
1248 menu->on_destroy = removeShortcutsForMenu;
1249 if (!parseCascade(scr, menu, file, filename)) {
1250 wMenuDestroy(menu, True);
1252 break;
1253 } else {
1254 wwarning(_("%s:no title given for the root menu"), filename);
1255 break;
1259 pclose(file);
1261 return menu;
1266 typedef struct {
1267 char *name;
1268 int index;
1269 } dir_data;
1272 static int
1273 myCompare(const void *d1, const void *d2)
1275 dir_data *p1 = *(dir_data**)d1;
1276 dir_data *p2 = *(dir_data**)d2;
1278 return strcmp(p1->name, p2->name);
1282 /************ Menu Configuration From Directory *************/
1284 static Bool
1285 isFilePackage(char *file)
1287 int l;
1289 /* check if the extension indicates this file is a
1290 * file package. For now, only recognize .themed */
1292 l = strlen(file);
1294 if (l > 7 && strcmp(&(file[l-7]), ".themed")==0) {
1295 return True;
1296 } else {
1297 return False;
1302 static WMenu*
1303 readMenuDirectory(WScreen *scr, char *title, char **path, char *command)
1305 DIR *dir;
1306 struct dirent *dentry;
1307 struct stat stat_buf;
1308 WMenu *menu=NULL;
1309 char *buffer;
1310 WMBag *dirs = NULL, *files = NULL;
1311 int length, i, have_space=0;
1312 dir_data *data;
1313 int stripExtension = 0;
1316 dirs = WMCreateBag(16);
1317 files = WMCreateBag(16);
1319 i=0;
1320 while (path[i]!=NULL) {
1321 if (strcmp(path[i], "-noext")==0) {
1322 stripExtension = 1;
1323 i++;
1324 continue;
1327 dir = opendir(path[i]);
1328 if (!dir) {
1329 i++;
1330 continue;
1333 while ((dentry = readdir(dir))) {
1335 if (strcmp(dentry->d_name, ".")==0 ||
1336 strcmp(dentry->d_name, "..")==0)
1337 continue;
1339 if (dentry->d_name[0] == '.')
1340 continue;
1342 buffer = malloc(strlen(path[i])+strlen(dentry->d_name)+4);
1343 if (!buffer) {
1344 wsyserror(_("out of memory while constructing directory menu %s"),
1345 path[i]);
1346 break;
1349 strcpy(buffer, path[i]);
1350 strcat(buffer, "/");
1351 strcat(buffer, dentry->d_name);
1353 if (stat(buffer, &stat_buf)!=0) {
1354 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1355 path[i], dentry->d_name);
1356 } else {
1357 Bool isFilePack = False;
1359 data = NULL;
1360 if (S_ISDIR(stat_buf.st_mode)
1361 && !(isFilePack = isFilePackage(dentry->d_name))) {
1363 /* access always returns success for user root */
1364 if (access(buffer, X_OK)==0) {
1365 /* Directory is accesible. Add to directory list */
1367 data = (dir_data*) wmalloc(sizeof(dir_data));
1368 data->name = wstrdup(dentry->d_name);
1369 data->index = i;
1371 WMPutInBag(dirs, data);
1373 } else if (S_ISREG(stat_buf.st_mode) || isFilePack) {
1374 /* Hack because access always returns X_OK success for user root */
1375 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1376 if ((command!=NULL && access(buffer, R_OK)==0) ||
1377 (command==NULL && access(buffer, X_OK)==0 &&
1378 (stat_buf.st_mode & S_IXANY))) {
1380 data = (dir_data*) wmalloc(sizeof(dir_data));
1381 data->name = wstrdup(dentry->d_name);
1382 data->index = i;
1384 WMPutInBag(files, data);
1388 free(buffer);
1391 closedir(dir);
1392 i++;
1395 if (!WMGetBagItemCount(dirs) && !WMGetBagItemCount(files)) {
1396 WMFreeBag(dirs);
1397 WMFreeBag(files);
1398 return NULL;
1401 WMSortBag(dirs, myCompare);
1402 WMSortBag(files, myCompare);
1404 menu = wMenuCreate(scr, title, False);
1405 menu->on_destroy = removeShortcutsForMenu;
1407 for (i = 0; i < WMGetBagItemCount(dirs); i++) {
1408 /* New directory. Use same OPEN_MENU command that was used
1409 * for the current directory. */
1410 dir_data *d = (dir_data*)WMGetFromBag(dirs, i);
1412 length = strlen(path[d->index])+strlen(d->name)+6;
1413 if (command)
1414 length += strlen(command) + 6;
1415 buffer = malloc(length);
1416 if (!buffer) {
1417 wsyserror(_("out of memory while constructing directory menu %s"),
1418 path[d->index]);
1419 break;
1422 have_space = strchr(path[d->index], ' ')!=NULL ||
1423 strchr(d->name, ' ')!=NULL;
1424 if (have_space) {
1425 buffer[0] = '"';
1426 buffer[1] = 0;
1427 strcat(buffer, path[d->index]);
1428 } else {
1429 strcpy(buffer, path[d->index]);
1431 strcat(buffer, "/");
1432 strcat(buffer, d->name);
1433 if (have_space)
1434 strcat(buffer, "\"");
1435 if (command) {
1436 strcat(buffer, " WITH ");
1437 strcat(buffer, command);
1440 addMenuEntry(menu, d->name, NULL, "OPEN_MENU", buffer, path[d->index]);
1442 free(buffer);
1443 if (d->name)
1444 free(d->name);
1445 free(d);
1448 for (i = 0; i < WMGetBagItemCount(files); i++) {
1449 /* executable: add as entry */
1450 dir_data *f = (dir_data*)WMGetFromBag(files, i);
1452 length = strlen(path[f->index])+strlen(f->name)+6;
1453 if (command)
1454 length += strlen(command);
1456 buffer = wmalloc(length);
1457 if (!buffer) {
1458 wsyserror(_("out of memory while constructing directory menu %s"),
1459 path[f->index]);
1460 break;
1463 have_space = strchr(path[f->index], ' ')!=NULL ||
1464 strchr(f->name, ' ')!=NULL;
1465 if (command!=NULL) {
1466 strcpy(buffer, command);
1467 strcat(buffer, " ");
1468 if (have_space)
1469 strcat(buffer, "\"");
1470 strcat(buffer, path[f->index]);
1471 } else {
1472 if (have_space) {
1473 buffer[0] = '"';
1474 buffer[1] = 0;
1475 strcat(buffer, path[f->index]);
1476 } else {
1477 strcpy(buffer, path[f->index]);
1480 strcat(buffer, "/");
1481 strcat(buffer, f->name);
1482 if (have_space)
1483 strcat(buffer, "\"");
1485 if (stripExtension) {
1486 char *ptr = strrchr(f->name, '.');
1487 if (ptr && ptr!=f->name)
1488 *ptr = 0;
1490 addMenuEntry(menu, f->name, NULL, "SHEXEC", buffer, path[f->index]);
1492 free(buffer);
1493 if (f->name)
1494 free(f->name);
1495 free(f);
1498 WMFreeBag(files);
1499 WMFreeBag(dirs);
1501 return menu;
1505 /************ Menu Configuration From WMRootMenu *************/
1507 static WMenu*
1508 makeDefaultMenu(WScreen *scr)
1510 WMenu *menu=NULL;
1512 menu = wMenuCreate(scr, _("Commands"), True);
1513 wMenuAddCallback(menu, "XTerm", execCommand, "xterm");
1514 wMenuAddCallback(menu, "rxvt", execCommand, "rxvt");
1515 wMenuAddCallback(menu, _("Restart"), restartCommand, NULL);
1516 wMenuAddCallback(menu, _("Exit..."), exitCommand, NULL);
1517 return menu;
1525 *----------------------------------------------------------------------
1526 * configureMenu--
1527 * Reads root menu configuration from defaults database.
1529 *----------------------------------------------------------------------
1531 static WMenu*
1532 configureMenu(WScreen *scr, proplist_t definition)
1534 WMenu *menu = NULL;
1535 proplist_t elem;
1536 int i, count;
1537 proplist_t title, command, params;
1538 char *tmp, *mtitle;
1541 if (PLIsString(definition)) {
1542 struct stat stat_buf;
1543 char *path = NULL;
1544 Bool menu_is_default = False;
1546 /* menu definition is a string. Probably a path, so parse the file */
1548 tmp = wexpandpath(PLGetString(definition));
1550 path = getLocalizedMenuFile(tmp);
1552 if (!path)
1553 path = wfindfile(DEF_CONFIG_PATHS, tmp);
1555 if (!path) {
1556 path = wfindfile(DEF_CONFIG_PATHS, DEF_MENU_FILE);
1557 menu_is_default = True;
1560 if (!path) {
1561 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"),
1562 tmp);
1563 free(tmp);
1564 return NULL;
1567 if (stat(path, &stat_buf)<0) {
1568 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path);
1569 free(path);
1570 free(tmp);
1571 return NULL;
1574 if (!scr->root_menu || stat_buf.st_mtime > scr->root_menu->timestamp
1575 /* if the pointer in WMRootMenu has changed */
1576 || WDRootMenu->timestamp > scr->root_menu->timestamp) {
1578 if (menu_is_default) {
1579 wwarning(_("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1580 path);
1583 menu = readMenuFile(scr, path);
1584 if (menu)
1585 menu->timestamp = MAX(stat_buf.st_mtime, WDRootMenu->timestamp);
1586 } else {
1587 menu = NULL;
1589 free(path);
1590 free(tmp);
1592 return menu;
1595 count = PLGetNumberOfElements(definition);
1596 if (count==0)
1597 return NULL;
1599 elem = PLGetArrayElement(definition, 0);
1600 if (!PLIsString(elem)) {
1601 tmp = PLGetDescription(elem);
1602 wwarning(_("%s:format error in root menu configuration \"%s\""),
1603 "WMRootMenu", tmp);
1604 free(tmp);
1605 return NULL;
1607 mtitle = PLGetString(elem);
1609 menu = wMenuCreate(scr, mtitle, False);
1610 menu->on_destroy = removeShortcutsForMenu;
1612 #ifdef GLOBAL_SUBMENU_FILE
1614 WMenu *submenu;
1615 WMenuEntry *mentry;
1617 submenu = readMenuFile(scr, GLOBAL_SUBMENU_FILE);
1619 if (submenu) {
1620 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL, NULL);
1621 wMenuEntrySetCascade(menu, mentry, submenu);
1624 #endif
1626 for (i=1; i<count; i++) {
1627 elem = PLGetArrayElement(definition, i);
1628 #if 0
1629 if (PLIsString(elem)) {
1630 char *file;
1632 file = PLGetString(elem);
1635 #endif
1636 if (!PLIsArray(elem) || PLGetNumberOfElements(elem) < 2)
1637 goto error;
1639 if (PLIsArray(PLGetArrayElement(elem,1))) {
1640 WMenu *submenu;
1641 WMenuEntry *mentry;
1643 /* submenu */
1644 submenu = configureMenu(scr, elem);
1645 if (submenu) {
1646 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL,
1647 NULL);
1648 wMenuEntrySetCascade(menu, mentry, submenu);
1650 } else {
1651 int idx = 0;
1652 char *shortcut;
1653 /* normal entry */
1655 title = PLGetArrayElement(elem, idx++);
1656 shortcut = PLGetArrayElement(elem, idx++);
1657 if (strcmp(PLGetString(shortcut), "SHORTCUT")==0) {
1658 shortcut = PLGetArrayElement(elem, idx++);
1659 command = PLGetArrayElement(elem, idx++);
1660 } else {
1661 command = shortcut;
1662 shortcut = NULL;
1664 params = PLGetArrayElement(elem, idx++);
1666 if (!title || !command)
1667 goto error;
1669 addMenuEntry(menu, PLGetString(title),
1670 shortcut ? PLGetString(shortcut) : NULL,
1671 PLGetString(command),
1672 params ? PLGetString(params) : NULL, "WMRootMenu");
1674 continue;
1676 error:
1677 tmp = PLGetDescription(elem);
1678 wwarning(_("%s:format error in root menu configuration \"%s\""),
1679 "WMRootMenu", tmp);
1680 free(tmp);
1683 return menu;
1694 *----------------------------------------------------------------------
1695 * OpenRootMenu--
1696 * Opens the root menu, parsing the menu configuration from the
1697 * defaults database.
1698 * If the menu is already mapped and is not sticked to the
1699 * root window, it will be unmapped.
1701 * Side effects:
1702 * The menu may be remade.
1704 * Notes:
1705 * Construction of OPEN_MENU entries are delayed to the moment the
1706 * user map's them.
1707 *----------------------------------------------------------------------
1709 void
1710 OpenRootMenu(WScreen *scr, int x, int y, int keyboard)
1712 WMenu *menu=NULL;
1713 proplist_t definition;
1715 static proplist_t domain=NULL;
1717 if (!domain) {
1718 domain = PLMakeString("WMRootMenu");
1722 scr->flags.root_menu_changed_shortcuts = 0;
1723 scr->flags.added_workspace_menu = 0;
1725 if (scr->root_menu && scr->root_menu->flags.mapped) {
1726 menu = scr->root_menu;
1727 if (!menu->flags.buttoned) {
1728 wMenuUnmap(menu);
1729 } else {
1730 wRaiseFrame(menu->frame->core);
1732 if (keyboard)
1733 wMenuMapAt(menu, 0, 0, True);
1734 else
1735 wMenuMapCopyAt(menu, x-menu->frame->core->width/2, y);
1737 return;
1741 definition = WDRootMenu->dictionary;
1744 definition = PLGetDomain(domain);
1746 if (definition) {
1747 if (PLIsArray(definition)) {
1748 if (!scr->root_menu
1749 || WDRootMenu->timestamp > scr->root_menu->timestamp) {
1750 menu = configureMenu(scr, definition);
1751 if (menu)
1752 menu->timestamp = WDRootMenu->timestamp;
1754 } else
1755 menu = NULL;
1756 } else {
1757 menu = configureMenu(scr, definition);
1761 if (!menu) {
1762 /* menu hasn't changed or could not be read */
1763 if (!scr->root_menu) {
1764 wMessageDialog(scr, _("Error"),
1765 _("The applications menu could not be loaded. "
1766 "Look at the console output for a detailed "
1767 "description of the errors."),
1768 _("OK"), NULL, NULL);
1770 menu = makeDefaultMenu(scr);
1771 scr->root_menu = menu;
1773 menu = scr->root_menu;
1774 } else {
1775 /* new root menu */
1776 if (scr->root_menu)
1777 wMenuDestroy(scr->root_menu, True);
1778 scr->root_menu = menu;
1780 if (menu) {
1781 wMenuMapAt(menu, x-menu->frame->core->width/2, y, keyboard);
1784 if (scr->flags.root_menu_changed_shortcuts)
1785 rebindKeygrabs(scr);
1788 #endif /* !LITE */