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