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