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