de3ec629f3962bfdbc120f777c6ccc62b798145c
[dana/openbox.git] / openbox / menu.c
1 #include "debug.h"
2 #include "menu.h"
3 #include "openbox.h"
4 #include "stacking.h"
5 #include "client.h"
6 #include "config.h"
7 #include "screen.h"
8 #include "menuframe.h"
9 #include "geom.h"
10 #include "misc.h"
11 #include "client_menu.h"
12 #include "client_list_menu.h"
13 #include "parser/parse.h"
14
15 typedef struct _ObMenuParseState ObMenuParseState;
16
17 struct _ObMenuParseState
18 {
19     ObMenu *parent;
20     ObMenu *pipe_creator;
21 };
22
23 static GHashTable *menu_hash = NULL;
24 static ObParseInst *menu_parse_inst;
25 static ObMenuParseState menu_parse_state;
26
27 static void menu_destroy_hash_value(ObMenu *self);
28 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
29                             gpointer data);
30 static void parse_menu_separator(ObParseInst *i,
31                                  xmlDocPtr doc, xmlNodePtr node,
32                                  gpointer data);
33 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
34                        gpointer data);
35
36 static void client_dest(gpointer client)
37 {
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);
41 }
42
43 void menu_startup(gboolean reconfig)
44 {
45     xmlDocPtr doc;
46     xmlNodePtr node;
47     gboolean loaded = FALSE;
48     GSList *it;
49
50     menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
51                                       (GDestroyNotify)menu_destroy_hash_value);
52
53     client_list_menu_startup();
54     client_menu_startup();
55
56     menu_parse_inst = parse_startup();
57
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,
62                    &menu_parse_state);
63     parse_register(menu_parse_inst, "separator",
64                    parse_menu_separator, &menu_parse_state);
65
66     for (it = config_menu_files; it; it = g_slist_next(it)) {
67         if (parse_load_menu(it->data, &doc, &node)) {
68             loaded = TRUE;
69             parse_tree(menu_parse_inst, doc, node->children);
70             xmlFreeDoc(doc);
71         }
72     }
73     if (!loaded) {
74         if (parse_load_menu("menu.xml", &doc, &node)) {
75             parse_tree(menu_parse_inst, doc, node->children);
76             xmlFreeDoc(doc);
77         }
78     }
79     
80     g_assert(menu_parse_state.parent == NULL);
81
82     if (!reconfig)
83         client_add_destructor(client_dest);
84 }
85
86 void menu_shutdown(gboolean reconfig)
87 {
88     if (!reconfig)
89         client_remove_destructor(client_dest);
90
91     parse_shutdown(menu_parse_inst);
92     menu_parse_inst = NULL;
93
94     menu_frame_hide_all();
95     g_hash_table_destroy(menu_hash);
96     menu_hash = NULL;
97 }
98
99 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
100 {
101     ObMenu *menu = val;
102     return menu->pipe_creator == data;
103 }
104
105 void menu_pipe_execute(ObMenu *self)
106 {
107     xmlDocPtr doc;
108     xmlNodePtr node;
109     gchar *output;
110     GError *err = NULL;
111
112     if (!self->execute)
113         return;
114
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);
117         g_error_free(err);
118         return;
119     }
120
121     if (parse_load_mem(output, strlen(output),
122                        "openbox_pipe_menu", &doc, &node))
123     {
124         g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
125         menu_clear_entries(self);
126
127         menu_parse_state.pipe_creator = self;
128         menu_parse_state.parent = self;
129         parse_tree(menu_parse_inst, doc, node->children);
130         xmlFreeDoc(doc);
131     } else {
132         g_warning("Invalid output from pipe-menu: %s", self->execute);
133     }
134
135     g_free(output);
136 }
137
138 static ObMenu* menu_from_name(gchar *name)
139 {
140     ObMenu *self = NULL;
141
142     g_assert(name != NULL);
143
144     if (!(self = g_hash_table_lookup(menu_hash, name)))
145         g_warning("Attempted to access menu '%s' but it does not exist.",
146                   name);
147     return self;
148 }  
149
150 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
151                             gpointer data)
152 {
153     ObMenuParseState *state = data;
154     gchar *label;
155     
156     if (state->parent) {
157         if (parse_attr_string("label", node, &label)) {
158             GSList *acts = NULL;
159
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
163                                           (i, doc, node,
164                                            OB_USER_ACTION_MENU_SELECTION));
165             menu_add_normal(state->parent, -1, label, acts);
166             g_free(label);
167         }
168     }
169 }
170
171 static void parse_menu_separator(ObParseInst *i,
172                                  xmlDocPtr doc, xmlNodePtr node,
173                                  gpointer data)
174 {
175     ObMenuParseState *state = data;
176
177     if (state->parent)
178         menu_add_separator(state->parent, -1);
179 }
180
181 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
182                        gpointer data)
183 {
184     ObMenuParseState *state = data;
185     gchar *name = NULL, *title = NULL, *script = NULL;
186     ObMenu *menu;
187
188     if (!parse_attr_string("id", node, &name))
189         goto parse_menu_fail;
190
191     if (!g_hash_table_lookup(menu_hash, name)) {
192         if (!parse_attr_string("label", node, &title))
193             goto parse_menu_fail;
194
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);
199             } else {
200                 ObMenu *old;
201
202                 old = state->parent;
203                 state->parent = menu;
204                 parse_tree(i, doc, node->children);
205                 state->parent = old;
206             }
207         }
208     }
209
210     if (state->parent)
211         menu_add_submenu(state->parent, -1, name);
212
213 parse_menu_fail:
214     g_free(name);
215     g_free(title);
216     g_free(script);
217 }
218
219 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
220 {
221     ObMenu *self;
222
223     self = g_new0(ObMenu, 1);
224     self->name = g_strdup(name);
225     self->title = g_strdup(title);
226     self->data = data;
227
228     g_hash_table_replace(menu_hash, self->name, self);
229
230     return self;
231 }
232
233 static void menu_destroy_hash_value(ObMenu *self)
234 {
235     /* make sure its not visible */
236     {
237         GList *it;
238         ObMenuFrame *f;
239
240         for (it = menu_frame_visible; it; it = g_list_next(it)) {
241             f = it->data;
242             if (f->menu == self)
243                 menu_frame_hide_all();
244         }
245     }
246
247     if (self->destroy_func)
248         self->destroy_func(self, self->data);
249
250     menu_clear_entries(self);
251     g_free(self->name);
252     g_free(self->title);
253     g_free(self->execute);
254
255     g_free(self);
256 }
257
258 void menu_free(ObMenu *menu)
259 {
260     g_hash_table_remove(menu_hash, menu->name);
261 }
262
263 void menu_show(gchar *name, gint x, gint y, ObClient *client)
264 {
265     ObMenu *self;
266     ObMenuFrame *frame;
267
268     if (!(self = menu_from_name(name))) return;
269
270     menu_frame_hide_all();
271
272     frame = menu_frame_new(self, client);
273     menu_frame_move(frame, x, y);
274     menu_frame_show(frame, NULL);
275     if (frame->entries)
276         menu_frame_select_next(frame);
277 }
278
279 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
280 {
281     ObMenuEntry *self;
282
283     g_assert(menu);
284
285     self = g_new0(ObMenuEntry, 1);
286     self->type = type;
287     self->menu = menu;
288     self->id = id;
289
290     switch (type) {
291     case OB_MENU_ENTRY_TYPE_NORMAL:
292         self->data.normal.enabled = TRUE;
293         break;
294     case OB_MENU_ENTRY_TYPE_SUBMENU:
295     case OB_MENU_ENTRY_TYPE_SEPARATOR:
296         break;
297     }
298
299     return self;
300 }
301
302 void menu_entry_free(ObMenuEntry *self)
303 {
304     if (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);
313             }
314             break;
315         case OB_MENU_ENTRY_TYPE_SUBMENU:
316             g_free(self->data.submenu.name);
317             break;
318         case OB_MENU_ENTRY_TYPE_SEPARATOR:
319             break;
320         }
321
322         g_free(self);
323     }
324 }
325
326 void menu_clear_entries(ObMenu *self)
327 {
328 #ifdef DEBUG
329     /* assert that the menu isn't visible */
330     {
331         GList *it;
332         ObMenuFrame *f;
333
334         for (it = menu_frame_visible; it; it = g_list_next(it)) {
335             f = it->data;
336             g_assert(f->menu != self);
337         }
338     }
339 #endif
340
341     while (self->entries) {
342         menu_entry_free(self->entries->data);
343         self->entries = g_list_delete_link(self->entries, self->entries);
344     }
345 }
346
347 void menu_entry_remove(ObMenuEntry *self)
348 {
349     self->menu->entries = g_list_remove(self->menu->entries, self);
350     menu_entry_free(self);
351 }
352
353 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
354                              GSList *actions)
355 {
356     ObMenuEntry *e;
357
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;
361
362     self->entries = g_list_append(self->entries, e);
363     return e;
364 }
365
366 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
367 {
368     ObMenuEntry *e;
369
370     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
371     e->data.submenu.name = g_strdup(submenu);
372
373     self->entries = g_list_append(self->entries, e);
374     return e;
375 }
376
377 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
378 {
379     ObMenuEntry *e;
380
381     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
382
383     self->entries = g_list_append(self->entries, e);
384     return e;
385 }
386
387 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
388 {
389     self->update_func = func;
390 }
391
392 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
393 {
394     self->execute_func = func;
395 }
396
397 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
398 {
399     self->destroy_func = func;
400 }
401
402 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
403 {
404     ObMenuEntry *ret = NULL;
405     GList *it;
406
407     for (it = self->entries; it; it = g_list_next(it)) {
408         ObMenuEntry *e = it->data;
409
410         if (e->id == id) {
411             ret = e;
412             break;
413         }
414     }
415     return ret;
416 }
417
418 void menu_find_submenus(ObMenu *self)
419 {
420     GList *it;
421
422     for (it = self->entries; it; it = g_list_next(it)) {
423         ObMenuEntry *e = it->data;
424
425         if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
426             e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
427     }
428 }