wmaker: Removed unused argument in function 'wDockFinishLaunch'
[wmaker-crm.git] / src / usermenu.c
blobd3f6fccca4d337f5a8e8b1209c055b755f0fcf52
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.
22 * * * * * * * * *
23 * User defined menu is good, but beer's always better
24 * if someone wanna start hacking something, He heard...
25 * TODO
26 * - enhance commands. (eg, exit, hide, list all app's member
27 * window and etc)
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.
32 * TODONOT
33 * - allow applications to share their menu. ] think it
34 * looks wierd since there still are more than 1 appicon.
36 * Syntax...
37 * (
38 * "Program Name",
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")),
42 * (
43 * "Submenu",
44 * ("Kill Command", KILL),
45 * ("Hide Command", HIDE),
46 * ("Hide Others Command", HIDE_OTHERS),
47 * ("Members", MEMBERS),
48 * ("Exit Command", EXIT)
49 * )
50 * )
52 * Tips:
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.
59 #include "wconfig.h"
61 #ifdef USER_MENU
63 #include <X11/Xlib.h>
64 #include <X11/Xutil.h>
65 #include <X11/Xproto.h>
66 #include <X11/Xatom.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <stdio.h>
70 #include <unistd.h>
72 #include "WindowMaker.h"
73 #include "menu.h"
74 #include "actions.h"
75 #include "keybind.h"
76 #include "xmodifier.h"
78 #include "framewin.h"
80 #define MAX_SHORTCUT_LENGTH 32
83 typedef struct {
84 WScreen *screen;
85 WShortKey *key;
86 int key_no;
87 } WUserMenuData;
89 static void notifyClient(WMenu * menu, WMenuEntry * entry)
91 XEvent event;
92 WUserMenuData *data = entry->clientdata;
93 WScreen *scr = data->screen;
94 Window window;
95 int i;
97 window = scr->focused_window->client_win;
99 for (i = 0; i < data->key_no; i++) {
100 event.xkey.type = KeyPress;
101 event.xkey.display = dpy;
102 event.xkey.window = window;
103 event.xkey.root = DefaultRootWindow(dpy);
104 event.xkey.subwindow = (Window) None;
105 event.xkey.x = 0x0;
106 event.xkey.y = 0x0;
107 event.xkey.x_root = 0x0;
108 event.xkey.y_root = 0x0;
109 event.xkey.keycode = data->key[i].keycode;
110 event.xkey.state = data->key[i].modifier;
111 event.xkey.same_screen = True;
112 event.xkey.time = CurrentTime;
113 if (XSendEvent(dpy, window, False, KeyPressMask, &event)) {
114 event.xkey.type = KeyRelease;
115 event.xkey.time = CurrentTime;
116 XSendEvent(dpy, window, True, KeyReleaseMask, &event);
121 static void removeUserMenudata(void *menudata)
123 WUserMenuData *data = menudata;
124 if (data->key)
125 wfree(data->key);
127 wfree(data);
130 static WUserMenuData *convertShortcuts(WScreen * scr, WMPropList * shortcut)
132 WUserMenuData *data;
133 KeySym ksym;
134 char *k, buf[MAX_SHORTCUT_LENGTH], *b;
135 int keycount, i, j, mod;
137 if (WMIsPLString(shortcut)) {
138 keycount = 1;
139 } else if (WMIsPLArray(shortcut)) {
140 keycount = WMGetPropListItemCount(shortcut);
141 } else {
142 return NULL;
145 data = wmalloc(sizeof(WUserMenuData));
146 if (!data)
147 return NULL;
149 data->key = wmalloc(sizeof(WShortKey) * keycount);
150 if (!data->key) {
151 wfree(data);
152 return NULL;
155 for (i = 0, j = 0; i < keycount; i++) {
156 data->key[j].modifier = 0;
157 if (WMIsPLArray(shortcut))
158 wstrlcpy(buf, WMGetFromPLString(WMGetFromPLArray(shortcut, i)), MAX_SHORTCUT_LENGTH);
159 else
160 wstrlcpy(buf, WMGetFromPLString(shortcut), MAX_SHORTCUT_LENGTH);
162 b = (char *)buf;
164 while ((k = strchr(b, '+')) != NULL) {
165 *k = 0;
166 mod = wXModifierFromKey(b);
167 if (mod < 0)
168 break;
170 data->key[j].modifier |= mod;
171 b = k + 1;
174 ksym = XStringToKeysym(b);
175 if (ksym == NoSymbol)
176 continue;
178 data->key[j].keycode = XKeysymToKeycode(dpy, ksym);
179 if (data->key[j].keycode)
180 j++;
183 keyover:
185 /* get key */
186 if (!j) {
187 puts("fatal j");
188 wfree(data->key);
189 wfree(data);
190 return NULL;
192 data->key_no = j;
193 data->screen = scr;
195 return data;
198 static WMenu *configureUserMenu(WScreen * scr, WMPropList * plum)
200 char *mtitle;
201 WMenu *menu = NULL;
202 WMPropList *elem, *title, *command, *params;
203 int count, i;
204 WUserMenuData *data;
206 if (!plum)
207 return NULL;
209 if (!WMIsPLArray(plum))
210 return NULL;
212 count = WMGetPropListItemCount(plum);
213 if (!count)
214 return NULL;
216 elem = WMGetFromPLArray(plum, 0);
217 if (!WMIsPLString(elem))
218 return NULL;
220 mtitle = WMGetFromPLString(elem);
222 menu = wMenuCreateForApp(scr, mtitle, True);
224 for (i = 1; i < count; i++) {
225 elem = WMGetFromPLArray(plum, i);
226 if (WMIsPLArray(WMGetFromPLArray(elem, 1))) {
227 WMenu *submenu;
228 WMenuEntry *mentry;
230 submenu = configureUserMenu(scr, elem);
231 if (submenu)
232 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL, NULL);
233 wMenuEntrySetCascade(menu, mentry, submenu);
234 } else {
235 int idx = 0;
236 WMPropList *instances = 0;
238 title = WMGetFromPLArray(elem, idx++);
239 command = WMGetFromPLArray(elem, idx++);
240 if (WMGetPropListItemCount(elem) >= 3)
241 params = WMGetFromPLArray(elem, idx++);
243 if (!title || !command)
244 return menu;
246 if (!strcmp("SHORTCUT", WMGetFromPLString(command))) {
247 WMenuEntry *entry;
249 data = convertShortcuts(scr, params);
250 if (data) {
251 entry = wMenuAddCallback(menu, WMGetFromPLString(title),
252 notifyClient, data);
254 if (entry) {
255 if (WMIsPLString(params))
256 entry->rtext =
257 GetShortcutString(WMGetFromPLString(params));
259 entry->free_cdata = removeUserMenudata;
261 if (WMGetPropListItemCount(elem) >= 4) {
262 instances = WMGetFromPLArray(elem, idx++);
263 if (WMIsPLArray(instances))
264 if (instances && WMGetPropListItemCount(instances)
265 && WMIsPLArray(instances)) {
266 entry->instances =
267 WMRetainPropList(instances);
276 return menu;
279 void wUserMenuRefreshInstances(WMenu * menu, WWindow * wwin)
281 WMenuEntry *entry;
282 int i, j, count, paintflag;
284 paintflag = 0;
286 if (!menu)
287 return;
289 for (i = 0; i < menu->entry_no; i++) {
290 if (menu->entries[i]->instances) {
291 WMPropList *ins;
292 int oldflag;
293 count = WMGetPropListItemCount(menu->entries[i]->instances);
295 oldflag = menu->entries[i]->flags.enabled;
296 menu->entries[i]->flags.enabled = 0;
297 for (j = 0; j < count; j++) {
298 ins = WMGetFromPLArray(menu->entries[i]->instances, j);
299 if (!strcmp(wwin->wm_instance, WMGetFromPLString(ins))) {
300 menu->entries[i]->flags.enabled = 1;
301 break;
304 if (oldflag != menu->entries[i]->flags.enabled)
305 paintflag = 1;
308 for (i = 0; i < menu->cascade_no; i++) {
309 if (!menu->cascades[i]->flags.brother)
310 wUserMenuRefreshInstances(menu->cascades[i], wwin);
311 else
312 wUserMenuRefreshInstances(menu->cascades[i]->brother, wwin);
315 if (paintflag)
316 wMenuPaint(menu);
319 static WMenu *readUserMenuFile(WScreen *scr, const char *file_name)
321 WMenu *menu = NULL;
322 WMPropList *plum;
324 plum = WMReadPropListFromFile(file_name);
325 if (plum) {
326 menu = configureUserMenu(scr, plum);
327 WMReleasePropList(plum);
329 return menu;
332 WMenu *wUserMenuGet(WScreen * scr, WWindow * wwin)
334 WMenu *menu = NULL;
335 char *path = NULL;
336 char *tmp;
337 if (wwin->wm_instance && wwin->wm_class) {
338 int len = strlen(wwin->wm_instance) + strlen(wwin->wm_class) + 7;
339 tmp = wmalloc(len);
340 snprintf(tmp, len, "%s.%s.menu", wwin->wm_instance, wwin->wm_class);
341 path = wfindfile(DEF_USER_MENU_PATHS, tmp);
342 wfree(tmp);
344 if (!path)
345 return NULL;
347 if (wwin)
348 menu = readUserMenuFile(scr, path);
350 wfree(path);
352 return menu;
355 #endif /* USER_MENU */