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