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