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;
43 if (!parse_load(file, "openbox_menu", doc, node)) {
44 g_warning("Failed to load menu from '%s'", file);
48 p = g_build_filename(g_get_home_dir(), ".openbox", file, NULL);
49 if (!parse_load(p, "openbox_menu", doc, node)) {
51 p = g_build_filename(RCDIR, file, NULL);
52 if (!parse_load(p, "openbox_menu", doc, node)) {
55 if (!parse_load(p, "openbox_menu", doc, node)) {
56 g_warning("Failed to load menu from '%s'", file);
66 static void client_dest(gpointer client)
68 /* menus can be associated with a client, so close any that are since
69 we are disappearing now */
70 menu_frame_hide_all_client(client);
73 void menu_startup(gboolean reconfig)
77 gboolean loaded = FALSE;
80 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
81 (GDestroyNotify)menu_destroy_hash_value);
83 client_list_menu_startup();
84 client_menu_startup();
86 menu_parse_inst = parse_startup();
88 menu_parse_state.parent = NULL;
89 menu_parse_state.pipe_creator = NULL;
90 parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
91 parse_register(menu_parse_inst, "item", parse_menu_item,
93 parse_register(menu_parse_inst, "separator",
94 parse_menu_separator, &menu_parse_state);
96 for (it = config_menu_files; it; it = g_slist_next(it)) {
97 if (menu_open(it->data, &doc, &node)) {
99 parse_tree(menu_parse_inst, doc, node->children);
104 if (menu_open("menu.xml", &doc, &node)) {
105 parse_tree(menu_parse_inst, doc, node->children);
110 g_assert(menu_parse_state.parent == NULL);
113 client_add_destructor(client_dest);
116 void menu_shutdown(gboolean reconfig)
119 client_remove_destructor(client_dest);
121 parse_shutdown(menu_parse_inst);
122 menu_parse_inst = NULL;
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_warning("Failed to execute command for pipe-menu: %s", err->message);
151 if (parse_load_mem(output, strlen(output),
152 "openbox_pipe_menu", &doc, &node))
154 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
155 menu_clear_entries(self);
157 menu_parse_state.pipe_creator = self;
158 menu_parse_state.parent = self;
159 parse_tree(menu_parse_inst, doc, node->children);
162 g_warning("Invalid output from pipe-menu: %s", self->execute);
166 static ObMenu* menu_from_name(gchar *name)
170 g_assert(name != NULL);
172 if (!(self = g_hash_table_lookup(menu_hash, name)))
173 g_warning("Attempted to access menu '%s' but it does not exist.",
178 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
181 ObMenuParseState *state = data;
185 if (parse_attr_string("label", node, &label)) {
188 for (node = node->children; node; node = node->next)
189 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action"))
190 acts = g_slist_append(acts, action_parse
192 OB_USER_ACTION_MENU_SELECTION));
193 menu_add_normal(state->parent, -1, label, acts);
199 static void parse_menu_separator(ObParseInst *i,
200 xmlDocPtr doc, xmlNodePtr node,
203 ObMenuParseState *state = data;
206 menu_add_separator(state->parent, -1);
209 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
212 ObMenuParseState *state = data;
213 gchar *name = NULL, *title = NULL, *script = NULL;
216 if (!parse_attr_string("id", node, &name))
217 goto parse_menu_fail;
219 if (!g_hash_table_lookup(menu_hash, name)) {
220 if (!parse_attr_string("label", node, &title))
221 goto parse_menu_fail;
223 if ((menu = menu_new(name, title, NULL))) {
224 menu->pipe_creator = state->pipe_creator;
225 if (parse_attr_string("execute", node, &script)) {
226 menu->execute = ob_expand_tilde(script);
231 state->parent = menu;
232 parse_tree(i, doc, node->children);
239 menu_add_submenu(state->parent, -1, name);
247 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
251 self = g_new0(ObMenu, 1);
252 self->name = g_strdup(name);
253 self->title = g_strdup(title);
256 g_hash_table_replace(menu_hash, self->name, self);
261 static void menu_destroy_hash_value(ObMenu *self)
263 /* XXX make sure its not visible */
265 if (self->destroy_func)
266 self->destroy_func(self, self->data);
268 menu_clear_entries(self);
271 g_free(self->execute);
274 void menu_free(ObMenu *menu)
276 g_hash_table_remove(menu_hash, menu->name);
279 void menu_show(gchar *name, gint x, gint y, ObClient *client)
284 if (!(self = menu_from_name(name))) return;
286 menu_frame_hide_all();
288 frame = menu_frame_new(self, client);
289 menu_frame_move(frame, x, y);
290 menu_frame_show(frame, NULL);
292 menu_frame_select_next(frame);
295 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
301 self = g_new0(ObMenuEntry, 1);
307 case OB_MENU_ENTRY_TYPE_NORMAL:
308 self->data.normal.enabled = TRUE;
310 case OB_MENU_ENTRY_TYPE_SUBMENU:
311 case OB_MENU_ENTRY_TYPE_SEPARATOR:
318 void menu_entry_free(ObMenuEntry *self)
321 switch (self->type) {
322 case OB_MENU_ENTRY_TYPE_NORMAL:
323 g_free(self->data.normal.label);
324 while (self->data.normal.actions) {
325 action_free(self->data.normal.actions->data);
326 self->data.normal.actions =
327 g_slist_delete_link(self->data.normal.actions,
328 self->data.normal.actions);
331 case OB_MENU_ENTRY_TYPE_SUBMENU:
332 g_free(self->data.submenu.name);
334 case OB_MENU_ENTRY_TYPE_SEPARATOR:
342 void menu_clear_entries(ObMenu *self)
344 /* XXX assert that the menu isn't visible */
346 while (self->entries) {
347 menu_entry_free(self->entries->data);
348 self->entries = g_list_delete_link(self->entries, self->entries);
352 void menu_entry_remove(ObMenuEntry *self)
354 self->menu->entries = g_list_remove(self->menu->entries, self);
355 menu_entry_free(self);
358 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
363 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
364 e->data.normal.label = g_strdup(label);
365 e->data.normal.actions = actions;
367 self->entries = g_list_append(self->entries, e);
371 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
375 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
376 e->data.submenu.name = g_strdup(submenu);
378 self->entries = g_list_append(self->entries, e);
382 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
386 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
388 self->entries = g_list_append(self->entries, e);
392 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
394 self->update_func = func;
397 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
399 self->execute_func = func;
402 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
404 self->destroy_func = func;
407 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
409 ObMenuEntry *ret = NULL;
412 for (it = self->entries; it; it = g_list_next(it)) {
413 ObMenuEntry *e = it->data;
423 void menu_find_submenus(ObMenu *self)
427 for (it = self->entries; it; it = g_list_next(it)) {
428 ObMenuEntry *e = it->data;
430 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
431 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);