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