1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 menu.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
30 #include "menuframe.h"
34 #include "client_menu.h"
35 #include "client_list_menu.h"
36 #include "client_list_combined_menu.h"
38 #include "parser/parse.h"
40 typedef struct _ObMenuParseState ObMenuParseState
;
42 struct _ObMenuParseState
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
;
53 static void menu_destroy_hash_value(ObMenu
*self
);
54 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
56 static void parse_menu_separator(ObParseInst
*i
,
57 xmlDocPtr doc
, xmlNodePtr node
,
59 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
61 static gunichar
parse_shortcut(const gchar
*label
, gboolean allow_shortcut
,
62 gchar
**strippedlabel
, guint
*position
,
63 gboolean
*always_show
);
66 static void client_dest(ObClient
*client
, gpointer data
)
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
);
73 void menu_startup(gboolean reconfig
)
77 gboolean loaded
= FALSE
;
80 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
81 (GDestroyNotify
)menu_destroy_hash_value
);
83 client_list_menu_startup(reconfig
);
84 client_list_combined_menu_startup(reconfig
);
85 client_menu_startup();
87 menu_parse_inst
= parse_startup();
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
,
94 parse_register(menu_parse_inst
, "separator",
95 parse_menu_separator
, &menu_parse_state
);
97 for (it
= config_menu_files
; it
; it
= g_slist_next(it
)) {
98 if (parse_load_menu(it
->data
, &doc
, &node
)) {
100 parse_tree(menu_parse_inst
, doc
, node
->children
);
103 g_message(_("Unable to find a valid menu file '%s'"),
104 (const gchar
*)it
->data
);
107 if (parse_load_menu("menu.xml", &doc
, &node
)) {
108 parse_tree(menu_parse_inst
, doc
, node
->children
);
111 g_message(_("Unable to find a valid menu file '%s'"),
115 g_assert(menu_parse_state
.parent
== NULL
);
118 client_add_destroy_notify(client_dest
, NULL
);
121 void menu_shutdown(gboolean reconfig
)
124 client_remove_destroy_notify(client_dest
);
126 parse_shutdown(menu_parse_inst
);
127 menu_parse_inst
= NULL
;
129 client_list_menu_shutdown(reconfig
);
130 client_list_combined_menu_shutdown(reconfig
);
132 menu_frame_hide_all();
133 g_hash_table_destroy(menu_hash
);
137 static gboolean
menu_pipe_submenu(gpointer key
, gpointer val
, gpointer data
)
140 return menu
->pipe_creator
!= NULL
;
143 static void clear_cache(gpointer key
, gpointer val
, gpointer data
)
147 menu_clear_entries(menu
);
150 void menu_clear_pipe_caches()
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
);
158 void menu_pipe_execute(ObMenu
*self
)
167 if (self
->entries
) /* the entries are already created and cached */
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
);
177 if (parse_load_mem(output
, strlen(output
),
178 "openbox_pipe_menu", &doc
, &node
))
180 menu_parse_state
.pipe_creator
= self
;
181 menu_parse_state
.parent
= self
;
182 parse_tree(menu_parse_inst
, doc
, node
->children
);
185 g_message(_("Invalid output from pipe-menu '%s'"), self
->execute
);
191 static ObMenu
* menu_from_name(gchar
*name
)
195 g_assert(name
!= NULL
);
197 if (!(self
= g_hash_table_lookup(menu_hash
, name
)))
198 g_message(_("Attempted to access menu '%s' but it does not exist"),
203 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
204 ((c) >= 'A' && (c) <= 'Z') || \
205 ((c) >= 'a' && (c) <= 'z'))
207 static gunichar
parse_shortcut(const gchar
*label
, gboolean allow_shortcut
,
208 gchar
**strippedlabel
, guint
*position
,
209 gboolean
*always_show
)
211 gunichar shortcut
= 0;
214 *always_show
= FALSE
;
216 g_assert(strippedlabel
!= NULL
);
219 *strippedlabel
= NULL
;
223 *strippedlabel
= g_strdup(label
);
225 /* if allow_shortcut is false, then you can't use the '_', instead you
226 have to just use the first valid character
229 i
= strchr(*strippedlabel
, '_');
230 if (allow_shortcut
&& i
!= NULL
) {
231 /* there is an underscore in the string */
233 /* you have to use a printable ascii character for shortcuts
234 don't allow space either, so you can have like "a _ b"
236 if (VALID_SHORTCUT(*(i
+1))) {
237 shortcut
= g_unichar_tolower(g_utf8_get_char(i
+1));
238 *position
= i
- *strippedlabel
;
241 /* remove the '_' from the string */
242 for (; *i
!= '\0'; ++i
)
244 } else if (*(i
+1) == '\0') {
245 /* no default shortcut if the '_' is the last character
246 (eg. "Exit_") for menu entries that you don't want
247 to be executed by mistake
252 /* there is no underscore, so find the first valid character to use
255 for (i
= *strippedlabel
; *i
!= '\0'; ++i
)
256 if (VALID_SHORTCUT(*i
)) {
257 *position
= i
- *strippedlabel
;
258 shortcut
= g_unichar_tolower(g_utf8_get_char(i
));
266 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
269 ObMenuParseState
*state
= data
;
273 if (parse_attr_string("label", node
, &label
)) {
276 for (node
= node
->children
; node
; node
= node
->next
)
277 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "action")) {
278 ObActionsAct
*a
= actions_parse(i
, doc
, node
);
280 acts
= g_slist_append(acts
, a
);
282 menu_add_normal(state
->parent
, -1, label
, acts
, TRUE
);
288 static void parse_menu_separator(ObParseInst
*i
,
289 xmlDocPtr doc
, xmlNodePtr node
,
292 ObMenuParseState
*state
= data
;
297 if (!parse_attr_string("label", node
, &label
))
300 menu_add_separator(state
->parent
, -1, label
);
305 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
308 ObMenuParseState
*state
= data
;
309 gchar
*name
= NULL
, *title
= NULL
, *script
= NULL
;
312 if (!parse_attr_string("id", node
, &name
))
313 goto parse_menu_fail
;
315 if (!g_hash_table_lookup(menu_hash
, name
)) {
316 if (!parse_attr_string("label", node
, &title
))
317 goto parse_menu_fail
;
319 if ((menu
= menu_new(name
, title
, TRUE
, NULL
))) {
320 menu
->pipe_creator
= state
->pipe_creator
;
321 if (parse_attr_string("execute", node
, &script
)) {
322 menu
->execute
= parse_expand_tilde(script
);
327 state
->parent
= menu
;
328 parse_tree(i
, doc
, node
->children
);
335 menu_add_submenu(state
->parent
, -1, name
);
343 ObMenu
* menu_new(const gchar
*name
, const gchar
*title
,
344 gboolean allow_shortcut_selection
, gpointer data
)
348 self
= g_new0(ObMenu
, 1);
349 self
->name
= g_strdup(name
);
352 self
->shortcut
= parse_shortcut(title
, allow_shortcut_selection
,
353 &self
->title
, &self
->shortcut_position
,
354 &self
->shortcut_always_show
);
356 g_hash_table_replace(menu_hash
, self
->name
, self
);
358 /* Each menu has a single more_menu. When the menu spills past what
359 can fit on the screen, a new menu frame entry is created from this
360 more_menu, and a new menu frame for the submenu is created for this
361 menu, also pointing to the more_menu.
363 This can be done multiple times using the same more_menu.
365 more_menu->more_menu will always be NULL, since there is only 1 for
367 self
->more_menu
= g_new0(ObMenu
, 1);
368 self
->more_menu
->name
= _("More...");
369 self
->more_menu
->title
= _("More...");
370 self
->more_menu
->data
= data
;
371 self
->more_menu
->shortcut
= g_unichar_tolower(g_utf8_get_char("M"));
373 self
->more_menu
->show_func
= self
->show_func
;
374 self
->more_menu
->hide_func
= self
->hide_func
;
375 self
->more_menu
->update_func
= self
->update_func
;
376 self
->more_menu
->execute_func
= self
->execute_func
;
377 self
->more_menu
->destroy_func
= self
->destroy_func
;
378 self
->more_menu
->place_func
= self
->place_func
;
383 static void menu_destroy_hash_value(ObMenu
*self
)
385 /* make sure its not visible */
390 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
393 menu_frame_hide_all();
397 if (self
->destroy_func
)
398 self
->destroy_func(self
, self
->data
);
400 menu_clear_entries(self
);
403 g_free(self
->execute
);
404 g_free(self
->more_menu
);
409 void menu_free(ObMenu
*menu
)
412 g_hash_table_remove(menu_hash
, menu
->name
);
415 static gboolean
menu_hide_delay_func(gpointer data
)
417 menu_can_hide
= TRUE
;
418 return FALSE
; /* no repeat */
421 void menu_show(gchar
*name
, gint x
, gint y
, gboolean mouse
, ObClient
*client
)
426 if (!(self
= menu_from_name(name
)) ||
427 grab_on_keyboard() || grab_on_pointer()) return;
429 /* if the requested menu is already the top visible menu, then don't
431 if (menu_frame_visible
) {
432 frame
= menu_frame_visible
->data
;
433 if (frame
->menu
== self
)
437 menu_frame_hide_all();
439 /* clear the pipe menus when showing a new menu */
440 menu_clear_pipe_caches();
442 frame
= menu_frame_new(self
, 0, client
);
443 if (!menu_frame_show_topmenu(frame
, x
, y
, mouse
))
444 menu_frame_free(frame
);
447 /* select the first entry if it's not a submenu and we opened
448 * the menu with the keyboard, and skip all headers */
449 GList
*it
= frame
->entries
;
451 ObMenuEntryFrame
*e
= it
->data
;
452 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
453 menu_frame_select(frame
, e
, FALSE
);
455 } else if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
)
456 it
= g_list_next(it
);
462 /* reset the hide timer */
464 menu_can_hide
= TRUE
;
466 menu_can_hide
= FALSE
;
467 ob_main_loop_timeout_add(ob_main_loop
,
468 config_menu_hide_delay
* 1000,
469 menu_hide_delay_func
,
470 NULL
, g_direct_equal
, NULL
);
475 gboolean
menu_hide_delay_reached()
477 return menu_can_hide
;
480 static ObMenuEntry
* menu_entry_new(ObMenu
*menu
, ObMenuEntryType type
, gint id
)
486 self
= g_new0(ObMenuEntry
, 1);
493 case OB_MENU_ENTRY_TYPE_NORMAL
:
494 self
->data
.normal
.enabled
= TRUE
;
496 case OB_MENU_ENTRY_TYPE_SUBMENU
:
497 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
504 void menu_entry_ref(ObMenuEntry
*self
)
509 void menu_entry_unref(ObMenuEntry
*self
)
511 if (self
&& --self
->ref
== 0) {
512 switch (self
->type
) {
513 case OB_MENU_ENTRY_TYPE_NORMAL
:
514 g_free(self
->data
.normal
.label
);
515 while (self
->data
.normal
.actions
) {
516 actions_act_unref(self
->data
.normal
.actions
->data
);
517 self
->data
.normal
.actions
=
518 g_slist_delete_link(self
->data
.normal
.actions
,
519 self
->data
.normal
.actions
);
522 case OB_MENU_ENTRY_TYPE_SUBMENU
:
523 g_free(self
->data
.submenu
.name
);
525 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
533 void menu_clear_entries(ObMenu
*self
)
536 /* assert that the menu isn't visible */
541 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
543 g_assert(f
->menu
!= self
);
548 while (self
->entries
) {
549 menu_entry_unref(self
->entries
->data
);
550 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
552 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
555 void menu_entry_remove(ObMenuEntry
*self
)
557 self
->menu
->entries
= g_list_remove(self
->menu
->entries
, self
);
558 menu_entry_unref(self
);
561 ObMenuEntry
* menu_add_normal(ObMenu
*self
, gint id
, const gchar
*label
,
562 GSList
*actions
, gboolean allow_shortcut
)
566 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_NORMAL
, id
);
567 e
->data
.normal
.actions
= actions
;
569 menu_entry_set_label(e
, label
, allow_shortcut
);
571 self
->entries
= g_list_append(self
->entries
, e
);
572 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
576 ObMenuEntry
* menu_get_more(ObMenu
*self
, guint show_from
)
579 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, -1);
580 /* points to itself */
581 e
->data
.submenu
.name
= g_strdup(self
->name
);
582 e
->data
.submenu
.submenu
= self
;
583 e
->data
.submenu
.show_from
= show_from
;
587 ObMenuEntry
* menu_add_submenu(ObMenu
*self
, gint id
, const gchar
*submenu
)
591 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, id
);
592 e
->data
.submenu
.name
= g_strdup(submenu
);
594 self
->entries
= g_list_append(self
->entries
, e
);
595 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
599 ObMenuEntry
* menu_add_separator(ObMenu
*self
, gint id
, const gchar
*label
)
603 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SEPARATOR
, id
);
605 menu_entry_set_label(e
, label
, FALSE
);
607 self
->entries
= g_list_append(self
->entries
, e
);
608 self
->more_menu
->entries
= self
->entries
; /* keep it in sync */
612 void menu_set_show_func(ObMenu
*self
, ObMenuShowFunc func
)
614 self
->show_func
= func
;
615 self
->more_menu
->show_func
= func
; /* keep it in sync */
618 void menu_set_hide_func(ObMenu
*self
, ObMenuHideFunc func
)
620 self
->hide_func
= func
;
621 self
->more_menu
->hide_func
= func
; /* keep it in sync */
624 void menu_set_update_func(ObMenu
*self
, ObMenuUpdateFunc func
)
626 self
->update_func
= func
;
627 self
->more_menu
->update_func
= func
; /* keep it in sync */
630 void menu_set_execute_func(ObMenu
*self
, ObMenuExecuteFunc func
)
632 self
->execute_func
= func
;
633 self
->more_menu
->execute_func
= func
; /* keep it in sync */
636 void menu_set_destroy_func(ObMenu
*self
, ObMenuDestroyFunc func
)
638 self
->destroy_func
= func
;
639 self
->more_menu
->destroy_func
= func
; /* keep it in sync */
642 void menu_set_place_func(ObMenu
*self
, ObMenuPlaceFunc func
)
644 self
->place_func
= func
;
645 self
->more_menu
->place_func
= func
; /* keep it in sync */
648 ObMenuEntry
* menu_find_entry_id(ObMenu
*self
, gint id
)
650 ObMenuEntry
*ret
= NULL
;
653 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
654 ObMenuEntry
*e
= it
->data
;
664 void menu_find_submenus(ObMenu
*self
)
668 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
669 ObMenuEntry
*e
= it
->data
;
671 if (e
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
672 e
->data
.submenu
.submenu
= menu_from_name(e
->data
.submenu
.name
);
676 void menu_entry_set_label(ObMenuEntry
*self
, const gchar
*label
,
677 gboolean allow_shortcut
)
679 switch (self
->type
) {
680 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
681 g_free(self
->data
.separator
.label
);
682 self
->data
.separator
.label
= g_strdup(label
);
684 case OB_MENU_ENTRY_TYPE_NORMAL
:
685 g_free(self
->data
.normal
.label
);
686 self
->data
.normal
.shortcut
=
687 parse_shortcut(label
, allow_shortcut
, &self
->data
.normal
.label
,
688 &self
->data
.normal
.shortcut_position
,
689 &self
->data
.normal
.shortcut_always_show
);
692 g_assert_not_reached();
696 void menu_show_all_shortcuts(ObMenu
*self
, gboolean show
)
698 self
->show_all_shortcuts
= show
;