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 client_list_menu_shutdown(reconfig);
119 client_list_combined_menu_shutdown(reconfig);
121 menu_frame_hide_all();
122 g_hash_table_destroy(menu_hash);
126 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
129 return menu->pipe_creator != NULL;
132 static void clear_cache(gpointer key, gpointer val, gpointer data)
136 menu_clear_entries(menu);
139 void menu_clear_pipe_caches(void)
141 /* delete any pipe menus' submenus */
142 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, NULL);
143 /* empty the top level pipe menus */
144 g_hash_table_foreach(menu_hash, clear_cache, NULL);
147 void menu_pipe_execute(ObMenu *self)
154 if (self->entries) /* the entries are already created and cached */
157 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
158 g_message(_("Failed to execute command for pipe-menu \"%s\": %s"),
159 self->execute, err->message);
164 if (obt_xml_load_mem(menu_parse_inst, output, strlen(output),
165 "openbox_pipe_menu"))
167 menu_parse_state.pipe_creator = self;
168 menu_parse_state.parent = self;
169 obt_xml_tree_from_root(menu_parse_inst);
170 obt_xml_close(menu_parse_inst);
172 g_message(_("Invalid output from pipe-menu \"%s\""), self->execute);
178 static ObMenu* menu_from_name(gchar *name)
182 g_assert(name != NULL);
184 if (!(self = g_hash_table_lookup(menu_hash, name)))
185 g_message(_("Attempted to access menu \"%s\" but it does not exist"),
190 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
191 ((c) >= 'A' && (c) <= 'Z') || \
192 ((c) >= 'a' && (c) <= 'z'))
194 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
195 gchar **strippedlabel, guint *position,
196 gboolean *always_show)
198 gunichar shortcut = 0;
201 *always_show = FALSE;
203 g_assert(strippedlabel != NULL);
206 *strippedlabel = NULL;
211 *strippedlabel = g_strdup(label);
213 /* if allow_shortcut is false, then you can't use the '_', instead you
214 have to just use the first valid character
217 /* allow __ to escape an underscore */
222 if (i && *(i+1) == '_') {
225 /* remove the escape '_' from the string */
226 for (j = i; *j != '\0'; ++j)
234 if (allow_shortcut && i != NULL) {
235 /* there is an underscore in the string */
237 /* you have to use a printable ascii character for shortcuts
238 don't allow space either, so you can have like "a _ b"
240 if (VALID_SHORTCUT(*(i+1))) {
241 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
242 *position = i - *strippedlabel;
245 /* remove the '_' from the string */
246 for (; *i != '\0'; ++i)
248 } else if (*(i+1) == '\0') {
249 /* no default shortcut if the '_' is the last character
250 (eg. "Exit_") for menu entries that you don't want
251 to be executed by mistake
256 /* there is no underscore, so find the first valid character to use
259 for (i = *strippedlabel; *i != '\0'; ++i)
260 if (VALID_SHORTCUT(*i)) {
261 *position = i - *strippedlabel;
262 shortcut = g_unichar_tolower(g_utf8_get_char(i));
270 static void parse_menu_item(xmlNodePtr node, gpointer data)
272 ObMenuParseState *state = data;
278 /* Don't try to extract "icon" attribute if icons in user-defined
279 menus are not enabled. */
281 if (obt_xml_attr_string(node, "label", &label)) {
285 c = obt_xml_find_node(node->children, "action");
287 ObActionsAct *action = actions_parse(c);
289 acts = g_slist_append(acts, action);
290 c = obt_xml_find_node(node->next, "action");
292 e = menu_add_normal(state->parent, -1, label, acts, TRUE);
294 if (config_menu_show_icons &&
295 obt_xml_attr_string(node, "icon", &icon))
297 e->data.normal.icon = RrImageNewFromName(ob_rr_icons, icon);
299 if (e->data.normal.icon)
300 e->data.normal.icon_alpha = 0xff;
309 static void parse_menu_separator(xmlNodePtr node, gpointer data)
311 ObMenuParseState *state = data;
316 if (!obt_xml_attr_string(node, "label", &label))
319 menu_add_separator(state->parent, -1, label);
324 static void parse_menu(xmlNodePtr node, gpointer data)
326 ObMenuParseState *state = data;
327 gchar *name = NULL, *title = NULL, *script = NULL;
332 if (!obt_xml_attr_string(node, "id", &name))
333 goto parse_menu_fail;
335 if (!g_hash_table_lookup(menu_hash, name)) {
336 if (!obt_xml_attr_string(node, "label", &title))
337 goto parse_menu_fail;
339 if ((menu = menu_new(name, title, TRUE, NULL))) {
340 menu->pipe_creator = state->pipe_creator;
341 if (obt_xml_attr_string(node, "execute", &script)) {
342 menu->execute = obt_paths_expand_tilde(script);
347 state->parent = menu;
348 obt_xml_tree(menu_parse_inst, node->children);
355 e = menu_add_submenu(state->parent, -1, name);
357 if (config_menu_show_icons &&
358 obt_xml_attr_string(node, "icon", &icon))
360 e->data.submenu.icon = RrImageNewFromName(ob_rr_icons, icon);
362 if (e->data.submenu.icon)
363 e->data.submenu.icon_alpha = 0xff;
375 ObMenu* menu_new(const gchar *name, const gchar *title,
376 gboolean allow_shortcut_selection, gpointer data)
380 self = g_slice_new0(ObMenu);
381 self->name = g_strdup(name);
384 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
385 &self->title, &self->shortcut_position,
386 &self->shortcut_always_show);
388 g_hash_table_replace(menu_hash, self->name, self);
390 /* Each menu has a single more_menu. When the menu spills past what
391 can fit on the screen, a new menu frame entry is created from this
392 more_menu, and a new menu frame for the submenu is created for this
393 menu, also pointing to the more_menu.
395 This can be done multiple times using the same more_menu.
397 more_menu->more_menu will always be NULL, since there is only 1 for
399 self->more_menu = g_slice_new0(ObMenu);
400 self->more_menu->name = _("More...");
401 self->more_menu->title = _("More...");
402 self->more_menu->data = data;
403 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
408 static void menu_destroy_hash_value(ObMenu *self)
410 /* make sure its not visible */
415 for (it = menu_frame_visible; it; it = g_list_next(it)) {
418 menu_frame_hide_all();
422 if (self->destroy_func)
423 self->destroy_func(self, self->data);
425 menu_clear_entries(self);
428 g_free(self->execute);
429 g_slice_free(ObMenu, self->more_menu);
431 g_slice_free(ObMenu, self);
434 void menu_free(ObMenu *menu)
437 g_hash_table_remove(menu_hash, menu->name);
440 static gboolean menu_hide_delay_func(gpointer data)
442 menu_can_hide = TRUE;
444 return FALSE; /* no repeat */
447 void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
452 if (!(self = menu_from_name(name)) ||
453 grab_on_keyboard() || grab_on_pointer()) return;
455 /* if the requested menu is already the top visible menu, then don't
457 if (menu_frame_visible) {
458 frame = menu_frame_visible->data;
459 if (frame->menu == self)
463 menu_frame_hide_all();
465 /* clear the pipe menus when showing a new menu */
466 menu_clear_pipe_caches();
468 frame = menu_frame_new(self, 0, client);
469 if (!menu_frame_show_topmenu(frame, x, y, mouse))
470 menu_frame_free(frame);
473 /* select the first entry if it's not a submenu and we opened
474 * the menu with the keyboard, and skip all headers */
475 GList *it = frame->entries;
477 ObMenuEntryFrame *e = it->data;
478 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
479 menu_frame_select(frame, e, FALSE);
481 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
482 it = g_list_next(it);
488 /* reset the hide timer */
490 menu_can_hide = TRUE;
492 menu_can_hide = FALSE;
493 if (menu_timeout_id) g_source_remove(menu_timeout_id);
494 menu_timeout_id = g_timeout_add_full(G_PRIORITY_DEFAULT,
495 config_menu_hide_delay,
496 menu_hide_delay_func,
502 gboolean menu_hide_delay_reached(void)
504 return menu_can_hide;
507 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
513 self = g_slice_new0(ObMenuEntry);
520 case OB_MENU_ENTRY_TYPE_NORMAL:
521 self->data.normal.enabled = TRUE;
523 case OB_MENU_ENTRY_TYPE_SUBMENU:
524 case OB_MENU_ENTRY_TYPE_SEPARATOR:
531 void menu_entry_ref(ObMenuEntry *self)
536 void menu_entry_unref(ObMenuEntry *self)
538 if (self && --self->ref == 0) {
539 switch (self->type) {
540 case OB_MENU_ENTRY_TYPE_NORMAL:
541 RrImageUnref(self->data.normal.icon);
542 g_free(self->data.normal.label);
543 while (self->data.normal.actions) {
544 actions_act_unref(self->data.normal.actions->data);
545 self->data.normal.actions =
546 g_slist_delete_link(self->data.normal.actions,
547 self->data.normal.actions);
550 case OB_MENU_ENTRY_TYPE_SUBMENU:
551 RrImageUnref(self->data.submenu.icon);
552 g_free(self->data.submenu.name);
554 case OB_MENU_ENTRY_TYPE_SEPARATOR:
555 g_free(self->data.separator.label);
559 g_slice_free(ObMenuEntry, self);
563 void menu_clear_entries(ObMenu *self)
566 /* assert that the menu isn't visible */
571 for (it = menu_frame_visible; it; it = g_list_next(it)) {
573 g_assert(f->menu != self);
578 while (self->entries) {
579 menu_entry_unref(self->entries->data);
580 self->entries = g_list_delete_link(self->entries, self->entries);
582 self->more_menu->entries = self->entries; /* keep it in sync */
585 void menu_entry_remove(ObMenuEntry *self)
587 self->menu->entries = g_list_remove(self->menu->entries, self);
588 menu_entry_unref(self);
591 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
592 GSList *actions, gboolean allow_shortcut)
596 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
597 e->data.normal.actions = actions;
599 menu_entry_set_label(e, label, allow_shortcut);
601 self->entries = g_list_append(self->entries, e);
602 self->more_menu->entries = self->entries; /* keep it in sync */
606 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
609 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
610 /* points to itself */
611 e->data.submenu.name = g_strdup(self->name);
612 e->data.submenu.submenu = self;
613 e->data.submenu.show_from = show_from;
617 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
621 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
622 e->data.submenu.name = g_strdup(submenu);
624 self->entries = g_list_append(self->entries, e);
625 self->more_menu->entries = self->entries; /* keep it in sync */
629 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
633 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
635 menu_entry_set_label(e, label, FALSE);
637 self->entries = g_list_append(self->entries, e);
638 self->more_menu->entries = self->entries; /* keep it in sync */
642 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
644 self->show_func = func;
647 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
649 self->hide_func = func;
652 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
654 self->update_func = func;
657 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
659 self->execute_func = func;
660 self->more_menu->execute_func = func; /* keep it in sync */
663 void menu_set_cleanup_func(ObMenu *self, ObMenuCleanupFunc func)
665 self->cleanup_func = func;
668 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
670 self->destroy_func = func;
673 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
675 self->place_func = func;
678 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
680 ObMenuEntry *ret = NULL;
683 for (it = self->entries; it; it = g_list_next(it)) {
684 ObMenuEntry *e = it->data;
694 void menu_find_submenus(ObMenu *self)
698 for (it = self->entries; it; it = g_list_next(it)) {
699 ObMenuEntry *e = it->data;
701 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
702 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
706 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
707 gboolean allow_shortcut)
709 switch (self->type) {
710 case OB_MENU_ENTRY_TYPE_SEPARATOR:
711 g_free(self->data.separator.label);
712 self->data.separator.label = g_strdup(label);
714 case OB_MENU_ENTRY_TYPE_NORMAL:
715 g_free(self->data.normal.label);
716 self->data.normal.shortcut =
717 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
718 &self->data.normal.shortcut_position,
719 &self->data.normal.shortcut_always_show);
722 g_assert_not_reached();
726 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
728 self->show_all_shortcuts = show;