1 /* rootmenu.c- user defined menu
3 * Window Maker window manager
5 * Copyright (c) 1997-2002 Alfredo K. Kojima
6 * Copyright (c) 1998-2002 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
);
341 wScreenSaveState(menu
->frame
->screen_ptr
);
346 infoPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
348 wShowInfoPanel(menu
->frame
->screen_ptr
);
353 legalPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
355 wShowLegalPanel(menu
->frame
->screen_ptr
);
359 /********************************************************************/
363 getLocalizedMenuFile(char *menu
)
372 len
= strlen(menu
)+strlen(Locale
)+8;
373 buffer
= wmalloc(len
);
375 /* try menu.locale_name */
376 snprintf(buffer
, len
, "%s.%s", menu
, Locale
);
377 if (access(buffer
, F_OK
)==0) {
380 /* check if it is in the form aa_bb.encoding and check for aa_bb */
381 ptr
= strchr(Locale
, '.');
384 if (access(buffer
, F_OK
)==0) {
388 /* now check for aa */
389 ptr
= strchr(buffer
, '_');
392 if (access(buffer
, F_OK
)==0) {
402 raiseMenus(WMenu
*menu
)
406 if (menu
->flags
.mapped
) {
407 wRaiseFrame(menu
->frame
->core
);
409 for (i
=0; i
<menu
->cascade_no
; i
++) {
410 if (menu
->cascades
[i
])
411 raiseMenus(menu
->cascades
[i
]);
418 wRootMenuPerformShortcut(XEvent
*event
)
420 WScreen
*scr
= wScreenForRootWindow(event
->xkey
.root
);
425 /* ignore CapsLock */
426 modifiers
= event
->xkey
.state
& ValidModMask
;
428 for (ptr
= shortcutList
; ptr
!=NULL
; ptr
= ptr
->next
) {
429 if (ptr
->keycode
==0 || ptr
->menu
->menu
->screen_ptr
!=scr
)
432 if (ptr
->keycode
==event
->xkey
.keycode
&& ptr
->modifier
==modifiers
) {
433 (*ptr
->entry
->callback
)(ptr
->menu
, ptr
->entry
);
443 wRootMenuBindShortcuts(Window window
)
449 if (ptr
->modifier
!=AnyModifier
) {
450 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
|LockMask
,
451 window
, True
, GrabModeAsync
, GrabModeAsync
);
453 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
,
454 window
, True
, GrabModeAsync
, GrabModeAsync
);
457 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
,
458 GrabModeAsync
, GrabModeAsync
);
465 rebindKeygrabs(WScreen
*scr
)
469 wwin
= scr
->focused_window
;
472 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
474 if (!WFLAGP(wwin
, no_bind_keys
)) {
475 wWindowSetKeyGrabs(wwin
);
483 removeShortcutsForMenu(WMenu
*menu
)
486 Shortcut
*newList
= NULL
;
491 if (ptr
->menu
== menu
) {
499 shortcutList
= newList
;
500 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
505 addShortcut(char *file
, char *shortcutDefinition
, WMenu
*menu
,
513 ptr
= wmalloc(sizeof(Shortcut
));
515 strcpy(buf
, shortcutDefinition
);
520 while ((k
= strchr(b
, '+'))!=NULL
) {
524 mod
= wXModifierFromKey(b
);
526 wwarning(_("%s:invalid key modifier \"%s\""), file
, b
);
530 ptr
->modifier
|= mod
;
536 ksym
= XStringToKeysym(b
);
538 if (ksym
==NoSymbol
) {
539 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
540 file
, shortcutDefinition
, entry
->text
);
545 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
546 if (ptr
->keycode
==0) {
547 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
548 shortcutDefinition
, entry
->text
);
556 ptr
->next
= shortcutList
;
559 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
565 /*******************************/
575 end
= &(line
[strlen(line
)])-1;
576 while (isspace(*line
) && *line
!=0) line
++;
577 while (end
>line
&& isspace(*end
)) {
586 next_token(char *line
, char **next
)
592 while (*line
==' ' || *line
=='\t') line
++;
598 while (*tmp
!=0 && *tmp
!='"') tmp
++;
600 wwarning(_("%s: unmatched '\"' in menu file"), line
);
611 } while (*tmp
!=0 && *tmp
!=' ' && *tmp
!='\t');
625 while (*tmp
==' ' || *tmp
=='\t') tmp
++;
635 separateCommand(char *line
, char ***file
, char **command
)
637 char *token
, *tmp
= line
;
638 WMArray
*array
= WMCreateArray(4);
644 token
= next_token(tmp
, &tmp
);
646 if (strcmp(token
, "WITH")==0) {
647 if (tmp
!=NULL
&& *tmp
!=0)
648 *command
= wstrdup(tmp
);
650 wwarning(_("%s: missing command"), line
);
653 WMAddToArray(array
, token
);
655 } while (token
!=NULL
&& tmp
!=NULL
);
657 count
= WMGetArrayItemCount(array
);
659 *file
= wmalloc(sizeof(char*)*(count
+1));
660 (*file
)[count
] = NULL
;
661 for (i
= 0; i
< count
; i
++) {
662 (*file
)[i
] = WMGetFromArray(array
, i
);
670 constructMenu(WMenu
*menu
, WMenuEntry
*entry
)
673 struct stat stat_buf
;
680 separateCommand((char*)entry
->clientdata
, &path
, &cmd
);
681 if (path
== NULL
|| *path
==NULL
|| **path
==0) {
682 wwarning(_("invalid OPEN_MENU specification: %s"),
683 (char*)entry
->clientdata
);
687 if (path
[0][0]=='|') {
690 if (!menu
->cascades
[entry
->cascade
] ||
691 menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
694 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
696 if(submenu
!= NULL
) {
697 if (path
[0][1] == '|')
698 submenu
->timestamp
= 0;
700 submenu
->timestamp
= 1; /* there's no automatic reloading */
708 while(path
[i
] != NULL
) {
711 if (strcmp(path
[i
], "-noext")==0) {
716 tmp
= wexpandpath(path
[i
]);
718 lpath
= getLocalizedMenuFile(tmp
);
727 if (stat(path
[i
], &stat_buf
)==0) {
728 if (last
< stat_buf
.st_mtime
)
729 last
= stat_buf
.st_mtime
;
733 wsyserror(_("%s:could not stat menu"), path
[i
]);
741 wsyserror(_("%s:could not stat menu:%s"), "OPEN_MENU",
742 (char*)entry
->clientdata
);
745 stat(path
[first
], &stat_buf
);
746 if (!menu
->cascades
[entry
->cascade
]
747 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
749 if (S_ISDIR(stat_buf
.st_mode
)) {
751 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
,
752 entry
->text
, path
, cmd
);
754 submenu
->timestamp
= last
;
755 } else if (S_ISREG(stat_buf
.st_mode
)) {
759 wwarning(_("too many parameters in OPEN_MENU: %s"),
760 (char*)entry
->clientdata
);
762 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
764 submenu
->timestamp
= stat_buf
.st_mtime
;
774 wMenuEntryRemoveCascade(menu
, entry
);
775 wMenuEntrySetCascade(menu
, entry
, submenu
);
780 while (path
[i
]!=NULL
)
789 cleanupWorkspaceMenu(WMenu
*menu
)
791 if (menu
->frame
->screen_ptr
->workspace_menu
== menu
)
792 menu
->frame
->screen_ptr
->workspace_menu
= NULL
;
797 addWorkspaceMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
802 if (scr
->flags
.added_workspace_menu
) {
803 wwarning(_("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
806 scr
->flags
.added_workspace_menu
= 1;
808 wsmenu
= wWorkspaceMenuMake(scr
, True
);
809 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
811 scr
->workspace_menu
= wsmenu
;
812 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
813 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
815 wWorkspaceMenuUpdate(scr
, wsmenu
);
822 cleanupWindowsMenu(WMenu
*menu
)
824 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
825 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
830 addWindowsMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
836 if (scr
->flags
.added_windows_menu
) {
837 wwarning(_("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
840 scr
->flags
.added_windows_menu
= 1;
842 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
843 wwmenu
->on_destroy
= cleanupWindowsMenu
;
844 scr
->switch_menu
= wwmenu
;
845 wwin
= scr
->focused_window
;
847 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
851 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
852 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
859 addMenuEntry(WMenu
*menu
, char *title
, char *shortcut
, char *command
,
860 char *params
, char *file_name
)
863 WMenuEntry
*entry
= NULL
;
864 Bool shortcutOk
= False
;
868 scr
= menu
->frame
->screen_ptr
;
869 if (strcmp(command
, "OPEN_MENU")==0) {
871 wwarning(_("%s:missing parameter for menu command \"%s\""),
877 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
879 path
= wstrdup(params
);
881 dummy
= wMenuCreate(scr
, title
, False
);
882 dummy
->on_destroy
= removeShortcutsForMenu
;
883 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
884 entry
->free_cdata
= free
;
885 wMenuEntrySetCascade(menu
, entry
, dummy
);
887 } else if (strcmp(command
, "EXEC")==0) {
889 wwarning(_("%s:missing parameter for menu command \"%s\""),
892 entry
= wMenuAddCallback(menu
, title
, execCommand
,
893 wstrconcat("exec ", params
));
894 entry
->free_cdata
= free
;
897 } else if (strcmp(command
, "SHEXEC")==0) {
899 wwarning(_("%s:missing parameter for menu command \"%s\""),
902 entry
= wMenuAddCallback(menu
, title
, execCommand
,
904 entry
->free_cdata
= free
;
907 } else if (strcmp(command
, "EXIT")==0) {
909 if (params
&& strcmp(params
, "QUICK")==0)
910 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void*)M_QUICK
);
912 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
915 } else if (strcmp(command
, "SHUTDOWN")==0) {
917 if (params
&& strcmp(params
, "QUICK")==0)
918 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
,
921 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
924 } else if (strcmp(command
, "REFRESH")==0) {
925 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
928 } else if (strcmp(command
, "WORKSPACE_MENU")==0) {
929 entry
= addWorkspaceMenu(scr
, menu
, title
);
932 } else if (strcmp(command
, "WINDOWS_MENU")==0) {
933 entry
= addWindowsMenu(scr
, menu
, title
);
936 } else if (strcmp(command
, "ARRANGE_ICONS")==0) {
937 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
940 } else if (strcmp(command
, "HIDE_OTHERS")==0) {
941 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
944 } else if (strcmp(command
, "SHOW_ALL")==0) {
945 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
948 } else if (strcmp(command
, "RESTART")==0) {
949 entry
= wMenuAddCallback(menu
, title
, restartCommand
,
950 params
? wstrdup(params
) : NULL
);
951 entry
->free_cdata
= free
;
953 } else if (strcmp(command
, "SAVE_SESSION")==0) {
954 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
957 } else if (strcmp(command
, "CLEAR_SESSION")==0) {
958 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
960 } else if (strcmp(command
, "INFO_PANEL")==0) {
961 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
963 } else if (strcmp(command
, "LEGAL_PANEL")==0) {
964 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
967 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
,
973 if (shortcut
&& entry
) {
975 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
,
978 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
980 entry
->rtext
= GetShortcutString(shortcut
);
982 entry->rtext = wstrdup(shortcut);
993 /******************* Menu Configuration From File *******************/
996 separateline(char *line
, char *title
, char *command
, char *parameter
,
1008 while (isspace(*line
) && (*line
!=0)) line
++;
1012 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
1017 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1019 strncpy(title
, line
, i
);
1023 /* get the command or shortcut keyword */
1024 while (isspace(*line
) && (*line
!=0)) line
++;
1028 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1029 strncpy(command
, line
, i
);
1033 if (strcmp(command
, "SHORTCUT")==0) {
1034 /* get the shortcut key */
1035 while (isspace(*line
) && (*line
!=0)) line
++;
1039 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
1044 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1046 strncpy(shortcut
, line
, i
);
1052 /* get the command */
1053 while (isspace(*line
) && (*line
!=0)) line
++;
1057 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1058 strncpy(command
, line
, i
);
1063 /* get the parameters */
1064 while (isspace(*line
) && (*line
!=0)) line
++;
1071 while (line
[l
]!=0 && line
[l
]!='"') {
1072 parameter
[l
] = line
[l
];
1080 while (isspace(line
[l
]) && (l
>0)) l
--;
1081 strncpy(parameter
, line
, l
);
1087 parseCascade(WScreen
*scr
, WMenu
*menu
, FILE *file
, char *file_name
)
1089 char linebuf
[MAXLINE
];
1090 char elinebuf
[MAXLINE
];
1091 char title
[MAXLINE
];
1092 char command
[MAXLINE
];
1093 char shortcut
[MAXLINE
];
1094 char params
[MAXLINE
];
1097 while (!IsEof(file
)) {
1101 fgets(linebuf
, MAXLINE
, file
);
1102 line
= cropline(linebuf
);
1103 lsize
= strlen(line
);
1105 if (line
[lsize
-1]=='\\') {
1108 fgets(elinebuf
, MAXLINE
, file
);
1109 line2
=cropline(elinebuf
);
1110 lsize2
=strlen(line2
);
1111 if (lsize2
+lsize
>MAXLINE
) {
1112 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1118 strcat(line
, line2
);
1123 } while (!ok
&& !IsEof(file
));
1127 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1131 separateline(line
, title
, command
, params
, shortcut
);
1134 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1139 if (strcasecmp(command
, "MENU")==0) {
1144 cascade
= wMenuCreate(scr
, title
, False
);
1145 cascade
->on_destroy
= removeShortcutsForMenu
;
1146 if (parseCascade(scr
, cascade
, file
, file_name
)==NULL
) {
1147 wMenuDestroy(cascade
, True
);
1149 wMenuEntrySetCascade(menu
,
1150 wMenuAddCallback(menu
, title
, NULL
, NULL
),
1153 } else if (strcasecmp(command
, "END")==0) {
1159 addMenuEntry(menu
, title
, shortcut
[0] ? shortcut
: NULL
, command
,
1160 params
[0] ? params
: NULL
, file_name
);
1164 wwarning(_("%s:syntax error in menu file:END declaration missing"),
1174 readMenuFile(WScreen
*scr
, char *file_name
)
1178 char linebuf
[MAXLINE
];
1179 char title
[MAXLINE
];
1180 char shortcut
[MAXLINE
];
1181 char command
[MAXLINE
];
1182 char params
[MAXLINE
];
1190 if (!wPreferences
.flags
.nocpp
) {
1191 args
= MakeCPPArgs(file_name
);
1193 wwarning(_("could not make arguments for menu file preprocessor"));
1195 snprintf(command
, sizeof(command
), "%s %s %s",
1196 CPP_PATH
, args
, file_name
);
1198 file
= popen(command
, "r");
1200 wsyserror(_("%s:could not open/preprocess menu file"),
1210 file
= fopen(file_name
, "rb");
1212 wsyserror(_("%s:could not open menu file"), file_name
);
1217 while (!IsEof(file
)) {
1218 if (!fgets(linebuf
, MAXLINE
, file
))
1220 line
= cropline(linebuf
);
1221 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1224 separateline(line
, title
, command
, params
, shortcut
);
1227 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1231 if (strcasecmp(command
, "MENU")==0) {
1232 menu
= wMenuCreate(scr
, title
, True
);
1233 menu
->on_destroy
= removeShortcutsForMenu
;
1234 if (!parseCascade(scr
, menu
, file
, file_name
)) {
1235 wMenuDestroy(menu
, True
);
1239 wwarning(_("%s:invalid menu file. MENU command is missing"),
1247 if (pclose(file
)==-1) {
1248 wsyserror(_("error reading preprocessed menu data"));
1260 /************ Menu Configuration From Pipe *************/
1263 readMenuPipe(WScreen
*scr
, char **file_name
)
1267 char linebuf
[MAXLINE
];
1268 char title
[MAXLINE
];
1269 char command
[MAXLINE
];
1270 char params
[MAXLINE
];
1271 char shortcut
[MAXLINE
];
1274 char flat_file
[MAXLINE
];
1281 flat_file
[0] = '\0';
1283 for(i
=0; file_name
[i
]!=NULL
; i
++) {
1284 strcat(flat_file
, file_name
[i
]);
1285 strcat(flat_file
, " ");
1287 filename
= flat_file
+ (flat_file
[1]=='|'?2:1);
1291 if (!wPreferences
.flags
.nocpp
) {
1292 args
= MakeCPPArgs(filename
);
1294 wwarning(_("could not make arguments for menu file preprocessor"));
1296 snprintf(command
, sizeof(command
), "%s | %s %s",
1297 filename
, CPP_PATH
, args
);
1300 file
= popen(command
, "r");
1302 wsyserror(_("%s:could not open/preprocess menu file"), filename
);
1312 file
= popen(filename
, "rb");
1315 wsyserror(_("%s:could not open menu file"), filename
);
1320 while (!IsEof(file
)) {
1321 if (!fgets(linebuf
, MAXLINE
, file
))
1323 line
= cropline(linebuf
);
1324 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1327 separateline(line
, title
, command
, params
, shortcut
);
1330 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1334 if (strcasecmp(command
, "MENU")==0) {
1335 menu
= wMenuCreate(scr
, title
, True
);
1336 menu
->on_destroy
= removeShortcutsForMenu
;
1337 if (!parseCascade(scr
, menu
, file
, filename
)) {
1338 wMenuDestroy(menu
, True
);
1342 wwarning(_("%s:no title given for the root menu"), filename
);
1361 myCompare(const void *d1
, const void *d2
)
1363 dir_data
*p1
= *(dir_data
**)d1
;
1364 dir_data
*p2
= *(dir_data
**)d2
;
1366 return strcmp(p1
->name
, p2
->name
);
1370 /************ Menu Configuration From Directory *************/
1373 isFilePackage(char *file
)
1377 /* check if the extension indicates this file is a
1378 * file package. For now, only recognize .themed */
1382 if (l
> 7 && strcmp(&(file
[l
-7]), ".themed")==0) {
1391 readMenuDirectory(WScreen
*scr
, char *title
, char **path
, char *command
)
1394 struct dirent
*dentry
;
1395 struct stat stat_buf
;
1398 WMArray
*dirs
= NULL
, *files
= NULL
;
1399 WMArrayIterator iter
;
1400 int length
, i
, have_space
=0;
1402 int stripExtension
= 0;
1405 dirs
= WMCreateArray(16);
1406 files
= WMCreateArray(16);
1409 while (path
[i
]!=NULL
) {
1410 if (strcmp(path
[i
], "-noext")==0) {
1416 dir
= opendir(path
[i
]);
1422 while ((dentry
= readdir(dir
))) {
1424 if (strcmp(dentry
->d_name
, ".")==0 ||
1425 strcmp(dentry
->d_name
, "..")==0)
1428 if (dentry
->d_name
[0] == '.')
1431 buffer
= malloc(strlen(path
[i
])+strlen(dentry
->d_name
)+4);
1433 wsyserror(_("out of memory while constructing directory menu %s"),
1438 strcpy(buffer
, path
[i
]);
1439 strcat(buffer
, "/");
1440 strcat(buffer
, dentry
->d_name
);
1442 if (stat(buffer
, &stat_buf
)!=0) {
1443 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1444 path
[i
], dentry
->d_name
);
1446 Bool isFilePack
= False
;
1449 if (S_ISDIR(stat_buf
.st_mode
)
1450 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1452 /* access always returns success for user root */
1453 if (access(buffer
, X_OK
)==0) {
1454 /* Directory is accesible. Add to directory list */
1456 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1457 data
->name
= wstrdup(dentry
->d_name
);
1460 WMAddToArray(dirs
, data
);
1462 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1463 /* Hack because access always returns X_OK success for user root */
1464 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1465 if ((command
!=NULL
&& access(buffer
, R_OK
)==0) ||
1466 (command
==NULL
&& access(buffer
, X_OK
)==0 &&
1467 (stat_buf
.st_mode
& S_IXANY
))) {
1469 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1470 data
->name
= wstrdup(dentry
->d_name
);
1473 WMAddToArray(files
, data
);
1484 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1490 WMSortArray(dirs
, myCompare
);
1491 WMSortArray(files
, myCompare
);
1493 menu
= wMenuCreate(scr
, title
, False
);
1494 menu
->on_destroy
= removeShortcutsForMenu
;
1496 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1497 /* New directory. Use same OPEN_MENU command that was used
1498 * for the current directory. */
1499 length
= strlen(path
[data
->index
])+strlen(data
->name
)+6;
1503 length
+= strlen(command
) + 6;
1504 buffer
= malloc(length
);
1506 wsyserror(_("out of memory while constructing directory menu %s"),
1513 strcat(buffer
, "-noext ");
1515 have_space
= strchr(path
[data
->index
], ' ')!=NULL
||
1516 strchr(data
->name
, ' ')!=NULL
;
1519 strcat(buffer
, "\"");
1520 strcat(buffer
, path
[data
->index
]);
1522 strcat(buffer
, "/");
1523 strcat(buffer
, data
->name
);
1525 strcat(buffer
, "\"");
1527 strcat(buffer
, " WITH ");
1528 strcat(buffer
, command
);
1531 addMenuEntry(menu
, data
->name
, NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1539 WM_ITERATE_ARRAY(files
, data
, iter
) {
1540 /* executable: add as entry */
1541 length
= strlen(path
[data
->index
])+strlen(data
->name
)+6;
1543 length
+= strlen(command
);
1545 buffer
= malloc(length
);
1547 wsyserror(_("out of memory while constructing directory menu %s"),
1552 have_space
= strchr(path
[data
->index
], ' ')!=NULL
||
1553 strchr(data
->name
, ' ')!=NULL
;
1554 if (command
!=NULL
) {
1555 strcpy(buffer
, command
);
1556 strcat(buffer
, " ");
1558 strcat(buffer
, "\"");
1559 strcat(buffer
, path
[data
->index
]);
1564 strcat(buffer
, path
[data
->index
]);
1566 strcpy(buffer
, path
[data
->index
]);
1569 strcat(buffer
, "/");
1570 strcat(buffer
, data
->name
);
1572 strcat(buffer
, "\"");
1574 if (stripExtension
) {
1575 char *ptr
= strrchr(data
->name
, '.');
1576 if (ptr
&& ptr
!=data
->name
)
1579 addMenuEntry(menu
, data
->name
, NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1594 /************ Menu Configuration From WMRootMenu *************/
1597 makeDefaultMenu(WScreen
*scr
)
1601 menu
= wMenuCreate(scr
, _("Commands"), True
);
1602 wMenuAddCallback(menu
, "XTerm", execCommand
, "xterm");
1603 wMenuAddCallback(menu
, "rxvt", execCommand
, "rxvt");
1604 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1605 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1614 *----------------------------------------------------------------------
1616 * Reads root menu configuration from defaults database.
1618 *----------------------------------------------------------------------
1621 configureMenu(WScreen
*scr
, WMPropList
*definition
)
1626 WMPropList
*title
, *command
, *params
;
1630 if (WMIsPLString(definition
)) {
1631 struct stat stat_buf
;
1633 Bool menu_is_default
= False
;
1635 /* menu definition is a string. Probably a path, so parse the file */
1637 tmp
= wexpandpath(WMGetFromPLString(definition
));
1639 path
= getLocalizedMenuFile(tmp
);
1642 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1645 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1646 menu_is_default
= True
;
1650 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"),
1656 if (stat(path
, &stat_buf
)<0) {
1657 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1663 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1664 /* if the pointer in WMRootMenu has changed */
1665 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1667 if (menu_is_default
) {
1668 wwarning(_("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1672 menu
= readMenuFile(scr
, path
);
1674 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1684 count
= WMGetPropListItemCount(definition
);
1688 elem
= WMGetFromPLArray(definition
, 0);
1689 if (!WMIsPLString(elem
)) {
1690 tmp
= WMGetPropListDescription(elem
, False
);
1691 wwarning(_("%s:format error in root menu configuration \"%s\""),
1696 mtitle
= WMGetFromPLString(elem
);
1698 menu
= wMenuCreate(scr
, mtitle
, False
);
1699 menu
->on_destroy
= removeShortcutsForMenu
;
1701 #ifdef GLOBAL_SUBMENU_FILE
1706 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1709 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1710 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1715 for (i
=1; i
<count
; i
++) {
1716 elem
= WMGetFromPLArray(definition
, i
);
1718 if (WMIsPLString(elem
)) {
1721 file
= WMGetFromPLString(elem
);
1725 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1728 if (WMIsPLArray(WMGetFromPLArray(elem
,1))) {
1733 submenu
= configureMenu(scr
, elem
);
1735 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
,
1737 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1741 WMPropList
*shortcut
;
1744 title
= WMGetFromPLArray(elem
, idx
++);
1745 shortcut
= WMGetFromPLArray(elem
, idx
++);
1746 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT")==0) {
1747 shortcut
= WMGetFromPLArray(elem
, idx
++);
1748 command
= WMGetFromPLArray(elem
, idx
++);
1753 params
= WMGetFromPLArray(elem
, idx
++);
1755 if (!title
|| !command
)
1758 addMenuEntry(menu
, WMGetFromPLString(title
),
1759 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1760 WMGetFromPLString(command
),
1761 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1766 tmp
= WMGetPropListDescription(elem
, False
);
1767 wwarning(_("%s:format error in root menu configuration \"%s\""),
1783 *----------------------------------------------------------------------
1785 * Opens the root menu, parsing the menu configuration from the
1786 * defaults database.
1787 * If the menu is already mapped and is not sticked to the
1788 * root window, it will be unmapped.
1791 * The menu may be remade.
1794 * Construction of OPEN_MENU entries are delayed to the moment the
1796 *----------------------------------------------------------------------
1799 OpenRootMenu(WScreen
*scr
, int x
, int y
, int keyboard
)
1802 WMPropList
*definition
;
1804 static WMPropList *domain=NULL;
1807 domain = WMCreatePLString("WMRootMenu");
1811 scr
->flags
.root_menu_changed_shortcuts
= 0;
1812 scr
->flags
.added_workspace_menu
= 0;
1813 scr
->flags
.added_windows_menu
= 0;
1815 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1816 menu
= scr
->root_menu
;
1817 if (!menu
->flags
.buttoned
) {
1820 wRaiseFrame(menu
->frame
->core
);
1823 wMenuMapAt(menu
, 0, 0, True
);
1825 wMenuMapCopyAt(menu
, x
-menu
->frame
->core
->width
/2, y
);
1831 definition
= WDRootMenu
->dictionary
;
1834 definition = PLGetDomain(domain);
1837 if (WMIsPLArray(definition
)) {
1839 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1840 menu
= configureMenu(scr
, definition
);
1842 menu
->timestamp
= WDRootMenu
->timestamp
;
1847 menu
= configureMenu(scr
, definition
);
1852 /* menu hasn't changed or could not be read */
1853 if (!scr
->root_menu
) {
1854 wMessageDialog(scr
, _("Error"),
1855 _("The applications menu could not be loaded. "
1856 "Look at the console output for a detailed "
1857 "description of the errors."),
1858 _("OK"), NULL
, NULL
);
1860 menu
= makeDefaultMenu(scr
);
1861 scr
->root_menu
= menu
;
1863 menu
= scr
->root_menu
;
1866 if (scr
->root_menu
) {
1867 wMenuDestroy(scr
->root_menu
, True
);
1869 scr
->root_menu
= menu
;
1874 if (keyboard
&& x
==0 && y
==0) {
1876 } else if (keyboard
&& x
==scr
->scr_width
/2 && y
==scr
->scr_height
/2) {
1877 newx
= x
- menu
->frame
->core
->width
/2;
1878 newy
= y
- menu
->frame
->core
->height
/2;
1880 newx
= x
- menu
->frame
->core
->width
/2;
1883 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1886 if (scr
->flags
.root_menu_changed_shortcuts
)
1887 rebindKeygrabs(scr
);