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