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 * || will do the same, but will not cache the contents.
132 * SAVE_SESSION - saves the current state of the desktop, which include
133 * all running applications, all their hints (geometry,
134 * position on screen, workspace they live on, the dock
135 * or clip from where they were launched, and
136 * if minimized, shaded or hidden. Also saves the current
137 * workspace the user is on. All will be restored on every
138 * start of windowmaker until another SAVE_SESSION or
139 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
140 * WindowMaker domain file, then saving is automatically
141 * done on every windowmaker exit, overwriting any
142 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
144 * CLEAR_SESSION - clears any previous saved session. This will not have
145 * any effect if SaveSessionOnExit is True.
154 execCommand(WMenu
*menu
, WMenuEntry
*entry
)
158 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char*)entry
->clientdata
);
160 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
161 GrabModeAsync
, GrabModeAsync
, None
, wCursor
[WCUR_WAIT
],
166 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
169 XUngrabPointer(dpy
, CurrentTime
);
175 exitCommand(WMenu
*menu
, WMenuEntry
*entry
)
177 static int inside
= 0;
180 /* prevent reentrant calls */
190 if ((long)entry
->clientdata
==M_QUICK
) {
193 int r
, oldSaveSessionFlag
;
195 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
196 r
= wExitDialog(menu
->frame
->screen_ptr
, _("Exit"),
197 _("Exit window manager?"),
198 _("Exit"), _("Cancel"), NULL
);
200 if (r
==WAPRDefault
) {
202 } else if (r
==WAPRAlternate
) {
203 /* Don't modify the "save session on exit" flag if the
204 * user canceled the operation. */
205 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
208 if (result
==R_EXIT
) {
210 printf("Exiting WindowMaker.\n");
212 Shutdown(WSExitMode
);
221 shutdownCommand(WMenu
*menu
, WMenuEntry
*entry
)
223 static int inside
= 0;
226 /* prevent reentrant calls */
237 if ((long)entry
->clientdata
==M_QUICK
)
241 if (wSessionIsManaged()) {
244 r
= wMessageDialog(menu
->frame
->screen_ptr
,
245 _("Close X session"),
246 _("Close Window System session?\n"
247 "Kill might close applications with unsaved data."),
248 _("Close"), _("Kill"), _("Cancel"));
251 else if (r
==WAPRAlternate
)
256 int r
, oldSaveSessionFlag
;
258 oldSaveSessionFlag
= wPreferences
.save_session_on_exit
;
260 r
= wExitDialog(menu
->frame
->screen_ptr
,
262 _("Kill Window System session?\n"
263 "(all applications will be closed)"),
264 _("Kill"), _("Cancel"), NULL
);
265 if (r
==WAPRDefault
) {
267 } else if (r
==WAPRAlternate
) {
268 /* Don't modify the "save session on exit" flag if the
269 * user canceled the operation. */
270 wPreferences
.save_session_on_exit
= oldSaveSessionFlag
;
275 if (result
!=R_CANCEL
) {
277 if (result
== R_CLOSE
) {
278 Shutdown(WSLogoutMode
);
280 #endif /* XSMP_ENABLED */
282 Shutdown(WSKillMode
);
293 restartCommand(WMenu
*menu
, WMenuEntry
*entry
)
295 Shutdown(WSRestartPreparationMode
);
296 Restart((char*)entry
->clientdata
, False
);
302 refreshCommand(WMenu
*menu
, WMenuEntry
*entry
)
304 wRefreshDesktop(menu
->frame
->screen_ptr
);
309 arrangeIconsCommand(WMenu
*menu
, WMenuEntry
*entry
)
311 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
315 showAllCommand(WMenu
*menu
, WMenuEntry
*entry
)
317 wShowAllWindows(menu
->frame
->screen_ptr
);
321 hideOthersCommand(WMenu
*menu
, WMenuEntry
*entry
)
323 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
328 saveSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
330 if (!wPreferences
.save_session_on_exit
)
331 wSessionSaveState(menu
->frame
->screen_ptr
);
333 wScreenSaveState(menu
->frame
->screen_ptr
);
338 clearSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
340 wSessionClearState(menu
->frame
->screen_ptr
);
345 infoPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
347 wShowInfoPanel(menu
->frame
->screen_ptr
);
352 legalPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
354 wShowLegalPanel(menu
->frame
->screen_ptr
);
358 /********************************************************************/
362 getLocalizedMenuFile(char *menu
)
371 len
= strlen(menu
)+strlen(Locale
)+8;
372 buffer
= wmalloc(len
);
374 /* try menu.locale_name */
375 snprintf(buffer
, len
, "%s.%s", menu
, Locale
);
376 if (access(buffer
, F_OK
)==0) {
379 /* check if it is in the form aa_bb.encoding and check for aa_bb */
380 ptr
= strchr(Locale
, '.');
383 if (access(buffer
, F_OK
)==0) {
387 /* now check for aa */
388 ptr
= strchr(buffer
, '_');
391 if (access(buffer
, F_OK
)==0) {
401 raiseMenus(WMenu
*menu
)
405 if (menu
->flags
.mapped
) {
406 wRaiseFrame(menu
->frame
->core
);
408 for (i
=0; i
<menu
->cascade_no
; i
++) {
409 if (menu
->cascades
[i
])
410 raiseMenus(menu
->cascades
[i
]);
417 wRootMenuPerformShortcut(XEvent
*event
)
419 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
424 /* ignore CapsLock */
425 modifiers
= event
->xkey
.state
& ValidModMask
;
427 for (ptr
= shortcutList
; ptr
!=NULL
; ptr
= ptr
->next
) {
428 if (ptr
->keycode
==0 || ptr
->menu
->menu
->screen_ptr
!=scr
)
431 if (ptr
->keycode
==event
->xkey
.keycode
&& ptr
->modifier
==modifiers
) {
432 (*ptr
->entry
->callback
)(ptr
->menu
, ptr
->entry
);
442 wRootMenuBindShortcuts(Window window
)
448 if (ptr
->modifier
!=AnyModifier
) {
449 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
|LockMask
,
450 window
, True
, GrabModeAsync
, GrabModeAsync
);
452 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
,
453 window
, True
, GrabModeAsync
, GrabModeAsync
);
456 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
,
457 GrabModeAsync
, GrabModeAsync
);
464 rebindKeygrabs(WScreen
*scr
)
468 wwin
= scr
->focused_window
;
471 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
473 if (!WFLAGP(wwin
, no_bind_keys
)) {
474 wWindowSetKeyGrabs(wwin
);
482 removeShortcutsForMenu(WMenu
*menu
)
485 Shortcut
*newList
= NULL
;
490 if (ptr
->menu
== menu
) {
498 shortcutList
= newList
;
499 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
504 addShortcut(char *file
, char *shortcutDefinition
, WMenu
*menu
,
512 ptr
= wmalloc(sizeof(Shortcut
));
514 strcpy(buf
, shortcutDefinition
);
519 while ((k
= strchr(b
, '+'))!=NULL
) {
523 mod
= wXModifierFromKey(b
);
525 wwarning(_("%s:invalid key modifier \"%s\""), file
, b
);
529 ptr
->modifier
|= mod
;
535 ksym
= XStringToKeysym(b
);
537 if (ksym
==NoSymbol
) {
538 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
539 file
, shortcutDefinition
, entry
->text
);
544 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
545 if (ptr
->keycode
==0) {
546 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
547 shortcutDefinition
, entry
->text
);
555 ptr
->next
= shortcutList
;
558 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
564 /*******************************/
574 end
= &(line
[strlen(line
)])-1;
575 while (isspace(*line
) && *line
!=0) line
++;
576 while (end
>line
&& isspace(*end
)) {
585 next_token(char *line
, char **next
)
591 while (*line
==' ' || *line
=='\t') line
++;
597 while (*tmp
!=0 && *tmp
!='"') tmp
++;
599 wwarning(_("%s: unmatched '\"' in menu file"), line
);
610 } while (*tmp
!=0 && *tmp
!=' ' && *tmp
!='\t');
624 while (*tmp
==' ' || *tmp
=='\t') tmp
++;
634 separateCommand(char *line
, char ***file
, char **command
)
636 char *token
, *tmp
= line
;
637 WMArray
*array
= WMCreateArray(4);
643 token
= next_token(tmp
, &tmp
);
645 if (strcmp(token
, "WITH")==0) {
646 if (tmp
!=NULL
&& *tmp
!=0)
647 *command
= wstrdup(tmp
);
649 wwarning(_("%s: missing command"), line
);
652 WMAddToArray(array
, token
);
654 } while (token
!=NULL
&& tmp
!=NULL
);
656 count
= WMGetArrayItemCount(array
);
658 *file
= wmalloc(sizeof(char*)*(count
+1));
659 (*file
)[count
] = NULL
;
660 for (i
= 0; i
< count
; i
++) {
661 (*file
)[i
] = WMGetFromArray(array
, i
);
669 constructMenu(WMenu
*menu
, WMenuEntry
*entry
)
672 struct stat stat_buf
;
679 separateCommand((char*)entry
->clientdata
, &path
, &cmd
);
680 if (path
== NULL
|| *path
==NULL
|| **path
==0) {
681 wwarning(_("invalid OPEN_MENU specification: %s"),
682 (char*)entry
->clientdata
);
686 if (path
[0][0]=='|') {
689 if (!menu
->cascades
[entry
->cascade
] ||
690 menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
693 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
695 if(submenu
!= NULL
) {
696 if (path
[0][1] == '|')
697 submenu
->timestamp
= 0;
699 submenu
->timestamp
= 1; /* there's no automatic reloading */
707 while(path
[i
] != NULL
) {
710 if (strcmp(path
[i
], "-noext")==0) {
715 tmp
= wexpandpath(path
[i
]);
717 lpath
= getLocalizedMenuFile(tmp
);
726 if (stat(path
[i
], &stat_buf
)==0) {
727 if (last
< stat_buf
.st_mtime
)
728 last
= stat_buf
.st_mtime
;
732 wsyserror(_("%s:could not stat menu"), path
[i
]);
740 wsyserror(_("%s:could not stat menu:%s"), "OPEN_MENU",
741 (char*)entry
->clientdata
);
744 stat(path
[first
], &stat_buf
);
745 if (!menu
->cascades
[entry
->cascade
]
746 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
748 if (S_ISDIR(stat_buf
.st_mode
)) {
750 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
,
751 entry
->text
, path
, cmd
);
753 submenu
->timestamp
= last
;
754 } else if (S_ISREG(stat_buf
.st_mode
)) {
758 wwarning(_("too many parameters in OPEN_MENU: %s"),
759 (char*)entry
->clientdata
);
761 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
763 submenu
->timestamp
= stat_buf
.st_mtime
;
773 wMenuEntryRemoveCascade(menu
, entry
);
774 wMenuEntrySetCascade(menu
, entry
, submenu
);
779 while (path
[i
]!=NULL
)
788 cleanupWorkspaceMenu(WMenu
*menu
)
790 if (menu
->frame
->screen_ptr
->workspace_menu
== menu
)
791 menu
->frame
->screen_ptr
->workspace_menu
= NULL
;
796 addWorkspaceMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
801 if (scr
->flags
.added_workspace_menu
) {
802 wwarning(_("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
805 scr
->flags
.added_workspace_menu
= 1;
807 wsmenu
= wWorkspaceMenuMake(scr
, True
);
808 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
810 scr
->workspace_menu
= wsmenu
;
811 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
812 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
814 wWorkspaceMenuUpdate(scr
, wsmenu
);
821 cleanupWindowsMenu(WMenu
*menu
)
823 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
824 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
829 addWindowsMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
835 if (scr
->flags
.added_windows_menu
) {
836 wwarning(_("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
839 scr
->flags
.added_windows_menu
= 1;
841 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
842 wwmenu
->on_destroy
= cleanupWindowsMenu
;
843 scr
->switch_menu
= wwmenu
;
844 wwin
= scr
->focused_window
;
846 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
850 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
851 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
858 addMenuEntry(WMenu
*menu
, char *title
, char *shortcut
, char *command
,
859 char *params
, char *file_name
)
862 WMenuEntry
*entry
= NULL
;
863 Bool shortcutOk
= False
;
867 scr
= menu
->frame
->screen_ptr
;
868 if (strcmp(command
, "OPEN_MENU")==0) {
870 wwarning(_("%s:missing parameter for menu command \"%s\""),
876 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
878 path
= wstrdup(params
);
880 dummy
= wMenuCreate(scr
, title
, False
);
881 dummy
->on_destroy
= removeShortcutsForMenu
;
882 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
883 entry
->free_cdata
= free
;
884 wMenuEntrySetCascade(menu
, entry
, dummy
);
886 } else if (strcmp(command
, "EXEC")==0) {
888 wwarning(_("%s:missing parameter for menu command \"%s\""),
891 entry
= wMenuAddCallback(menu
, title
, execCommand
,
892 wstrconcat("exec ", params
));
893 entry
->free_cdata
= free
;
896 } else if (strcmp(command
, "SHEXEC")==0) {
898 wwarning(_("%s:missing parameter for menu command \"%s\""),
901 entry
= wMenuAddCallback(menu
, title
, execCommand
,
903 entry
->free_cdata
= free
;
906 } else if (strcmp(command
, "EXIT")==0) {
908 if (params
&& strcmp(params
, "QUICK")==0)
909 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void*)M_QUICK
);
911 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
914 } else if (strcmp(command
, "SHUTDOWN")==0) {
916 if (params
&& strcmp(params
, "QUICK")==0)
917 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
,
920 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
923 } else if (strcmp(command
, "REFRESH")==0) {
924 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
927 } else if (strcmp(command
, "WORKSPACE_MENU")==0) {
928 entry
= addWorkspaceMenu(scr
, menu
, title
);
931 } else if (strcmp(command
, "WINDOWS_MENU")==0) {
932 entry
= addWindowsMenu(scr
, menu
, title
);
935 } else if (strcmp(command
, "ARRANGE_ICONS")==0) {
936 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
939 } else if (strcmp(command
, "HIDE_OTHERS")==0) {
940 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
943 } else if (strcmp(command
, "SHOW_ALL")==0) {
944 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
947 } else if (strcmp(command
, "RESTART")==0) {
948 entry
= wMenuAddCallback(menu
, title
, restartCommand
,
949 params
? wstrdup(params
) : NULL
);
950 entry
->free_cdata
= free
;
952 } else if (strcmp(command
, "SAVE_SESSION")==0) {
953 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
956 } else if (strcmp(command
, "CLEAR_SESSION")==0) {
957 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
959 } else if (strcmp(command
, "INFO_PANEL")==0) {
960 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
962 } else if (strcmp(command
, "LEGAL_PANEL")==0) {
963 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
966 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
,
972 if (shortcut
&& entry
) {
974 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
,
977 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
979 entry
->rtext
= GetShortcutString(shortcut
);
981 entry->rtext = wstrdup(shortcut);
992 /******************* Menu Configuration From File *******************/
995 separateline(char *line
, char *title
, char *command
, char *parameter
,
1007 while (isspace(*line
) && (*line
!=0)) line
++;
1011 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
1016 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1018 strncpy(title
, line
, i
);
1022 /* get the command or shortcut keyword */
1023 while (isspace(*line
) && (*line
!=0)) line
++;
1027 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1028 strncpy(command
, line
, i
);
1032 if (strcmp(command
, "SHORTCUT")==0) {
1033 /* get the shortcut key */
1034 while (isspace(*line
) && (*line
!=0)) line
++;
1038 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
1043 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1045 strncpy(shortcut
, line
, i
);
1051 /* get the command */
1052 while (isspace(*line
) && (*line
!=0)) line
++;
1056 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1057 strncpy(command
, line
, i
);
1062 /* get the parameters */
1063 while (isspace(*line
) && (*line
!=0)) line
++;
1070 while (line
[l
]!=0 && line
[l
]!='"') {
1071 parameter
[l
] = line
[l
];
1079 while (isspace(line
[l
]) && (l
>0)) l
--;
1080 strncpy(parameter
, line
, l
);
1086 parseCascade(WScreen
*scr
, WMenu
*menu
, FILE *file
, char *file_name
)
1088 char linebuf
[MAXLINE
];
1089 char elinebuf
[MAXLINE
];
1090 char title
[MAXLINE
];
1091 char command
[MAXLINE
];
1092 char shortcut
[MAXLINE
];
1093 char params
[MAXLINE
];
1096 while (!IsEof(file
)) {
1100 fgets(linebuf
, MAXLINE
, file
);
1101 line
= cropline(linebuf
);
1102 lsize
= strlen(line
);
1104 if (line
[lsize
-1]=='\\') {
1107 fgets(elinebuf
, MAXLINE
, file
);
1108 line2
=cropline(elinebuf
);
1109 lsize2
=strlen(line2
);
1110 if (lsize2
+lsize
>MAXLINE
) {
1111 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1117 strcat(line
, line2
);
1122 } while (!ok
&& !IsEof(file
));
1126 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1130 separateline(line
, title
, command
, params
, shortcut
);
1133 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1138 if (strcasecmp(command
, "MENU")==0) {
1143 cascade
= wMenuCreate(scr
, title
, False
);
1144 cascade
->on_destroy
= removeShortcutsForMenu
;
1145 if (parseCascade(scr
, cascade
, file
, file_name
)==NULL
) {
1146 wMenuDestroy(cascade
, True
);
1148 wMenuEntrySetCascade(menu
,
1149 wMenuAddCallback(menu
, title
, NULL
, NULL
),
1152 } else if (strcasecmp(command
, "END")==0) {
1158 addMenuEntry(menu
, title
, shortcut
[0] ? shortcut
: NULL
, command
,
1159 params
[0] ? params
: NULL
, file_name
);
1163 wwarning(_("%s:syntax error in menu file:END declaration missing"),
1173 readMenuFile(WScreen
*scr
, char *file_name
)
1177 char linebuf
[MAXLINE
];
1178 char title
[MAXLINE
];
1179 char shortcut
[MAXLINE
];
1180 char command
[MAXLINE
];
1181 char params
[MAXLINE
];
1189 if (!wPreferences
.flags
.nocpp
) {
1190 args
= MakeCPPArgs(file_name
);
1192 wwarning(_("could not make arguments for menu file preprocessor"));
1194 snprintf(command
, sizeof(command
), "%s %s %s",
1195 CPP_PATH
, args
, file_name
);
1197 file
= popen(command
, "r");
1199 wsyserror(_("%s:could not open/preprocess menu file"),
1209 file
= fopen(file_name
, "r");
1211 wsyserror(_("%s:could not open menu file"), file_name
);
1216 while (!IsEof(file
)) {
1217 if (!fgets(linebuf
, MAXLINE
, file
))
1219 line
= cropline(linebuf
);
1220 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1223 separateline(line
, title
, command
, params
, shortcut
);
1226 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1230 if (strcasecmp(command
, "MENU")==0) {
1231 menu
= wMenuCreate(scr
, title
, True
);
1232 menu
->on_destroy
= removeShortcutsForMenu
;
1233 if (!parseCascade(scr
, menu
, file
, file_name
)) {
1234 wMenuDestroy(menu
, True
);
1238 wwarning(_("%s:invalid menu file. MENU command is missing"),
1246 if (pclose(file
)==-1) {
1247 wsyserror(_("error reading preprocessed menu data"));
1259 /************ Menu Configuration From Pipe *************/
1262 readMenuPipe(WScreen
*scr
, char **file_name
)
1266 char linebuf
[MAXLINE
];
1267 char title
[MAXLINE
];
1268 char command
[MAXLINE
];
1269 char params
[MAXLINE
];
1270 char shortcut
[MAXLINE
];
1273 char flat_file
[MAXLINE
];
1280 flat_file
[0] = '\0';
1282 for(i
=0; file_name
[i
]!=NULL
; i
++) {
1283 strcat(flat_file
, file_name
[i
]);
1284 strcat(flat_file
, " ");
1286 filename
= flat_file
+1;
1289 if (!wPreferences
.flags
.nocpp
) {
1290 args
= MakeCPPArgs(filename
);
1292 wwarning(_("could not make arguments for menu file preprocessor"));
1294 snprintf(command
, sizeof(command
), "%s | %s %s",
1295 filename
, CPP_PATH
, args
);
1298 file
= popen(command
, "r");
1300 wsyserror(_("%s:could not open/preprocess menu file"), filename
);
1310 file
= popen(filename
, "r");
1313 wsyserror(_("%s:could not open menu file"), filename
);
1318 while (!IsEof(file
)) {
1319 if (!fgets(linebuf
, MAXLINE
, file
))
1321 line
= cropline(linebuf
);
1322 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1325 separateline(line
, title
, command
, params
, shortcut
);
1328 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1332 if (strcasecmp(command
, "MENU")==0) {
1333 menu
= wMenuCreate(scr
, title
, True
);
1334 menu
->on_destroy
= removeShortcutsForMenu
;
1335 if (!parseCascade(scr
, menu
, file
, filename
)) {
1336 wMenuDestroy(menu
, True
);
1340 wwarning(_("%s:no title given for the root menu"), filename
);
1359 myCompare(const void *d1
, const void *d2
)
1361 dir_data
*p1
= *(dir_data
**)d1
;
1362 dir_data
*p2
= *(dir_data
**)d2
;
1364 return strcmp(p1
->name
, p2
->name
);
1368 /************ Menu Configuration From Directory *************/
1371 isFilePackage(char *file
)
1375 /* check if the extension indicates this file is a
1376 * file package. For now, only recognize .themed */
1380 if (l
> 7 && strcmp(&(file
[l
-7]), ".themed")==0) {
1389 readMenuDirectory(WScreen
*scr
, char *title
, char **path
, char *command
)
1392 struct dirent
*dentry
;
1393 struct stat stat_buf
;
1396 WMArray
*dirs
= NULL
, *files
= NULL
;
1397 WMArrayIterator iter
;
1398 int length
, i
, have_space
=0;
1400 int stripExtension
= 0;
1403 dirs
= WMCreateArray(16);
1404 files
= WMCreateArray(16);
1407 while (path
[i
]!=NULL
) {
1408 if (strcmp(path
[i
], "-noext")==0) {
1414 dir
= opendir(path
[i
]);
1420 while ((dentry
= readdir(dir
))) {
1422 if (strcmp(dentry
->d_name
, ".")==0 ||
1423 strcmp(dentry
->d_name
, "..")==0)
1426 if (dentry
->d_name
[0] == '.')
1429 buffer
= malloc(strlen(path
[i
])+strlen(dentry
->d_name
)+4);
1431 wsyserror(_("out of memory while constructing directory menu %s"),
1436 strcpy(buffer
, path
[i
]);
1437 strcat(buffer
, "/");
1438 strcat(buffer
, dentry
->d_name
);
1440 if (stat(buffer
, &stat_buf
)!=0) {
1441 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1442 path
[i
], dentry
->d_name
);
1444 Bool isFilePack
= False
;
1447 if (S_ISDIR(stat_buf
.st_mode
)
1448 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1450 /* access always returns success for user root */
1451 if (access(buffer
, X_OK
)==0) {
1452 /* Directory is accesible. Add to directory list */
1454 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1455 data
->name
= wstrdup(dentry
->d_name
);
1458 WMAddToArray(dirs
, data
);
1460 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1461 /* Hack because access always returns X_OK success for user root */
1462 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1463 if ((command
!=NULL
&& access(buffer
, R_OK
)==0) ||
1464 (command
==NULL
&& access(buffer
, X_OK
)==0 &&
1465 (stat_buf
.st_mode
& S_IXANY
))) {
1467 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1468 data
->name
= wstrdup(dentry
->d_name
);
1471 WMAddToArray(files
, data
);
1482 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1488 WMSortArray(dirs
, myCompare
);
1489 WMSortArray(files
, myCompare
);
1491 menu
= wMenuCreate(scr
, title
, False
);
1492 menu
->on_destroy
= removeShortcutsForMenu
;
1494 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1495 /* New directory. Use same OPEN_MENU command that was used
1496 * for the current directory. */
1497 length
= strlen(path
[data
->index
])+strlen(data
->name
)+6;
1501 length
+= strlen(command
) + 6;
1502 buffer
= malloc(length
);
1504 wsyserror(_("out of memory while constructing directory menu %s"),
1511 strcat(buffer
, "-noext ");
1513 have_space
= strchr(path
[data
->index
], ' ')!=NULL
||
1514 strchr(data
->name
, ' ')!=NULL
;
1517 strcat(buffer
, "\"");
1518 strcat(buffer
, path
[data
->index
]);
1520 strcat(buffer
, "/");
1521 strcat(buffer
, data
->name
);
1523 strcat(buffer
, "\"");
1525 strcat(buffer
, " WITH ");
1526 strcat(buffer
, command
);
1529 addMenuEntry(menu
, data
->name
, NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1537 WM_ITERATE_ARRAY(files
, data
, iter
) {
1538 /* executable: add as entry */
1539 length
= strlen(path
[data
->index
])+strlen(data
->name
)+6;
1541 length
+= strlen(command
);
1543 buffer
= malloc(length
);
1545 wsyserror(_("out of memory while constructing directory menu %s"),
1550 have_space
= strchr(path
[data
->index
], ' ')!=NULL
||
1551 strchr(data
->name
, ' ')!=NULL
;
1552 if (command
!=NULL
) {
1553 strcpy(buffer
, command
);
1554 strcat(buffer
, " ");
1556 strcat(buffer
, "\"");
1557 strcat(buffer
, path
[data
->index
]);
1562 strcat(buffer
, path
[data
->index
]);
1564 strcpy(buffer
, path
[data
->index
]);
1567 strcat(buffer
, "/");
1568 strcat(buffer
, data
->name
);
1570 strcat(buffer
, "\"");
1572 if (stripExtension
) {
1573 char *ptr
= strrchr(data
->name
, '.');
1574 if (ptr
&& ptr
!=data
->name
)
1577 addMenuEntry(menu
, data
->name
, NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1592 /************ Menu Configuration From WMRootMenu *************/
1595 makeDefaultMenu(WScreen
*scr
)
1599 menu
= wMenuCreate(scr
, _("Commands"), True
);
1600 wMenuAddCallback(menu
, "XTerm", execCommand
, "xterm");
1601 wMenuAddCallback(menu
, "rxvt", execCommand
, "rxvt");
1602 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1603 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1612 *----------------------------------------------------------------------
1614 * Reads root menu configuration from defaults database.
1616 *----------------------------------------------------------------------
1619 configureMenu(WScreen
*scr
, WMPropList
*definition
)
1624 WMPropList
*title
, *command
, *params
;
1628 if (WMIsPLString(definition
)) {
1629 struct stat stat_buf
;
1631 Bool menu_is_default
= False
;
1633 /* menu definition is a string. Probably a path, so parse the file */
1635 tmp
= wexpandpath(WMGetFromPLString(definition
));
1637 path
= getLocalizedMenuFile(tmp
);
1640 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1643 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1644 menu_is_default
= True
;
1648 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"),
1654 if (stat(path
, &stat_buf
)<0) {
1655 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1661 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1662 /* if the pointer in WMRootMenu has changed */
1663 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1665 if (menu_is_default
) {
1666 wwarning(_("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1670 menu
= readMenuFile(scr
, path
);
1672 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1682 count
= WMGetPropListItemCount(definition
);
1686 elem
= WMGetFromPLArray(definition
, 0);
1687 if (!WMIsPLString(elem
)) {
1688 tmp
= WMGetPropListDescription(elem
, False
);
1689 wwarning(_("%s:format error in root menu configuration \"%s\""),
1694 mtitle
= WMGetFromPLString(elem
);
1696 menu
= wMenuCreate(scr
, mtitle
, False
);
1697 menu
->on_destroy
= removeShortcutsForMenu
;
1699 #ifdef GLOBAL_SUBMENU_FILE
1704 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1707 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1708 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1713 for (i
=1; i
<count
; i
++) {
1714 elem
= WMGetFromPLArray(definition
, i
);
1716 if (WMIsPLString(elem
)) {
1719 file
= WMGetFromPLString(elem
);
1723 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1726 if (WMIsPLArray(WMGetFromPLArray(elem
,1))) {
1731 submenu
= configureMenu(scr
, elem
);
1733 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
,
1735 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1739 WMPropList
*shortcut
;
1742 title
= WMGetFromPLArray(elem
, idx
++);
1743 shortcut
= WMGetFromPLArray(elem
, idx
++);
1744 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT")==0) {
1745 shortcut
= WMGetFromPLArray(elem
, idx
++);
1746 command
= WMGetFromPLArray(elem
, idx
++);
1751 params
= WMGetFromPLArray(elem
, idx
++);
1753 if (!title
|| !command
)
1756 addMenuEntry(menu
, WMGetFromPLString(title
),
1757 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1758 WMGetFromPLString(command
),
1759 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1764 tmp
= WMGetPropListDescription(elem
, False
);
1765 wwarning(_("%s:format error in root menu configuration \"%s\""),
1781 *----------------------------------------------------------------------
1783 * Opens the root menu, parsing the menu configuration from the
1784 * defaults database.
1785 * If the menu is already mapped and is not sticked to the
1786 * root window, it will be unmapped.
1789 * The menu may be remade.
1792 * Construction of OPEN_MENU entries are delayed to the moment the
1794 *----------------------------------------------------------------------
1797 OpenRootMenu(WScreen
*scr
, int x
, int y
, int keyboard
)
1800 WMPropList
*definition
;
1802 static WMPropList *domain=NULL;
1805 domain = WMCreatePLString("WMRootMenu");
1809 scr
->flags
.root_menu_changed_shortcuts
= 0;
1810 scr
->flags
.added_workspace_menu
= 0;
1811 scr
->flags
.added_windows_menu
= 0;
1813 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1814 menu
= scr
->root_menu
;
1815 if (!menu
->flags
.buttoned
) {
1818 wRaiseFrame(menu
->frame
->core
);
1821 wMenuMapAt(menu
, 0, 0, True
);
1823 wMenuMapCopyAt(menu
, x
-menu
->frame
->core
->width
/2, y
);
1829 definition
= WDRootMenu
->dictionary
;
1832 definition = PLGetDomain(domain);
1835 if (WMIsPLArray(definition
)) {
1837 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1838 menu
= configureMenu(scr
, definition
);
1840 menu
->timestamp
= WDRootMenu
->timestamp
;
1845 menu
= configureMenu(scr
, definition
);
1850 /* menu hasn't changed or could not be read */
1851 if (!scr
->root_menu
) {
1852 wMessageDialog(scr
, _("Error"),
1853 _("The applications menu could not be loaded. "
1854 "Look at the console output for a detailed "
1855 "description of the errors."),
1856 _("OK"), NULL
, NULL
);
1858 menu
= makeDefaultMenu(scr
);
1859 scr
->root_menu
= menu
;
1861 menu
= scr
->root_menu
;
1864 if (scr
->root_menu
) {
1865 wMenuDestroy(scr
->root_menu
, True
);
1867 scr
->root_menu
= menu
;
1872 if (keyboard
&& x
==0 && y
==0) {
1874 } else if (keyboard
&& x
==scr
->scr_width
/2 && y
==scr
->scr_height
/2) {
1875 newx
= x
- menu
->frame
->core
->width
/2;
1876 newy
= y
- menu
->frame
->core
->height
/2;
1878 newx
= x
- menu
->frame
->core
->width
/2;
1881 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1884 if (scr
->flags
.root_menu_changed_shortcuts
)
1885 rebindKeygrabs(scr
);