]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
move the keyboard and mouse plugins into the kernel for mucho sexiness.
[chaz/openbox] / openbox / menu.c
1 #include "debug.h"
2 #include "menu.h"
3 #include "openbox.h"
4 #include "stacking.h"
5 #include "client.h"
6 #include "grab.h"
7 #include "screen.h"
8 #include "geom.h"
9 #include "plugin.h"
10 #include "misc.h"
11
12 GHashTable *menu_hash = NULL;
13 GList *menu_visible = NULL;
14
15 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
16 LeaveWindowMask)
17 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
18 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
19 ButtonPressMask | ButtonReleaseMask)
20
21 static void parse_menu(xmlDocPtr doc, xmlNodePtr node, void *data)
22 {
23 parse_menu_full(doc, node, data, TRUE);
24 }
25
26
27 void parse_menu_full(xmlDocPtr doc, xmlNodePtr node, void *data,
28 gboolean newmenu)
29 {
30 ObAction *act;
31 xmlNodePtr nact;
32
33 gchar *id = NULL, *title = NULL, *label = NULL, *plugin;
34 ObMenu *menu = NULL, *parent;
35
36 if (newmenu == TRUE) {
37 if (!parse_attr_string("id", node, &id))
38 goto parse_menu_fail;
39 if (!parse_attr_string("label", node, &title))
40 goto parse_menu_fail;
41 ob_debug("menu label %s\n", title);
42
43 if (parse_attr_string("plugin", node, &plugin)) {
44 PluginMenuCreateData data;
45 data.doc = doc;
46 data.node = node;
47 data.parent = menu;
48
49 if (plugin_open_reopen(plugin))
50 parent = plugin_create(plugin, &data);
51 g_free(plugin);
52 } else
53 menu = menu_new(title, id, data ? *((ObMenu**)data) : NULL);
54
55 if (data)
56 *((ObMenu**)data) = menu;
57 } else {
58 menu = (ObMenu *)data;
59 }
60
61 node = node->xmlChildrenNode;
62
63 while (node) {
64 if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
65 if (parse_attr_string("plugin", node, &plugin)) {
66 PluginMenuCreateData data;
67 data.doc = doc;
68 data.node = node;
69 data.parent = menu;
70 if (plugin_open_reopen(plugin))
71 parent = plugin_create(plugin, &data);
72 g_free(plugin);
73 } else {
74 parent = menu;
75 parse_menu(doc, node, &parent);
76 menu_add_entry(menu, menu_entry_new_submenu(parent->label,
77 parent));
78 }
79
80 }
81 else if (!xmlStrcasecmp(node->name, (const xmlChar*) "item")) {
82 if (parse_attr_string("label", node, &label)) {
83 if ((nact = parse_find_node("action", node->xmlChildrenNode)))
84 act = action_parse(doc, nact);
85 else
86 act = NULL;
87 if (act)
88 menu_add_entry(menu, menu_entry_new(label, act));
89 else
90 menu_add_entry(menu, menu_entry_new_separator(label));
91 g_free(label);
92 }
93 }
94 node = node->next;
95 }
96
97 parse_menu_fail:
98 g_free(id);
99 g_free(title);
100 }
101
102 void menu_control_show(ObMenu *self, int x, int y, ObClient *client);
103
104 void menu_destroy_hash_key(ObMenu *menu)
105 {
106 g_free(menu);
107 }
108
109 void menu_destroy_hash_value(ObMenu *self)
110 {
111 GList *it;
112
113 for (it = self->entries; it; it = it->next)
114 menu_entry_free(it->data);
115 g_list_free(self->entries);
116
117 g_free(self->label);
118 g_free(self->name);
119
120 g_hash_table_remove(window_map, &self->title);
121 g_hash_table_remove(window_map, &self->frame);
122 g_hash_table_remove(window_map, &self->items);
123
124 stacking_remove(self);
125
126 RrAppearanceFree(self->a_title);
127 RrAppearanceFree(self->a_items);
128 XDestroyWindow(ob_display, self->title);
129 XDestroyWindow(ob_display, self->frame);
130 XDestroyWindow(ob_display, self->items);
131
132 g_free(self);
133 }
134
135 void menu_entry_free(ObMenuEntry *self)
136 {
137 g_free(self->label);
138 action_free(self->action);
139
140 g_hash_table_remove(window_map, &self->item);
141
142 RrAppearanceFree(self->a_item);
143 RrAppearanceFree(self->a_disabled);
144 RrAppearanceFree(self->a_hilite);
145 RrAppearanceFree(self->a_submenu);
146 XDestroyWindow(ob_display, self->item);
147 XDestroyWindow(ob_display, self->submenu_pic);
148 g_free(self);
149 }
150
151 void menu_startup()
152 {
153 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
154 (GDestroyNotify)menu_destroy_hash_key,
155 (GDestroyNotify)menu_destroy_hash_value);
156
157 parse_register("menu", parse_menu, NULL);
158
159 }
160
161 void menu_shutdown()
162 {
163 g_hash_table_destroy(menu_hash);
164 }
165
166 static Window createWindow(Window parent, unsigned long mask,
167 XSetWindowAttributes *attrib)
168 {
169 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
170 RrDepth(ob_rr_inst), InputOutput,
171 RrVisual(ob_rr_inst), mask, attrib);
172
173 }
174
175 ObMenu *menu_new_full(char *label, char *name, ObMenu *parent,
176 menu_controller_show show, menu_controller_update update,
177 menu_controller_selected selected,
178 menu_controller_hide hide,
179 menu_controller_mouseover mouseover)
180 {
181 XSetWindowAttributes attrib;
182 ObMenu *self;
183
184 self = g_new0(ObMenu, 1);
185 self->obwin.type = Window_Menu;
186 self->label = g_strdup(label);
187 self->name = g_strdup(name);
188 self->parent = parent;
189 self->open_submenu = NULL;
190 self->over = NULL;
191
192 self->entries = NULL;
193 self->shown = FALSE;
194 self->invalid = TRUE;
195
196 /* default controllers */
197 self->show = (show != NULL ? show : menu_show_full);
198 self->hide = (hide != NULL ? hide : menu_hide);
199 self->update = (update != NULL ? update : menu_render);
200 self->mouseover = (mouseover != NULL ? mouseover :
201 menu_control_mouseover);
202 self->selected = (selected != NULL ? selected : menu_entry_fire);
203
204 self->plugin = NULL;
205 self->plugin_data = NULL;
206
207 attrib.override_redirect = TRUE;
208 attrib.event_mask = FRAME_EVENTMASK;
209 self->frame = createWindow(RootWindow(ob_display, ob_screen),
210 CWOverrideRedirect|CWEventMask, &attrib);
211 attrib.event_mask = TITLE_EVENTMASK;
212 self->title = createWindow(self->frame, CWEventMask, &attrib);
213 self->items = createWindow(self->frame, 0, &attrib);
214
215 self->a_title = self->a_items = NULL;
216
217 XMapWindow(ob_display, self->title);
218 XMapWindow(ob_display, self->items);
219
220 g_hash_table_insert(window_map, &self->frame, self);
221 g_hash_table_insert(window_map, &self->title, self);
222 g_hash_table_insert(window_map, &self->items, self);
223 g_hash_table_insert(menu_hash, g_strdup(name), self);
224
225 stacking_add(MENU_AS_WINDOW(self));
226 stacking_raise(MENU_AS_WINDOW(self));
227
228 return self;
229 }
230
231 void menu_free(char *name)
232 {
233 g_hash_table_remove(menu_hash, name);
234 }
235
236 ObMenuEntry *menu_entry_new_full(char *label, ObAction *action,
237 ObMenuEntryRenderType render_type,
238 gpointer submenu)
239 {
240 ObMenuEntry *menu_entry = g_new0(ObMenuEntry, 1);
241
242 menu_entry->label = g_strdup(label);
243 menu_entry->render_type = render_type;
244 menu_entry->action = action;
245
246 menu_entry->hilite = FALSE;
247 menu_entry->enabled = TRUE;
248
249 menu_entry->submenu = submenu;
250
251 return menu_entry;
252 }
253
254 void menu_entry_set_submenu(ObMenuEntry *entry, ObMenu *submenu)
255 {
256 g_assert(entry != NULL);
257
258 entry->submenu = submenu;
259
260 if(entry->parent != NULL)
261 entry->parent->invalid = TRUE;
262 }
263
264 void menu_add_entry(ObMenu *menu, ObMenuEntry *entry)
265 {
266 XSetWindowAttributes attrib;
267
268 g_assert(menu != NULL);
269 g_assert(entry != NULL);
270 g_assert(entry->item == None);
271
272 menu->entries = g_list_append(menu->entries, entry);
273 entry->parent = menu;
274
275 attrib.event_mask = ENTRY_EVENTMASK;
276 entry->item = createWindow(menu->items, CWEventMask, &attrib);
277 entry->submenu_pic = createWindow(menu->items, CWEventMask, &attrib);
278 XMapWindow(ob_display, entry->item);
279 XMapWindow(ob_display, entry->submenu_pic);
280
281 entry->a_item = entry->a_disabled = entry->a_hilite = entry->a_submenu
282 = NULL;
283
284 menu->invalid = TRUE;
285
286 g_hash_table_insert(window_map, &entry->item, menu);
287 g_hash_table_insert(window_map, &entry->submenu_pic, menu);
288 }
289
290 void menu_show(char *name, int x, int y, ObClient *client)
291 {
292 ObMenu *self;
293
294 self = g_hash_table_lookup(menu_hash, name);
295 if (!self) {
296 g_warning("Attempted to show menu '%s' but it does not exist.",
297 name);
298 return;
299 }
300
301 menu_show_full(self, x, y, client);
302 }
303
304 void menu_show_full(ObMenu *self, int x, int y, ObClient *client)
305 {
306 g_assert(self != NULL);
307
308 self->update(self);
309
310 self->client = client;
311
312 if (!self->shown) {
313 if (!(self->parent && self->parent->shown)) {
314 grab_pointer(TRUE, None);
315 grab_keyboard(TRUE);
316 }
317 menu_visible = g_list_append(menu_visible, self);
318 }
319
320 menu_control_show(self, x, y, client);
321 }
322
323 void menu_hide(ObMenu *self) {
324 if (self->shown) {
325 XUnmapWindow(ob_display, self->frame);
326 self->shown = FALSE;
327 if (self->open_submenu)
328 self->open_submenu->hide(self->open_submenu);
329 if (self->parent && self->parent->open_submenu == self) {
330 self->parent->open_submenu = NULL;
331 }
332
333 if (!(self->parent && self->parent->shown)) {
334 grab_keyboard(FALSE);
335 grab_pointer(FALSE, None);
336 }
337 menu_visible = g_list_remove(menu_visible, self);
338 if (self->over) {
339 ((ObMenuEntry *)self->over->data)->hilite = FALSE;
340 menu_entry_render(self->over->data);
341 self->over = NULL;
342 }
343 }
344 }
345
346 void menu_clear(ObMenu *self) {
347 GList *it;
348
349 for (it = self->entries; it; it = it->next) {
350 ObMenuEntry *entry = it->data;
351 menu_entry_free(entry);
352 }
353 self->entries = NULL;
354 self->invalid = TRUE;
355 }
356
357
358 ObMenuEntry *menu_find_entry(ObMenu *menu, Window win)
359 {
360 GList *it;
361
362 for (it = menu->entries; it; it = it->next) {
363 ObMenuEntry *entry = it->data;
364 if (entry->item == win)
365 return entry;
366 }
367 return NULL;
368 }
369
370 ObMenuEntry *menu_find_entry_by_submenu(ObMenu *menu, ObMenu *submenu)
371 {
372 GList *it;
373
374 for (it = menu->entries; it; it = it->next) {
375 ObMenuEntry *entry = it->data;
376 if (entry->submenu == submenu)
377 return entry;
378 }
379 return NULL;
380 }
381
382 ObMenuEntry *menu_find_entry_by_pos(ObMenu *menu, int x, int y)
383 {
384 if (x < 0 || x >= menu->size.width || y < 0 || y >= menu->size.height)
385 return NULL;
386
387 y -= menu->title_h + ob_rr_theme->bwidth;
388 if (y < 0) return NULL;
389
390 ob_debug("%d %p\n", y/menu->item_h,
391 g_list_nth_data(menu->entries, y / menu->item_h));
392 return g_list_nth_data(menu->entries, y / menu->item_h);
393 }
394
395 void menu_entry_fire(ObMenuEntry *self, unsigned int button, unsigned int x,
396 unsigned int y)
397 {
398 ObMenu *m;
399
400 /* ignore wheel scrolling */
401 if (button == 4 || button == 5) return;
402
403 if (self->action) {
404 self->action->data.any.c = self->parent->client;
405 self->action->func(&self->action->data);
406
407 /* hide the whole thing */
408 m = self->parent;
409 while (m->parent) m = m->parent;
410 m->hide(m);
411 }
412 }
413
414 /*
415 Default menu controller action for showing.
416 */
417
418 void menu_control_show(ObMenu *self, int x, int y, ObClient *client)
419 {
420 guint i;
421 Rect *a = NULL;
422
423 g_assert(!self->invalid);
424
425 for (i = 0; i < screen_num_monitors; ++i) {
426 a = screen_physical_area_monitor(i);
427 if (RECT_CONTAINS(*a, x, y))
428 break;
429 }
430 g_assert(a != NULL);
431 self->xin_area = i;
432
433 POINT_SET(self->location,
434 MIN(x, a->x + a->width - 1 - self->size.width),
435 MIN(y, a->y + a->height - 1 - self->size.height));
436 XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
437
438 if (!self->shown) {
439 XMapWindow(ob_display, self->frame);
440 stacking_raise(MENU_AS_WINDOW(self));
441 self->shown = TRUE;
442 } else if (self->shown && self->open_submenu) {
443 self->open_submenu->hide(self->open_submenu);
444 }
445 }
446
447 void menu_control_mouseover(ObMenuEntry *self, gboolean enter)
448 {
449 int x;
450 Rect *a;
451 ObMenuEntry *e;
452
453 g_assert(self != NULL);
454
455 if (enter) {
456 /* TODO: we prolly don't need open_submenu */
457 if (self->parent->open_submenu && self->submenu
458 != self->parent->open_submenu)
459 {
460 e = (ObMenuEntry *) self->parent->over->data;
461 e->hilite = FALSE;
462 menu_entry_render(e);
463 self->parent->open_submenu->hide(self->parent->open_submenu);
464 }
465
466 if (self->submenu && self->parent->open_submenu != self->submenu) {
467 self->parent->open_submenu = self->submenu;
468
469 /* shouldn't be invalid since it must be displayed */
470 g_assert(!self->parent->invalid);
471 /* TODO: I don't understand why these bevels should be here.
472 Something must be wrong in the width calculation */
473 x = self->parent->location.x + self->parent->size.width +
474 ob_rr_theme->bwidth - ob_rr_theme->menu_overlap;
475
476 /* need to get the width. is this bad?*/
477 self->parent->update(self->submenu);
478
479 a = screen_physical_area_monitor(self->parent->xin_area);
480
481 if (self->submenu->size.width + x >= a->x + a->width) {
482 int newparentx = a->x + a->width
483 - self->submenu->size.width
484 - self->parent->size.width
485 - ob_rr_theme->bwidth
486 - ob_rr_theme->menu_overlap;
487
488 x = a->x + a->width - self->submenu->size.width
489 - ob_rr_theme->menu_overlap;
490 XWarpPointer(ob_display, None, None, 0, 0, 0, 0,
491 newparentx - self->parent->location.x, 0);
492
493 menu_show_full(self->parent, newparentx,
494 self->parent->location.y, self->parent->client);
495 }
496
497 menu_show_full(self->submenu, x,
498 self->parent->location.y + self->y,
499 self->parent->client);
500 }
501 self->hilite = TRUE;
502 self->parent->over = g_list_find(self->parent->entries, self);
503
504 } else
505 self->hilite = FALSE;
506
507 menu_entry_render(self);
508 }
509
510 void menu_control_keyboard_nav(unsigned int key)
511 {
512 static ObMenu *current_menu = NULL;
513 ObMenuEntry *e = NULL;
514
515 ObKey obkey = OB_NUM_KEYS;
516
517 /* hrmm. could be fixed */
518 if (key == ob_keycode(OB_KEY_DOWN))
519 obkey = OB_KEY_DOWN;
520 else if (key == ob_keycode(OB_KEY_UP))
521 obkey = OB_KEY_UP;
522 else if (key == ob_keycode(OB_KEY_RIGHT)) /* fuck */
523 obkey = OB_KEY_RIGHT;
524 else if (key == ob_keycode(OB_KEY_LEFT)) /* users */
525 obkey = OB_KEY_LEFT;
526 else if (key == ob_keycode(OB_KEY_RETURN))
527 obkey = OB_KEY_RETURN;
528
529
530 if (current_menu == NULL)
531 current_menu = menu_visible->data;
532
533 switch (obkey) {
534 case OB_KEY_DOWN: {
535 if (current_menu->over) {
536 current_menu->mouseover(current_menu->over->data, FALSE);
537 current_menu->over = (current_menu->over->next != NULL ?
538 current_menu->over->next :
539 current_menu->entries);
540 }
541 else
542 current_menu->over = current_menu->entries;
543
544 if (current_menu->over)
545 current_menu->mouseover(current_menu->over->data, TRUE);
546
547 break;
548 }
549 case OB_KEY_UP: {
550 if (current_menu->over) {
551 current_menu->mouseover(current_menu->over->data, FALSE);
552 current_menu->over = (current_menu->over->prev != NULL ?
553 current_menu->over->prev :
554 g_list_last(current_menu->entries));
555 } else
556 current_menu->over = g_list_last(current_menu->entries);
557
558 if (current_menu->over)
559 current_menu->mouseover(current_menu->over->data, TRUE);
560
561 break;
562 }
563 case OB_KEY_RIGHT: {
564 if (current_menu->over == NULL)
565 return;
566 e = (ObMenuEntry *)current_menu->over->data;
567 if (e->submenu) {
568 current_menu->mouseover(e, TRUE);
569 current_menu = e->submenu;
570 current_menu->over = current_menu->entries;
571 if (current_menu->over)
572 current_menu->mouseover(current_menu->over->data, TRUE);
573 }
574 break;
575 }
576
577 case OB_KEY_RETURN: {
578 if (current_menu->over == NULL)
579 return;
580 e = (ObMenuEntry *)current_menu->over->data;
581
582 current_menu->mouseover(e, FALSE);
583 current_menu->over = NULL;
584 /* zero is enter */
585 menu_entry_fire(e, 0, 0, 0);
586 }
587
588 case OB_KEY_LEFT: {
589 if (current_menu->over != NULL) {
590 current_menu->mouseover(current_menu->over->data, FALSE);
591 current_menu->over = NULL;
592 }
593
594 current_menu->hide(current_menu);
595
596 if (current_menu->parent)
597 current_menu = current_menu->parent;
598
599 break;
600 }
601 default:
602 ((ObMenu *)menu_visible->data)->hide(menu_visible->data);
603 current_menu = NULL;
604 }
605 return;
606 }
607
608 void menu_noop()
609 {
610 /* This noop brought to you by OLS 2003 Email Garden. */
611 }
This page took 0.063629 seconds and 4 git commands to generate.