11 #include "client_menu.h"
12 #include "client_list_menu.h"
13 #include "parser/parse.h"
15 typedef struct _ObMenuParseState ObMenuParseState;
17 struct _ObMenuParseState
23 static GHashTable *menu_hash = NULL;
24 static ObParseInst *menu_parse_inst;
25 static ObMenuParseState menu_parse_state;
27 static void menu_destroy_hash_value(ObMenu *self);
28 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
30 static void parse_menu_separator(ObParseInst *i,
31 xmlDocPtr doc, xmlNodePtr node,
33 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
36 static void client_dest(gpointer client)
38 /* menus can be associated with a client, so close any that are since
39 we are disappearing now */
40 menu_frame_hide_all_client(client);
43 void menu_startup(gboolean reconfig)
47 gboolean loaded = FALSE;
50 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
51 (GDestroyNotify)menu_destroy_hash_value);
53 client_list_menu_startup();
54 client_menu_startup();
56 menu_parse_inst = parse_startup();
58 menu_parse_state.parent = NULL;
59 menu_parse_state.pipe_creator = NULL;
60 parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
61 parse_register(menu_parse_inst, "item", parse_menu_item,
63 parse_register(menu_parse_inst, "separator",
64 parse_menu_separator, &menu_parse_state);
66 for (it = config_menu_files; it; it = g_slist_next(it)) {
67 if (parse_load_menu(it->data, &doc, &node)) {
69 parse_tree(menu_parse_inst, doc, node->children);
74 if (parse_load_menu("menu.xml", &doc, &node)) {
75 parse_tree(menu_parse_inst, doc, node->children);
80 g_assert(menu_parse_state.parent == NULL);
83 client_add_destructor(client_dest);
86 void menu_shutdown(gboolean reconfig)
89 client_remove_destructor(client_dest);
91 parse_shutdown(menu_parse_inst);
92 menu_parse_inst = NULL;
94 menu_frame_hide_all();
95 g_hash_table_destroy(menu_hash);
99 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
102 return menu->pipe_creator == data;
105 void menu_pipe_execute(ObMenu *self)
115 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
116 g_warning("Failed to execute command for pipe-menu: %s", err->message);
121 if (parse_load_mem(output, strlen(output),
122 "openbox_pipe_menu", &doc, &node))
124 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
125 menu_clear_entries(self);
127 menu_parse_state.pipe_creator = self;
128 menu_parse_state.parent = self;
129 parse_tree(menu_parse_inst, doc, node->children);
132 g_warning("Invalid output from pipe-menu: %s", self->execute);
138 static ObMenu* menu_from_name(gchar *name)
142 g_assert(name != NULL);
144 if (!(self = g_hash_table_lookup(menu_hash, name)))
145 g_warning("Attempted to access menu '%s' but it does not exist.",
150 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
153 ObMenuParseState *state = data;
157 if (parse_attr_string("label", node, &label)) {
160 for (node = node->children; node; node = node->next)
161 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action"))
162 acts = g_slist_append(acts, action_parse
164 OB_USER_ACTION_MENU_SELECTION));
165 menu_add_normal(state->parent, -1, label, acts);
171 static void parse_menu_separator(ObParseInst *i,
172 xmlDocPtr doc, xmlNodePtr node,
175 ObMenuParseState *state = data;
178 menu_add_separator(state->parent, -1);
181 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
184 ObMenuParseState *state = data;
185 gchar *name = NULL, *title = NULL, *script = NULL;
188 if (!parse_attr_string("id", node, &name))
189 goto parse_menu_fail;
191 if (!g_hash_table_lookup(menu_hash, name)) {
192 if (!parse_attr_string("label", node, &title))
193 goto parse_menu_fail;
195 if ((menu = menu_new(name, title, NULL))) {
196 menu->pipe_creator = state->pipe_creator;
197 if (parse_attr_string("execute", node, &script)) {
198 menu->execute = parse_expand_tilde(script);
203 state->parent = menu;
204 parse_tree(i, doc, node->children);
211 menu_add_submenu(state->parent, -1, name);
219 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
223 self = g_new0(ObMenu, 1);
224 self->name = g_strdup(name);
225 self->title = g_strdup(title);
228 g_hash_table_replace(menu_hash, self->name, self);
233 static void menu_destroy_hash_value(ObMenu *self)
235 /* make sure its not visible */
240 for (it = menu_frame_visible; it; it = g_list_next(it)) {
243 menu_frame_hide_all();
247 if (self->destroy_func)
248 self->destroy_func(self, self->data);
250 menu_clear_entries(self);
253 g_free(self->execute);
258 void menu_free(ObMenu *menu)
260 g_hash_table_remove(menu_hash, menu->name);
263 void menu_show(gchar *name, gint x, gint y, ObClient *client)
268 if (!(self = menu_from_name(name))) return;
270 menu_frame_hide_all();
272 frame = menu_frame_new(self, client);
273 menu_frame_move(frame, x, y);
274 menu_frame_show(frame, NULL);
276 menu_frame_select_next(frame);
279 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
285 self = g_new0(ObMenuEntry, 1);
291 case OB_MENU_ENTRY_TYPE_NORMAL:
292 self->data.normal.enabled = TRUE;
294 case OB_MENU_ENTRY_TYPE_SUBMENU:
295 case OB_MENU_ENTRY_TYPE_SEPARATOR:
302 void menu_entry_free(ObMenuEntry *self)
305 switch (self->type) {
306 case OB_MENU_ENTRY_TYPE_NORMAL:
307 g_free(self->data.normal.label);
308 while (self->data.normal.actions) {
309 action_free(self->data.normal.actions->data);
310 self->data.normal.actions =
311 g_slist_delete_link(self->data.normal.actions,
312 self->data.normal.actions);
315 case OB_MENU_ENTRY_TYPE_SUBMENU:
316 g_free(self->data.submenu.name);
318 case OB_MENU_ENTRY_TYPE_SEPARATOR:
326 void menu_clear_entries(ObMenu *self)
329 /* assert that the menu isn't visible */
334 for (it = menu_frame_visible; it; it = g_list_next(it)) {
336 g_assert(f->menu != self);
341 while (self->entries) {
342 menu_entry_free(self->entries->data);
343 self->entries = g_list_delete_link(self->entries, self->entries);
347 void menu_entry_remove(ObMenuEntry *self)
349 self->menu->entries = g_list_remove(self->menu->entries, self);
350 menu_entry_free(self);
353 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
358 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
359 e->data.normal.label = g_strdup(label);
360 e->data.normal.actions = actions;
362 self->entries = g_list_append(self->entries, e);
366 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
370 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
371 e->data.submenu.name = g_strdup(submenu);
373 self->entries = g_list_append(self->entries, e);
377 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
381 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
383 self->entries = g_list_append(self->entries, e);
387 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
389 self->update_func = func;
392 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
394 self->execute_func = func;
397 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
399 self->destroy_func = func;
402 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
404 ObMenuEntry *ret = NULL;
407 for (it = self->entries; it; it = g_list_next(it)) {
408 ObMenuEntry *e = it->data;
418 void menu_find_submenus(ObMenu *self)
422 for (it = self->entries; it; it = g_list_next(it)) {
423 ObMenuEntry *e = it->data;
425 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
426 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);