12 #include "client_menu.h"
13 #include "client_list_menu.h"
14 #include "parser/parse.h"
16 typedef struct _ObMenuParseState ObMenuParseState;
18 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 gboolean menu_open(gchar *file, xmlDocPtr *doc, xmlNodePtr *node)
38 gboolean loaded = TRUE;
41 p = g_build_filename(g_get_home_dir(), ".openbox", file, NULL);
42 if (!parse_load(p, "openbox_menu", doc, node)) {
44 p = g_build_filename(RCDIR, file, NULL);
45 if (!parse_load(p, "openbox_menu", doc, node)) {
48 if (!parse_load(p, "openbox_menu", doc, node)) {
49 g_warning("Failed to load menu from '%s'", file);
58 static void client_dest(gpointer client)
60 /* menus can be associated with a client, so close any that are since
61 we are disappearing now */
62 menu_frame_hide_all_client(client);
69 gboolean loaded = FALSE;
72 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
73 (GDestroyNotify)menu_destroy_hash_value);
75 client_list_menu_startup();
76 client_menu_startup();
78 menu_parse_inst = parse_startup();
80 menu_parse_state.menus = NULL;
81 parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
82 parse_register(menu_parse_inst, "item", parse_menu_item,
84 parse_register(menu_parse_inst, "separator",
85 parse_menu_separator, &menu_parse_state);
87 for (it = config_menu_files; it; it = g_slist_next(it)) {
88 if (menu_open(it->data, &doc, &node)) {
90 parse_tree(menu_parse_inst, doc, node->xmlChildrenNode);
95 if (menu_open("menu.xml", &doc, &node)) {
96 parse_tree(menu_parse_inst, doc, node->xmlChildrenNode);
101 g_assert(menu_parse_state.menus == NULL);
103 client_add_destructor(client_dest);
108 client_remove_destructor(client_dest);
110 parse_shutdown(menu_parse_inst);
111 menu_parse_inst = NULL;
113 menu_frame_hide_all();
114 g_hash_table_destroy(menu_hash);
118 void menu_pipe_execute(ObMenu *self)
128 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err))
130 g_warning("Failed to execute command for pipe-menu: %s", err->message);
135 if (parse_load_mem(output, strlen(output),
136 "openbox_pipe_menu", &doc, &node))
138 menu_clear_entries(self);
140 menu_parse_state.menus = g_slist_prepend(NULL, self);
141 parse_tree(menu_parse_inst, doc, node->xmlChildrenNode);
142 menu_parse_state.menus = g_slist_remove(menu_parse_state.menus, self);
145 g_assert(menu_parse_state.menus == NULL);
149 static ObMenu* menu_from_name(gchar *name)
153 g_assert(name != NULL);
155 if (!(self = g_hash_table_lookup(menu_hash, name)))
156 g_warning("Attempted to access menu '%s' but it does not exist.",
161 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
164 ObMenuParseState *state = data;
168 if (parse_attr_string("label", node, &label)) {
171 for (node = node->xmlChildrenNode; node; node = node->next)
172 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action"))
173 acts = g_slist_append(acts, action_parse(i, doc, node));
174 menu_add_normal(state->menus->data, -1, label, acts);
180 static void parse_menu_separator(ObParseInst *i,
181 xmlDocPtr doc, xmlNodePtr node,
184 ObMenuParseState *state = data;
187 menu_add_separator(state->menus->data, -1);
190 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
193 ObMenuParseState *state = data;
194 gchar *name = NULL, *title = NULL, *script = NULL;
197 if (!parse_attr_string("id", node, &name))
198 goto parse_menu_fail;
200 if (!g_hash_table_lookup(menu_hash, name)) {
201 if (!parse_attr_string("label", node, &title))
202 goto parse_menu_fail;
204 if ((menu = menu_new(name, title, NULL))) {
205 if (parse_attr_string("execute", node, &script)) {
206 menu->execute = ob_expand_tilde(script);
208 state->menus = g_slist_prepend(state->menus, menu);
209 parse_tree(i, doc, node->xmlChildrenNode);
210 state->menus = g_slist_delete_link(state->menus, state->menus);
216 menu_add_submenu(state->menus->data, -1, name);
224 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
228 self = g_new0(ObMenu, 1);
229 self->name = g_strdup(name);
230 self->title = g_strdup(title);
233 g_hash_table_replace(menu_hash, self->name, self);
238 static void menu_destroy_hash_value(ObMenu *self)
240 /* XXX make sure its not visible */
242 if (self->destroy_func)
243 self->destroy_func(self, self->data);
245 menu_clear_entries(self);
248 g_free(self->execute);
251 void menu_free(ObMenu *menu)
253 g_hash_table_remove(menu_hash, menu->name);
256 void menu_show(gchar *name, gint x, gint y, ObClient *client)
261 if (!(self = menu_from_name(name))) return;
263 frame = menu_frame_new(self, client);
264 menu_frame_move(frame, x, y);
265 menu_frame_show(frame, NULL);
267 menu_frame_select_next(frame);
270 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
276 self = g_new0(ObMenuEntry, 1);
282 case OB_MENU_ENTRY_TYPE_NORMAL:
283 self->data.normal.enabled = TRUE;
285 case OB_MENU_ENTRY_TYPE_SUBMENU:
286 case OB_MENU_ENTRY_TYPE_SEPARATOR:
293 void menu_entry_free(ObMenuEntry *self)
296 switch (self->type) {
297 case OB_MENU_ENTRY_TYPE_NORMAL:
298 g_free(self->data.normal.label);
299 while (self->data.normal.actions) {
300 action_free(self->data.normal.actions->data);
301 self->data.normal.actions =
302 g_slist_delete_link(self->data.normal.actions,
303 self->data.normal.actions);
306 case OB_MENU_ENTRY_TYPE_SUBMENU:
307 g_free(self->data.submenu.name);
309 case OB_MENU_ENTRY_TYPE_SEPARATOR:
317 void menu_clear_entries(ObMenu *self)
319 /* XXX assert that the menu isn't visible */
321 while (self->entries) {
322 menu_entry_free(self->entries->data);
323 self->entries = g_list_delete_link(self->entries, self->entries);
327 void menu_entry_remove(ObMenuEntry *self)
329 self->menu->entries = g_list_remove(self->menu->entries, self);
330 menu_entry_free(self);
333 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
338 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
339 e->data.normal.label = g_strdup(label);
340 e->data.normal.actions = actions;
342 self->entries = g_list_append(self->entries, e);
346 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
350 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
351 e->data.submenu.name = g_strdup(submenu);
353 self->entries = g_list_append(self->entries, e);
357 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
361 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
363 self->entries = g_list_append(self->entries, e);
367 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
369 self->update_func = func;
372 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
374 self->execute_func = func;
377 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
379 self->destroy_func = func;
382 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
384 ObMenuEntry *ret = NULL;
387 for (it = self->entries; it; it = g_list_next(it)) {
388 ObMenuEntry *e = it->data;
398 void menu_find_submenus(ObMenu *self)
402 for (it = self->entries; it; it = g_list_next(it)) {
403 ObMenuEntry *e = it->data;
405 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
406 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);