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