12 #include "client_menu.h"
13 #include "client_list_menu.h"
14 #include "parser/parse.h"
16 typedef struct _ObMenuParseState ObMenuParseState;
18 struct _ObMenuParseState
24 static GHashTable *menu_hash = NULL;
25 static ObParseInst *menu_parse_inst;
26 static ObMenuParseState menu_parse_state;
28 static void menu_destroy_hash_value(ObMenu *self);
29 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
31 static void parse_menu_separator(ObParseInst *i,
32 xmlDocPtr doc, xmlNodePtr node,
34 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
37 static gboolean menu_open(gchar *file, xmlDocPtr *doc, xmlNodePtr *node)
39 gboolean loaded = TRUE;
42 p = g_build_filename(g_get_home_dir(), ".openbox", file, NULL);
43 if (!parse_load(p, "openbox_menu", doc, node)) {
45 p = g_build_filename(RCDIR, file, NULL);
46 if (!parse_load(p, "openbox_menu", doc, node)) {
49 if (!parse_load(p, "openbox_menu", doc, node)) {
50 g_warning("Failed to load menu from '%s'", file);
59 static void client_dest(gpointer client)
61 /* menus can be associated with a client, so close any that are since
62 we are disappearing now */
63 menu_frame_hide_all_client(client);
66 void menu_startup(gboolean reconfig)
70 gboolean loaded = FALSE;
73 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
74 (GDestroyNotify)menu_destroy_hash_value);
76 client_list_menu_startup();
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 (menu_open(it->data, &doc, &node)) {
92 parse_tree(menu_parse_inst, doc, node->xmlChildrenNode);
97 if (menu_open("menu.xml", &doc, &node)) {
98 parse_tree(menu_parse_inst, doc, node->xmlChildrenNode);
103 g_assert(menu_parse_state.parent == NULL);
106 client_add_destructor(client_dest);
109 void menu_shutdown(gboolean reconfig)
112 client_remove_destructor(client_dest);
114 parse_shutdown(menu_parse_inst);
115 menu_parse_inst = NULL;
117 menu_frame_hide_all();
118 g_hash_table_destroy(menu_hash);
122 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
125 return menu->pipe_creator == data;
128 void menu_pipe_execute(ObMenu *self)
138 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
139 g_warning("Failed to execute command for pipe-menu: %s", err->message);
144 if (parse_load_mem(output, strlen(output),
145 "openbox_pipe_menu", &doc, &node))
147 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
148 menu_clear_entries(self);
150 menu_parse_state.pipe_creator = self;
151 menu_parse_state.parent = self;
152 parse_tree(menu_parse_inst, doc, node->xmlChildrenNode);
155 g_warning("Invalid output from pipe-menu: %s", self->execute);
159 static ObMenu* menu_from_name(gchar *name)
163 g_assert(name != NULL);
165 if (!(self = g_hash_table_lookup(menu_hash, name)))
166 g_warning("Attempted to access menu '%s' but it does not exist.",
171 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
174 ObMenuParseState *state = data;
178 if (parse_attr_string("label", node, &label)) {
181 for (node = node->xmlChildrenNode; node; node = node->next)
182 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action"))
183 acts = g_slist_append(acts, action_parse(i, doc, node));
184 menu_add_normal(state->parent, -1, label, acts);
190 static void parse_menu_separator(ObParseInst *i,
191 xmlDocPtr doc, xmlNodePtr node,
194 ObMenuParseState *state = data;
197 menu_add_separator(state->parent, -1);
200 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
203 ObMenuParseState *state = data;
204 gchar *name = NULL, *title = NULL, *script = NULL;
207 if (!parse_attr_string("id", node, &name))
208 goto parse_menu_fail;
210 if (!g_hash_table_lookup(menu_hash, name)) {
211 if (!parse_attr_string("label", node, &title))
212 goto parse_menu_fail;
214 if ((menu = menu_new(name, title, NULL))) {
215 menu->pipe_creator = state->pipe_creator;
216 if (parse_attr_string("execute", node, &script)) {
217 menu->execute = ob_expand_tilde(script);
222 state->parent = menu;
223 parse_tree(i, doc, node->xmlChildrenNode);
230 menu_add_submenu(state->parent, -1, name);
238 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
242 self = g_new0(ObMenu, 1);
243 self->name = g_strdup(name);
244 self->title = g_strdup(title);
247 g_hash_table_replace(menu_hash, self->name, self);
252 static void menu_destroy_hash_value(ObMenu *self)
254 /* XXX make sure its not visible */
256 if (self->destroy_func)
257 self->destroy_func(self, self->data);
259 menu_clear_entries(self);
262 g_free(self->execute);
265 void menu_free(ObMenu *menu)
267 g_hash_table_remove(menu_hash, menu->name);
270 void menu_show(gchar *name, gint x, gint y, ObClient *client)
275 if (!(self = menu_from_name(name))) return;
277 frame = menu_frame_new(self, client);
278 menu_frame_move(frame, x, y);
279 menu_frame_show(frame, NULL);
281 menu_frame_select_next(frame);
284 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
290 self = g_new0(ObMenuEntry, 1);
296 case OB_MENU_ENTRY_TYPE_NORMAL:
297 self->data.normal.enabled = TRUE;
299 case OB_MENU_ENTRY_TYPE_SUBMENU:
300 case OB_MENU_ENTRY_TYPE_SEPARATOR:
307 void menu_entry_free(ObMenuEntry *self)
310 switch (self->type) {
311 case OB_MENU_ENTRY_TYPE_NORMAL:
312 g_free(self->data.normal.label);
313 while (self->data.normal.actions) {
314 action_free(self->data.normal.actions->data);
315 self->data.normal.actions =
316 g_slist_delete_link(self->data.normal.actions,
317 self->data.normal.actions);
320 case OB_MENU_ENTRY_TYPE_SUBMENU:
321 g_free(self->data.submenu.name);
323 case OB_MENU_ENTRY_TYPE_SEPARATOR:
331 void menu_clear_entries(ObMenu *self)
333 /* XXX assert that the menu isn't visible */
335 while (self->entries) {
336 menu_entry_free(self->entries->data);
337 self->entries = g_list_delete_link(self->entries, self->entries);
341 void menu_entry_remove(ObMenuEntry *self)
343 self->menu->entries = g_list_remove(self->menu->entries, self);
344 menu_entry_free(self);
347 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
352 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
353 e->data.normal.label = g_strdup(label);
354 e->data.normal.actions = actions;
356 self->entries = g_list_append(self->entries, e);
360 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
364 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
365 e->data.submenu.name = g_strdup(submenu);
367 self->entries = g_list_append(self->entries, e);
371 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
375 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
377 self->entries = g_list_append(self->entries, e);
381 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
383 self->update_func = func;
386 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
388 self->execute_func = func;
391 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
393 self->destroy_func = func;
396 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
398 ObMenuEntry *ret = NULL;
401 for (it = self->entries; it; it = g_list_next(it)) {
402 ObMenuEntry *e = it->data;
412 void menu_find_submenus(ObMenu *self)
416 for (it = self->entries; it; it = g_list_next(it)) {
417 ObMenuEntry *e = it->data;
419 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
420 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);