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.
27 #include "menuframe.h"
31 #include "client_menu.h"
32 #include "client_list_menu.h"
33 #include "client_list_combined_menu.h"
34 #include "parser/parse.h"
36 typedef struct _ObMenuParseState ObMenuParseState
;
38 struct _ObMenuParseState
44 static GHashTable
*menu_hash
= NULL
;
45 static ObParseInst
*menu_parse_inst
;
46 static ObMenuParseState menu_parse_state
;
48 static void menu_destroy_hash_value(ObMenu
*self
);
49 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
51 static void parse_menu_separator(ObParseInst
*i
,
52 xmlDocPtr doc
, xmlNodePtr node
,
54 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
57 static void client_dest(ObClient
*client
, gpointer data
)
59 /* menus can be associated with a client, so close any that are since
60 we are disappearing now */
61 menu_frame_hide_all_client(client
);
64 void menu_startup(gboolean reconfig
)
68 gboolean loaded
= FALSE
;
71 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
72 (GDestroyNotify
)menu_destroy_hash_value
);
74 client_list_menu_startup(reconfig
);
75 client_list_combined_menu_startup(reconfig
);
76 client_menu_startup();
78 menu_parse_inst
= parse_startup();
80 menu_parse_state
.parent
= NULL
;
81 menu_parse_state
.pipe_creator
= NULL
;
82 parse_register(menu_parse_inst
, "menu", parse_menu
, &menu_parse_state
);
83 parse_register(menu_parse_inst
, "item", parse_menu_item
,
85 parse_register(menu_parse_inst
, "separator",
86 parse_menu_separator
, &menu_parse_state
);
88 for (it
= config_menu_files
; it
; it
= g_slist_next(it
)) {
89 if (parse_load_menu(it
->data
, &doc
, &node
)) {
91 parse_tree(menu_parse_inst
, doc
, node
->children
);
96 if (parse_load_menu("menu.xml", &doc
, &node
)) {
97 parse_tree(menu_parse_inst
, doc
, node
->children
);
102 g_assert(menu_parse_state
.parent
== NULL
);
105 client_add_destructor(client_dest
, NULL
);
108 void menu_shutdown(gboolean reconfig
)
111 client_remove_destructor(client_dest
);
113 parse_shutdown(menu_parse_inst
);
114 menu_parse_inst
= NULL
;
116 client_list_menu_shutdown(reconfig
);
117 client_list_combined_menu_shutdown(reconfig
);
119 menu_frame_hide_all();
120 g_hash_table_destroy(menu_hash
);
124 static gboolean
menu_pipe_submenu(gpointer key
, gpointer val
, gpointer data
)
127 return menu
->pipe_creator
== data
;
130 void menu_pipe_execute(ObMenu
*self
)
140 if (!g_spawn_command_line_sync(self
->execute
, &output
, NULL
, NULL
, &err
)) {
141 g_warning("Failed to execute command for pipe-menu: %s", err
->message
);
146 if (parse_load_mem(output
, strlen(output
),
147 "openbox_pipe_menu", &doc
, &node
))
149 g_hash_table_foreach_remove(menu_hash
, menu_pipe_submenu
, self
);
150 menu_clear_entries(self
);
152 menu_parse_state
.pipe_creator
= self
;
153 menu_parse_state
.parent
= self
;
154 parse_tree(menu_parse_inst
, doc
, node
->children
);
157 g_warning("Invalid output from pipe-menu: %s", self
->execute
);
163 static ObMenu
* menu_from_name(gchar
*name
)
167 g_assert(name
!= NULL
);
169 if (!(self
= g_hash_table_lookup(menu_hash
, name
)))
170 g_warning("Attempted to access menu '%s' but it does not exist.",
175 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
178 ObMenuParseState
*state
= data
;
182 if (parse_attr_string("label", node
, &label
)) {
185 for (node
= node
->children
; node
; node
= node
->next
)
186 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "action")) {
187 ObAction
*a
= action_parse
188 (i
, doc
, node
, OB_USER_ACTION_MENU_SELECTION
);
190 acts
= g_slist_append(acts
, a
);
192 menu_add_normal(state
->parent
, -1, label
, acts
);
198 static void parse_menu_separator(ObParseInst
*i
,
199 xmlDocPtr doc
, xmlNodePtr node
,
202 ObMenuParseState
*state
= data
;
207 if (!parse_attr_string("label", node
, &label
))
210 menu_add_separator(state
->parent
, -1, label
);
215 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
218 ObMenuParseState
*state
= data
;
219 gchar
*name
= NULL
, *title
= NULL
, *script
= NULL
;
222 if (!parse_attr_string("id", node
, &name
))
223 goto parse_menu_fail
;
225 if (!g_hash_table_lookup(menu_hash
, name
)) {
226 if (!parse_attr_string("label", node
, &title
))
227 goto parse_menu_fail
;
229 if ((menu
= menu_new(name
, title
, NULL
))) {
230 menu
->pipe_creator
= state
->pipe_creator
;
231 if (parse_attr_string("execute", node
, &script
)) {
232 menu
->execute
= parse_expand_tilde(script
);
237 state
->parent
= menu
;
238 parse_tree(i
, doc
, node
->children
);
245 menu_add_submenu(state
->parent
, -1, name
);
253 ObMenu
* menu_new(const gchar
*name
, const gchar
*title
, gpointer data
)
257 self
= g_new0(ObMenu
, 1);
258 self
->name
= g_strdup(name
);
259 self
->title
= g_strdup(title
);
262 g_hash_table_replace(menu_hash
, self
->name
, self
);
267 static void menu_destroy_hash_value(ObMenu
*self
)
269 /* make sure its not visible */
274 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
277 menu_frame_hide_all();
281 if (self
->destroy_func
)
282 self
->destroy_func(self
, self
->data
);
284 menu_clear_entries(self
);
287 g_free(self
->execute
);
292 void menu_free(ObMenu
*menu
)
294 g_hash_table_remove(menu_hash
, menu
->name
);
297 void menu_show(gchar
*name
, gint x
, gint y
, ObClient
*client
)
302 if (!(self
= menu_from_name(name
))
303 || keyboard_interactively_grabbed()) return;
305 /* if the requested menu is already the top visible menu, then don't
307 if (menu_frame_visible
) {
308 frame
= menu_frame_visible
->data
;
309 if (frame
->menu
== self
)
313 menu_frame_hide_all();
315 frame
= menu_frame_new(self
, client
);
316 if (!menu_frame_show_topmenu(frame
, x
, y
))
317 menu_frame_free(frame
);
318 else if (frame
->entries
) {
319 ObMenuEntryFrame
*e
= frame
->entries
->data
;
320 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
321 e
->entry
->data
.normal
.enabled
)
322 menu_frame_select(frame
, e
);
326 static ObMenuEntry
* menu_entry_new(ObMenu
*menu
, ObMenuEntryType type
, gint id
)
332 self
= g_new0(ObMenuEntry
, 1);
338 case OB_MENU_ENTRY_TYPE_NORMAL
:
339 self
->data
.normal
.enabled
= TRUE
;
341 case OB_MENU_ENTRY_TYPE_SUBMENU
:
342 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
349 void menu_entry_free(ObMenuEntry
*self
)
352 switch (self
->type
) {
353 case OB_MENU_ENTRY_TYPE_NORMAL
:
354 g_free(self
->data
.normal
.label
);
355 while (self
->data
.normal
.actions
) {
356 action_unref(self
->data
.normal
.actions
->data
);
357 self
->data
.normal
.actions
=
358 g_slist_delete_link(self
->data
.normal
.actions
,
359 self
->data
.normal
.actions
);
362 case OB_MENU_ENTRY_TYPE_SUBMENU
:
363 g_free(self
->data
.submenu
.name
);
365 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
373 void menu_clear_entries(ObMenu
*self
)
376 /* assert that the menu isn't visible */
381 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
383 g_assert(f
->menu
!= self
);
388 while (self
->entries
) {
389 menu_entry_free(self
->entries
->data
);
390 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
394 void menu_entry_remove(ObMenuEntry
*self
)
396 self
->menu
->entries
= g_list_remove(self
->menu
->entries
, self
);
397 menu_entry_free(self
);
400 ObMenuEntry
* menu_add_normal(ObMenu
*self
, gint id
, const gchar
*label
,
405 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_NORMAL
, id
);
406 e
->data
.normal
.label
= g_strdup(label
);
407 e
->data
.normal
.actions
= actions
;
409 self
->entries
= g_list_append(self
->entries
, e
);
413 ObMenuEntry
* menu_add_submenu(ObMenu
*self
, gint id
, const gchar
*submenu
)
417 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, id
);
418 e
->data
.submenu
.name
= g_strdup(submenu
);
420 self
->entries
= g_list_append(self
->entries
, e
);
424 ObMenuEntry
* menu_add_separator(ObMenu
*self
, gint id
, const gchar
*label
)
428 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SEPARATOR
, id
);
429 e
->data
.separator
.label
= g_strdup(label
);
431 self
->entries
= g_list_append(self
->entries
, e
);
435 void menu_set_update_func(ObMenu
*self
, ObMenuUpdateFunc func
)
437 self
->update_func
= func
;
440 void menu_set_execute_func(ObMenu
*self
, ObMenuExecuteFunc func
)
442 self
->execute_func
= func
;
445 void menu_set_destroy_func(ObMenu
*self
, ObMenuDestroyFunc func
)
447 self
->destroy_func
= func
;
450 ObMenuEntry
* menu_find_entry_id(ObMenu
*self
, gint id
)
452 ObMenuEntry
*ret
= NULL
;
455 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
456 ObMenuEntry
*e
= it
->data
;
466 void menu_find_submenus(ObMenu
*self
)
470 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
471 ObMenuEntry
*e
= it
->data
;
473 if (e
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
474 e
->data
.submenu
.submenu
= menu_from_name(e
->data
.submenu
.name
);