1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 menu.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
29 #include "menuframe.h"
33 #include "client_menu.h"
34 #include "client_list_menu.h"
35 #include "client_list_combined_menu.h"
38 #include "obt/paths.h"
40 typedef struct _ObMenuParseState ObMenuParseState;
42 struct _ObMenuParseState
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;
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);
62 void menu_startup(gboolean reconfig)
64 gboolean loaded = FALSE;
67 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
68 (GDestroyNotify)menu_destroy_hash_value);
70 client_list_menu_startup(reconfig);
71 client_list_combined_menu_startup(reconfig);
72 client_menu_startup();
74 menu_parse_inst = obt_xml_instance_new();
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,
81 obt_xml_register(menu_parse_inst, "separator",
82 parse_menu_separator, &menu_parse_state);
84 for (it = config_menu_files; it; it = g_slist_next(it)) {
85 if (obt_xml_load_config_file(menu_parse_inst,
91 obt_xml_tree_from_root(menu_parse_inst);
92 obt_xml_close(menu_parse_inst);
94 g_message(_("Unable to find a valid menu file \"%s\""),
95 (const gchar*)it->data);
98 if (obt_xml_load_config_file(menu_parse_inst,
103 obt_xml_tree_from_root(menu_parse_inst);
104 obt_xml_close(menu_parse_inst);
106 g_message(_("Unable to find a valid menu file \"%s\""),
110 g_assert(menu_parse_state.parent == NULL);
113 void menu_shutdown(gboolean reconfig)
115 obt_xml_instance_unref(menu_parse_inst);
116 menu_parse_inst = NULL;
118 menu_frame_hide_all();
120 client_list_combined_menu_shutdown(reconfig);
121 client_list_menu_shutdown(reconfig);
123 g_hash_table_destroy(menu_hash);
127 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
130 return menu->pipe_creator != NULL;
133 static void clear_cache(gpointer key, gpointer val, gpointer data)
137 menu_clear_entries(menu);
140 void menu_clear_pipe_caches(void)
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);
148 void menu_pipe_execute(ObMenu *self)
155 if (self->entries) /* the entries are already created and cached */
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);
165 if (obt_xml_load_mem(menu_parse_inst, output, strlen(output),
166 "openbox_pipe_menu"))
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);
173 g_message(_("Invalid output from pipe-menu \"%s\""), self->execute);
179 static ObMenu* menu_from_name(gchar *name)
183 g_assert(name != NULL);
185 if (!(self = g_hash_table_lookup(menu_hash, name)))
186 g_message(_("Attempted to access menu \"%s\" but it does not exist"),
191 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
192 ((c) >= 'A' && (c) <= 'Z') || \
193 ((c) >= 'a' && (c) <= 'z'))
195 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
196 gchar **strippedlabel, guint *position,
197 gboolean *always_show)
199 gunichar shortcut = 0;
202 *always_show = FALSE;
204 g_assert(strippedlabel != NULL);
207 *strippedlabel = NULL;
212 *strippedlabel = g_strdup(label);
214 /* if allow_shortcut is false, then you can't use the '_', instead you
215 have to just use the first valid character
218 /* allow __ to escape an underscore */
223 if (i && *(i+1) == '_') {
226 /* remove the escape '_' from the string */
227 for (j = i; *j != '\0'; ++j)
235 if (allow_shortcut && i != NULL) {
236 /* there is an underscore in the string */
238 /* you have to use a printable ascii character for shortcuts
239 don't allow space either, so you can have like "a _ b"
241 if (VALID_SHORTCUT(*(i+1))) {
242 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
243 *position = i - *strippedlabel;
246 /* remove the '_' from the string */
247 for (; *i != '\0'; ++i)
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
257 /* there is no underscore, so find the first valid character to use
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));
271 static void parse_menu_item(xmlNodePtr node, gpointer data)
273 ObMenuParseState *state = data;
279 /* Don't try to extract "icon" attribute if icons in user-defined
280 menus are not enabled. */
282 if (obt_xml_attr_string_unstripped(node, "label", &label)) {
286 c = obt_xml_find_node(node->children, "action");
288 ObActionsAct *action = actions_parse(c);
290 acts = g_slist_append(acts, action);
291 c = obt_xml_find_node(c->next, "action");
293 e = menu_add_normal(state->parent, -1, label, acts, TRUE);
295 if (config_menu_show_icons &&
296 obt_xml_attr_string(node, "icon", &icon))
298 e->data.normal.icon = RrImageNewFromName(ob_rr_icons, icon);
300 if (e->data.normal.icon)
301 e->data.normal.icon_alpha = 0xff;
310 static void parse_menu_separator(xmlNodePtr node, gpointer data)
312 ObMenuParseState *state = data;
317 if (!obt_xml_attr_string_unstripped(node, "label", &label))
320 menu_add_separator(state->parent, -1, label);
325 static void parse_menu(xmlNodePtr node, gpointer data)
327 ObMenuParseState *state = data;
328 gchar *name = NULL, *title = NULL, *script = NULL;
333 if (!obt_xml_attr_string(node, "id", &name))
334 goto parse_menu_fail;
336 if (!g_hash_table_lookup(menu_hash, name)) {
337 if (!obt_xml_attr_string_unstripped(node, "label", &title))
338 goto parse_menu_fail;
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);
348 state->parent = menu;
349 obt_xml_tree(menu_parse_inst, node->children);
356 e = menu_add_submenu(state->parent, -1, name);
358 if (config_menu_show_icons &&
359 obt_xml_attr_string(node, "icon", &icon))
361 e->data.submenu.icon = RrImageNewFromName(ob_rr_icons, icon);
363 if (e->data.submenu.icon)
364 e->data.submenu.icon_alpha = 0xff;
376 ObMenu* menu_new(const gchar *name, const gchar *title,
377 gboolean allow_shortcut_selection, gpointer data)
381 self = g_slice_new0(ObMenu);
382 self->name = g_strdup(name);
385 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
386 &self->title, &self->shortcut_position,
387 &self->shortcut_always_show);
388 self->collate_key = g_utf8_collate_key(self->title, -1);
390 g_hash_table_replace(menu_hash, self->name, self);
392 /* Each menu has a single more_menu. When the menu spills past what
393 can fit on the screen, a new menu frame entry is created from this
394 more_menu, and a new menu frame for the submenu is created for this
395 menu, also pointing to the more_menu.
397 This can be done multiple times using the same more_menu.
399 more_menu->more_menu will always be NULL, since there is only 1 for
401 self->more_menu = g_slice_new0(ObMenu);
402 self->more_menu->name = _("More...");
403 self->more_menu->title = _("More...");
404 self->more_menu->collate_key = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
405 self->more_menu->data = data;
406 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
411 static void menu_destroy_hash_value(ObMenu *self)
413 /* make sure its not visible */
418 for (it = menu_frame_visible; it; it = g_list_next(it)) {
421 menu_frame_hide_all();
425 if (self->destroy_func)
426 self->destroy_func(self, self->data);
428 menu_clear_entries(self);
431 g_free(self->collate_key);
432 g_free(self->execute);
433 g_slice_free(ObMenu, self->more_menu);
435 g_slice_free(ObMenu, self);
438 void menu_free(ObMenu *menu)
441 g_hash_table_remove(menu_hash, menu->name);
444 static gboolean menu_hide_delay_func(gpointer data)
446 menu_can_hide = TRUE;
448 return FALSE; /* no repeat */
451 void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
456 if (!(self = menu_from_name(name)) ||
457 grab_on_keyboard() || grab_on_pointer()) return;
459 /* if the requested menu is already the top visible menu, then don't
461 if (menu_frame_visible) {
462 frame = menu_frame_visible->data;
463 if (frame->menu == self)
467 menu_frame_hide_all();
469 /* clear the pipe menus when showing a new menu */
470 menu_clear_pipe_caches();
472 frame = menu_frame_new(self, 0, client);
473 if (!menu_frame_show_topmenu(frame, x, y, mouse))
474 menu_frame_free(frame);
477 /* select the first entry if it's not a submenu and we opened
478 * the menu with the keyboard, and skip all headers */
479 GList *it = frame->entries;
481 ObMenuEntryFrame *e = it->data;
482 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
483 menu_frame_select(frame, e, FALSE);
485 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
486 it = g_list_next(it);
492 /* reset the hide timer */
494 menu_can_hide = TRUE;
496 menu_can_hide = FALSE;
497 if (menu_timeout_id) g_source_remove(menu_timeout_id);
498 menu_timeout_id = g_timeout_add_full(G_PRIORITY_DEFAULT,
499 config_menu_hide_delay,
500 menu_hide_delay_func,
506 gboolean menu_hide_delay_reached(void)
508 return menu_can_hide;
511 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
517 self = g_slice_new0(ObMenuEntry);
524 case OB_MENU_ENTRY_TYPE_NORMAL:
525 self->data.normal.enabled = TRUE;
527 case OB_MENU_ENTRY_TYPE_SUBMENU:
528 case OB_MENU_ENTRY_TYPE_SEPARATOR:
535 void menu_entry_ref(ObMenuEntry *self)
540 void menu_entry_unref(ObMenuEntry *self)
542 if (self && --self->ref == 0) {
543 switch (self->type) {
544 case OB_MENU_ENTRY_TYPE_NORMAL:
545 RrImageUnref(self->data.normal.icon);
546 g_free(self->data.normal.label);
547 g_free(self->data.normal.collate_key);
548 while (self->data.normal.actions) {
549 actions_act_unref(self->data.normal.actions->data);
550 self->data.normal.actions =
551 g_slist_delete_link(self->data.normal.actions,
552 self->data.normal.actions);
555 case OB_MENU_ENTRY_TYPE_SUBMENU:
556 RrImageUnref(self->data.submenu.icon);
557 g_free(self->data.submenu.name);
559 case OB_MENU_ENTRY_TYPE_SEPARATOR:
560 g_free(self->data.separator.label);
564 g_slice_free(ObMenuEntry, self);
568 void menu_clear_entries(ObMenu *self)
571 /* assert that the menu isn't visible */
576 for (it = menu_frame_visible; it; it = g_list_next(it)) {
578 g_assert(f->menu != self);
583 while (self->entries) {
584 menu_entry_unref(self->entries->data);
585 self->entries = g_list_delete_link(self->entries, self->entries);
587 self->more_menu->entries = self->entries; /* keep it in sync */
590 void menu_entry_remove(ObMenuEntry *self)
592 self->menu->entries = g_list_remove(self->menu->entries, self);
593 menu_entry_unref(self);
596 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
597 GSList *actions, gboolean allow_shortcut)
601 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
602 e->data.normal.actions = actions;
604 menu_entry_set_label(e, label, allow_shortcut);
606 self->entries = g_list_append(self->entries, e);
607 self->more_menu->entries = self->entries; /* keep it in sync */
611 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
614 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
615 /* points to itself */
616 e->data.submenu.name = g_strdup(self->name);
617 e->data.submenu.submenu = self;
618 e->data.submenu.show_from = show_from;
622 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
626 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
627 e->data.submenu.name = g_strdup(submenu);
629 self->entries = g_list_append(self->entries, e);
630 self->more_menu->entries = self->entries; /* keep it in sync */
634 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
638 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
640 menu_entry_set_label(e, label, FALSE);
642 self->entries = g_list_append(self->entries, e);
643 self->more_menu->entries = self->entries; /* keep it in sync */
647 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
649 self->show_func = func;
652 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
654 self->hide_func = func;
657 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
659 self->update_func = func;
662 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
664 self->execute_func = func;
665 self->more_menu->execute_func = func; /* keep it in sync */
668 void menu_set_cleanup_func(ObMenu *self, ObMenuCleanupFunc func)
670 self->cleanup_func = func;
673 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
675 self->destroy_func = func;
678 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
680 self->place_func = func;
683 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
685 ObMenuEntry *ret = NULL;
688 for (it = self->entries; it; it = g_list_next(it)) {
689 ObMenuEntry *e = it->data;
699 void menu_find_submenus(ObMenu *self)
703 for (it = self->entries; it; it = g_list_next(it)) {
704 ObMenuEntry *e = it->data;
706 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
707 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
711 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
712 gboolean allow_shortcut)
714 switch (self->type) {
715 case OB_MENU_ENTRY_TYPE_SEPARATOR:
716 g_free(self->data.separator.label);
717 self->data.separator.label = g_strdup(label);
719 case OB_MENU_ENTRY_TYPE_NORMAL:
720 g_free(self->data.normal.label);
721 g_free(self->data.normal.collate_key);
722 self->data.normal.shortcut =
723 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
724 &self->data.normal.shortcut_position,
725 &self->data.normal.shortcut_always_show);
726 self->data.normal.collate_key =
727 g_utf8_collate_key(self->data.normal.label, -1);
730 g_assert_not_reached();
734 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
736 self->show_all_shortcuts = show;
739 static int sort_func(const void *a, const void *b) {
740 const ObMenuEntry *e[2] = {*(ObMenuEntry**)a, *(ObMenuEntry**)b};
744 for (i = 0; i < 2; ++i) {
745 if (e[i]->type == OB_MENU_ENTRY_TYPE_NORMAL)
746 k[i] = e[i]->data.normal.collate_key;
748 g_assert(e[i]->type == OB_MENU_ENTRY_TYPE_SUBMENU);
749 if (e[i]->data.submenu.submenu)
750 k[i] = e[i]->data.submenu.submenu->collate_key;
752 return -1; /* arbitrary really.. the submenu doesn't exist. */
755 return strcmp(k[0], k[1]);
759 @param start The first entry in the range to sort.
760 @param end The last entry in the range to sort.
762 static void sort_range(ObMenu *self, GList *start, GList *end, guint len)
769 ar = g_slice_alloc(sizeof(ObMenuEntry*) * len);
770 for (i = 0, it = start; it != g_list_next(end); ++i, it = g_list_next(it))
772 qsort(ar, len, sizeof(ObMenuEntry*), sort_func);
773 for (i = 0, it = start; it != g_list_next(end); ++i, it = g_list_next(it))
775 g_slice_free1(sizeof(ObMenuEntry*) * len, ar);
778 void menu_sort_entries(ObMenu *self)
780 GList *it, *start, *end, *last;
783 /* need the submenus to know their labels for sorting */
784 menu_find_submenus(self);
786 start = self->entries;
788 for (it = self->entries; it; it = g_list_next(it)) {
789 ObMenuEntry *e = it->data;
790 if (e->type == OB_MENU_ENTRY_TYPE_SEPARATOR) {
791 end = g_list_previous(it);
792 sort_range(self, start, end, len);
794 it = g_list_next(it); /* skip over the separator */
802 sort_range(self, start, last, len);