Revert "Prevent windows from drifting on restart."
[wmaker-crm.git] / src / usermenu.c
blob22d089f6b03d75b215e5e3d4430771fda3a80d96
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 "funcs.h"
76 #include "keybind.h"
77 #include "xmodifier.h"
79 #include "framewin.h"
81 #define MAX_SHORTCUT_LENGTH 32
83 /*** var ***/
84 extern WPreferences wPreferences;
86 typedef struct {
87 WScreen *screen;
88 WShortKey *key;
89 int key_no;
90 } WUserMenuData;
92 static void notifyClient(WMenu * menu, WMenuEntry * entry)
94 XEvent event;
95 WUserMenuData *data = entry->clientdata;
96 WScreen *scr = data->screen;
97 Window window;
98 int i;
100 window = scr->focused_window->client_win;
102 for (i = 0; i < data->key_no; i++) {
103 event.xkey.type = KeyPress;
104 event.xkey.display = dpy;
105 event.xkey.window = window;
106 event.xkey.root = DefaultRootWindow(dpy);
107 event.xkey.subwindow = (Window) None;
108 event.xkey.x = 0x0;
109 event.xkey.y = 0x0;
110 event.xkey.x_root = 0x0;
111 event.xkey.y_root = 0x0;
112 event.xkey.keycode = data->key[i].keycode;
113 event.xkey.state = data->key[i].modifier;
114 event.xkey.same_screen = True;
115 event.xkey.time = CurrentTime;
116 if (XSendEvent(dpy, window, False, KeyPressMask, &event)) {
117 event.xkey.type = KeyRelease;
118 event.xkey.time = CurrentTime;
119 XSendEvent(dpy, window, True, KeyReleaseMask, &event);
124 static void removeUserMenudata(void *menudata)
126 WUserMenuData *data = menudata;
127 if (data->key)
128 wfree(data->key);
130 wfree(data);
133 static WUserMenuData *convertShortcuts(WScreen * scr, WMPropList * shortcut)
135 WUserMenuData *data;
136 KeySym ksym;
137 char *k, buf[MAX_SHORTCUT_LENGTH], *b;
138 int keycount, i, j, mod;
140 if (WMIsPLString(shortcut)) {
141 keycount = 1;
142 } else if (WMIsPLArray(shortcut)) {
143 keycount = WMGetPropListItemCount(shortcut);
144 } else {
145 return NULL;
148 data = wmalloc(sizeof(WUserMenuData));
149 if (!data)
150 return NULL;
152 data->key = wmalloc(sizeof(WShortKey) * keycount);
153 if (!data->key) {
154 wfree(data);
155 return NULL;
158 for (i = 0, j = 0; i < keycount; i++) {
159 data->key[j].modifier = 0;
160 if (WMIsPLArray(shortcut))
161 wstrlcpy(buf, WMGetFromPLString(WMGetFromPLArray(shortcut, i)), MAX_SHORTCUT_LENGTH);
162 else
163 wstrlcpy(buf, WMGetFromPLString(shortcut), MAX_SHORTCUT_LENGTH);
165 b = (char *)buf;
167 while ((k = strchr(b, '+')) != NULL) {
168 *k = 0;
169 mod = wXModifierFromKey(b);
170 if (mod < 0)
171 break;
173 data->key[j].modifier |= mod;
174 b = k + 1;
177 ksym = XStringToKeysym(b);
178 if (ksym == NoSymbol)
179 continue;
181 data->key[j].keycode = XKeysymToKeycode(dpy, ksym);
182 if (data->key[j].keycode)
183 j++;
186 keyover:
188 /* get key */
189 if (!j) {
190 puts("fatal j");
191 wfree(data->key);
192 wfree(data);
193 return NULL;
195 data->key_no = j;
196 data->screen = scr;
198 return data;
201 static WMenu *configureUserMenu(WScreen * scr, WMPropList * plum)
203 char *mtitle;
204 WMenu *menu = NULL;
205 WMPropList *elem, *title, *command, *params;
206 int count, i;
207 WUserMenuData *data;
209 if (!plum)
210 return NULL;
212 if (!WMIsPLArray(plum))
213 return NULL;
215 count = WMGetPropListItemCount(plum);
216 if (!count)
217 return NULL;
219 elem = WMGetFromPLArray(plum, 0);
220 if (!WMIsPLString(elem))
221 return NULL;
223 mtitle = WMGetFromPLString(elem);
225 menu = wMenuCreateForApp(scr, mtitle, True);
227 for (i = 1; i < count; i++) {
228 elem = WMGetFromPLArray(plum, i);
229 if (WMIsPLArray(WMGetFromPLArray(elem, 1))) {
230 WMenu *submenu;
231 WMenuEntry *mentry;
233 submenu = configureUserMenu(scr, elem);
234 if (submenu)
235 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL, NULL);
236 wMenuEntrySetCascade(menu, mentry, submenu);
237 } else {
238 int idx = 0;
239 WMPropList *instances = 0;
241 title = WMGetFromPLArray(elem, idx++);
242 command = WMGetFromPLArray(elem, idx++);
243 if (WMGetPropListItemCount(elem) >= 3)
244 params = WMGetFromPLArray(elem, idx++);
246 if (!title || !command)
247 return menu;
249 if (!strcmp("SHORTCUT", WMGetFromPLString(command))) {
250 WMenuEntry *entry;
252 data = convertShortcuts(scr, params);
253 if (data) {
254 entry = wMenuAddCallback(menu, WMGetFromPLString(title),
255 notifyClient, data);
257 if (entry) {
258 if (WMIsPLString(params))
259 entry->rtext =
260 GetShortcutString(WMGetFromPLString(params));
262 entry->free_cdata = removeUserMenudata;
264 if (WMGetPropListItemCount(elem) >= 4) {
265 instances = WMGetFromPLArray(elem, idx++);
266 if (WMIsPLArray(instances))
267 if (instances && WMGetPropListItemCount(instances)
268 && WMIsPLArray(instances)) {
269 entry->instances =
270 WMRetainPropList(instances);
279 return menu;
282 void wUserMenuRefreshInstances(WMenu * menu, WWindow * wwin)
284 WMenuEntry *entry;
285 int i, j, count, paintflag;
287 paintflag = 0;
289 if (!menu)
290 return;
292 for (i = 0; i < menu->entry_no; i++) {
293 if (menu->entries[i]->instances) {
294 WMPropList *ins;
295 int oldflag;
296 count = WMGetPropListItemCount(menu->entries[i]->instances);
298 oldflag = menu->entries[i]->flags.enabled;
299 menu->entries[i]->flags.enabled = 0;
300 for (j = 0; j < count; j++) {
301 ins = WMGetFromPLArray(menu->entries[i]->instances, j);
302 if (!strcmp(wwin->wm_instance, WMGetFromPLString(ins))) {
303 menu->entries[i]->flags.enabled = 1;
304 break;
307 if (oldflag != menu->entries[i]->flags.enabled)
308 paintflag = 1;
311 for (i = 0; i < menu->cascade_no; i++) {
312 if (!menu->cascades[i]->flags.brother)
313 wUserMenuRefreshInstances(menu->cascades[i], wwin);
314 else
315 wUserMenuRefreshInstances(menu->cascades[i]->brother, wwin);
318 if (paintflag)
319 wMenuPaint(menu);
322 static WMenu *readUserMenuFile(WScreen * scr, char *file_name)
324 WMenu *menu = NULL;
325 WMPropList *plum;
327 plum = WMReadPropListFromFile(file_name);
328 if (plum) {
329 menu = configureUserMenu(scr, plum);
330 WMReleasePropList(plum);
332 return menu;
335 WMenu *wUserMenuGet(WScreen * scr, WWindow * wwin)
337 WMenu *menu = NULL;
338 char *path = NULL;
339 char *tmp;
340 if (wwin->wm_instance && wwin->wm_class) {
341 int len = strlen(wwin->wm_instance) + strlen(wwin->wm_class) + 7;
342 tmp = wmalloc(len);
343 snprintf(tmp, len, "%s.%s.menu", wwin->wm_instance, wwin->wm_class);
344 path = wfindfile(DEF_USER_MENU_PATHS, tmp);
345 wfree(tmp);
347 if (!path)
348 return NULL;
350 if (wwin)
351 menu = readUserMenuFile(scr, path);
353 wfree(path);
355 return menu;
358 #endif /* USER_MENU */