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