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