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