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