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 Ben 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 "parser/parse.h"
35 typedef struct _ObMenuParseState ObMenuParseState
;
37 struct _ObMenuParseState
43 static GHashTable
*menu_hash
= NULL
;
44 static ObParseInst
*menu_parse_inst
;
45 static ObMenuParseState menu_parse_state
;
47 static void menu_destroy_hash_value(ObMenu
*self
);
48 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
50 static void parse_menu_separator(ObParseInst
*i
,
51 xmlDocPtr doc
, xmlNodePtr node
,
53 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
56 static void client_dest(ObClient
*client
, gpointer data
)
58 /* menus can be associated with a client, so close any that are since
59 we are disappearing now */
60 menu_frame_hide_all_client(client
);
63 void menu_startup(gboolean reconfig
)
67 gboolean loaded
= FALSE
;
70 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
71 (GDestroyNotify
)menu_destroy_hash_value
);
73 client_list_menu_startup(reconfig
);
74 client_menu_startup();
76 menu_parse_inst
= parse_startup();
78 menu_parse_state
.parent
= NULL
;
79 menu_parse_state
.pipe_creator
= NULL
;
80 parse_register(menu_parse_inst
, "menu", parse_menu
, &menu_parse_state
);
81 parse_register(menu_parse_inst
, "item", parse_menu_item
,
83 parse_register(menu_parse_inst
, "separator",
84 parse_menu_separator
, &menu_parse_state
);
86 for (it
= config_menu_files
; it
; it
= g_slist_next(it
)) {
87 if (parse_load_menu(it
->data
, &doc
, &node
)) {
89 parse_tree(menu_parse_inst
, doc
, node
->children
);
94 if (parse_load_menu("menu.xml", &doc
, &node
)) {
95 parse_tree(menu_parse_inst
, doc
, node
->children
);
100 g_assert(menu_parse_state
.parent
== NULL
);
103 client_add_destructor(client_dest
, NULL
);
106 void menu_shutdown(gboolean reconfig
)
109 client_remove_destructor(client_dest
);
111 parse_shutdown(menu_parse_inst
);
112 menu_parse_inst
= NULL
;
114 client_list_menu_shutdown(reconfig
);
116 menu_frame_hide_all();
117 g_hash_table_destroy(menu_hash
);
121 static gboolean
menu_pipe_submenu(gpointer key
, gpointer val
, gpointer data
)
124 return menu
->pipe_creator
== data
;
127 void menu_pipe_execute(ObMenu
*self
)
137 if (!g_spawn_command_line_sync(self
->execute
, &output
, NULL
, NULL
, &err
)) {
138 g_warning("Failed to execute command for pipe-menu: %s", err
->message
);
143 if (parse_load_mem(output
, strlen(output
),
144 "openbox_pipe_menu", &doc
, &node
))
146 g_hash_table_foreach_remove(menu_hash
, menu_pipe_submenu
, self
);
147 menu_clear_entries(self
);
149 menu_parse_state
.pipe_creator
= self
;
150 menu_parse_state
.parent
= self
;
151 parse_tree(menu_parse_inst
, doc
, node
->children
);
154 g_warning("Invalid output from pipe-menu: %s", self
->execute
);
160 static ObMenu
* menu_from_name(gchar
*name
)
164 g_assert(name
!= NULL
);
166 if (!(self
= g_hash_table_lookup(menu_hash
, name
)))
167 g_warning("Attempted to access menu '%s' but it does not exist.",
172 static void parse_menu_item(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
175 ObMenuParseState
*state
= data
;
179 if (parse_attr_string("label", node
, &label
)) {
182 for (node
= node
->children
; node
; node
= node
->next
)
183 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "action")) {
184 ObAction
*a
= action_parse
185 (i
, doc
, node
, OB_USER_ACTION_MENU_SELECTION
);
187 acts
= g_slist_append(acts
, a
);
189 menu_add_normal(state
->parent
, -1, label
, acts
);
195 static void parse_menu_separator(ObParseInst
*i
,
196 xmlDocPtr doc
, xmlNodePtr node
,
199 ObMenuParseState
*state
= data
;
202 menu_add_separator(state
->parent
, -1);
205 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
208 ObMenuParseState
*state
= data
;
209 gchar
*name
= NULL
, *title
= NULL
, *script
= NULL
;
212 if (!parse_attr_string("id", node
, &name
))
213 goto parse_menu_fail
;
215 if (!g_hash_table_lookup(menu_hash
, name
)) {
216 if (!parse_attr_string("label", node
, &title
))
217 goto parse_menu_fail
;
219 if ((menu
= menu_new(name
, title
, NULL
))) {
220 menu
->pipe_creator
= state
->pipe_creator
;
221 if (parse_attr_string("execute", node
, &script
)) {
222 menu
->execute
= parse_expand_tilde(script
);
227 state
->parent
= menu
;
228 parse_tree(i
, doc
, node
->children
);
235 menu_add_submenu(state
->parent
, -1, name
);
243 ObMenu
* menu_new(gchar
*name
, gchar
*title
, gpointer data
)
247 self
= g_new0(ObMenu
, 1);
248 self
->name
= g_strdup(name
);
249 self
->title
= g_strdup(title
);
252 g_hash_table_replace(menu_hash
, self
->name
, self
);
257 static void menu_destroy_hash_value(ObMenu
*self
)
259 /* make sure its not visible */
264 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
267 menu_frame_hide_all();
271 if (self
->destroy_func
)
272 self
->destroy_func(self
, self
->data
);
274 menu_clear_entries(self
);
277 g_free(self
->execute
);
282 void menu_free(ObMenu
*menu
)
284 g_hash_table_remove(menu_hash
, menu
->name
);
287 void menu_show(gchar
*name
, gint x
, gint y
, ObClient
*client
)
293 if (!(self
= menu_from_name(name
))
294 || keyboard_interactively_grabbed()) return;
296 /* if the requested menu is already the top visible menu, then don't
298 if (menu_frame_visible
) {
299 frame
= menu_frame_visible
->data
;
300 if (frame
->menu
== self
)
304 menu_frame_hide_all();
306 frame
= menu_frame_new(self
, client
);
307 if (client
&& x
< 0 && y
< 0) {
308 x
= client
->frame
->area
.x
+ client
->frame
->size
.left
;
309 y
= client
->frame
->area
.y
+ client
->frame
->size
.top
;
310 menu_frame_move(frame
, x
, y
);
312 menu_frame_move(frame
,
313 x
- ob_rr_theme
->bwidth
, y
- ob_rr_theme
->bwidth
);
314 for (i
= 0; i
< screen_num_monitors
; ++i
) {
315 Rect
*a
= screen_physical_area_monitor(i
);
316 if (RECT_CONTAINS(*a
, x
, y
)) {
321 if (!menu_frame_show(frame
, NULL
))
322 menu_frame_free(frame
);
323 else if (frame
->entries
) {
324 ObMenuEntryFrame
*e
= frame
->entries
->data
;
325 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
326 e
->entry
->data
.normal
.enabled
)
327 menu_frame_select(frame
, e
);
331 static ObMenuEntry
* menu_entry_new(ObMenu
*menu
, ObMenuEntryType type
, gint id
)
337 self
= g_new0(ObMenuEntry
, 1);
343 case OB_MENU_ENTRY_TYPE_NORMAL
:
344 self
->data
.normal
.enabled
= TRUE
;
346 case OB_MENU_ENTRY_TYPE_SUBMENU
:
347 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
354 void menu_entry_free(ObMenuEntry
*self
)
357 switch (self
->type
) {
358 case OB_MENU_ENTRY_TYPE_NORMAL
:
359 g_free(self
->data
.normal
.label
);
360 while (self
->data
.normal
.actions
) {
361 action_unref(self
->data
.normal
.actions
->data
);
362 self
->data
.normal
.actions
=
363 g_slist_delete_link(self
->data
.normal
.actions
,
364 self
->data
.normal
.actions
);
367 case OB_MENU_ENTRY_TYPE_SUBMENU
:
368 g_free(self
->data
.submenu
.name
);
370 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
378 void menu_clear_entries(ObMenu
*self
)
381 /* assert that the menu isn't visible */
386 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
388 g_assert(f
->menu
!= self
);
393 while (self
->entries
) {
394 menu_entry_free(self
->entries
->data
);
395 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
399 void menu_entry_remove(ObMenuEntry
*self
)
401 self
->menu
->entries
= g_list_remove(self
->menu
->entries
, self
);
402 menu_entry_free(self
);
405 ObMenuEntry
* menu_add_normal(ObMenu
*self
, gint id
, gchar
*label
,
410 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_NORMAL
, id
);
411 e
->data
.normal
.label
= g_strdup(label
);
412 e
->data
.normal
.actions
= actions
;
414 self
->entries
= g_list_append(self
->entries
, e
);
418 ObMenuEntry
* menu_add_submenu(ObMenu
*self
, gint id
, gchar
*submenu
)
422 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SUBMENU
, id
);
423 e
->data
.submenu
.name
= g_strdup(submenu
);
425 self
->entries
= g_list_append(self
->entries
, e
);
429 ObMenuEntry
* menu_add_separator(ObMenu
*self
, gint id
)
433 e
= menu_entry_new(self
, OB_MENU_ENTRY_TYPE_SEPARATOR
, id
);
435 self
->entries
= g_list_append(self
->entries
, e
);
439 void menu_set_update_func(ObMenu
*self
, ObMenuUpdateFunc func
)
441 self
->update_func
= func
;
444 void menu_set_execute_func(ObMenu
*self
, ObMenuExecuteFunc func
)
446 self
->execute_func
= func
;
449 void menu_set_destroy_func(ObMenu
*self
, ObMenuDestroyFunc func
)
451 self
->destroy_func
= func
;
454 ObMenuEntry
* menu_find_entry_id(ObMenu
*self
, gint id
)
456 ObMenuEntry
*ret
= NULL
;
459 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
460 ObMenuEntry
*e
= it
->data
;
470 void menu_find_submenus(ObMenu
*self
)
474 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
475 ObMenuEntry
*e
= it
->data
;
477 if (e
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
478 e
->data
.submenu
.submenu
= menu_from_name(e
->data
.submenu
.name
);