1 /* usermenu.c- user defined menu
3 * Window Maker window manager
5 * Copyright (c) hmmm... Should I put everybody's name here?
6 * Where's my lawyer?? -- ]d :D
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 along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * User defined menu is good, but beer's always better
24 * if someone wanna start hacking something, He heard...
26 * - enhance commands. (eg, exit, hide, list all app's member
28 * - cache menu... dunno.. if people really use this feature :P
29 * - Violins, senseless violins!
30 * that's all, right now :P
31 * - external! WINGs menu editor.
33 * - allow applications to share their menu. ] think it
34 * looks wierd since there still are more than 1 appicon.
39 * ("Command 1", SHORTCUT, 1),
40 * ("Command 2", SHORTCUT, 2, ("Allowed_instant_1", "Allowed_instant_2")),
41 * ("Command 3", SHORTCUT, (3,4,5), ("Allowed_instant_1")),
44 * ("Kill Command", KILL),
45 * ("Hide Command", HIDE),
46 * ("Hide Others Command", HIDE_OTHERS),
47 * ("Members", MEMBERS),
48 * ("Exit Command", EXIT)
53 * - If you don't want short cut keys to be listed
54 * in the right side of entries, you can just put them
55 * in array instead of using the string directly.
64 #include <X11/Xutil.h>
65 #include <X11/Xproto.h>
66 #include <X11/Xatom.h>
72 #include "WindowMaker.h"
76 #include "xmodifier.h"
80 #define MAX_SHORTCUT_LENGTH 32
83 extern WPreferences wPreferences
;
91 static void notifyClient(WMenu
* menu
, WMenuEntry
* entry
)
94 WUserMenuData
*data
= entry
->clientdata
;
95 WScreen
*scr
= data
->screen
;
99 window
= scr
->focused_window
->client_win
;
101 for (i
= 0; i
< data
->key_no
; i
++) {
102 event
.xkey
.type
= KeyPress
;
103 event
.xkey
.display
= dpy
;
104 event
.xkey
.window
= window
;
105 event
.xkey
.root
= DefaultRootWindow(dpy
);
106 event
.xkey
.subwindow
= (Window
) None
;
109 event
.xkey
.x_root
= 0x0;
110 event
.xkey
.y_root
= 0x0;
111 event
.xkey
.keycode
= data
->key
[i
].keycode
;
112 event
.xkey
.state
= data
->key
[i
].modifier
;
113 event
.xkey
.same_screen
= True
;
114 event
.xkey
.time
= CurrentTime
;
115 if (XSendEvent(dpy
, window
, False
, KeyPressMask
, &event
)) {
116 event
.xkey
.type
= KeyRelease
;
117 event
.xkey
.time
= CurrentTime
;
118 XSendEvent(dpy
, window
, True
, KeyReleaseMask
, &event
);
123 static void removeUserMenudata(void *menudata
)
125 WUserMenuData
*data
= menudata
;
132 static WUserMenuData
*convertShortcuts(WScreen
* scr
, WMPropList
* shortcut
)
136 char *k
, buf
[MAX_SHORTCUT_LENGTH
], *b
;
137 int keycount
, i
, j
, mod
;
139 if (WMIsPLString(shortcut
)) {
141 } else if (WMIsPLArray(shortcut
)) {
142 keycount
= WMGetPropListItemCount(shortcut
);
147 data
= wmalloc(sizeof(WUserMenuData
));
151 data
->key
= wmalloc(sizeof(WShortKey
) * keycount
);
157 for (i
= 0, j
= 0; i
< keycount
; i
++) {
158 data
->key
[j
].modifier
= 0;
159 if (WMIsPLArray(shortcut
))
160 wstrlcpy(buf
, WMGetFromPLString(WMGetFromPLArray(shortcut
, i
)), MAX_SHORTCUT_LENGTH
);
162 wstrlcpy(buf
, WMGetFromPLString(shortcut
), MAX_SHORTCUT_LENGTH
);
166 while ((k
= strchr(b
, '+')) != NULL
) {
168 mod
= wXModifierFromKey(b
);
172 data
->key
[j
].modifier
|= mod
;
176 ksym
= XStringToKeysym(b
);
177 if (ksym
== NoSymbol
)
180 data
->key
[j
].keycode
= XKeysymToKeycode(dpy
, ksym
);
181 if (data
->key
[j
].keycode
)
200 static WMenu
*configureUserMenu(WScreen
* scr
, WMPropList
* plum
)
204 WMPropList
*elem
, *title
, *command
, *params
;
211 if (!WMIsPLArray(plum
))
214 count
= WMGetPropListItemCount(plum
);
218 elem
= WMGetFromPLArray(plum
, 0);
219 if (!WMIsPLString(elem
))
222 mtitle
= WMGetFromPLString(elem
);
224 menu
= wMenuCreateForApp(scr
, mtitle
, True
);
226 for (i
= 1; i
< count
; i
++) {
227 elem
= WMGetFromPLArray(plum
, i
);
228 if (WMIsPLArray(WMGetFromPLArray(elem
, 1))) {
232 submenu
= configureUserMenu(scr
, elem
);
234 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
235 wMenuEntrySetCascade(menu
, mentry
, submenu
);
238 WMPropList
*instances
= 0;
240 title
= WMGetFromPLArray(elem
, idx
++);
241 command
= WMGetFromPLArray(elem
, idx
++);
242 if (WMGetPropListItemCount(elem
) >= 3)
243 params
= WMGetFromPLArray(elem
, idx
++);
245 if (!title
|| !command
)
248 if (!strcmp("SHORTCUT", WMGetFromPLString(command
))) {
251 data
= convertShortcuts(scr
, params
);
253 entry
= wMenuAddCallback(menu
, WMGetFromPLString(title
),
257 if (WMIsPLString(params
))
259 GetShortcutString(WMGetFromPLString(params
));
261 entry
->free_cdata
= removeUserMenudata
;
263 if (WMGetPropListItemCount(elem
) >= 4) {
264 instances
= WMGetFromPLArray(elem
, idx
++);
265 if (WMIsPLArray(instances
))
266 if (instances
&& WMGetPropListItemCount(instances
)
267 && WMIsPLArray(instances
)) {
269 WMRetainPropList(instances
);
281 void wUserMenuRefreshInstances(WMenu
* menu
, WWindow
* wwin
)
284 int i
, j
, count
, paintflag
;
291 for (i
= 0; i
< menu
->entry_no
; i
++) {
292 if (menu
->entries
[i
]->instances
) {
295 count
= WMGetPropListItemCount(menu
->entries
[i
]->instances
);
297 oldflag
= menu
->entries
[i
]->flags
.enabled
;
298 menu
->entries
[i
]->flags
.enabled
= 0;
299 for (j
= 0; j
< count
; j
++) {
300 ins
= WMGetFromPLArray(menu
->entries
[i
]->instances
, j
);
301 if (!strcmp(wwin
->wm_instance
, WMGetFromPLString(ins
))) {
302 menu
->entries
[i
]->flags
.enabled
= 1;
306 if (oldflag
!= menu
->entries
[i
]->flags
.enabled
)
310 for (i
= 0; i
< menu
->cascade_no
; i
++) {
311 if (!menu
->cascades
[i
]->flags
.brother
)
312 wUserMenuRefreshInstances(menu
->cascades
[i
], wwin
);
314 wUserMenuRefreshInstances(menu
->cascades
[i
]->brother
, wwin
);
321 static WMenu
*readUserMenuFile(WScreen
*scr
, const char *file_name
)
326 plum
= WMReadPropListFromFile(file_name
);
328 menu
= configureUserMenu(scr
, plum
);
329 WMReleasePropList(plum
);
334 WMenu
*wUserMenuGet(WScreen
* scr
, WWindow
* wwin
)
339 if (wwin
->wm_instance
&& wwin
->wm_class
) {
340 int len
= strlen(wwin
->wm_instance
) + strlen(wwin
->wm_class
) + 7;
342 snprintf(tmp
, len
, "%s.%s.menu", wwin
->wm_instance
, wwin
->wm_class
);
343 path
= wfindfile(DEF_USER_MENU_PATHS
, tmp
);
350 menu
= readUserMenuFile(scr
, path
);
357 #endif /* USER_MENU */