12 #include "client_menu.h"
13 #include "client_list_menu.h"
14 #include "parser/parse.h"
16 typedef struct _ObMenuParseState ObMenuParseState
;
18 struct _ObMenuParseState
24 static GHashTable
*menu_hash
= NULL
;
25 static ObParseInst
*menu_parse_inst
;
26 static ObMenuParseState menu_parse_state
;
28 static void menu_destroy_hash_value(ObMenu
*self
);
29 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
31 static void parse_menu_separator(ObParseInst
*i
,
32 xmlDocPtr doc
, xmlNodePtr node
,
34 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
37 static void client_dest(gpointer client
)
39 /* menus can be associated with a client, so close any that are since
40 we are disappearing now */
41 menu_frame_hide_all_client(client
);
44 void menu_startup(gboolean reconfig
)
48 gboolean loaded
= FALSE
;
51 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
52 (GDestroyNotify
)menu_destroy_hash_value
);
54 client_list_menu_startup();
55 client_menu_startup();
57 menu_parse_inst
= parse_startup();
59 menu_parse_state
.parent
= NULL
;
60 menu_parse_state
.pipe_creator
= NULL
;
61 parse_register(menu_parse_inst
, "menu", parse_menu
, &menu_parse_state
);
62 parse_register(menu_parse_inst
, "item", parse_menu_item
,
64 parse_register(menu_parse_inst
, "separator",
65 parse_menu_separator
, &menu_parse_state
);
67 for (it
= config_menu_files
; it
; it
= g_slist_next(it
)) {
68 if (parse_load_menu(it
->data
, &doc
, &node
)) {
70 parse_tree(menu_parse_inst
, doc
, node
->children
);
75 if (parse_load_menu("menu.xml", &doc
, &node
)) {
76 parse_tree(menu_parse_inst
, doc
, node
->children
);
81 g_assert(menu_parse_state
.parent
== NULL
);
84 client_add_destructor(client_dest
);
87 void menu_shutdown(gboolean reconfig
)
90 client_remove_destructor(client_dest
);
92 parse_shutdown(menu_parse_inst
);
93 menu_parse_inst
= NULL
;
95 menu_frame_hide_all();
96 g_hash_table_destroy(menu_hash
);
100 static gboolean
menu_pipe_submenu(gpointer key
, gpointer val
, gpointer data
)
103 return menu
->pipe_creator
== data
;
106 void menu_pipe_execute(ObMenu
*self
)
116 if (!g_spawn_command_line_sync(self
->execute
, &output
, NULL
, NULL
, &err
)) {
117 g_warning("Failed to execute command for pipe-menu: %s", err
->message
);
122 if (parse_load_mem(output
, strlen(output
),
123 "openbox_pipe_menu", &doc
, &node
))
125 g_hash_table_foreach_remove(menu_hash
, menu_pipe_submenu
, self
);
126 menu_clear_entries(self
);
128 menu_parse_state
.pipe_creator
= self
;
129 menu_parse_state
.parent
= self
;
130 parse_tree(menu_parse_inst
, doc
, node
->children
);
133 g_warning("Invalid output from pipe-menu: %s", self
->execute
);
139 static ObMenu
* menu_from_name(gchar
*name
)
143 g_assert(name
!= NULL
);
145 if (!(self
= g_hash_table_lookup(menu_hash
, name
)))
146 g_warning("Attempted to access menu '%s' but it does not exist.",
151 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
154 ObMenuParseState
*state
= data
;
158 if (parse_attr_string("label", node
, &label
)) {
161 for (node
= node
->children
; node
; node
= node
->next
)
162 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "action"))
163 acts
= g_slist_append(acts
, action_parse
165 OB_USER_ACTION_MENU_SELECTION
));
166 menu_add_normal(state
->parent
, -1, label
, acts
);
172 static void parse_menu_separator(ObParseInst
*i
,
173 xmlDocPtr doc
, xmlNodePtr node
,
176 ObMenuParseState
*state
= data
;
179 menu_add_separator(state
->parent
, -1);
182 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
185 ObMenuParseState
*state
= data
;
186 gchar
*name
= NULL
, *title
= NULL
, *script
= NULL
;
189 if (!parse_attr_string("id", node
, &name
))
190 goto parse_menu_fail
;
192 if (!g_hash_table_lookup(menu_hash
, name
)) {
193 if (!parse_attr_string("label", node
, &title
))
194 goto parse_menu_fail
;
196 if ((menu
= menu_new(name
, title
, NULL
))) {
197 menu
->pipe_creator
= state
->pipe_creator
;
198 if (parse_attr_string("execute", node
, &script
)) {
199 menu
->execute
= parse_expand_tilde(script
);
204 state
->parent
= menu
;
205 parse_tree(i
, doc
, node
->children
);
212 menu_add_submenu(state
->parent
, -1, name
);
220 ObMenu
* menu_new(gchar
*name
, gchar
*title
, gpointer data
)
224 self
= g_new0(ObMenu
, 1);
225 self
->name
= g_strdup(name
);
226 self
->title
= g_strdup(title
);
229 g_hash_table_replace(menu_hash
, self
->name
, self
);
234 static void menu_destroy_hash_value(ObMenu
*self
)
236 /* make sure its not visible */
241 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
244 menu_frame_hide_all();
248 if (self
->destroy_func
)
249 self
->destroy_func(self
, self
->data
);
251 menu_clear_entries(self
);
254 g_free(self
->execute
);
259 void menu_free(ObMenu
*menu
)
261 g_hash_table_remove(menu_hash
, menu
->name
);
264 void menu_show(gchar
*name
, gint x
, gint y
, ObClient
*client
)
269 if (!(self
= menu_from_name(name
))) return;
271 menu_frame_hide_all();
273 frame
= menu_frame_new(self
, client
);
274 menu_frame_move(frame
, x
, y
);
275 menu_frame_show(frame
, NULL
);
277 menu_frame_select_next(frame
);
280 static ObMenuEntry
* menu_entry_new(ObMenu
*menu
, ObMenuEntryType type
, gint id
)
286 self
= g_new0(ObMenuEntry
, 1);
292 case OB_MENU_ENTRY_TYPE_NORMAL
:
293 self
->data
.normal
.enabled
= TRUE
;
295 case OB_MENU_ENTRY_TYPE_SUBMENU
:
296 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
303 void menu_entry_free(ObMenuEntry
*self
)
306 switch (self
->type
) {
307 case OB_MENU_ENTRY_TYPE_NORMAL
:
308 g_free(self
->data
.normal
.label
);
309 while (self
->data
.normal
.actions
) {
310 action_free(self
->data
.normal
.actions
->data
);
311 self
->data
.normal
.actions
=
312 g_slist_delete_link(self
->data
.normal
.actions
,
313 self
->data
.normal
.actions
);
316 case OB_MENU_ENTRY_TYPE_SUBMENU
:
317 g_free(self
->data
.submenu
.name
);
319 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
327 void menu_clear_entries(ObMenu
*self
)
330 /* assert that the menu isn't visible */
335 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
337 g_assert(f
->menu
!= self
);
342 while (self
->entries
) {
343 menu_entry_free(self
->entries
->data
);
344 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
348 void menu_entry_remove(ObMenuEntry
*self
)
350 self
->menu
->entries
= g_list_remove(self
->menu
->entries
, self
);
351 menu_entry_free(self
);
354 ObMenuEntry
* menu_add_normal(ObMenu
*self
, gint id
, gchar
*label
,
359 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_NORMAL
, id
);
360 e
->data
.normal
.label
= g_strdup(label
);
361 e
->data
.normal
.actions
= actions
;
363 self
->entries
= g_list_append(self
->entries
, e
);
367 ObMenuEntry
* menu_add_submenu(ObMenu
*self
, gint id
, gchar
*submenu
)
371 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, id
);
372 e
->data
.submenu
.name
= g_strdup(submenu
);
374 self
->entries
= g_list_append(self
->entries
, e
);
378 ObMenuEntry
* menu_add_separator(ObMenu
*self
, gint id
)
382 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SEPARATOR
, id
);
384 self
->entries
= g_list_append(self
->entries
, e
);
388 void menu_set_update_func(ObMenu
*self
, ObMenuUpdateFunc func
)
390 self
->update_func
= func
;
393 void menu_set_execute_func(ObMenu
*self
, ObMenuExecuteFunc func
)
395 self
->execute_func
= func
;
398 void menu_set_destroy_func(ObMenu
*self
, ObMenuDestroyFunc func
)
400 self
->destroy_func
= func
;
403 ObMenuEntry
* menu_find_entry_id(ObMenu
*self
, gint id
)
405 ObMenuEntry
*ret
= NULL
;
408 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
409 ObMenuEntry
*e
= it
->data
;
419 void menu_find_submenus(ObMenu
*self
)
423 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
424 ObMenuEntry
*e
= it
->data
;
426 if (e
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
427 e
->data
.submenu
.submenu
= menu_from_name(e
->data
.submenu
.name
);