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,
34 #include <sys/types.h>
41 #include <X11/Xutil.h>
42 #include <X11/Xatom.h>
44 #include "WindowMaker.h"
51 #include "workspace.h"
55 #include "xmodifier.h"
63 extern WDDomain
*WDRootMenu
;
65 extern Cursor wCursor
[WCUR_LAST
];
67 extern Time LastTimestamp
;
69 extern WPreferences wPreferences
;
71 extern int wScreenCount
;
73 static WMenu
*readMenuPipe(WScreen
*scr
, char **file_name
);
74 static WMenu
*readMenuFile(WScreen
*scr
, char *file_name
);
75 static WMenu
*readMenuDirectory(WScreen
*scr
, char *title
, char **file_name
,
79 typedef struct Shortcut
{
80 struct Shortcut
*next
;
90 static Shortcut
*shortcutList
= NULL
;
97 * "Title" EXEC command_to_exec -params
99 * "Title" EXEC command_to_exec -params
101 * "Workspaces" WORKSPACE_MENU
102 * "Title" built_in_command
104 * "Quick Quit" EXIT QUICK
107 * Commands may be preceded by SHORTCUT key
111 * INFO_PANEL - shows the Info Panel
112 * LEGAL_PANEL - shows the Legal info panel
113 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
114 * REFRESH - forces the desktop to be repainted
115 * EXIT [QUICK] - exit the window manager [without confirmation]
116 * EXEC <program> - execute an external program
117 * WORKSPACE_MENU - places the workspace submenu
119 * RESTART [<window manager>] - restarts the window manager
120 * SHOW_ALL - unhide all windows on workspace
121 * HIDE_OTHERS - hides all windows excep the focused one
122 * OPEN_MENU file - read menu data from file which must be a valid menu file.
123 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
124 * - read menu data from directory(ies) and
125 * eventually precede each with a command.
126 * OPEN_MENU | command
127 * - opens command and uses its stdout to construct and insert
128 * the resulting menu in current position. The output of
129 * command must be a valid menu description.
130 * The space between '|' and command is optional.
131 * SAVE_SESSION - saves the current state of the desktop, which include
132 * all running applications, all their hints (geometry,
133 * position on screen, workspace they live on, the dock
134 * or clip from where they were launched, and
135 * if minimized, shaded or hidden. Also saves the current
136 * workspace the user is on. All will be restored on every
137 * start of windowmaker until another SAVE_SESSION or
138 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
139 * WindowMaker domain file, then saving is automatically
140 * done on every windowmaker exit, overwriting any
141 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
143 * CLEAR_SESSION - clears any previous saved session. This will not have
144 * any effect if SaveSessionOnExit is True.
148 #define MAX(a,b) ((a)>(b) ? (a) : (b))
156 execCommand(WMenu
*menu
, WMenuEntry
*entry
)
160 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char*)entry
->clientdata
);
162 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
163 GrabModeAsync
, GrabModeAsync
, None
, wCursor
[WCUR_WAIT
],
168 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
171 XUngrabPointer(dpy
, CurrentTime
);
177 exitCommand(WMenu
*menu
, WMenuEntry
*entry
)
179 static int inside
= 0;
181 /* prevent reentrant calls */
186 if ((long)entry
->clientdata
==M_QUICK
187 || wMessageDialog(menu
->frame
->screen_ptr
, _("Exit"),
188 _("Exit window manager?"),
189 _("Exit"), _("Cancel"), NULL
)==WAPRDefault
) {
191 printf("Exiting WindowMaker.\n");
193 Shutdown(WSExitMode
);
200 shutdownCommand(WMenu
*menu
, WMenuEntry
*entry
)
202 static int inside
= 0;
205 /* prevent reentrant calls */
216 if ((long)entry
->clientdata
==M_QUICK
)
220 if (wSessionIsManaged()) {
223 r
= wMessageDialog(menu
->frame
->screen_ptr
,
224 _("Close X session"),
225 _("Close Window System session?\n"
226 "Kill might close applications with unsaved data."),
227 _("Close"), _("Kill"), _("Cancel"));
230 else if (r
==WAPRAlternate
)
237 r
= wMessageDialog(menu
->frame
->screen_ptr
,
239 _("Kill Window System session?\n"
240 "(all applications will be closed)"),
241 _("Kill"), _("Cancel"), NULL
);
247 if (result
!=R_CANCEL
) {
249 if (result
== R_CLOSE
) {
250 Shutdown(WSLogoutMode
);
252 #endif /* XSMP_ENABLED */
254 Shutdown(WSKillMode
);
265 restartCommand(WMenu
*menu
, WMenuEntry
*entry
)
267 Shutdown(WSRestartPreparationMode
);
268 Restart((char*)entry
->clientdata
);
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 LinkedList
*list
= NULL
;
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 list
= list_cons(token
, list
);
621 } while (token
!=NULL
&& tmp
!=NULL
);
623 count
= list_length(list
);
625 *file
= wmalloc(sizeof(char*)*(count
+1));
627 (*file
)[count
] = NULL
;
629 (*file
)[--i
] = list
->head
;
630 list_remove_head(&list
);
637 constructMenu(WMenu
*menu
, WMenuEntry
*entry
)
640 struct stat stat_buf
;
647 separateCommand((char*)entry
->clientdata
, &path
, &cmd
);
648 if (!path
|| *path
==NULL
|| **path
==0) {
649 wwarning(_("invalid OPEN_MENU specification: %s"),
650 (char*)entry
->clientdata
);
654 if (path
[0][0]=='|') {
657 if (!menu
->cascades
[entry
->cascade
] ||
658 menu
->cascades
[entry
->cascade
]->timestamp
== 0) {
661 submenu
= readMenuPipe(menu
->frame
->screen_ptr
, path
);
663 /* there's no automatic reloading */
665 submenu
->timestamp
= 1;
672 while(path
[i
] != NULL
) {
675 if (strcmp(path
[i
], "-noext")==0) {
680 tmp
= wexpandpath(path
[i
]);
682 lpath
= getLocalizedMenuFile(tmp
);
691 if (stat(path
[i
], &stat_buf
)==0) {
692 if (last
< stat_buf
.st_mtime
)
693 last
= stat_buf
.st_mtime
;
697 wsyserror(_("%s:could not stat menu"), path
[i
]);
705 wsyserror(_("%s:could not stat menu:%s"), "OPEN_MENU",
706 (char*)entry
->clientdata
);
709 stat(path
[first
], &stat_buf
);
710 if (!menu
->cascades
[entry
->cascade
]
711 || menu
->cascades
[entry
->cascade
]->timestamp
< last
) {
713 if (S_ISDIR(stat_buf
.st_mode
)) {
715 submenu
= readMenuDirectory(menu
->frame
->screen_ptr
,
716 entry
->text
, path
, cmd
);
718 submenu
->timestamp
= last
;
719 } else if (S_ISREG(stat_buf
.st_mode
)) {
723 wwarning(_("too many parameters in OPEN_MENU: %s"),
724 (char*)entry
->clientdata
);
726 submenu
= readMenuFile(menu
->frame
->screen_ptr
, path
[first
]);
728 submenu
->timestamp
= stat_buf
.st_mtime
;
738 wMenuEntryRemoveCascade(menu
, entry
);
739 wMenuEntrySetCascade(menu
, entry
, submenu
);
744 while (path
[i
]!=NULL
)
753 addWorkspaceMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
758 if (scr
->flags
.added_workspace_menu
) {
759 wwarning(_("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
762 scr
->flags
.added_workspace_menu
= 1;
764 wsmenu
= wWorkspaceMenuMake(scr
, True
);
765 scr
->workspace_menu
= wsmenu
;
766 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
767 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
769 wWorkspaceMenuUpdate(scr
, wsmenu
);
776 addMenuEntry(WMenu
*menu
, char *title
, char *shortcut
, char *command
,
777 char *params
, char *file_name
)
780 WMenuEntry
*entry
= NULL
;
781 Bool shortcutOk
= False
;
785 scr
= menu
->frame
->screen_ptr
;
786 if (strcmp(command
, "OPEN_MENU")==0) {
788 wwarning(_("%s:missing parameter for menu command \"%s\""),
794 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
796 path
= wstrdup(params
);
798 dummy
= wMenuCreate(scr
, title
, False
);
799 dummy
->on_destroy
= removeShortcutsForMenu
;
800 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
801 entry
->free_cdata
= free
;
802 wMenuEntrySetCascade(menu
, entry
, dummy
);
804 } else if (strcmp(command
, "EXEC")==0) {
806 wwarning(_("%s:missing parameter for menu command \"%s\""),
809 entry
= wMenuAddCallback(menu
, title
, execCommand
, wstrdup(params
));
810 entry
->free_cdata
= free
;
813 } else if (strcmp(command
, "EXIT")==0) {
815 if (params
&& strcmp(params
, "QUICK")==0)
816 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void*)M_QUICK
);
818 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
821 } else if (strcmp(command
, "SHUTDOWN")==0) {
823 if (params
&& strcmp(params
, "QUICK")==0)
824 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
,
827 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
830 } else if (strcmp(command
, "REFRESH")==0) {
831 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
834 } else if (strcmp(command
, "WORKSPACE_MENU")==0) {
835 entry
= addWorkspaceMenu(scr
, menu
, title
);
838 } else if (strcmp(command
, "ARRANGE_ICONS")==0) {
839 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
842 } else if (strcmp(command
, "HIDE_OTHERS")==0) {
843 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
846 } else if (strcmp(command
, "SHOW_ALL")==0) {
847 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
850 } else if (strcmp(command
, "RESTART")==0) {
851 entry
= wMenuAddCallback(menu
, title
, restartCommand
,
852 params
? wstrdup(params
) : NULL
);
853 entry
->free_cdata
= free
;
855 } else if (strcmp(command
, "SAVE_SESSION")==0) {
856 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
859 } else if (strcmp(command
, "CLEAR_SESSION")==0) {
860 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
862 } else if (strcmp(command
, "INFO_PANEL")==0) {
863 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
865 } else if (strcmp(command
, "LEGAL_PANEL")==0) {
866 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
869 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
,
875 if (shortcut
&& entry
) {
877 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
,
880 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
882 entry
->rtext
= GetShortcutString(shortcut
);
884 entry->rtext = wstrdup(shortcut);
895 /******************* Menu Configuration From File *******************/
898 separateline(char *line
, char *title
, char *command
, char *parameter
,
910 while (isspace(*line
) && (*line
!=0)) line
++;
914 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
919 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
921 strncpy(title
, line
, i
);
925 /* get the command or shortcut keyword */
926 while (isspace(*line
) && (*line
!=0)) line
++;
930 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
931 strncpy(command
, line
, i
);
935 if (strcmp(command
, "SHORTCUT")==0) {
936 /* get the shortcut key */
937 while (isspace(*line
) && (*line
!=0)) line
++;
941 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
946 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
948 strncpy(shortcut
, line
, i
);
954 /* get the command */
955 while (isspace(*line
) && (*line
!=0)) line
++;
959 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
960 strncpy(command
, line
, i
);
965 /* get the parameters */
966 while (isspace(*line
) && (*line
!=0)) line
++;
973 while (line
[l
]!=0 && line
[l
]!='"') {
974 parameter
[l
] = line
[l
];
982 while (isspace(line
[l
]) && (l
>0)) l
--;
983 strncpy(parameter
, line
, l
);
989 parseCascade(WScreen
*scr
, WMenu
*menu
, FILE *file
, char *file_name
)
991 char linebuf
[MAXLINE
];
992 char elinebuf
[MAXLINE
];
994 char command
[MAXLINE
];
995 char shortcut
[MAXLINE
];
996 char params
[MAXLINE
];
999 while (!IsEof(file
)) {
1003 fgets(linebuf
, MAXLINE
, file
);
1004 line
= cropline(linebuf
);
1005 lsize
= strlen(line
);
1007 if (line
[lsize
-1]=='\\') {
1010 fgets(elinebuf
, MAXLINE
, file
);
1011 line2
=cropline(elinebuf
);
1012 lsize2
=strlen(line2
);
1013 if (lsize2
+lsize
>MAXLINE
) {
1014 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1020 strcat(line
, line2
);
1025 } while (!ok
&& !IsEof(file
));
1029 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1033 separateline(line
, title
, command
, params
, shortcut
);
1036 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1041 if (strcasecmp(command
, "MENU")==0) {
1046 cascade
= wMenuCreate(scr
, title
, False
);
1047 cascade
->on_destroy
= removeShortcutsForMenu
;
1048 if (parseCascade(scr
, cascade
, file
, file_name
)==NULL
) {
1049 wMenuDestroy(cascade
, True
);
1051 wMenuEntrySetCascade(menu
,
1052 wMenuAddCallback(menu
, title
, NULL
, NULL
),
1055 } else if (strcasecmp(command
, "END")==0) {
1061 addMenuEntry(menu
, title
, shortcut
[0] ? shortcut
: NULL
, command
,
1062 params
[0] ? params
: NULL
, file_name
);
1066 wwarning(_("%s:syntax error in menu file:END declaration missing"),
1076 readMenuFile(WScreen
*scr
, char *file_name
)
1080 char linebuf
[MAXLINE
];
1081 char title
[MAXLINE
];
1082 char shortcut
[MAXLINE
];
1083 char command
[MAXLINE
];
1084 char params
[MAXLINE
];
1092 if (!wPreferences
.flags
.nocpp
) {
1093 args
= MakeCPPArgs(file_name
);
1095 wwarning(_("could not make arguments for menu file preprocessor"));
1097 sprintf(command
, "%s %s %s", CPP_PATH
, args
, file_name
);
1099 file
= popen(command
, "r");
1101 wsyserror(_("%s:could not open/preprocess menu file"),
1111 file
= fopen(file_name
, "r");
1113 wsyserror(_("%s:could not open menu file"), file_name
);
1118 while (!IsEof(file
)) {
1119 if (!fgets(linebuf
, MAXLINE
, file
))
1121 line
= cropline(linebuf
);
1122 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1125 separateline(line
, title
, command
, params
, shortcut
);
1128 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1132 if (strcasecmp(command
, "MENU")==0) {
1133 menu
= wMenuCreate(scr
, title
, True
);
1134 menu
->on_destroy
= removeShortcutsForMenu
;
1135 if (!parseCascade(scr
, menu
, file
, file_name
)) {
1136 wMenuDestroy(menu
, True
);
1140 wwarning(_("%s:invalid menu file. MENU command is missing"),
1148 if (pclose(file
)==-1) {
1149 wsyserror(_("error reading preprocessed menu data"));
1161 /************ Menu Configuration From Pipe *************/
1164 readMenuPipe(WScreen
*scr
, char **file_name
)
1168 char linebuf
[MAXLINE
];
1169 char title
[MAXLINE
];
1170 char command
[MAXLINE
];
1171 char params
[MAXLINE
];
1172 char shortcut
[MAXLINE
];
1175 char flat_file
[MAXLINE
];
1182 flat_file
[0] = '\0';
1184 for(i
= 0 ; file_name
[i
] != NULL
; i
++) {
1185 strcat(flat_file
, file_name
[i
]);
1186 strcat(flat_file
, " ");
1188 filename
= flat_file
+1;
1191 if (!wPreferences
.flags
.nocpp
) {
1192 args
= MakeCPPArgs(filename
);
1194 wwarning(_("could not make arguments for menu file preprocessor"));
1196 sprintf(command
, "%s | %s %s", filename
, CPP_PATH
, args
);
1199 file
= popen(command
, "r");
1201 wsyserror(_("%s:could not open/preprocess menu file"), filename
);
1211 file
= popen(filename
, "r");
1214 wsyserror(_("%s:could not open menu file"), filename
);
1219 while (!IsEof(file
)) {
1220 if (!fgets(linebuf
, MAXLINE
, file
))
1222 line
= cropline(linebuf
);
1223 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1226 separateline(line
, title
, command
, params
, shortcut
);
1229 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1233 if (strcasecmp(command
, "MENU")==0) {
1234 menu
= wMenuCreate(scr
, title
, True
);
1235 menu
->on_destroy
= removeShortcutsForMenu
;
1236 if (!parseCascade(scr
, menu
, file
, filename
)) {
1237 wMenuDestroy(menu
, True
);
1241 wwarning(_("%s:no title given for the root menu"), filename
);
1260 myCompare(dir_data
*d1
, dir_data
*d2
)
1262 return strcmp(d1
->name
, d2
->name
);
1266 /************ Menu Configuration From Directory *************/
1269 isFilePackage(char *file
)
1273 /* check if the extension indicates this file is a
1274 * file package. For now, only recognize .themed */
1278 if (l
> 7 && strcmp(&(file
[l
-7]), ".themed")==0) {
1287 readMenuDirectory(WScreen
*scr
, char *title
, char **path
, char *command
)
1290 struct dirent
*dentry
;
1291 struct stat stat_buf
;
1294 LinkedList
*dirs
= NULL
, *files
= NULL
;
1295 int length
, i
, have_space
=0;
1297 int stripExtension
= 0;
1300 while (path
[i
]!=NULL
) {
1301 if (strcmp(path
[i
], "-noext")==0) {
1307 dir
= opendir(path
[i
]);
1313 while ((dentry
= readdir(dir
))) {
1315 if (strcmp(dentry
->d_name
, ".")==0 ||
1316 strcmp(dentry
->d_name
, "..")==0)
1319 if (dentry
->d_name
[0] == '.')
1322 buffer
= wmalloc(strlen(path
[i
])+strlen(dentry
->d_name
)+4);
1324 wsyserror(_("out of memory while constructing directory menu %s"),
1329 strcpy(buffer
, path
[i
]);
1330 strcat(buffer
, "/");
1331 strcat(buffer
, dentry
->d_name
);
1333 if (stat(buffer
, &stat_buf
)!=0) {
1334 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1335 path
[i
], dentry
->d_name
);
1337 Bool isFilePack
= False
;
1340 if (S_ISDIR(stat_buf
.st_mode
)
1341 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1343 /* access always returns success for user root */
1344 if (access(buffer
, X_OK
)==0) {
1345 /* Directory is accesible. Add to directory list */
1347 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1348 data
->name
= wstrdup(dentry
->d_name
);
1351 list_insert_sorted(data
, &dirs
, (int(*)())myCompare
);
1353 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1354 /* Hack because access always returns X_OK success for user root */
1355 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1356 if ((command
!=NULL
&& access(buffer
, R_OK
)==0) ||
1357 (command
==NULL
&& access(buffer
, X_OK
)==0 &&
1358 (stat_buf
.st_mode
& S_IXANY
))) {
1360 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1361 data
->name
= wstrdup(dentry
->d_name
);
1364 list_insert_sorted(data
, &files
, (int(*)())myCompare
);
1375 if (!dirs
&& !files
)
1378 menu
= wMenuCreate(scr
, title
, False
);
1379 menu
->on_destroy
= removeShortcutsForMenu
;
1381 while (dirs
!= NULL
) {
1382 /* New directory. Use same OPEN_MENU command that was used
1383 * for the current directory. */
1384 dir_data
*d
= (dir_data
*)dirs
->head
;
1386 length
= strlen(path
[d
->index
])+strlen(d
->name
)+6;
1388 length
+= strlen(command
) + 6;
1389 buffer
= wmalloc(length
);
1391 wsyserror(_("out of memory while constructing directory menu %s"),
1396 have_space
= strchr(path
[d
->index
], ' ')!=NULL
||
1397 strchr(d
->name
, ' ')!=NULL
;
1401 strcat(buffer
, path
[d
->index
]);
1403 strcpy(buffer
, path
[d
->index
]);
1405 strcat(buffer
, "/");
1406 strcat(buffer
, d
->name
);
1408 strcat(buffer
, "\"");
1410 strcat(buffer
, " WITH ");
1411 strcat(buffer
, command
);
1414 addMenuEntry(menu
, d
->name
, NULL
, "OPEN_MENU", buffer
, path
[d
->index
]);
1422 list_remove_head(&dirs
);
1425 while (files
!= NULL
) {
1426 /* executable: add as entry */
1427 dir_data
*f
= (dir_data
*) files
->head
;;
1429 length
= strlen(path
[f
->index
])+strlen(f
->name
)+6;
1431 length
+= strlen(command
);
1433 buffer
= wmalloc(length
);
1435 wsyserror(_("out of memory while constructing directory menu %s"),
1440 have_space
= strchr(path
[f
->index
], ' ')!=NULL
||
1441 strchr(f
->name
, ' ')!=NULL
;
1442 if (command
!=NULL
) {
1443 strcpy(buffer
, command
);
1444 strcat(buffer
, " ");
1446 strcat(buffer
, "\"");
1447 strcat(buffer
, path
[f
->index
]);
1452 strcat(buffer
, path
[f
->index
]);
1454 strcpy(buffer
, path
[f
->index
]);
1457 strcat(buffer
, "/");
1458 strcat(buffer
, f
->name
);
1460 strcat(buffer
, "\"");
1462 if (stripExtension
) {
1463 char *ptr
= strrchr(f
->name
, '.');
1464 if (ptr
&& ptr
!=f
->name
)
1467 addMenuEntry(menu
, f
->name
, NULL
, "EXEC", buffer
, path
[f
->index
]);
1475 list_remove_head(&files
);
1482 /************ Menu Configuration From WMRootMenu *************/
1485 makeDefaultMenu(WScreen
*scr
)
1489 menu
= wMenuCreate(scr
, _("Commands"), True
);
1490 wMenuAddCallback(menu
, "XTerm", execCommand
, "xterm");
1491 wMenuAddCallback(menu
, "rxvt", execCommand
, "rxvt");
1492 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1493 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1502 *----------------------------------------------------------------------
1504 * Reads root menu configuration from defaults database.
1506 *----------------------------------------------------------------------
1509 configureMenu(WScreen
*scr
, proplist_t definition
)
1514 proplist_t title
, command
, params
;
1518 if (PLIsString(definition
)) {
1519 struct stat stat_buf
;
1521 Bool menu_is_default
= False
;
1523 /* menu definition is a string. Probably a path, so parse the file */
1525 tmp
= wexpandpath(PLGetString(definition
));
1527 path
= getLocalizedMenuFile(tmp
);
1530 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1533 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1534 menu_is_default
= True
;
1538 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"),
1544 if (stat(path
, &stat_buf
)<0) {
1545 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1551 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1552 /* if the pointer in WMRootMenu has changed */
1553 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1555 if (menu_is_default
) {
1556 wwarning(_("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1560 menu
= readMenuFile(scr
, path
);
1562 menu
->timestamp
= MAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1572 count
= PLGetNumberOfElements(definition
);
1576 elem
= PLGetArrayElement(definition
, 0);
1577 if (!PLIsString(elem
)) {
1578 tmp
= PLGetDescription(elem
);
1579 wwarning(_("%s:format error in root menu configuration \"%s\""),
1584 mtitle
= PLGetString(elem
);
1586 menu
= wMenuCreate(scr
, mtitle
, False
);
1587 menu
->on_destroy
= removeShortcutsForMenu
;
1589 #ifdef GLOBAL_SUBMENU_FILE
1594 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1597 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1598 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1603 for (i
=1; i
<count
; i
++) {
1604 elem
= PLGetArrayElement(definition
, i
);
1606 if (PLIsString(elem
)) {
1609 file
= PLGetString(elem
);
1613 if (!PLIsArray(elem
) || PLGetNumberOfElements(elem
) < 2)
1616 if (PLIsArray(PLGetArrayElement(elem
,1))) {
1621 submenu
= configureMenu(scr
, elem
);
1623 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
,
1625 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1632 title
= PLGetArrayElement(elem
, idx
++);
1633 shortcut
= PLGetArrayElement(elem
, idx
++);
1634 if (strcmp(PLGetString(shortcut
), "SHORTCUT")==0) {
1635 shortcut
= PLGetArrayElement(elem
, idx
++);
1636 command
= PLGetArrayElement(elem
, idx
++);
1641 params
= PLGetArrayElement(elem
, idx
++);
1643 if (!title
|| !command
)
1646 addMenuEntry(menu
, PLGetString(title
),
1647 shortcut
? PLGetString(shortcut
) : NULL
,
1648 PLGetString(command
),
1649 params
? PLGetString(params
) : NULL
, "WMRootMenu");
1654 tmp
= PLGetDescription(elem
);
1655 wwarning(_("%s:format error in root menu configuration \"%s\""),
1671 *----------------------------------------------------------------------
1673 * Opens the root menu, parsing the menu configuration from the
1674 * defaults database.
1675 * If the menu is already mapped and is not sticked to the
1676 * root window, it will be unmapped.
1679 * The menu may be remade.
1682 * Construction of OPEN_MENU entries are delayed to the moment the
1684 *----------------------------------------------------------------------
1687 OpenRootMenu(WScreen
*scr
, int x
, int y
, int keyboard
)
1690 proplist_t definition
;
1692 static proplist_t domain=NULL;
1695 domain = PLMakeString("WMRootMenu");
1699 scr
->flags
.root_menu_changed_shortcuts
= 0;
1700 scr
->flags
.added_workspace_menu
= 0;
1702 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1703 menu
= scr
->root_menu
;
1704 if (!menu
->flags
.buttoned
) {
1707 wRaiseFrame(menu
->frame
->core
);
1710 wMenuMapAt(menu
, 0, 0, True
);
1712 wMenuMapCopyAt(menu
, x
-menu
->frame
->core
->width
/2,
1713 y
-menu
->frame
->top_width
/2);
1719 definition
= WDRootMenu
->dictionary
;
1722 definition = PLGetDomain(domain);
1725 if (PLIsArray(definition
)) {
1727 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1728 menu
= configureMenu(scr
, definition
);
1730 menu
->timestamp
= WDRootMenu
->timestamp
;
1735 menu
= configureMenu(scr
, definition
);
1740 /* menu hasn't changed or could not be read */
1741 if (!scr
->root_menu
) {
1742 wMessageDialog(scr
, _("Error"),
1743 _("The applications menu could not be loaded."
1744 "Look at the console output for a detailed"
1745 "description of the errors"),
1746 _("OK"), NULL
, NULL
);
1748 menu
= makeDefaultMenu(scr
);
1749 scr
->root_menu
= menu
;
1751 menu
= scr
->root_menu
;
1755 wMenuDestroy(scr
->root_menu
, True
);
1756 scr
->root_menu
= menu
;
1759 wMenuMapAt(menu
, x
-menu
->frame
->core
->width
/2, y
-menu
->frame
->top_width
/2,
1763 if (scr
->flags
.root_menu_changed_shortcuts
)
1764 rebindKeygrabs(scr
);