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 "client_list_menu.h"
37 #include "client_list_combined_menu.h"
40 #include "obt/paths.h"
42 typedef struct _ObMenuParseState ObMenuParseState;
44 struct _ObMenuParseState
50 static GHashTable *menu_hash = NULL;
51 static ObtXmlInst *menu_parse_inst;
52 static ObMenuParseState menu_parse_state;
53 static gboolean menu_can_hide = FALSE;
54 static guint menu_timeout_id = 0;
56 static void menu_destroy_hash_value(ObMenu *self);
57 static void parse_menu_item(xmlNodePtr node, gpointer data);
58 static void parse_menu_separator(xmlNodePtr node, gpointer data);
59 static void parse_menu(xmlNodePtr node, gpointer data);
60 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
61 gchar **strippedlabel, guint *position,
62 gboolean *always_show);
64 void menu_startup(gboolean reconfig)
66 gboolean loaded = FALSE;
69 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
70 (GDestroyNotify)menu_destroy_hash_value);
72 client_list_menu_startup(reconfig);
73 client_list_combined_menu_startup(reconfig);
74 client_menu_startup();
76 menu_parse_inst = obt_xml_instance_new();
78 menu_parse_state.parent = NULL;
79 menu_parse_state.pipe_creator = NULL;
80 obt_xml_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
81 obt_xml_register(menu_parse_inst, "item", parse_menu_item,
83 obt_xml_register(menu_parse_inst, "separator",
84 parse_menu_separator, &menu_parse_state);
86 for (it = config_menu_files; it; it = g_slist_next(it)) {
87 if (obt_xml_load_config_file(menu_parse_inst,
93 obt_xml_tree_from_root(menu_parse_inst);
94 obt_xml_close(menu_parse_inst);
96 g_message(_("Unable to find a valid menu file \"%s\""),
97 (const gchar*)it->data);
100 if (obt_xml_load_config_file(menu_parse_inst,
105 obt_xml_tree_from_root(menu_parse_inst);
106 obt_xml_close(menu_parse_inst);
108 g_message(_("Unable to find a valid menu file \"%s\""),
112 g_assert(menu_parse_state.parent == NULL);
115 void menu_shutdown(gboolean reconfig)
117 obt_xml_instance_unref(menu_parse_inst);
118 menu_parse_inst = NULL;
120 menu_frame_hide_all();
122 client_list_combined_menu_shutdown(reconfig);
123 client_list_menu_shutdown(reconfig);
125 g_hash_table_destroy(menu_hash);
129 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
132 return menu->pipe_creator != NULL;
135 static void clear_cache(gpointer key, gpointer val, gpointer data)
139 menu_clear_entries(menu);
142 void menu_clear_pipe_caches(void)
144 /* delete any pipe menus' submenus */
145 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, NULL);
146 /* empty the top level pipe menus */
147 g_hash_table_foreach(menu_hash, clear_cache, NULL);
150 void menu_pipe_execute(ObMenu *self)
157 if (self->entries) /* the entries are already created and cached */
160 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
161 g_message(_("Failed to execute command for pipe-menu \"%s\": %s"),
162 self->execute, err->message);
167 if (obt_xml_load_mem(menu_parse_inst, output, strlen(output),
168 "openbox_pipe_menu"))
170 menu_parse_state.pipe_creator = self;
171 menu_parse_state.parent = self;
172 obt_xml_tree_from_root(menu_parse_inst);
173 obt_xml_close(menu_parse_inst);
175 g_message(_("Invalid output from pipe-menu \"%s\""), self->execute);
181 static ObMenu* menu_from_name(gchar *name)
185 g_assert(name != NULL);
187 if (!(self = g_hash_table_lookup(menu_hash, name)))
188 g_message(_("Attempted to access menu \"%s\" but it does not exist"),
193 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
194 ((c) >= 'A' && (c) <= 'Z') || \
195 ((c) >= 'a' && (c) <= 'z'))
197 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
198 gchar **strippedlabel, guint *position,
199 gboolean *always_show)
201 gunichar shortcut = 0;
204 *always_show = FALSE;
206 g_assert(strippedlabel != NULL);
209 *strippedlabel = NULL;
214 *strippedlabel = g_strdup(label);
216 /* if allow_shortcut is false, then you can't use the '_', instead you
217 have to just use the first valid character
220 /* allow __ to escape an underscore */
225 if (i && *(i+1) == '_') {
228 /* remove the escape '_' from the string */
229 for (j = i; *j != '\0'; ++j)
237 if (allow_shortcut && i != NULL) {
238 /* there is an underscore in the string */
240 /* you have to use a printable ascii character for shortcuts
241 don't allow space either, so you can have like "a _ b"
243 if (VALID_SHORTCUT(*(i+1))) {
244 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
245 *position = i - *strippedlabel;
248 /* remove the '_' from the string */
249 for (; *i != '\0'; ++i)
251 } else if (*(i+1) == '\0') {
252 /* no default shortcut if the '_' is the last character
253 (eg. "Exit_") for menu entries that you don't want
254 to be executed by mistake
259 /* there is no underscore, so find the first valid character to use
262 for (i = *strippedlabel; *i != '\0'; ++i)
263 if (VALID_SHORTCUT(*i)) {
264 *position = i - *strippedlabel;
265 shortcut = g_unichar_tolower(g_utf8_get_char(i));
273 static void parse_menu_item(xmlNodePtr node, gpointer data)
275 ObMenuParseState *state = data;
281 /* Don't try to extract "icon" attribute if icons in user-defined
282 menus are not enabled. */
284 if (obt_xml_attr_string(node, "label", &label)) {
287 ObActionList *acts = NULL;
290 c = obt_xml_find_sibling(node->children, "action");
291 p = action_parser_new();
295 cc = xmlNodeGetContent(c);
296 al = action_parser_read_string(p, (gchar*)cc);
298 acts = action_list_concat(acts, al);
300 c = obt_xml_find_sibling(c->next, "action");
302 e = menu_add_normal(state->parent, -1, label, acts, TRUE);
303 action_list_unref(acts);
305 if (config_menu_show_icons &&
306 obt_xml_attr_string(node, "icon", &icon))
308 e->data.normal.icon = RrImageNewFromName(ob_rr_icons, icon);
310 if (e->data.normal.icon)
311 e->data.normal.icon_alpha = 0xff;
320 static void parse_menu_separator(xmlNodePtr node, gpointer data)
322 ObMenuParseState *state = data;
327 if (!obt_xml_attr_string(node, "label", &label))
330 menu_add_separator(state->parent, -1, label);
335 static void parse_menu(xmlNodePtr node, gpointer data)
337 ObMenuParseState *state = data;
338 gchar *name = NULL, *title = NULL, *script = NULL;
343 if (!obt_xml_attr_string(node, "id", &name))
344 goto parse_menu_fail;
346 if (!g_hash_table_lookup(menu_hash, name)) {
347 if (!obt_xml_attr_string(node, "label", &title))
348 goto parse_menu_fail;
350 if ((menu = menu_new(name, title, TRUE, NULL))) {
351 menu->pipe_creator = state->pipe_creator;
352 if (obt_xml_attr_string(node, "execute", &script)) {
353 menu->execute = obt_paths_expand_tilde(script);
358 state->parent = menu;
359 obt_xml_tree(menu_parse_inst, node->children);
366 e = menu_add_submenu(state->parent, -1, name);
368 if (config_menu_show_icons &&
369 obt_xml_attr_string(node, "icon", &icon))
371 e->data.submenu.icon = RrImageNewFromName(ob_rr_icons, icon);
373 if (e->data.submenu.icon)
374 e->data.submenu.icon_alpha = 0xff;
386 ObMenu* menu_new(const gchar *name, const gchar *title,
387 gboolean allow_shortcut_selection, gpointer data)
391 self = g_slice_new0(ObMenu);
392 self->name = g_strdup(name);
395 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
396 &self->title, &self->shortcut_position,
397 &self->shortcut_always_show);
398 self->collate_key = g_utf8_collate_key(self->title, -1);
400 g_hash_table_replace(menu_hash, self->name, self);
402 /* Each menu has a single more_menu. When the menu spills past what
403 can fit on the screen, a new menu frame entry is created from this
404 more_menu, and a new menu frame for the submenu is created for this
405 menu, also pointing to the more_menu.
407 This can be done multiple times using the same more_menu.
409 more_menu->more_menu will always be NULL, since there is only 1 for
411 self->more_menu = g_slice_new0(ObMenu);
412 self->more_menu->name = _("More...");
413 self->more_menu->title = _("More...");
414 self->more_menu->collate_key = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
415 self->more_menu->data = data;
416 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
421 static void menu_destroy_hash_value(ObMenu *self)
423 /* make sure its not visible */
428 for (it = menu_frame_visible; it; it = g_list_next(it)) {
431 menu_frame_hide_all();
435 if (self->destroy_func)
436 self->destroy_func(self, self->data);
438 menu_clear_entries(self);
441 g_free(self->collate_key);
442 g_free(self->execute);
443 g_slice_free(ObMenu, self->more_menu);
445 g_slice_free(ObMenu, self);
448 void menu_free(ObMenu *menu)
451 g_hash_table_remove(menu_hash, menu->name);
454 static gboolean menu_hide_delay_func(gpointer data)
456 menu_can_hide = TRUE;
458 return FALSE; /* no repeat */
461 void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
466 if (!(self = menu_from_name(name)) ||
467 grab_on_keyboard() || grab_on_pointer()) return;
469 /* if the requested menu is already the top visible menu, then don't
471 if (menu_frame_visible) {
472 frame = menu_frame_visible->data;
473 if (frame->menu == self)
477 menu_frame_hide_all();
479 /* clear the pipe menus when showing a new menu */
480 menu_clear_pipe_caches();
482 frame = menu_frame_new(self, 0, client);
483 if (!menu_frame_show_topmenu(frame, x, y, mouse))
484 menu_frame_free(frame);
487 /* select the first entry if it's not a submenu and we opened
488 * the menu with the keyboard, and skip all headers */
489 GList *it = frame->entries;
491 ObMenuEntryFrame *e = it->data;
492 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
493 menu_frame_select(frame, e, FALSE);
495 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
496 it = g_list_next(it);
502 /* reset the hide timer */
504 menu_can_hide = TRUE;
506 menu_can_hide = FALSE;
507 if (menu_timeout_id) g_source_remove(menu_timeout_id);
508 menu_timeout_id = g_timeout_add_full(G_PRIORITY_DEFAULT,
509 config_menu_hide_delay,
510 menu_hide_delay_func,
516 gboolean menu_hide_delay_reached(void)
518 return menu_can_hide;
521 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
527 self = g_slice_new0(ObMenuEntry);
534 case OB_MENU_ENTRY_TYPE_NORMAL:
535 self->data.normal.enabled = TRUE;
537 case OB_MENU_ENTRY_TYPE_SUBMENU:
538 case OB_MENU_ENTRY_TYPE_SEPARATOR:
545 void menu_entry_ref(ObMenuEntry *self)
550 void menu_entry_unref(ObMenuEntry *self)
552 if (self && --self->ref == 0) {
553 switch (self->type) {
554 case OB_MENU_ENTRY_TYPE_NORMAL:
555 RrImageUnref(self->data.normal.icon);
556 g_free(self->data.normal.label);
557 g_free(self->data.normal.collate_key);
558 action_list_unref(self->data.normal.actions);
560 case OB_MENU_ENTRY_TYPE_SUBMENU:
561 RrImageUnref(self->data.submenu.icon);
562 g_free(self->data.submenu.name);
564 case OB_MENU_ENTRY_TYPE_SEPARATOR:
565 g_free(self->data.separator.label);
569 g_slice_free(ObMenuEntry, self);
573 void menu_clear_entries(ObMenu *self)
576 /* assert that the menu isn't visible */
581 for (it = menu_frame_visible; it; it = g_list_next(it)) {
583 g_assert(f->menu != self);
588 while (self->entries) {
589 menu_entry_unref(self->entries->data);
590 self->entries = g_list_delete_link(self->entries, self->entries);
592 self->more_menu->entries = self->entries; /* keep it in sync */
595 void menu_entry_remove(ObMenuEntry *self)
597 self->menu->entries = g_list_remove(self->menu->entries, self);
598 menu_entry_unref(self);
601 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
602 ObActionList *actions, gboolean allow_shortcut)
606 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
607 e->data.normal.actions = actions;
608 action_list_ref(actions);
610 menu_entry_set_label(e, label, allow_shortcut);
612 self->entries = g_list_append(self->entries, e);
613 self->more_menu->entries = self->entries; /* keep it in sync */
617 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
620 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
621 /* points to itself */
622 e->data.submenu.name = g_strdup(self->name);
623 e->data.submenu.submenu = self;
624 e->data.submenu.show_from = show_from;
628 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
632 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
633 e->data.submenu.name = g_strdup(submenu);
635 self->entries = g_list_append(self->entries, e);
636 self->more_menu->entries = self->entries; /* keep it in sync */
640 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
644 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
646 menu_entry_set_label(e, label, FALSE);
648 self->entries = g_list_append(self->entries, e);
649 self->more_menu->entries = self->entries; /* keep it in sync */
653 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
655 self->show_func = func;
658 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
660 self->hide_func = func;
663 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
665 self->update_func = func;
668 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
670 self->execute_func = func;
671 self->more_menu->execute_func = func; /* keep it in sync */
674 void menu_set_cleanup_func(ObMenu *self, ObMenuCleanupFunc func)
676 self->cleanup_func = func;
679 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
681 self->destroy_func = func;
684 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
686 self->place_func = func;
689 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
691 ObMenuEntry *ret = NULL;
694 for (it = self->entries; it; it = g_list_next(it)) {
695 ObMenuEntry *e = it->data;
705 void menu_find_submenus(ObMenu *self)
709 for (it = self->entries; it; it = g_list_next(it)) {
710 ObMenuEntry *e = it->data;
712 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
713 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
717 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
718 gboolean allow_shortcut)
720 switch (self->type) {
721 case OB_MENU_ENTRY_TYPE_SEPARATOR:
722 g_free(self->data.separator.label);
723 self->data.separator.label = g_strdup(label);
725 case OB_MENU_ENTRY_TYPE_NORMAL:
726 g_free(self->data.normal.label);
727 g_free(self->data.normal.collate_key);
728 self->data.normal.shortcut =
729 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
730 &self->data.normal.shortcut_position,
731 &self->data.normal.shortcut_always_show);
732 self->data.normal.collate_key =
733 g_utf8_collate_key(self->data.normal.label, -1);
736 g_assert_not_reached();
740 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
742 self->show_all_shortcuts = show;
745 static int sort_func(const void *a, const void *b) {
746 const ObMenuEntry *e[2] = {*(ObMenuEntry**)a, *(ObMenuEntry**)b};
750 for (i = 0; i < 2; ++i) {
751 if (e[i]->type == OB_MENU_ENTRY_TYPE_NORMAL)
752 k[i] = e[i]->data.normal.collate_key;
754 g_assert(e[i]->type == OB_MENU_ENTRY_TYPE_SUBMENU);
755 if (e[i]->data.submenu.submenu)
756 k[i] = e[i]->data.submenu.submenu->collate_key;
758 return -1; /* arbitrary really.. the submenu doesn't exist. */
761 return strcmp(k[0], k[1]);
765 @param start The first entry in the range to sort.
766 @param end The last entry in the range to sort.
768 static void sort_range(ObMenu *self, GList *start, GList *end, guint len)
775 ar = g_slice_alloc(sizeof(ObMenuEntry*) * len);
776 for (i = 0, it = start; it != g_list_next(end); ++i, it = g_list_next(it))
778 qsort(ar, len, sizeof(ObMenuEntry*), sort_func);
779 for (i = 0, it = start; it != g_list_next(end); ++i, it = g_list_next(it))
781 g_slice_free1(sizeof(ObMenuEntry*) * len, ar);
784 void menu_sort_entries(ObMenu *self)
786 GList *it, *start, *end, *last;
789 /* need the submenus to know their labels for sorting */
790 menu_find_submenus(self);
792 start = self->entries;
794 for (it = self->entries; it; it = g_list_next(it)) {
795 ObMenuEntry *e = it->data;
796 if (e->type == OB_MENU_ENTRY_TYPE_SEPARATOR) {
797 end = g_list_previous(it);
798 sort_range(self, start, end, len);
800 it = g_list_next(it); /* skip over the separator */
808 sort_range(self, start, last, len);