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.
28 #include "action_list.h"
29 #include "action_parser.h"
31 #include "menuframe.h"
35 #include "client_menu.h"
36 #include "apps_menu.h"
37 #include "client_list_menu.h"
38 #include "client_list_combined_menu.h"
41 #include "obt/paths.h"
43 typedef struct _ObMenuParseState ObMenuParseState;
45 struct _ObMenuParseState
51 static GHashTable *menu_hash = NULL;
52 static ObtXmlInst *menu_parse_inst;
53 static ObMenuParseState menu_parse_state;
54 static gboolean menu_can_hide = FALSE;
55 static guint menu_timeout_id = 0;
57 static void menu_destroy_hash_value(ObMenu *self);
58 static void parse_menu_item(xmlNodePtr node, gpointer data);
59 static void parse_menu_separator(xmlNodePtr node, gpointer data);
60 static void parse_menu(xmlNodePtr node, gpointer data);
61 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
62 gchar **strippedlabel, guint *position,
63 gboolean *always_show);
65 void menu_startup(gboolean reconfig)
67 gboolean loaded = FALSE;
70 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
71 (GDestroyNotify)menu_destroy_hash_value);
73 apps_menu_startup(reconfig);
74 client_list_menu_startup(reconfig);
75 client_list_combined_menu_startup(reconfig);
76 client_menu_startup();
78 menu_parse_inst = obt_xml_instance_new();
80 menu_parse_state.parent = NULL;
81 menu_parse_state.pipe_creator = NULL;
82 obt_xml_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
83 obt_xml_register(menu_parse_inst, "item", parse_menu_item,
85 obt_xml_register(menu_parse_inst, "separator",
86 parse_menu_separator, &menu_parse_state);
88 for (it = config_menu_files; it; it = g_slist_next(it)) {
89 if (obt_xml_load_config_file(menu_parse_inst,
95 obt_xml_tree_from_root(menu_parse_inst);
96 obt_xml_close(menu_parse_inst);
98 g_message(_("Unable to find a valid menu file \"%s\""),
99 (const gchar*)it->data);
102 if (obt_xml_load_config_file(menu_parse_inst,
107 obt_xml_tree_from_root(menu_parse_inst);
108 obt_xml_close(menu_parse_inst);
110 g_message(_("Unable to find a valid menu file \"%s\""),
114 g_assert(menu_parse_state.parent == NULL);
117 void menu_shutdown(gboolean reconfig)
119 obt_xml_instance_unref(menu_parse_inst);
120 menu_parse_inst = NULL;
122 menu_frame_hide_all();
124 client_list_combined_menu_shutdown(reconfig);
125 client_list_menu_shutdown(reconfig);
126 apps_menu_shutdown(reconfig);
128 g_hash_table_destroy(menu_hash);
132 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
135 return menu->pipe_creator != NULL;
138 static void clear_cache(gpointer key, gpointer val, gpointer data)
142 menu_clear_entries(menu);
145 void menu_clear_pipe_caches(void)
147 /* delete any pipe menus' submenus */
148 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, NULL);
149 /* empty the top level pipe menus */
150 g_hash_table_foreach(menu_hash, clear_cache, NULL);
153 void menu_pipe_execute(ObMenu *self)
160 if (self->entries) /* the entries are already created and cached */
163 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
164 g_message(_("Failed to execute command for pipe-menu \"%s\": %s"),
165 self->execute, err->message);
170 if (obt_xml_load_mem(menu_parse_inst, output, strlen(output),
171 "openbox_pipe_menu"))
173 menu_parse_state.pipe_creator = self;
174 menu_parse_state.parent = self;
175 obt_xml_tree_from_root(menu_parse_inst);
176 obt_xml_close(menu_parse_inst);
178 g_message(_("Invalid output from pipe-menu \"%s\""), self->execute);
184 static ObMenu* menu_from_name(gchar *name)
188 g_assert(name != NULL);
190 if (!(self = g_hash_table_lookup(menu_hash, name)))
191 g_message(_("Attempted to access menu \"%s\" but it does not exist"),
196 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
197 ((c) >= 'A' && (c) <= 'Z') || \
198 ((c) >= 'a' && (c) <= 'z'))
200 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
201 gchar **strippedlabel, guint *position,
202 gboolean *always_show)
204 gunichar shortcut = 0;
207 *always_show = FALSE;
209 g_assert(strippedlabel != NULL);
212 *strippedlabel = NULL;
217 *strippedlabel = g_strdup(label);
219 /* if allow_shortcut is false, then you can't use the '_', instead you
220 have to just use the first valid character
223 /* allow __ to escape an underscore */
228 if (i && *(i+1) == '_') {
231 /* remove the escape '_' from the string */
232 for (j = i; *j != '\0'; ++j)
240 if (allow_shortcut && i != NULL) {
241 /* there is an underscore in the string */
243 /* you have to use a printable ascii character for shortcuts
244 don't allow space either, so you can have like "a _ b"
246 if (VALID_SHORTCUT(*(i+1))) {
247 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
248 *position = i - *strippedlabel;
251 /* remove the '_' from the string */
252 for (; *i != '\0'; ++i)
254 } else if (*(i+1) == '\0') {
255 /* no default shortcut if the '_' is the last character
256 (eg. "Exit_") for menu entries that you don't want
257 to be executed by mistake
262 /* there is no underscore, so find the first valid character to use
265 for (i = *strippedlabel; *i != '\0'; ++i)
266 if (VALID_SHORTCUT(*i)) {
267 *position = i - *strippedlabel;
268 shortcut = g_unichar_tolower(g_utf8_get_char(i));
276 static void parse_menu_item(xmlNodePtr node, gpointer data)
278 ObMenuParseState *state = data;
284 /* Don't try to extract "icon" attribute if icons in user-defined
285 menus are not enabled. */
287 if (obt_xml_attr_string(node, "label", &label)) {
290 ObActionList *acts = NULL;
293 c = obt_xml_find_sibling(node->children, "action");
294 p = action_parser_new();
298 cc = xmlNodeGetContent(c);
299 al = action_parser_read_string(p, (gchar*)cc);
301 acts = action_list_concat(acts, al);
303 c = obt_xml_find_sibling(node->next, "action");
305 e = menu_add_normal(state->parent, -1, label, acts, TRUE);
306 action_list_unref(acts);
308 if (config_menu_show_icons &&
309 obt_xml_attr_string(node, "icon", &icon))
311 e->data.normal.icon = RrImageNewFromName(ob_rr_icons, icon);
313 if (e->data.normal.icon)
314 e->data.normal.icon_alpha = 0xff;
323 static void parse_menu_separator(xmlNodePtr node, gpointer data)
325 ObMenuParseState *state = data;
330 if (!obt_xml_attr_string(node, "label", &label))
333 menu_add_separator(state->parent, -1, label);
338 static void parse_menu(xmlNodePtr node, gpointer data)
340 ObMenuParseState *state = data;
341 gchar *name = NULL, *title = NULL, *script = NULL;
346 if (!obt_xml_attr_string(node, "id", &name))
347 goto parse_menu_fail;
349 if (!g_hash_table_lookup(menu_hash, name)) {
350 if (!obt_xml_attr_string(node, "label", &title))
351 goto parse_menu_fail;
353 if ((menu = menu_new(name, title, TRUE, NULL))) {
354 menu->pipe_creator = state->pipe_creator;
355 if (obt_xml_attr_string(node, "execute", &script)) {
356 menu->execute = obt_paths_expand_tilde(script);
361 state->parent = menu;
362 obt_xml_tree(menu_parse_inst, node->children);
369 e = menu_add_submenu(state->parent, -1, name);
371 if (config_menu_show_icons &&
372 obt_xml_attr_string(node, "icon", &icon))
374 e->data.submenu.icon = RrImageNewFromName(ob_rr_icons, icon);
376 if (e->data.submenu.icon)
377 e->data.submenu.icon_alpha = 0xff;
389 ObMenu* menu_new(const gchar *name, const gchar *title,
390 gboolean allow_shortcut_selection, gpointer data)
394 self = g_slice_new0(ObMenu);
395 self->name = g_strdup(name);
398 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
399 &self->title, &self->shortcut_position,
400 &self->shortcut_always_show);
401 self->collate_key = g_utf8_collate_key(self->title, -1);
403 g_hash_table_replace(menu_hash, self->name, self);
405 /* Each menu has a single more_menu. When the menu spills past what
406 can fit on the screen, a new menu frame entry is created from this
407 more_menu, and a new menu frame for the submenu is created for this
408 menu, also pointing to the more_menu.
410 This can be done multiple times using the same more_menu.
412 more_menu->more_menu will always be NULL, since there is only 1 for
414 self->more_menu = g_slice_new0(ObMenu);
415 self->more_menu->name = _("More...");
416 self->more_menu->title = _("More...");
417 self->more_menu->collate_key = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
418 self->more_menu->data = data;
419 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
424 static void menu_destroy_hash_value(ObMenu *self)
426 /* make sure its not visible */
431 for (it = menu_frame_visible; it; it = g_list_next(it)) {
434 menu_frame_hide_all();
438 if (self->destroy_func)
439 self->destroy_func(self, self->data);
441 menu_clear_entries(self);
444 g_free(self->collate_key);
445 g_free(self->execute);
446 g_slice_free(ObMenu, self->more_menu);
448 g_slice_free(ObMenu, self);
451 void menu_free(ObMenu *menu)
454 g_hash_table_remove(menu_hash, menu->name);
457 static gboolean menu_hide_delay_func(gpointer data)
459 menu_can_hide = TRUE;
461 return FALSE; /* no repeat */
464 void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
469 if (!(self = menu_from_name(name)) ||
470 grab_on_keyboard() || grab_on_pointer()) return;
472 /* if the requested menu is already the top visible menu, then don't
474 if (menu_frame_visible) {
475 frame = menu_frame_visible->data;
476 if (frame->menu == self)
480 menu_frame_hide_all();
482 /* clear the pipe menus when showing a new menu */
483 menu_clear_pipe_caches();
485 frame = menu_frame_new(self, 0, client);
486 if (!menu_frame_show_topmenu(frame, x, y, mouse))
487 menu_frame_free(frame);
490 /* select the first entry if it's not a submenu and we opened
491 * the menu with the keyboard, and skip all headers */
492 GList *it = frame->entries;
494 ObMenuEntryFrame *e = it->data;
495 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
496 menu_frame_select(frame, e, FALSE);
498 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
499 it = g_list_next(it);
505 /* reset the hide timer */
507 menu_can_hide = TRUE;
509 menu_can_hide = FALSE;
510 if (menu_timeout_id) g_source_remove(menu_timeout_id);
511 menu_timeout_id = g_timeout_add_full(G_PRIORITY_DEFAULT,
512 config_menu_hide_delay,
513 menu_hide_delay_func,
519 gboolean menu_hide_delay_reached(void)
521 return menu_can_hide;
524 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
530 self = g_slice_new0(ObMenuEntry);
537 case OB_MENU_ENTRY_TYPE_NORMAL:
538 self->data.normal.enabled = TRUE;
540 case OB_MENU_ENTRY_TYPE_SUBMENU:
541 case OB_MENU_ENTRY_TYPE_SEPARATOR:
548 void menu_entry_ref(ObMenuEntry *self)
553 void menu_entry_unref(ObMenuEntry *self)
555 if (self && --self->ref == 0) {
556 switch (self->type) {
557 case OB_MENU_ENTRY_TYPE_NORMAL:
558 RrImageUnref(self->data.normal.icon);
559 g_free(self->data.normal.label);
560 g_free(self->data.normal.collate_key);
561 action_list_unref(self->data.normal.actions);
563 case OB_MENU_ENTRY_TYPE_SUBMENU:
564 RrImageUnref(self->data.submenu.icon);
565 g_free(self->data.submenu.name);
567 case OB_MENU_ENTRY_TYPE_SEPARATOR:
568 g_free(self->data.separator.label);
572 g_slice_free(ObMenuEntry, self);
576 void menu_clear_entries(ObMenu *self)
579 /* assert that the menu isn't visible */
584 for (it = menu_frame_visible; it; it = g_list_next(it)) {
586 g_assert(f->menu != self);
591 while (self->entries) {
592 menu_entry_unref(self->entries->data);
593 self->entries = g_list_delete_link(self->entries, self->entries);
595 self->more_menu->entries = self->entries; /* keep it in sync */
598 void menu_entry_remove(ObMenuEntry *self)
600 self->menu->entries = g_list_remove(self->menu->entries, self);
601 menu_entry_unref(self);
604 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
605 ObActionList *actions, gboolean allow_shortcut)
609 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
610 e->data.normal.actions = actions;
611 action_list_ref(actions);
613 menu_entry_set_label(e, label, allow_shortcut);
615 self->entries = g_list_append(self->entries, e);
616 self->more_menu->entries = self->entries; /* keep it in sync */
620 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
623 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
624 /* points to itself */
625 e->data.submenu.name = g_strdup(self->name);
626 e->data.submenu.submenu = self;
627 e->data.submenu.show_from = show_from;
631 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
635 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
636 e->data.submenu.name = g_strdup(submenu);
638 self->entries = g_list_append(self->entries, e);
639 self->more_menu->entries = self->entries; /* keep it in sync */
643 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
647 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
649 menu_entry_set_label(e, label, FALSE);
651 self->entries = g_list_append(self->entries, e);
652 self->more_menu->entries = self->entries; /* keep it in sync */
656 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
658 self->show_func = func;
661 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
663 self->hide_func = func;
666 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
668 self->update_func = func;
671 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
673 self->execute_func = func;
674 self->more_menu->execute_func = func; /* keep it in sync */
677 void menu_set_cleanup_func(ObMenu *self, ObMenuCleanupFunc func)
679 self->cleanup_func = func;
682 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
684 self->destroy_func = func;
687 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
689 self->place_func = func;
692 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
694 ObMenuEntry *ret = NULL;
697 for (it = self->entries; it; it = g_list_next(it)) {
698 ObMenuEntry *e = it->data;
708 void menu_find_submenus(ObMenu *self)
712 for (it = self->entries; it; it = g_list_next(it)) {
713 ObMenuEntry *e = it->data;
715 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
716 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
720 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
721 gboolean allow_shortcut)
723 switch (self->type) {
724 case OB_MENU_ENTRY_TYPE_SEPARATOR:
725 g_free(self->data.separator.label);
726 self->data.separator.label = g_strdup(label);
728 case OB_MENU_ENTRY_TYPE_NORMAL:
729 g_free(self->data.normal.label);
730 g_free(self->data.normal.collate_key);
731 self->data.normal.shortcut =
732 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
733 &self->data.normal.shortcut_position,
734 &self->data.normal.shortcut_always_show);
735 self->data.normal.collate_key =
736 g_utf8_collate_key(self->data.normal.label, -1);
739 g_assert_not_reached();
743 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
745 self->show_all_shortcuts = show;
748 static int sort_func(const void *a, const void *b) {
749 const ObMenuEntry *e[2] = {*(ObMenuEntry**)a, *(ObMenuEntry**)b};
753 for (i = 0; i < 2; ++i) {
754 if (e[i]->type == OB_MENU_ENTRY_TYPE_NORMAL)
755 k[i] = e[i]->data.normal.collate_key;
757 g_assert(e[i]->type == OB_MENU_ENTRY_TYPE_SUBMENU);
758 if (e[i]->data.submenu.submenu)
759 k[i] = e[i]->data.submenu.submenu->collate_key;
761 return -1; /* arbitrary really.. the submenu doesn't exist. */
764 return strcmp(k[0], k[1]);
768 @param start The first entry in the range to sort.
769 @param end The last entry in the range to sort.
771 static void sort_range(ObMenu *self, GList *start, GList *end, guint len)
778 ar = g_slice_alloc(sizeof(ObMenuEntry*) * len);
779 for (i = 0, it = start; it != g_list_next(end); ++i, it = g_list_next(it))
781 qsort(ar, len, sizeof(ObMenuEntry*), sort_func);
782 for (i = 0, it = start; it != g_list_next(end); ++i, it = g_list_next(it))
784 g_slice_free1(sizeof(ObMenuEntry*) * len, ar);
787 void menu_sort_entries(ObMenu *self)
789 GList *it, *start, *end, *last;
792 /* need the submenus to know their labels for sorting */
793 menu_find_submenus(self);
795 start = self->entries;
797 for (it = self->entries; it; it = g_list_next(it)) {
798 ObMenuEntry *e = it->data;
799 if (e->type == OB_MENU_ENTRY_TYPE_SEPARATOR) {
800 end = g_list_previous(it);
801 sort_range(self, start, end, len);
803 it = g_list_next(it); /* skip over the separator */
811 sort_range(self, start, last, len);