fix a crash from chroot of invalid keys, and make keybindings reload better
[dana/openbox.git] / openbox / keyboard.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    keyboard.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 "mainloop.h"
21 #include "focus.h"
22 #include "screen.h"
23 #include "frame.h"
24 #include "openbox.h"
25 #include "event.h"
26 #include "grab.h"
27 #include "client.h"
28 #include "actions.h"
29 #include "prop.h"
30 #include "menuframe.h"
31 #include "config.h"
32 #include "keytree.h"
33 #include "keyboard.h"
34 #include "translate.h"
35 #include "moveresize.h"
36 #include "popup.h"
37 #include "gettext.h"
38
39 #include <glib.h>
40
41 KeyBindingTree *keyboard_firstnode = NULL;
42 static ObPopup *popup = NULL;
43 static KeyBindingTree *curpos;
44
45 static void grab_keys(gboolean grab)
46 {
47     KeyBindingTree *p;
48
49     ungrab_all_keys(RootWindow(ob_display, ob_screen));
50
51     if (grab) {
52         p = curpos ? curpos->first_child : keyboard_firstnode;
53         while (p) {
54             if (p->key)
55                 grab_key(p->key, p->state, RootWindow(ob_display, ob_screen),
56                          GrabModeAsync);
57             p = p->next_sibling;
58         }
59         if (curpos)
60             grab_key(config_keyboard_reset_keycode,
61                      config_keyboard_reset_state,
62                      RootWindow(ob_display, ob_screen), GrabModeAsync);
63     }
64 }
65
66 static gboolean chain_timeout(gpointer data)
67 {
68     keyboard_reset_chains(0);
69     return FALSE; /* don't repeat */
70 }
71
72 static void set_curpos(KeyBindingTree *newpos)
73 {
74     if (curpos == newpos) return;
75
76     grab_keys(FALSE);
77     curpos = newpos;
78     grab_keys(TRUE);
79
80     if (curpos != NULL) {
81         gchar *text = NULL;
82         GList *it;
83         Rect *a;
84
85         for (it = curpos->keylist; it; it = g_list_next(it)) {
86             gchar *oldtext = text;
87             if (text == NULL)
88                 text = g_strdup(it->data);
89             else
90                 text = g_strconcat(text, " - ", it->data, NULL);
91             g_free(oldtext);
92         }
93
94         a = screen_physical_area_active();
95         popup_position(popup, NorthWestGravity, a->x + 10, a->y + 10);
96         /* 1 second delay for the popup to show */
97         popup_delay_show(popup, G_USEC_PER_SEC, text);
98         g_free(text);
99         g_free(a);
100     } else {
101         popup_hide(popup);
102     }
103 }
104
105 void keyboard_reset_chains(gint break_chroots)
106 {
107     KeyBindingTree *p;
108
109     for (p = curpos; p; p = p->parent) {
110         if (p->chroot) {
111             if (break_chroots == 0) break; /* stop here */
112             if (break_chroots > 0)
113                 --break_chroots;
114         }
115     }
116     set_curpos(p);
117 }
118
119 void keyboard_unbind_all(void)
120 {
121     tree_destroy(keyboard_firstnode);
122     keyboard_firstnode = NULL;
123 }
124
125 void keyboard_chroot(GList *keylist)
126 {
127     /* try do it in the existing tree. if we can't that means it is an empty
128        chroot binding. so add it to the tree then. */
129     if (!tree_chroot(keyboard_firstnode, keylist)) {
130         KeyBindingTree *tree;
131         if (!(tree = tree_build(keylist)))
132             return;
133         tree_chroot(tree, keylist);
134         tree_assimilate(tree);
135     }
136 }
137
138 gboolean keyboard_bind(GList *keylist, ObActionsAct *action)
139 {
140     KeyBindingTree *tree, *t;
141     gboolean conflict;
142
143     g_assert(keylist != NULL);
144     g_assert(action != NULL);
145
146     if (!(tree = tree_build(keylist)))
147         return FALSE;
148
149     if ((t = tree_find(tree, &conflict)) != NULL) {
150         /* already bound to something, use the existing tree */
151         tree_destroy(tree);
152         tree = NULL;
153     } else
154         t = tree;
155
156     if (conflict) {
157         g_message(_("Conflict with key binding in config file"));
158         tree_destroy(tree);
159         return FALSE;
160     }
161
162     /* find the bottom node */
163     for (; t->first_child; t = t->first_child);
164
165     /* set the action */
166     t->actions = g_slist_append(t->actions, action);
167     /* assimilate this built tree into the main tree. assimilation
168        destroys/uses the tree */
169     if (tree) tree_assimilate(tree);
170
171     return TRUE;
172 }
173
174 #if 0
175 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
176 {
177     gboolean handled = FALSE;
178     gboolean done = FALSE;
179     gboolean cancel = FALSE;
180
181     if (istate.active) {
182         if ((e->type == KeyRelease && !(istate.state & e->xkey.state))) {
183             done = TRUE;
184             handled = TRUE;
185         } else if (e->type == KeyPress) {
186             /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
187               done = TRUE;
188               else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
189                   cancel = done = TRUE;
190                   handled = TRUE;
191               }
192         } else if (e->type == ButtonPress) {
193             cancel = TRUE;
194             done = TRUE;
195             handled = FALSE;
196         }
197
198         if (done)
199             keyboard_interactive_end(e->xkey.state, cancel, e->xkey.time,TRUE);
200
201         if (handled)
202             *client = istate.client;
203     }
204
205     return handled;
206 }
207 #endif
208
209 void keyboard_event(ObClient *client, const XEvent *e)
210 {
211     KeyBindingTree *p;
212
213     if (e->type == KeyRelease) {
214         grab_key_passive_count(-1);
215         return;
216     }
217
218     g_assert(e->type == KeyPress);
219     grab_key_passive_count(1);
220
221     if (e->xkey.keycode == config_keyboard_reset_keycode &&
222         e->xkey.state == config_keyboard_reset_state)
223     {
224         ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
225         keyboard_reset_chains(-1);
226         return;
227     }
228
229     if (curpos == NULL)
230         p = keyboard_firstnode;
231     else
232         p = curpos->first_child;
233     while (p) {
234         if (p->key == e->xkey.keycode &&
235             p->state == e->xkey.state)
236         {
237             /* if we hit a key binding, then close any open menus and run it */
238             if (menu_frame_visible)
239                 menu_frame_hide_all();
240
241             if (p->first_child != NULL) { /* part of a chain */
242                 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
243                 /* 3 second timeout for chains */
244                 ob_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
245                                          chain_timeout, NULL,
246                                          g_direct_equal, NULL);
247                 set_curpos(p);
248             } else if (p->chroot)         /* an empty chroot */
249                 set_curpos(p);
250             else {
251                 GSList *it;
252
253                 for (it = p->actions; it; it = g_slist_next(it))
254                     if (actions_act_is_interactive(it->data)) break;
255                 if (it == NULL) /* reset if the actions are not interactive */
256                     keyboard_reset_chains(0);
257
258                 actions_run_acts(p->actions, OB_USER_ACTION_KEYBOARD_KEY,
259                                  e->xkey.state, e->xkey.x_root, e->xkey.y_root,
260                                  0, OB_FRAME_CONTEXT_NONE, client);
261             }
262             break;
263         }
264         p = p->next_sibling;
265     }
266 }
267
268 static void node_rebind(KeyBindingTree *node)
269 {
270     if (node->first_child) {
271         /* find leaf nodes */
272         node_rebind(node->first_child);
273
274         /* for internal nodes, add them to the tree if they
275            are a chroot, but do this after adding their
276            children */
277         if (node->chroot)
278             keyboard_chroot(node->keylist);
279     }
280     else {
281         /* for leaf nodes, rebind each action assigned to it */
282         GSList *it;
283         while (node->actions) {
284             /* add each action, and remove them from the original tree so
285                they don't get free'd on us */
286             keyboard_bind(node->keylist, node->actions->data);
287             node->actions = g_slist_delete_link(node->actions, node->actions);
288         }
289
290         if (node->chroot)
291             keyboard_chroot(node->keylist);
292     }
293
294     /* go through each sibling */
295     if (node->next_sibling) node_rebind(node->next_sibling);
296 }
297
298 void keyboard_rebind(void)
299 {
300     KeyBindingTree *old;
301
302     old = keyboard_firstnode;
303     keyboard_firstnode = NULL;
304     node_rebind(old);
305
306     tree_destroy(old);
307     set_curpos(NULL);
308     grab_keys(TRUE);
309 }
310
311 void keyboard_startup(gboolean reconfig)
312 {
313     grab_keys(TRUE);
314     popup = popup_new();
315     popup_set_text_align(popup, RR_JUSTIFY_CENTER);
316 }
317
318 void keyboard_shutdown(gboolean reconfig)
319 {
320     ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
321
322     keyboard_unbind_all();
323     set_curpos(NULL);
324
325     popup_free(popup);
326     popup = NULL;
327 }
328