1 /* rootmenu.c- user defined menu
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998 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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
33 #include <sys/types.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xatom.h>
43 #include "WindowMaker.h"
50 #include "workspace.h"
54 #include "xmodifier.h"
61 extern WDDomain
*WDRootMenu
;
63 extern Cursor wCursor
[WCUR_LAST
];
65 extern Time LastTimestamp
;
67 extern WPreferences wPreferences
;
69 extern int wScreenCount
;
71 static WMenu
*readMenuPipe(WScreen
*scr
, char **file_name
);
72 static WMenu
*readMenuFile(WScreen
*scr
, char *file_name
);
73 static WMenu
*readMenuDirectory(WScreen
*scr
, char *title
, char **file_name
,
77 typedef struct Shortcut
{
78 struct Shortcut
*next
;
88 static Shortcut
*shortcutList
= NULL
;
95 * "Title" EXEC command_to_exec -params
97 * "Title" EXEC command_to_exec -params
99 * "Workspaces" WORKSPACE_MENU
100 * "Title" built_in_command
102 * "Quick Quit" EXIT QUICK
105 * Commands may be preceded by SHORTCUT key
109 * INFO_PANEL - shows the Info Panel
110 * LEGAL_PANEL - shows the Legal info panel
111 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
112 * REFRESH - forces the desktop to be repainted
113 * EXIT [QUICK] - exit the window manager [without confirmation]
114 * EXEC <program> - execute an external program
115 * SHEXEC <command> - execute a shell command
116 * WORKSPACE_MENU - places the workspace submenu
118 * RESTART [<window manager>] - restarts the window manager
119 * SHOW_ALL - unhide all windows on workspace
120 * HIDE_OTHERS - hides all windows excep the focused one
121 * OPEN_MENU file - read menu data from file which must be a valid menu file.
122 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
123 * - read menu data from directory(ies) and
124 * eventually precede each with a command.
125 * OPEN_MENU | command
126 * - opens command and uses its stdout to construct and insert
127 * the resulting menu in current position. The output of
128 * command must be a valid menu description.
129 * The space between '|' and command is optional.
130 * SAVE_SESSION - saves the current state of the desktop, which include
131 * all running applications, all their hints (geometry,
132 * position on screen, workspace they live on, the dock
133 * or clip from where they were launched, and
134 * if minimized, shaded or hidden. Also saves the current
135 * workspace the user is on. All will be restored on every
136 * start of windowmaker until another SAVE_SESSION or
137 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
138 * WindowMaker domain file, then saving is automatically
139 * done on every windowmaker exit, overwriting any
140 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
142 * CLEAR_SESSION - clears any previous saved session. This will not have
143 * any effect if SaveSessionOnExit is True.
152 execCommand(WMenu
*menu
, WMenuEntry
*entry
)
156 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char*)entry
->clientdata
);
158 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
159 GrabModeAsync
, GrabModeAsync
, None
, wCursor
[WCUR_WAIT
],
164 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
167 XUngrabPointer(dpy
, CurrentTime
);
173 exitCommand(WMenu
*menu
, WMenuEntry
*entry
)
175 static int inside
= 0;
177 /* prevent reentrant calls */
182 if ((long)entry
->clientdata
==M_QUICK
183 || wMessageDialog(menu
->frame
->screen_ptr
, _("Exit"),
184 _("Exit window manager?"),
185 _("Exit"), _("Cancel"), NULL
)==WAPRDefault
) {
187 printf("Exiting WindowMaker.\n");
189 Shutdown(WSExitMode
);
196 shutdownCommand(WMenu
*menu
, WMenuEntry
*entry
)
198 static int inside
= 0;
201 /* prevent reentrant calls */
212 if ((long)entry
->clientdata
==M_QUICK
)
216 if (wSessionIsManaged()) {
219 r
= wMessageDialog(menu
->frame
->screen_ptr
,
220 _("Close X session"),
221 _("Close Window System session?\n"
222 "Kill might close applications with unsaved data."),
223 _("Close"), _("Kill"), _("Cancel"));
226 else if (r
==WAPRAlternate
)
233 r
= wMessageDialog(menu
->frame
->screen_ptr
,
235 _("Kill Window System session?\n"
236 "(all applications will be closed)"),
237 _("Kill"), _("Cancel"), NULL
);
243 if (result
!=R_CANCEL
) {
245 if (result
== R_CLOSE
) {
246 Shutdown(WSLogoutMode
);
248 #endif /* XSMP_ENABLED */
250 Shutdown(WSKillMode
);
261 restartCommand(WMenu
*menu
, WMenuEntry
*entry
)
263 Shutdown(WSRestartPreparationMode
);
264 Restart((char*)entry
->clientdata
, False
);
270 refreshCommand(WMenu
*menu
, WMenuEntry
*entry
)
272 wRefreshDesktop(menu
->frame
->screen_ptr
);
277 arrangeIconsCommand(WMenu
*menu
, WMenuEntry
*entry
)
279 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
283 showAllCommand(WMenu
*menu
, WMenuEntry
*entry
)
285 wShowAllWindows(menu
->frame
->screen_ptr
);
289 hideOthersCommand(WMenu
*menu
, WMenuEntry
*entry
)
291 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
296 saveSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
298 if (!wPreferences
.save_session_on_exit
)
299 wSessionSaveState(menu
->frame
->screen_ptr
);
301 wScreenSaveState(menu
->frame
->screen_ptr
);
306 clearSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
308 wSessionClearState(menu
->frame
->screen_ptr
);
313 infoPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
315 wShowInfoPanel(menu
->frame
->screen_ptr
);
320 legalPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
322 wShowLegalPanel(menu
->frame
->screen_ptr
);
326 /********************************************************************/
330 getLocalizedMenuFile(char *menu
)
339 len
= strlen(menu
)+32;
340 buffer
= wmalloc(len
);
342 /* try menu.locale_name */
343 snprintf(buffer
, len
, "%s.%s", menu
, Locale
);
344 if (access(buffer
, F_OK
)==0) {
347 /* check if it is in the form aa_bb.encoding and check for aa_bb */
348 ptr
= strchr(Locale
, '.');
351 if (access(buffer
, F_OK
)==0) {
355 /* now check for aa */
356 ptr
= strchr(buffer
, '_');
359 if (access(buffer
, F_OK
)==0) {
369 raiseMenus(WMenu
*menu
)
373 if (menu
->flags
.mapped
) {
374 wRaiseFrame(menu
->frame
->core
);
376 for (i
=0; i
<menu
->cascade_no
; i
++) {
377 if (menu
->cascades
[i
])
378 raiseMenus(menu
->cascades
[i
]);
385 wRootMenuPerformShortcut(XEvent
*event
)
391 /* ignore CapsLock */
392 modifiers
= event
->xkey
.state
& ValidModMask
;
394 for (ptr
= shortcutList
; ptr
!=NULL
; ptr
= ptr
->next
) {
398 if (ptr
->keycode
==event
->xkey
.keycode
&& (ptr
->modifier
==modifiers
)) {
399 (*ptr
->entry
->callback
)(ptr
->menu
, ptr
->entry
);
408 wRootMenuBindShortcuts(Window window
)
414 if (ptr
->modifier
!=AnyModifier
) {
415 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
|LockMask
,
416 window
, True
, GrabModeAsync
, GrabModeAsync
);
418 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
,
419 window
, True
, GrabModeAsync
, GrabModeAsync
);
422 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
,
423 GrabModeAsync
, GrabModeAsync
);
430 rebindKeygrabs(WScreen
*scr
)
434 wwin
= scr
->focused_window
;
437 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
439 if (!WFLAGP(wwin
, no_bind_keys
)) {
440 wWindowSetKeyGrabs(wwin
);
448 removeShortcutsForMenu(WMenu
*menu
)
451 Shortcut
*newList
= NULL
;
456 if (ptr
->menu
== menu
) {
464 shortcutList
= newList
;
465 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
470 addShortcut(char *file
, char *shortcutDefinition
, WMenu
*menu
,
478 ptr
= wmalloc(sizeof(Shortcut
));
480 strcpy(buf
, shortcutDefinition
);
485 while ((k
= strchr(b
, '+'))!=NULL
) {
489 mod
= wXModifierFromKey(b
);
491 wwarning(_("%s:invalid key modifier \"%s\""), file
, b
);
495 ptr
->modifier
|= mod
;
501 ksym
= XStringToKeysym(b
);
503 if (ksym
==NoSymbol
) {
504 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
505 file
, shortcutDefinition
, entry
->text
);
510 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
511 if (ptr
->keycode
==0) {
512 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
513 shortcutDefinition
, entry
->text
);
521 ptr
->next
= shortcutList
;
524 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
530 /*******************************/
540 end
= &(line
[strlen(line
)])-1;
541 while (isspace(*line
) && *line
!=0) line
++;
542 while (end
>line
&& isspace(*end
)) {
551 next_token(char *line
, char **next
)
557 while (*line
==' ' || *line
=='\t') line
++;
563 while (*tmp
!=0 && *tmp
!='"') tmp
++;
565 wwarning(_("%s: unmatched '\"' in menu file"), line
);
576 } while (*tmp
!=0 && *tmp
!=' ' && *tmp
!='\t');
590 while (*tmp
==' ' || *tmp
=='\t') tmp
++;
600 separateCommand(char *line
, char ***file
, char **command
)
602 char *token
, *tmp
= line
;
603 WMArray
*array
= WMCreateArray(4);
609 token
= next_token(tmp
, &tmp
);
611 if (strcmp(token
, "WITH")==0) {
612 if (tmp
!=NULL
&& *tmp
!=0)
613 *command
= wstrdup(tmp
);
615 wwarning(_("%s: missing command"), line
);
618 WMAddToArray(array
, token
);
620 } while (token
!=NULL
&& tmp
!=NULL
);
622 count
= WMGetArrayItemCount(array
);
624 *file
= wmalloc(sizeof(char*)*(count
+1));
625 (*file
)[count
] = NULL
;
626 for (i
= 0; i
< count
; i
++) {
627 (*file
)[i
] = WMGetFromArray(array
, i
);
635 constructMenu(WMenu
*menu
, WMenuEntry
*entry
)
638 struct stat stat_buf
;
645 separateCommand((char*)entry
->clientdata
, &path
, &cmd
);
646 if (path
== NULL
|| *path
==NULL
|| **path
==0) {
647 wwarning(_("invalid OPEN_MENU specification: %s"),
648 (char*)entry
->clientdata
);
652 if (path
[0][0]=='|') {
655 if (!menu
->cascades
[entry
->cascade
] ||
656 menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
659 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
661 /* there's no automatic reloading */
663 submenu
->timestamp
= 1;
670 while(path
[i
] != NULL
) {
673 if (strcmp(path
[i
], "-noext")==0) {
678 tmp
= wexpandpath(path
[i
]);
680 lpath
= getLocalizedMenuFile(tmp
);
689 if (stat(path
[i
], &stat_buf
)==0) {
690 if (last
< stat_buf
.st_mtime
)
691 last
= stat_buf
.st_mtime
;
695 wsyserror(_("%s:could not stat menu"), path
[i
]);
703 wsyserror(_("%s:could not stat menu:%s"), "OPEN_MENU",
704 (char*)entry
->clientdata
);
707 stat(path
[first
], &stat_buf
);
708 if (!menu
->cascades
[entry
->cascade
]
709 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
711 if (S_ISDIR(stat_buf
.st_mode
)) {
713 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
,
714 entry
->text
, path
, cmd
);
716 submenu
->timestamp
= last
;
717 } else if (S_ISREG(stat_buf
.st_mode
)) {
721 wwarning(_("too many parameters in OPEN_MENU: %s"),
722 (char*)entry
->clientdata
);
724 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
726 submenu
->timestamp
= stat_buf
.st_mtime
;
736 wMenuEntryRemoveCascade(menu
, entry
);
737 wMenuEntrySetCascade(menu
, entry
, submenu
);
742 while (path
[i
]!=NULL
)
751 cleanupWorkspaceMenu(WMenu
*menu
)
753 if (menu
->frame
->screen_ptr
->workspace_menu
== menu
)
754 menu
->frame
->screen_ptr
->workspace_menu
= NULL
;
759 addWorkspaceMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
764 if (scr
->flags
.added_workspace_menu
) {
765 wwarning(_("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
768 scr
->flags
.added_workspace_menu
= 1;
770 wsmenu
= wWorkspaceMenuMake(scr
, True
);
771 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
773 scr
->workspace_menu
= wsmenu
;
774 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
775 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
777 wWorkspaceMenuUpdate(scr
, wsmenu
);
784 cleanupWindowsMenu(WMenu
*menu
)
786 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
787 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
792 addWindowsMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
798 if (scr
->flags
.added_windows_menu
) {
799 wwarning(_("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
802 scr
->flags
.added_windows_menu
= 1;
804 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
805 wwmenu
->on_destroy
= cleanupWindowsMenu
;
806 scr
->switch_menu
= wwmenu
;
807 wwin
= scr
->focused_window
;
809 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
813 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
814 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
821 addMenuEntry(WMenu
*menu
, char *title
, char *shortcut
, char *command
,
822 char *params
, char *file_name
)
825 WMenuEntry
*entry
= NULL
;
826 Bool shortcutOk
= False
;
830 scr
= menu
->frame
->screen_ptr
;
831 if (strcmp(command
, "OPEN_MENU")==0) {
833 wwarning(_("%s:missing parameter for menu command \"%s\""),
839 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
841 path
= wstrdup(params
);
843 dummy
= wMenuCreate(scr
, title
, False
);
844 dummy
->on_destroy
= removeShortcutsForMenu
;
845 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
846 entry
->free_cdata
= free
;
847 wMenuEntrySetCascade(menu
, entry
, dummy
);
849 } else if (strcmp(command
, "EXEC")==0) {
851 wwarning(_("%s:missing parameter for menu command \"%s\""),
854 entry
= wMenuAddCallback(menu
, title
, execCommand
,
855 wstrconcat("exec ", params
));
856 entry
->free_cdata
= free
;
859 } else if (strcmp(command
, "SHEXEC")==0) {
861 wwarning(_("%s:missing parameter for menu command \"%s\""),
864 entry
= wMenuAddCallback(menu
, title
, execCommand
,
866 entry
->free_cdata
= free
;
869 } else if (strcmp(command
, "EXIT")==0) {
871 if (params
&& strcmp(params
, "QUICK")==0)
872 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void*)M_QUICK
);
874 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
877 } else if (strcmp(command
, "SHUTDOWN")==0) {
879 if (params
&& strcmp(params
, "QUICK")==0)
880 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
,
883 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
886 } else if (strcmp(command
, "REFRESH")==0) {
887 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
890 } else if (strcmp(command
, "WORKSPACE_MENU")==0) {
891 entry
= addWorkspaceMenu(scr
, menu
, title
);
894 } else if (strcmp(command
, "WINDOWS_MENU")==0) {
895 entry
= addWindowsMenu(scr
, menu
, title
);
898 } else if (strcmp(command
, "ARRANGE_ICONS")==0) {
899 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
902 } else if (strcmp(command
, "HIDE_OTHERS")==0) {
903 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
906 } else if (strcmp(command
, "SHOW_ALL")==0) {
907 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
910 } else if (strcmp(command
, "RESTART")==0) {
911 entry
= wMenuAddCallback(menu
, title
, restartCommand
,
912 params
? wstrdup(params
) : NULL
);
913 entry
->free_cdata
= free
;
915 } else if (strcmp(command
, "SAVE_SESSION")==0) {
916 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
919 } else if (strcmp(command
, "CLEAR_SESSION")==0) {
920 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
922 } else if (strcmp(command
, "INFO_PANEL")==0) {
923 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
925 } else if (strcmp(command
, "LEGAL_PANEL")==0) {
926 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
929 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
,
935 if (shortcut
&& entry
) {
937 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
,
940 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
942 entry
->rtext
= GetShortcutString(shortcut
);
944 entry->rtext = wstrdup(shortcut);
955 /******************* Menu Configuration From File *******************/
958 separateline(char *line
, char *title
, char *command
, char *parameter
,
970 while (isspace(*line
) && (*line
!=0)) line
++;
974 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
979 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
981 strncpy(title
, line
, i
);
985 /* get the command or shortcut keyword */
986 while (isspace(*line
) && (*line
!=0)) line
++;
990 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
991 strncpy(command
, line
, i
);
995 if (strcmp(command
, "SHORTCUT")==0) {
996 /* get the shortcut key */
997 while (isspace(*line
) && (*line
!=0)) line
++;
1001 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
1006 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1008 strncpy(shortcut
, line
, i
);
1014 /* get the command */
1015 while (isspace(*line
) && (*line
!=0)) line
++;
1019 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1020 strncpy(command
, line
, i
);
1025 /* get the parameters */
1026 while (isspace(*line
) && (*line
!=0)) line
++;
1033 while (line
[l
]!=0 && line
[l
]!='"') {
1034 parameter
[l
] = line
[l
];
1042 while (isspace(line
[l
]) && (l
>0)) l
--;
1043 strncpy(parameter
, line
, l
);
1049 parseCascade(WScreen
*scr
, WMenu
*menu
, FILE *file
, char *file_name
)
1051 char linebuf
[MAXLINE
];
1052 char elinebuf
[MAXLINE
];
1053 char title
[MAXLINE
];
1054 char command
[MAXLINE
];
1055 char shortcut
[MAXLINE
];
1056 char params
[MAXLINE
];
1059 while (!IsEof(file
)) {
1063 fgets(linebuf
, MAXLINE
, file
);
1064 line
= cropline(linebuf
);
1065 lsize
= strlen(line
);
1067 if (line
[lsize
-1]=='\\') {
1070 fgets(elinebuf
, MAXLINE
, file
);
1071 line2
=cropline(elinebuf
);
1072 lsize2
=strlen(line2
);
1073 if (lsize2
+lsize
>MAXLINE
) {
1074 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1080 strcat(line
, line2
);
1085 } while (!ok
&& !IsEof(file
));
1089 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1093 separateline(line
, title
, command
, params
, shortcut
);
1096 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1101 if (strcasecmp(command
, "MENU")==0) {
1106 cascade
= wMenuCreate(scr
, title
, False
);
1107 cascade
->on_destroy
= removeShortcutsForMenu
;
1108 if (parseCascade(scr
, cascade
, file
, file_name
)==NULL
) {
1109 wMenuDestroy(cascade
, True
);
1111 wMenuEntrySetCascade(menu
,
1112 wMenuAddCallback(menu
, title
, NULL
, NULL
),
1115 } else if (strcasecmp(command
, "END")==0) {
1121 addMenuEntry(menu
, title
, shortcut
[0] ? shortcut
: NULL
, command
,
1122 params
[0] ? params
: NULL
, file_name
);
1126 wwarning(_("%s:syntax error in menu file:END declaration missing"),
1136 readMenuFile(WScreen
*scr
, char *file_name
)
1140 char linebuf
[MAXLINE
];
1141 char title
[MAXLINE
];
1142 char shortcut
[MAXLINE
];
1143 char command
[MAXLINE
];
1144 char params
[MAXLINE
];
1152 if (!wPreferences
.flags
.nocpp
) {
1153 args
= MakeCPPArgs(file_name
);
1155 wwarning(_("could not make arguments for menu file preprocessor"));
1157 snprintf(command
, sizeof(command
), "%s %s %s",
1158 CPP_PATH
, args
, file_name
);
1160 file
= popen(command
, "r");
1162 wsyserror(_("%s:could not open/preprocess menu file"),
1172 file
= fopen(file_name
, "r");
1174 wsyserror(_("%s:could not open menu file"), file_name
);
1179 while (!IsEof(file
)) {
1180 if (!fgets(linebuf
, MAXLINE
, file
))
1182 line
= cropline(linebuf
);
1183 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1186 separateline(line
, title
, command
, params
, shortcut
);
1189 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1193 if (strcasecmp(command
, "MENU")==0) {
1194 menu
= wMenuCreate(scr
, title
, True
);
1195 menu
->on_destroy
= removeShortcutsForMenu
;
1196 if (!parseCascade(scr
, menu
, file
, file_name
)) {
1197 wMenuDestroy(menu
, True
);
1201 wwarning(_("%s:invalid menu file. MENU command is missing"),
1209 if (pclose(file
)==-1) {
1210 wsyserror(_("error reading preprocessed menu data"));
1222 /************ Menu Configuration From Pipe *************/
1225 readMenuPipe(WScreen
*scr
, char **file_name
)
1229 char linebuf
[MAXLINE
];
1230 char title
[MAXLINE
];
1231 char command
[MAXLINE
];
1232 char params
[MAXLINE
];
1233 char shortcut
[MAXLINE
];
1236 char flat_file
[MAXLINE
];
1243 flat_file
[0] = '\0';
1245 for(i
= 0 ; file_name
[i
] != NULL
; i
++) {
1246 strcat(flat_file
, file_name
[i
]);
1247 strcat(flat_file
, " ");
1249 filename
= flat_file
+1;
1252 if (!wPreferences
.flags
.nocpp
) {
1253 args
= MakeCPPArgs(filename
);
1255 wwarning(_("could not make arguments for menu file preprocessor"));
1257 snprintf(command
, sizeof(command
), "%s | %s %s",
1258 filename
, CPP_PATH
, args
);
1261 file
= popen(command
, "r");
1263 wsyserror(_("%s:could not open/preprocess menu file"), filename
);
1273 file
= popen(filename
, "r");
1276 wsyserror(_("%s:could not open menu file"), filename
);
1281 while (!IsEof(file
)) {
1282 if (!fgets(linebuf
, MAXLINE
, file
))
1284 line
= cropline(linebuf
);
1285 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1288 separateline(line
, title
, command
, params
, shortcut
);
1291 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1295 if (strcasecmp(command
, "MENU")==0) {
1296 menu
= wMenuCreate(scr
, title
, True
);
1297 menu
->on_destroy
= removeShortcutsForMenu
;
1298 if (!parseCascade(scr
, menu
, file
, filename
)) {
1299 wMenuDestroy(menu
, True
);
1303 wwarning(_("%s:no title given for the root menu"), filename
);
1322 myCompare(const void *d1
, const void *d2
)
1324 dir_data
*p1
= *(dir_data
**)d1
;
1325 dir_data
*p2
= *(dir_data
**)d2
;
1327 return strcmp(p1
->name
, p2
->name
);
1331 /************ Menu Configuration From Directory *************/
1334 isFilePackage(char *file
)
1338 /* check if the extension indicates this file is a
1339 * file package. For now, only recognize .themed */
1343 if (l
> 7 && strcmp(&(file
[l
-7]), ".themed")==0) {
1352 readMenuDirectory(WScreen
*scr
, char *title
, char **path
, char *command
)
1355 struct dirent
*dentry
;
1356 struct stat stat_buf
;
1359 WMArray
*dirs
= NULL
, *files
= NULL
;
1360 WMArrayIterator iter
;
1361 int length
, i
, have_space
=0;
1363 int stripExtension
= 0;
1366 dirs
= WMCreateArray(16);
1367 files
= WMCreateArray(16);
1370 while (path
[i
]!=NULL
) {
1371 if (strcmp(path
[i
], "-noext")==0) {
1377 dir
= opendir(path
[i
]);
1383 while ((dentry
= readdir(dir
))) {
1385 if (strcmp(dentry
->d_name
, ".")==0 ||
1386 strcmp(dentry
->d_name
, "..")==0)
1389 if (dentry
->d_name
[0] == '.')
1392 buffer
= malloc(strlen(path
[i
])+strlen(dentry
->d_name
)+4);
1394 wsyserror(_("out of memory while constructing directory menu %s"),
1399 strcpy(buffer
, path
[i
]);
1400 strcat(buffer
, "/");
1401 strcat(buffer
, dentry
->d_name
);
1403 if (stat(buffer
, &stat_buf
)!=0) {
1404 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1405 path
[i
], dentry
->d_name
);
1407 Bool isFilePack
= False
;
1410 if (S_ISDIR(stat_buf
.st_mode
)
1411 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1413 /* access always returns success for user root */
1414 if (access(buffer
, X_OK
)==0) {
1415 /* Directory is accesible. Add to directory list */
1417 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1418 data
->name
= wstrdup(dentry
->d_name
);
1421 WMAddToArray(dirs
, data
);
1423 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1424 /* Hack because access always returns X_OK success for user root */
1425 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1426 if ((command
!=NULL
&& access(buffer
, R_OK
)==0) ||
1427 (command
==NULL
&& access(buffer
, X_OK
)==0 &&
1428 (stat_buf
.st_mode
& S_IXANY
))) {
1430 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1431 data
->name
= wstrdup(dentry
->d_name
);
1434 WMAddToArray(files
, data
);
1445 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1451 WMSortArray(dirs
, myCompare
);
1452 WMSortArray(files
, myCompare
);
1454 menu
= wMenuCreate(scr
, title
, False
);
1455 menu
->on_destroy
= removeShortcutsForMenu
;
1457 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1458 /* New directory. Use same OPEN_MENU command that was used
1459 * for the current directory. */
1460 length
= strlen(path
[data
->index
])+strlen(data
->name
)+6;
1464 length
+= strlen(command
) + 6;
1465 buffer
= malloc(length
);
1467 wsyserror(_("out of memory while constructing directory menu %s"),
1474 strcat(buffer
, "-noext ");
1476 have_space
= strchr(path
[data
->index
], ' ')!=NULL
||
1477 strchr(data
->name
, ' ')!=NULL
;
1480 strcat(buffer
, "\"");
1481 strcat(buffer
, path
[data
->index
]);
1483 strcat(buffer
, "/");
1484 strcat(buffer
, data
->name
);
1486 strcat(buffer
, "\"");
1488 strcat(buffer
, " WITH ");
1489 strcat(buffer
, command
);
1492 addMenuEntry(menu
, data
->name
, NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1500 WM_ITERATE_ARRAY(files
, data
, iter
) {
1501 /* executable: add as entry */
1502 length
= strlen(path
[data
->index
])+strlen(data
->name
)+6;
1504 length
+= strlen(command
);
1506 buffer
= malloc(length
);
1508 wsyserror(_("out of memory while constructing directory menu %s"),
1513 have_space
= strchr(path
[data
->index
], ' ')!=NULL
||
1514 strchr(data
->name
, ' ')!=NULL
;
1515 if (command
!=NULL
) {
1516 strcpy(buffer
, command
);
1517 strcat(buffer
, " ");
1519 strcat(buffer
, "\"");
1520 strcat(buffer
, path
[data
->index
]);
1525 strcat(buffer
, path
[data
->index
]);
1527 strcpy(buffer
, path
[data
->index
]);
1530 strcat(buffer
, "/");
1531 strcat(buffer
, data
->name
);
1533 strcat(buffer
, "\"");
1535 if (stripExtension
) {
1536 char *ptr
= strrchr(data
->name
, '.');
1537 if (ptr
&& ptr
!=data
->name
)
1540 addMenuEntry(menu
, data
->name
, NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1555 /************ Menu Configuration From WMRootMenu *************/
1558 makeDefaultMenu(WScreen
*scr
)
1562 menu
= wMenuCreate(scr
, _("Commands"), True
);
1563 wMenuAddCallback(menu
, "XTerm", execCommand
, "xterm");
1564 wMenuAddCallback(menu
, "rxvt", execCommand
, "rxvt");
1565 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1566 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1575 *----------------------------------------------------------------------
1577 * Reads root menu configuration from defaults database.
1579 *----------------------------------------------------------------------
1582 configureMenu(WScreen
*scr
, proplist_t definition
)
1587 proplist_t title
, command
, params
;
1591 if (PLIsString(definition
)) {
1592 struct stat stat_buf
;
1594 Bool menu_is_default
= False
;
1596 /* menu definition is a string. Probably a path, so parse the file */
1598 tmp
= wexpandpath(PLGetString(definition
));
1600 path
= getLocalizedMenuFile(tmp
);
1603 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1606 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1607 menu_is_default
= True
;
1611 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"),
1617 if (stat(path
, &stat_buf
)<0) {
1618 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1624 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1625 /* if the pointer in WMRootMenu has changed */
1626 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1628 if (menu_is_default
) {
1629 wwarning(_("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1633 menu
= readMenuFile(scr
, path
);
1635 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1645 count
= PLGetNumberOfElements(definition
);
1649 elem
= PLGetArrayElement(definition
, 0);
1650 if (!PLIsString(elem
)) {
1651 tmp
= PLGetDescription(elem
);
1652 wwarning(_("%s:format error in root menu configuration \"%s\""),
1657 mtitle
= PLGetString(elem
);
1659 menu
= wMenuCreate(scr
, mtitle
, False
);
1660 menu
->on_destroy
= removeShortcutsForMenu
;
1662 #ifdef GLOBAL_SUBMENU_FILE
1667 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1670 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1671 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1676 for (i
=1; i
<count
; i
++) {
1677 elem
= PLGetArrayElement(definition
, i
);
1679 if (PLIsString(elem
)) {
1682 file
= PLGetString(elem
);
1686 if (!PLIsArray(elem
) || PLGetNumberOfElements(elem
) < 2)
1689 if (PLIsArray(PLGetArrayElement(elem
,1))) {
1694 submenu
= configureMenu(scr
, elem
);
1696 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
,
1698 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1705 title
= PLGetArrayElement(elem
, idx
++);
1706 shortcut
= PLGetArrayElement(elem
, idx
++);
1707 if (strcmp(PLGetString(shortcut
), "SHORTCUT")==0) {
1708 shortcut
= PLGetArrayElement(elem
, idx
++);
1709 command
= PLGetArrayElement(elem
, idx
++);
1714 params
= PLGetArrayElement(elem
, idx
++);
1716 if (!title
|| !command
)
1719 addMenuEntry(menu
, PLGetString(title
),
1720 shortcut
? PLGetString(shortcut
) : NULL
,
1721 PLGetString(command
),
1722 params
? PLGetString(params
) : NULL
, "WMRootMenu");
1727 tmp
= PLGetDescription(elem
);
1728 wwarning(_("%s:format error in root menu configuration \"%s\""),
1744 *----------------------------------------------------------------------
1746 * Opens the root menu, parsing the menu configuration from the
1747 * defaults database.
1748 * If the menu is already mapped and is not sticked to the
1749 * root window, it will be unmapped.
1752 * The menu may be remade.
1755 * Construction of OPEN_MENU entries are delayed to the moment the
1757 *----------------------------------------------------------------------
1760 OpenRootMenu(WScreen
*scr
, int x
, int y
, int keyboard
)
1763 proplist_t definition
;
1765 static proplist_t domain=NULL;
1768 domain = PLMakeString("WMRootMenu");
1772 scr
->flags
.root_menu_changed_shortcuts
= 0;
1773 scr
->flags
.added_workspace_menu
= 0;
1774 scr
->flags
.added_windows_menu
= 0;
1776 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1777 menu
= scr
->root_menu
;
1778 if (!menu
->flags
.buttoned
) {
1781 wRaiseFrame(menu
->frame
->core
);
1784 wMenuMapAt(menu
, 0, 0, True
);
1786 wMenuMapCopyAt(menu
, x
-menu
->frame
->core
->width
/2, y
);
1792 definition
= WDRootMenu
->dictionary
;
1795 definition = PLGetDomain(domain);
1798 if (PLIsArray(definition
)) {
1800 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1801 menu
= configureMenu(scr
, definition
);
1803 menu
->timestamp
= WDRootMenu
->timestamp
;
1808 menu
= configureMenu(scr
, definition
);
1813 /* menu hasn't changed or could not be read */
1814 if (!scr
->root_menu
) {
1815 wMessageDialog(scr
, _("Error"),
1816 _("The applications menu could not be loaded. "
1817 "Look at the console output for a detailed "
1818 "description of the errors."),
1819 _("OK"), NULL
, NULL
);
1821 menu
= makeDefaultMenu(scr
);
1822 scr
->root_menu
= menu
;
1824 menu
= scr
->root_menu
;
1827 if (scr
->root_menu
) {
1828 wMenuDestroy(scr
->root_menu
, True
);
1830 scr
->root_menu
= menu
;
1835 if (keyboard
&& x
==0 && y
==0) {
1837 } else if (keyboard
&& x
==scr
->scr_width
/2 && y
==scr
->scr_height
/2) {
1838 newx
= x
- menu
->frame
->core
->width
/2;
1839 newy
= y
- menu
->frame
->core
->height
/2;
1841 newx
= x
- menu
->frame
->core
->width
/2;
1844 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1847 if (scr
->flags
.root_menu_changed_shortcuts
)
1848 rebindKeygrabs(scr
);