remove plugins from the menu parsing
[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 static GHashTable *menu_hash = NULL;
17
18 ObParseInst *menu_parse_inst;
19
20 typedef struct _ObMenuParseState ObMenuParseState;
21
22 struct _ObMenuParseState
23 {
24     GSList *menus;
25 };
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 void menu_startup()
59 {
60     ObMenuParseState parse_state;
61     xmlDocPtr doc;
62     xmlNodePtr node;
63     gboolean loaded = FALSE;
64     GSList *it;
65
66     menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
67                                       (GDestroyNotify)menu_destroy_hash_value);
68
69     client_list_menu_startup();
70     client_menu_startup();
71
72     menu_parse_inst = parse_startup();
73
74     for (it = config_menu_files; it; it = g_slist_next(it)) {
75         if (menu_open(it->data, &doc, &node))
76             loaded = TRUE;
77
78     }
79     if (!loaded)
80         loaded = menu_open("menu", &doc, &node);
81
82     if (loaded) {
83         parse_state.menus = NULL;
84
85         parse_register(menu_parse_inst, "menu", parse_menu, &parse_state);
86         parse_register(menu_parse_inst, "item", parse_menu_item, &parse_state);
87         parse_register(menu_parse_inst, "separator",
88                        parse_menu_separator, &parse_state);
89         parse_tree(menu_parse_inst, doc, node->xmlChildrenNode);
90         xmlFreeDoc(doc);
91     }
92 }
93
94 void menu_shutdown()
95 {
96     parse_shutdown(menu_parse_inst);
97     menu_parse_inst = NULL;
98
99     menu_frame_hide_all();
100     g_hash_table_destroy(menu_hash);
101     menu_hash = NULL;
102 }
103
104 static ObMenu* menu_from_name(gchar *name)
105 {
106     ObMenu *self = NULL;
107
108     g_assert(name != NULL);
109
110     if (!(self = g_hash_table_lookup(menu_hash, name)))
111         g_warning("Attempted to access menu '%s' but it does not exist.",
112                   name);
113     return self;
114 }  
115
116 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
117                             gpointer data)
118 {
119     ObMenuParseState *state = data;
120     gchar *label;
121     
122     if (state->menus) {
123         if (parse_attr_string("label", node, &label)) {
124             GSList *acts = NULL;
125
126             for (node = node->xmlChildrenNode; node; node = node->next)
127                 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action"))
128                     acts = g_slist_append(acts, action_parse(i, doc, node));
129             menu_add_normal(state->menus->data, -1, label, acts);
130             g_free(label);
131         }
132     }
133 }
134
135 static void parse_menu_separator(ObParseInst *i,
136                                  xmlDocPtr doc, xmlNodePtr node,
137                                  gpointer data)
138 {
139     ObMenuParseState *state = data;
140
141     if (state->menus)
142         menu_add_separator(state->menus->data, -1);
143 }
144
145 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
146                        gpointer data)
147 {
148     ObMenuParseState *state = data;
149     gchar *name = NULL, *title = NULL;
150     ObMenu *menu;
151
152     if (!parse_attr_string("id", node, &name))
153         goto parse_menu_fail;
154
155     if (!g_hash_table_lookup(menu_hash, name)) {
156         if (!parse_attr_string("label", node, &title))
157             goto parse_menu_fail;
158
159         if ((menu = menu_new(name, title, NULL))) {
160             state->menus = g_slist_prepend(state->menus, menu);
161             parse_tree(i, doc, node->xmlChildrenNode);
162             state->menus = g_slist_delete_link(state->menus, state->menus);
163         }
164     }
165
166     if (state->menus)
167         menu_add_submenu(state->menus->data, -1, name);
168
169 parse_menu_fail:
170     g_free(name);
171     g_free(title);
172 }
173
174
175 static void menu_destroy_hash_value(ObMenu *self)
176 {
177     /* XXX make sure its not visible */
178
179     if (self->destroy_func)
180         self->destroy_func(self, self->data);
181
182     menu_clear_entries(self);
183     g_free(self->name);
184     g_free(self->title);
185 }
186
187 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
188 {
189     ObMenu *self;
190
191     /*if (g_hash_table_lookup(menu_hash, name)) return FALSE;*/
192
193     self = g_new0(ObMenu, 1);
194     self->name = g_strdup(name);
195     self->title = g_strdup(title);
196     self->data = data;
197
198     g_hash_table_replace(menu_hash, self->name, self);
199
200     return self;
201 }
202
203 void menu_free(ObMenu *menu)
204 {
205     g_hash_table_remove(menu_hash, menu->name);
206 }
207
208 void menu_show(gchar *name, gint x, gint y, ObClient *client)
209 {
210     ObMenu *self;
211     ObMenuFrame *frame;
212
213     if (!(self = menu_from_name(name))) return;
214
215     frame = menu_frame_new(self, client);
216     menu_frame_move(frame, x, y);
217     menu_frame_show(frame, NULL);
218 }
219
220 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
221 {
222     ObMenuEntry *self;
223
224     g_assert(menu);
225
226     self = g_new0(ObMenuEntry, 1);
227     self->type = type;
228     self->menu = menu;
229     self->id = id;
230
231     switch (type) {
232     case OB_MENU_ENTRY_TYPE_NORMAL:
233         self->data.normal.enabled = TRUE;
234         break;
235     case OB_MENU_ENTRY_TYPE_SUBMENU:
236     case OB_MENU_ENTRY_TYPE_SEPARATOR:
237         break;
238     }
239
240     return self;
241 }
242
243 void menu_entry_free(ObMenuEntry *self)
244 {
245     if (self) {
246         switch (self->type) {
247         case OB_MENU_ENTRY_TYPE_NORMAL:
248             g_free(self->data.normal.label);
249             while (self->data.normal.actions) {
250                 action_free(self->data.normal.actions->data);
251                 self->data.normal.actions =
252                     g_slist_delete_link(self->data.normal.actions,
253                                         self->data.normal.actions);
254             }
255             break;
256         case OB_MENU_ENTRY_TYPE_SUBMENU:
257             g_free(self->data.submenu.name);
258             break;
259         case OB_MENU_ENTRY_TYPE_SEPARATOR:
260             break;
261         }
262
263         g_free(self);
264     }
265 }
266
267 void menu_clear_entries(ObMenu *self)
268 {
269     /* XXX assert that the menu isn't visible */
270
271     while (self->entries) {
272         menu_entry_free(self->entries->data);
273         self->entries = g_list_delete_link(self->entries, self->entries);
274     }
275 }
276
277 void menu_entry_remove(ObMenuEntry *self)
278 {
279     self->menu->entries = g_list_remove(self->menu->entries, self);
280     menu_entry_free(self);
281 }
282
283 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
284                              GSList *actions)
285 {
286     ObMenuEntry *e;
287
288     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
289     e->data.normal.label = g_strdup(label);
290     e->data.normal.actions = actions;
291
292     self->entries = g_list_append(self->entries, e);
293     return e;
294 }
295
296 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
297 {
298     ObMenuEntry *e;
299
300     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
301     e->data.submenu.name = g_strdup(submenu);
302
303     self->entries = g_list_append(self->entries, e);
304     return e;
305 }
306
307 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
308 {
309     ObMenuEntry *e;
310
311     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
312
313     self->entries = g_list_append(self->entries, e);
314     return e;
315 }
316
317 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
318 {
319     self->update_func = func;
320 }
321
322 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
323 {
324     self->execute_func = func;
325 }
326
327 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
328 {
329     self->destroy_func = func;
330 }
331
332 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
333 {
334     ObMenuEntry *ret = NULL;
335     GList *it;
336
337     for (it = self->entries; it; it = g_list_next(it)) {
338         ObMenuEntry *e = it->data;
339
340         if (e->id == id) {
341             ret = e;
342             break;
343         }
344     }
345     return ret;
346 }
347
348 void menu_find_submenus(ObMenu *self)
349 {
350     GList *it;
351
352     for (it = self->entries; it; it = g_list_next(it)) {
353         ObMenuEntry *e = it->data;
354
355         if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
356             e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
357     }
358 }