]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
Move the main loop out into the libobt
[chaz/openbox] / openbox / menu.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 menu.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "debug.h"
21 #include "menu.h"
22 #include "openbox.h"
23 #include "stacking.h"
24 #include "grab.h"
25 #include "client.h"
26 #include "config.h"
27 #include "actions.h"
28 #include "screen.h"
29 #include "menuframe.h"
30 #include "keyboard.h"
31 #include "geom.h"
32 #include "misc.h"
33 #include "client_menu.h"
34 #include "client_list_menu.h"
35 #include "client_list_combined_menu.h"
36 #include "gettext.h"
37 #include "parser/parse.h"
38
39 typedef struct _ObMenuParseState ObMenuParseState;
40
41 struct _ObMenuParseState
42 {
43 ObMenu *parent;
44 ObMenu *pipe_creator;
45 };
46
47 static GHashTable *menu_hash = NULL;
48 static ObParseInst *menu_parse_inst;
49 static ObMenuParseState menu_parse_state;
50 static gboolean menu_can_hide = FALSE;
51
52 static void menu_destroy_hash_value(ObMenu *self);
53 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
54 gpointer data);
55 static void parse_menu_separator(ObParseInst *i,
56 xmlDocPtr doc, xmlNodePtr node,
57 gpointer data);
58 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
59 gpointer data);
60 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
61 gchar **strippedlabel, guint *position,
62 gboolean *always_show);
63
64
65 static void client_dest(ObClient *client, gpointer data)
66 {
67 /* menus can be associated with a client, so close any that are since
68 we are disappearing now */
69 menu_frame_hide_all_client(client);
70 }
71
72 void menu_startup(gboolean reconfig)
73 {
74 xmlDocPtr doc;
75 xmlNodePtr node;
76 gboolean loaded = FALSE;
77 GSList *it;
78
79 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
80 (GDestroyNotify)menu_destroy_hash_value);
81
82 client_list_menu_startup(reconfig);
83 client_list_combined_menu_startup(reconfig);
84 client_menu_startup();
85
86 menu_parse_inst = parse_startup();
87
88 menu_parse_state.parent = NULL;
89 menu_parse_state.pipe_creator = NULL;
90 parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
91 parse_register(menu_parse_inst, "item", parse_menu_item,
92 &menu_parse_state);
93 parse_register(menu_parse_inst, "separator",
94 parse_menu_separator, &menu_parse_state);
95
96 for (it = config_menu_files; it; it = g_slist_next(it)) {
97 if (parse_load_menu(it->data, &doc, &node)) {
98 loaded = TRUE;
99 parse_tree(menu_parse_inst, doc, node->children);
100 xmlFreeDoc(doc);
101 } else
102 g_message(_("Unable to find a valid menu file '%s'"),
103 (const gchar*)it->data);
104 }
105 if (!loaded) {
106 if (parse_load_menu("menu.xml", &doc, &node)) {
107 parse_tree(menu_parse_inst, doc, node->children);
108 xmlFreeDoc(doc);
109 } else
110 g_message(_("Unable to find a valid menu file '%s'"),
111 "menu.xml");
112 }
113
114 g_assert(menu_parse_state.parent == NULL);
115
116 if (!reconfig)
117 client_add_destroy_notify(client_dest, NULL);
118 }
119
120 void menu_shutdown(gboolean reconfig)
121 {
122 if (!reconfig)
123 client_remove_destroy_notify(client_dest);
124
125 parse_shutdown(menu_parse_inst);
126 menu_parse_inst = NULL;
127
128 client_list_menu_shutdown(reconfig);
129 client_list_combined_menu_shutdown(reconfig);
130
131 menu_frame_hide_all();
132 g_hash_table_destroy(menu_hash);
133 menu_hash = NULL;
134 }
135
136 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
137 {
138 ObMenu *menu = val;
139 return menu->pipe_creator != NULL;
140 }
141
142 static void clear_cache(gpointer key, gpointer val, gpointer data)
143 {
144 ObMenu *menu = val;
145 if (menu->execute)
146 menu_clear_entries(menu);
147 }
148
149 void menu_clear_pipe_caches(void)
150 {
151 /* delete any pipe menus' submenus */
152 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, NULL);
153 /* empty the top level pipe menus */
154 g_hash_table_foreach(menu_hash, clear_cache, NULL);
155 }
156
157 void menu_pipe_execute(ObMenu *self)
158 {
159 xmlDocPtr doc;
160 xmlNodePtr node;
161 gchar *output;
162 GError *err = NULL;
163
164 if (!self->execute)
165 return;
166 if (self->entries) /* the entries are already created and cached */
167 return;
168
169 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
170 g_message(_("Failed to execute command for pipe-menu '%s': %s"),
171 self->execute, err->message);
172 g_error_free(err);
173 return;
174 }
175
176 if (parse_load_mem(output, strlen(output),
177 "openbox_pipe_menu", &doc, &node))
178 {
179 menu_parse_state.pipe_creator = self;
180 menu_parse_state.parent = self;
181 parse_tree(menu_parse_inst, doc, node->children);
182 xmlFreeDoc(doc);
183 } else {
184 g_message(_("Invalid output from pipe-menu '%s'"), self->execute);
185 }
186
187 g_free(output);
188 }
189
190 static ObMenu* menu_from_name(gchar *name)
191 {
192 ObMenu *self = NULL;
193
194 g_assert(name != NULL);
195
196 if (!(self = g_hash_table_lookup(menu_hash, name)))
197 g_message(_("Attempted to access menu '%s' but it does not exist"),
198 name);
199 return self;
200 }
201
202 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
203 ((c) >= 'A' && (c) <= 'Z') || \
204 ((c) >= 'a' && (c) <= 'z'))
205
206 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
207 gchar **strippedlabel, guint *position,
208 gboolean *always_show)
209 {
210 gunichar shortcut = 0;
211
212 *position = 0;
213 *always_show = FALSE;
214
215 g_assert(strippedlabel != NULL);
216
217 if (label == NULL) {
218 *strippedlabel = NULL;
219 } else {
220 gchar *i;
221
222 *strippedlabel = g_strdup(label);
223
224 /* if allow_shortcut is false, then you can't use the '_', instead you
225 have to just use the first valid character
226 */
227
228 i = strchr(*strippedlabel, '_');
229 if (allow_shortcut && i != NULL) {
230 /* there is an underscore in the string */
231
232 /* you have to use a printable ascii character for shortcuts
233 don't allow space either, so you can have like "a _ b"
234 */
235 if (VALID_SHORTCUT(*(i+1))) {
236 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
237 *position = i - *strippedlabel;
238 *always_show = TRUE;
239
240 /* remove the '_' from the string */
241 for (; *i != '\0'; ++i)
242 *i = *(i+1);
243 } else if (*(i+1) == '\0') {
244 /* no default shortcut if the '_' is the last character
245 (eg. "Exit_") for menu entries that you don't want
246 to be executed by mistake
247 */
248 *i = '\0';
249 }
250 } else {
251 /* there is no underscore, so find the first valid character to use
252 instead */
253
254 for (i = *strippedlabel; *i != '\0'; ++i)
255 if (VALID_SHORTCUT(*i)) {
256 *position = i - *strippedlabel;
257 shortcut = g_unichar_tolower(g_utf8_get_char(i));
258 break;
259 }
260 }
261 }
262 return shortcut;
263 }
264
265 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
266 gpointer data)
267 {
268 ObMenuParseState *state = data;
269 gchar *label;
270
271 if (state->parent) {
272 if (parse_attr_string("label", node, &label)) {
273 GSList *acts = NULL;
274
275 for (node = node->children; node; node = node->next)
276 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
277 ObActionsAct *a = actions_parse(i, doc, node);
278 if (a)
279 acts = g_slist_append(acts, a);
280 }
281 menu_add_normal(state->parent, -1, label, acts, TRUE);
282 g_free(label);
283 }
284 }
285 }
286
287 static void parse_menu_separator(ObParseInst *i,
288 xmlDocPtr doc, xmlNodePtr node,
289 gpointer data)
290 {
291 ObMenuParseState *state = data;
292
293 if (state->parent) {
294 gchar *label;
295
296 if (!parse_attr_string("label", node, &label))
297 label = NULL;
298
299 menu_add_separator(state->parent, -1, label);
300 g_free(label);
301 }
302 }
303
304 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
305 gpointer data)
306 {
307 ObMenuParseState *state = data;
308 gchar *name = NULL, *title = NULL, *script = NULL;
309 ObMenu *menu;
310
311 if (!parse_attr_string("id", node, &name))
312 goto parse_menu_fail;
313
314 if (!g_hash_table_lookup(menu_hash, name)) {
315 if (!parse_attr_string("label", node, &title))
316 goto parse_menu_fail;
317
318 if ((menu = menu_new(name, title, TRUE, NULL))) {
319 menu->pipe_creator = state->pipe_creator;
320 if (parse_attr_string("execute", node, &script)) {
321 menu->execute = parse_expand_tilde(script);
322 } else {
323 ObMenu *old;
324
325 old = state->parent;
326 state->parent = menu;
327 parse_tree(i, doc, node->children);
328 state->parent = old;
329 }
330 }
331 }
332
333 if (state->parent)
334 menu_add_submenu(state->parent, -1, name);
335
336 parse_menu_fail:
337 g_free(name);
338 g_free(title);
339 g_free(script);
340 }
341
342 ObMenu* menu_new(const gchar *name, const gchar *title,
343 gboolean allow_shortcut_selection, gpointer data)
344 {
345 ObMenu *self;
346
347 self = g_new0(ObMenu, 1);
348 self->name = g_strdup(name);
349 self->data = data;
350
351 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
352 &self->title, &self->shortcut_position,
353 &self->shortcut_always_show);
354
355 g_hash_table_replace(menu_hash, self->name, self);
356
357 /* Each menu has a single more_menu. When the menu spills past what
358 can fit on the screen, a new menu frame entry is created from this
359 more_menu, and a new menu frame for the submenu is created for this
360 menu, also pointing to the more_menu.
361
362 This can be done multiple times using the same more_menu.
363
364 more_menu->more_menu will always be NULL, since there is only 1 for
365 each menu. */
366 self->more_menu = g_new0(ObMenu, 1);
367 self->more_menu->name = _("More...");
368 self->more_menu->title = _("More...");
369 self->more_menu->data = data;
370 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
371
372 return self;
373 }
374
375 static void menu_destroy_hash_value(ObMenu *self)
376 {
377 /* make sure its not visible */
378 {
379 GList *it;
380 ObMenuFrame *f;
381
382 for (it = menu_frame_visible; it; it = g_list_next(it)) {
383 f = it->data;
384 if (f->menu == self)
385 menu_frame_hide_all();
386 }
387 }
388
389 if (self->destroy_func)
390 self->destroy_func(self, self->data);
391
392 menu_clear_entries(self);
393 g_free(self->name);
394 g_free(self->title);
395 g_free(self->execute);
396 g_free(self->more_menu);
397
398 g_free(self);
399 }
400
401 void menu_free(ObMenu *menu)
402 {
403 if (menu)
404 g_hash_table_remove(menu_hash, menu->name);
405 }
406
407 static gboolean menu_hide_delay_func(gpointer data)
408 {
409 menu_can_hide = TRUE;
410 return FALSE; /* no repeat */
411 }
412
413 void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
414 {
415 ObMenu *self;
416 ObMenuFrame *frame;
417
418 if (!(self = menu_from_name(name)) ||
419 grab_on_keyboard() || grab_on_pointer()) return;
420
421 /* if the requested menu is already the top visible menu, then don't
422 bother */
423 if (menu_frame_visible) {
424 frame = menu_frame_visible->data;
425 if (frame->menu == self)
426 return;
427 }
428
429 menu_frame_hide_all();
430
431 /* clear the pipe menus when showing a new menu */
432 menu_clear_pipe_caches();
433
434 frame = menu_frame_new(self, 0, client);
435 if (!menu_frame_show_topmenu(frame, x, y, mouse))
436 menu_frame_free(frame);
437 else {
438 if (!mouse) {
439 /* select the first entry if it's not a submenu and we opened
440 * the menu with the keyboard, and skip all headers */
441 GList *it = frame->entries;
442 while (it) {
443 ObMenuEntryFrame *e = it->data;
444 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
445 menu_frame_select(frame, e, FALSE);
446 break;
447 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
448 it = g_list_next(it);
449 else
450 break;
451 }
452 }
453
454 /* reset the hide timer */
455 if (!mouse)
456 menu_can_hide = TRUE;
457 else {
458 menu_can_hide = FALSE;
459 obt_main_loop_timeout_add(ob_main_loop,
460 config_menu_hide_delay * 1000,
461 menu_hide_delay_func,
462 NULL, g_direct_equal, NULL);
463 }
464 }
465 }
466
467 gboolean menu_hide_delay_reached(void)
468 {
469 return menu_can_hide;
470 }
471
472 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
473 {
474 ObMenuEntry *self;
475
476 g_assert(menu);
477
478 self = g_new0(ObMenuEntry, 1);
479 self->ref = 1;
480 self->type = type;
481 self->menu = menu;
482 self->id = id;
483
484 switch (type) {
485 case OB_MENU_ENTRY_TYPE_NORMAL:
486 self->data.normal.enabled = TRUE;
487 break;
488 case OB_MENU_ENTRY_TYPE_SUBMENU:
489 case OB_MENU_ENTRY_TYPE_SEPARATOR:
490 break;
491 }
492
493 return self;
494 }
495
496 void menu_entry_ref(ObMenuEntry *self)
497 {
498 ++self->ref;
499 }
500
501 void menu_entry_unref(ObMenuEntry *self)
502 {
503 if (self && --self->ref == 0) {
504 switch (self->type) {
505 case OB_MENU_ENTRY_TYPE_NORMAL:
506 g_free(self->data.normal.label);
507 while (self->data.normal.actions) {
508 actions_act_unref(self->data.normal.actions->data);
509 self->data.normal.actions =
510 g_slist_delete_link(self->data.normal.actions,
511 self->data.normal.actions);
512 }
513 break;
514 case OB_MENU_ENTRY_TYPE_SUBMENU:
515 g_free(self->data.submenu.name);
516 break;
517 case OB_MENU_ENTRY_TYPE_SEPARATOR:
518 g_free(self->data.separator.label);
519 break;
520 }
521
522 g_free(self);
523 }
524 }
525
526 void menu_clear_entries(ObMenu *self)
527 {
528 #ifdef DEBUG
529 /* assert that the menu isn't visible */
530 {
531 GList *it;
532 ObMenuFrame *f;
533
534 for (it = menu_frame_visible; it; it = g_list_next(it)) {
535 f = it->data;
536 g_assert(f->menu != self);
537 }
538 }
539 #endif
540
541 while (self->entries) {
542 menu_entry_unref(self->entries->data);
543 self->entries = g_list_delete_link(self->entries, self->entries);
544 }
545 self->more_menu->entries = self->entries; /* keep it in sync */
546 }
547
548 void menu_entry_remove(ObMenuEntry *self)
549 {
550 self->menu->entries = g_list_remove(self->menu->entries, self);
551 menu_entry_unref(self);
552 }
553
554 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
555 GSList *actions, gboolean allow_shortcut)
556 {
557 ObMenuEntry *e;
558
559 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
560 e->data.normal.actions = actions;
561
562 menu_entry_set_label(e, label, allow_shortcut);
563
564 self->entries = g_list_append(self->entries, e);
565 self->more_menu->entries = self->entries; /* keep it in sync */
566 return e;
567 }
568
569 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
570 {
571 ObMenuEntry *e;
572 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
573 /* points to itself */
574 e->data.submenu.name = g_strdup(self->name);
575 e->data.submenu.submenu = self;
576 e->data.submenu.show_from = show_from;
577 return e;
578 }
579
580 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
581 {
582 ObMenuEntry *e;
583
584 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
585 e->data.submenu.name = g_strdup(submenu);
586
587 self->entries = g_list_append(self->entries, e);
588 self->more_menu->entries = self->entries; /* keep it in sync */
589 return e;
590 }
591
592 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
593 {
594 ObMenuEntry *e;
595
596 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
597
598 menu_entry_set_label(e, label, FALSE);
599
600 self->entries = g_list_append(self->entries, e);
601 self->more_menu->entries = self->entries; /* keep it in sync */
602 return e;
603 }
604
605 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
606 {
607 self->show_func = func;
608 }
609
610 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
611 {
612 self->hide_func = func;
613 }
614
615 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
616 {
617 self->update_func = func;
618 }
619
620 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
621 {
622 self->execute_func = func;
623 self->more_menu->execute_func = func; /* keep it in sync */
624 }
625
626 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
627 {
628 self->destroy_func = func;
629 }
630
631 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
632 {
633 self->place_func = func;
634 }
635
636 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
637 {
638 ObMenuEntry *ret = NULL;
639 GList *it;
640
641 for (it = self->entries; it; it = g_list_next(it)) {
642 ObMenuEntry *e = it->data;
643
644 if (e->id == id) {
645 ret = e;
646 break;
647 }
648 }
649 return ret;
650 }
651
652 void menu_find_submenus(ObMenu *self)
653 {
654 GList *it;
655
656 for (it = self->entries; it; it = g_list_next(it)) {
657 ObMenuEntry *e = it->data;
658
659 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
660 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
661 }
662 }
663
664 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
665 gboolean allow_shortcut)
666 {
667 switch (self->type) {
668 case OB_MENU_ENTRY_TYPE_SEPARATOR:
669 g_free(self->data.separator.label);
670 self->data.separator.label = g_strdup(label);
671 break;
672 case OB_MENU_ENTRY_TYPE_NORMAL:
673 g_free(self->data.normal.label);
674 self->data.normal.shortcut =
675 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
676 &self->data.normal.shortcut_position,
677 &self->data.normal.shortcut_always_show);
678 break;
679 default:
680 g_assert_not_reached();
681 }
682 }
683
684 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
685 {
686 self->show_all_shortcuts = show;
687 }
This page took 0.064747 seconds and 5 git commands to generate.