changed indentation to use spaces only
[wmaker-crm.git] / src / usermenu.c
blobd1a77654b0fd72aa98318e1ea21239dd755adf0f
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 "wcore.h"
75 #include "menu.h"
76 #include "actions.h"
77 #include "funcs.h"
78 #include "keybind.h"
80 #include "framewin.h"
83 /*** var ***/
84 extern WPreferences wPreferences;
86 typedef struct {
87 WScreen *screen;
88 WShortKey *key;
89 int key_no;
90 } WUserMenuData;
93 static void
94 notifyClient(WMenu *menu, WMenuEntry *entry)
96 XEvent event;
97 WUserMenuData *data = entry->clientdata;
98 WScreen *scr = data->screen;
99 Window window;
100 int i;
102 window=scr->focused_window->client_win;
104 for(i=0;i<data->key_no;i++) {
105 event.xkey.type = KeyPress;
106 event.xkey.display = dpy;
107 event.xkey.window = window;
108 event.xkey.root = DefaultRootWindow(dpy);
109 event.xkey.subwindow = (Window)None;
110 event.xkey.x = 0x0;
111 event.xkey.y = 0x0;
112 event.xkey.x_root = 0x0;
113 event.xkey.y_root = 0x0;
114 event.xkey.keycode = data->key[i].keycode;
115 event.xkey.state = data->key[i].modifier;
116 event.xkey.same_screen = True;
117 event.xkey.time = CurrentTime;
118 if (XSendEvent(dpy, window, False, KeyPressMask, &event)) {
119 event.xkey.type = KeyRelease;
120 event.xkey.time = CurrentTime;
121 XSendEvent(dpy, window, True, KeyReleaseMask, &event);
126 static void
127 removeUserMenudata(void *menudata)
129 WUserMenuData *data = menudata;
130 if(data->key) wfree(data->key);
131 wfree(data);
135 static WUserMenuData*
136 convertShortcuts(WScreen *scr, WMPropList *shortcut)
138 WUserMenuData *data;
139 KeySym ksym;
140 char *k;
141 char *buffer;
142 char buf[128], *b;
143 int keycount,i,j,mod;
145 if (WMIsPLString(shortcut)) {
146 keycount = 1;
148 else if (WMIsPLArray(shortcut)) {
149 keycount = WMGetPropListItemCount(shortcut);
151 else return NULL;
152 /*for (i=0;i<keycount;i++){*/
154 data = wmalloc(sizeof(WUserMenuData));
155 if (!data) return NULL;
156 data->key = wmalloc(sizeof(WShortKey)*keycount);
157 if (!data->key) {
158 wfree(data);
159 return NULL;
163 for (i=0,j=0;i<keycount;i++) {
164 data->key[j].modifier = 0;
165 if (WMIsPLArray(shortcut)) {
166 strcpy(buf, WMGetFromPLString(WMGetFromPLArray(shortcut, i)));
167 } else {
168 strcpy(buf, WMGetFromPLString(shortcut));
170 b = (char*)buf;
172 while ((k = strchr(b, '+'))!=NULL) {
173 *k = 0;
174 mod = wXModifierFromKey(b);
175 if (mod<0) {
176 break;
178 data->key[j].modifier |= mod;
179 b = k+1;
182 ksym = XStringToKeysym(b);
183 if (ksym==NoSymbol) {
184 continue;
187 data->key[j].keycode = XKeysymToKeycode(dpy, ksym);
188 if (data->key[j].keycode) {
189 j++;
193 keyover:
195 /* get key */
196 if (!j) {
197 puts("fatal j");
198 wfree(data->key);
199 wfree(data);
200 return NULL;
202 data->key_no = j;
203 data->screen = scr;
205 return data;
208 static WMenu*
209 configureUserMenu(WScreen *scr, WMPropList *plum)
211 char *mtitle;
212 WMenu *menu=NULL;
213 WMPropList *elem, *title, *command, *params;
214 int count,i;
215 WUserMenuData *data;
217 if (!plum) return NULL;
218 if (!WMIsPLArray(plum)) {
219 return NULL;
222 count = WMGetPropListItemCount(plum);
223 if (!count) return NULL;
225 elem = WMGetFromPLArray(plum, 0);
226 if (!WMIsPLString(elem)) {
227 return NULL;
230 mtitle = WMGetFromPLString(elem);
232 menu=wMenuCreateForApp(scr, mtitle, True);
234 for(i=1; i<count; i++) {
235 elem = WMGetFromPLArray(plum,i);
236 if(WMIsPLArray(WMGetFromPLArray(elem,1))) {
237 WMenu *submenu;
238 WMenuEntry *mentry;
240 submenu = configureUserMenu(scr,elem);
241 if (submenu)
242 mentry = wMenuAddCallback(menu, submenu->frame->title,
243 NULL, NULL);
244 wMenuEntrySetCascade(menu, mentry, submenu);
246 else {
247 int idx = 0;
248 WMPropList *instances=0;
250 title = WMGetFromPLArray(elem,idx++);
251 command = WMGetFromPLArray(elem,idx++);
252 if (WMGetPropListItemCount(elem) >= 3)
253 params = WMGetFromPLArray(elem,idx++);
255 if (!title || !command)
256 return menu;
258 if (!strcmp("SHORTCUT",WMGetFromPLString(command))) {
259 WMenuEntry *entry;
261 data = convertShortcuts(scr, params);
262 if (data){
263 entry = wMenuAddCallback(menu, WMGetFromPLString(title),
264 notifyClient, data);
266 if (entry) {
267 if (WMIsPLString(params)) {
268 entry->rtext = GetShortcutString(WMGetFromPLString(params));
270 entry->free_cdata = removeUserMenudata;
272 if (WMGetPropListItemCount(elem) >= 4) {
273 instances = WMGetFromPLArray(elem,idx++);
274 if(WMIsPLArray(instances))
275 if (instances && WMGetPropListItemCount(instances)
276 && WMIsPLArray(instances)){
277 entry->instances = WMRetainPropList(instances);
287 return menu;
290 void
291 wUserMenuRefreshInstances(WMenu *menu, WWindow *wwin)
293 WMenuEntry* entry;
294 int i,j,count,paintflag;
296 paintflag=0;
298 if(!menu) return;
300 for (i=0; i<menu->entry_no; i++) {
301 if (menu->entries[i]->instances){
302 WMPropList *ins;
303 int oldflag;
304 count = WMGetPropListItemCount(menu->entries[i]->instances);
306 oldflag = menu->entries[i]->flags.enabled;
307 menu->entries[i]->flags.enabled = 0;
308 for (j=0; j<count;j++) {
309 ins = WMGetFromPLArray(menu->entries[i]->instances,j);
310 if (!strcmp(wwin->wm_instance,WMGetFromPLString(ins))) {
311 menu->entries[i]->flags.enabled = 1;
312 break;
315 if (oldflag != menu->entries[i]->flags.enabled)
316 paintflag=1;
319 for (i=0; i < menu->cascade_no; i++) {
320 if (!menu->cascades[i]->flags.brother)
321 wUserMenuRefreshInstances(menu->cascades[i], wwin);
322 else
323 wUserMenuRefreshInstances(menu->cascades[i]->brother, wwin);
326 if (paintflag)
327 wMenuPaint(menu);
331 static WMenu*
332 readUserMenuFile(WScreen *scr, char *file_name)
334 WMenu *menu;
335 char *mtitle;
336 WMPropList *plum, *elem, *title, *command, *params;
337 int count,i;
339 menu=NULL;
340 plum = WMReadPropListFromFile(file_name);
341 /**/
343 if(plum){
344 menu = configureUserMenu(scr, plum);
345 WMReleasePropList(plum);
347 return menu;
351 WMenu*
352 wUserMenuGet(WScreen *scr, WWindow *wwin)
354 WMenu *menu = NULL;
355 char buffer[100];
356 char *path = NULL;
357 char *tmp;
358 if (wwin->wm_instance && wwin->wm_class) {
359 int len = strlen(wwin->wm_instance)+strlen(wwin->wm_class)+7;
360 tmp=wmalloc(len);
361 snprintf(tmp,len,"%s.%s.menu",wwin->wm_instance,wwin->wm_class);
362 path = wfindfile(DEF_USER_MENU_PATHS,tmp);
363 wfree(tmp);
365 if (!path) return NULL;
367 if (wwin) {
368 menu = readUserMenuFile(scr, path);
371 wfree(path);
373 return menu;
376 #endif /* USER_MENU */