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