1 /* rootmenu.c- user defined menu
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
31 #include <sys/types.h>
38 #include <X11/Xutil.h>
39 #include <X11/Xatom.h>
41 #include "WindowMaker.h"
48 #include "workspace.h"
52 #include "xmodifier.h"
60 extern WDDomain
*WDRootMenu
;
62 extern wCursor
[WCUR_LAST
];
64 extern Time LastTimestamp
;
66 extern WPreferences wPreferences
;
69 static WMenu
*readMenuPipe(WScreen
*scr
, char **file_name
);
70 static WMenu
*readMenuFile(WScreen
*scr
, char *file_name
);
71 static WMenu
*readMenuDirectory(WScreen
*scr
, char *title
, char **file_name
,
75 typedef struct Shortcut
{
76 struct Shortcut
*next
;
86 static Shortcut
*shortcutList
= NULL
;
93 * "Title" EXEC command_to_exec -params
95 * "Title" EXEC command_to_exec -params
97 * "Workspaces" WORKSPACE_MENU
98 * "Title" built_in_command
100 * "Quick Quit" EXIT QUICK
103 * Commands may be preceded by SHORTCUT key
107 * INFO_PANEL - shows the Info Panel
108 * LEGAL_PANEL - shows the Legal info panel
109 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
110 * REFRESH - forces the desktop to be repainted
111 * EXIT [QUICK] - exit the window manager [without confirmation]
112 * EXEC <program> - execute an external program
113 * WORKSPACE_MENU - places the workspace submenu
115 * RESTART [<window manager>] - restarts the window manager
116 * SHOW_ALL - unhide all windows on workspace
117 * HIDE_OTHERS - hides all windows excep the focused one
118 * OPEN_MENU file - read menu data from file which must be a valid menu file.
119 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
120 * - read menu data from directory(ies) and
121 * eventually precede each with a command.
122 * OPEN_MENU | command
123 * - opens command and uses its stdout to construct and insert
124 * the resulting menu in current position. The output of
125 * command must be a valid menu description.
126 * The space between '|' and command is optional.
127 * SAVE_SESSION - saves the current state of the desktop, which include
128 * all running applications, all their hints (geometry,
129 * position on screen, workspace they live on, the dock
130 * or clip from where they were launched, and
131 * if minimized, shaded or hidden. Also saves the current
132 * workspace the user is on. All will be restored on every
133 * start of windowmaker until another SAVE_SESSION or
134 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
135 * WindowMaker domain file, then saving is automatically
136 * done on every windowmaker exit, overwriting any
137 * SAVE_SESSION or CLEAR_SESSION (see below).
138 * CLEAR_SESSION - clears any previous saved session. This will not have
139 * any effect if SaveSessionOnExit is True.
143 #define MAX(a,b) ((a)>(b) ? (a) : (b))
151 execCommand(WMenu
*menu
, WMenuEntry
*entry
)
154 static char *shell
= NULL
;
157 shell
= getenv("SHELL");
162 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char*)entry
->clientdata
);
164 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
165 GrabModeAsync
, GrabModeAsync
, None
, wCursor
[WCUR_WAIT
],
170 /* We should not use ParseCommand() here, because it does not handle
171 * correctly '~/' expansion and '>' redirection.
172 * While we can do '~/' expansion ourselves, we can do nothing about
174 * So we better let /bin/sh to do that for us. Dan.
178 SetupEnvironment(menu
->frame
->screen_ptr
);
179 close(ConnectionNumber(dpy
));
183 execl(shell
, shell
, "-c", cmdline
, NULL
);
184 wsyserror("could not exec %s -c %s\n", shell
, cmdline
);
189 XUngrabPointer(dpy
, CurrentTime
);
195 exitCommand(WMenu
*menu
, WMenuEntry
*entry
)
197 static int inside
= 0;
199 /* prevent reentrant calls */
204 if ((int)entry
->clientdata
==M_QUICK
205 || wMessageDialog(menu
->frame
->screen_ptr
, _("Exit"),
206 _("Exit window manager?"),
207 _("Exit"), _("Cancel"), NULL
)==WAPRDefault
) {
209 printf("Exiting WindowMaker.\n");
212 wScreenSaveState(menu
->frame
->screen_ptr
);
214 RestoreDesktop(menu
->frame
->screen_ptr
);
224 shutdownCommand(WMenu
*menu
, WMenuEntry
*entry
)
226 static int inside
= 0;
228 /* prevent reentrant calls */
233 if ((int)entry
->clientdata
==M_QUICK
234 || wMessageDialog(menu
->frame
->screen_ptr
, _("Close X session"),
235 _("Close Window System session?\n(all applications will be closed)"),
236 _("Exit"), _("Cancel"), NULL
)==WAPRDefault
) {
237 /* printf(_("Exiting...\n"));*/
239 wScreenSaveState(menu
->frame
->screen_ptr
);
241 WipeDesktop(menu
->frame
->screen_ptr
);
251 restartCommand(WMenu
*menu
, WMenuEntry
*entry
)
253 wScreenSaveState(menu
->frame
->screen_ptr
);
255 RestoreDesktop(menu
->frame
->screen_ptr
);
256 Restart((char*)entry
->clientdata
);
261 refreshCommand(WMenu
*menu
, WMenuEntry
*entry
)
263 wRefreshDesktop(menu
->frame
->screen_ptr
);
268 arrangeIconsCommand(WMenu
*menu
, WMenuEntry
*entry
)
270 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
274 showAllCommand(WMenu
*menu
, WMenuEntry
*entry
)
276 wShowAllWindows(menu
->frame
->screen_ptr
);
280 hideOthersCommand(WMenu
*menu
, WMenuEntry
*entry
)
282 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
287 saveSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
289 wSessionSaveState(menu
->frame
->screen_ptr
);
294 clearSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
296 wSessionClearState(menu
->frame
->screen_ptr
);
301 infoPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
303 wShowInfoPanel(menu
->frame
->screen_ptr
);
308 legalPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
310 wShowLegalPanel(menu
->frame
->screen_ptr
);
315 /********************************************************************/
318 raiseMenus(WMenu
*menu
)
322 if (menu
->flags
.mapped
) {
323 wRaiseFrame(menu
->frame
->core
);
325 for (i
=0; i
<menu
->cascade_no
; i
++) {
326 if (menu
->cascades
[i
])
327 raiseMenus(menu
->cascades
[i
]);
334 wRootMenuPerformShortcut(XEvent
*event
)
340 /* ignore CapsLock */
341 modifiers
= event
->xkey
.state
& ValidModMask
;
343 for (ptr
= shortcutList
; ptr
!=NULL
; ptr
= ptr
->next
) {
347 if (ptr
->keycode
==event
->xkey
.keycode
&& (ptr
->modifier
==modifiers
)) {
348 (*ptr
->entry
->callback
)(ptr
->menu
, ptr
->entry
);
357 wRootMenuBindShortcuts(Window window
)
363 if (ptr
->modifier
!=AnyModifier
) {
364 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
|LockMask
,
365 window
, True
, GrabModeAsync
, GrabModeAsync
);
367 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
,
368 window
, True
, GrabModeAsync
, GrabModeAsync
);
371 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
,
372 GrabModeAsync
, GrabModeAsync
);
379 rebindKeygrabs(WScreen
*scr
)
383 wwin
= scr
->focused_window
;
386 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
388 if (!wwin
->window_flags
.no_bind_keys
) {
389 wWindowSetKeyGrabs(wwin
);
397 removeShortcutsForMenu(WMenu
*menu
)
400 Shortcut
*newList
= NULL
;
405 if (ptr
->menu
== menu
) {
413 shortcutList
= newList
;
414 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
419 addShortcut(char *file
, char *shortcutDefinition
, WMenu
*menu
,
427 ptr
= wmalloc(sizeof(Shortcut
));
429 strcpy(buf
, shortcutDefinition
);
434 while ((k
= strchr(b
, '+'))!=NULL
) {
438 mod
= wXModifierFromKey(b
);
440 wwarning(_("%s:invalid key modifier \"%s\""), file
,
445 ptr
->modifier
|= mod
;
451 ksym
= XStringToKeysym(b
);
453 if (ksym
==NoSymbol
) {
454 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
455 file
, shortcutDefinition
, entry
->text
);
460 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
461 if (ptr
->keycode
==0) {
462 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
463 shortcutDefinition
, entry
->text
);
471 ptr
->next
= shortcutList
;
474 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
480 /*******************************/
490 end
= &(line
[strlen(line
)])-1;
491 while (isspace(*line
) && *line
!=0) line
++;
492 while (end
>line
&& isspace(*end
)) {
501 next_token(char *line
, char **next
)
507 while (*line
==' ' || *line
=='\t') line
++;
513 while (*tmp
!=0 && *tmp
!='"') tmp
++;
515 wwarning(_("%s: unmatched '\"' in menu file"), line
);
526 } while (*tmp
!=0 && *tmp
!=' ' && *tmp
!='\t');
540 while (*tmp
==' ' || *tmp
=='\t') tmp
++;
550 separateCommand(char *line
, char ***file
, char **command
)
552 char *token
, *tmp
= line
;
553 LinkedList
*list
= NULL
;
559 token
= next_token(tmp
, &tmp
);
561 if (strcmp(token
, "WITH")==0) {
562 if (tmp
!=NULL
&& *tmp
!=0)
563 *command
= wstrdup(tmp
);
565 wwarning(_("%s: missing command"), line
);
568 list
= list_cons(token
, list
);
570 } while (token
!=NULL
&& tmp
!=NULL
);
572 count
= list_length(list
);
574 *file
= wmalloc(sizeof(char*)*(count
+1));
576 (*file
)[count
] = NULL
;
578 (*file
)[--i
] = list
->head
;
579 list_remove_head(&list
);
586 constructMenu(WMenu
*menu
, WMenuEntry
*entry
)
589 struct stat stat_buf
;
596 separateCommand((char*)entry
->clientdata
, &path
, &cmd
);
597 if (!path
|| *path
==NULL
|| **path
==0) {
598 wwarning(_("invalid OPEN_MENU specification: %s"),
599 (char*)entry
->clientdata
);
603 if (path
[0][0]=='|') {
606 if (!menu
->cascades
[entry
->cascade
] ||
607 menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
610 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
612 /* there's no automatic reloading */
614 submenu
->timestamp
= 1;
621 while(path
[i
] != NULL
) {
624 tmp
= wexpandpath(path
[i
]);
629 lpath
= wmalloc(strlen(path
[i
])+32);
631 strcpy(lpath
, path
[i
]);
633 strcat(lpath
, Locale
);
634 if (stat(lpath
, &stat_buf
)<0) {
638 lpath
[strlen(lpath
)-(i
-2)]=0;
639 if (stat(lpath
, &stat_buf
)==0) {
657 if (stat(path
[i
], &stat_buf
)==0) {
658 if (last
< stat_buf
.st_mtime
)
659 last
= stat_buf
.st_mtime
;
663 wsyserror(_("%s:could not stat menu"), path
[i
]);
671 wsyserror(_("%s:could not stat menu :%s"), "OPEN_MENU",
672 (char*)entry
->clientdata
);
675 stat(path
[first
], &stat_buf
);
676 if (!menu
->cascades
[entry
->cascade
]
677 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
679 if (S_ISDIR(stat_buf
.st_mode
)) {
681 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
,
682 entry
->text
, &path
[first
], cmd
);
684 submenu
->timestamp
= last
;
685 } else if (S_ISREG(stat_buf
.st_mode
)) {
689 wwarning(_("too many parameters in OPEN_MENU: %s"),
690 (char*)entry
->clientdata
);
692 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
694 submenu
->timestamp
= stat_buf
.st_mtime
;
704 wMenuEntryRemoveCascade(menu
, entry
);
705 wMenuEntrySetCascade(menu
, entry
, submenu
);
710 while (path
[i
]!=NULL
)
719 addWorkspaceMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
724 if (scr
->flags
.added_workspace_menu
) {
725 wwarning(_("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
728 scr
->flags
.added_workspace_menu
= 1;
730 wsmenu
= wWorkspaceMenuMake(scr
, True
);
731 scr
->workspace_menu
= wsmenu
;
732 entry
=wMenuAddCallback(menu
, title
, NULL
, NULL
);
733 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
735 wWorkspaceMenuUpdate(scr
, wsmenu
);
742 addMenuEntry(WMenu
*menu
, char *title
, char *shortcut
, char *command
,
743 char *params
, char *file_name
)
746 WMenuEntry
*entry
= NULL
;
747 Bool shortcutOk
= False
;
751 scr
= menu
->frame
->screen_ptr
;
752 if (strcmp(command
, "OPEN_MENU")==0) {
754 wwarning(_("%s:missing parameter for menu command \"%s\""),
760 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
762 path
= wstrdup(params
);
764 dummy
= wMenuCreate(scr
, title
, False
);
765 dummy
->on_destroy
= removeShortcutsForMenu
;
766 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
767 entry
->free_cdata
= free
;
768 wMenuEntrySetCascade(menu
, entry
, dummy
);
770 } else if (strcmp(command
, "EXEC")==0) {
772 wwarning(_("%s:missing parameter for menu command \"%s\""),
775 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrdup(params
));
776 entry
->free_cdata
= free
;
779 } else if (strcmp(command
, "EXIT")==0) {
781 if (params
&& strcmp(params
, "QUICK")==0)
782 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void*)M_QUICK
);
784 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
787 } else if (strcmp(command
, "SHUTDOWN")==0) {
789 if (params
&& strcmp(params
, "QUICK")==0)
790 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
,
793 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
796 } else if (strcmp(command
, "REFRESH")==0) {
797 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
800 } else if (strcmp(command
, "WORKSPACE_MENU")==0) {
801 entry
= addWorkspaceMenu(scr
, menu
, title
);
804 } else if (strcmp(command
, "ARRANGE_ICONS")==0) {
805 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
808 } else if (strcmp(command
, "HIDE_OTHERS")==0) {
809 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
812 } else if (strcmp(command
, "SHOW_ALL")==0) {
813 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
816 } else if (strcmp(command
, "RESTART")==0) {
817 entry
= wMenuAddCallback(menu
, title
, restartCommand
,
818 params
? wstrdup(params
) : NULL
);
819 entry
->free_cdata
= free
;
821 } else if (strcmp(command
, "SAVE_SESSION")==0) {
822 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
825 } else if (strcmp(command
, "CLEAR_SESSION")==0) {
826 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
828 } else if (strcmp(command
, "INFO_PANEL")==0) {
829 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
831 } else if (strcmp(command
, "LEGAL_PANEL")==0) {
832 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
835 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
,
841 if (shortcut
&& entry
) {
843 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
,
846 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
848 entry
->rtext
= GetShortcutString(shortcut
);
850 entry->rtext = wstrdup(shortcut);
861 /******************* Menu Configuration From File *******************/
864 separateline(char *line
, char *title
, char *command
, char *parameter
,
876 while (isspace(*line
) && (*line
!=0)) line
++;
880 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
885 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
887 strncpy(title
, line
, i
);
891 /* get the command or shortcut keyword */
892 while (isspace(*line
) && (*line
!=0)) line
++;
896 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
897 strncpy(command
, line
, i
);
901 if (strcmp(command
, "SHORTCUT")==0) {
902 /* get the shortcut key */
903 while (isspace(*line
) && (*line
!=0)) line
++;
907 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
912 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
914 strncpy(shortcut
, line
, i
);
920 /* get the command */
921 while (isspace(*line
) && (*line
!=0)) line
++;
925 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
926 strncpy(command
, line
, i
);
931 /* get the parameters */
932 while (isspace(*line
) && (*line
!=0)) line
++;
939 while (line
[l
]!=0 && line
[l
]!='"') {
940 parameter
[l
] = line
[l
];
948 while (isspace(line
[l
]) && (l
>0)) l
--;
949 strncpy(parameter
, line
, l
);
955 parseCascade(WScreen
*scr
, WMenu
*menu
, FILE *file
, char *file_name
)
957 char linebuf
[MAXLINE
];
958 char elinebuf
[MAXLINE
];
960 char command
[MAXLINE
];
961 char shortcut
[MAXLINE
];
962 char params
[MAXLINE
];
965 while (!IsEof(file
)) {
969 fgets(linebuf
, MAXLINE
, file
);
970 line
= cropline(linebuf
);
971 lsize
= strlen(line
);
973 if (line
[lsize
-1]=='\\') {
976 fgets(elinebuf
, MAXLINE
, file
);
977 line2
=cropline(elinebuf
);
978 lsize2
=strlen(line2
);
979 if (lsize2
+lsize
>MAXLINE
) {
980 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
991 } while (!ok
&& !IsEof(file
));
995 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
999 separateline(line
, title
, command
, params
, shortcut
);
1002 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1007 if (strcasecmp(command
, "MENU")==0) {
1012 cascade
= wMenuCreate(scr
, title
, False
);
1013 cascade
->on_destroy
= removeShortcutsForMenu
;
1014 if (parseCascade(scr
, cascade
, file
, file_name
)==NULL
) {
1015 wMenuDestroy(cascade
, True
);
1017 wMenuEntrySetCascade(menu
,
1018 wMenuAddCallback(menu
, title
, NULL
, NULL
),
1021 } else if (strcasecmp(command
, "END")==0) {
1027 addMenuEntry(menu
, title
, shortcut
[0] ? shortcut
: NULL
, command
,
1028 params
[0] ? params
: NULL
, file_name
);
1032 wwarning(_("%s:syntax error in menu file:END declaration missing"),
1042 readMenuFile(WScreen
*scr
, char *file_name
)
1046 char linebuf
[MAXLINE
];
1047 char title
[MAXLINE
];
1048 char shortcut
[MAXLINE
];
1049 char command
[MAXLINE
];
1050 char params
[MAXLINE
];
1058 if (!wPreferences
.flags
.nocpp
) {
1059 args
= MakeCPPArgs(file_name
);
1061 wwarning(_("could not make arguments for menu file preprocessor"));
1063 sprintf(command
, "%s %s %s", CPP_PATH
, args
, file_name
);
1065 file
= popen(command
, "r");
1067 wsyserror(_("%s:could not open/preprocess menu file"),
1077 file
= fopen(file_name
, "r");
1079 wsyserror(_("%s:could not open menu file"), file_name
);
1084 while (!IsEof(file
)) {
1085 if (!fgets(linebuf
, MAXLINE
, file
))
1087 line
= cropline(linebuf
);
1088 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1091 separateline(line
, title
, command
, params
, shortcut
);
1094 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1098 if (strcasecmp(command
, "MENU")==0) {
1099 menu
= wMenuCreate(scr
, title
, True
);
1100 menu
->on_destroy
= removeShortcutsForMenu
;
1101 if (!parseCascade(scr
, menu
, file
, file_name
)) {
1102 wMenuDestroy(menu
, True
);
1106 wwarning(_("%s:invalid menu file. MENU command is missing"),
1114 if (pclose(file
)==-1) {
1115 wsyserror(_("error reading preprocessed menu data"));
1127 /************ Menu Configuration From Pipe *************/
1130 readMenuPipe(WScreen
*scr
, char **file_name
)
1134 char linebuf
[MAXLINE
];
1135 char title
[MAXLINE
];
1136 char command
[MAXLINE
];
1137 char params
[MAXLINE
];
1138 char shortcut
[MAXLINE
];
1141 char flat_file
[MAXLINE
];
1148 flat_file
[0] = '\0';
1150 for(i
= 0 ; file_name
[i
] != NULL
; i
++) {
1151 strcat(flat_file
, file_name
[i
]);
1152 strcat(flat_file
, " ");
1154 filename
= flat_file
+1;
1157 if (!wPreferences
.flags
.nocpp
) {
1158 args
= MakeCPPArgs(filename
);
1160 wwarning(_("could not make arguments for menu file preprocessor"));
1162 sprintf(command
, "%s | %s %s", filename
, CPP_PATH
, args
);
1165 file
= popen(command
, "r");
1167 wsyserror(_("%s:could not open/preprocess menu file"), filename
);
1177 file
= popen(filename
, "r");
1180 wsyserror(_("%s:could not open menu file"), filename
);
1185 while (!IsEof(file
)) {
1186 if (!fgets(linebuf
, MAXLINE
, file
))
1188 line
= cropline(linebuf
);
1189 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1192 separateline(line
, title
, command
, params
, shortcut
);
1195 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1199 if (strcasecmp(command
, "MENU")==0) {
1200 menu
= wMenuCreate(scr
, title
, True
);
1201 menu
->on_destroy
= removeShortcutsForMenu
;
1202 if (!parseCascade(scr
, menu
, file
, filename
)) {
1203 wMenuDestroy(menu
, True
);
1207 wwarning(_("%s:no title given for the root menu"), filename
);
1226 myCompare(dir_data
*d1
, dir_data
*d2
)
1228 return strcmp(d1
->name
, d2
->name
);
1232 /************ Menu Configuration From Directory *************/
1234 readMenuDirectory(WScreen
*scr
, char *title
, char **path
, char *command
)
1237 struct dirent
*dentry
;
1238 struct stat stat_buf
;
1241 LinkedList
*dirs
= NULL
, *files
= NULL
;
1242 int length
, i
, have_space
=0;
1246 while (path
[i
]!=NULL
) {
1247 dir
= opendir(path
[i
]);
1253 while ((dentry
= readdir(dir
))) {
1255 if (strcmp(dentry
->d_name
, ".")==0 ||
1256 strcmp(dentry
->d_name
, "..")==0)
1259 buffer
= wmalloc(strlen(path
[i
])+strlen(dentry
->d_name
)+4);
1261 wsyserror(_("out of memory while constructing directory menu %s"),
1266 strcpy(buffer
, path
[i
]);
1267 strcat(buffer
, "/");
1268 strcat(buffer
, dentry
->d_name
);
1270 if (stat(buffer
, &stat_buf
)!=0) {
1271 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1272 path
[i
], dentry
->d_name
);
1274 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1275 data
->name
= wstrdup(dentry
->d_name
);
1277 if (S_ISDIR(stat_buf
.st_mode
)) {
1278 /* access always returns success for user root */
1279 if (access(buffer
, X_OK
)==0) {
1280 /* Directory is accesible. Add to directory list */
1281 list_insert_sorted(data
, &dirs
, (int(*)())myCompare
);
1284 } else if (S_ISREG(stat_buf
.st_mode
)) {
1285 /* Hack because access always returns X_OK success for user root */
1286 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1287 if ((command
!=NULL
&& access(buffer
, R_OK
)==0) ||
1288 (command
==NULL
&& access(buffer
, X_OK
)==0 &&
1289 (stat_buf
.st_mode
& S_IXANY
))) {
1290 list_insert_sorted(data
, &files
, (int(*)())myCompare
);
1308 if (!dirs
&& !files
)
1311 menu
= wMenuCreate(scr
, title
, False
);
1312 menu
->on_destroy
= removeShortcutsForMenu
;
1314 while (dirs
!= NULL
) {
1315 /* New directory. Use same OPEN_MENU command that was used
1316 * for the current directory. */
1317 dir_data
*d
= (dir_data
*)dirs
->head
;
1319 length
= strlen(path
[d
->index
])+strlen(d
->name
)+6;
1321 length
+= strlen(command
) + 6;
1322 buffer
= wmalloc(length
);
1324 wsyserror(_("out of memory while constructing directory menu %s"),
1329 have_space
= strchr(path
[d
->index
], ' ')!=NULL
||
1330 strchr(d
->name
, ' ')!=NULL
;
1334 strcat(buffer
, path
[d
->index
]);
1336 strcpy(buffer
, path
[d
->index
]);
1338 strcat(buffer
, "/");
1339 strcat(buffer
, d
->name
);
1341 strcat(buffer
, "\"");
1343 strcat(buffer
, " WITH ");
1344 strcat(buffer
, command
);
1347 addMenuEntry(menu
, d
->name
, NULL
, "OPEN_MENU", buffer
, path
[d
->index
]);
1355 list_remove_head(&dirs
);
1358 while (files
!= NULL
) {
1359 /* executable: add as entry */
1360 dir_data
*f
= (dir_data
*) files
->head
;;
1362 length
= strlen(path
[f
->index
])+strlen(f
->name
)+6;
1364 length
+= strlen(command
);
1366 buffer
= wmalloc(length
);
1368 wsyserror(_("out of memory while constructing directory menu %s"),
1373 have_space
= strchr(path
[f
->index
], ' ')!=NULL
||
1374 strchr(f
->name
, ' ')!=NULL
;
1375 if (command
!=NULL
) {
1376 strcpy(buffer
, command
);
1377 strcat(buffer
, " ");
1379 strcat(buffer
, "\"");
1380 strcat(buffer
, path
[f
->index
]);
1385 strcat(buffer
, path
[f
->index
]);
1387 strcpy(buffer
, path
[f
->index
]);
1390 strcat(buffer
, "/");
1391 strcat(buffer
, f
->name
);
1393 strcat(buffer
, "\"");
1395 addMenuEntry(menu
, f
->name
, NULL
, "EXEC", buffer
, path
[f
->index
]);
1403 list_remove_head(&files
);
1410 /************ Menu Configuration From WMRootMenu *************/
1413 makeDefaultMenu(WScreen
*scr
)
1417 menu
= wMenuCreate(scr
, _("Commands"), True
);
1418 wMenuAddCallback(menu
, "XTerm", execCommand
, "xterm");
1419 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1428 *----------------------------------------------------------------------
1430 * Reads root menu configuration from defaults database.
1432 *----------------------------------------------------------------------
1435 configureMenu(WScreen
*scr
, proplist_t definition
)
1440 proplist_t title
, command
, params
;
1444 if (PLIsString(definition
)) {
1445 struct stat stat_buf
;
1448 /* menu definition is a string. Probably a path, so parse the file */
1450 tmp
= wexpandpath(PLGetString(definition
));
1453 path
= wmalloc(strlen(tmp
)+32);
1457 strcat(path
, Locale
);
1459 /* look for menu.xy */
1460 if (stat(path
, &stat_buf
)<0) {
1464 path
[strlen(path
)-(i
-2)]=0;
1465 /* look for menu.xy_zw */
1466 if (stat(path
, &stat_buf
)<0) {
1468 /* If did not find any localized menus, try
1469 * only menu. This can also mean that
1470 * the path in WMRootMenu was already the
1471 * path for the localized menu (eg: menu = "menu.ab")
1483 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1486 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"),
1492 if (stat(path
, &stat_buf
)<0) {
1493 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1499 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1500 /* if the pointer in WMRootMenu has changed */
1501 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1502 menu
= readMenuFile(scr
, path
);
1504 menu
->timestamp
= MAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1514 count
= PLGetNumberOfElements(definition
);
1518 elem
= PLGetArrayElement(definition
, 0);
1519 if (!PLIsString(elem
)) {
1520 tmp
= PLGetDescription(elem
);
1521 wwarning(_("%s:format error in root menu configuration \"%s\""),
1526 mtitle
= PLGetString(elem
);
1528 menu
= wMenuCreate(scr
, mtitle
, False
);
1529 menu
->on_destroy
= removeShortcutsForMenu
;
1531 for (i
=1; i
<count
; i
++) {
1532 elem
= PLGetArrayElement(definition
, i
);
1534 if (!PLIsArray(elem
) || PLGetNumberOfElements(elem
) < 2)
1537 if (PLIsArray(PLGetArrayElement(elem
,1))) {
1542 submenu
= configureMenu(scr
, elem
);
1544 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
,
1546 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1553 title
= PLGetArrayElement(elem
, idx
++);
1554 shortcut
= PLGetArrayElement(elem
, idx
++);
1555 if (strcmp(PLGetString(shortcut
), "SHORTCUT")==0) {
1556 shortcut
= PLGetArrayElement(elem
, idx
++);
1557 command
= PLGetArrayElement(elem
, idx
++);
1562 params
= PLGetArrayElement(elem
, idx
++);
1564 if (!title
|| !command
)
1567 addMenuEntry(menu
, PLGetString(title
),
1568 shortcut
? PLGetString(shortcut
) : NULL
,
1569 PLGetString(command
),
1570 params
? PLGetString(params
) : NULL
, "WMRootMenu");
1575 tmp
= PLGetDescription(elem
);
1576 wwarning(_("%s:format error in root menu configuration \"%s\""),
1592 *----------------------------------------------------------------------
1594 * Opens the root menu, parsing the menu configuration from the
1595 * defaults database.
1596 * If the menu is already mapped and is not sticked to the
1597 * root window, it will be unmapped.
1600 * The menu may be remade.
1603 * Construction of OPEN_MENU entries are delayed to the moment the
1605 *----------------------------------------------------------------------
1608 OpenRootMenu(WScreen
*scr
, int x
, int y
, int keyboard
)
1611 proplist_t definition
;
1613 static proplist_t domain=NULL;
1616 domain = PLMakeString("WMRootMenu");
1620 scr
->flags
.root_menu_changed_shortcuts
= 0;
1621 scr
->flags
.added_workspace_menu
= 0;
1623 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1624 menu
= scr
->root_menu
;
1625 if (!menu
->flags
.buttoned
) {
1628 wRaiseFrame(menu
->frame
->core
);
1631 wMenuMapAt(menu
, 0, 0, True
);
1633 wMenuMapCopyAt(menu
, x
-menu
->frame
->core
->width
/2,
1634 y
-menu
->frame
->top_width
/2);
1640 definition
= WDRootMenu
->dictionary
;
1643 definition = PLGetDomain(domain);
1646 if (PLIsArray(definition
)) {
1648 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1649 menu
= configureMenu(scr
, definition
);
1651 menu
->timestamp
= WDRootMenu
->timestamp
;
1655 menu
= configureMenu(scr
, definition
);
1660 /* menu hasn't changed or could not be read */
1661 if (!scr
->root_menu
) {
1662 menu
= makeDefaultMenu(scr
);
1663 scr
->root_menu
= menu
;
1665 menu
= scr
->root_menu
;
1669 wMenuDestroy(scr
->root_menu
, True
);
1670 scr
->root_menu
= menu
;
1673 wMenuMapAt(menu
, x
-menu
->frame
->core
->width
/2, y
-menu
->frame
->top_width
/2,
1677 if (scr
->flags
.root_menu_changed_shortcuts
)
1678 rebindKeygrabs(scr
);