1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 menu.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
26 #include "menuframe.h"
29 #include "client_menu.h"
30 #include "client_list_menu.h"
31 #include "parser/parse.h"
33 typedef struct _ObMenuParseState ObMenuParseState;
35 struct _ObMenuParseState
41 static GHashTable *menu_hash = NULL;
42 static ObParseInst *menu_parse_inst;
43 static ObMenuParseState menu_parse_state;
45 static void menu_destroy_hash_value(ObMenu *self);
46 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
48 static void parse_menu_separator(ObParseInst *i,
49 xmlDocPtr doc, xmlNodePtr node,
51 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
54 static void client_dest(gpointer client)
56 /* menus can be associated with a client, so close any that are since
57 we are disappearing now */
58 menu_frame_hide_all_client(client);
61 void menu_startup(gboolean reconfig)
65 gboolean loaded = FALSE;
68 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
69 (GDestroyNotify)menu_destroy_hash_value);
71 client_list_menu_startup();
72 client_menu_startup();
74 menu_parse_inst = parse_startup();
76 menu_parse_state.parent = NULL;
77 menu_parse_state.pipe_creator = NULL;
78 parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
79 parse_register(menu_parse_inst, "item", parse_menu_item,
81 parse_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 (parse_load_menu(it->data, &doc, &node)) {
87 parse_tree(menu_parse_inst, doc, node->children);
92 if (parse_load_menu("menu.xml", &doc, &node)) {
93 parse_tree(menu_parse_inst, doc, node->children);
98 g_assert(menu_parse_state.parent == NULL);
101 client_add_destructor(client_dest);
104 void menu_shutdown(gboolean reconfig)
107 client_remove_destructor(client_dest);
109 parse_shutdown(menu_parse_inst);
110 menu_parse_inst = NULL;
112 menu_frame_hide_all();
113 g_hash_table_destroy(menu_hash);
117 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
120 return menu->pipe_creator == data;
123 void menu_pipe_execute(ObMenu *self)
133 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
134 g_warning("Failed to execute command for pipe-menu: %s", err->message);
139 if (parse_load_mem(output, strlen(output),
140 "openbox_pipe_menu", &doc, &node))
142 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
143 menu_clear_entries(self);
145 menu_parse_state.pipe_creator = self;
146 menu_parse_state.parent = self;
147 parse_tree(menu_parse_inst, doc, node->children);
150 g_warning("Invalid output from pipe-menu: %s", self->execute);
156 static ObMenu* menu_from_name(gchar *name)
160 g_assert(name != NULL);
162 if (!(self = g_hash_table_lookup(menu_hash, name)))
163 g_warning("Attempted to access menu '%s' but it does not exist.",
168 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
171 ObMenuParseState *state = data;
175 if (parse_attr_string("label", node, &label)) {
178 for (node = node->children; node; node = node->next)
179 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action"))
180 acts = g_slist_append(acts, action_parse
182 OB_USER_ACTION_MENU_SELECTION));
183 menu_add_normal(state->parent, -1, label, acts);
189 static void parse_menu_separator(ObParseInst *i,
190 xmlDocPtr doc, xmlNodePtr node,
193 ObMenuParseState *state = data;
196 menu_add_separator(state->parent, -1);
199 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
202 ObMenuParseState *state = data;
203 gchar *name = NULL, *title = NULL, *script = NULL;
206 if (!parse_attr_string("id", node, &name))
207 goto parse_menu_fail;
209 if (!g_hash_table_lookup(menu_hash, name)) {
210 if (!parse_attr_string("label", node, &title))
211 goto parse_menu_fail;
213 if ((menu = menu_new(name, title, NULL))) {
214 menu->pipe_creator = state->pipe_creator;
215 if (parse_attr_string("execute", node, &script)) {
216 menu->execute = parse_expand_tilde(script);
221 state->parent = menu;
222 parse_tree(i, doc, node->children);
229 menu_add_submenu(state->parent, -1, name);
237 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
241 self = g_new0(ObMenu, 1);
242 self->name = g_strdup(name);
243 self->title = g_strdup(title);
246 g_hash_table_replace(menu_hash, self->name, self);
251 static void menu_destroy_hash_value(ObMenu *self)
253 /* make sure its not visible */
258 for (it = menu_frame_visible; it; it = g_list_next(it)) {
261 menu_frame_hide_all();
265 if (self->destroy_func)
266 self->destroy_func(self, self->data);
268 menu_clear_entries(self);
271 g_free(self->execute);
276 void menu_free(ObMenu *menu)
278 g_hash_table_remove(menu_hash, menu->name);
281 void menu_show(gchar *name, gint x, gint y, ObClient *client)
286 if (!(self = menu_from_name(name))) return;
288 menu_frame_hide_all();
290 frame = menu_frame_new(self, client);
291 menu_frame_move(frame, x, y);
292 menu_frame_show(frame, NULL);
294 menu_frame_select_next(frame);
297 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
303 self = g_new0(ObMenuEntry, 1);
309 case OB_MENU_ENTRY_TYPE_NORMAL:
310 self->data.normal.enabled = TRUE;
312 case OB_MENU_ENTRY_TYPE_SUBMENU:
313 case OB_MENU_ENTRY_TYPE_SEPARATOR:
320 void menu_entry_free(ObMenuEntry *self)
323 switch (self->type) {
324 case OB_MENU_ENTRY_TYPE_NORMAL:
325 g_free(self->data.normal.label);
326 while (self->data.normal.actions) {
327 action_free(self->data.normal.actions->data);
328 self->data.normal.actions =
329 g_slist_delete_link(self->data.normal.actions,
330 self->data.normal.actions);
333 case OB_MENU_ENTRY_TYPE_SUBMENU:
334 g_free(self->data.submenu.name);
336 case OB_MENU_ENTRY_TYPE_SEPARATOR:
344 void menu_clear_entries(ObMenu *self)
347 /* assert that the menu isn't visible */
352 for (it = menu_frame_visible; it; it = g_list_next(it)) {
354 g_assert(f->menu != self);
359 while (self->entries) {
360 menu_entry_free(self->entries->data);
361 self->entries = g_list_delete_link(self->entries, self->entries);
365 void menu_entry_remove(ObMenuEntry *self)
367 self->menu->entries = g_list_remove(self->menu->entries, self);
368 menu_entry_free(self);
371 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
376 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
377 e->data.normal.label = g_strdup(label);
378 e->data.normal.actions = actions;
380 self->entries = g_list_append(self->entries, e);
384 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
388 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
389 e->data.submenu.name = g_strdup(submenu);
391 self->entries = g_list_append(self->entries, e);
395 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
399 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
401 self->entries = g_list_append(self->entries, e);
405 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
407 self->update_func = func;
410 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
412 self->execute_func = func;
415 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
417 self->destroy_func = func;
420 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
422 ObMenuEntry *ret = NULL;
425 for (it = self->entries; it; it = g_list_next(it)) {
426 ObMenuEntry *e = it->data;
436 void menu_find_submenus(ObMenu *self)
440 for (it = self->entries; it; it = g_list_next(it)) {
441 ObMenuEntry *e = it->data;
443 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
444 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);