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