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.
27 #include "menuframe.h"
31 #include "client_menu.h"
32 #include "client_list_menu.h"
33 #include "client_list_combined_menu.h"
35 #include "parser/parse.h"
37 typedef struct _ObMenuParseState ObMenuParseState;
39 struct _ObMenuParseState
45 static GHashTable *menu_hash = NULL;
46 static ObParseInst *menu_parse_inst;
47 static ObMenuParseState menu_parse_state;
49 static void menu_destroy_hash_value(ObMenu *self);
50 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
52 static void parse_menu_separator(ObParseInst *i,
53 xmlDocPtr doc, xmlNodePtr node,
55 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
58 static void client_dest(ObClient *client, gpointer data)
60 /* menus can be associated with a client, so close any that are since
61 we are disappearing now */
62 menu_frame_hide_all_client(client);
65 void menu_startup(gboolean reconfig)
69 gboolean loaded = FALSE;
72 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
73 (GDestroyNotify)menu_destroy_hash_value);
75 client_list_menu_startup(reconfig);
76 client_list_combined_menu_startup(reconfig);
77 client_menu_startup();
79 menu_parse_inst = parse_startup();
81 menu_parse_state.parent = NULL;
82 menu_parse_state.pipe_creator = NULL;
83 parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
84 parse_register(menu_parse_inst, "item", parse_menu_item,
86 parse_register(menu_parse_inst, "separator",
87 parse_menu_separator, &menu_parse_state);
89 for (it = config_menu_files; it; it = g_slist_next(it)) {
90 if (parse_load_menu(it->data, &doc, &node)) {
92 parse_tree(menu_parse_inst, doc, node->children);
95 g_message(_("Unable to find a valid menu file '%s'"),
96 (const gchar*)it->data);
99 if (parse_load_menu("menu.xml", &doc, &node)) {
100 parse_tree(menu_parse_inst, doc, node->children);
103 g_message(_("Unable to find a valid menu file '%s'"),
107 g_assert(menu_parse_state.parent == NULL);
110 client_add_destructor(client_dest, NULL);
113 void menu_shutdown(gboolean reconfig)
116 client_remove_destructor(client_dest);
118 parse_shutdown(menu_parse_inst);
119 menu_parse_inst = NULL;
121 client_list_menu_shutdown(reconfig);
122 client_list_combined_menu_shutdown(reconfig);
124 menu_frame_hide_all();
125 g_hash_table_destroy(menu_hash);
129 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
132 return menu->pipe_creator == data;
135 void menu_pipe_execute(ObMenu *self)
145 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
146 g_message(_("Failed to execute command for pipe-menu '%s': %s"),
147 self->execute, err->message);
152 if (parse_load_mem(output, strlen(output),
153 "openbox_pipe_menu", &doc, &node))
155 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
156 menu_clear_entries(self);
158 menu_parse_state.pipe_creator = self;
159 menu_parse_state.parent = self;
160 parse_tree(menu_parse_inst, doc, node->children);
163 g_message(_("Invalid output from pipe-menu '%s'"), self->execute);
169 static ObMenu* menu_from_name(gchar *name)
173 g_assert(name != NULL);
175 if (!(self = g_hash_table_lookup(menu_hash, name)))
176 g_message(_("Attempted to access menu '%s' but it does not exist"),
181 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
184 ObMenuParseState *state = data;
188 if (parse_attr_string("label", node, &label)) {
191 for (node = node->children; node; node = node->next)
192 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
193 ObAction *a = action_parse
194 (i, doc, node, OB_USER_ACTION_MENU_SELECTION);
196 acts = g_slist_append(acts, a);
198 menu_add_normal(state->parent, -1, label, acts);
204 static void parse_menu_separator(ObParseInst *i,
205 xmlDocPtr doc, xmlNodePtr node,
208 ObMenuParseState *state = data;
213 if (!parse_attr_string("label", node, &label))
216 menu_add_separator(state->parent, -1, label);
221 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
224 ObMenuParseState *state = data;
225 gchar *name = NULL, *title = NULL, *script = NULL;
228 if (!parse_attr_string("id", node, &name))
229 goto parse_menu_fail;
231 if (!g_hash_table_lookup(menu_hash, name)) {
232 if (!parse_attr_string("label", node, &title))
233 goto parse_menu_fail;
235 if ((menu = menu_new(name, title, NULL))) {
236 menu->pipe_creator = state->pipe_creator;
237 if (parse_attr_string("execute", node, &script)) {
238 menu->execute = parse_expand_tilde(script);
243 state->parent = menu;
244 parse_tree(i, doc, node->children);
251 menu_add_submenu(state->parent, -1, name);
259 ObMenu* menu_new(const gchar *name, const gchar *title, gpointer data)
263 self = g_new0(ObMenu, 1);
264 self->name = g_strdup(name);
265 self->title = g_strdup(title);
268 g_hash_table_replace(menu_hash, self->name, self);
273 static void menu_destroy_hash_value(ObMenu *self)
275 /* make sure its not visible */
280 for (it = menu_frame_visible; it; it = g_list_next(it)) {
283 menu_frame_hide_all();
287 if (self->destroy_func)
288 self->destroy_func(self, self->data);
290 menu_clear_entries(self);
293 g_free(self->execute);
298 void menu_free(ObMenu *menu)
300 g_hash_table_remove(menu_hash, menu->name);
303 void menu_show(gchar *name, gint x, gint y, ObClient *client)
308 if (!(self = menu_from_name(name))
309 || keyboard_interactively_grabbed()) return;
311 /* if the requested menu is already the top visible menu, then don't
313 if (menu_frame_visible) {
314 frame = menu_frame_visible->data;
315 if (frame->menu == self)
319 menu_frame_hide_all();
321 frame = menu_frame_new(self, client);
322 if (!menu_frame_show_topmenu(frame, x, y))
323 menu_frame_free(frame);
324 else if (frame->entries) {
325 ObMenuEntryFrame *e = frame->entries->data;
326 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
327 e->entry->data.normal.enabled)
328 menu_frame_select(frame, e);
332 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
338 self = g_new0(ObMenuEntry, 1);
344 case OB_MENU_ENTRY_TYPE_NORMAL:
345 self->data.normal.enabled = TRUE;
347 case OB_MENU_ENTRY_TYPE_SUBMENU:
348 case OB_MENU_ENTRY_TYPE_SEPARATOR:
355 void menu_entry_free(ObMenuEntry *self)
358 switch (self->type) {
359 case OB_MENU_ENTRY_TYPE_NORMAL:
360 g_free(self->data.normal.label);
361 while (self->data.normal.actions) {
362 action_unref(self->data.normal.actions->data);
363 self->data.normal.actions =
364 g_slist_delete_link(self->data.normal.actions,
365 self->data.normal.actions);
368 case OB_MENU_ENTRY_TYPE_SUBMENU:
369 g_free(self->data.submenu.name);
371 case OB_MENU_ENTRY_TYPE_SEPARATOR:
379 void menu_clear_entries(ObMenu *self)
382 /* assert that the menu isn't visible */
387 for (it = menu_frame_visible; it; it = g_list_next(it)) {
389 g_assert(f->menu != self);
394 while (self->entries) {
395 menu_entry_free(self->entries->data);
396 self->entries = g_list_delete_link(self->entries, self->entries);
400 void menu_entry_remove(ObMenuEntry *self)
402 self->menu->entries = g_list_remove(self->menu->entries, self);
403 menu_entry_free(self);
406 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
411 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
412 e->data.normal.label = g_strdup(label);
413 e->data.normal.actions = actions;
415 self->entries = g_list_append(self->entries, e);
419 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
423 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
424 e->data.submenu.name = g_strdup(submenu);
426 self->entries = g_list_append(self->entries, e);
430 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
434 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
435 e->data.separator.label = g_strdup(label);
437 self->entries = g_list_append(self->entries, e);
441 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
443 self->update_func = func;
446 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
448 self->execute_func = func;
451 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
453 self->destroy_func = func;
456 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
458 ObMenuEntry *ret = NULL;
461 for (it = self->entries; it; it = g_list_next(it)) {
462 ObMenuEntry *e = it->data;
472 void menu_find_submenus(ObMenu *self)
476 for (it = self->entries; it; it = g_list_next(it)) {
477 ObMenuEntry *e = it->data;
479 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
480 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);