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