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