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