code style fix
[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(i, doc, node));
184             menu_add_normal(state->parent, -1, label, acts);
185             g_free(label);
186         }
187     }
188 }
189
190 static void parse_menu_separator(ObParseInst *i,
191                                  xmlDocPtr doc, xmlNodePtr node,
192                                  gpointer data)
193 {
194     ObMenuParseState *state = data;
195
196     if (state->parent)
197         menu_add_separator(state->parent, -1);
198 }
199
200 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
201                        gpointer data)
202 {
203     ObMenuParseState *state = data;
204     gchar *name = NULL, *title = NULL, *script = NULL;
205     ObMenu *menu;
206
207     if (!parse_attr_string("id", node, &name))
208         goto parse_menu_fail;
209
210     if (!g_hash_table_lookup(menu_hash, name)) {
211         if (!parse_attr_string("label", node, &title))
212             goto parse_menu_fail;
213
214         if ((menu = menu_new(name, title, NULL))) {
215             menu->pipe_creator = state->pipe_creator;
216             if (parse_attr_string("execute", node, &script)) {
217                 menu->execute = ob_expand_tilde(script);
218             } else {
219                 ObMenu *old;
220
221                 old = state->parent;
222                 state->parent = menu;
223                 parse_tree(i, doc, node->xmlChildrenNode);
224                 state->parent = old;
225             }
226         }
227     }
228
229     if (state->parent)
230         menu_add_submenu(state->parent, -1, name);
231
232 parse_menu_fail:
233     g_free(name);
234     g_free(title);
235     g_free(script);
236 }
237
238 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
239 {
240     ObMenu *self;
241
242     self = g_new0(ObMenu, 1);
243     self->name = g_strdup(name);
244     self->title = g_strdup(title);
245     self->data = data;
246
247     g_hash_table_replace(menu_hash, self->name, self);
248
249     return self;
250 }
251
252 static void menu_destroy_hash_value(ObMenu *self)
253 {
254     /* XXX make sure its not visible */
255
256     if (self->destroy_func)
257         self->destroy_func(self, self->data);
258
259     menu_clear_entries(self);
260     g_free(self->name);
261     g_free(self->title);
262     g_free(self->execute);
263 }
264
265 void menu_free(ObMenu *menu)
266 {
267     g_hash_table_remove(menu_hash, menu->name);
268 }
269
270 void menu_show(gchar *name, gint x, gint y, ObClient *client)
271 {
272     ObMenu *self;
273     ObMenuFrame *frame;
274
275     if (!(self = menu_from_name(name))) return;
276
277     frame = menu_frame_new(self, client);
278     menu_frame_move(frame, x, y);
279     menu_frame_show(frame, NULL);
280     if (frame->entries)
281         menu_frame_select_next(frame);
282 }
283
284 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
285 {
286     ObMenuEntry *self;
287
288     g_assert(menu);
289
290     self = g_new0(ObMenuEntry, 1);
291     self->type = type;
292     self->menu = menu;
293     self->id = id;
294
295     switch (type) {
296     case OB_MENU_ENTRY_TYPE_NORMAL:
297         self->data.normal.enabled = TRUE;
298         break;
299     case OB_MENU_ENTRY_TYPE_SUBMENU:
300     case OB_MENU_ENTRY_TYPE_SEPARATOR:
301         break;
302     }
303
304     return self;
305 }
306
307 void menu_entry_free(ObMenuEntry *self)
308 {
309     if (self) {
310         switch (self->type) {
311         case OB_MENU_ENTRY_TYPE_NORMAL:
312             g_free(self->data.normal.label);
313             while (self->data.normal.actions) {
314                 action_free(self->data.normal.actions->data);
315                 self->data.normal.actions =
316                     g_slist_delete_link(self->data.normal.actions,
317                                         self->data.normal.actions);
318             }
319             break;
320         case OB_MENU_ENTRY_TYPE_SUBMENU:
321             g_free(self->data.submenu.name);
322             break;
323         case OB_MENU_ENTRY_TYPE_SEPARATOR:
324             break;
325         }
326
327         g_free(self);
328     }
329 }
330
331 void menu_clear_entries(ObMenu *self)
332 {
333     /* XXX assert that the menu isn't visible */
334
335     while (self->entries) {
336         menu_entry_free(self->entries->data);
337         self->entries = g_list_delete_link(self->entries, self->entries);
338     }
339 }
340
341 void menu_entry_remove(ObMenuEntry *self)
342 {
343     self->menu->entries = g_list_remove(self->menu->entries, self);
344     menu_entry_free(self);
345 }
346
347 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
348                              GSList *actions)
349 {
350     ObMenuEntry *e;
351
352     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
353     e->data.normal.label = g_strdup(label);
354     e->data.normal.actions = actions;
355
356     self->entries = g_list_append(self->entries, e);
357     return e;
358 }
359
360 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
361 {
362     ObMenuEntry *e;
363
364     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
365     e->data.submenu.name = g_strdup(submenu);
366
367     self->entries = g_list_append(self->entries, e);
368     return e;
369 }
370
371 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
372 {
373     ObMenuEntry *e;
374
375     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
376
377     self->entries = g_list_append(self->entries, e);
378     return e;
379 }
380
381 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
382 {
383     self->update_func = func;
384 }
385
386 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
387 {
388     self->execute_func = func;
389 }
390
391 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
392 {
393     self->destroy_func = func;
394 }
395
396 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
397 {
398     ObMenuEntry *ret = NULL;
399     GList *it;
400
401     for (it = self->entries; it; it = g_list_next(it)) {
402         ObMenuEntry *e = it->data;
403
404         if (e->id == id) {
405             ret = e;
406             break;
407         }
408     }
409     return ret;
410 }
411
412 void menu_find_submenus(ObMenu *self)
413 {
414     GList *it;
415
416     for (it = self->entries; it; it = g_list_next(it)) {
417         ObMenuEntry *e = it->data;
418
419         if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
420             e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
421     }
422 }