]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
hide menus before showing a new menu root
[chaz/openbox] / 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
166 static ObMenu* menu_from_name(gchar *name)
167 {
168 ObMenu *self = NULL;
169
170 g_assert(name != NULL);
171
172 if (!(self = g_hash_table_lookup(menu_hash, name)))
173 g_warning("Attempted to access menu '%s' but it does not exist.",
174 name);
175 return self;
176 }
177
178 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
179 gpointer data)
180 {
181 ObMenuParseState *state = data;
182 gchar *label;
183
184 if (state->parent) {
185 if (parse_attr_string("label", node, &label)) {
186 GSList *acts = NULL;
187
188 for (node = node->children; node; node = node->next)
189 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action"))
190 acts = g_slist_append(acts, action_parse
191 (i, doc, node,
192 OB_USER_ACTION_MENU_SELECTION));
193 menu_add_normal(state->parent, -1, label, acts);
194 g_free(label);
195 }
196 }
197 }
198
199 static void parse_menu_separator(ObParseInst *i,
200 xmlDocPtr doc, xmlNodePtr node,
201 gpointer data)
202 {
203 ObMenuParseState *state = data;
204
205 if (state->parent)
206 menu_add_separator(state->parent, -1);
207 }
208
209 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
210 gpointer data)
211 {
212 ObMenuParseState *state = data;
213 gchar *name = NULL, *title = NULL, *script = NULL;
214 ObMenu *menu;
215
216 if (!parse_attr_string("id", node, &name))
217 goto parse_menu_fail;
218
219 if (!g_hash_table_lookup(menu_hash, name)) {
220 if (!parse_attr_string("label", node, &title))
221 goto parse_menu_fail;
222
223 if ((menu = menu_new(name, title, NULL))) {
224 menu->pipe_creator = state->pipe_creator;
225 if (parse_attr_string("execute", node, &script)) {
226 menu->execute = ob_expand_tilde(script);
227 } else {
228 ObMenu *old;
229
230 old = state->parent;
231 state->parent = menu;
232 parse_tree(i, doc, node->children);
233 state->parent = old;
234 }
235 }
236 }
237
238 if (state->parent)
239 menu_add_submenu(state->parent, -1, name);
240
241 parse_menu_fail:
242 g_free(name);
243 g_free(title);
244 g_free(script);
245 }
246
247 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
248 {
249 ObMenu *self;
250
251 self = g_new0(ObMenu, 1);
252 self->name = g_strdup(name);
253 self->title = g_strdup(title);
254 self->data = data;
255
256 g_hash_table_replace(menu_hash, self->name, self);
257
258 return self;
259 }
260
261 static void menu_destroy_hash_value(ObMenu *self)
262 {
263 /* XXX make sure its not visible */
264
265 if (self->destroy_func)
266 self->destroy_func(self, self->data);
267
268 menu_clear_entries(self);
269 g_free(self->name);
270 g_free(self->title);
271 g_free(self->execute);
272 }
273
274 void menu_free(ObMenu *menu)
275 {
276 g_hash_table_remove(menu_hash, menu->name);
277 }
278
279 void menu_show(gchar *name, gint x, gint y, ObClient *client)
280 {
281 ObMenu *self;
282 ObMenuFrame *frame;
283
284 if (!(self = menu_from_name(name))) return;
285
286 menu_frame_hide_all();
287
288 frame = menu_frame_new(self, client);
289 menu_frame_move(frame, x, y);
290 menu_frame_show(frame, NULL);
291 if (frame->entries)
292 menu_frame_select_next(frame);
293 }
294
295 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
296 {
297 ObMenuEntry *self;
298
299 g_assert(menu);
300
301 self = g_new0(ObMenuEntry, 1);
302 self->type = type;
303 self->menu = menu;
304 self->id = id;
305
306 switch (type) {
307 case OB_MENU_ENTRY_TYPE_NORMAL:
308 self->data.normal.enabled = TRUE;
309 break;
310 case OB_MENU_ENTRY_TYPE_SUBMENU:
311 case OB_MENU_ENTRY_TYPE_SEPARATOR:
312 break;
313 }
314
315 return self;
316 }
317
318 void menu_entry_free(ObMenuEntry *self)
319 {
320 if (self) {
321 switch (self->type) {
322 case OB_MENU_ENTRY_TYPE_NORMAL:
323 g_free(self->data.normal.label);
324 while (self->data.normal.actions) {
325 action_free(self->data.normal.actions->data);
326 self->data.normal.actions =
327 g_slist_delete_link(self->data.normal.actions,
328 self->data.normal.actions);
329 }
330 break;
331 case OB_MENU_ENTRY_TYPE_SUBMENU:
332 g_free(self->data.submenu.name);
333 break;
334 case OB_MENU_ENTRY_TYPE_SEPARATOR:
335 break;
336 }
337
338 g_free(self);
339 }
340 }
341
342 void menu_clear_entries(ObMenu *self)
343 {
344 /* XXX assert that the menu isn't visible */
345
346 while (self->entries) {
347 menu_entry_free(self->entries->data);
348 self->entries = g_list_delete_link(self->entries, self->entries);
349 }
350 }
351
352 void menu_entry_remove(ObMenuEntry *self)
353 {
354 self->menu->entries = g_list_remove(self->menu->entries, self);
355 menu_entry_free(self);
356 }
357
358 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
359 GSList *actions)
360 {
361 ObMenuEntry *e;
362
363 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
364 e->data.normal.label = g_strdup(label);
365 e->data.normal.actions = actions;
366
367 self->entries = g_list_append(self->entries, e);
368 return e;
369 }
370
371 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
372 {
373 ObMenuEntry *e;
374
375 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
376 e->data.submenu.name = g_strdup(submenu);
377
378 self->entries = g_list_append(self->entries, e);
379 return e;
380 }
381
382 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
383 {
384 ObMenuEntry *e;
385
386 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
387
388 self->entries = g_list_append(self->entries, e);
389 return e;
390 }
391
392 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
393 {
394 self->update_func = func;
395 }
396
397 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
398 {
399 self->execute_func = func;
400 }
401
402 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
403 {
404 self->destroy_func = func;
405 }
406
407 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
408 {
409 ObMenuEntry *ret = NULL;
410 GList *it;
411
412 for (it = self->entries; it; it = g_list_next(it)) {
413 ObMenuEntry *e = it->data;
414
415 if (e->id == id) {
416 ret = e;
417 break;
418 }
419 }
420 return ret;
421 }
422
423 void menu_find_submenus(ObMenu *self)
424 {
425 GList *it;
426
427 for (it = self->entries; it; it = g_list_next(it)) {
428 ObMenuEntry *e = it->data;
429
430 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
431 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
432 }
433 }
This page took 0.05274 seconds and 5 git commands to generate.