Cleanup includes of wcore.h, defaults.h and pixmap.h
[wmaker-crm.git] / src / usermenu.c
blob781addceb624b7cb6dee13fad79960a77816caee
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,
21 * USA.
23 * * * * * * * * *
24 * User defined menu is good, but beer's always better
25 * if someone wanna start hacking something, He heard...
26 * TODO
27 * - enhance commands. (eg, exit, hide, list all app's member
28 * window and etc)
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.
33 * TODONOT
34 * - allow applications to share their menu. ] think it
35 * looks wierd since there still are more than 1 appicon.
37 * Syntax...
38 * (
39 * "Program Name",
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")),
43 * (
44 * "Submenu",
45 * ("Kill Command", KILL),
46 * ("Hide Command", HIDE),
47 * ("Hide Others Command", HIDE_OTHERS),
48 * ("Members", MEMBERS),
49 * ("Exit Command", EXIT)
50 * )
51 * )
53 * Tips:
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.
60 #include "wconfig.h"
62 #ifdef USER_MENU
64 #include <X11/Xlib.h>
65 #include <X11/Xutil.h>
66 #include <X11/Xproto.h>
67 #include <X11/Xatom.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <stdio.h>
71 #include <unistd.h>
73 #include "WindowMaker.h"
74 #include "menu.h"
75 #include "actions.h"
76 #include "funcs.h"
77 #include "keybind.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);
129 wfree(data);
132 static WUserMenuData *convertShortcuts(WScreen * scr, WMPropList * shortcut)
134 WUserMenuData *data;
135 KeySym ksym;
136 char *k;
137 char *buffer;
138 char buf[MAX_SHORTCUT_LENGTH], *b;
139 int keycount, i, j, mod;
141 if (WMIsPLString(shortcut)) {
142 keycount = 1;
143 } else if (WMIsPLArray(shortcut)) {
144 keycount = WMGetPropListItemCount(shortcut);
145 } else
146 return NULL;
147 /*for (i=0;i<keycount;i++){ */
149 data = wmalloc(sizeof(WUserMenuData));
150 if (!data)
151 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 strncpy(buf, WMGetFromPLString(WMGetFromPLArray(shortcut, i)), MAX_SHORTCUT_LENGTH);
162 } else {
163 strncpy(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;
182 data->key[j].keycode = XKeysymToKeycode(dpy, ksym);
183 if (data->key[j].keycode) {
184 j++;
188 keyover:
190 /* get key */
191 if (!j) {
192 puts("fatal j");
193 wfree(data->key);
194 wfree(data);
195 return NULL;
197 data->key_no = j;
198 data->screen = scr;
200 return data;
203 static WMenu *configureUserMenu(WScreen * scr, WMPropList * plum)
205 char *mtitle;
206 WMenu *menu = NULL;
207 WMPropList *elem, *title, *command, *params;
208 int count, i;
209 WUserMenuData *data;
211 if (!plum)
212 return NULL;
213 if (!WMIsPLArray(plum)) {
214 return NULL;
217 count = WMGetPropListItemCount(plum);
218 if (!count)
219 return NULL;
221 elem = WMGetFromPLArray(plum, 0);
222 if (!WMIsPLString(elem)) {
223 return NULL;
226 mtitle = WMGetFromPLString(elem);
228 menu = wMenuCreateForApp(scr, mtitle, True);
230 for (i = 1; i < count; i++) {
231 elem = WMGetFromPLArray(plum, i);
232 if (WMIsPLArray(WMGetFromPLArray(elem, 1))) {
233 WMenu *submenu;
234 WMenuEntry *mentry;
236 submenu = configureUserMenu(scr, elem);
237 if (submenu)
238 mentry = wMenuAddCallback(menu, submenu->frame->title, NULL, NULL);
239 wMenuEntrySetCascade(menu, mentry, submenu);
240 } else {
241 int idx = 0;
242 WMPropList *instances = 0;
244 title = WMGetFromPLArray(elem, idx++);
245 command = WMGetFromPLArray(elem, idx++);
246 if (WMGetPropListItemCount(elem) >= 3)
247 params = WMGetFromPLArray(elem, idx++);
249 if (!title || !command)
250 return menu;
252 if (!strcmp("SHORTCUT", WMGetFromPLString(command))) {
253 WMenuEntry *entry;
255 data = convertShortcuts(scr, params);
256 if (data) {
257 entry = wMenuAddCallback(menu, WMGetFromPLString(title),
258 notifyClient, data);
260 if (entry) {
261 if (WMIsPLString(params)) {
262 entry->rtext =
263 GetShortcutString(WMGetFromPLString(params));
265 entry->free_cdata = removeUserMenudata;
267 if (WMGetPropListItemCount(elem) >= 4) {
268 instances = WMGetFromPLArray(elem, idx++);
269 if (WMIsPLArray(instances))
270 if (instances && WMGetPropListItemCount(instances)
271 && WMIsPLArray(instances)) {
272 entry->instances =
273 WMRetainPropList(instances);
282 return menu;
285 void wUserMenuRefreshInstances(WMenu * menu, WWindow * wwin)
287 WMenuEntry *entry;
288 int i, j, count, paintflag;
290 paintflag = 0;
292 if (!menu)
293 return;
295 for (i = 0; i < menu->entry_no; i++) {
296 if (menu->entries[i]->instances) {
297 WMPropList *ins;
298 int oldflag;
299 count = WMGetPropListItemCount(menu->entries[i]->instances);
301 oldflag = menu->entries[i]->flags.enabled;
302 menu->entries[i]->flags.enabled = 0;
303 for (j = 0; j < count; j++) {
304 ins = WMGetFromPLArray(menu->entries[i]->instances, j);
305 if (!strcmp(wwin->wm_instance, WMGetFromPLString(ins))) {
306 menu->entries[i]->flags.enabled = 1;
307 break;
310 if (oldflag != menu->entries[i]->flags.enabled)
311 paintflag = 1;
314 for (i = 0; i < menu->cascade_no; i++) {
315 if (!menu->cascades[i]->flags.brother)
316 wUserMenuRefreshInstances(menu->cascades[i], wwin);
317 else
318 wUserMenuRefreshInstances(menu->cascades[i]->brother, wwin);
321 if (paintflag)
322 wMenuPaint(menu);
325 static WMenu *readUserMenuFile(WScreen * scr, char *file_name)
327 WMenu *menu;
328 char *mtitle;
329 WMPropList *plum, *elem, *title, *command, *params;
330 int count, i;
332 menu = NULL;
333 plum = WMReadPropListFromFile(file_name);
334 /**/ if (plum) {
335 menu = configureUserMenu(scr, plum);
336 WMReleasePropList(plum);
338 return menu;
341 WMenu *wUserMenuGet(WScreen * scr, WWindow * wwin)
343 WMenu *menu = NULL;
344 char buffer[100];
345 char *path = NULL;
346 char *tmp;
347 if (wwin->wm_instance && wwin->wm_class) {
348 int len = strlen(wwin->wm_instance) + strlen(wwin->wm_class) + 7;
349 tmp = wmalloc(len);
350 snprintf(tmp, len, "%s.%s.menu", wwin->wm_instance, wwin->wm_class);
351 path = wfindfile(DEF_USER_MENU_PATHS, tmp);
352 wfree(tmp);
354 if (!path)
355 return NULL;
357 if (wwin) {
358 menu = readUserMenuFile(scr, path);
361 wfree(path);
363 return menu;
366 #endif /* USER_MENU */