]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
well.. it compiles..
[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 "mainloop.h"
24 #include "stacking.h"
25 #include "grab.h"
26 #include "client.h"
27 #include "config.h"
28 #include "actions.h"
29 #include "screen.h"
30 #include "menuframe.h"
31 #include "keyboard.h"
32 #include "geom.h"
33 #include "misc.h"
34 #include "client_menu.h"
35 #include "client_list_menu.h"
36 #include "client_list_combined_menu.h"
37 #include "gettext.h"
38 #include "parser/parse.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 ObParseInst *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(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
55 gpointer data);
56 static void parse_menu_separator(ObParseInst *i,
57 xmlDocPtr doc, xmlNodePtr node,
58 gpointer data);
59 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
60 gpointer data);
61 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
62 gchar **strippedlabel, guint *position);
63
64
65 static void client_dest(ObClient *client, gpointer data)
66 {
67 /* menus can be associated with a client, so close any that are since
68 we are disappearing now */
69 menu_frame_hide_all_client(client);
70 }
71
72 void menu_startup(gboolean reconfig)
73 {
74 xmlDocPtr doc;
75 xmlNodePtr node;
76 gboolean loaded = FALSE;
77 GSList *it;
78
79 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
80 (GDestroyNotify)menu_destroy_hash_value);
81
82 client_list_menu_startup(reconfig);
83 client_list_combined_menu_startup(reconfig);
84 client_menu_startup();
85
86 menu_parse_inst = parse_startup();
87
88 menu_parse_state.parent = NULL;
89 menu_parse_state.pipe_creator = NULL;
90 parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
91 parse_register(menu_parse_inst, "item", parse_menu_item,
92 &menu_parse_state);
93 parse_register(menu_parse_inst, "separator",
94 parse_menu_separator, &menu_parse_state);
95
96 for (it = config_menu_files; it; it = g_slist_next(it)) {
97 if (parse_load_menu(it->data, &doc, &node)) {
98 loaded = TRUE;
99 parse_tree(menu_parse_inst, doc, node->children);
100 xmlFreeDoc(doc);
101 } else
102 g_message(_("Unable to find a valid menu file '%s'"),
103 (const gchar*)it->data);
104 }
105 if (!loaded) {
106 if (parse_load_menu("menu.xml", &doc, &node)) {
107 parse_tree(menu_parse_inst, doc, node->children);
108 xmlFreeDoc(doc);
109 } else
110 g_message(_("Unable to find a valid menu file '%s'"),
111 "menu.xml");
112 }
113
114 g_assert(menu_parse_state.parent == NULL);
115
116 if (!reconfig)
117 client_add_destroy_notify(client_dest, NULL);
118 }
119
120 void menu_shutdown(gboolean reconfig)
121 {
122 if (!reconfig)
123 client_remove_destroy_notify(client_dest);
124
125 parse_shutdown(menu_parse_inst);
126 menu_parse_inst = NULL;
127
128 client_list_menu_shutdown(reconfig);
129 client_list_combined_menu_shutdown(reconfig);
130
131 menu_frame_hide_all();
132 g_hash_table_destroy(menu_hash);
133 menu_hash = NULL;
134 }
135
136 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
137 {
138 ObMenu *menu = val;
139 return menu->pipe_creator != NULL;
140 }
141
142 static void clear_cache(gpointer key, gpointer val, gpointer data)
143 {
144 ObMenu *menu = val;
145 if (menu->execute)
146 menu_clear_entries(menu);
147 }
148
149 void menu_clear_pipe_caches()
150 {
151 /* delete any pipe menus' submenus */
152 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, NULL);
153 /* empty the top level pipe menus */
154 g_hash_table_foreach(menu_hash, clear_cache, NULL);
155 }
156
157 void menu_pipe_execute(ObMenu *self)
158 {
159 xmlDocPtr doc;
160 xmlNodePtr node;
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 (parse_load_mem(output, strlen(output),
177 "openbox_pipe_menu", &doc, &node))
178 {
179 menu_parse_state.pipe_creator = self;
180 menu_parse_state.parent = self;
181 parse_tree(menu_parse_inst, doc, node->children);
182 xmlFreeDoc(doc);
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 {
209 gunichar shortcut = 0;
210
211 *position = 0;
212
213 g_assert(strippedlabel != NULL);
214
215 if (label == NULL) {
216 *strippedlabel = NULL;
217 } else {
218 gchar *i;
219
220 *strippedlabel = g_strdup(label);
221
222 /* if allow_shortcut is false, then you can't use the &, instead you
223 have to just use the first valid character
224 */
225
226 i = strchr(*strippedlabel, '&');
227 if (allow_shortcut && i != NULL) {
228 /* there is an ampersand in the string */
229
230 /* you have to use a printable ascii character for shortcuts
231 don't allow space either, so you can have like "a & b"
232 */
233 if (VALID_SHORTCUT(*(i+1))) {
234 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
235 *position = i - *strippedlabel;
236
237 /* remove the & from the string */
238 for (; *i != '\0'; ++i)
239 *i = *(i+1);
240 }
241 } else {
242 /* there is no ampersand, so find the first valid character to use
243 instead */
244
245 for (i = *strippedlabel; *i != '\0'; ++i)
246 if (VALID_SHORTCUT(*i)) {
247 *position = i - *strippedlabel;
248 shortcut = g_unichar_tolower(g_utf8_get_char(i));
249 break;
250 }
251 }
252 }
253 return shortcut;
254 }
255
256 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
257 gpointer data)
258 {
259 ObMenuParseState *state = data;
260 gchar *label;
261
262 if (state->parent) {
263 if (parse_attr_string("label", node, &label)) {
264 GSList *acts = NULL;
265
266 for (node = node->children; node; node = node->next)
267 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
268 ObActionsAct *a = actions_parse(i, doc, node);
269 if (a)
270 acts = g_slist_append(acts, a);
271 }
272 menu_add_normal(state->parent, -1, label, acts, FALSE);
273 g_free(label);
274 }
275 }
276 }
277
278 static void parse_menu_separator(ObParseInst *i,
279 xmlDocPtr doc, xmlNodePtr node,
280 gpointer data)
281 {
282 ObMenuParseState *state = data;
283
284 if (state->parent) {
285 gchar *label;
286
287 if (!parse_attr_string("label", node, &label))
288 label = NULL;
289
290 menu_add_separator(state->parent, -1, label);
291 g_free(label);
292 }
293 }
294
295 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
296 gpointer data)
297 {
298 ObMenuParseState *state = data;
299 gchar *name = NULL, *title = NULL, *script = NULL;
300 ObMenu *menu;
301
302 if (!parse_attr_string("id", node, &name))
303 goto parse_menu_fail;
304
305 if (!g_hash_table_lookup(menu_hash, name)) {
306 if (!parse_attr_string("label", node, &title))
307 goto parse_menu_fail;
308
309 if ((menu = menu_new(name, title, FALSE, NULL))) {
310 menu->pipe_creator = state->pipe_creator;
311 if (parse_attr_string("execute", node, &script)) {
312 menu->execute = parse_expand_tilde(script);
313 } else {
314 ObMenu *old;
315
316 old = state->parent;
317 state->parent = menu;
318 parse_tree(i, doc, node->children);
319 state->parent = old;
320 }
321 }
322 }
323
324 if (state->parent)
325 menu_add_submenu(state->parent, -1, name);
326
327 parse_menu_fail:
328 g_free(name);
329 g_free(title);
330 g_free(script);
331 }
332
333 ObMenu* menu_new(const gchar *name, const gchar *title,
334 gboolean allow_shortcut_selection, gpointer data)
335 {
336 ObMenu *self;
337
338 self = g_new0(ObMenu, 1);
339 self->name = g_strdup(name);
340 self->data = data;
341
342 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
343 &self->title, &self->shortcut_position);
344
345 g_hash_table_replace(menu_hash, self->name, self);
346
347 /* Each menu has a single more_menu. When the menu spills past what
348 can fit on the screen, a new menu frame entry is created from this
349 more_menu, and a new menu frame for the submenu is created for this
350 menu, also pointing to the more_menu.
351
352 This can be done multiple times using the same more_menu.
353
354 more_menu->more_menu will always be NULL, since there is only 1 for
355 each menu. */
356 self->more_menu = g_new0(ObMenu, 1);
357 self->more_menu->name = _("More...");
358 self->more_menu->title = _("More...");
359 self->more_menu->data = data;
360 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
361
362 self->more_menu->show_func = self->show_func;
363 self->more_menu->hide_func = self->hide_func;
364 self->more_menu->update_func = self->update_func;
365 self->more_menu->execute_func = self->execute_func;
366 self->more_menu->destroy_func = self->destroy_func;
367 self->more_menu->place_func = self->place_func;
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, gint button, 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, button))
433 menu_frame_free(frame);
434 else {
435 if (!button) {
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 (!button)
453 menu_can_hide = TRUE;
454 else {
455 menu_can_hide = FALSE;
456 ob_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()
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 g_free(self->data.normal.label);
504 while (self->data.normal.actions) {
505 actions_act_unref(self->data.normal.actions->data);
506 self->data.normal.actions =
507 g_slist_delete_link(self->data.normal.actions,
508 self->data.normal.actions);
509 }
510 break;
511 case OB_MENU_ENTRY_TYPE_SUBMENU:
512 g_free(self->data.submenu.name);
513 break;
514 case OB_MENU_ENTRY_TYPE_SEPARATOR:
515 break;
516 }
517
518 g_free(self);
519 }
520 }
521
522 void menu_clear_entries(ObMenu *self)
523 {
524 #ifdef DEBUG
525 /* assert that the menu isn't visible */
526 {
527 GList *it;
528 ObMenuFrame *f;
529
530 for (it = menu_frame_visible; it; it = g_list_next(it)) {
531 f = it->data;
532 g_assert(f->menu != self);
533 }
534 }
535 #endif
536
537 while (self->entries) {
538 menu_entry_unref(self->entries->data);
539 self->entries = g_list_delete_link(self->entries, self->entries);
540 }
541 self->more_menu->entries = self->entries; /* keep it in sync */
542 }
543
544 void menu_entry_remove(ObMenuEntry *self)
545 {
546 self->menu->entries = g_list_remove(self->menu->entries, self);
547 menu_entry_unref(self);
548 }
549
550 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
551 GSList *actions, gboolean allow_shortcut)
552 {
553 ObMenuEntry *e;
554
555 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
556 e->data.normal.actions = actions;
557
558 menu_entry_set_label(e, label, allow_shortcut);
559
560 self->entries = g_list_append(self->entries, e);
561 self->more_menu->entries = self->entries; /* keep it in sync */
562 return e;
563 }
564
565 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
566 {
567 ObMenuEntry *e;
568 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
569 /* points to itself */
570 e->data.submenu.name = g_strdup(self->name);
571 e->data.submenu.submenu = self;
572 e->data.submenu.show_from = show_from;
573 return e;
574 }
575
576 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
577 {
578 ObMenuEntry *e;
579
580 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
581 e->data.submenu.name = g_strdup(submenu);
582
583 self->entries = g_list_append(self->entries, e);
584 self->more_menu->entries = self->entries; /* keep it in sync */
585 return e;
586 }
587
588 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
589 {
590 ObMenuEntry *e;
591
592 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
593
594 menu_entry_set_label(e, label, FALSE);
595
596 self->entries = g_list_append(self->entries, e);
597 self->more_menu->entries = self->entries; /* keep it in sync */
598 return e;
599 }
600
601 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
602 {
603 self->show_func = func;
604 self->more_menu->show_func = func; /* keep it in sync */
605 }
606
607 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
608 {
609 self->hide_func = func;
610 self->more_menu->hide_func = func; /* keep it in sync */
611 }
612
613 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
614 {
615 self->update_func = func;
616 self->more_menu->update_func = func; /* keep it in sync */
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 self->more_menu->destroy_func = func; /* keep it in sync */
629 }
630
631 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
632 {
633 self->place_func = func;
634 self->more_menu->place_func = func; /* keep it in sync */
635 }
636
637 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
638 {
639 ObMenuEntry *ret = NULL;
640 GList *it;
641
642 for (it = self->entries; it; it = g_list_next(it)) {
643 ObMenuEntry *e = it->data;
644
645 if (e->id == id) {
646 ret = e;
647 break;
648 }
649 }
650 return ret;
651 }
652
653 void menu_find_submenus(ObMenu *self)
654 {
655 GList *it;
656
657 for (it = self->entries; it; it = g_list_next(it)) {
658 ObMenuEntry *e = it->data;
659
660 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
661 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
662 }
663 }
664
665 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
666 gboolean allow_shortcut)
667 {
668 switch (self->type) {
669 case OB_MENU_ENTRY_TYPE_SEPARATOR:
670 g_free(self->data.separator.label);
671 self->data.separator.label = g_strdup(label);
672 break;
673 case OB_MENU_ENTRY_TYPE_NORMAL:
674 g_free(self->data.normal.label);
675 self->data.normal.shortcut =
676 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
677 &self->data.normal.shortcut_position);
678 break;
679 default:
680 g_assert_not_reached();
681 }
682 }
683
684 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
685 {
686 self->show_all_shortcuts = show;
687 }
This page took 0.068501 seconds and 5 git commands to generate.