]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
1) translate all of openbox's output
[chaz/openbox] / openbox / menu.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 menu.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
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.
11
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.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "debug.h"
21 #include "menu.h"
22 #include "openbox.h"
23 #include "stacking.h"
24 #include "client.h"
25 #include "config.h"
26 #include "screen.h"
27 #include "menuframe.h"
28 #include "keyboard.h"
29 #include "geom.h"
30 #include "misc.h"
31 #include "client_menu.h"
32 #include "client_list_menu.h"
33 #include "client_list_combined_menu.h"
34 #include "gettext.h"
35 #include "parser/parse.h"
36
37 typedef struct _ObMenuParseState ObMenuParseState;
38
39 struct _ObMenuParseState
40 {
41 ObMenu *parent;
42 ObMenu *pipe_creator;
43 };
44
45 static GHashTable *menu_hash = NULL;
46 static ObParseInst *menu_parse_inst;
47 static ObMenuParseState menu_parse_state;
48
49 static void menu_destroy_hash_value(ObMenu *self);
50 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
51 gpointer data);
52 static void parse_menu_separator(ObParseInst *i,
53 xmlDocPtr doc, xmlNodePtr node,
54 gpointer data);
55 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
56 gpointer data);
57
58 static void client_dest(ObClient *client, gpointer data)
59 {
60 /* menus can be associated with a client, so close any that are since
61 we are disappearing now */
62 menu_frame_hide_all_client(client);
63 }
64
65 void menu_startup(gboolean reconfig)
66 {
67 xmlDocPtr doc;
68 xmlNodePtr node;
69 gboolean loaded = FALSE;
70 GSList *it;
71
72 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
73 (GDestroyNotify)menu_destroy_hash_value);
74
75 client_list_menu_startup(reconfig);
76 client_list_combined_menu_startup(reconfig);
77 client_menu_startup();
78
79 menu_parse_inst = parse_startup();
80
81 menu_parse_state.parent = NULL;
82 menu_parse_state.pipe_creator = NULL;
83 parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
84 parse_register(menu_parse_inst, "item", parse_menu_item,
85 &menu_parse_state);
86 parse_register(menu_parse_inst, "separator",
87 parse_menu_separator, &menu_parse_state);
88
89 for (it = config_menu_files; it; it = g_slist_next(it)) {
90 if (parse_load_menu(it->data, &doc, &node)) {
91 loaded = TRUE;
92 parse_tree(menu_parse_inst, doc, node->children);
93 xmlFreeDoc(doc);
94 } else
95 g_message(_("Unable to find a valid menu file '%s'"),
96 (const gchar*)it->data);
97 }
98 if (!loaded) {
99 if (parse_load_menu("menu.xml", &doc, &node)) {
100 parse_tree(menu_parse_inst, doc, node->children);
101 xmlFreeDoc(doc);
102 } else
103 g_message(_("Unable to find a valid menu file '%s'"),
104 "menu.xml");
105 }
106
107 g_assert(menu_parse_state.parent == NULL);
108
109 if (!reconfig)
110 client_add_destructor(client_dest, NULL);
111 }
112
113 void menu_shutdown(gboolean reconfig)
114 {
115 if (!reconfig)
116 client_remove_destructor(client_dest);
117
118 parse_shutdown(menu_parse_inst);
119 menu_parse_inst = NULL;
120
121 client_list_menu_shutdown(reconfig);
122 client_list_combined_menu_shutdown(reconfig);
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_message(_("Failed to execute command for pipe-menu '%s': %s"),
147 self->execute, err->message);
148 g_error_free(err);
149 return;
150 }
151
152 if (parse_load_mem(output, strlen(output),
153 "openbox_pipe_menu", &doc, &node))
154 {
155 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
156 menu_clear_entries(self);
157
158 menu_parse_state.pipe_creator = self;
159 menu_parse_state.parent = self;
160 parse_tree(menu_parse_inst, doc, node->children);
161 xmlFreeDoc(doc);
162 } else {
163 g_message(_("Invalid output from pipe-menu '%s'"), self->execute);
164 }
165
166 g_free(output);
167 }
168
169 static ObMenu* menu_from_name(gchar *name)
170 {
171 ObMenu *self = NULL;
172
173 g_assert(name != NULL);
174
175 if (!(self = g_hash_table_lookup(menu_hash, name)))
176 g_message(_("Attempted to access menu '%s' but it does not exist"),
177 name);
178 return self;
179 }
180
181 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
182 gpointer data)
183 {
184 ObMenuParseState *state = data;
185 gchar *label;
186
187 if (state->parent) {
188 if (parse_attr_string("label", node, &label)) {
189 GSList *acts = NULL;
190
191 for (node = node->children; node; node = node->next)
192 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
193 ObAction *a = action_parse
194 (i, doc, node, OB_USER_ACTION_MENU_SELECTION);
195 if (a)
196 acts = g_slist_append(acts, a);
197 }
198 menu_add_normal(state->parent, -1, label, acts);
199 g_free(label);
200 }
201 }
202 }
203
204 static void parse_menu_separator(ObParseInst *i,
205 xmlDocPtr doc, xmlNodePtr node,
206 gpointer data)
207 {
208 ObMenuParseState *state = data;
209
210 if (state->parent) {
211 gchar *label;
212
213 if (!parse_attr_string("label", node, &label))
214 label = NULL;
215
216 menu_add_separator(state->parent, -1, label);
217 g_free(label);
218 }
219 }
220
221 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
222 gpointer data)
223 {
224 ObMenuParseState *state = data;
225 gchar *name = NULL, *title = NULL, *script = NULL;
226 ObMenu *menu;
227
228 if (!parse_attr_string("id", node, &name))
229 goto parse_menu_fail;
230
231 if (!g_hash_table_lookup(menu_hash, name)) {
232 if (!parse_attr_string("label", node, &title))
233 goto parse_menu_fail;
234
235 if ((menu = menu_new(name, title, NULL))) {
236 menu->pipe_creator = state->pipe_creator;
237 if (parse_attr_string("execute", node, &script)) {
238 menu->execute = parse_expand_tilde(script);
239 } else {
240 ObMenu *old;
241
242 old = state->parent;
243 state->parent = menu;
244 parse_tree(i, doc, node->children);
245 state->parent = old;
246 }
247 }
248 }
249
250 if (state->parent)
251 menu_add_submenu(state->parent, -1, name);
252
253 parse_menu_fail:
254 g_free(name);
255 g_free(title);
256 g_free(script);
257 }
258
259 ObMenu* menu_new(const gchar *name, const gchar *title, gpointer data)
260 {
261 ObMenu *self;
262
263 self = g_new0(ObMenu, 1);
264 self->name = g_strdup(name);
265 self->title = g_strdup(title);
266 self->data = data;
267
268 g_hash_table_replace(menu_hash, self->name, self);
269
270 return self;
271 }
272
273 static void menu_destroy_hash_value(ObMenu *self)
274 {
275 /* make sure its not visible */
276 {
277 GList *it;
278 ObMenuFrame *f;
279
280 for (it = menu_frame_visible; it; it = g_list_next(it)) {
281 f = it->data;
282 if (f->menu == self)
283 menu_frame_hide_all();
284 }
285 }
286
287 if (self->destroy_func)
288 self->destroy_func(self, self->data);
289
290 menu_clear_entries(self);
291 g_free(self->name);
292 g_free(self->title);
293 g_free(self->execute);
294
295 g_free(self);
296 }
297
298 void menu_free(ObMenu *menu)
299 {
300 g_hash_table_remove(menu_hash, menu->name);
301 }
302
303 void menu_show(gchar *name, gint x, gint y, ObClient *client)
304 {
305 ObMenu *self;
306 ObMenuFrame *frame;
307
308 if (!(self = menu_from_name(name))
309 || keyboard_interactively_grabbed()) return;
310
311 /* if the requested menu is already the top visible menu, then don't
312 bother */
313 if (menu_frame_visible) {
314 frame = menu_frame_visible->data;
315 if (frame->menu == self)
316 return;
317 }
318
319 menu_frame_hide_all();
320
321 frame = menu_frame_new(self, client);
322 if (!menu_frame_show_topmenu(frame, x, y))
323 menu_frame_free(frame);
324 else if (frame->entries) {
325 ObMenuEntryFrame *e = frame->entries->data;
326 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
327 e->entry->data.normal.enabled)
328 menu_frame_select(frame, e);
329 }
330 }
331
332 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
333 {
334 ObMenuEntry *self;
335
336 g_assert(menu);
337
338 self = g_new0(ObMenuEntry, 1);
339 self->type = type;
340 self->menu = menu;
341 self->id = id;
342
343 switch (type) {
344 case OB_MENU_ENTRY_TYPE_NORMAL:
345 self->data.normal.enabled = TRUE;
346 break;
347 case OB_MENU_ENTRY_TYPE_SUBMENU:
348 case OB_MENU_ENTRY_TYPE_SEPARATOR:
349 break;
350 }
351
352 return self;
353 }
354
355 void menu_entry_free(ObMenuEntry *self)
356 {
357 if (self) {
358 switch (self->type) {
359 case OB_MENU_ENTRY_TYPE_NORMAL:
360 g_free(self->data.normal.label);
361 while (self->data.normal.actions) {
362 action_unref(self->data.normal.actions->data);
363 self->data.normal.actions =
364 g_slist_delete_link(self->data.normal.actions,
365 self->data.normal.actions);
366 }
367 break;
368 case OB_MENU_ENTRY_TYPE_SUBMENU:
369 g_free(self->data.submenu.name);
370 break;
371 case OB_MENU_ENTRY_TYPE_SEPARATOR:
372 break;
373 }
374
375 g_free(self);
376 }
377 }
378
379 void menu_clear_entries(ObMenu *self)
380 {
381 #ifdef DEBUG
382 /* assert that the menu isn't visible */
383 {
384 GList *it;
385 ObMenuFrame *f;
386
387 for (it = menu_frame_visible; it; it = g_list_next(it)) {
388 f = it->data;
389 g_assert(f->menu != self);
390 }
391 }
392 #endif
393
394 while (self->entries) {
395 menu_entry_free(self->entries->data);
396 self->entries = g_list_delete_link(self->entries, self->entries);
397 }
398 }
399
400 void menu_entry_remove(ObMenuEntry *self)
401 {
402 self->menu->entries = g_list_remove(self->menu->entries, self);
403 menu_entry_free(self);
404 }
405
406 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
407 GSList *actions)
408 {
409 ObMenuEntry *e;
410
411 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
412 e->data.normal.label = g_strdup(label);
413 e->data.normal.actions = actions;
414
415 self->entries = g_list_append(self->entries, e);
416 return e;
417 }
418
419 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
420 {
421 ObMenuEntry *e;
422
423 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
424 e->data.submenu.name = g_strdup(submenu);
425
426 self->entries = g_list_append(self->entries, e);
427 return e;
428 }
429
430 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
431 {
432 ObMenuEntry *e;
433
434 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
435 e->data.separator.label = g_strdup(label);
436
437 self->entries = g_list_append(self->entries, e);
438 return e;
439 }
440
441 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
442 {
443 self->update_func = func;
444 }
445
446 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
447 {
448 self->execute_func = func;
449 }
450
451 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
452 {
453 self->destroy_func = func;
454 }
455
456 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
457 {
458 ObMenuEntry *ret = NULL;
459 GList *it;
460
461 for (it = self->entries; it; it = g_list_next(it)) {
462 ObMenuEntry *e = it->data;
463
464 if (e->id == id) {
465 ret = e;
466 break;
467 }
468 }
469 return ret;
470 }
471
472 void menu_find_submenus(ObMenu *self)
473 {
474 GList *it;
475
476 for (it = self->entries; it; it = g_list_next(it)) {
477 ObMenuEntry *e = it->data;
478
479 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
480 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
481 }
482 }
This page took 0.062362 seconds and 4 git commands to generate.