*** empty log message ***
[wmaker-crm.git] / src / usermenu.c
blobc60fe132f5e6b022302323815081b135695bf52b
1 /* User defined menu is good, but beer's always better
2 * if someone wanna start hacking something, He heard...
3 * TODO
4 * - enhance commands. (eg, exit, hide, list all app's member
5 * window and etc)
6 * - cache menu... dunno.. if people really use this feature :P
7 * - Violins, senseless violins!
8 * that's all, right now :P
9 * - external! WINGs menu editor.
10 * TODONOT
11 * - allow applications to share their menu. ] think it
12 * looks wierd since there still are more than 1 appicon.
14 * Syntax...
15 * (
16 * "Program Name",
17 * ("Command 1", SHORTCUT, 1),
18 * ("Command 2", SHORTCUT, 2, ("Allowed_instant_1", "Allowed_instant_2")),
19 * ("Command 3", SHORTCUT, (3,4,5), ("Allowed_instant_1")),
20 * (
21 * "Submenu",
22 * ("Kill Command", KILL),
23 * ("Hide Command", HIDE),
24 * ("Hide Others Command", HIDE_OTHERS),
25 * ("Members", MEMBERS),
26 * ("Exit Command", EXIT)
27 * )
28 * )
30 * Tips:
31 * - If you don't want short cut keys to be listed
32 * in the right side of entries, you can just put them
33 * in array instead of using the string directly.
37 #include "wconfig.h"
39 #ifdef USER_MENU
41 #include <X11/Xlib.h>
42 #include <X11/Xutil.h>
43 #include <X11/Xproto.h>
44 #include <X11/Xatom.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdio.h>
48 #include <unistd.h>
50 #include "WindowMaker.h"
51 #include "wcore.h"
52 #include "menu.h"
53 #include "actions.h"
54 #include "funcs.h"
55 #include "keybind.h"
57 #include "framewin.h"
60 extern proplist_t ReadProplistFromFile(char *file);
61 /*** var ***/
62 extern WPreferences wPreferences;
64 typedef struct {
65 WScreen *screen;
66 WShortKey *key;
67 int key_no;
68 } WUserMenuData;
71 static void
72 notifyClient(WMenu *menu, WMenuEntry *entry){
73 XEvent event;
74 WUserMenuData *data = entry->clientdata;
75 WScreen *scr = data->screen;
76 Window window;
77 int i;
79 window=scr->focused_window->client_win;
81 for(i=0;i<data->key_no;i++){
82 event.xkey.type = KeyPress;
83 event.xkey.display = dpy;
84 event.xkey.window = window;
85 event.xkey.subwindow = 0x0;
86 event.xkey.x = 0x0;
87 event.xkey.y = 0x0;
88 event.xkey.x_root = 0x0;
89 event.xkey.y_root = 0x0;
90 event.xkey.keycode = data->key[i].keycode;
91 event.xkey.state = data->key[i].modifier;
92 event.xkey.same_screen = YES;
93 XSendEvent(dpy, window, False, NoEventMask, &event);
94 XFlush(dpy);
98 static void
99 removeUserMenudata(void *menudata){
100 WUserMenuData *data = menudata;
101 if(data->key) free(data->key);
102 free(data);
106 static WUserMenuData*
107 convertShortcuts(WScreen *scr, proplist_t shortcut){
108 WUserMenuData *data;
109 KeySym ksym;
110 char *k;
111 char *buffer;
112 char buf[128], *b;
113 int keycount,i,j,mod;
115 if (PLIsString(shortcut)){
116 keycount = 1;
118 else if (PLIsArray(shortcut)){
119 keycount = PLGetNumberOfElements(shortcut);
121 else return NULL;
122 /*for (i=0;i<keycount;i++){*/
124 data = wmalloc(sizeof(WUserMenuData));
125 if (!data) return NULL;
126 data->key = wmalloc(sizeof(WShortKey)*keycount);
127 if (!data->key) {
128 free(data);
129 return NULL;
133 for (i=0,j=0;i<keycount;i++) {
134 data->key[j].modifier = 0;
135 if (PLIsArray(shortcut)) {
136 strcpy(buf, PLGetString(PLGetArrayElement(shortcut, i)));
138 else {
139 strcpy(buf, PLGetString(shortcut));
141 b = (char*)buf;
143 while ((k = strchr(b, '+'))!=NULL) {
144 *k = 0;
145 mod = wXModifierFromKey(b);
146 if (mod<0) {
147 break;
149 data->key[j].modifier |= mod;
150 b = k+1;
153 ksym = XStringToKeysym(b);
154 if (ksym==NoSymbol) {
155 continue;
158 data->key[j].keycode = XKeysymToKeycode(dpy, ksym);
159 if (data->key[j].keycode) {
160 j++;
164 keyover:
166 /* get key */
167 if (!j) {
168 puts("fatal j");
169 free(data->key);
170 free(data);
171 return NULL;
173 data->key_no = j;
174 data->screen = scr;
176 return data;
179 static WMenu*
180 configureUserMenu(WScreen *scr, proplist_t plum){
181 char *mtitle;
182 WMenu *menu=NULL;
183 proplist_t elem, title, command, params;
184 int count,i;
185 WUserMenuData *data;
187 if (!plum) return NULL;
188 if (!PLIsArray(plum)){
189 return NULL;
192 count = PLGetNumberOfElements(plum);
193 if (!count) return NULL;
195 elem = PLGetArrayElement(plum, 0);
196 if (!PLIsString(elem)){
197 return NULL;
200 mtitle = PLGetString(elem);
202 menu=wMenuCreateForApp(scr, mtitle, True);
204 for(i=1; i<count; i++){
205 elem = PLGetArrayElement(plum,i);
206 if(PLIsArray(PLGetArrayElement(elem,1))){
207 WMenu *submenu;
208 WMenuEntry *mentry;
210 submenu = configureUserMenu(scr,elem);
211 if (submenu)
212 mentry = wMenuAddCallback(menu, submenu->frame->title,
213 NULL, NULL);
214 wMenuEntrySetCascade(menu, mentry, submenu);
216 else {
217 int idx = 0;
218 proplist_t instances=0;
220 title = PLGetArrayElement(elem,idx++);
221 command = PLGetArrayElement(elem,idx++);
222 if (PLGetNumberOfElements(elem) >= 3)
223 params = PLGetArrayElement(elem,idx++);
225 if (!title || !command)
226 return menu;
228 if (!strcmp("SHORTCUT",PLGetString(command))){
229 WMenuEntry *entry;
231 data = convertShortcuts(scr, params);
232 if (data){
233 entry = wMenuAddCallback(menu, PLGetString(title),
234 notifyClient, data);
236 if (entry) {
237 if (PLIsString(params)) {
238 entry->rtext = GetShortcutString(PLGetString(params));
240 entry->free_cdata = removeUserMenudata;
242 if (PLGetNumberOfElements(elem) >= 4){
243 instances = PLGetArrayElement(elem,idx++);
244 if(PLIsArray(instances))
245 if (instances && PLGetNumberOfElements(instances)
246 && PLIsArray(instances)){
247 entry->instances = PLRetain(instances);
257 return menu;
260 void
261 wUserMenuRefreshInstances(WMenu *menu, WWindow *wwin)
263 WMenuEntry* entry;
264 int i,j,count,paintflag;
266 paintflag=0;
268 if(!menu) return;
270 for (i=0; i<menu->entry_no; i++) {
271 if (menu->entries[i]->instances){
272 proplist_t ins;
273 int oldflag;
274 count = PLGetNumberOfElements(menu->entries[i]->instances);
276 oldflag = menu->entries[i]->flags.enabled;
277 menu->entries[i]->flags.enabled = 0;
278 for (j=0; j<count;j++){
279 ins = PLGetArrayElement(menu->entries[i]->instances,j);
280 if (!strcmp(wwin->wm_instance,PLGetString(ins))){
281 menu->entries[i]->flags.enabled = 1;
282 break;
285 if (oldflag != menu->entries[i]->flags.enabled)
286 paintflag=1;
289 for (i=0; i < menu->cascade_no; i++) {
290 if (!menu->cascades[i]->flags.brother)
291 wUserMenuRefreshInstances(menu->cascades[i], wwin);
292 else
293 wUserMenuRefreshInstances(menu->cascades[i]->brother, wwin);
296 if(paintflag)
297 wMenuPaint(menu);
301 static WMenu*
302 readUserMenuFile(WScreen *scr, char *file_name)
304 WMenu *menu;
305 char *mtitle;
306 proplist_t plum, elem, title, command, params;
307 int count,i;
309 menu=NULL;
310 plum = ReadProplistFromFile(file_name);
311 /**/
313 if(plum){
314 menu = configureUserMenu(scr, plum);
315 PLRelease(plum);
317 return menu;
321 WMenu*
322 wUserMenuGet(WScreen *scr, WWindow *wwin){
323 WMenu *menu = NULL;
324 char buffer[100];
325 char *prefix, *menufile;
327 prefix = getenv("HOME");
328 if (!prefix)
329 prefix = ".";
330 /* this file/path code will be replaced :D - ]d */
331 menufile = malloc(strlen(prefix)+128);
332 if (!menufile) return NULL;
334 if (wwin) {
335 FILE *f;
336 sprintf(menufile, "%s/GNUstep/Library/WindowMaker/UserMenus/%s.%s.menu",
337 prefix, wwin->wm_instance, wwin->wm_class);
338 f = fopen(menufile, "r");
339 if (f) {
340 fclose(f);
341 menu = readUserMenuFile(scr, menufile);
345 free(menufile);
346 return menu;
349 #endif /* USER_MENU */