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(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(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(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);
389 g_hash_table_replace(menu_hash, self->name, self);
391 /* Each menu has a single more_menu. When the menu spills past what
392 can fit on the screen, a new menu frame entry is created from this
393 more_menu, and a new menu frame for the submenu is created for this
394 menu, also pointing to the more_menu.
396 This can be done multiple times using the same more_menu.
398 more_menu->more_menu will always be NULL, since there is only 1 for
400 self->more_menu = g_slice_new0(ObMenu);
401 self->more_menu->name = _("More...");
402 self->more_menu->title = _("More...");
403 self->more_menu->data = data;
404 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
409 static void menu_destroy_hash_value(ObMenu *self)
411 /* make sure its not visible */
416 for (it = menu_frame_visible; it; it = g_list_next(it)) {
419 menu_frame_hide_all();
423 if (self->destroy_func)
424 self->destroy_func(self, self->data);
426 menu_clear_entries(self);
429 g_free(self->execute);
430 g_slice_free(ObMenu, self->more_menu);
432 g_slice_free(ObMenu, self);
435 void menu_free(ObMenu *menu)
438 g_hash_table_remove(menu_hash, menu->name);
441 static gboolean menu_hide_delay_func(gpointer data)
443 menu_can_hide = TRUE;
445 return FALSE; /* no repeat */
448 void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
453 if (!(self = menu_from_name(name)) ||
454 grab_on_keyboard() || grab_on_pointer()) return;
456 /* if the requested menu is already the top visible menu, then don't
458 if (menu_frame_visible) {
459 frame = menu_frame_visible->data;
460 if (frame->menu == self)
464 menu_frame_hide_all();
466 /* clear the pipe menus when showing a new menu */
467 menu_clear_pipe_caches();
469 frame = menu_frame_new(self, 0, client);
470 if (!menu_frame_show_topmenu(frame, x, y, mouse))
471 menu_frame_free(frame);
474 /* select the first entry if it's not a submenu and we opened
475 * the menu with the keyboard, and skip all headers */
476 GList *it = frame->entries;
478 ObMenuEntryFrame *e = it->data;
479 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
480 menu_frame_select(frame, e, FALSE);
482 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
483 it = g_list_next(it);
489 /* reset the hide timer */
491 menu_can_hide = TRUE;
493 menu_can_hide = FALSE;
494 if (menu_timeout_id) g_source_remove(menu_timeout_id);
495 menu_timeout_id = g_timeout_add_full(G_PRIORITY_DEFAULT,
496 config_menu_hide_delay,
497 menu_hide_delay_func,
503 gboolean menu_hide_delay_reached(void)
505 return menu_can_hide;
508 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
514 self = g_slice_new0(ObMenuEntry);
521 case OB_MENU_ENTRY_TYPE_NORMAL:
522 self->data.normal.enabled = TRUE;
524 case OB_MENU_ENTRY_TYPE_SUBMENU:
525 case OB_MENU_ENTRY_TYPE_SEPARATOR:
532 void menu_entry_ref(ObMenuEntry *self)
537 void menu_entry_unref(ObMenuEntry *self)
539 if (self && --self->ref == 0) {
540 switch (self->type) {
541 case OB_MENU_ENTRY_TYPE_NORMAL:
542 RrImageUnref(self->data.normal.icon);
543 g_free(self->data.normal.label);
544 while (self->data.normal.actions) {
545 actions_act_unref(self->data.normal.actions->data);
546 self->data.normal.actions =
547 g_slist_delete_link(self->data.normal.actions,
548 self->data.normal.actions);
551 case OB_MENU_ENTRY_TYPE_SUBMENU:
552 RrImageUnref(self->data.submenu.icon);
553 g_free(self->data.submenu.name);
555 case OB_MENU_ENTRY_TYPE_SEPARATOR:
556 g_free(self->data.separator.label);
560 g_slice_free(ObMenuEntry, self);
564 void menu_clear_entries(ObMenu *self)
567 /* assert that the menu isn't visible */
572 for (it = menu_frame_visible; it; it = g_list_next(it)) {
574 g_assert(f->menu != self);
579 while (self->entries) {
580 menu_entry_unref(self->entries->data);
581 self->entries = g_list_delete_link(self->entries, self->entries);
583 self->more_menu->entries = self->entries; /* keep it in sync */
586 void menu_entry_remove(ObMenuEntry *self)
588 self->menu->entries = g_list_remove(self->menu->entries, self);
589 menu_entry_unref(self);
592 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
593 GSList *actions, gboolean allow_shortcut)
597 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
598 e->data.normal.actions = actions;
600 menu_entry_set_label(e, label, allow_shortcut);
602 self->entries = g_list_append(self->entries, e);
603 self->more_menu->entries = self->entries; /* keep it in sync */
607 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
610 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
611 /* points to itself */
612 e->data.submenu.name = g_strdup(self->name);
613 e->data.submenu.submenu = self;
614 e->data.submenu.show_from = show_from;
618 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
622 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
623 e->data.submenu.name = g_strdup(submenu);
625 self->entries = g_list_append(self->entries, e);
626 self->more_menu->entries = self->entries; /* keep it in sync */
630 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
634 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
636 menu_entry_set_label(e, label, FALSE);
638 self->entries = g_list_append(self->entries, e);
639 self->more_menu->entries = self->entries; /* keep it in sync */
643 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
645 self->show_func = func;
648 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
650 self->hide_func = func;
653 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
655 self->update_func = func;
658 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
660 self->execute_func = func;
661 self->more_menu->execute_func = func; /* keep it in sync */
664 void menu_set_cleanup_func(ObMenu *self, ObMenuCleanupFunc func)
666 self->cleanup_func = func;
669 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
671 self->destroy_func = func;
674 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
676 self->place_func = func;
679 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
681 ObMenuEntry *ret = NULL;
684 for (it = self->entries; it; it = g_list_next(it)) {
685 ObMenuEntry *e = it->data;
695 void menu_find_submenus(ObMenu *self)
699 for (it = self->entries; it; it = g_list_next(it)) {
700 ObMenuEntry *e = it->data;
702 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
703 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
707 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
708 gboolean allow_shortcut)
710 switch (self->type) {
711 case OB_MENU_ENTRY_TYPE_SEPARATOR:
712 g_free(self->data.separator.label);
713 self->data.separator.label = g_strdup(label);
715 case OB_MENU_ENTRY_TYPE_NORMAL:
716 g_free(self->data.normal.label);
717 self->data.normal.shortcut =
718 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
719 &self->data.normal.shortcut_position,
720 &self->data.normal.shortcut_always_show);
723 g_assert_not_reached();
727 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
729 self->show_all_shortcuts = show;