]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
allow multiple escaped _'s in a menu label, and allow a real _ to come later in the...
[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/xml.h"
38 #include "obt/paths.h"
39
40 typedef struct _ObMenuParseState ObMenuParseState;
41
42 struct _ObMenuParseState
43 {
44 ObMenu *parent;
45 ObMenu *pipe_creator;
46 };
47
48 static GHashTable *menu_hash = NULL;
49 static ObtXmlInst *menu_parse_inst;
50 static ObMenuParseState menu_parse_state;
51 static gboolean menu_can_hide = FALSE;
52
53 static void menu_destroy_hash_value(ObMenu *self);
54 static void parse_menu_item(xmlNodePtr node, gpointer data);
55 static void parse_menu_separator(xmlNodePtr node, gpointer data);
56 static void parse_menu(xmlNodePtr node, gpointer data);
57 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
58 gchar **strippedlabel, guint *position,
59 gboolean *always_show);
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_xml_instance_new();
81
82 menu_parse_state.parent = NULL;
83 menu_parse_state.pipe_creator = NULL;
84 obt_xml_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
85 obt_xml_register(menu_parse_inst, "item", parse_menu_item,
86 &menu_parse_state);
87 obt_xml_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_xml_load_config_file(menu_parse_inst,
92 "openbox",
93 it->data,
94 "openbox_menu"))
95 {
96 loaded = TRUE;
97 obt_xml_tree_from_root(menu_parse_inst);
98 obt_xml_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_xml_load_config_file(menu_parse_inst,
105 "openbox",
106 "menu.xml",
107 "openbox_menu"))
108 {
109 obt_xml_tree_from_root(menu_parse_inst);
110 obt_xml_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_xml_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_xml_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_xml_tree_from_root(menu_parse_inst);
182 obt_xml_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 gboolean escape;
222
223 *strippedlabel = g_strdup(label);
224
225 /* if allow_shortcut is false, then you can't use the '_', instead you
226 have to just use the first valid character
227 */
228
229 /* allow __ to escape an underscore */
230 i = *strippedlabel;
231 do {
232 escape = FALSE;
233 i = strchr(i, '_');
234 if (i && *(i+1) == '_') {
235 gchar *j;
236
237 /* remove the escape '_' from the string */
238 for (j = i; *j != '\0'; ++j)
239 *j = *(j+1);
240
241 ++i;
242 escape = TRUE;
243 }
244 } while (escape);
245
246 if (allow_shortcut && i != NULL) {
247 /* there is an underscore in the string */
248
249 /* you have to use a printable ascii character for shortcuts
250 don't allow space either, so you can have like "a _ b"
251 */
252 if (VALID_SHORTCUT(*(i+1))) {
253 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
254 *position = i - *strippedlabel;
255 *always_show = TRUE;
256
257 /* remove the '_' from the string */
258 for (; *i != '\0'; ++i)
259 *i = *(i+1);
260 } else if (*(i+1) == '\0') {
261 /* no default shortcut if the '_' is the last character
262 (eg. "Exit_") for menu entries that you don't want
263 to be executed by mistake
264 */
265 *i = '\0';
266 }
267 } else {
268 /* there is no underscore, so find the first valid character to use
269 instead */
270
271 for (i = *strippedlabel; *i != '\0'; ++i)
272 if (VALID_SHORTCUT(*i)) {
273 *position = i - *strippedlabel;
274 shortcut = g_unichar_tolower(g_utf8_get_char(i));
275 break;
276 }
277 }
278 }
279 return shortcut;
280 }
281
282 static void parse_menu_item(xmlNodePtr node, gpointer data)
283 {
284 ObMenuParseState *state = data;
285 gchar *label;
286 gchar *icon;
287 ObMenuEntry *e;
288
289 if (state->parent) {
290 /* Don't try to extract "icon" attribute if icons in user-defined
291 menus are not enabled. */
292
293 if (obt_xml_attr_string(node, "label", &label)) {
294 xmlNodePtr c;
295 GSList *acts = NULL;
296
297 c = obt_xml_find_node(node->children, "action");
298 while (c) {
299 ObActionsAct *action = actions_parse(c);
300 if (action)
301 acts = g_slist_append(acts, action);
302 c = obt_xml_find_node(node->next, "action");
303 }
304 e = menu_add_normal(state->parent, -1, label, acts, TRUE);
305
306 if (config_menu_show_icons &&
307 obt_xml_attr_string(node, "icon", &icon))
308 {
309 RrImage *ic;
310
311 ic = RrImageCacheFindName(ob_rr_icons, icon);
312 if (ic)
313 RrImageRef(ic);
314 else {
315 ic = RrImageNew(ob_rr_icons);
316 if (!RrImageAddPictureName(ic, icon)) {
317 RrImageUnref(ic); /* no need to keep it around */
318 ic = NULL;
319 }
320 }
321 e->data.normal.icon = ic;
322
323 if (e->data.normal.icon)
324 e->data.normal.icon_alpha = 0xff;
325
326 g_free(icon);
327 }
328 g_free(label);
329 }
330 }
331 }
332
333 static void parse_menu_separator(xmlNodePtr node, gpointer data)
334 {
335 ObMenuParseState *state = data;
336
337 if (state->parent) {
338 gchar *label;
339
340 if (!obt_xml_attr_string(node, "label", &label))
341 label = NULL;
342
343 menu_add_separator(state->parent, -1, label);
344 g_free(label);
345 }
346 }
347
348 static void parse_menu(xmlNodePtr node, gpointer data)
349 {
350 ObMenuParseState *state = data;
351 gchar *name = NULL, *title = NULL, *script = NULL;
352 ObMenu *menu;
353
354 if (!obt_xml_attr_string(node, "id", &name))
355 goto parse_menu_fail;
356
357 if (!g_hash_table_lookup(menu_hash, name)) {
358 if (!obt_xml_attr_string(node, "label", &title))
359 goto parse_menu_fail;
360
361 if ((menu = menu_new(name, title, TRUE, NULL))) {
362 menu->pipe_creator = state->pipe_creator;
363 if (obt_xml_attr_string(node, "execute", &script)) {
364 menu->execute = obt_paths_expand_tilde(script);
365 } else {
366 ObMenu *old;
367
368 old = state->parent;
369 state->parent = menu;
370 obt_xml_tree(menu_parse_inst, node->children);
371 state->parent = old;
372 }
373 }
374 }
375
376 if (state->parent)
377 menu_add_submenu(state->parent, -1, name);
378
379 parse_menu_fail:
380 g_free(name);
381 g_free(title);
382 g_free(script);
383 }
384
385 ObMenu* menu_new(const gchar *name, const gchar *title,
386 gboolean allow_shortcut_selection, gpointer data)
387 {
388 ObMenu *self;
389
390 self = g_new0(ObMenu, 1);
391 self->name = g_strdup(name);
392 self->data = data;
393
394 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
395 &self->title, &self->shortcut_position,
396 &self->shortcut_always_show);
397
398 g_hash_table_replace(menu_hash, self->name, self);
399
400 /* Each menu has a single more_menu. When the menu spills past what
401 can fit on the screen, a new menu frame entry is created from this
402 more_menu, and a new menu frame for the submenu is created for this
403 menu, also pointing to the more_menu.
404
405 This can be done multiple times using the same more_menu.
406
407 more_menu->more_menu will always be NULL, since there is only 1 for
408 each menu. */
409 self->more_menu = g_new0(ObMenu, 1);
410 self->more_menu->name = _("More...");
411 self->more_menu->title = _("More...");
412 self->more_menu->data = data;
413 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
414
415 return self;
416 }
417
418 static void menu_destroy_hash_value(ObMenu *self)
419 {
420 /* make sure its not visible */
421 {
422 GList *it;
423 ObMenuFrame *f;
424
425 for (it = menu_frame_visible; it; it = g_list_next(it)) {
426 f = it->data;
427 if (f->menu == self)
428 menu_frame_hide_all();
429 }
430 }
431
432 if (self->destroy_func)
433 self->destroy_func(self, self->data);
434
435 menu_clear_entries(self);
436 g_free(self->name);
437 g_free(self->title);
438 g_free(self->execute);
439 g_free(self->more_menu);
440
441 g_free(self);
442 }
443
444 void menu_free(ObMenu *menu)
445 {
446 if (menu)
447 g_hash_table_remove(menu_hash, menu->name);
448 }
449
450 static gboolean menu_hide_delay_func(gpointer data)
451 {
452 menu_can_hide = TRUE;
453 return FALSE; /* no repeat */
454 }
455
456 void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
457 {
458 ObMenu *self;
459 ObMenuFrame *frame;
460
461 if (!(self = menu_from_name(name)) ||
462 grab_on_keyboard() || grab_on_pointer()) return;
463
464 /* if the requested menu is already the top visible menu, then don't
465 bother */
466 if (menu_frame_visible) {
467 frame = menu_frame_visible->data;
468 if (frame->menu == self)
469 return;
470 }
471
472 menu_frame_hide_all();
473
474 /* clear the pipe menus when showing a new menu */
475 menu_clear_pipe_caches();
476
477 frame = menu_frame_new(self, 0, client);
478 if (!menu_frame_show_topmenu(frame, x, y, mouse))
479 menu_frame_free(frame);
480 else {
481 if (!mouse) {
482 /* select the first entry if it's not a submenu and we opened
483 * the menu with the keyboard, and skip all headers */
484 GList *it = frame->entries;
485 while (it) {
486 ObMenuEntryFrame *e = it->data;
487 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
488 menu_frame_select(frame, e, FALSE);
489 break;
490 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
491 it = g_list_next(it);
492 else
493 break;
494 }
495 }
496
497 /* reset the hide timer */
498 if (!mouse)
499 menu_can_hide = TRUE;
500 else {
501 menu_can_hide = FALSE;
502 obt_main_loop_timeout_add(ob_main_loop,
503 config_menu_hide_delay * 1000,
504 menu_hide_delay_func,
505 NULL, g_direct_equal, NULL);
506 }
507 }
508 }
509
510 gboolean menu_hide_delay_reached(void)
511 {
512 return menu_can_hide;
513 }
514
515 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
516 {
517 ObMenuEntry *self;
518
519 g_assert(menu);
520
521 self = g_new0(ObMenuEntry, 1);
522 self->ref = 1;
523 self->type = type;
524 self->menu = menu;
525 self->id = id;
526
527 switch (type) {
528 case OB_MENU_ENTRY_TYPE_NORMAL:
529 self->data.normal.enabled = TRUE;
530 break;
531 case OB_MENU_ENTRY_TYPE_SUBMENU:
532 case OB_MENU_ENTRY_TYPE_SEPARATOR:
533 break;
534 }
535
536 return self;
537 }
538
539 void menu_entry_ref(ObMenuEntry *self)
540 {
541 ++self->ref;
542 }
543
544 void menu_entry_unref(ObMenuEntry *self)
545 {
546 if (self && --self->ref == 0) {
547 switch (self->type) {
548 case OB_MENU_ENTRY_TYPE_NORMAL:
549 RrImageUnref(self->data.normal.icon);
550 g_free(self->data.normal.label);
551 while (self->data.normal.actions) {
552 actions_act_unref(self->data.normal.actions->data);
553 self->data.normal.actions =
554 g_slist_delete_link(self->data.normal.actions,
555 self->data.normal.actions);
556 }
557 break;
558 case OB_MENU_ENTRY_TYPE_SUBMENU:
559 g_free(self->data.submenu.name);
560 break;
561 case OB_MENU_ENTRY_TYPE_SEPARATOR:
562 g_free(self->data.separator.label);
563 break;
564 }
565
566 g_free(self);
567 }
568 }
569
570 void menu_clear_entries(ObMenu *self)
571 {
572 #ifdef DEBUG
573 /* assert that the menu isn't visible */
574 {
575 GList *it;
576 ObMenuFrame *f;
577
578 for (it = menu_frame_visible; it; it = g_list_next(it)) {
579 f = it->data;
580 g_assert(f->menu != self);
581 }
582 }
583 #endif
584
585 while (self->entries) {
586 menu_entry_unref(self->entries->data);
587 self->entries = g_list_delete_link(self->entries, self->entries);
588 }
589 self->more_menu->entries = self->entries; /* keep it in sync */
590 }
591
592 void menu_entry_remove(ObMenuEntry *self)
593 {
594 self->menu->entries = g_list_remove(self->menu->entries, self);
595 menu_entry_unref(self);
596 }
597
598 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
599 GSList *actions, gboolean allow_shortcut)
600 {
601 ObMenuEntry *e;
602
603 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
604 e->data.normal.actions = actions;
605
606 menu_entry_set_label(e, label, allow_shortcut);
607
608 self->entries = g_list_append(self->entries, e);
609 self->more_menu->entries = self->entries; /* keep it in sync */
610 return e;
611 }
612
613 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
614 {
615 ObMenuEntry *e;
616 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
617 /* points to itself */
618 e->data.submenu.name = g_strdup(self->name);
619 e->data.submenu.submenu = self;
620 e->data.submenu.show_from = show_from;
621 return e;
622 }
623
624 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
625 {
626 ObMenuEntry *e;
627
628 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
629 e->data.submenu.name = g_strdup(submenu);
630
631 self->entries = g_list_append(self->entries, e);
632 self->more_menu->entries = self->entries; /* keep it in sync */
633 return e;
634 }
635
636 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
637 {
638 ObMenuEntry *e;
639
640 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
641
642 menu_entry_set_label(e, label, FALSE);
643
644 self->entries = g_list_append(self->entries, e);
645 self->more_menu->entries = self->entries; /* keep it in sync */
646 return e;
647 }
648
649 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
650 {
651 self->show_func = func;
652 }
653
654 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
655 {
656 self->hide_func = func;
657 }
658
659 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
660 {
661 self->update_func = func;
662 }
663
664 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
665 {
666 self->execute_func = func;
667 self->more_menu->execute_func = func; /* keep it in sync */
668 }
669
670 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
671 {
672 self->destroy_func = func;
673 }
674
675 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
676 {
677 self->place_func = func;
678 }
679
680 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
681 {
682 ObMenuEntry *ret = NULL;
683 GList *it;
684
685 for (it = self->entries; it; it = g_list_next(it)) {
686 ObMenuEntry *e = it->data;
687
688 if (e->id == id) {
689 ret = e;
690 break;
691 }
692 }
693 return ret;
694 }
695
696 void menu_find_submenus(ObMenu *self)
697 {
698 GList *it;
699
700 for (it = self->entries; it; it = g_list_next(it)) {
701 ObMenuEntry *e = it->data;
702
703 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
704 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
705 }
706 }
707
708 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
709 gboolean allow_shortcut)
710 {
711 switch (self->type) {
712 case OB_MENU_ENTRY_TYPE_SEPARATOR:
713 g_free(self->data.separator.label);
714 self->data.separator.label = g_strdup(label);
715 break;
716 case OB_MENU_ENTRY_TYPE_NORMAL:
717 g_free(self->data.normal.label);
718 self->data.normal.shortcut =
719 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
720 &self->data.normal.shortcut_position,
721 &self->data.normal.shortcut_always_show);
722 break;
723 default:
724 g_assert_not_reached();
725 }
726 }
727
728 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
729 {
730 self->show_all_shortcuts = show;
731 }
This page took 0.071129 seconds and 5 git commands to generate.