]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
was using random memory for parsing pipe menus with the new parse api
[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-2007 Dana 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 "grab.h"
25 #include "client.h"
26 #include "config.h"
27 #include "actions.h"
28 #include "screen.h"
29 #include "menuframe.h"
30 #include "keyboard.h"
31 #include "geom.h"
32 #include "misc.h"
33 #include "client_menu.h"
34 #include "client_list_menu.h"
35 #include "client_list_combined_menu.h"
36 #include "gettext.h"
37 #include "obt/parse.h"
38
39 typedef struct _ObMenuParseState ObMenuParseState;
40
41 struct _ObMenuParseState
42 {
43 ObMenu *parent;
44 ObMenu *pipe_creator;
45 };
46
47 static GHashTable *menu_hash = NULL;
48 static ObtParseInst *menu_parse_inst;
49 static ObMenuParseState menu_parse_state;
50 static gboolean menu_can_hide = FALSE;
51
52 static void menu_destroy_hash_value(ObMenu *self);
53 static void parse_menu_item(xmlNodePtr node, gpointer data);
54 static void parse_menu_separator(xmlNodePtr node, gpointer data);
55 static void parse_menu(xmlNodePtr node, gpointer data);
56 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
57 gchar **strippedlabel, guint *position,
58 gboolean *always_show);
59
60
61 static void client_dest(ObClient *client, gpointer data)
62 {
63 /* menus can be associated with a client, so close any that are since
64 we are disappearing now */
65 menu_frame_hide_all_client(client);
66 }
67
68 void menu_startup(gboolean reconfig)
69 {
70 gboolean loaded = FALSE;
71 GSList *it;
72
73 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
74 (GDestroyNotify)menu_destroy_hash_value);
75
76 client_list_menu_startup(reconfig);
77 client_list_combined_menu_startup(reconfig);
78 client_menu_startup();
79
80 menu_parse_inst = obt_parse_instance_new();
81
82 menu_parse_state.parent = NULL;
83 menu_parse_state.pipe_creator = NULL;
84 obt_parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
85 obt_parse_register(menu_parse_inst, "item", parse_menu_item,
86 &menu_parse_state);
87 obt_parse_register(menu_parse_inst, "separator",
88 parse_menu_separator, &menu_parse_state);
89
90 for (it = config_menu_files; it; it = g_slist_next(it)) {
91 if (obt_parse_load_config_file(menu_parse_inst,
92 "openbox",
93 it->data,
94 "openbox_menu"))
95 {
96 loaded = TRUE;
97 obt_parse_tree_from_root(menu_parse_inst);
98 obt_parse_close(menu_parse_inst);
99 } else
100 g_message(_("Unable to find a valid menu file '%s'"),
101 (const gchar*)it->data);
102 }
103 if (!loaded) {
104 if (obt_parse_load_config_file(menu_parse_inst,
105 "openbox",
106 "menu.xml",
107 "openbox_menu"))
108 {
109 obt_parse_tree_from_root(menu_parse_inst);
110 obt_parse_close(menu_parse_inst);
111 } else
112 g_message(_("Unable to find a valid menu file '%s'"),
113 "menu.xml");
114 }
115
116 g_assert(menu_parse_state.parent == NULL);
117
118 if (!reconfig)
119 client_add_destroy_notify(client_dest, NULL);
120 }
121
122 void menu_shutdown(gboolean reconfig)
123 {
124 if (!reconfig)
125 client_remove_destroy_notify(client_dest);
126
127 obt_parse_instance_unref(menu_parse_inst);
128 menu_parse_inst = NULL;
129
130 client_list_menu_shutdown(reconfig);
131 client_list_combined_menu_shutdown(reconfig);
132
133 menu_frame_hide_all();
134 g_hash_table_destroy(menu_hash);
135 menu_hash = NULL;
136 }
137
138 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
139 {
140 ObMenu *menu = val;
141 return menu->pipe_creator != NULL;
142 }
143
144 static void clear_cache(gpointer key, gpointer val, gpointer data)
145 {
146 ObMenu *menu = val;
147 if (menu->execute)
148 menu_clear_entries(menu);
149 }
150
151 void menu_clear_pipe_caches(void)
152 {
153 /* delete any pipe menus' submenus */
154 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, NULL);
155 /* empty the top level pipe menus */
156 g_hash_table_foreach(menu_hash, clear_cache, NULL);
157 }
158
159 void menu_pipe_execute(ObMenu *self)
160 {
161 gchar *output;
162 GError *err = NULL;
163
164 if (!self->execute)
165 return;
166 if (self->entries) /* the entries are already created and cached */
167 return;
168
169 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
170 g_message(_("Failed to execute command for pipe-menu '%s': %s"),
171 self->execute, err->message);
172 g_error_free(err);
173 return;
174 }
175
176 if (obt_parse_load_mem(menu_parse_inst, output, strlen(output),
177 "openbox_pipe_menu"))
178 {
179 menu_parse_state.pipe_creator = self;
180 menu_parse_state.parent = self;
181 obt_parse_tree_from_root(menu_parse_inst);
182 obt_parse_close(menu_parse_inst);
183 } else {
184 g_message(_("Invalid output from pipe-menu '%s'"), self->execute);
185 }
186
187 g_free(output);
188 }
189
190 static ObMenu* menu_from_name(gchar *name)
191 {
192 ObMenu *self = NULL;
193
194 g_assert(name != NULL);
195
196 if (!(self = g_hash_table_lookup(menu_hash, name)))
197 g_message(_("Attempted to access menu '%s' but it does not exist"),
198 name);
199 return self;
200 }
201
202 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
203 ((c) >= 'A' && (c) <= 'Z') || \
204 ((c) >= 'a' && (c) <= 'z'))
205
206 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
207 gchar **strippedlabel, guint *position,
208 gboolean *always_show)
209 {
210 gunichar shortcut = 0;
211
212 *position = 0;
213 *always_show = FALSE;
214
215 g_assert(strippedlabel != NULL);
216
217 if (label == NULL) {
218 *strippedlabel = NULL;
219 } else {
220 gchar *i;
221
222 *strippedlabel = g_strdup(label);
223
224 /* if allow_shortcut is false, then you can't use the '_', instead you
225 have to just use the first valid character
226 */
227
228 i = strchr(*strippedlabel, '_');
229 if (allow_shortcut && i != NULL) {
230 /* there is an underscore in the string */
231
232 /* you have to use a printable ascii character for shortcuts
233 don't allow space either, so you can have like "a _ b"
234 */
235 if (VALID_SHORTCUT(*(i+1))) {
236 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
237 *position = i - *strippedlabel;
238 *always_show = TRUE;
239
240 /* remove the '_' from the string */
241 for (; *i != '\0'; ++i)
242 *i = *(i+1);
243 } else if (*(i+1) == '\0') {
244 /* no default shortcut if the '_' is the last character
245 (eg. "Exit_") for menu entries that you don't want
246 to be executed by mistake
247 */
248 *i = '\0';
249 }
250 } else {
251 /* there is no underscore, so find the first valid character to use
252 instead */
253
254 for (i = *strippedlabel; *i != '\0'; ++i)
255 if (VALID_SHORTCUT(*i)) {
256 *position = i - *strippedlabel;
257 shortcut = g_unichar_tolower(g_utf8_get_char(i));
258 break;
259 }
260 }
261 }
262 return shortcut;
263 }
264
265 static void parse_menu_item(xmlNodePtr node, gpointer data)
266 {
267 ObMenuParseState *state = data;
268 gchar *label;
269
270 if (state->parent) {
271 if (obt_parse_attr_string(node, "label", &label)) {
272 GSList *acts = NULL;
273
274 for (node = node->children; node; node = node->next)
275 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
276 ObActionsAct *a = actions_parse(node);
277 if (a)
278 acts = g_slist_append(acts, a);
279 }
280 menu_add_normal(state->parent, -1, label, acts, TRUE);
281 g_free(label);
282 }
283 }
284 }
285
286 static void parse_menu_separator(xmlNodePtr node, gpointer data)
287 {
288 ObMenuParseState *state = data;
289
290 if (state->parent) {
291 gchar *label;
292
293 if (!obt_parse_attr_string(node, "label", &label))
294 label = NULL;
295
296 menu_add_separator(state->parent, -1, label);
297 g_free(label);
298 }
299 }
300
301 static void parse_menu(xmlNodePtr node, gpointer data)
302 {
303 ObMenuParseState *state = data;
304 gchar *name = NULL, *title = NULL, *script = NULL;
305 ObMenu *menu;
306
307 if (!obt_parse_attr_string(node, "id", &name))
308 goto parse_menu_fail;
309
310 if (!g_hash_table_lookup(menu_hash, name)) {
311 if (!obt_parse_attr_string(node, "label", &title))
312 goto parse_menu_fail;
313
314 if ((menu = menu_new(name, title, TRUE, NULL))) {
315 menu->pipe_creator = state->pipe_creator;
316 if (obt_parse_attr_string(node, "execute", &script)) {
317 menu->execute = parse_expand_tilde(script);
318 } else {
319 ObMenu *old;
320
321 old = state->parent;
322 state->parent = menu;
323 obt_parse_tree(menu_parse_inst, node->children);
324 state->parent = old;
325 }
326 }
327 }
328
329 if (state->parent)
330 menu_add_submenu(state->parent, -1, name);
331
332 parse_menu_fail:
333 g_free(name);
334 g_free(title);
335 g_free(script);
336 }
337
338 ObMenu* menu_new(const gchar *name, const gchar *title,
339 gboolean allow_shortcut_selection, gpointer data)
340 {
341 ObMenu *self;
342
343 self = g_new0(ObMenu, 1);
344 self->name = g_strdup(name);
345 self->data = data;
346
347 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
348 &self->title, &self->shortcut_position,
349 &self->shortcut_always_show);
350
351 g_hash_table_replace(menu_hash, self->name, self);
352
353 /* Each menu has a single more_menu. When the menu spills past what
354 can fit on the screen, a new menu frame entry is created from this
355 more_menu, and a new menu frame for the submenu is created for this
356 menu, also pointing to the more_menu.
357
358 This can be done multiple times using the same more_menu.
359
360 more_menu->more_menu will always be NULL, since there is only 1 for
361 each menu. */
362 self->more_menu = g_new0(ObMenu, 1);
363 self->more_menu->name = _("More...");
364 self->more_menu->title = _("More...");
365 self->more_menu->data = data;
366 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
367
368 return self;
369 }
370
371 static void menu_destroy_hash_value(ObMenu *self)
372 {
373 /* make sure its not visible */
374 {
375 GList *it;
376 ObMenuFrame *f;
377
378 for (it = menu_frame_visible; it; it = g_list_next(it)) {
379 f = it->data;
380 if (f->menu == self)
381 menu_frame_hide_all();
382 }
383 }
384
385 if (self->destroy_func)
386 self->destroy_func(self, self->data);
387
388 menu_clear_entries(self);
389 g_free(self->name);
390 g_free(self->title);
391 g_free(self->execute);
392 g_free(self->more_menu);
393
394 g_free(self);
395 }
396
397 void menu_free(ObMenu *menu)
398 {
399 if (menu)
400 g_hash_table_remove(menu_hash, menu->name);
401 }
402
403 static gboolean menu_hide_delay_func(gpointer data)
404 {
405 menu_can_hide = TRUE;
406 return FALSE; /* no repeat */
407 }
408
409 void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
410 {
411 ObMenu *self;
412 ObMenuFrame *frame;
413
414 if (!(self = menu_from_name(name)) ||
415 grab_on_keyboard() || grab_on_pointer()) return;
416
417 /* if the requested menu is already the top visible menu, then don't
418 bother */
419 if (menu_frame_visible) {
420 frame = menu_frame_visible->data;
421 if (frame->menu == self)
422 return;
423 }
424
425 menu_frame_hide_all();
426
427 /* clear the pipe menus when showing a new menu */
428 menu_clear_pipe_caches();
429
430 frame = menu_frame_new(self, 0, client);
431 if (!menu_frame_show_topmenu(frame, x, y, mouse))
432 menu_frame_free(frame);
433 else {
434 if (!mouse) {
435 /* select the first entry if it's not a submenu and we opened
436 * the menu with the keyboard, and skip all headers */
437 GList *it = frame->entries;
438 while (it) {
439 ObMenuEntryFrame *e = it->data;
440 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
441 menu_frame_select(frame, e, FALSE);
442 break;
443 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
444 it = g_list_next(it);
445 else
446 break;
447 }
448 }
449
450 /* reset the hide timer */
451 if (!mouse)
452 menu_can_hide = TRUE;
453 else {
454 menu_can_hide = FALSE;
455 obt_main_loop_timeout_add(ob_main_loop,
456 config_menu_hide_delay * 1000,
457 menu_hide_delay_func,
458 NULL, g_direct_equal, NULL);
459 }
460 }
461 }
462
463 gboolean menu_hide_delay_reached(void)
464 {
465 return menu_can_hide;
466 }
467
468 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
469 {
470 ObMenuEntry *self;
471
472 g_assert(menu);
473
474 self = g_new0(ObMenuEntry, 1);
475 self->ref = 1;
476 self->type = type;
477 self->menu = menu;
478 self->id = id;
479
480 switch (type) {
481 case OB_MENU_ENTRY_TYPE_NORMAL:
482 self->data.normal.enabled = TRUE;
483 break;
484 case OB_MENU_ENTRY_TYPE_SUBMENU:
485 case OB_MENU_ENTRY_TYPE_SEPARATOR:
486 break;
487 }
488
489 return self;
490 }
491
492 void menu_entry_ref(ObMenuEntry *self)
493 {
494 ++self->ref;
495 }
496
497 void menu_entry_unref(ObMenuEntry *self)
498 {
499 if (self && --self->ref == 0) {
500 switch (self->type) {
501 case OB_MENU_ENTRY_TYPE_NORMAL:
502 g_free(self->data.normal.label);
503 while (self->data.normal.actions) {
504 actions_act_unref(self->data.normal.actions->data);
505 self->data.normal.actions =
506 g_slist_delete_link(self->data.normal.actions,
507 self->data.normal.actions);
508 }
509 break;
510 case OB_MENU_ENTRY_TYPE_SUBMENU:
511 g_free(self->data.submenu.name);
512 break;
513 case OB_MENU_ENTRY_TYPE_SEPARATOR:
514 g_free(self->data.separator.label);
515 break;
516 }
517
518 g_free(self);
519 }
520 }
521
522 void menu_clear_entries(ObMenu *self)
523 {
524 #ifdef DEBUG
525 /* assert that the menu isn't visible */
526 {
527 GList *it;
528 ObMenuFrame *f;
529
530 for (it = menu_frame_visible; it; it = g_list_next(it)) {
531 f = it->data;
532 g_assert(f->menu != self);
533 }
534 }
535 #endif
536
537 while (self->entries) {
538 menu_entry_unref(self->entries->data);
539 self->entries = g_list_delete_link(self->entries, self->entries);
540 }
541 self->more_menu->entries = self->entries; /* keep it in sync */
542 }
543
544 void menu_entry_remove(ObMenuEntry *self)
545 {
546 self->menu->entries = g_list_remove(self->menu->entries, self);
547 menu_entry_unref(self);
548 }
549
550 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
551 GSList *actions, gboolean allow_shortcut)
552 {
553 ObMenuEntry *e;
554
555 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
556 e->data.normal.actions = actions;
557
558 menu_entry_set_label(e, label, allow_shortcut);
559
560 self->entries = g_list_append(self->entries, e);
561 self->more_menu->entries = self->entries; /* keep it in sync */
562 return e;
563 }
564
565 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
566 {
567 ObMenuEntry *e;
568 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
569 /* points to itself */
570 e->data.submenu.name = g_strdup(self->name);
571 e->data.submenu.submenu = self;
572 e->data.submenu.show_from = show_from;
573 return e;
574 }
575
576 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
577 {
578 ObMenuEntry *e;
579
580 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
581 e->data.submenu.name = g_strdup(submenu);
582
583 self->entries = g_list_append(self->entries, e);
584 self->more_menu->entries = self->entries; /* keep it in sync */
585 return e;
586 }
587
588 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
589 {
590 ObMenuEntry *e;
591
592 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
593
594 menu_entry_set_label(e, label, FALSE);
595
596 self->entries = g_list_append(self->entries, e);
597 self->more_menu->entries = self->entries; /* keep it in sync */
598 return e;
599 }
600
601 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
602 {
603 self->show_func = func;
604 }
605
606 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
607 {
608 self->hide_func = func;
609 }
610
611 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
612 {
613 self->update_func = func;
614 }
615
616 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
617 {
618 self->execute_func = func;
619 self->more_menu->execute_func = func; /* keep it in sync */
620 }
621
622 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
623 {
624 self->destroy_func = func;
625 }
626
627 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
628 {
629 self->place_func = func;
630 }
631
632 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
633 {
634 ObMenuEntry *ret = NULL;
635 GList *it;
636
637 for (it = self->entries; it; it = g_list_next(it)) {
638 ObMenuEntry *e = it->data;
639
640 if (e->id == id) {
641 ret = e;
642 break;
643 }
644 }
645 return ret;
646 }
647
648 void menu_find_submenus(ObMenu *self)
649 {
650 GList *it;
651
652 for (it = self->entries; it; it = g_list_next(it)) {
653 ObMenuEntry *e = it->data;
654
655 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
656 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
657 }
658 }
659
660 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
661 gboolean allow_shortcut)
662 {
663 switch (self->type) {
664 case OB_MENU_ENTRY_TYPE_SEPARATOR:
665 g_free(self->data.separator.label);
666 self->data.separator.label = g_strdup(label);
667 break;
668 case OB_MENU_ENTRY_TYPE_NORMAL:
669 g_free(self->data.normal.label);
670 self->data.normal.shortcut =
671 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
672 &self->data.normal.shortcut_position,
673 &self->data.normal.shortcut_always_show);
674 break;
675 default:
676 g_assert_not_reached();
677 }
678 }
679
680 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
681 {
682 self->show_all_shortcuts = show;
683 }
This page took 0.061695 seconds and 5 git commands to generate.