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