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 gboolean
menu_open(gchar
*file
, xmlDocPtr
*doc
, xmlNodePtr
*node
)
39 gboolean loaded
= TRUE
;
43 if (!parse_load(file
, "openbox_menu", doc
, node
)) {
44 g_warning("Failed to load menu from '%s'", file
);
48 p
= g_build_filename(g_get_home_dir(), ".openbox", file
, NULL
);
49 if (!parse_load(p
, "openbox_menu", doc
, node
)) {
51 p
= g_build_filename(RCDIR
, file
, NULL
);
52 if (!parse_load(p
, "openbox_menu", doc
, node
)) {
55 if (!parse_load(p
, "openbox_menu", doc
, node
)) {
56 g_warning("Failed to load menu from '%s'", file
);
66 static void client_dest(gpointer client
)
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();
84 client_menu_startup();
86 menu_parse_inst
= parse_startup();
88 menu_parse_state
.parent
= NULL
;
89 menu_parse_state
.pipe_creator
= NULL
;
90 parse_register(menu_parse_inst
, "menu", parse_menu
, &menu_parse_state
);
91 parse_register(menu_parse_inst
, "item", parse_menu_item
,
93 parse_register(menu_parse_inst
, "separator",
94 parse_menu_separator
, &menu_parse_state
);
96 for (it
= config_menu_files
; it
; it
= g_slist_next(it
)) {
97 if (menu_open(it
->data
, &doc
, &node
)) {
99 parse_tree(menu_parse_inst
, doc
, node
->children
);
104 if (menu_open("menu.xml", &doc
, &node
)) {
105 parse_tree(menu_parse_inst
, doc
, node
->children
);
110 g_assert(menu_parse_state
.parent
== NULL
);
113 client_add_destructor(client_dest
);
116 void menu_shutdown(gboolean reconfig
)
119 client_remove_destructor(client_dest
);
121 parse_shutdown(menu_parse_inst
);
122 menu_parse_inst
= NULL
;
124 menu_frame_hide_all();
125 g_hash_table_destroy(menu_hash
);
129 static gboolean
menu_pipe_submenu(gpointer key
, gpointer val
, gpointer data
)
132 return menu
->pipe_creator
== data
;
135 void menu_pipe_execute(ObMenu
*self
)
145 if (!g_spawn_command_line_sync(self
->execute
, &output
, NULL
, NULL
, &err
)) {
146 g_warning("Failed to execute command for pipe-menu: %s", err
->message
);
151 if (parse_load_mem(output
, strlen(output
),
152 "openbox_pipe_menu", &doc
, &node
))
154 g_hash_table_foreach_remove(menu_hash
, menu_pipe_submenu
, self
);
155 menu_clear_entries(self
);
157 menu_parse_state
.pipe_creator
= self
;
158 menu_parse_state
.parent
= self
;
159 parse_tree(menu_parse_inst
, doc
, node
->children
);
162 g_warning("Invalid output from pipe-menu: %s", self
->execute
);
168 static ObMenu
* menu_from_name(gchar
*name
)
172 g_assert(name
!= NULL
);
174 if (!(self
= g_hash_table_lookup(menu_hash
, name
)))
175 g_warning("Attempted to access menu '%s' but it does not exist.",
180 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
183 ObMenuParseState
*state
= data
;
187 if (parse_attr_string("label", node
, &label
)) {
190 for (node
= node
->children
; node
; node
= node
->next
)
191 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "action"))
192 acts
= g_slist_append(acts
, action_parse
194 OB_USER_ACTION_MENU_SELECTION
));
195 menu_add_normal(state
->parent
, -1, label
, acts
);
201 static void parse_menu_separator(ObParseInst
*i
,
202 xmlDocPtr doc
, xmlNodePtr node
,
205 ObMenuParseState
*state
= data
;
208 menu_add_separator(state
->parent
, -1);
211 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
214 ObMenuParseState
*state
= data
;
215 gchar
*name
= NULL
, *title
= NULL
, *script
= NULL
;
218 if (!parse_attr_string("id", node
, &name
))
219 goto parse_menu_fail
;
221 if (!g_hash_table_lookup(menu_hash
, name
)) {
222 if (!parse_attr_string("label", node
, &title
))
223 goto parse_menu_fail
;
225 if ((menu
= menu_new(name
, title
, NULL
))) {
226 menu
->pipe_creator
= state
->pipe_creator
;
227 if (parse_attr_string("execute", node
, &script
)) {
228 menu
->execute
= ob_expand_tilde(script
);
233 state
->parent
= menu
;
234 parse_tree(i
, doc
, node
->children
);
241 menu_add_submenu(state
->parent
, -1, name
);
249 ObMenu
* menu_new(gchar
*name
, gchar
*title
, gpointer data
)
253 self
= g_new0(ObMenu
, 1);
254 self
->name
= g_strdup(name
);
255 self
->title
= g_strdup(title
);
258 g_hash_table_replace(menu_hash
, self
->name
, self
);
263 static void menu_destroy_hash_value(ObMenu
*self
)
265 /* make sure its not visible */
270 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
273 menu_frame_hide_all();
277 if (self
->destroy_func
)
278 self
->destroy_func(self
, self
->data
);
280 menu_clear_entries(self
);
283 g_free(self
->execute
);
288 void menu_free(ObMenu
*menu
)
290 g_hash_table_remove(menu_hash
, menu
->name
);
293 void menu_show(gchar
*name
, gint x
, gint y
, ObClient
*client
)
298 if (!(self
= menu_from_name(name
))) return;
300 menu_frame_hide_all();
302 frame
= menu_frame_new(self
, client
);
303 menu_frame_move(frame
, x
, y
);
304 menu_frame_show(frame
, NULL
);
306 menu_frame_select_next(frame
);
309 static ObMenuEntry
* menu_entry_new(ObMenu
*menu
, ObMenuEntryType type
, gint id
)
315 self
= g_new0(ObMenuEntry
, 1);
321 case OB_MENU_ENTRY_TYPE_NORMAL
:
322 self
->data
.normal
.enabled
= TRUE
;
324 case OB_MENU_ENTRY_TYPE_SUBMENU
:
325 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
332 void menu_entry_free(ObMenuEntry
*self
)
335 switch (self
->type
) {
336 case OB_MENU_ENTRY_TYPE_NORMAL
:
337 g_free(self
->data
.normal
.label
);
338 while (self
->data
.normal
.actions
) {
339 action_free(self
->data
.normal
.actions
->data
);
340 self
->data
.normal
.actions
=
341 g_slist_delete_link(self
->data
.normal
.actions
,
342 self
->data
.normal
.actions
);
345 case OB_MENU_ENTRY_TYPE_SUBMENU
:
346 g_free(self
->data
.submenu
.name
);
348 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
356 void menu_clear_entries(ObMenu
*self
)
359 /* assert that the menu isn't visible */
364 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
366 g_assert(f
->menu
!= self
);
371 while (self
->entries
) {
372 menu_entry_free(self
->entries
->data
);
373 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
377 void menu_entry_remove(ObMenuEntry
*self
)
379 self
->menu
->entries
= g_list_remove(self
->menu
->entries
, self
);
380 menu_entry_free(self
);
383 ObMenuEntry
* menu_add_normal(ObMenu
*self
, gint id
, gchar
*label
,
388 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_NORMAL
, id
);
389 e
->data
.normal
.label
= g_strdup(label
);
390 e
->data
.normal
.actions
= actions
;
392 self
->entries
= g_list_append(self
->entries
, e
);
396 ObMenuEntry
* menu_add_submenu(ObMenu
*self
, gint id
, gchar
*submenu
)
400 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, id
);
401 e
->data
.submenu
.name
= g_strdup(submenu
);
403 self
->entries
= g_list_append(self
->entries
, e
);
407 ObMenuEntry
* menu_add_separator(ObMenu
*self
, gint id
)
411 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SEPARATOR
, id
);
413 self
->entries
= g_list_append(self
->entries
, e
);
417 void menu_set_update_func(ObMenu
*self
, ObMenuUpdateFunc func
)
419 self
->update_func
= func
;
422 void menu_set_execute_func(ObMenu
*self
, ObMenuExecuteFunc func
)
424 self
->execute_func
= func
;
427 void menu_set_destroy_func(ObMenu
*self
, ObMenuDestroyFunc func
)
429 self
->destroy_func
= func
;
432 ObMenuEntry
* menu_find_entry_id(ObMenu
*self
, gint id
)
434 ObMenuEntry
*ret
= NULL
;
437 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
438 ObMenuEntry
*e
= it
->data
;
448 void menu_find_submenus(ObMenu
*self
)
452 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
453 ObMenuEntry
*e
= it
->data
;
455 if (e
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
456 e
->data
.submenu
.submenu
= menu_from_name(e
->data
.submenu
.name
);