update copyright step 2
[mikachu/openbox.git] / 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        Ben 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 "parser/parse.h"
34
35 typedef struct _ObMenuParseState ObMenuParseState;
36
37 struct _ObMenuParseState
38 {
39     ObMenu *parent;
40     ObMenu *pipe_creator;
41 };
42
43 static GHashTable *menu_hash = NULL;
44 static ObParseInst *menu_parse_inst;
45 static ObMenuParseState menu_parse_state;
46
47 static void menu_destroy_hash_value(ObMenu *self);
48 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
49                             gpointer data);
50 static void parse_menu_separator(ObParseInst *i,
51                                  xmlDocPtr doc, xmlNodePtr node,
52                                  gpointer data);
53 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
54                        gpointer data);
55
56 static void client_dest(ObClient *client, gpointer data)
57 {
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);
61 }
62
63 void menu_startup(gboolean reconfig)
64 {
65     xmlDocPtr doc;
66     xmlNodePtr node;
67     gboolean loaded = FALSE;
68     GSList *it;
69
70     menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
71                                       (GDestroyNotify)menu_destroy_hash_value);
72
73     client_list_menu_startup(reconfig);
74     client_menu_startup();
75
76     menu_parse_inst = parse_startup();
77
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,
82                    &menu_parse_state);
83     parse_register(menu_parse_inst, "separator",
84                    parse_menu_separator, &menu_parse_state);
85
86     for (it = config_menu_files; it; it = g_slist_next(it)) {
87         if (parse_load_menu(it->data, &doc, &node)) {
88             loaded = TRUE;
89             parse_tree(menu_parse_inst, doc, node->children);
90             xmlFreeDoc(doc);
91         }
92     }
93     if (!loaded) {
94         if (parse_load_menu("menu.xml", &doc, &node)) {
95             parse_tree(menu_parse_inst, doc, node->children);
96             xmlFreeDoc(doc);
97         }
98     }
99     
100     g_assert(menu_parse_state.parent == NULL);
101
102     if (!reconfig)
103         client_add_destructor(client_dest, NULL);
104 }
105
106 void menu_shutdown(gboolean reconfig)
107 {
108     if (!reconfig)
109         client_remove_destructor(client_dest);
110
111     parse_shutdown(menu_parse_inst);
112     menu_parse_inst = NULL;
113
114     client_list_menu_shutdown(reconfig);
115
116     menu_frame_hide_all();
117     g_hash_table_destroy(menu_hash);
118     menu_hash = NULL;
119 }
120
121 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
122 {
123     ObMenu *menu = val;
124     return menu->pipe_creator == data;
125 }
126
127 void menu_pipe_execute(ObMenu *self)
128 {
129     xmlDocPtr doc;
130     xmlNodePtr node;
131     gchar *output;
132     GError *err = NULL;
133
134     if (!self->execute)
135         return;
136
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);
139         g_error_free(err);
140         return;
141     }
142
143     if (parse_load_mem(output, strlen(output),
144                        "openbox_pipe_menu", &doc, &node))
145     {
146         g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
147         menu_clear_entries(self);
148
149         menu_parse_state.pipe_creator = self;
150         menu_parse_state.parent = self;
151         parse_tree(menu_parse_inst, doc, node->children);
152         xmlFreeDoc(doc);
153     } else {
154         g_warning("Invalid output from pipe-menu: %s", self->execute);
155     }
156
157     g_free(output);
158 }
159
160 static ObMenu* menu_from_name(gchar *name)
161 {
162     ObMenu *self = NULL;
163
164     g_assert(name != NULL);
165
166     if (!(self = g_hash_table_lookup(menu_hash, name)))
167         g_warning("Attempted to access menu '%s' but it does not exist.",
168                   name);
169     return self;
170 }  
171
172 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
173                             gpointer data)
174 {
175     ObMenuParseState *state = data;
176     gchar *label;
177     
178     if (state->parent) {
179         if (parse_attr_string("label", node, &label)) {
180             GSList *acts = NULL;
181
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);
186                     if (a)
187                         acts = g_slist_append(acts, a);
188                 }
189             menu_add_normal(state->parent, -1, label, acts);
190             g_free(label);
191         }
192     }
193 }
194
195 static void parse_menu_separator(ObParseInst *i,
196                                  xmlDocPtr doc, xmlNodePtr node,
197                                  gpointer data)
198 {
199     ObMenuParseState *state = data;
200
201     if (state->parent)
202         menu_add_separator(state->parent, -1);
203 }
204
205 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
206                        gpointer data)
207 {
208     ObMenuParseState *state = data;
209     gchar *name = NULL, *title = NULL, *script = NULL;
210     ObMenu *menu;
211
212     if (!parse_attr_string("id", node, &name))
213         goto parse_menu_fail;
214
215     if (!g_hash_table_lookup(menu_hash, name)) {
216         if (!parse_attr_string("label", node, &title))
217             goto parse_menu_fail;
218
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);
223             } else {
224                 ObMenu *old;
225
226                 old = state->parent;
227                 state->parent = menu;
228                 parse_tree(i, doc, node->children);
229                 state->parent = old;
230             }
231         }
232     }
233
234     if (state->parent)
235         menu_add_submenu(state->parent, -1, name);
236
237 parse_menu_fail:
238     g_free(name);
239     g_free(title);
240     g_free(script);
241 }
242
243 ObMenu* menu_new(gchar *name, gchar *title, gpointer data)
244 {
245     ObMenu *self;
246
247     self = g_new0(ObMenu, 1);
248     self->name = g_strdup(name);
249     self->title = g_strdup(title);
250     self->data = data;
251
252     g_hash_table_replace(menu_hash, self->name, self);
253
254     return self;
255 }
256
257 static void menu_destroy_hash_value(ObMenu *self)
258 {
259     /* make sure its not visible */
260     {
261         GList *it;
262         ObMenuFrame *f;
263
264         for (it = menu_frame_visible; it; it = g_list_next(it)) {
265             f = it->data;
266             if (f->menu == self)
267                 menu_frame_hide_all();
268         }
269     }
270
271     if (self->destroy_func)
272         self->destroy_func(self, self->data);
273
274     menu_clear_entries(self);
275     g_free(self->name);
276     g_free(self->title);
277     g_free(self->execute);
278
279     g_free(self);
280 }
281
282 void menu_free(ObMenu *menu)
283 {
284     g_hash_table_remove(menu_hash, menu->name);
285 }
286
287 void menu_show(gchar *name, gint x, gint y, ObClient *client)
288 {
289     ObMenu *self;
290     ObMenuFrame *frame;
291     guint i;
292
293     if (!(self = menu_from_name(name))
294         || keyboard_interactively_grabbed()) return;
295
296     /* if the requested menu is already the top visible menu, then don't
297        bother */
298     if (menu_frame_visible) {
299         frame = menu_frame_visible->data;
300         if (frame->menu == self)
301             return;
302     }
303
304     menu_frame_hide_all();
305
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);
311     } else
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)) {
317             frame->monitor = i;
318             break;
319         }
320     }
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);
328     }
329 }
330
331 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
332 {
333     ObMenuEntry *self;
334
335     g_assert(menu);
336
337     self = g_new0(ObMenuEntry, 1);
338     self->type = type;
339     self->menu = menu;
340     self->id = id;
341
342     switch (type) {
343     case OB_MENU_ENTRY_TYPE_NORMAL:
344         self->data.normal.enabled = TRUE;
345         break;
346     case OB_MENU_ENTRY_TYPE_SUBMENU:
347     case OB_MENU_ENTRY_TYPE_SEPARATOR:
348         break;
349     }
350
351     return self;
352 }
353
354 void menu_entry_free(ObMenuEntry *self)
355 {
356     if (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);
365             }
366             break;
367         case OB_MENU_ENTRY_TYPE_SUBMENU:
368             g_free(self->data.submenu.name);
369             break;
370         case OB_MENU_ENTRY_TYPE_SEPARATOR:
371             break;
372         }
373
374         g_free(self);
375     }
376 }
377
378 void menu_clear_entries(ObMenu *self)
379 {
380 #ifdef DEBUG
381     /* assert that the menu isn't visible */
382     {
383         GList *it;
384         ObMenuFrame *f;
385
386         for (it = menu_frame_visible; it; it = g_list_next(it)) {
387             f = it->data;
388             g_assert(f->menu != self);
389         }
390     }
391 #endif
392
393     while (self->entries) {
394         menu_entry_free(self->entries->data);
395         self->entries = g_list_delete_link(self->entries, self->entries);
396     }
397 }
398
399 void menu_entry_remove(ObMenuEntry *self)
400 {
401     self->menu->entries = g_list_remove(self->menu->entries, self);
402     menu_entry_free(self);
403 }
404
405 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, gchar *label,
406                              GSList *actions)
407 {
408     ObMenuEntry *e;
409
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;
413
414     self->entries = g_list_append(self->entries, e);
415     return e;
416 }
417
418 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, gchar *submenu)
419 {
420     ObMenuEntry *e;
421
422     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
423     e->data.submenu.name = g_strdup(submenu);
424
425     self->entries = g_list_append(self->entries, e);
426     return e;
427 }
428
429 ObMenuEntry* menu_add_separator(ObMenu *self, gint id)
430 {
431     ObMenuEntry *e;
432
433     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
434
435     self->entries = g_list_append(self->entries, e);
436     return e;
437 }
438
439 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
440 {
441     self->update_func = func;
442 }
443
444 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
445 {
446     self->execute_func = func;
447 }
448
449 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
450 {
451     self->destroy_func = func;
452 }
453
454 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
455 {
456     ObMenuEntry *ret = NULL;
457     GList *it;
458
459     for (it = self->entries; it; it = g_list_next(it)) {
460         ObMenuEntry *e = it->data;
461
462         if (e->id == id) {
463             ret = e;
464             break;
465         }
466     }
467     return ret;
468 }
469
470 void menu_find_submenus(ObMenu *self)
471 {
472     GList *it;
473
474     for (it = self->entries; it; it = g_list_next(it)) {
475         ObMenuEntry *e = it->data;
476
477         if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
478             e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
479     }
480 }