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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
24 * User defined menu is good, but beer's always better
25 * if someone wanna start hacking something, He heard...
27 * - enhance commands. (eg, exit, hide, list all app's member
29 * - cache menu... dunno.. if people really use this feature :P
30 * - Violins, senseless violins!
31 * that's all, right now :P
32 * - external! WINGs menu editor.
34 * - allow applications to share their menu. ] think it
35 * looks wierd since there still are more than 1 appicon.
40 * ("Command 1", SHORTCUT, 1),
41 * ("Command 2", SHORTCUT, 2, ("Allowed_instant_1", "Allowed_instant_2")),
42 * ("Command 3", SHORTCUT, (3,4,5), ("Allowed_instant_1")),
45 * ("Kill Command", KILL),
46 * ("Hide Command", HIDE),
47 * ("Hide Others Command", HIDE_OTHERS),
48 * ("Members", MEMBERS),
49 * ("Exit Command", EXIT)
54 * - If you don't want short cut keys to be listed
55 * in the right side of entries, you can just put them
56 * in array instead of using the string directly.
65 #include <X11/Xutil.h>
66 #include <X11/Xproto.h>
67 #include <X11/Xatom.h>
73 #include "WindowMaker.h"
82 #define MAX_SHORTCUT_LENGTH 32
85 extern WPreferences wPreferences
;
93 static void notifyClient(WMenu
* menu
, WMenuEntry
* entry
)
96 WUserMenuData
*data
= entry
->clientdata
;
97 WScreen
*scr
= data
->screen
;
101 window
= scr
->focused_window
->client_win
;
103 for (i
= 0; i
< data
->key_no
; i
++) {
104 event
.xkey
.type
= KeyPress
;
105 event
.xkey
.display
= dpy
;
106 event
.xkey
.window
= window
;
107 event
.xkey
.root
= DefaultRootWindow(dpy
);
108 event
.xkey
.subwindow
= (Window
) None
;
111 event
.xkey
.x_root
= 0x0;
112 event
.xkey
.y_root
= 0x0;
113 event
.xkey
.keycode
= data
->key
[i
].keycode
;
114 event
.xkey
.state
= data
->key
[i
].modifier
;
115 event
.xkey
.same_screen
= True
;
116 event
.xkey
.time
= CurrentTime
;
117 if (XSendEvent(dpy
, window
, False
, KeyPressMask
, &event
)) {
118 event
.xkey
.type
= KeyRelease
;
119 event
.xkey
.time
= CurrentTime
;
120 XSendEvent(dpy
, window
, True
, KeyReleaseMask
, &event
);
125 static void removeUserMenudata(void *menudata
)
127 WUserMenuData
*data
= menudata
;
133 static WUserMenuData
*convertShortcuts(WScreen
* scr
, WMPropList
* shortcut
)
139 char buf
[MAX_SHORTCUT_LENGTH
], *b
;
140 int keycount
, i
, j
, mod
;
142 if (WMIsPLString(shortcut
)) {
144 } else if (WMIsPLArray(shortcut
)) {
145 keycount
= WMGetPropListItemCount(shortcut
);
148 /*for (i=0;i<keycount;i++){ */
150 data
= wmalloc(sizeof(WUserMenuData
));
153 data
->key
= wmalloc(sizeof(WShortKey
) * keycount
);
159 for (i
= 0, j
= 0; i
< keycount
; i
++) {
160 data
->key
[j
].modifier
= 0;
161 if (WMIsPLArray(shortcut
)) {
162 strncpy(buf
, WMGetFromPLString(WMGetFromPLArray(shortcut
, i
)), MAX_SHORTCUT_LENGTH
);
164 strncpy(buf
, WMGetFromPLString(shortcut
), MAX_SHORTCUT_LENGTH
);
168 while ((k
= strchr(b
, '+')) != NULL
) {
170 mod
= wXModifierFromKey(b
);
174 data
->key
[j
].modifier
|= mod
;
178 ksym
= XStringToKeysym(b
);
179 if (ksym
== NoSymbol
) {
183 data
->key
[j
].keycode
= XKeysymToKeycode(dpy
, ksym
);
184 if (data
->key
[j
].keycode
) {
204 static WMenu
*configureUserMenu(WScreen
* scr
, WMPropList
* plum
)
208 WMPropList
*elem
, *title
, *command
, *params
;
214 if (!WMIsPLArray(plum
)) {
218 count
= WMGetPropListItemCount(plum
);
222 elem
= WMGetFromPLArray(plum
, 0);
223 if (!WMIsPLString(elem
)) {
227 mtitle
= WMGetFromPLString(elem
);
229 menu
= wMenuCreateForApp(scr
, mtitle
, True
);
231 for (i
= 1; i
< count
; i
++) {
232 elem
= WMGetFromPLArray(plum
, i
);
233 if (WMIsPLArray(WMGetFromPLArray(elem
, 1))) {
237 submenu
= configureUserMenu(scr
, elem
);
239 mentry
= wMenuAddCallback(menu
, submenu
->frame
->title
, NULL
, NULL
);
240 wMenuEntrySetCascade(menu
, mentry
, submenu
);
243 WMPropList
*instances
= 0;
245 title
= WMGetFromPLArray(elem
, idx
++);
246 command
= WMGetFromPLArray(elem
, idx
++);
247 if (WMGetPropListItemCount(elem
) >= 3)
248 params
= WMGetFromPLArray(elem
, idx
++);
250 if (!title
|| !command
)
253 if (!strcmp("SHORTCUT", WMGetFromPLString(command
))) {
256 data
= convertShortcuts(scr
, params
);
258 entry
= wMenuAddCallback(menu
, WMGetFromPLString(title
),
262 if (WMIsPLString(params
)) {
264 GetShortcutString(WMGetFromPLString(params
));
266 entry
->free_cdata
= removeUserMenudata
;
268 if (WMGetPropListItemCount(elem
) >= 4) {
269 instances
= WMGetFromPLArray(elem
, idx
++);
270 if (WMIsPLArray(instances
))
271 if (instances
&& WMGetPropListItemCount(instances
)
272 && WMIsPLArray(instances
)) {
274 WMRetainPropList(instances
);
286 void wUserMenuRefreshInstances(WMenu
* menu
, WWindow
* wwin
)
289 int i
, j
, count
, paintflag
;
296 for (i
= 0; i
< menu
->entry_no
; i
++) {
297 if (menu
->entries
[i
]->instances
) {
300 count
= WMGetPropListItemCount(menu
->entries
[i
]->instances
);
302 oldflag
= menu
->entries
[i
]->flags
.enabled
;
303 menu
->entries
[i
]->flags
.enabled
= 0;
304 for (j
= 0; j
< count
; j
++) {
305 ins
= WMGetFromPLArray(menu
->entries
[i
]->instances
, j
);
306 if (!strcmp(wwin
->wm_instance
, WMGetFromPLString(ins
))) {
307 menu
->entries
[i
]->flags
.enabled
= 1;
311 if (oldflag
!= menu
->entries
[i
]->flags
.enabled
)
315 for (i
= 0; i
< menu
->cascade_no
; i
++) {
316 if (!menu
->cascades
[i
]->flags
.brother
)
317 wUserMenuRefreshInstances(menu
->cascades
[i
], wwin
);
319 wUserMenuRefreshInstances(menu
->cascades
[i
]->brother
, wwin
);
326 static WMenu
*readUserMenuFile(WScreen
* scr
, char *file_name
)
330 WMPropList
*plum
, *elem
, *title
, *command
, *params
;
334 plum
= WMReadPropListFromFile(file_name
);
336 menu
= configureUserMenu(scr
, plum
);
337 WMReleasePropList(plum
);
342 WMenu
*wUserMenuGet(WScreen
* scr
, WWindow
* wwin
)
348 if (wwin
->wm_instance
&& wwin
->wm_class
) {
349 int len
= strlen(wwin
->wm_instance
) + strlen(wwin
->wm_class
) + 7;
351 snprintf(tmp
, len
, "%s.%s.menu", wwin
->wm_instance
, wwin
->wm_class
);
352 path
= wfindfile(DEF_USER_MENU_PATHS
, tmp
);
359 menu
= readUserMenuFile(scr
, path
);
367 #endif /* USER_MENU */