1 /* rootmenu.c- user defined menu
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 * Copyright (c) 1998 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
33 #include <sys/types.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xatom.h>
43 #include "WindowMaker.h"
50 #include "workspace.h"
54 #include "xmodifier.h"
56 #include <WINGs/WUtil.h>
62 extern WDDomain
*WDRootMenu
;
64 extern Cursor wCursor
[WCUR_LAST
];
66 extern Time LastTimestamp
;
68 extern WPreferences wPreferences
;
70 extern int wScreenCount
;
72 static WMenu
*readMenuPipe(WScreen
*scr
, char **file_name
);
73 static WMenu
*readMenuFile(WScreen
*scr
, char *file_name
);
74 static WMenu
*readMenuDirectory(WScreen
*scr
, char *title
, char **file_name
,
78 typedef struct Shortcut
{
79 struct Shortcut
*next
;
89 static Shortcut
*shortcutList
= NULL
;
96 * "Title" EXEC command_to_exec -params
98 * "Title" EXEC command_to_exec -params
100 * "Workspaces" WORKSPACE_MENU
101 * "Title" built_in_command
103 * "Quick Quit" EXIT QUICK
106 * Commands may be preceded by SHORTCUT key
110 * INFO_PANEL - shows the Info Panel
111 * LEGAL_PANEL - shows the Legal info panel
112 * SHUTDOWN [QUICK] - closes the X server [without confirmation]
113 * REFRESH - forces the desktop to be repainted
114 * EXIT [QUICK] - exit the window manager [without confirmation]
115 * EXEC <program> - execute an external program
116 * SHEXEC <command> - execute a shell command
117 * WORKSPACE_MENU - places the workspace submenu
119 * RESTART [<window manager>] - restarts the window manager
120 * SHOW_ALL - unhide all windows on workspace
121 * HIDE_OTHERS - hides all windows excep the focused one
122 * OPEN_MENU file - read menu data from file which must be a valid menu file.
123 * OPEN_MENU /some/dir [/some/other/dir ...] [WITH command -options]
124 * - read menu data from directory(ies) and
125 * eventually precede each with a command.
126 * OPEN_MENU | command
127 * - opens command and uses its stdout to construct and insert
128 * the resulting menu in current position. The output of
129 * command must be a valid menu description.
130 * The space between '|' and command is optional.
131 * SAVE_SESSION - saves the current state of the desktop, which include
132 * all running applications, all their hints (geometry,
133 * position on screen, workspace they live on, the dock
134 * or clip from where they were launched, and
135 * if minimized, shaded or hidden. Also saves the current
136 * workspace the user is on. All will be restored on every
137 * start of windowmaker until another SAVE_SESSION or
138 * CLEAR_SESSION is used. If SaveSessionOnExit = Yes; in
139 * WindowMaker domain file, then saving is automatically
140 * done on every windowmaker exit, overwriting any
141 * SAVE_SESSION or CLEAR_SESSION (see below). Also save
143 * CLEAR_SESSION - clears any previous saved session. This will not have
144 * any effect if SaveSessionOnExit is True.
153 execCommand(WMenu
*menu
, WMenuEntry
*entry
)
157 cmdline
= ExpandOptions(menu
->frame
->screen_ptr
, (char*)entry
->clientdata
);
159 XGrabPointer(dpy
, menu
->frame
->screen_ptr
->root_win
, True
, 0,
160 GrabModeAsync
, GrabModeAsync
, None
, wCursor
[WCUR_WAIT
],
165 ExecuteShellCommand(menu
->frame
->screen_ptr
, cmdline
);
168 XUngrabPointer(dpy
, CurrentTime
);
174 exitCommand(WMenu
*menu
, WMenuEntry
*entry
)
176 static int inside
= 0;
178 /* prevent reentrant calls */
183 if ((long)entry
->clientdata
==M_QUICK
184 || wMessageDialog(menu
->frame
->screen_ptr
, _("Exit"),
185 _("Exit window manager?"),
186 _("Exit"), _("Cancel"), NULL
)==WAPRDefault
) {
188 printf("Exiting WindowMaker.\n");
190 Shutdown(WSExitMode
);
197 shutdownCommand(WMenu
*menu
, WMenuEntry
*entry
)
199 static int inside
= 0;
202 /* prevent reentrant calls */
213 if ((long)entry
->clientdata
==M_QUICK
)
217 if (wSessionIsManaged()) {
220 r
= wMessageDialog(menu
->frame
->screen_ptr
,
221 _("Close X session"),
222 _("Close Window System session?\n"
223 "Kill might close applications with unsaved data."),
224 _("Close"), _("Kill"), _("Cancel"));
227 else if (r
==WAPRAlternate
)
234 r
= wMessageDialog(menu
->frame
->screen_ptr
,
236 _("Kill Window System session?\n"
237 "(all applications will be closed)"),
238 _("Kill"), _("Cancel"), NULL
);
244 if (result
!=R_CANCEL
) {
246 if (result
== R_CLOSE
) {
247 Shutdown(WSLogoutMode
);
249 #endif /* XSMP_ENABLED */
251 Shutdown(WSKillMode
);
262 restartCommand(WMenu
*menu
, WMenuEntry
*entry
)
264 Shutdown(WSRestartPreparationMode
);
265 Restart((char*)entry
->clientdata
, False
);
271 refreshCommand(WMenu
*menu
, WMenuEntry
*entry
)
273 wRefreshDesktop(menu
->frame
->screen_ptr
);
278 arrangeIconsCommand(WMenu
*menu
, WMenuEntry
*entry
)
280 wArrangeIcons(menu
->frame
->screen_ptr
, True
);
284 showAllCommand(WMenu
*menu
, WMenuEntry
*entry
)
286 wShowAllWindows(menu
->frame
->screen_ptr
);
290 hideOthersCommand(WMenu
*menu
, WMenuEntry
*entry
)
292 wHideOtherApplications(menu
->frame
->screen_ptr
->focused_window
);
297 saveSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
299 if (!wPreferences
.save_session_on_exit
)
300 wSessionSaveState(menu
->frame
->screen_ptr
);
302 wScreenSaveState(menu
->frame
->screen_ptr
);
307 clearSessionCommand(WMenu
*menu
, WMenuEntry
*entry
)
309 wSessionClearState(menu
->frame
->screen_ptr
);
314 infoPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
316 wShowInfoPanel(menu
->frame
->screen_ptr
);
321 legalPanelCommand(WMenu
*menu
, WMenuEntry
*entry
)
323 wShowLegalPanel(menu
->frame
->screen_ptr
);
327 /********************************************************************/
331 getLocalizedMenuFile(char *menu
)
340 len
= strlen(menu
)+strlen(Locale
)+8;
341 buffer
= wmalloc(len
);
343 /* try menu.locale_name */
344 snprintf(buffer
, len
, "%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 cleanupWorkspaceMenu(WMenu
*menu
)
754 if (menu
->frame
->screen_ptr
->workspace_menu
== menu
)
755 menu
->frame
->screen_ptr
->workspace_menu
= NULL
;
760 addWorkspaceMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
765 if (scr
->flags
.added_workspace_menu
) {
766 wwarning(_("There are more than one WORKSPACE_MENU commands in the applications menu. Only one is allowed."));
769 scr
->flags
.added_workspace_menu
= 1;
771 wsmenu
= wWorkspaceMenuMake(scr
, True
);
772 wsmenu
->on_destroy
= cleanupWorkspaceMenu
;
774 scr
->workspace_menu
= wsmenu
;
775 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
776 wMenuEntrySetCascade(menu
, entry
, wsmenu
);
778 wWorkspaceMenuUpdate(scr
, wsmenu
);
785 cleanupWindowsMenu(WMenu
*menu
)
787 if (menu
->frame
->screen_ptr
->switch_menu
== menu
)
788 menu
->frame
->screen_ptr
->switch_menu
= NULL
;
793 addWindowsMenu(WScreen
*scr
, WMenu
*menu
, char *title
)
799 if (scr
->flags
.added_windows_menu
) {
800 wwarning(_("There are more than one WINDOWS_MENU commands in the applications menu. Only one is allowed."));
803 scr
->flags
.added_windows_menu
= 1;
805 wwmenu
= wMenuCreate(scr
, _("Window List"), False
);
806 wwmenu
->on_destroy
= cleanupWindowsMenu
;
807 scr
->switch_menu
= wwmenu
;
808 wwin
= scr
->focused_window
;
810 UpdateSwitchMenu(scr
, wwin
, ACTION_ADD
);
814 entry
= wMenuAddCallback(menu
, title
, NULL
, NULL
);
815 wMenuEntrySetCascade(menu
, entry
, wwmenu
);
822 addMenuEntry(WMenu
*menu
, char *title
, char *shortcut
, char *command
,
823 char *params
, char *file_name
)
826 WMenuEntry
*entry
= NULL
;
827 Bool shortcutOk
= False
;
831 scr
= menu
->frame
->screen_ptr
;
832 if (strcmp(command
, "OPEN_MENU")==0) {
834 wwarning(_("%s:missing parameter for menu command \"%s\""),
840 path
= wfindfile(DEF_CONFIG_PATHS
, params
);
842 path
= wstrdup(params
);
844 dummy
= wMenuCreate(scr
, title
, False
);
845 dummy
->on_destroy
= removeShortcutsForMenu
;
846 entry
= wMenuAddCallback(menu
, title
, constructMenu
, path
);
847 entry
->free_cdata
= free
;
848 wMenuEntrySetCascade(menu
, entry
, dummy
);
850 } else if (strcmp(command
, "EXEC")==0) {
852 wwarning(_("%s:missing parameter for menu command \"%s\""),
855 entry
= wMenuAddCallback(menu
, title
, execCommand
,
856 wstrconcat("exec ", params
));
857 entry
->free_cdata
= free
;
860 } else if (strcmp(command
, "SHEXEC")==0) {
862 wwarning(_("%s:missing parameter for menu command \"%s\""),
865 entry
= wMenuAddCallback(menu
, title
, execCommand
,
867 entry
->free_cdata
= free
;
870 } else if (strcmp(command
, "EXIT")==0) {
872 if (params
&& strcmp(params
, "QUICK")==0)
873 entry
= wMenuAddCallback(menu
, title
, exitCommand
, (void*)M_QUICK
);
875 entry
= wMenuAddCallback(menu
, title
, exitCommand
, NULL
);
878 } else if (strcmp(command
, "SHUTDOWN")==0) {
880 if (params
&& strcmp(params
, "QUICK")==0)
881 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
,
884 entry
= wMenuAddCallback(menu
, title
, shutdownCommand
, NULL
);
887 } else if (strcmp(command
, "REFRESH")==0) {
888 entry
= wMenuAddCallback(menu
, title
, refreshCommand
, NULL
);
891 } else if (strcmp(command
, "WORKSPACE_MENU")==0) {
892 entry
= addWorkspaceMenu(scr
, menu
, title
);
895 } else if (strcmp(command
, "WINDOWS_MENU")==0) {
896 entry
= addWindowsMenu(scr
, menu
, title
);
899 } else if (strcmp(command
, "ARRANGE_ICONS")==0) {
900 entry
= wMenuAddCallback(menu
, title
, arrangeIconsCommand
, NULL
);
903 } else if (strcmp(command
, "HIDE_OTHERS")==0) {
904 entry
= wMenuAddCallback(menu
, title
, hideOthersCommand
, NULL
);
907 } else if (strcmp(command
, "SHOW_ALL")==0) {
908 entry
= wMenuAddCallback(menu
, title
, showAllCommand
, NULL
);
911 } else if (strcmp(command
, "RESTART")==0) {
912 entry
= wMenuAddCallback(menu
, title
, restartCommand
,
913 params
? wstrdup(params
) : NULL
);
914 entry
->free_cdata
= free
;
916 } else if (strcmp(command
, "SAVE_SESSION")==0) {
917 entry
= wMenuAddCallback(menu
, title
, saveSessionCommand
, NULL
);
920 } else if (strcmp(command
, "CLEAR_SESSION")==0) {
921 entry
= wMenuAddCallback(menu
, title
, clearSessionCommand
, NULL
);
923 } else if (strcmp(command
, "INFO_PANEL")==0) {
924 entry
= wMenuAddCallback(menu
, title
, infoPanelCommand
, NULL
);
926 } else if (strcmp(command
, "LEGAL_PANEL")==0) {
927 entry
= wMenuAddCallback(menu
, title
, legalPanelCommand
, NULL
);
930 wwarning(_("%s:unknown command \"%s\" in menu config."), file_name
,
936 if (shortcut
&& entry
) {
938 wwarning(_("%s:can't add shortcut for entry \"%s\""), file_name
,
941 if (addShortcut(file_name
, shortcut
, menu
, entry
)) {
943 entry
->rtext
= GetShortcutString(shortcut
);
945 entry->rtext = wstrdup(shortcut);
956 /******************* Menu Configuration From File *******************/
959 separateline(char *line
, char *title
, char *command
, char *parameter
,
971 while (isspace(*line
) && (*line
!=0)) line
++;
975 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
980 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
982 strncpy(title
, line
, i
);
986 /* get the command or shortcut keyword */
987 while (isspace(*line
) && (*line
!=0)) line
++;
991 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
992 strncpy(command
, line
, i
);
996 if (strcmp(command
, "SHORTCUT")==0) {
997 /* get the shortcut key */
998 while (isspace(*line
) && (*line
!=0)) line
++;
1002 while (line
[i
]!='"' && (line
[i
]!=0)) i
++;
1007 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1009 strncpy(shortcut
, line
, i
);
1015 /* get the command */
1016 while (isspace(*line
) && (*line
!=0)) line
++;
1020 while (!isspace(line
[i
]) && (line
[i
]!=0)) i
++;
1021 strncpy(command
, line
, i
);
1026 /* get the parameters */
1027 while (isspace(*line
) && (*line
!=0)) line
++;
1034 while (line
[l
]!=0 && line
[l
]!='"') {
1035 parameter
[l
] = line
[l
];
1043 while (isspace(line
[l
]) && (l
>0)) l
--;
1044 strncpy(parameter
, line
, l
);
1050 parseCascade(WScreen
*scr
, WMenu
*menu
, FILE *file
, char *file_name
)
1052 char linebuf
[MAXLINE
];
1053 char elinebuf
[MAXLINE
];
1054 char title
[MAXLINE
];
1055 char command
[MAXLINE
];
1056 char shortcut
[MAXLINE
];
1057 char params
[MAXLINE
];
1060 while (!IsEof(file
)) {
1064 fgets(linebuf
, MAXLINE
, file
);
1065 line
= cropline(linebuf
);
1066 lsize
= strlen(line
);
1068 if (line
[lsize
-1]=='\\') {
1071 fgets(elinebuf
, MAXLINE
, file
);
1072 line2
=cropline(elinebuf
);
1073 lsize2
=strlen(line2
);
1074 if (lsize2
+lsize
>MAXLINE
) {
1075 wwarning(_("%s:maximal line size exceeded in menu config: %s"),
1081 strcat(line
, line2
);
1086 } while (!ok
&& !IsEof(file
));
1090 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1094 separateline(line
, title
, command
, params
, shortcut
);
1097 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1102 if (strcasecmp(command
, "MENU")==0) {
1107 cascade
= wMenuCreate(scr
, title
, False
);
1108 cascade
->on_destroy
= removeShortcutsForMenu
;
1109 if (parseCascade(scr
, cascade
, file
, file_name
)==NULL
) {
1110 wMenuDestroy(cascade
, True
);
1112 wMenuEntrySetCascade(menu
,
1113 wMenuAddCallback(menu
, title
, NULL
, NULL
),
1116 } else if (strcasecmp(command
, "END")==0) {
1122 addMenuEntry(menu
, title
, shortcut
[0] ? shortcut
: NULL
, command
,
1123 params
[0] ? params
: NULL
, file_name
);
1127 wwarning(_("%s:syntax error in menu file:END declaration missing"),
1137 readMenuFile(WScreen
*scr
, char *file_name
)
1141 char linebuf
[MAXLINE
];
1142 char title
[MAXLINE
];
1143 char shortcut
[MAXLINE
];
1144 char command
[MAXLINE
];
1145 char params
[MAXLINE
];
1153 if (!wPreferences
.flags
.nocpp
) {
1154 args
= MakeCPPArgs(file_name
);
1156 wwarning(_("could not make arguments for menu file preprocessor"));
1158 snprintf(command
, sizeof(command
), "%s %s %s",
1159 CPP_PATH
, args
, file_name
);
1161 file
= popen(command
, "r");
1163 wsyserror(_("%s:could not open/preprocess menu file"),
1173 file
= fopen(file_name
, "r");
1175 wsyserror(_("%s:could not open menu file"), file_name
);
1180 while (!IsEof(file
)) {
1181 if (!fgets(linebuf
, MAXLINE
, file
))
1183 line
= cropline(linebuf
);
1184 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1187 separateline(line
, title
, command
, params
, shortcut
);
1190 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1194 if (strcasecmp(command
, "MENU")==0) {
1195 menu
= wMenuCreate(scr
, title
, True
);
1196 menu
->on_destroy
= removeShortcutsForMenu
;
1197 if (!parseCascade(scr
, menu
, file
, file_name
)) {
1198 wMenuDestroy(menu
, True
);
1202 wwarning(_("%s:invalid menu file. MENU command is missing"),
1210 if (pclose(file
)==-1) {
1211 wsyserror(_("error reading preprocessed menu data"));
1223 /************ Menu Configuration From Pipe *************/
1226 readMenuPipe(WScreen
*scr
, char **file_name
)
1230 char linebuf
[MAXLINE
];
1231 char title
[MAXLINE
];
1232 char command
[MAXLINE
];
1233 char params
[MAXLINE
];
1234 char shortcut
[MAXLINE
];
1237 char flat_file
[MAXLINE
];
1244 flat_file
[0] = '\0';
1246 for(i
=0; file_name
[i
]!=NULL
; i
++) {
1247 strcat(flat_file
, file_name
[i
]);
1248 strcat(flat_file
, " ");
1250 filename
= flat_file
+1;
1253 if (!wPreferences
.flags
.nocpp
) {
1254 args
= MakeCPPArgs(filename
);
1256 wwarning(_("could not make arguments for menu file preprocessor"));
1258 snprintf(command
, sizeof(command
), "%s | %s %s",
1259 filename
, CPP_PATH
, args
);
1262 file
= popen(command
, "r");
1264 wsyserror(_("%s:could not open/preprocess menu file"), filename
);
1274 file
= popen(filename
, "r");
1277 wsyserror(_("%s:could not open menu file"), filename
);
1282 while (!IsEof(file
)) {
1283 if (!fgets(linebuf
, MAXLINE
, file
))
1285 line
= cropline(linebuf
);
1286 if (line
[0]==0 || line
[0]=='#' || (line
[0]=='/' && line
[1]=='/'))
1289 separateline(line
, title
, command
, params
, shortcut
);
1292 wwarning(_("%s:missing command in menu config: %s"), file_name
,
1296 if (strcasecmp(command
, "MENU")==0) {
1297 menu
= wMenuCreate(scr
, title
, True
);
1298 menu
->on_destroy
= removeShortcutsForMenu
;
1299 if (!parseCascade(scr
, menu
, file
, filename
)) {
1300 wMenuDestroy(menu
, True
);
1304 wwarning(_("%s:no title given for the root menu"), filename
);
1323 myCompare(const void *d1
, const void *d2
)
1325 dir_data
*p1
= *(dir_data
**)d1
;
1326 dir_data
*p2
= *(dir_data
**)d2
;
1328 return strcmp(p1
->name
, p2
->name
);
1332 /************ Menu Configuration From Directory *************/
1335 isFilePackage(char *file
)
1339 /* check if the extension indicates this file is a
1340 * file package. For now, only recognize .themed */
1344 if (l
> 7 && strcmp(&(file
[l
-7]), ".themed")==0) {
1353 readMenuDirectory(WScreen
*scr
, char *title
, char **path
, char *command
)
1356 struct dirent
*dentry
;
1357 struct stat stat_buf
;
1360 WMArray
*dirs
= NULL
, *files
= NULL
;
1361 WMArrayIterator iter
;
1362 int length
, i
, have_space
=0;
1364 int stripExtension
= 0;
1367 dirs
= WMCreateArray(16);
1368 files
= WMCreateArray(16);
1371 while (path
[i
]!=NULL
) {
1372 if (strcmp(path
[i
], "-noext")==0) {
1378 dir
= opendir(path
[i
]);
1384 while ((dentry
= readdir(dir
))) {
1386 if (strcmp(dentry
->d_name
, ".")==0 ||
1387 strcmp(dentry
->d_name
, "..")==0)
1390 if (dentry
->d_name
[0] == '.')
1393 buffer
= malloc(strlen(path
[i
])+strlen(dentry
->d_name
)+4);
1395 wsyserror(_("out of memory while constructing directory menu %s"),
1400 strcpy(buffer
, path
[i
]);
1401 strcat(buffer
, "/");
1402 strcat(buffer
, dentry
->d_name
);
1404 if (stat(buffer
, &stat_buf
)!=0) {
1405 wsyserror(_("%s:could not stat file \"%s\" in menu directory"),
1406 path
[i
], dentry
->d_name
);
1408 Bool isFilePack
= False
;
1411 if (S_ISDIR(stat_buf
.st_mode
)
1412 && !(isFilePack
= isFilePackage(dentry
->d_name
))) {
1414 /* access always returns success for user root */
1415 if (access(buffer
, X_OK
)==0) {
1416 /* Directory is accesible. Add to directory list */
1418 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1419 data
->name
= wstrdup(dentry
->d_name
);
1422 WMAddToArray(dirs
, data
);
1424 } else if (S_ISREG(stat_buf
.st_mode
) || isFilePack
) {
1425 /* Hack because access always returns X_OK success for user root */
1426 #define S_IXANY (S_IXUSR | S_IXGRP | S_IXOTH)
1427 if ((command
!=NULL
&& access(buffer
, R_OK
)==0) ||
1428 (command
==NULL
&& access(buffer
, X_OK
)==0 &&
1429 (stat_buf
.st_mode
& S_IXANY
))) {
1431 data
= (dir_data
*) wmalloc(sizeof(dir_data
));
1432 data
->name
= wstrdup(dentry
->d_name
);
1435 WMAddToArray(files
, data
);
1446 if (!WMGetArrayItemCount(dirs
) && !WMGetArrayItemCount(files
)) {
1452 WMSortArray(dirs
, myCompare
);
1453 WMSortArray(files
, myCompare
);
1455 menu
= wMenuCreate(scr
, title
, False
);
1456 menu
->on_destroy
= removeShortcutsForMenu
;
1458 WM_ITERATE_ARRAY(dirs
, data
, iter
) {
1459 /* New directory. Use same OPEN_MENU command that was used
1460 * for the current directory. */
1461 length
= strlen(path
[data
->index
])+strlen(data
->name
)+6;
1465 length
+= strlen(command
) + 6;
1466 buffer
= malloc(length
);
1468 wsyserror(_("out of memory while constructing directory menu %s"),
1475 strcat(buffer
, "-noext ");
1477 have_space
= strchr(path
[data
->index
], ' ')!=NULL
||
1478 strchr(data
->name
, ' ')!=NULL
;
1481 strcat(buffer
, "\"");
1482 strcat(buffer
, path
[data
->index
]);
1484 strcat(buffer
, "/");
1485 strcat(buffer
, data
->name
);
1487 strcat(buffer
, "\"");
1489 strcat(buffer
, " WITH ");
1490 strcat(buffer
, command
);
1493 addMenuEntry(menu
, data
->name
, NULL
, "OPEN_MENU", buffer
, path
[data
->index
]);
1501 WM_ITERATE_ARRAY(files
, data
, iter
) {
1502 /* executable: add as entry */
1503 length
= strlen(path
[data
->index
])+strlen(data
->name
)+6;
1505 length
+= strlen(command
);
1507 buffer
= malloc(length
);
1509 wsyserror(_("out of memory while constructing directory menu %s"),
1514 have_space
= strchr(path
[data
->index
], ' ')!=NULL
||
1515 strchr(data
->name
, ' ')!=NULL
;
1516 if (command
!=NULL
) {
1517 strcpy(buffer
, command
);
1518 strcat(buffer
, " ");
1520 strcat(buffer
, "\"");
1521 strcat(buffer
, path
[data
->index
]);
1526 strcat(buffer
, path
[data
->index
]);
1528 strcpy(buffer
, path
[data
->index
]);
1531 strcat(buffer
, "/");
1532 strcat(buffer
, data
->name
);
1534 strcat(buffer
, "\"");
1536 if (stripExtension
) {
1537 char *ptr
= strrchr(data
->name
, '.');
1538 if (ptr
&& ptr
!=data
->name
)
1541 addMenuEntry(menu
, data
->name
, NULL
, "SHEXEC", buffer
, path
[data
->index
]);
1556 /************ Menu Configuration From WMRootMenu *************/
1559 makeDefaultMenu(WScreen
*scr
)
1563 menu
= wMenuCreate(scr
, _("Commands"), True
);
1564 wMenuAddCallback(menu
, "XTerm", execCommand
, "xterm");
1565 wMenuAddCallback(menu
, "rxvt", execCommand
, "rxvt");
1566 wMenuAddCallback(menu
, _("Restart"), restartCommand
, NULL
);
1567 wMenuAddCallback(menu
, _("Exit..."), exitCommand
, NULL
);
1576 *----------------------------------------------------------------------
1578 * Reads root menu configuration from defaults database.
1580 *----------------------------------------------------------------------
1583 configureMenu(WScreen
*scr
, WMPropList
*definition
)
1588 WMPropList
*title
, *command
, *params
;
1592 if (WMIsPLString(definition
)) {
1593 struct stat stat_buf
;
1595 Bool menu_is_default
= False
;
1597 /* menu definition is a string. Probably a path, so parse the file */
1599 tmp
= wexpandpath(WMGetFromPLString(definition
));
1601 path
= getLocalizedMenuFile(tmp
);
1604 path
= wfindfile(DEF_CONFIG_PATHS
, tmp
);
1607 path
= wfindfile(DEF_CONFIG_PATHS
, DEF_MENU_FILE
);
1608 menu_is_default
= True
;
1612 wsyserror(_("could not find menu file \"%s\" referenced in WMRootMenu"),
1618 if (stat(path
, &stat_buf
)<0) {
1619 wsyserror(_("could not access menu \"%s\" referenced in WMRootMenu"), path
);
1625 if (!scr
->root_menu
|| stat_buf
.st_mtime
> scr
->root_menu
->timestamp
1626 /* if the pointer in WMRootMenu has changed */
1627 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1629 if (menu_is_default
) {
1630 wwarning(_("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
1634 menu
= readMenuFile(scr
, path
);
1636 menu
->timestamp
= WMAX(stat_buf
.st_mtime
, WDRootMenu
->timestamp
);
1646 count
= WMGetPropListItemCount(definition
);
1650 elem
= WMGetFromPLArray(definition
, 0);
1651 if (!WMIsPLString(elem
)) {
1652 tmp
= WMGetPropListDescription(elem
, False
);
1653 wwarning(_("%s:format error in root menu configuration \"%s\""),
1658 mtitle
= WMGetFromPLString(elem
);
1660 menu
= wMenuCreate(scr
, mtitle
, False
);
1661 menu
->on_destroy
= removeShortcutsForMenu
;
1663 #ifdef GLOBAL_SUBMENU_FILE
1668 submenu
= readMenuFile(scr
, GLOBAL_SUBMENU_FILE
);
1671 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
1672 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1677 for (i
=1; i
<count
; i
++) {
1678 elem
= WMGetFromPLArray(definition
, i
);
1680 if (WMIsPLString(elem
)) {
1683 file
= WMGetFromPLString(elem
);
1687 if (!WMIsPLArray(elem
) || WMGetPropListItemCount(elem
) < 2)
1690 if (WMIsPLArray(WMGetFromPLArray(elem
,1))) {
1695 submenu
= configureMenu(scr
, elem
);
1697 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
,
1699 wMenuEntrySetCascade(menu
, mentry
, submenu
);
1703 WMPropList
*shortcut
;
1706 title
= WMGetFromPLArray(elem
, idx
++);
1707 shortcut
= WMGetFromPLArray(elem
, idx
++);
1708 if (strcmp(WMGetFromPLString(shortcut
), "SHORTCUT")==0) {
1709 shortcut
= WMGetFromPLArray(elem
, idx
++);
1710 command
= WMGetFromPLArray(elem
, idx
++);
1715 params
= WMGetFromPLArray(elem
, idx
++);
1717 if (!title
|| !command
)
1720 addMenuEntry(menu
, WMGetFromPLString(title
),
1721 shortcut
? WMGetFromPLString(shortcut
) : NULL
,
1722 WMGetFromPLString(command
),
1723 params
? WMGetFromPLString(params
) : NULL
, "WMRootMenu");
1728 tmp
= WMGetPropListDescription(elem
, False
);
1729 wwarning(_("%s:format error in root menu configuration \"%s\""),
1745 *----------------------------------------------------------------------
1747 * Opens the root menu, parsing the menu configuration from the
1748 * defaults database.
1749 * If the menu is already mapped and is not sticked to the
1750 * root window, it will be unmapped.
1753 * The menu may be remade.
1756 * Construction of OPEN_MENU entries are delayed to the moment the
1758 *----------------------------------------------------------------------
1761 OpenRootMenu(WScreen
*scr
, int x
, int y
, int keyboard
)
1764 WMPropList
*definition
;
1766 static WMPropList *domain=NULL;
1769 domain = WMCreatePLString("WMRootMenu");
1773 scr
->flags
.root_menu_changed_shortcuts
= 0;
1774 scr
->flags
.added_workspace_menu
= 0;
1775 scr
->flags
.added_windows_menu
= 0;
1777 if (scr
->root_menu
&& scr
->root_menu
->flags
.mapped
) {
1778 menu
= scr
->root_menu
;
1779 if (!menu
->flags
.buttoned
) {
1782 wRaiseFrame(menu
->frame
->core
);
1785 wMenuMapAt(menu
, 0, 0, True
);
1787 wMenuMapCopyAt(menu
, x
-menu
->frame
->core
->width
/2, y
);
1793 definition
= WDRootMenu
->dictionary
;
1796 definition = PLGetDomain(domain);
1799 if (WMIsPLArray(definition
)) {
1801 || WDRootMenu
->timestamp
> scr
->root_menu
->timestamp
) {
1802 menu
= configureMenu(scr
, definition
);
1804 menu
->timestamp
= WDRootMenu
->timestamp
;
1809 menu
= configureMenu(scr
, definition
);
1814 /* menu hasn't changed or could not be read */
1815 if (!scr
->root_menu
) {
1816 wMessageDialog(scr
, _("Error"),
1817 _("The applications menu could not be loaded. "
1818 "Look at the console output for a detailed "
1819 "description of the errors."),
1820 _("OK"), NULL
, NULL
);
1822 menu
= makeDefaultMenu(scr
);
1823 scr
->root_menu
= menu
;
1825 menu
= scr
->root_menu
;
1828 if (scr
->root_menu
) {
1829 wMenuDestroy(scr
->root_menu
, True
);
1831 scr
->root_menu
= menu
;
1836 if (keyboard
&& x
==0 && y
==0) {
1838 } else if (keyboard
&& x
==scr
->scr_width
/2 && y
==scr
->scr_height
/2) {
1839 newx
= x
- menu
->frame
->core
->width
/2;
1840 newy
= y
- menu
->frame
->core
->height
/2;
1842 newx
= x
- menu
->frame
->core
->width
/2;
1845 wMenuMapAt(menu
, newx
, newy
, keyboard
);
1848 if (scr
->flags
.root_menu_changed_shortcuts
)
1849 rebindKeygrabs(scr
);