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))
299 ic = RrImageCacheFindName(ob_rr_icons, icon);
303 ic = RrImageNew(ob_rr_icons);
304 if (!RrImageAddPictureName(ic, icon)) {
305 RrImageUnref(ic); /* no need to keep it around */
309 e->data.normal.icon = ic;
311 if (e->data.normal.icon)
312 e->data.normal.icon_alpha = 0xff;
321 static void parse_menu_separator(xmlNodePtr node, gpointer data)
323 ObMenuParseState *state = data;
328 if (!obt_xml_attr_string(node, "label", &label))
331 menu_add_separator(state->parent, -1, label);
336 static void parse_menu(xmlNodePtr node, gpointer data)
338 ObMenuParseState *state = data;
339 gchar *name = NULL, *title = NULL, *script = NULL;
342 if (!obt_xml_attr_string(node, "id", &name))
343 goto parse_menu_fail;
345 if (!g_hash_table_lookup(menu_hash, name)) {
346 if (!obt_xml_attr_string(node, "label", &title))
347 goto parse_menu_fail;
349 if ((menu = menu_new(name, title, TRUE, NULL))) {
350 menu->pipe_creator = state->pipe_creator;
351 if (obt_xml_attr_string(node, "execute", &script)) {
352 menu->execute = obt_paths_expand_tilde(script);
357 state->parent = menu;
358 obt_xml_tree(menu_parse_inst, node->children);
365 menu_add_submenu(state->parent, -1, name);
373 ObMenu* menu_new(const gchar *name, const gchar *title,
374 gboolean allow_shortcut_selection, gpointer data)
378 self = g_slice_new0(ObMenu);
379 self->name = g_strdup(name);
382 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
383 &self->title, &self->shortcut_position,
384 &self->shortcut_always_show);
386 g_hash_table_replace(menu_hash, self->name, self);
388 /* Each menu has a single more_menu. When the menu spills past what
389 can fit on the screen, a new menu frame entry is created from this
390 more_menu, and a new menu frame for the submenu is created for this
391 menu, also pointing to the more_menu.
393 This can be done multiple times using the same more_menu.
395 more_menu->more_menu will always be NULL, since there is only 1 for
397 self->more_menu = g_slice_new0(ObMenu);
398 self->more_menu->name = _("More...");
399 self->more_menu->title = _("More...");
400 self->more_menu->data = data;
401 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
406 static void menu_destroy_hash_value(ObMenu *self)
408 /* make sure its not visible */
413 for (it = menu_frame_visible; it; it = g_list_next(it)) {
416 menu_frame_hide_all();
420 if (self->destroy_func)
421 self->destroy_func(self, self->data);
423 menu_clear_entries(self);
426 g_free(self->execute);
427 g_slice_free(ObMenu, self->more_menu);
429 g_slice_free(ObMenu, self);
432 void menu_free(ObMenu *menu)
435 g_hash_table_remove(menu_hash, menu->name);
438 static gboolean menu_hide_delay_func(gpointer data)
440 menu_can_hide = TRUE;
442 return FALSE; /* no repeat */
445 void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
450 if (!(self = menu_from_name(name)) ||
451 grab_on_keyboard() || grab_on_pointer()) return;
453 /* if the requested menu is already the top visible menu, then don't
455 if (menu_frame_visible) {
456 frame = menu_frame_visible->data;
457 if (frame->menu == self)
461 menu_frame_hide_all();
463 /* clear the pipe menus when showing a new menu */
464 menu_clear_pipe_caches();
466 frame = menu_frame_new(self, 0, client);
467 if (!menu_frame_show_topmenu(frame, x, y, mouse))
468 menu_frame_free(frame);
471 /* select the first entry if it's not a submenu and we opened
472 * the menu with the keyboard, and skip all headers */
473 GList *it = frame->entries;
475 ObMenuEntryFrame *e = it->data;
476 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
477 menu_frame_select(frame, e, FALSE);
479 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
480 it = g_list_next(it);
486 /* reset the hide timer */
488 menu_can_hide = TRUE;
490 menu_can_hide = FALSE;
491 if (menu_timeout_id) g_source_remove(menu_timeout_id);
492 menu_timeout_id = g_timeout_add_full(G_PRIORITY_DEFAULT,
493 config_menu_hide_delay,
494 menu_hide_delay_func,
500 gboolean menu_hide_delay_reached(void)
502 return menu_can_hide;
505 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
511 self = g_slice_new0(ObMenuEntry);
518 case OB_MENU_ENTRY_TYPE_NORMAL:
519 self->data.normal.enabled = TRUE;
521 case OB_MENU_ENTRY_TYPE_SUBMENU:
522 case OB_MENU_ENTRY_TYPE_SEPARATOR:
529 void menu_entry_ref(ObMenuEntry *self)
534 void menu_entry_unref(ObMenuEntry *self)
536 if (self && --self->ref == 0) {
537 switch (self->type) {
538 case OB_MENU_ENTRY_TYPE_NORMAL:
539 RrImageUnref(self->data.normal.icon);
540 g_free(self->data.normal.label);
541 while (self->data.normal.actions) {
542 actions_act_unref(self->data.normal.actions->data);
543 self->data.normal.actions =
544 g_slist_delete_link(self->data.normal.actions,
545 self->data.normal.actions);
548 case OB_MENU_ENTRY_TYPE_SUBMENU:
549 g_free(self->data.submenu.name);
551 case OB_MENU_ENTRY_TYPE_SEPARATOR:
552 g_free(self->data.separator.label);
556 g_slice_free(ObMenuEntry, self);
560 void menu_clear_entries(ObMenu *self)
563 /* assert that the menu isn't visible */
568 for (it = menu_frame_visible; it; it = g_list_next(it)) {
570 g_assert(f->menu != self);
575 while (self->entries) {
576 menu_entry_unref(self->entries->data);
577 self->entries = g_list_delete_link(self->entries, self->entries);
579 self->more_menu->entries = self->entries; /* keep it in sync */
582 void menu_entry_remove(ObMenuEntry *self)
584 self->menu->entries = g_list_remove(self->menu->entries, self);
585 menu_entry_unref(self);
588 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
589 GSList *actions, gboolean allow_shortcut)
593 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
594 e->data.normal.actions = actions;
596 menu_entry_set_label(e, label, allow_shortcut);
598 self->entries = g_list_append(self->entries, e);
599 self->more_menu->entries = self->entries; /* keep it in sync */
603 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
606 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
607 /* points to itself */
608 e->data.submenu.name = g_strdup(self->name);
609 e->data.submenu.submenu = self;
610 e->data.submenu.show_from = show_from;
614 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
618 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
619 e->data.submenu.name = g_strdup(submenu);
621 self->entries = g_list_append(self->entries, e);
622 self->more_menu->entries = self->entries; /* keep it in sync */
626 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
630 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
632 menu_entry_set_label(e, label, FALSE);
634 self->entries = g_list_append(self->entries, e);
635 self->more_menu->entries = self->entries; /* keep it in sync */
639 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
641 self->show_func = func;
644 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
646 self->hide_func = func;
649 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
651 self->update_func = func;
654 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
656 self->execute_func = func;
657 self->more_menu->execute_func = func; /* keep it in sync */
660 void menu_set_cleanup_func(ObMenu *self, ObMenuCleanupFunc func)
662 self->cleanup_func = func;
665 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
667 self->destroy_func = func;
670 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
672 self->place_func = func;
675 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
677 ObMenuEntry *ret = NULL;
680 for (it = self->entries; it; it = g_list_next(it)) {
681 ObMenuEntry *e = it->data;
691 void menu_find_submenus(ObMenu *self)
695 for (it = self->entries; it; it = g_list_next(it)) {
696 ObMenuEntry *e = it->data;
698 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
699 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
703 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
704 gboolean allow_shortcut)
706 switch (self->type) {
707 case OB_MENU_ENTRY_TYPE_SEPARATOR:
708 g_free(self->data.separator.label);
709 self->data.separator.label = g_strdup(label);
711 case OB_MENU_ENTRY_TYPE_NORMAL:
712 g_free(self->data.normal.label);
713 self->data.normal.shortcut =
714 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
715 &self->data.normal.shortcut_position,
716 &self->data.normal.shortcut_always_show);
719 g_assert_not_reached();
723 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
725 self->show_all_shortcuts = show;