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"
56 #include <WINGs/WUtil.h>
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
,
78 typedef struct Shortcut
{
79 struct Shortcut
*next
;
89 static Shortcut
*shortcutList
= NULL
;
96 * "Title" EXEC command_to_exec -params
98 * "Title" EXEC command_to_exec -params
100 * "Workspaces" WORKSPACE_MENU
101 * "Title" built_in_command
103 * "Quick Quit" EXIT QUICK
106 * Commands may be preceded by SHORTCUT key
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
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
143 * CLEAR_SESSION - clears any previous saved session. This will not have
144 * any effect if SaveSessionOnExit is True.
153 execCommand(WMenu
*menu
, WMenuEntry
*entry
)
157 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char*)entry
->clientdata
);
159 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
160 GrabModeAsync
, GrabModeAsync
, None
, wCursor
[WCUR_WAIT
],
165 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
168 XUngrabPointer(dpy
, CurrentTime
);
174 exitCommand(WMenu
*menu
, WMenuEntry
*entry
)
176 static int inside
= 0;
179 /* prevent reentrant calls */
189 if ((long)entry
->clientdata
==M_QUICK
) {
192 int r
, oldSaveSessionFlag
;
194 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
195 r
= wExitDialog(menu
->frame
->screen_ptr
, _("Exit"),
196 _("Exit window manager?"),
197 _("Exit"), _("Cancel"), NULL
);
199 if (r
==WAPRDefault
) {
201 } else if (r
==WAPRAlternate
) {
202 /* Don't modify the "save session on exit" flag if the
203 * user canceled the operation. */
204 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
207 if (result
==R_EXIT
) {
209 printf("Exiting WindowMaker.\n");
211 Shutdown(WSExitMode
);
220 shutdownCommand(WMenu
*menu
, WMenuEntry
*entry
)
222 static int inside
= 0;
225 /* prevent reentrant calls */
236 if ((long)entry
->clientdata
==M_QUICK
)
240 if (wSessionIsManaged()) {
243 r
= wMessageDialog(menu
->frame
->screen_ptr
,
244 _("Close X session"),
245 _("Close Window System session?\n"
246 "Kill might close applications with unsaved data."),
247 _("Close"), _("Kill"), _("Cancel"));
250 else if (r
==WAPRAlternate
)
255 int r
, oldSaveSessionFlag
;
257 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
259 r
= wExitDialog(menu
->frame
->screen_ptr
,
261 _("Kill Window System session?\n"
262 "(all applications will be closed)"),
263 _("Kill"), _("Cancel"), NULL
);
264 if (r
==WAPRDefault
) {
266 } else if (r
==WAPRAlternate
) {
267 /* Don't modify the "save session on exit" flag if the
268 * user canceled the operation. */
269 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
274 if (result
!=R_CANCEL
) {
276 if (result
== R_CLOSE
) {
277 Shutdown(WSLogoutMode
);
279 #endif /* XSMP_ENABLED */
281 Shutdown(WSKillMode
);
292 restartCommand(WMenu
*menu
, WMenuEntry
*entry
)
294 Shutdown(WSRestartPreparationMode
);
295 Restart((char*)entry
->clientdata
, False
);
301 refreshCommand(WMenu
*menu
, WMenuEntry
*entry
)
303 wRefreshDesktop(menu
->frame
->screen_ptr
);
308 arrangeIconsCommand(WMenu
*menu
, WMenuEntry
*entry
)
310 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
314 showAllCommand(WMenu
*menu
, WMenuEntry
*entry
)
316 wShowAllWindows(menu
->frame
->screen_ptr
);
320 hideOthersCommand(WMenu
*menu
, WMenuEntry
*entry
)
322 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
327 saveSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
329 if (!wPreferences
.save_session_on_exit
)
330 wSessionSaveState(menu
->frame
->screen_ptr
);
332 wScreenSaveState(menu
->frame
->screen_ptr
);
337 clearSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
339 wSessionClearState(menu
->frame
->screen_ptr
);
344 infoPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
346 wShowInfoPanel(menu
->frame
->screen_ptr
);
351 legalPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
353 wShowLegalPanel(menu
->frame
->screen_ptr
);
357 /********************************************************************/
361 getLocalizedMenuFile(char *menu
)
370 len
= strlen(menu
)+strlen(Locale
)+8;
371 buffer
= wmalloc(len
);
373 /* try menu.locale_name */
374 snprintf(buffer
, len
, "%s.%s", menu
, Locale
);
375 if (access(buffer
, F_OK
)==0) {
378 /* check if it is in the form aa_bb.encoding and check for aa_bb */
379 ptr
= strchr(Locale
, '.');
382 if (access(buffer
, F_OK
)==0) {
386 /* now check for aa */
387 ptr
= strchr(buffer
, '_');
390 if (access(buffer
, F_OK
)==0) {
400 raiseMenus(WMenu
*menu
)
404 if (menu
->flags
.mapped
) {
405 wRaiseFrame(menu
->frame
->core
);
407 for (i
=0; i
<menu
->cascade_no
; i
++) {
408 if (menu
->cascades
[i
])
409 raiseMenus(menu
->cascades
[i
]);
416 wRootMenuPerformShortcut(XEvent
*event
)
422 /* ignore CapsLock */
423 modifiers
= event
->xkey
.state
& ValidModMask
;
425 for (ptr
= shortcutList
; ptr
!=NULL
; ptr
= ptr
->next
) {
429 if (ptr
->keycode
==event
->xkey
.keycode
&& (ptr
->modifier
==modifiers
)) {
430 (*ptr
->entry
->callback
)(ptr
->menu
, ptr
->entry
);
439 wRootMenuBindShortcuts(Window window
)
445 if (ptr
->modifier
!=AnyModifier
) {
446 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
|LockMask
,
447 window
, True
, GrabModeAsync
, GrabModeAsync
);
449 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
,
450 window
, True
, GrabModeAsync
, GrabModeAsync
);
453 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
,
454 GrabModeAsync
, GrabModeAsync
);
461 rebindKeygrabs(WScreen
*scr
)
465 wwin
= scr
->focused_window
;
468 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
470 if (!WFLAGP(wwin
, no_bind_keys
)) {
471 wWindowSetKeyGrabs(wwin
);
479 removeShortcutsForMenu(WMenu
*menu
)
482 Shortcut
*newList
= NULL
;
487 if (ptr
->menu
== menu
) {
495 shortcutList
= newList
;
496 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
501 addShortcut(char *file
, char *shortcutDefinition
, WMenu
*menu
,
509 ptr
= wmalloc(sizeof(Shortcut
));
511 strcpy(buf
, shortcutDefinition
);
516 while ((k
= strchr(b
, '+'))!=NULL
) {
520 mod
= wXModifierFromKey(b
);
522 wwarning(_("%s:invalid key modifier \"%s\""), file
, b
);
526 ptr
->modifier
|= mod
;
532 ksym
= XStringToKeysym(b
);
534 if (ksym
==NoSymbol
) {
535 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
536 file
, shortcutDefinition
, entry
->text
);
541 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
542 if (ptr
->keycode
==0) {
543 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
544 shortcutDefinition
, entry
->text
);
552 ptr
->next
= shortcutList
;
555 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
561 /*******************************/
571 end
= &(line
[strlen(line
)])-1;
572 while (isspace(*line
) && *line
!=0) line
++;
573 while (end
>line
&& isspace(*end
)) {
582 next_token(char *line
, char **next
)
588 while (*line
==' ' || *line
=='\t') line
++;
594 while (*tmp
!=0 && *tmp
!='"') tmp
++;
596 wwarning(_("%s: unmatched '\"' in menu file"), line
);
607 } while (*tmp
!=0 && *tmp
!=' ' && *tmp
!='\t');
621 while (*tmp
==' ' || *tmp
=='\t') tmp
++;
631 separateCommand(char *line
, char ***file
, char **command
)
633 char *token
, *tmp
= line
;
634 WMArray
*array
= WMCreateArray(4);
640 token
= next_token(tmp
, &tmp
);
642 if (strcmp(token
, "WITH")==0) {
643 if (tmp
!=NULL
&& *tmp
!=0)
644 *command
= wstrdup(tmp
);
646 wwarning(_("%s: missing command"), line
);
649 WMAddToArray(array
, token
);
651 } while (token
!=NULL
&& tmp
!=NULL
);
653 count
= WMGetArrayItemCount(array
);
655 *file
= wmalloc(sizeof(char*)*(count
+1));
656 (*file
)[count
] = NULL
;
657 for (i
= 0; i
< count
; i
++) {
658 (*file
)[i
] = WMGetFromArray(array
, i
);
666 constructMenu(WMenu
*menu
, WMenuEntry
*entry
)
669 struct stat stat_buf
;
676 separateCommand((char*)entry
->clientdata
, &path
, &cmd
);
677 if (path
== NULL
|| *path
==NULL
|| **path
==0) {
678 wwarning(_("invalid OPEN_MENU specification: %s"),
679 (char*)entry
->clientdata
);
683 if (path
[0][0]=='|') {
686 if (!menu
->cascades
[entry
->cascade
] ||
687 menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
690 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
692 /* there's no automatic reloading */
694 submenu
->timestamp
= 1;
701 while(path
[i
] != NULL
) {
704 if (strcmp(path
[i
], "-noext")==0) {
709 tmp
= wexpandpath(path
[i
]);
711 lpath
= getLocalizedMenuFile(tmp
);
720 if (stat(path
[i
], &stat_buf
)==0) {
721 if (last
< stat_buf
.st_mtime
)
722 last
= stat_buf
.st_mtime
;
726 wsyserror(_("%s:could not stat menu"), path
[i
]);
734 wsyserror(_("%s:could not stat menu:%s"), "OPEN_MENU",
735 (char*)entry
->clientdata
);
738 stat(path
[first
], &stat_buf
);
739 if (!menu
->cascades
[entry
->cascade
]
740 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
742 if (S_ISDIR(stat_buf
.st_mode
)) {
744 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
,
745 entry
->text
, path
, cmd
);
747 submenu
->timestamp
= last
;
748 } else if (S_ISREG(stat_buf
.st_mode
)) {
752 wwarning(_("too many parameters in OPEN_MENU: %s"),
753 (char*)entry
->clientdata
);
755 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
757 submenu
->timestamp
= stat_buf
.st_mtime
;
767 wMenuEntryRemoveCascade(menu
, entry
);
768 wMenuEntrySetCascade(menu
, entry
, submenu
);
773 while (path
[i
]!=NULL
)
782 cleanupWorkspaceMenu(WMenu
*menu
)
784 if (menu
->frame
->screen_ptr
->workspace_menu
== menu
)
785 menu
->frame
->screen_ptr
->workspace_menu
= NULL
;
790 addWorkspaceMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
795 if (scr
->flags
.added_workspace_menu
) {
796 wwarning(_("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
799 scr
->flags
.added_workspace_menu
= 1;
801 wsmenu
= wWorkspaceMenuMake(scr
, True
);
802 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
804 scr
->workspace_menu
= wsmenu
;
805 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
806 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
808 wWorkspaceMenuUpdate(scr
, wsmenu
);
815 cleanupWindowsMenu(WMenu
*menu
)
817 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
818 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
823 addWindowsMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
829 if (scr
->flags
.added_windows_menu
) {
830 wwarning(_("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
833 scr
->flags
.added_windows_menu
= 1;
835 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
836 wwmenu
->on_destroy
= cleanupWindowsMenu
;
837 scr
->switch_menu
= wwmenu
;
838 wwin
= scr
->focused_window
;
840 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
844 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
845 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
852 addMenuEntry(WMenu
*menu
, char *title
, char *shortcut
, char *command
,
853 char *params
, char *file_name
)
856 WMenuEntry
*entry
= NULL
;
857 Bool shortcutOk
= False
;
861 scr
= menu
->frame
->screen_ptr
;
862 if (strcmp(command
, "OPEN_MENU")==0) {
864 wwarning(_("%s:missing parameter for menu command \"%s\""),
870 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
872 path
= wstrdup(params
);
874 dummy
= wMenuCreate(scr
, title
, False
);
875 dummy
->on_destroy
= removeShortcutsForMenu
;
876 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
877 entry
->free_cdata
= free
;
878 wMenuEntrySetCascade(menu
, entry
, dummy
);
880 } else if (strcmp(command
, "EXEC")==0) {
882 wwarning(_("%s:missing parameter for menu command \"%s\""),
885 entry
= wMenuAddCallback(menu
, title
, execCommand
,
886 wstrconcat("exec ", params
));
887 entry
->free_cdata
= free
;
890 } else if (strcmp(command
, "SHEXEC")==0) {
892 wwarning(_("%s:missing parameter for menu command \"%s\""),
895 entry
= wMenuAddCallback(menu
, title
, execCommand
,
897 entry
->free_cdata
= free
;
900 } else if (strcmp(command
, "EXIT")==0) {
902 if (params
&& strcmp(params
, "QUICK")==0)
903 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void*)M_QUICK
);
905 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
908 } else if (strcmp(command
, "SHUTDOWN")==0) {
910 if (params
&& strcmp(params
, "QUICK")==0)
911 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
,
914 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
917 } else if (strcmp(command
, "REFRESH")==0) {
918 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
921 } else if (strcmp(command
, "WORKSPACE_MENU")==0) {
922 entry
= addWorkspaceMenu(scr
, menu
, title
);
925 } else if (strcmp(command
, "WINDOWS_MENU")==0) {
926 entry
= addWindowsMenu(scr
, menu
, title
);
929 } else if (strcmp(command
, "ARRANGE_ICONS")==0) {
930 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
933 } else if (strcmp(command
, "HIDE_OTHERS")==0) {
934 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
937 } else if (strcmp(command
, "SHOW_ALL")==0) {
938 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
941 } else if (strcmp(command
, "RESTART")==0) {
942 entry
= wMenuAddCallback(menu
, title
, restartCommand
,
943 params
? wstrdup(params
) : NULL
);
944 entry
->free_cdata
= free
;
946 } else if (strcmp(command
, "SAVE_SESSION")==0) {
947 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
950 } else if (strcmp(command
, "CLEAR_SESSION")==0) {
951 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
953 } else if (strcmp(command
, "INFO_PANEL")==0) {
954 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
956 } else if (strcmp(command
, "LEGAL_PANEL")==0) {
957 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
960 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
,
966 if (shortcut
&& entry
) {
968 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
,
971 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
973 entry
->rtext
= GetShortcutString(shortcut
);
975 entry->rtext = wstrdup(shortcut);
986 /******************* Menu Configuration From File *******************/
989 separateline(char *line
, char *title
, char *command
, char *parameter
,
1001 while (isspace(*line
) && (*line
!=0)) line
++;
1005 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
1010 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1012 strncpy(title
, line
, i
);
1016 /* get the command or shortcut keyword */
1017 while (isspace(*line
) && (*line
!=0)) line
++;
1021 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1022 strncpy(command
, line
, i
);
1026 if (strcmp(command
, "SHORTCUT")==0) {
1027 /* get the shortcut key */
1028 while (isspace(*line
) && (*line
!=0)) line
++;
1032 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
1037 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1039 strncpy(shortcut
, line
, i
);
1045 /* get the command */
1046 while (isspace(*line
) && (*line
!=0)) line
++;
1050 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1051 strncpy(command
, line
, i
);
1056 /* get the parameters */
1057 while (isspace(*line
) && (*line
!=0)) line
++;
1064 while (line
[l
]!=0 && line
[l
]!='"') {
1065 parameter
[l
] = line
[l
];
1073 while (isspace(line
[l
]) && (l
>0)) l
--;
1074 strncpy(parameter
, line
, l
);
1080 parseCascade(WScreen
*scr
, WMenu
*menu
, FILE *file
, char *file_name
)
1082 char linebuf
[MAXLINE
];
1083 char elinebuf
[MAXLINE
];
1084 char title
[MAXLINE
];
1085 char command
[MAXLINE
];
1086 char shortcut
[MAXLINE
];
1087 char params
[MAXLINE
];
1090 while (!IsEof(file
)) {
1094 fgets(linebuf
, MAXLINE
, file
);
1095 line
= cropline(linebuf
);
1096 lsize
= strlen(line
);
1098 if (line
[lsize
-1]=='\\') {
1101 fgets(elinebuf
, MAXLINE
, file
);
1102 line2
=cropline(elinebuf
);
1103 lsize2
=strlen(line2
);
1104 if (lsize2
+lsize
>MAXLINE
) {
1105 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1111 strcat(line
, line2
);
1116 } while (!ok
&& !IsEof(file
));
1120 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1124 separateline(line
, title
, command
, params
, shortcut
);
1127 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1132 if (strcasecmp(command
, "MENU")==0) {
1137 cascade
= wMenuCreate(scr
, title
, False
);
1138 cascade
->on_destroy
= removeShortcutsForMenu
;
1139 if (parseCascade(scr
, cascade
, file
, file_name
)==NULL
) {
1140 wMenuDestroy(cascade
, True
);
1142 wMenuEntrySetCascade(menu
,
1143 wMenuAddCallback(menu
, title
, NULL
, NULL
),
1146 } else if (strcasecmp(command
, "END")==0) {
1152 addMenuEntry(menu
, title
, shortcut
[0] ? shortcut
: NULL
, command
,
1153 params
[0] ? params
: NULL
, file_name
);
1157 wwarning(_("%s:syntax error in menu file:END declaration missing"),
1167 readMenuFile(WScreen
*scr
, char *file_name
)
1171 char linebuf
[MAXLINE
];
1172 char title
[MAXLINE
];
1173 char shortcut
[MAXLINE
];
1174 char command
[MAXLINE
];
1175 char params
[MAXLINE
];
1183 if (!wPreferences
.flags
.nocpp
) {
1184 args
= MakeCPPArgs(file_name
);
1186 wwarning(_("could not make arguments for menu file preprocessor"));
1188 snprintf(command
, sizeof(command
), "%s %s %s",
1189 CPP_PATH
, args
, file_name
);
1191 file
= popen(command
, "r");
1193 wsyserror(_("%s:could not open/preprocess menu file"),
1203 file
= fopen(file_name
, "r");
1205 wsyserror(_("%s:could not open menu file"), file_name
);
1210 while (!IsEof(file
)) {
1211 if (!fgets(linebuf
, MAXLINE
, file
))
1213 line
= cropline(linebuf
);
1214 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1217 separateline(line
, title
, command
, params
, shortcut
);
1220 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1224 if (strcasecmp(command
, "MENU")==0) {
1225 menu
= wMenuCreate(scr
, title
, True
);
1226 menu
->on_destroy
= removeShortcutsForMenu
;
1227 if (!parseCascade(scr
, menu
, file
, file_name
)) {
1228 wMenuDestroy(menu
, True
);
1232 wwarning(_("%s:invalid menu file. MENU command is missing"),
1240 if (pclose(file
)==-1) {
1241 wsyserror(_("error reading preprocessed menu data"));
1253 /************ Menu Configuration From Pipe *************/
1256 readMenuPipe(WScreen
*scr
, char **file_name
)
1260 char linebuf
[MAXLINE
];
1261 char title
[MAXLINE
];
1262 char command
[MAXLINE
];
1263 char params
[MAXLINE
];
1264 char shortcut
[MAXLINE
];
1267 char flat_file
[MAXLINE
];
1274 flat_file
[0] = '\0';
1276 for(i
=0; file_name
[i
]!=NULL
; i
++) {
1277 strcat(flat_file
, file_name
[i
]);
1278 strcat(flat_file
, " ");
1280 filename
= flat_file
+1;
1283 if (!wPreferences
.flags
.nocpp
) {
1284 args
= MakeCPPArgs(filename
);
1286 wwarning(_("could not make arguments for menu file preprocessor"));
1288 snprintf(command
, sizeof(command
), "%s | %s %s",
1289 filename
, CPP_PATH
, args
);
1292 file
= popen(command
, "r");
1294 wsyserror(_("%s:could not open/preprocess menu file"), filename
);
1304 file
= popen(filename
, "r");
1307 wsyserror(_("%s:could not open menu file"), filename
);
1312 while (!IsEof(file
)) {
1313 if (!fgets(linebuf
, MAXLINE
, file
))
1315 line
= cropline(linebuf
);
1316 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1319 separateline(line
, title
, command
, params
, shortcut
);
1322 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1326 if (strcasecmp(command
, "MENU")==0) {
1327 menu
= wMenuCreate(scr
, title
, True
);
1328 menu
->on_destroy
= removeShortcutsForMenu
;
1329 if (!parseCascade(scr
, menu
, file
, filename
)) {
1330 wMenuDestroy(menu
, True
);
1334 wwarning(_("%s:no title given for the root menu"), filename
);
1353 myCompare(const void *d1
, const void *d2
)
1355 dir_data
*p1
= *(dir_data
**)d1
;
1356 dir_data
*p2
= *(dir_data
**)d2
;
1358 return strcmp(p1
->name
, p2
->name
);
1362 /************ Menu Configuration From Directory *************/
1365 isFilePackage(char *file
)
1369 /* check if the extension indicates this file is a
1370 * file package. For now, only recognize .themed */
1374 if (l
> 7 && strcmp(&(file
[l
-7]), ".themed")==0) {
1383 readMenuDirectory(WScreen
*scr
, char *title
, char **path
, char *command
)
1386 struct dirent
*dentry
;
1387 struct stat stat_buf
;
1390 WMArray
*dirs
= NULL
, *files
= NULL
;
1391 WMArrayIterator iter
;
1392 int length
, i
, have_space
=0;
1394 int stripExtension
= 0;
1397 dirs
= WMCreateArray(16);
1398 files
= WMCreateArray(16);
1401 while (path
[i
]!=NULL
) {
1402 if (strcmp(path
[i
], "-noext")==0) {
1408 dir
= opendir(path
[i
]);
1414 while ((dentry
= readdir(dir
))) {
1416 if (strcmp(dentry
->d_name
, ".")==0 ||
1417 strcmp(dentry
->d_name
, "..")==0)
1420 if (dentry
->d_name
[0] == '.')
1423 buffer
= malloc(strlen(path
[i
])+strlen(dentry
->d_name
)+4);
1425 wsyserror(_("out of memory while constructing directory menu %s"),
1430 strcpy(buffer
, path
[i
]);
1431 strcat(buffer
, "/");
1432 strcat(buffer
, dentry
->d_name
);
1434 if (stat(buffer
, &stat_buf
)!=0) {
1435 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1436 path
[i
], dentry
->d_name
);
1438 Bool isFilePack
= False
;
1441 if (S_ISDIR(stat_buf
.st_mode
)
1442 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1444 /* access always returns success for user root */
1445 if (access(buffer
, X_OK
)==0) {
1446 /* Directory is accesible. Add to directory list */
1448 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1449 data
->name
= wstrdup(dentry
->d_name
);
1452 WMAddToArray(dirs
, data
);
1454 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1455 /* Hack because access always returns X_OK success for user root */
1456 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1457 if ((command
!=NULL
&& access(buffer
, R_OK
)==0) ||
1458 (command
==NULL
&& access(buffer
, X_OK
)==0 &&
1459 (stat_buf
.st_mode
& S_IXANY
))) {
1461 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1462 data
->name
= wstrdup(dentry
->d_name
);
1465 WMAddToArray(files
, data
);
1476 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1482 WMSortArray(dirs
, myCompare
);
1483 WMSortArray(files
, myCompare
);
1485 menu
= wMenuCreate(scr
, title
, False
);
1486 menu
->on_destroy
= removeShortcutsForMenu
;
1488 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1489 /* New directory. Use same OPEN_MENU command that was used
1490 * for the current directory. */
1491 length
= strlen(path
[data
->index
])+strlen(data
->name
)+6;
1495 length
+= strlen(command
) + 6;
1496 buffer
= malloc(length
);
1498 wsyserror(_("out of memory while constructing directory menu %s"),
1505 strcat(buffer
, "-noext ");
1507 have_space
= strchr(path
[data
->index
], ' ')!=NULL
||
1508 strchr(data
->name
, ' ')!=NULL
;
1511 strcat(buffer
, "\"");
1512 strcat(buffer
, path
[data
->index
]);
1514 strcat(buffer
, "/");
1515 strcat(buffer
, data
->name
);
1517 strcat(buffer
, "\"");
1519 strcat(buffer
, " WITH ");
1520 strcat(buffer
, command
);
1523 addMenuEntry(menu
, data
->name
, NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1531 WM_ITERATE_ARRAY(files
, data
, iter
) {
1532 /* executable: add as entry */
1533 length
= strlen(path
[data
->index
])+strlen(data
->name
)+6;
1535 length
+= strlen(command
);
1537 buffer
= malloc(length
);
1539 wsyserror(_("out of memory while constructing directory menu %s"),
1544 have_space
= strchr(path
[data
->index
], ' ')!=NULL
||
1545 strchr(data
->name
, ' ')!=NULL
;
1546 if (command
!=NULL
) {
1547 strcpy(buffer
, command
);
1548 strcat(buffer
, " ");
1550 strcat(buffer
, "\"");
1551 strcat(buffer
, path
[data
->index
]);
1556 strcat(buffer
, path
[data
->index
]);
1558 strcpy(buffer
, path
[data
->index
]);
1561 strcat(buffer
, "/");
1562 strcat(buffer
, data
->name
);
1564 strcat(buffer
, "\"");
1566 if (stripExtension
) {
1567 char *ptr
= strrchr(data
->name
, '.');
1568 if (ptr
&& ptr
!=data
->name
)
1571 addMenuEntry(menu
, data
->name
, NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1586 /************ Menu Configuration From WMRootMenu *************/
1589 makeDefaultMenu(WScreen
*scr
)
1593 menu
= wMenuCreate(scr
, _("Commands"), True
);
1594 wMenuAddCallback(menu
, "XTerm", execCommand
, "xterm");
1595 wMenuAddCallback(menu
, "rxvt", execCommand
, "rxvt");
1596 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1597 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1606 *----------------------------------------------------------------------
1608 * Reads root menu configuration from defaults database.
1610 *----------------------------------------------------------------------
1613 configureMenu(WScreen
*scr
, WMPropList
*definition
)
1618 WMPropList
*title
, *command
, *params
;
1622 if (WMIsPLString(definition
)) {
1623 struct stat stat_buf
;
1625 Bool menu_is_default
= False
;
1627 /* menu definition is a string. Probably a path, so parse the file */
1629 tmp
= wexpandpath(WMGetFromPLString(definition
));
1631 path
= getLocalizedMenuFile(tmp
);
1634 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1637 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1638 menu_is_default
= True
;
1642 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"),
1648 if (stat(path
, &stat_buf
)<0) {
1649 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1655 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1656 /* if the pointer in WMRootMenu has changed */
1657 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1659 if (menu_is_default
) {
1660 wwarning(_("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1664 menu
= readMenuFile(scr
, path
);
1666 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1676 count
= WMGetPropListItemCount(definition
);
1680 elem
= WMGetFromPLArray(definition
, 0);
1681 if (!WMIsPLString(elem
)) {
1682 tmp
= WMGetPropListDescription(elem
, False
);
1683 wwarning(_("%s:format error in root menu configuration \"%s\""),
1688 mtitle
= WMGetFromPLString(elem
);
1690 menu
= wMenuCreate(scr
, mtitle
, False
);
1691 menu
->on_destroy
= removeShortcutsForMenu
;
1693 #ifdef GLOBAL_SUBMENU_FILE
1698 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1701 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1702 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1707 for (i
=1; i
<count
; i
++) {
1708 elem
= WMGetFromPLArray(definition
, i
);
1710 if (WMIsPLString(elem
)) {
1713 file
= WMGetFromPLString(elem
);
1717 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1720 if (WMIsPLArray(WMGetFromPLArray(elem
,1))) {
1725 submenu
= configureMenu(scr
, elem
);
1727 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
,
1729 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1733 WMPropList
*shortcut
;
1736 title
= WMGetFromPLArray(elem
, idx
++);
1737 shortcut
= WMGetFromPLArray(elem
, idx
++);
1738 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT")==0) {
1739 shortcut
= WMGetFromPLArray(elem
, idx
++);
1740 command
= WMGetFromPLArray(elem
, idx
++);
1745 params
= WMGetFromPLArray(elem
, idx
++);
1747 if (!title
|| !command
)
1750 addMenuEntry(menu
, WMGetFromPLString(title
),
1751 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1752 WMGetFromPLString(command
),
1753 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1758 tmp
= WMGetPropListDescription(elem
, False
);
1759 wwarning(_("%s:format error in root menu configuration \"%s\""),
1775 *----------------------------------------------------------------------
1777 * Opens the root menu, parsing the menu configuration from the
1778 * defaults database.
1779 * If the menu is already mapped and is not sticked to the
1780 * root window, it will be unmapped.
1783 * The menu may be remade.
1786 * Construction of OPEN_MENU entries are delayed to the moment the
1788 *----------------------------------------------------------------------
1791 OpenRootMenu(WScreen
*scr
, int x
, int y
, int keyboard
)
1794 WMPropList
*definition
;
1796 static WMPropList *domain=NULL;
1799 domain = WMCreatePLString("WMRootMenu");
1803 scr
->flags
.root_menu_changed_shortcuts
= 0;
1804 scr
->flags
.added_workspace_menu
= 0;
1805 scr
->flags
.added_windows_menu
= 0;
1807 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1808 menu
= scr
->root_menu
;
1809 if (!menu
->flags
.buttoned
) {
1812 wRaiseFrame(menu
->frame
->core
);
1815 wMenuMapAt(menu
, 0, 0, True
);
1817 wMenuMapCopyAt(menu
, x
-menu
->frame
->core
->width
/2, y
);
1823 definition
= WDRootMenu
->dictionary
;
1826 definition = PLGetDomain(domain);
1829 if (WMIsPLArray(definition
)) {
1831 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1832 menu
= configureMenu(scr
, definition
);
1834 menu
->timestamp
= WDRootMenu
->timestamp
;
1839 menu
= configureMenu(scr
, definition
);
1844 /* menu hasn't changed or could not be read */
1845 if (!scr
->root_menu
) {
1846 wMessageDialog(scr
, _("Error"),
1847 _("The applications menu could not be loaded. "
1848 "Look at the console output for a detailed "
1849 "description of the errors."),
1850 _("OK"), NULL
, NULL
);
1852 menu
= makeDefaultMenu(scr
);
1853 scr
->root_menu
= menu
;
1855 menu
= scr
->root_menu
;
1858 if (scr
->root_menu
) {
1859 wMenuDestroy(scr
->root_menu
, True
);
1861 scr
->root_menu
= menu
;
1866 if (keyboard
&& x
==0 && y
==0) {
1868 } else if (keyboard
&& x
==scr
->scr_width
/2 && y
==scr
->scr_height
/2) {
1869 newx
= x
- menu
->frame
->core
->width
/2;
1870 newy
= y
- menu
->frame
->core
->height
/2;
1872 newx
= x
- menu
->frame
->core
->width
/2;
1875 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1878 if (scr
->flags
.root_menu_changed_shortcuts
)
1879 rebindKeygrabs(scr
);