add chrooting. use chroot="true" on the chroot location
[mikachu/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 "action.h"
29 #include "prop.h"
30 #include "config.h"
31 #include "keytree.h"
32 #include "keyboard.h"
33 #include "translate.h"
34 #include "moveresize.h"
35 #include "popup.h"
36 #include "gettext.h"
37
38 #include <glib.h>
39
40 typedef struct {
41     guint state;
42     ObClient *client;
43     GSList *actions;
44     ObFrameContext context;
45 } ObInteractiveState;
46
47 KeyBindingTree *keyboard_firstnode = NULL;
48 static ObPopup *popup = NULL;
49 static GSList *interactive_states;
50 static KeyBindingTree *curpos;
51
52 static void grab_keys(gboolean grab)
53 {
54     KeyBindingTree *p;
55
56     ungrab_all_keys(RootWindow(ob_display, ob_screen));
57
58     if (grab) {
59         p = curpos ? curpos->first_child : keyboard_firstnode;
60         while (p) {
61             grab_key(p->key, p->state, RootWindow(ob_display, ob_screen),
62                      GrabModeAsync);
63             p = p->next_sibling;
64         }
65         if (curpos)
66             grab_key(config_keyboard_reset_keycode,
67                      config_keyboard_reset_state,
68                      RootWindow(ob_display, ob_screen), GrabModeAsync);
69     }
70 }
71
72 static gboolean popup_show_timeout(gpointer data)
73 {
74     gchar *text = data;
75     popup_show(popup, text);
76
77     return FALSE; /* don't repeat */
78 }
79
80 static void set_curpos(KeyBindingTree *newpos)
81 {
82     grab_keys(FALSE);
83     curpos = newpos;
84     grab_keys(TRUE);
85
86     if (curpos != NULL) {
87         gchar *text = NULL;
88         GList *it;
89
90         for (it = curpos->keylist; it; it = g_list_next(it))
91             text = g_strconcat((text ? text : ""), it->data, "-", NULL);
92
93         popup_position(popup, NorthWestGravity, 10, 10);
94         if (popup->mapped) {
95             popup_show_timeout(text);
96             g_free(text);
97         } else {
98             ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
99             ob_main_loop_timeout_add(ob_main_loop, 1 * G_USEC_PER_SEC,
100                                      popup_show_timeout, text,
101                                      g_direct_equal, g_free);
102         }
103     } else {
104         popup_hide(popup);
105         ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
106     }
107 }
108
109 void keyboard_reset_chains(gint break_chroots)
110 {
111     KeyBindingTree *p;
112
113     for (p = curpos; p; p = p->parent) {
114         if (p->chroot) {
115             if (break_chroots == 0) break; /* stop here */
116             if (break_chroots > 0)
117                 --break_chroots;
118         }
119     }
120     set_curpos(p);
121 }
122
123 void keyboard_unbind_all()
124 {
125     tree_destroy(keyboard_firstnode);
126     keyboard_firstnode = NULL;
127 }
128
129 void keyboard_chroot(GList *keylist)
130 {
131     /* try do it in the existing tree. if we can't that means it is an empty
132        chroot binding. so add it to the tree then. */
133     if (!tree_chroot(keyboard_firstnode, keylist)) {
134         KeyBindingTree *tree;
135         if (!(tree = tree_build(keylist)))
136             return;
137         tree_chroot(tree, keylist);
138         tree_assimilate(tree);
139     }
140 }
141
142 gboolean keyboard_bind(GList *keylist, ObAction *action)
143 {
144     KeyBindingTree *tree, *t;
145     gboolean conflict;
146     gboolean mods = TRUE;
147
148     g_assert(keylist != NULL);
149     g_assert(action != NULL);
150
151     if (!(tree = tree_build(keylist)))
152         return FALSE;
153
154     if ((t = tree_find(tree, &conflict)) != NULL) {
155         /* already bound to something, use the existing tree */
156         tree_destroy(tree);
157         tree = NULL;
158     } else
159         t = tree;
160
161     if (conflict) {
162         g_message(_("Conflict with key binding in config file"));
163         tree_destroy(tree);
164         return FALSE;
165     }
166
167     /* find if every key in this chain has modifiers, and also find the
168        bottom node of the tree */
169     while (t->first_child) {
170         if (!t->state)
171             mods = FALSE;
172         t = t->first_child;
173     }
174
175     /* when there are no modifiers in the binding, then the action cannot
176        be interactive */
177     if (!mods && action->data.any.interactive) {
178         action->data.any.interactive = FALSE;
179         action->data.inter.final = TRUE;
180     }
181
182     /* set the action */
183     t->actions = g_slist_append(t->actions, action);
184     /* assimilate this built tree into the main tree. assimilation
185        destroys/uses the tree */
186     if (tree) tree_assimilate(tree);
187
188     return TRUE;
189 }
190
191 gboolean keyboard_interactive_grab(guint state, ObClient *client,
192                                    ObAction *action)
193 {
194     ObInteractiveState *s;
195
196     g_assert(action->data.any.interactive);
197
198     if (!interactive_states) {
199         grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER);
200         if (!grab_keyboard(TRUE)) {
201             grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
202             return FALSE;
203         }
204     }
205
206     s = g_new(ObInteractiveState, 1);
207
208     s->state = state;
209     s->client = client;
210     s->actions = g_slist_append(NULL, action);
211
212     interactive_states = g_slist_append(interactive_states, s);
213
214     return TRUE;
215 }
216
217 void keyboard_interactive_end(ObInteractiveState *s,
218                               guint state, gboolean cancel, Time time)
219 {
220     action_run_interactive(s->actions, s->client, state, time, cancel, TRUE);
221
222     g_slist_free(s->actions);
223     g_free(s);
224
225     interactive_states = g_slist_remove(interactive_states, s);
226
227     if (!interactive_states) {
228         grab_keyboard(FALSE);
229         grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
230     }
231 }
232
233 void keyboard_interactive_end_client(ObClient *client, gpointer data)
234 {
235     GSList *it, *next;
236
237     for (it = interactive_states; it; it = next) {
238         ObInteractiveState *s = it->data;
239
240         next = g_slist_next(it);
241
242         if (s->client == client)
243             s->client = NULL;
244     }
245 }
246
247 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
248 {
249     GSList *it, *next;
250     gboolean handled = FALSE;
251     gboolean done = FALSE;
252     gboolean cancel = FALSE;
253
254     for (it = interactive_states; it; it = next) {
255         ObInteractiveState *s = it->data;
256
257         next = g_slist_next(it);
258         
259         if ((e->type == KeyRelease && 
260              !(s->state & e->xkey.state)))
261             done = TRUE;
262         else if (e->type == KeyPress) {
263             /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
264                 done = TRUE;
265             else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
266                 cancel = done = TRUE;
267         } else if (e->type == ButtonPress)
268             cancel = done = TRUE;
269
270         if (done) {
271             keyboard_interactive_end(s, e->xkey.state, cancel, e->xkey.time);
272
273             handled = TRUE;
274         } else
275             *client = s->client;
276     }
277
278     return handled;
279 }
280
281 void keyboard_event(ObClient *client, const XEvent *e)
282 {
283     KeyBindingTree *p;
284
285     g_assert(e->type == KeyPress);
286
287     if (e->xkey.keycode == config_keyboard_reset_keycode &&
288         e->xkey.state == config_keyboard_reset_state)
289     {
290         keyboard_reset_chains(-1);
291         return;
292     }
293
294     if (curpos == NULL)
295         p = keyboard_firstnode;
296     else
297         p = curpos->first_child;
298     while (p) {
299         if (p->key == e->xkey.keycode &&
300             p->state == e->xkey.state)
301         {
302             if (p->first_child != NULL) /* part of a chain */
303                 set_curpos(p);
304             else if (p->chroot)         /* an empty chroot */
305                 set_curpos(p);
306             else {
307                 keyboard_reset_chains(0);
308
309                 action_run_key(p->actions, client, e->xkey.state,
310                                e->xkey.x_root, e->xkey.y_root,
311                                e->xkey.time);
312             }
313             break;
314         }
315         p = p->next_sibling;
316     }
317 }
318
319 gboolean keyboard_interactively_grabbed()
320 {
321     return !!interactive_states;
322 }
323
324 void keyboard_startup(gboolean reconfig)
325 {
326     grab_keys(TRUE);
327     popup = popup_new(FALSE);
328
329     if (!reconfig)
330         client_add_destructor(keyboard_interactive_end_client, NULL);
331 }
332
333 void keyboard_shutdown(gboolean reconfig)
334 {
335     GSList *it;
336
337     if (!reconfig)
338         client_remove_destructor(keyboard_interactive_end_client);
339
340     for (it = interactive_states; it; it = g_slist_next(it))
341         g_free(it->data);
342     g_slist_free(interactive_states);
343     interactive_states = NULL;
344
345     ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
346
347     keyboard_unbind_all();
348     set_curpos(NULL);
349
350     popup_free(popup);
351     popup = NULL;
352 }
353