1 /* rootmenu.c- user defined menu
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
33 #include <sys/types.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xatom.h>
43 #include "WindowMaker.h"
50 #include "workspace.h"
54 #include "xmodifier.h"
61 extern WDDomain
*WDRootMenu
;
63 extern Cursor wCursor
[WCUR_LAST
];
65 extern Time LastTimestamp
;
67 extern WPreferences wPreferences
;
69 extern int wScreenCount
;
71 static WMenu
*readMenuPipe(WScreen
*scr
, char **file_name
);
72 static WMenu
*readMenuFile(WScreen
*scr
, char *file_name
);
73 static WMenu
*readMenuDirectory(WScreen
*scr
, char *title
, char **file_name
,
77 typedef struct Shortcut
{
78 struct Shortcut
*next
;
88 static Shortcut
*shortcutList
= NULL
;
95 * "Title" EXEC command_to_exec -params
97 * "Title" EXEC command_to_exec -params
99 * "Workspaces" WORKSPACE_MENU
100 * "Title" built_in_command
102 * "Quick Quit" EXIT QUICK
105 * Commands may be preceded by SHORTCUT key
109 * INFO_PANEL - shows the Info Panel
110 * LEGAL_PANEL - shows the Legal info panel
111 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
112 * REFRESH - forces the desktop to be repainted
113 * EXIT [QUICK] - exit the window manager [without confirmation]
114 * EXEC <program> - execute an external program
115 * SHEXEC <command> - execute a shell command
116 * WORKSPACE_MENU - places the workspace submenu
118 * RESTART [<window manager>] - restarts the window manager
119 * SHOW_ALL - unhide all windows on workspace
120 * HIDE_OTHERS - hides all windows excep the focused one
121 * OPEN_MENU file - read menu data from file which must be a valid menu file.
122 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
123 * - read menu data from directory(ies) and
124 * eventually precede each with a command.
125 * OPEN_MENU | command
126 * - opens command and uses its stdout to construct and insert
127 * the resulting menu in current position. The output of
128 * command must be a valid menu description.
129 * The space between '|' and command is optional.
130 * SAVE_SESSION - saves the current state of the desktop, which include
131 * all running applications, all their hints (geometry,
132 * position on screen, workspace they live on, the dock
133 * or clip from where they were launched, and
134 * if minimized, shaded or hidden. Also saves the current
135 * workspace the user is on. All will be restored on every
136 * start of windowmaker until another SAVE_SESSION or
137 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
138 * WindowMaker domain file, then saving is automatically
139 * done on every windowmaker exit, overwriting any
140 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
142 * CLEAR_SESSION - clears any previous saved session. This will not have
143 * any effect if SaveSessionOnExit is True.
147 #define MAX(a,b) ((a)>(b) ? (a) : (b))
155 execCommand(WMenu
*menu
, WMenuEntry
*entry
)
159 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char*)entry
->clientdata
);
161 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
162 GrabModeAsync
, GrabModeAsync
, None
, wCursor
[WCUR_WAIT
],
167 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
170 XUngrabPointer(dpy
, CurrentTime
);
176 exitCommand(WMenu
*menu
, WMenuEntry
*entry
)
178 static int inside
= 0;
180 /* prevent reentrant calls */
185 if ((long)entry
->clientdata
==M_QUICK
186 || wMessageDialog(menu
->frame
->screen_ptr
, _("Exit"),
187 _("Exit window manager?"),
188 _("Exit"), _("Cancel"), NULL
)==WAPRDefault
) {
190 printf("Exiting WindowMaker.\n");
192 Shutdown(WSExitMode
);
199 shutdownCommand(WMenu
*menu
, WMenuEntry
*entry
)
201 static int inside
= 0;
204 /* prevent reentrant calls */
215 if ((long)entry
->clientdata
==M_QUICK
)
219 if (wSessionIsManaged()) {
222 r
= wMessageDialog(menu
->frame
->screen_ptr
,
223 _("Close X session"),
224 _("Close Window System session?\n"
225 "Kill might close applications with unsaved data."),
226 _("Close"), _("Kill"), _("Cancel"));
229 else if (r
==WAPRAlternate
)
236 r
= wMessageDialog(menu
->frame
->screen_ptr
,
238 _("Kill Window System session?\n"
239 "(all applications will be closed)"),
240 _("Kill"), _("Cancel"), NULL
);
246 if (result
!=R_CANCEL
) {
248 if (result
== R_CLOSE
) {
249 Shutdown(WSLogoutMode
);
251 #endif /* XSMP_ENABLED */
253 Shutdown(WSKillMode
);
264 restartCommand(WMenu
*menu
, WMenuEntry
*entry
)
266 Shutdown(WSRestartPreparationMode
);
267 Restart((char*)entry
->clientdata
, False
);
273 refreshCommand(WMenu
*menu
, WMenuEntry
*entry
)
275 wRefreshDesktop(menu
->frame
->screen_ptr
);
280 arrangeIconsCommand(WMenu
*menu
, WMenuEntry
*entry
)
282 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
286 showAllCommand(WMenu
*menu
, WMenuEntry
*entry
)
288 wShowAllWindows(menu
->frame
->screen_ptr
);
292 hideOthersCommand(WMenu
*menu
, WMenuEntry
*entry
)
294 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
299 saveSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
301 if (!wPreferences
.save_session_on_exit
)
302 wSessionSaveState(menu
->frame
->screen_ptr
);
304 wScreenSaveState(menu
->frame
->screen_ptr
);
309 clearSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
311 wSessionClearState(menu
->frame
->screen_ptr
);
316 infoPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
318 wShowInfoPanel(menu
->frame
->screen_ptr
);
323 legalPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
325 wShowLegalPanel(menu
->frame
->screen_ptr
);
329 /********************************************************************/
333 getLocalizedMenuFile(char *menu
)
341 buffer
= wmalloc(strlen(menu
)+32);
343 /* try menu.locale_name */
344 sprintf(buffer
, "%s.%s", menu
, Locale
);
345 if (access(buffer
, F_OK
)==0) {
348 /* check if it is in the form aa_bb.encoding and check for aa_bb */
349 ptr
= strchr(Locale
, '.');
352 if (access(buffer
, F_OK
)==0) {
356 /* now check for aa */
357 ptr
= strchr(buffer
, '_');
360 if (access(buffer
, F_OK
)==0) {
370 raiseMenus(WMenu
*menu
)
374 if (menu
->flags
.mapped
) {
375 wRaiseFrame(menu
->frame
->core
);
377 for (i
=0; i
<menu
->cascade_no
; i
++) {
378 if (menu
->cascades
[i
])
379 raiseMenus(menu
->cascades
[i
]);
386 wRootMenuPerformShortcut(XEvent
*event
)
392 /* ignore CapsLock */
393 modifiers
= event
->xkey
.state
& ValidModMask
;
395 for (ptr
= shortcutList
; ptr
!=NULL
; ptr
= ptr
->next
) {
399 if (ptr
->keycode
==event
->xkey
.keycode
&& (ptr
->modifier
==modifiers
)) {
400 (*ptr
->entry
->callback
)(ptr
->menu
, ptr
->entry
);
409 wRootMenuBindShortcuts(Window window
)
415 if (ptr
->modifier
!=AnyModifier
) {
416 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
|LockMask
,
417 window
, True
, GrabModeAsync
, GrabModeAsync
);
419 wHackedGrabKey(ptr
->keycode
, ptr
->modifier
,
420 window
, True
, GrabModeAsync
, GrabModeAsync
);
423 XGrabKey(dpy
, ptr
->keycode
, ptr
->modifier
, window
, True
,
424 GrabModeAsync
, GrabModeAsync
);
431 rebindKeygrabs(WScreen
*scr
)
435 wwin
= scr
->focused_window
;
438 XUngrabKey(dpy
, AnyKey
, AnyModifier
, wwin
->frame
->core
->window
);
440 if (!WFLAGP(wwin
, no_bind_keys
)) {
441 wWindowSetKeyGrabs(wwin
);
449 removeShortcutsForMenu(WMenu
*menu
)
452 Shortcut
*newList
= NULL
;
457 if (ptr
->menu
== menu
) {
465 shortcutList
= newList
;
466 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
471 addShortcut(char *file
, char *shortcutDefinition
, WMenu
*menu
,
479 ptr
= wmalloc(sizeof(Shortcut
));
481 strcpy(buf
, shortcutDefinition
);
486 while ((k
= strchr(b
, '+'))!=NULL
) {
490 mod
= wXModifierFromKey(b
);
492 wwarning(_("%s:invalid key modifier \"%s\""), file
, b
);
496 ptr
->modifier
|= mod
;
502 ksym
= XStringToKeysym(b
);
504 if (ksym
==NoSymbol
) {
505 wwarning(_("%s:invalid kbd shortcut specification \"%s\" for entry %s"),
506 file
, shortcutDefinition
, entry
->text
);
511 ptr
->keycode
= XKeysymToKeycode(dpy
, ksym
);
512 if (ptr
->keycode
==0) {
513 wwarning(_("%s:invalid key in shortcut \"%s\" for entry %s"), file
,
514 shortcutDefinition
, entry
->text
);
522 ptr
->next
= shortcutList
;
525 menu
->menu
->screen_ptr
->flags
.root_menu_changed_shortcuts
= 1;
531 /*******************************/
541 end
= &(line
[strlen(line
)])-1;
542 while (isspace(*line
) && *line
!=0) line
++;
543 while (end
>line
&& isspace(*end
)) {
552 next_token(char *line
, char **next
)
558 while (*line
==' ' || *line
=='\t') line
++;
564 while (*tmp
!=0 && *tmp
!='"') tmp
++;
566 wwarning(_("%s: unmatched '\"' in menu file"), line
);
577 } while (*tmp
!=0 && *tmp
!=' ' && *tmp
!='\t');
591 while (*tmp
==' ' || *tmp
=='\t') tmp
++;
601 separateCommand(char *line
, char ***file
, char **command
)
603 char *token
, *tmp
= line
;
604 WMArray
*array
= WMCreateArray(4);
610 token
= next_token(tmp
, &tmp
);
612 if (strcmp(token
, "WITH")==0) {
613 if (tmp
!=NULL
&& *tmp
!=0)
614 *command
= wstrdup(tmp
);
616 wwarning(_("%s: missing command"), line
);
619 WMAddToArray(array
, token
);
621 } while (token
!=NULL
&& tmp
!=NULL
);
623 count
= WMGetArrayItemCount(array
);
625 *file
= wmalloc(sizeof(char*)*(count
+1));
626 (*file
)[count
] = NULL
;
627 for (i
= 0; i
< count
; i
++) {
628 (*file
)[i
] = WMGetFromArray(array
, i
);
636 constructMenu(WMenu
*menu
, WMenuEntry
*entry
)
639 struct stat stat_buf
;
646 separateCommand((char*)entry
->clientdata
, &path
, &cmd
);
647 if (path
== NULL
|| *path
==NULL
|| **path
==0) {
648 wwarning(_("invalid OPEN_MENU specification: %s"),
649 (char*)entry
->clientdata
);
653 if (path
[0][0]=='|') {
656 if (!menu
->cascades
[entry
->cascade
] ||
657 menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
660 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
662 /* there's no automatic reloading */
664 submenu
->timestamp
= 1;
671 while(path
[i
] != NULL
) {
674 if (strcmp(path
[i
], "-noext")==0) {
679 tmp
= wexpandpath(path
[i
]);
681 lpath
= getLocalizedMenuFile(tmp
);
690 if (stat(path
[i
], &stat_buf
)==0) {
691 if (last
< stat_buf
.st_mtime
)
692 last
= stat_buf
.st_mtime
;
696 wsyserror(_("%s:could not stat menu"), path
[i
]);
704 wsyserror(_("%s:could not stat menu:%s"), "OPEN_MENU",
705 (char*)entry
->clientdata
);
708 stat(path
[first
], &stat_buf
);
709 if (!menu
->cascades
[entry
->cascade
]
710 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
712 if (S_ISDIR(stat_buf
.st_mode
)) {
714 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
,
715 entry
->text
, path
, cmd
);
717 submenu
->timestamp
= last
;
718 } else if (S_ISREG(stat_buf
.st_mode
)) {
722 wwarning(_("too many parameters in OPEN_MENU: %s"),
723 (char*)entry
->clientdata
);
725 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
727 submenu
->timestamp
= stat_buf
.st_mtime
;
737 wMenuEntryRemoveCascade(menu
, entry
);
738 wMenuEntrySetCascade(menu
, entry
, submenu
);
743 while (path
[i
]!=NULL
)
752 addWorkspaceMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
757 if (scr
->flags
.added_workspace_menu
) {
758 wwarning(_("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
761 scr
->flags
.added_workspace_menu
= 1;
763 wsmenu
= wWorkspaceMenuMake(scr
, True
);
764 scr
->workspace_menu
= wsmenu
;
765 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
766 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
768 wWorkspaceMenuUpdate(scr
, wsmenu
);
774 addWindowsMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
780 if (scr
->flags
.added_windows_menu
) {
781 wwarning(_("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
784 scr
->flags
.added_windows_menu
= 1;
786 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
787 scr
->switch_menu
= wwmenu
;
788 wwin
= scr
->focused_window
;
790 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
794 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
795 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
801 addMenuEntry(WMenu
*menu
, char *title
, char *shortcut
, char *command
,
802 char *params
, char *file_name
)
805 WMenuEntry
*entry
= NULL
;
806 Bool shortcutOk
= False
;
810 scr
= menu
->frame
->screen_ptr
;
811 if (strcmp(command
, "OPEN_MENU")==0) {
813 wwarning(_("%s:missing parameter for menu command \"%s\""),
819 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
821 path
= wstrdup(params
);
823 dummy
= wMenuCreate(scr
, title
, False
);
824 dummy
->on_destroy
= removeShortcutsForMenu
;
825 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
826 entry
->free_cdata
= free
;
827 wMenuEntrySetCascade(menu
, entry
, dummy
);
829 } else if (strcmp(command
, "EXEC")==0) {
831 wwarning(_("%s:missing parameter for menu command \"%s\""),
834 entry
= wMenuAddCallback(menu
, title
, execCommand
,
835 wstrconcat("exec ", params
));
836 entry
->free_cdata
= free
;
839 } else if (strcmp(command
, "SHEXEC")==0) {
841 wwarning(_("%s:missing parameter for menu command \"%s\""),
844 entry
= wMenuAddCallback(menu
, title
, execCommand
,
846 entry
->free_cdata
= free
;
849 } else if (strcmp(command
, "EXIT")==0) {
851 if (params
&& strcmp(params
, "QUICK")==0)
852 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void*)M_QUICK
);
854 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
857 } else if (strcmp(command
, "SHUTDOWN")==0) {
859 if (params
&& strcmp(params
, "QUICK")==0)
860 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
,
863 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
866 } else if (strcmp(command
, "REFRESH")==0) {
867 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
870 } else if (strcmp(command
, "WORKSPACE_MENU")==0) {
871 entry
= addWorkspaceMenu(scr
, menu
, title
);
874 } else if (strcmp(command
, "WINDOWS_MENU")==0) {
875 entry
= addWindowsMenu(scr
, menu
, title
);
878 } else if (strcmp(command
, "ARRANGE_ICONS")==0) {
879 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
882 } else if (strcmp(command
, "HIDE_OTHERS")==0) {
883 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
886 } else if (strcmp(command
, "SHOW_ALL")==0) {
887 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
890 } else if (strcmp(command
, "RESTART")==0) {
891 entry
= wMenuAddCallback(menu
, title
, restartCommand
,
892 params
? wstrdup(params
) : NULL
);
893 entry
->free_cdata
= free
;
895 } else if (strcmp(command
, "SAVE_SESSION")==0) {
896 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
899 } else if (strcmp(command
, "CLEAR_SESSION")==0) {
900 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
902 } else if (strcmp(command
, "INFO_PANEL")==0) {
903 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
905 } else if (strcmp(command
, "LEGAL_PANEL")==0) {
906 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
909 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
,
915 if (shortcut
&& entry
) {
917 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
,
920 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
922 entry
->rtext
= GetShortcutString(shortcut
);
924 entry->rtext = wstrdup(shortcut);
935 /******************* Menu Configuration From File *******************/
938 separateline(char *line
, char *title
, char *command
, char *parameter
,
950 while (isspace(*line
) && (*line
!=0)) line
++;
954 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
959 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
961 strncpy(title
, line
, i
);
965 /* get the command or shortcut keyword */
966 while (isspace(*line
) && (*line
!=0)) line
++;
970 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
971 strncpy(command
, line
, i
);
975 if (strcmp(command
, "SHORTCUT")==0) {
976 /* get the shortcut key */
977 while (isspace(*line
) && (*line
!=0)) line
++;
981 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
986 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
988 strncpy(shortcut
, line
, i
);
994 /* get the command */
995 while (isspace(*line
) && (*line
!=0)) line
++;
999 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1000 strncpy(command
, line
, i
);
1005 /* get the parameters */
1006 while (isspace(*line
) && (*line
!=0)) line
++;
1013 while (line
[l
]!=0 && line
[l
]!='"') {
1014 parameter
[l
] = line
[l
];
1022 while (isspace(line
[l
]) && (l
>0)) l
--;
1023 strncpy(parameter
, line
, l
);
1029 parseCascade(WScreen
*scr
, WMenu
*menu
, FILE *file
, char *file_name
)
1031 char linebuf
[MAXLINE
];
1032 char elinebuf
[MAXLINE
];
1033 char title
[MAXLINE
];
1034 char command
[MAXLINE
];
1035 char shortcut
[MAXLINE
];
1036 char params
[MAXLINE
];
1039 while (!IsEof(file
)) {
1043 fgets(linebuf
, MAXLINE
, file
);
1044 line
= cropline(linebuf
);
1045 lsize
= strlen(line
);
1047 if (line
[lsize
-1]=='\\') {
1050 fgets(elinebuf
, MAXLINE
, file
);
1051 line2
=cropline(elinebuf
);
1052 lsize2
=strlen(line2
);
1053 if (lsize2
+lsize
>MAXLINE
) {
1054 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1060 strcat(line
, line2
);
1065 } while (!ok
&& !IsEof(file
));
1069 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1073 separateline(line
, title
, command
, params
, shortcut
);
1076 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1081 if (strcasecmp(command
, "MENU")==0) {
1086 cascade
= wMenuCreate(scr
, title
, False
);
1087 cascade
->on_destroy
= removeShortcutsForMenu
;
1088 if (parseCascade(scr
, cascade
, file
, file_name
)==NULL
) {
1089 wMenuDestroy(cascade
, True
);
1091 wMenuEntrySetCascade(menu
,
1092 wMenuAddCallback(menu
, title
, NULL
, NULL
),
1095 } else if (strcasecmp(command
, "END")==0) {
1101 addMenuEntry(menu
, title
, shortcut
[0] ? shortcut
: NULL
, command
,
1102 params
[0] ? params
: NULL
, file_name
);
1106 wwarning(_("%s:syntax error in menu file:END declaration missing"),
1116 readMenuFile(WScreen
*scr
, char *file_name
)
1120 char linebuf
[MAXLINE
];
1121 char title
[MAXLINE
];
1122 char shortcut
[MAXLINE
];
1123 char command
[MAXLINE
];
1124 char params
[MAXLINE
];
1132 if (!wPreferences
.flags
.nocpp
) {
1133 args
= MakeCPPArgs(file_name
);
1135 wwarning(_("could not make arguments for menu file preprocessor"));
1137 sprintf(command
, "%s %s %s", CPP_PATH
, args
, file_name
);
1139 file
= popen(command
, "r");
1141 wsyserror(_("%s:could not open/preprocess menu file"),
1151 file
= fopen(file_name
, "r");
1153 wsyserror(_("%s:could not open menu file"), file_name
);
1158 while (!IsEof(file
)) {
1159 if (!fgets(linebuf
, MAXLINE
, file
))
1161 line
= cropline(linebuf
);
1162 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1165 separateline(line
, title
, command
, params
, shortcut
);
1168 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1172 if (strcasecmp(command
, "MENU")==0) {
1173 menu
= wMenuCreate(scr
, title
, True
);
1174 menu
->on_destroy
= removeShortcutsForMenu
;
1175 if (!parseCascade(scr
, menu
, file
, file_name
)) {
1176 wMenuDestroy(menu
, True
);
1180 wwarning(_("%s:invalid menu file. MENU command is missing"),
1188 if (pclose(file
)==-1) {
1189 wsyserror(_("error reading preprocessed menu data"));
1201 /************ Menu Configuration From Pipe *************/
1204 readMenuPipe(WScreen
*scr
, char **file_name
)
1208 char linebuf
[MAXLINE
];
1209 char title
[MAXLINE
];
1210 char command
[MAXLINE
];
1211 char params
[MAXLINE
];
1212 char shortcut
[MAXLINE
];
1215 char flat_file
[MAXLINE
];
1222 flat_file
[0] = '\0';
1224 for(i
= 0 ; file_name
[i
] != NULL
; i
++) {
1225 strcat(flat_file
, file_name
[i
]);
1226 strcat(flat_file
, " ");
1228 filename
= flat_file
+1;
1231 if (!wPreferences
.flags
.nocpp
) {
1232 args
= MakeCPPArgs(filename
);
1234 wwarning(_("could not make arguments for menu file preprocessor"));
1236 sprintf(command
, "%s | %s %s", filename
, CPP_PATH
, args
);
1239 file
= popen(command
, "r");
1241 wsyserror(_("%s:could not open/preprocess menu file"), filename
);
1251 file
= popen(filename
, "r");
1254 wsyserror(_("%s:could not open menu file"), filename
);
1259 while (!IsEof(file
)) {
1260 if (!fgets(linebuf
, MAXLINE
, file
))
1262 line
= cropline(linebuf
);
1263 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1266 separateline(line
, title
, command
, params
, shortcut
);
1269 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1273 if (strcasecmp(command
, "MENU")==0) {
1274 menu
= wMenuCreate(scr
, title
, True
);
1275 menu
->on_destroy
= removeShortcutsForMenu
;
1276 if (!parseCascade(scr
, menu
, file
, filename
)) {
1277 wMenuDestroy(menu
, True
);
1281 wwarning(_("%s:no title given for the root menu"), filename
);
1300 myCompare(const void *d1
, const void *d2
)
1302 dir_data
*p1
= *(dir_data
**)d1
;
1303 dir_data
*p2
= *(dir_data
**)d2
;
1305 return strcmp(p1
->name
, p2
->name
);
1309 /************ Menu Configuration From Directory *************/
1312 isFilePackage(char *file
)
1316 /* check if the extension indicates this file is a
1317 * file package. For now, only recognize .themed */
1321 if (l
> 7 && strcmp(&(file
[l
-7]), ".themed")==0) {
1330 readMenuDirectory(WScreen
*scr
, char *title
, char **path
, char *command
)
1333 struct dirent
*dentry
;
1334 struct stat stat_buf
;
1337 WMArray
*dirs
= NULL
, *files
= NULL
;
1338 WMArrayIterator iter
;
1339 int length
, i
, have_space
=0;
1341 int stripExtension
= 0;
1344 dirs
= WMCreateArray(16);
1345 files
= WMCreateArray(16);
1348 while (path
[i
]!=NULL
) {
1349 if (strcmp(path
[i
], "-noext")==0) {
1355 dir
= opendir(path
[i
]);
1361 while ((dentry
= readdir(dir
))) {
1363 if (strcmp(dentry
->d_name
, ".")==0 ||
1364 strcmp(dentry
->d_name
, "..")==0)
1367 if (dentry
->d_name
[0] == '.')
1370 buffer
= malloc(strlen(path
[i
])+strlen(dentry
->d_name
)+4);
1372 wsyserror(_("out of memory while constructing directory menu %s"),
1377 strcpy(buffer
, path
[i
]);
1378 strcat(buffer
, "/");
1379 strcat(buffer
, dentry
->d_name
);
1381 if (stat(buffer
, &stat_buf
)!=0) {
1382 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1383 path
[i
], dentry
->d_name
);
1385 Bool isFilePack
= False
;
1388 if (S_ISDIR(stat_buf
.st_mode
)
1389 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1391 /* access always returns success for user root */
1392 if (access(buffer
, X_OK
)==0) {
1393 /* Directory is accesible. Add to directory list */
1395 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1396 data
->name
= wstrdup(dentry
->d_name
);
1399 WMAddToArray(dirs
, data
);
1401 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1402 /* Hack because access always returns X_OK success for user root */
1403 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1404 if ((command
!=NULL
&& access(buffer
, R_OK
)==0) ||
1405 (command
==NULL
&& access(buffer
, X_OK
)==0 &&
1406 (stat_buf
.st_mode
& S_IXANY
))) {
1408 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1409 data
->name
= wstrdup(dentry
->d_name
);
1412 WMAddToArray(files
, data
);
1423 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1429 WMSortArray(dirs
, myCompare
);
1430 WMSortArray(files
, myCompare
);
1432 menu
= wMenuCreate(scr
, title
, False
);
1433 menu
->on_destroy
= removeShortcutsForMenu
;
1435 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1436 /* New directory. Use same OPEN_MENU command that was used
1437 * for the current directory. */
1438 length
= strlen(path
[data
->index
])+strlen(data
->name
)+6;
1442 length
+= strlen(command
) + 6;
1443 buffer
= malloc(length
);
1445 wsyserror(_("out of memory while constructing directory menu %s"),
1452 strcat(buffer
, "-noext ");
1454 have_space
= strchr(path
[data
->index
], ' ')!=NULL
||
1455 strchr(data
->name
, ' ')!=NULL
;
1458 strcat(buffer
, "\"");
1459 strcat(buffer
, path
[data
->index
]);
1461 strcat(buffer
, "/");
1462 strcat(buffer
, data
->name
);
1464 strcat(buffer
, "\"");
1466 strcat(buffer
, " WITH ");
1467 strcat(buffer
, command
);
1470 addMenuEntry(menu
, data
->name
, NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1478 WM_ITERATE_ARRAY(files
, data
, iter
) {
1479 /* executable: add as entry */
1480 length
= strlen(path
[data
->index
])+strlen(data
->name
)+6;
1482 length
+= strlen(command
);
1484 buffer
= malloc(length
);
1486 wsyserror(_("out of memory while constructing directory menu %s"),
1491 have_space
= strchr(path
[data
->index
], ' ')!=NULL
||
1492 strchr(data
->name
, ' ')!=NULL
;
1493 if (command
!=NULL
) {
1494 strcpy(buffer
, command
);
1495 strcat(buffer
, " ");
1497 strcat(buffer
, "\"");
1498 strcat(buffer
, path
[data
->index
]);
1503 strcat(buffer
, path
[data
->index
]);
1505 strcpy(buffer
, path
[data
->index
]);
1508 strcat(buffer
, "/");
1509 strcat(buffer
, data
->name
);
1511 strcat(buffer
, "\"");
1513 if (stripExtension
) {
1514 char *ptr
= strrchr(data
->name
, '.');
1515 if (ptr
&& ptr
!=data
->name
)
1518 addMenuEntry(menu
, data
->name
, NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1533 /************ Menu Configuration From WMRootMenu *************/
1536 makeDefaultMenu(WScreen
*scr
)
1540 menu
= wMenuCreate(scr
, _("Commands"), True
);
1541 wMenuAddCallback(menu
, "XTerm", execCommand
, "xterm");
1542 wMenuAddCallback(menu
, "rxvt", execCommand
, "rxvt");
1543 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1544 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1553 *----------------------------------------------------------------------
1555 * Reads root menu configuration from defaults database.
1557 *----------------------------------------------------------------------
1560 configureMenu(WScreen
*scr
, proplist_t definition
)
1565 proplist_t title
, command
, params
;
1569 if (PLIsString(definition
)) {
1570 struct stat stat_buf
;
1572 Bool menu_is_default
= False
;
1574 /* menu definition is a string. Probably a path, so parse the file */
1576 tmp
= wexpandpath(PLGetString(definition
));
1578 path
= getLocalizedMenuFile(tmp
);
1581 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1584 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1585 menu_is_default
= True
;
1589 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"),
1595 if (stat(path
, &stat_buf
)<0) {
1596 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1602 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1603 /* if the pointer in WMRootMenu has changed */
1604 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1606 if (menu_is_default
) {
1607 wwarning(_("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1611 menu
= readMenuFile(scr
, path
);
1613 menu
->timestamp
= MAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1623 count
= PLGetNumberOfElements(definition
);
1627 elem
= PLGetArrayElement(definition
, 0);
1628 if (!PLIsString(elem
)) {
1629 tmp
= PLGetDescription(elem
);
1630 wwarning(_("%s:format error in root menu configuration \"%s\""),
1635 mtitle
= PLGetString(elem
);
1637 menu
= wMenuCreate(scr
, mtitle
, False
);
1638 menu
->on_destroy
= removeShortcutsForMenu
;
1640 #ifdef GLOBAL_SUBMENU_FILE
1645 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1648 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1649 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1654 for (i
=1; i
<count
; i
++) {
1655 elem
= PLGetArrayElement(definition
, i
);
1657 if (PLIsString(elem
)) {
1660 file
= PLGetString(elem
);
1664 if (!PLIsArray(elem
) || PLGetNumberOfElements(elem
) < 2)
1667 if (PLIsArray(PLGetArrayElement(elem
,1))) {
1672 submenu
= configureMenu(scr
, elem
);
1674 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
,
1676 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1683 title
= PLGetArrayElement(elem
, idx
++);
1684 shortcut
= PLGetArrayElement(elem
, idx
++);
1685 if (strcmp(PLGetString(shortcut
), "SHORTCUT")==0) {
1686 shortcut
= PLGetArrayElement(elem
, idx
++);
1687 command
= PLGetArrayElement(elem
, idx
++);
1692 params
= PLGetArrayElement(elem
, idx
++);
1694 if (!title
|| !command
)
1697 addMenuEntry(menu
, PLGetString(title
),
1698 shortcut
? PLGetString(shortcut
) : NULL
,
1699 PLGetString(command
),
1700 params
? PLGetString(params
) : NULL
, "WMRootMenu");
1705 tmp
= PLGetDescription(elem
);
1706 wwarning(_("%s:format error in root menu configuration \"%s\""),
1722 *----------------------------------------------------------------------
1724 * Opens the root menu, parsing the menu configuration from the
1725 * defaults database.
1726 * If the menu is already mapped and is not sticked to the
1727 * root window, it will be unmapped.
1730 * The menu may be remade.
1733 * Construction of OPEN_MENU entries are delayed to the moment the
1735 *----------------------------------------------------------------------
1738 OpenRootMenu(WScreen
*scr
, int x
, int y
, int keyboard
)
1741 proplist_t definition
;
1743 static proplist_t domain=NULL;
1746 domain = PLMakeString("WMRootMenu");
1750 scr
->flags
.root_menu_changed_shortcuts
= 0;
1751 scr
->flags
.added_workspace_menu
= 0;
1753 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1754 menu
= scr
->root_menu
;
1755 if (!menu
->flags
.buttoned
) {
1758 wRaiseFrame(menu
->frame
->core
);
1761 wMenuMapAt(menu
, 0, 0, True
);
1763 wMenuMapCopyAt(menu
, x
-menu
->frame
->core
->width
/2, y
);
1769 definition
= WDRootMenu
->dictionary
;
1772 definition = PLGetDomain(domain);
1775 if (PLIsArray(definition
)) {
1777 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1778 menu
= configureMenu(scr
, definition
);
1780 menu
->timestamp
= WDRootMenu
->timestamp
;
1785 menu
= configureMenu(scr
, definition
);
1790 /* menu hasn't changed or could not be read */
1791 if (!scr
->root_menu
) {
1792 wMessageDialog(scr
, _("Error"),
1793 _("The applications menu could not be loaded. "
1794 "Look at the console output for a detailed "
1795 "description of the errors."),
1796 _("OK"), NULL
, NULL
);
1798 menu
= makeDefaultMenu(scr
);
1799 scr
->root_menu
= menu
;
1801 menu
= scr
->root_menu
;
1805 wMenuDestroy(scr
->root_menu
, True
);
1806 scr
->root_menu
= menu
;
1809 wMenuMapAt(menu
, x
-menu
->frame
->core
->width
/2, y
, keyboard
);
1812 if (scr
->flags
.root_menu_changed_shortcuts
)
1813 rebindKeygrabs(scr
);