]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
remove comment that isnt right anymore
[chaz/openbox] / openbox / menu.c
1 #include "debug.h"
2 #include "menu.h"
3 #include "openbox.h"
4 #include "stacking.h"
5 #include "client.h"
6 #include "grab.h"
7 #include "config.h"
8 #include "screen.h"
9 #include "menuframe.h"
10 #include "geom.h"
11 #include "misc.h"
12 #include "client_menu.h"
13 #include "client_list_menu.h"
14 #include "parser/parse.h"
15
16 typedef struct _ObMenuParseState ObMenuParseState;
17
18 struct _ObMenuParseState
19 {
20 ObMenu *parent;
21 ObMenu *pipe_creator;
22 };
23
24 static GHashTable *menu_hash = NULL;
25 static ObParseInst *menu_parse_inst;
26 static ObMenuParseState menu_parse_state;
27
28 static void menu_destroy_hash_value(ObMenu *self);
29 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
30 gpointer data);
31 static void parse_menu_separator(ObParseInst *i,
32 xmlDocPtr doc, xmlNodePtr node,
33 gpointer data);
34 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
35 gpointer data);
36
37 static gboolean menu_open(gchar *file, xmlDocPtr *doc, xmlNodePtr *node)
38 {
39 gboolean loaded = TRUE;
40 gchar *p;
41
42 if (file[0] == '/') {
43 if (!parse_load(file, "openbox_menu", doc, node)) {
44 g_warning("Failed to load menu from '%s'", file);
45 loaded = FALSE;
46 }
47 } else {
48 p = g_build_filename(g_get_home_dir(), ".openbox", file, NULL);
49 if (!parse_load(p, "openbox_menu", doc, node)) {
50 g_free(p);
51 p = g_build_filename(RCDIR, file, NULL);
52 if (!parse_load(p, "openbox_menu", doc, node)) {
53 g_free(p);
54 p = g_strdup(file);
55 if (!parse_load(p, "openbox_menu", doc, node)) {
56 g_warning("Failed to load menu from '%s'", file);
57 loaded = FALSE;
58 }
59 }
60 }
61 g_free(p);
62 }
63 return loaded;
64 }
65
66 static void client_dest(gpointer client)
67 {
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);
71 }
72
73 void menu_startup(gboolean reconfig)
74 {
75 xmlDocPtr doc;
76 xmlNodePtr node;
77 gboolean loaded = FALSE;
78 GSList *it;
79
80 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
81 (GDestroyNotify)menu_destroy_hash_value);
82
83 client_list_menu_startup();
84 client_menu_startup();
85
86 menu_parse_inst = parse_startup();
87
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,
92 &menu_parse_state);
93 parse_register(menu_parse_inst, "separator",
94 parse_menu_separator, &menu_parse_state);
95
96 for (it = config_menu_files; it; it = g_slist_next(it)) {
97 if (menu_open(it->data, &doc, &node)) {
98 loaded = TRUE;
99 parse_tree(menu_parse_inst, doc, node->children);
100 xmlFreeDoc(doc);
101 }
102 }
103 if (!loaded) {
104 if (menu_open("menu.xml", &doc, &node)) {
105 parse_tree(menu_parse_inst, doc, node->children);
106 xmlFreeDoc(doc);
107 }
108 }
109
110 g_assert(menu_parse_state.parent == NULL);
111
112 if (!reconfig)
113 client_add_destructor(client_dest);
114 }
115
116 void menu_shutdown(gboolean reconfig)
117 {
118 if (!reconfig)
119 client_remove_destructor(client_dest);
120
121 parse_shutdown(menu_parse_inst);
122 menu_parse_inst = NULL;
123
124 menu_frame_hide_all();
125 g_hash_table_destroy(menu_hash);
126 menu_hash = NULL;
127 }
128
129 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
130 {
131 ObMenu *menu = val;
132 return menu->pipe_creator == data;
133 }
134
135 void menu_pipe_execute(ObMenu *self)
136 {
137 xmlDocPtr doc;
138 xmlNodePtr node;
139 gchar *output;
140 GError *err = NULL;
141
142 if (!self->execute)
143 return;
144
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);
147 g_error_free(err);
148 return;
149 }
150
151 if (parse_load_mem(output, strlen(output),
152 "openbox_pipe_menu", &doc, &node))
153 {
154 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
155 menu_clear_entries(self);
156
157 menu_parse_state.pipe_creator = self;
158 menu_parse_state.parent = self;
159 parse_tree(menu_parse_inst, doc, node->children);
160 xmlFreeDoc(doc);
161 } else {
162 g_warning("Invalid output from pipe-menu: %s", self->execute);
163 }
164 }
165
166 static ObMenu* menu_from_name(gchar *name)
167 {
168 ObMenu *self = NULL;
169
170 g_assert(name != NULL);
171
172 if (!(self = g_hash_table_lookup(menu_hash, name)))
173 g_warning("Attempted to access menu '%s' but it does not exist.",
174 name);
175 return self;
176 }
177
178 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
179 gpointer data)
180 {
181 ObMenuParseState *state = data;
182 gchar *label;
183
184 if (state->parent) {
185 if (parse_attr_string("label", node, &label)) {
186 GSList *acts = NULL;
187
188 for (node = node->children; node; node = node->next)
189 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action"))
190 acts = g_slist_append(acts, action_parse
191 (i, doc, node,
192 OB_USER_ACTION_MENU_SELECTION));
193 menu_add_normal(state->parent, -1, label, acts);
194 g_free(label);
195 }
196 }
197 }
198
199 static void parse_menu_separator(ObParseInst *i,
200 xmlDocPtr doc, xmlNodePtr node,
201 gpointer data)
202 {
203 ObMenuParseState *state = data;
204
205 if (state->parent)
206 menu_add_separator(state->parent, -1);
207 }
208
209 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
210 gpointer data)
211 {
212 ObMenuParseState *state = data;
213 gchar *name = NULL, *title = NULL, *script = NULL;
214 ObMenu *menu;
215
216 if (!parse_attr_string("id", node, &name))
217 goto parse_menu_fail;
218
219 if (!g_hash_table_lookup(menu_hash, name)) {
220 if (!parse_attr_string("label", node, &title))
221 goto parse_menu_fail;
222
223 if ((menu = menu_new(name, title, NULL))) {
224 menu->pipe_creator = state->pipe_creator;
225 if (parse_attr_string("execute", node, &script)) {
226 menu->execute = ob_expand_tilde(script);
227 } else {
228 ObMenu *old;
229
230 old = state->parent;
231 state->parent = menu;
232 parse_tree(i, doc, node->children);
233 state->parent = old;
234 }
235 }
236 }
237
238 if (state->parent)
239 menu_add_submenu(state->parent, -1, name);
240
241 parse_menu_fail:
242 g_free(name);
243 g_free(title);
244 g_free(script);
245 }
246
247 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
248 {
249 ObMenu *self;
250
251 self = g_new0(ObMenu, 1);
252 self->name = g_strdup(name);
253 self->title = g_strdup(title);
254 self->data = data;
255
256 g_hash_table_replace(menu_hash, self->name, self);
257
258 return self;
259 }
260
261 static void menu_destroy_hash_value(ObMenu *self)
262 {
263 /* XXX make sure its not visible */
264
265 if (self->destroy_func)
266 self->destroy_func(self, self->data);
267
268 menu_clear_entries(self);
269 g_free(self->name);
270 g_free(self->title);
271 g_free(self->execute);
272 }
273
274 void menu_free(ObMenu *menu)
275 {
276 g_hash_table_remove(menu_hash, menu->name);
277 }
278
279 void menu_show(gchar *name, gint x, gint y, ObClient *client)
280 {
281 ObMenu *self;
282 ObMenuFrame *frame;
283
284 if (!(self = menu_from_name(name))) return;
285
286 frame = menu_frame_new(self, client);
287 menu_frame_move(frame, x, y);
288 menu_frame_show(frame, NULL);
289 if (frame->entries)
290 menu_frame_select_next(frame);
291 }
292
293 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
294 {
295 ObMenuEntry *self;
296
297 g_assert(menu);
298
299 self = g_new0(ObMenuEntry, 1);
300 self->type = type;
301 self->menu = menu;
302 self->id = id;
303
304 switch (type) {
305 case OB_MENU_ENTRY_TYPE_NORMAL:
306 self->data.normal.enabled = TRUE;
307 break;
308 case OB_MENU_ENTRY_TYPE_SUBMENU:
309 case OB_MENU_ENTRY_TYPE_SEPARATOR:
310 break;
311 }
312
313 return self;
314 }
315
316 void menu_entry_free(ObMenuEntry *self)
317 {
318 if (self) {
319 switch (self->type) {
320 case OB_MENU_ENTRY_TYPE_NORMAL:
321 g_free(self->data.normal.label);
322 while (self->data.normal.actions) {
323 action_free(self->data.normal.actions->data);
324 self->data.normal.actions =
325 g_slist_delete_link(self->data.normal.actions,
326 self->data.normal.actions);
327 }
328 break;
329 case OB_MENU_ENTRY_TYPE_SUBMENU:
330 g_free(self->data.submenu.name);
331 break;
332 case OB_MENU_ENTRY_TYPE_SEPARATOR:
333 break;
334 }
335
336 g_free(self);
337 }
338 }
339
340 void menu_clear_entries(ObMenu *self)
341 {
342 /* XXX assert that the menu isn't visible */
343
344 while (self->entries) {
345 menu_entry_free(self->entries->data);
346 self->entries = g_list_delete_link(self->entries, self->entries);
347 }
348 }
349
350 void menu_entry_remove(ObMenuEntry *self)
351 {
352 self->menu->entries = g_list_remove(self->menu->entries, self);
353 menu_entry_free(self);
354 }
355
356 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
357 GSList *actions)
358 {
359 ObMenuEntry *e;
360
361 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
362 e->data.normal.label = g_strdup(label);
363 e->data.normal.actions = actions;
364
365 self->entries = g_list_append(self->entries, e);
366 return e;
367 }
368
369 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
370 {
371 ObMenuEntry *e;
372
373 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
374 e->data.submenu.name = g_strdup(submenu);
375
376 self->entries = g_list_append(self->entries, e);
377 return e;
378 }
379
380 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
381 {
382 ObMenuEntry *e;
383
384 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
385
386 self->entries = g_list_append(self->entries, e);
387 return e;
388 }
389
390 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
391 {
392 self->update_func = func;
393 }
394
395 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
396 {
397 self->execute_func = func;
398 }
399
400 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
401 {
402 self->destroy_func = func;
403 }
404
405 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
406 {
407 ObMenuEntry *ret = NULL;
408 GList *it;
409
410 for (it = self->entries; it; it = g_list_next(it)) {
411 ObMenuEntry *e = it->data;
412
413 if (e->id == id) {
414 ret = e;
415 break;
416 }
417 }
418 return ret;
419 }
420
421 void menu_find_submenus(ObMenu *self)
422 {
423 GList *it;
424
425 for (it = self->entries; it; it = g_list_next(it)) {
426 ObMenuEntry *e = it->data;
427
428 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
429 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
430 }
431 }
This page took 0.050869 seconds and 4 git commands to generate.