]> icculus.org git repositories - dana/openbox.git/blob - openbox/menu.c
69a5fe5aa4010d6da5bdbdeb95830a6070c9c2f6
[dana/openbox.git] / openbox / menu.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    menu.c for the Openbox window manager
4    Copyright (c) 2003        Ben Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "debug.h"
20 #include "menu.h"
21 #include "openbox.h"
22 #include "stacking.h"
23 #include "client.h"
24 #include "config.h"
25 #include "screen.h"
26 #include "menuframe.h"
27 #include "keyboard.h"
28 #include "geom.h"
29 #include "misc.h"
30 #include "client_menu.h"
31 #include "client_list_menu.h"
32 #include "parser/parse.h"
33
34 typedef struct _ObMenuParseState ObMenuParseState;
35
36 struct _ObMenuParseState
37 {
38     ObMenu *parent;
39     ObMenu *pipe_creator;
40 };
41
42 static GHashTable *menu_hash = NULL;
43 static ObParseInst *menu_parse_inst;
44 static ObMenuParseState menu_parse_state;
45
46 static void menu_destroy_hash_value(ObMenu *self);
47 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
48                             gpointer data);
49 static void parse_menu_separator(ObParseInst *i,
50                                  xmlDocPtr doc, xmlNodePtr node,
51                                  gpointer data);
52 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
53                        gpointer data);
54
55 static void client_dest(ObClient *client, gpointer data)
56 {
57     /* menus can be associated with a client, so close any that are since
58        we are disappearing now */
59     menu_frame_hide_all_client(client);
60 }
61
62 void menu_startup(gboolean reconfig)
63 {
64     xmlDocPtr doc;
65     xmlNodePtr node;
66     gboolean loaded = FALSE;
67     GSList *it;
68
69     menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
70                                       (GDestroyNotify)menu_destroy_hash_value);
71
72     client_list_menu_startup(reconfig);
73     client_menu_startup();
74
75     menu_parse_inst = parse_startup();
76
77     menu_parse_state.parent = NULL;
78     menu_parse_state.pipe_creator = NULL;
79     parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
80     parse_register(menu_parse_inst, "item", parse_menu_item,
81                    &menu_parse_state);
82     parse_register(menu_parse_inst, "separator",
83                    parse_menu_separator, &menu_parse_state);
84
85     for (it = config_menu_files; it; it = g_slist_next(it)) {
86         if (parse_load_menu(it->data, &doc, &node)) {
87             loaded = TRUE;
88             parse_tree(menu_parse_inst, doc, node->children);
89             xmlFreeDoc(doc);
90         }
91     }
92     if (!loaded) {
93         if (parse_load_menu("menu.xml", &doc, &node)) {
94             parse_tree(menu_parse_inst, doc, node->children);
95             xmlFreeDoc(doc);
96         }
97     }
98     
99     g_assert(menu_parse_state.parent == NULL);
100
101     if (!reconfig)
102         client_add_destructor(client_dest, NULL);
103 }
104
105 void menu_shutdown(gboolean reconfig)
106 {
107     if (!reconfig)
108         client_remove_destructor(client_dest);
109
110     parse_shutdown(menu_parse_inst);
111     menu_parse_inst = NULL;
112
113     client_list_menu_shutdown(reconfig);
114
115     menu_frame_hide_all();
116     g_hash_table_destroy(menu_hash);
117     menu_hash = NULL;
118 }
119
120 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
121 {
122     ObMenu *menu = val;
123     return menu->pipe_creator == data;
124 }
125
126 void menu_pipe_execute(ObMenu *self)
127 {
128     xmlDocPtr doc;
129     xmlNodePtr node;
130     gchar *output;
131     GError *err = NULL;
132
133     if (!self->execute)
134         return;
135
136     if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
137         g_warning("Failed to execute command for pipe-menu: %s", err->message);
138         g_error_free(err);
139         return;
140     }
141
142     if (parse_load_mem(output, strlen(output),
143                        "openbox_pipe_menu", &doc, &node))
144     {
145         g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
146         menu_clear_entries(self);
147
148         menu_parse_state.pipe_creator = self;
149         menu_parse_state.parent = self;
150         parse_tree(menu_parse_inst, doc, node->children);
151         xmlFreeDoc(doc);
152     } else {
153         g_warning("Invalid output from pipe-menu: %s", self->execute);
154     }
155
156     g_free(output);
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->children; node; node = node->next)
182                 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
183                     ObAction *a = action_parse
184                         (i, doc, node, OB_USER_ACTION_MENU_SELECTION);
185                     if (a)
186                         acts = g_slist_append(acts, a);
187                 }
188             menu_add_normal(state->parent, -1, label, acts);
189             g_free(label);
190         }
191     }
192 }
193
194 static void parse_menu_separator(ObParseInst *i,
195                                  xmlDocPtr doc, xmlNodePtr node,
196                                  gpointer data)
197 {
198     ObMenuParseState *state = data;
199
200     if (state->parent)
201         menu_add_separator(state->parent, -1);
202 }
203
204 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
205                        gpointer data)
206 {
207     ObMenuParseState *state = data;
208     gchar *name = NULL, *title = NULL, *script = NULL;
209     ObMenu *menu;
210
211     if (!parse_attr_string("id", node, &name))
212         goto parse_menu_fail;
213
214     if (!g_hash_table_lookup(menu_hash, name)) {
215         if (!parse_attr_string("label", node, &title))
216             goto parse_menu_fail;
217
218         if ((menu = menu_new(name, title, NULL))) {
219             menu->pipe_creator = state->pipe_creator;
220             if (parse_attr_string("execute", node, &script)) {
221                 menu->execute = parse_expand_tilde(script);
222             } else {
223                 ObMenu *old;
224
225                 old = state->parent;
226                 state->parent = menu;
227                 parse_tree(i, doc, node->children);
228                 state->parent = old;
229             }
230         }
231     }
232
233     if (state->parent)
234         menu_add_submenu(state->parent, -1, name);
235
236 parse_menu_fail:
237     g_free(name);
238     g_free(title);
239     g_free(script);
240 }
241
242 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
243 {
244     ObMenu *self;
245
246     self = g_new0(ObMenu, 1);
247     self->name = g_strdup(name);
248     self->title = g_strdup(title);
249     self->data = data;
250
251     g_hash_table_replace(menu_hash, self->name, self);
252
253     return self;
254 }
255
256 static void menu_destroy_hash_value(ObMenu *self)
257 {
258     /* make sure its not visible */
259     {
260         GList *it;
261         ObMenuFrame *f;
262
263         for (it = menu_frame_visible; it; it = g_list_next(it)) {
264             f = it->data;
265             if (f->menu == self)
266                 menu_frame_hide_all();
267         }
268     }
269
270     if (self->destroy_func)
271         self->destroy_func(self, self->data);
272
273     menu_clear_entries(self);
274     g_free(self->name);
275     g_free(self->title);
276     g_free(self->execute);
277
278     g_free(self);
279 }
280
281 void menu_free(ObMenu *menu)
282 {
283     g_hash_table_remove(menu_hash, menu->name);
284 }
285
286 void menu_show(gchar *name, gint x, gint y, ObClient *client)
287 {
288     ObMenu *self;
289     ObMenuFrame *frame;
290     guint i;
291
292     if (!(self = menu_from_name(name))
293         || keyboard_interactively_grabbed()) return;
294
295     /* if the requested menu is already the top visible menu, then don't
296        bother */
297     if (menu_frame_visible) {
298         frame = menu_frame_visible->data;
299         if (frame->menu == self)
300             return;
301     }
302
303     menu_frame_hide_all();
304
305     frame = menu_frame_new(self, client);
306     if (client && x < 0 && y < 0) {
307         x = client->frame->area.x + client->frame->size.left;
308         y = client->frame->area.y + client->frame->size.top;
309         menu_frame_move(frame, x, y);
310     } else
311         menu_frame_move(frame,
312                         x - ob_rr_theme->bwidth, y - ob_rr_theme->bwidth);
313     for (i = 0; i < screen_num_monitors; ++i) {
314         Rect *a = screen_physical_area_monitor(i);
315         if (RECT_CONTAINS(*a, x, y)) {
316             frame->monitor = i;
317             break;
318         }
319     }
320     if (!menu_frame_show(frame, NULL))
321         menu_frame_free(frame);
322     else if (frame->entries) {
323         ObMenuEntryFrame *e = frame->entries->data;
324         if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
325             e->entry->data.normal.enabled)
326                 menu_frame_select(frame, e);
327     }
328 }
329
330 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
331 {
332     ObMenuEntry *self;
333
334     g_assert(menu);
335
336     self = g_new0(ObMenuEntry, 1);
337     self->type = type;
338     self->menu = menu;
339     self->id = id;
340
341     switch (type) {
342     case OB_MENU_ENTRY_TYPE_NORMAL:
343         self->data.normal.enabled = TRUE;
344         break;
345     case OB_MENU_ENTRY_TYPE_SUBMENU:
346     case OB_MENU_ENTRY_TYPE_SEPARATOR:
347         break;
348     }
349
350     return self;
351 }
352
353 void menu_entry_free(ObMenuEntry *self)
354 {
355     if (self) {
356         switch (self->type) {
357         case OB_MENU_ENTRY_TYPE_NORMAL:
358             g_free(self->data.normal.label);
359             while (self->data.normal.actions) {
360                 action_unref(self->data.normal.actions->data);
361                 self->data.normal.actions =
362                     g_slist_delete_link(self->data.normal.actions,
363                                         self->data.normal.actions);
364             }
365             break;
366         case OB_MENU_ENTRY_TYPE_SUBMENU:
367             g_free(self->data.submenu.name);
368             break;
369         case OB_MENU_ENTRY_TYPE_SEPARATOR:
370             break;
371         }
372
373         g_free(self);
374     }
375 }
376
377 void menu_clear_entries(ObMenu *self)
378 {
379 #ifdef DEBUG
380     /* assert that the menu isn't visible */
381     {
382         GList *it;
383         ObMenuFrame *f;
384
385         for (it = menu_frame_visible; it; it = g_list_next(it)) {
386             f = it->data;
387             g_assert(f->menu != self);
388         }
389     }
390 #endif
391
392     while (self->entries) {
393         menu_entry_free(self->entries->data);
394         self->entries = g_list_delete_link(self->entries, self->entries);
395     }
396 }
397
398 void menu_entry_remove(ObMenuEntry *self)
399 {
400     self->menu->entries = g_list_remove(self->menu->entries, self);
401     menu_entry_free(self);
402 }
403
404 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
405                              GSList *actions)
406 {
407     ObMenuEntry *e;
408
409     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
410     e->data.normal.label = g_strdup(label);
411     e->data.normal.actions = actions;
412
413     self->entries = g_list_append(self->entries, e);
414     return e;
415 }
416
417 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
418 {
419     ObMenuEntry *e;
420
421     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
422     e->data.submenu.name = g_strdup(submenu);
423
424     self->entries = g_list_append(self->entries, e);
425     return e;
426 }
427
428 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
429 {
430     ObMenuEntry *e;
431
432     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
433
434     self->entries = g_list_append(self->entries, e);
435     return e;
436 }
437
438 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
439 {
440     self->update_func = func;
441 }
442
443 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
444 {
445     self->execute_func = func;
446 }
447
448 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
449 {
450     self->destroy_func = func;
451 }
452
453 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
454 {
455     ObMenuEntry *ret = NULL;
456     GList *it;
457
458     for (it = self->entries; it; it = g_list_next(it)) {
459         ObMenuEntry *e = it->data;
460
461         if (e->id == id) {
462             ret = e;
463             break;
464         }
465     }
466     return ret;
467 }
468
469 void menu_find_submenus(ObMenu *self)
470 {
471     GList *it;
472
473     for (it = self->entries; it; it = g_list_next(it)) {
474         ObMenuEntry *e = it->data;
475
476         if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
477             e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
478     }
479 }