]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
fix how the dock is placed, its struts, and set a window type hint on it
[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 "mainloop.h"
24 #include "stacking.h"
25 #include "client.h"
26 #include "config.h"
27 #include "screen.h"
28 #include "menuframe.h"
29 #include "keyboard.h"
30 #include "geom.h"
31 #include "misc.h"
32 #include "client_menu.h"
33 #include "client_list_menu.h"
34 #include "client_list_combined_menu.h"
35 #include "gettext.h"
36 #include "parser/parse.h"
37
38 typedef struct _ObMenuParseState ObMenuParseState;
39
40 struct _ObMenuParseState
41 {
42 ObMenu *parent;
43 ObMenu *pipe_creator;
44 };
45
46 static GHashTable *menu_hash = NULL;
47 static ObParseInst *menu_parse_inst;
48 static ObMenuParseState menu_parse_state;
49 static gboolean menu_can_hide = FALSE;
50
51 static void menu_destroy_hash_value(ObMenu *self);
52 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
53 gpointer data);
54 static void parse_menu_separator(ObParseInst *i,
55 xmlDocPtr doc, xmlNodePtr node,
56 gpointer data);
57 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
58 gpointer data);
59 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
60 gchar **strippedlabel, guint *position);
61
62
63 static void client_dest(ObClient *client, gpointer data)
64 {
65 /* menus can be associated with a client, so close any that are since
66 we are disappearing now */
67 menu_frame_hide_all_client(client);
68 }
69
70 void menu_startup(gboolean reconfig)
71 {
72 xmlDocPtr doc;
73 xmlNodePtr node;
74 gboolean loaded = FALSE;
75 GSList *it;
76
77 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
78 (GDestroyNotify)menu_destroy_hash_value);
79
80 client_list_menu_startup(reconfig);
81 client_list_combined_menu_startup(reconfig);
82 client_menu_startup();
83
84 menu_parse_inst = parse_startup();
85
86 menu_parse_state.parent = NULL;
87 menu_parse_state.pipe_creator = NULL;
88 parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
89 parse_register(menu_parse_inst, "item", parse_menu_item,
90 &menu_parse_state);
91 parse_register(menu_parse_inst, "separator",
92 parse_menu_separator, &menu_parse_state);
93
94 for (it = config_menu_files; it; it = g_slist_next(it)) {
95 if (parse_load_menu(it->data, &doc, &node)) {
96 loaded = TRUE;
97 parse_tree(menu_parse_inst, doc, node->children);
98 xmlFreeDoc(doc);
99 } else
100 g_message(_("Unable to find a valid menu file '%s'"),
101 (const gchar*)it->data);
102 }
103 if (!loaded) {
104 if (parse_load_menu("menu.xml", &doc, &node)) {
105 parse_tree(menu_parse_inst, doc, node->children);
106 xmlFreeDoc(doc);
107 } else
108 g_message(_("Unable to find a valid menu file '%s'"),
109 "menu.xml");
110 }
111
112 g_assert(menu_parse_state.parent == NULL);
113
114 if (!reconfig)
115 client_add_destroy_notify(client_dest, NULL);
116 }
117
118 void menu_shutdown(gboolean reconfig)
119 {
120 if (!reconfig)
121 client_remove_destroy_notify(client_dest);
122
123 parse_shutdown(menu_parse_inst);
124 menu_parse_inst = NULL;
125
126 client_list_menu_shutdown(reconfig);
127 client_list_combined_menu_shutdown(reconfig);
128
129 menu_frame_hide_all();
130 g_hash_table_destroy(menu_hash);
131 menu_hash = NULL;
132 }
133
134 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
135 {
136 ObMenu *menu = val;
137 return menu->pipe_creator != NULL;
138 }
139
140 static void clear_cache(gpointer key, gpointer val, gpointer data)
141 {
142 ObMenu *menu = val;
143 if (menu->execute)
144 menu_clear_entries(menu);
145 }
146
147 void menu_clear_pipe_caches()
148 {
149 /* delete any pipe menus' submenus */
150 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, NULL);
151 /* empty the top level pipe menus */
152 g_hash_table_foreach(menu_hash, clear_cache, NULL);
153 }
154
155 void menu_pipe_execute(ObMenu *self)
156 {
157 xmlDocPtr doc;
158 xmlNodePtr node;
159 gchar *output;
160 GError *err = NULL;
161
162 if (!self->execute)
163 return;
164 if (self->entries) /* the entries are already created and cached */
165 return;
166
167 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
168 g_message(_("Failed to execute command for pipe-menu '%s': %s"),
169 self->execute, err->message);
170 g_error_free(err);
171 return;
172 }
173
174 if (parse_load_mem(output, strlen(output),
175 "openbox_pipe_menu", &doc, &node))
176 {
177 menu_parse_state.pipe_creator = self;
178 menu_parse_state.parent = self;
179 parse_tree(menu_parse_inst, doc, node->children);
180 xmlFreeDoc(doc);
181 } else {
182 g_message(_("Invalid output from pipe-menu '%s'"), self->execute);
183 }
184
185 g_free(output);
186 }
187
188 static ObMenu* menu_from_name(gchar *name)
189 {
190 ObMenu *self = NULL;
191
192 g_assert(name != NULL);
193
194 if (!(self = g_hash_table_lookup(menu_hash, name)))
195 g_message(_("Attempted to access menu '%s' but it does not exist"),
196 name);
197 return self;
198 }
199
200 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
201 ((c) >= 'A' && (c) <= 'Z') || \
202 ((c) >= 'a' && (c) <= 'z'))
203
204 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
205 gchar **strippedlabel, guint *position)
206 {
207 gunichar shortcut = 0;
208
209 *position = 0;
210
211 g_assert(strippedlabel != NULL);
212
213 if (label == NULL) {
214 *strippedlabel = NULL;
215 } else {
216 gchar *i;
217
218 *strippedlabel = g_strdup(label);
219
220 /* if allow_shortcut is false, then you can't use the &, instead you
221 have to just use the first valid character
222 */
223
224 i = strchr(*strippedlabel, '&');
225 if (allow_shortcut && i != NULL) {
226 /* there is an ampersand in the string */
227
228 /* you have to use a printable ascii character for shortcuts
229 don't allow space either, so you can have like "a & b"
230 */
231 if (VALID_SHORTCUT(*(i+1))) {
232 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
233 *position = i - *strippedlabel;
234
235 /* remove the & from the string */
236 for (; *i != '\0'; ++i)
237 *i = *(i+1);
238 }
239 } else {
240 /* there is no ampersand, so find the first valid character to use
241 instead */
242
243 for (i = *strippedlabel; *i != '\0'; ++i)
244 if (VALID_SHORTCUT(*i)) {
245 *position = i - *strippedlabel;
246 shortcut = g_unichar_tolower(g_utf8_get_char(i));
247 break;
248 }
249 }
250 }
251 return shortcut;
252 }
253
254 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
255 gpointer data)
256 {
257 ObMenuParseState *state = data;
258 gchar *label;
259
260 if (state->parent) {
261 if (parse_attr_string("label", node, &label)) {
262 GSList *acts = NULL;
263
264 for (node = node->children; node; node = node->next)
265 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
266 ObAction *a = action_parse
267 (i, doc, node, OB_USER_ACTION_MENU_SELECTION);
268 if (a)
269 acts = g_slist_append(acts, a);
270 }
271 menu_add_normal(state->parent, -1, label, acts, FALSE);
272 g_free(label);
273 }
274 }
275 }
276
277 static void parse_menu_separator(ObParseInst *i,
278 xmlDocPtr doc, xmlNodePtr node,
279 gpointer data)
280 {
281 ObMenuParseState *state = data;
282
283 if (state->parent) {
284 gchar *label;
285
286 if (!parse_attr_string("label", node, &label))
287 label = NULL;
288
289 menu_add_separator(state->parent, -1, label);
290 g_free(label);
291 }
292 }
293
294 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
295 gpointer data)
296 {
297 ObMenuParseState *state = data;
298 gchar *name = NULL, *title = NULL, *script = NULL;
299 ObMenu *menu;
300
301 if (!parse_attr_string("id", node, &name))
302 goto parse_menu_fail;
303
304 if (!g_hash_table_lookup(menu_hash, name)) {
305 if (!parse_attr_string("label", node, &title))
306 goto parse_menu_fail;
307
308 if ((menu = menu_new(name, title, FALSE, NULL))) {
309 menu->pipe_creator = state->pipe_creator;
310 if (parse_attr_string("execute", node, &script)) {
311 menu->execute = parse_expand_tilde(script);
312 } else {
313 ObMenu *old;
314
315 old = state->parent;
316 state->parent = menu;
317 parse_tree(i, doc, node->children);
318 state->parent = old;
319 }
320 }
321 }
322
323 if (state->parent)
324 menu_add_submenu(state->parent, -1, name);
325
326 parse_menu_fail:
327 g_free(name);
328 g_free(title);
329 g_free(script);
330 }
331
332 ObMenu* menu_new(const gchar *name, const gchar *title,
333 gboolean allow_shortcut_selection, gpointer data)
334 {
335 ObMenu *self;
336
337 self = g_new0(ObMenu, 1);
338 self->name = g_strdup(name);
339 self->data = data;
340
341 self->shortcut = parse_shortcut(title, allow_shortcut_selection,
342 &self->title, &self->shortcut_position);
343
344 g_hash_table_replace(menu_hash, self->name, self);
345
346 /* Each menu has a single more_menu. When the menu spills past what
347 can fit on the screen, a new menu frame entry is created from this
348 more_menu, and a new menu frame for the submenu is created for this
349 menu, also pointing to the more_menu.
350
351 This can be done multiple times using the same more_menu.
352
353 more_menu->more_menu will always be NULL, since there is only 1 for
354 each menu. */
355 self->more_menu = g_new0(ObMenu, 1);
356 self->more_menu->name = _("More...");
357 self->more_menu->title = _("More...");
358 self->more_menu->data = data;
359 self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
360
361 self->more_menu->show_func = self->show_func;
362 self->more_menu->hide_func = self->hide_func;
363 self->more_menu->update_func = self->update_func;
364 self->more_menu->execute_func = self->execute_func;
365 self->more_menu->destroy_func = self->destroy_func;
366 self->more_menu->place_func = self->place_func;
367
368 return self;
369 }
370
371 static void menu_destroy_hash_value(ObMenu *self)
372 {
373 /* make sure its not visible */
374 {
375 GList *it;
376 ObMenuFrame *f;
377
378 for (it = menu_frame_visible; it; it = g_list_next(it)) {
379 f = it->data;
380 if (f->menu == self)
381 menu_frame_hide_all();
382 }
383 }
384
385 if (self->destroy_func)
386 self->destroy_func(self, self->data);
387
388 menu_clear_entries(self);
389 g_free(self->name);
390 g_free(self->title);
391 g_free(self->execute);
392 g_free(self->more_menu);
393
394 g_free(self);
395 }
396
397 void menu_free(ObMenu *menu)
398 {
399 if (menu)
400 g_hash_table_remove(menu_hash, menu->name);
401 }
402
403 static gboolean menu_hide_delay_func(gpointer data)
404 {
405 menu_can_hide = TRUE;
406 return FALSE; /* no repeat */
407 }
408
409 void menu_show(gchar *name, gint x, gint y, gint button, ObClient *client)
410 {
411 ObMenu *self;
412 ObMenuFrame *frame;
413
414 if (!(self = menu_from_name(name))
415 || keyboard_interactively_grabbed()) return;
416
417 /* if the requested menu is already the top visible menu, then don't
418 bother */
419 if (menu_frame_visible) {
420 frame = menu_frame_visible->data;
421 if (frame->menu == self)
422 return;
423 }
424
425 menu_frame_hide_all();
426
427 /* clear the pipe menus when showing a new menu */
428 menu_clear_pipe_caches();
429
430 frame = menu_frame_new(self, 0, client);
431 if (!menu_frame_show_topmenu(frame, x, y, button))
432 menu_frame_free(frame);
433 else if (!button) {
434 /* select the first entry if it's not a submenu and we opened
435 * the menu with the keyboard, and skip all headers */
436 GList *it = frame->entries;
437 while (it) {
438 ObMenuEntryFrame *e = it->data;
439 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
440 menu_frame_select(frame, e, FALSE);
441 break;
442 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
443 it = g_list_next(it);
444 else
445 break;
446 }
447 }
448
449 if (!button)
450 menu_can_hide = TRUE;
451 else {
452 menu_can_hide = FALSE;
453 ob_main_loop_timeout_add(ob_main_loop,
454 config_menu_hide_delay * 1000,
455 menu_hide_delay_func,
456 NULL, g_direct_equal, NULL);
457 }
458 }
459
460 gboolean menu_hide_delay_reached()
461 {
462 return menu_can_hide;
463 }
464
465 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
466 {
467 ObMenuEntry *self;
468
469 g_assert(menu);
470
471 self = g_new0(ObMenuEntry, 1);
472 self->ref = 1;
473 self->type = type;
474 self->menu = menu;
475 self->id = id;
476
477 switch (type) {
478 case OB_MENU_ENTRY_TYPE_NORMAL:
479 self->data.normal.enabled = TRUE;
480 break;
481 case OB_MENU_ENTRY_TYPE_SUBMENU:
482 case OB_MENU_ENTRY_TYPE_SEPARATOR:
483 break;
484 }
485
486 return self;
487 }
488
489 void menu_entry_ref(ObMenuEntry *self)
490 {
491 ++self->ref;
492 }
493
494 void menu_entry_unref(ObMenuEntry *self)
495 {
496 if (self && --self->ref == 0) {
497 switch (self->type) {
498 case OB_MENU_ENTRY_TYPE_NORMAL:
499 g_free(self->data.normal.label);
500 while (self->data.normal.actions) {
501 action_unref(self->data.normal.actions->data);
502 self->data.normal.actions =
503 g_slist_delete_link(self->data.normal.actions,
504 self->data.normal.actions);
505 }
506 break;
507 case OB_MENU_ENTRY_TYPE_SUBMENU:
508 g_free(self->data.submenu.name);
509 break;
510 case OB_MENU_ENTRY_TYPE_SEPARATOR:
511 break;
512 }
513
514 g_free(self);
515 }
516 }
517
518 void menu_clear_entries(ObMenu *self)
519 {
520 #ifdef DEBUG
521 /* assert that the menu isn't visible */
522 {
523 GList *it;
524 ObMenuFrame *f;
525
526 for (it = menu_frame_visible; it; it = g_list_next(it)) {
527 f = it->data;
528 g_assert(f->menu != self);
529 }
530 }
531 #endif
532
533 while (self->entries) {
534 menu_entry_unref(self->entries->data);
535 self->entries = g_list_delete_link(self->entries, self->entries);
536 }
537 self->more_menu->entries = self->entries; /* keep it in sync */
538 }
539
540 void menu_entry_remove(ObMenuEntry *self)
541 {
542 self->menu->entries = g_list_remove(self->menu->entries, self);
543 menu_entry_unref(self);
544 }
545
546 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
547 GSList *actions, gboolean allow_shortcut)
548 {
549 ObMenuEntry *e;
550
551 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
552 e->data.normal.actions = actions;
553
554 menu_entry_set_label(e, label, allow_shortcut);
555
556 self->entries = g_list_append(self->entries, e);
557 self->more_menu->entries = self->entries; /* keep it in sync */
558 return e;
559 }
560
561 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
562 {
563 ObMenuEntry *e;
564 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
565 /* points to itself */
566 e->data.submenu.name = g_strdup(self->name);
567 e->data.submenu.submenu = self;
568 e->data.submenu.show_from = show_from;
569 return e;
570 }
571
572 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
573 {
574 ObMenuEntry *e;
575
576 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
577 e->data.submenu.name = g_strdup(submenu);
578
579 self->entries = g_list_append(self->entries, e);
580 self->more_menu->entries = self->entries; /* keep it in sync */
581 return e;
582 }
583
584 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
585 {
586 ObMenuEntry *e;
587
588 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
589
590 menu_entry_set_label(e, label, FALSE);
591
592 self->entries = g_list_append(self->entries, e);
593 self->more_menu->entries = self->entries; /* keep it in sync */
594 return e;
595 }
596
597 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
598 {
599 self->show_func = func;
600 self->more_menu->show_func = func; /* keep it in sync */
601 }
602
603 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
604 {
605 self->hide_func = func;
606 self->more_menu->hide_func = func; /* keep it in sync */
607 }
608
609 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
610 {
611 self->update_func = func;
612 self->more_menu->update_func = func; /* keep it in sync */
613 }
614
615 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
616 {
617 self->execute_func = func;
618 self->more_menu->execute_func = func; /* keep it in sync */
619 }
620
621 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
622 {
623 self->destroy_func = func;
624 self->more_menu->destroy_func = func; /* keep it in sync */
625 }
626
627 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
628 {
629 self->place_func = func;
630 self->more_menu->place_func = func; /* keep it in sync */
631 }
632
633 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
634 {
635 ObMenuEntry *ret = NULL;
636 GList *it;
637
638 for (it = self->entries; it; it = g_list_next(it)) {
639 ObMenuEntry *e = it->data;
640
641 if (e->id == id) {
642 ret = e;
643 break;
644 }
645 }
646 return ret;
647 }
648
649 void menu_find_submenus(ObMenu *self)
650 {
651 GList *it;
652
653 for (it = self->entries; it; it = g_list_next(it)) {
654 ObMenuEntry *e = it->data;
655
656 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
657 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
658 }
659 }
660
661 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
662 gboolean allow_shortcut)
663 {
664 switch (self->type) {
665 case OB_MENU_ENTRY_TYPE_SEPARATOR:
666 g_free(self->data.separator.label);
667 self->data.separator.label = g_strdup(label);
668 break;
669 case OB_MENU_ENTRY_TYPE_NORMAL:
670 g_free(self->data.normal.label);
671 self->data.normal.shortcut =
672 parse_shortcut(label, allow_shortcut, &self->data.normal.label,
673 &self->data.normal.shortcut_position);
674 break;
675 default:
676 g_assert_not_reached();
677 }
678 }
679
680 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
681 {
682 self->show_all_shortcuts = show;
683 }
This page took 0.069947 seconds and 4 git commands to generate.