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.menus = 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.menus == 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))
140 g_warning("Failed to execute command for pipe-menu: %s", err->message);
145 if (parse_load_mem(output, strlen(output),
146 "openbox_pipe_menu", &doc, &node))
148 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
149 menu_clear_entries(self);
151 menu_parse_state.pipe_creator = self;
152 menu_parse_state.menus = g_slist_prepend(NULL, self);
153 parse_tree(menu_parse_inst, doc, node->xmlChildrenNode);
154 menu_parse_state.menus = g_slist_remove(menu_parse_state.menus, self);
155 menu_parse_state.pipe_creator = NULL;
158 g_assert(menu_parse_state.menus == NULL);
160 g_warning("Invalid output from pipe-menu: %s", self->execute);
164 static ObMenu* menu_from_name(gchar *name)
168 g_assert(name != NULL);
170 if (!(self = g_hash_table_lookup(menu_hash, name)))
171 g_warning("Attempted to access menu '%s' but it does not exist.",
176 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
179 ObMenuParseState *state = data;
183 if (parse_attr_string("label", node, &label)) {
186 for (node = node->xmlChildrenNode; node; node = node->next)
187 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action"))
188 acts = g_slist_append(acts, action_parse(i, doc, node));
189 menu_add_normal(state->menus->data, -1, label, acts);
195 static void parse_menu_separator(ObParseInst *i,
196 xmlDocPtr doc, xmlNodePtr node,
199 ObMenuParseState *state = data;
202 menu_add_separator(state->menus->data, -1);
205 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
208 ObMenuParseState *state = data;
209 gchar *name = NULL, *title = NULL, *script = NULL;
212 if (!parse_attr_string("id", node, &name))
213 goto parse_menu_fail;
215 if (!g_hash_table_lookup(menu_hash, name)) {
216 if (!parse_attr_string("label", node, &title))
217 goto parse_menu_fail;
219 if ((menu = menu_new(name, title, NULL))) {
220 menu->pipe_creator = state->pipe_creator;
221 if (parse_attr_string("execute", node, &script)) {
222 menu->execute = ob_expand_tilde(script);
224 state->menus = g_slist_prepend(state->menus, menu);
225 parse_tree(i, doc, node->xmlChildrenNode);
226 state->menus = g_slist_delete_link(state->menus, state->menus);
232 menu_add_submenu(state->menus->data, -1, name);
240 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
244 self = g_new0(ObMenu, 1);
245 self->name = g_strdup(name);
246 self->title = g_strdup(title);
249 g_hash_table_replace(menu_hash, self->name, self);
254 static void menu_destroy_hash_value(ObMenu *self)
256 /* XXX make sure its not visible */
258 if (self->destroy_func)
259 self->destroy_func(self, self->data);
261 menu_clear_entries(self);
264 g_free(self->execute);
267 void menu_free(ObMenu *menu)
269 g_hash_table_remove(menu_hash, menu->name);
272 void menu_show(gchar *name, gint x, gint y, ObClient *client)
277 if (!(self = menu_from_name(name))) return;
279 frame = menu_frame_new(self, client);
280 menu_frame_move(frame, x, y);
281 menu_frame_show(frame, NULL);
283 menu_frame_select_next(frame);
286 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
292 self = g_new0(ObMenuEntry, 1);
298 case OB_MENU_ENTRY_TYPE_NORMAL:
299 self->data.normal.enabled = TRUE;
301 case OB_MENU_ENTRY_TYPE_SUBMENU:
302 case OB_MENU_ENTRY_TYPE_SEPARATOR:
309 void menu_entry_free(ObMenuEntry *self)
312 switch (self->type) {
313 case OB_MENU_ENTRY_TYPE_NORMAL:
314 g_free(self->data.normal.label);
315 while (self->data.normal.actions) {
316 action_free(self->data.normal.actions->data);
317 self->data.normal.actions =
318 g_slist_delete_link(self->data.normal.actions,
319 self->data.normal.actions);
322 case OB_MENU_ENTRY_TYPE_SUBMENU:
323 g_free(self->data.submenu.name);
325 case OB_MENU_ENTRY_TYPE_SEPARATOR:
333 void menu_clear_entries(ObMenu *self)
335 /* XXX assert that the menu isn't visible */
337 while (self->entries) {
338 menu_entry_free(self->entries->data);
339 self->entries = g_list_delete_link(self->entries, self->entries);
343 void menu_entry_remove(ObMenuEntry *self)
345 self->menu->entries = g_list_remove(self->menu->entries, self);
346 menu_entry_free(self);
349 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
354 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
355 e->data.normal.label = g_strdup(label);
356 e->data.normal.actions = actions;
358 self->entries = g_list_append(self->entries, e);
362 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
366 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
367 e->data.submenu.name = g_strdup(submenu);
369 self->entries = g_list_append(self->entries, e);
373 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
377 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
379 self->entries = g_list_append(self->entries, e);
383 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
385 self->update_func = func;
388 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
390 self->execute_func = func;
393 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
395 self->destroy_func = func;
398 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
400 ObMenuEntry *ret = NULL;
403 for (it = self->entries; it; it = g_list_next(it)) {
404 ObMenuEntry *e = it->data;
414 void menu_find_submenus(ObMenu *self)
418 for (it = self->entries; it; it = g_list_next(it)) {
419 ObMenuEntry *e = it->data;
421 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
422 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);