no tabs
[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) 2003        Ben Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "mainloop.h"
20 #include "focus.h"
21 #include "screen.h"
22 #include "frame.h"
23 #include "openbox.h"
24 #include "event.h"
25 #include "grab.h"
26 #include "client.h"
27 #include "action.h"
28 #include "prop.h"
29 #include "config.h"
30 #include "keytree.h"
31 #include "keyboard.h"
32 #include "translate.h"
33 #include "moveresize.h"
34
35 #include <glib.h>
36
37 KeyBindingTree *keyboard_firstnode;
38
39 typedef struct {
40     guint state;
41     ObClient *client;
42     ObAction *action;
43     ObFrameContext context;
44 } ObInteractiveState;
45
46 static GSList *interactive_states;
47
48 static KeyBindingTree *curpos;
49
50 static void grab_for_window(Window win, gboolean grab)
51 {
52     KeyBindingTree *p;
53
54     ungrab_all_keys(win);
55
56     if (grab) {
57         p = curpos ? curpos->first_child : keyboard_firstnode;
58         while (p) {
59             grab_key(p->key, p->state, win, GrabModeAsync);
60             p = p->next_sibling;
61         }
62         if (curpos)
63             grab_key(config_keyboard_reset_keycode,
64                      config_keyboard_reset_state,
65                      win, GrabModeAsync);
66     }
67 }
68
69 void keyboard_grab_for_client(ObClient *c, gboolean grab)
70 {
71     grab_for_window(c->window, grab);
72 }
73
74 static void grab_keys(gboolean grab)
75 {
76     GList *it;
77
78     grab_for_window(screen_support_win, grab);
79     for (it = client_list; it; it = g_list_next(it))
80         grab_for_window(((ObClient*)it->data)->window, grab);
81 }
82
83 static gboolean chain_timeout(gpointer data)
84 {
85     keyboard_reset_chains();
86
87     return FALSE; /* don't repeat */
88 }
89
90 void keyboard_reset_chains()
91 {
92     ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
93
94     if (curpos) {
95         grab_keys(FALSE);
96         curpos = NULL;
97         grab_keys(TRUE);
98     }
99 }
100
101 gboolean keyboard_bind(GList *keylist, ObAction *action)
102 {
103     KeyBindingTree *tree, *t;
104     gboolean conflict;
105     gboolean mods = TRUE;
106
107     g_assert(keylist != NULL);
108     g_assert(action != NULL);
109
110     if (!(tree = tree_build(keylist)))
111         return FALSE;
112
113     if ((t = tree_find(tree, &conflict)) != NULL) {
114         /* already bound to something, use the existing tree */
115         tree_destroy(tree);
116         tree = NULL;
117     } else
118         t = tree;
119
120     if (conflict) {
121         g_warning("conflict with binding");
122         tree_destroy(tree);
123         return FALSE;
124     }
125
126     /* find if every key in this chain has modifiers, and also find the
127        bottom node of the tree */
128     while (t->first_child) {
129         if (!t->state)
130             mods = FALSE;
131         t = t->first_child;
132     }
133
134     /* when there are no modifiers in the binding, then the action cannot
135        be interactive */
136     if (!mods && action->data.any.interactive) {
137         action->data.any.interactive = FALSE;
138         action->data.inter.final = TRUE;
139     }
140
141     /* set the action */
142     t->actions = g_slist_append(t->actions, action);
143     /* assimilate this built tree into the main tree. assimilation
144        destroys/uses the tree */
145     if (tree) tree_assimilate(tree);
146
147     return TRUE;
148 }
149
150 void keyboard_interactive_grab(guint state, ObClient *client,
151                                ObAction *action)
152 {
153     ObInteractiveState *s;
154
155     g_assert(action->data.any.interactive);
156
157     if (moveresize_in_progress)
158         moveresize_end(FALSE);
159
160     if (!interactive_states) {
161         if (!grab_keyboard(TRUE))
162             return;
163         if (!grab_pointer(TRUE, OB_CURSOR_NONE)) {
164             grab_keyboard(FALSE);
165             return;
166         }
167     }
168
169     s = g_new(ObInteractiveState, 1);
170
171     s->state = state;
172     s->client = client;
173     s->action = action;
174
175     interactive_states = g_slist_append(interactive_states, s);
176 }
177
178 void keyboard_interactive_end(ObInteractiveState *s,
179                               guint state, gboolean cancel)
180 {
181     action_run_interactive(s->action, s->client, state, cancel, TRUE);
182
183     g_free(s);
184
185     interactive_states = g_slist_remove(interactive_states, s);
186
187     if (!interactive_states) {
188         grab_keyboard(FALSE);
189         grab_pointer(FALSE, OB_CURSOR_NONE);
190         keyboard_reset_chains();
191     }
192 }
193
194 void keyboard_interactive_end_client(gpointer data)
195 {
196     GSList *it, *next;
197     ObClient *c = data;
198
199     for (it = interactive_states; it; it = next) {
200         ObInteractiveState *s = it->data;
201
202         next = g_slist_next(it);
203
204         if (s->client == c)
205             keyboard_interactive_end(s, 0, FALSE);
206     }
207 }
208
209 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
210 {
211     GSList *it, *next;
212     gboolean handled = FALSE;
213     gboolean done = FALSE;
214     gboolean cancel = FALSE;
215
216     for (it = interactive_states; it; it = next) {
217         ObInteractiveState *s = it->data;
218
219         next = g_slist_next(it);
220         
221         if ((e->type == KeyRelease && 
222              !(s->state & e->xkey.state)))
223             done = TRUE;
224         else if (e->type == KeyPress) {
225             if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
226                 done = TRUE;
227             else if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
228                 cancel = done = TRUE;
229         }
230         if (done) {
231             keyboard_interactive_end(s, e->xkey.state, cancel);
232
233             handled = TRUE;
234         } else
235             *client = s->client;
236     }
237
238     return handled;
239 }
240
241 void keyboard_event(ObClient *client, const XEvent *e)
242 {
243     KeyBindingTree *p;
244
245     g_assert(e->type == KeyPress);
246
247     if (e->xkey.keycode == config_keyboard_reset_keycode &&
248         e->xkey.state == config_keyboard_reset_state)
249     {
250         keyboard_reset_chains();
251         return;
252     }
253
254     if (curpos == NULL)
255         p = keyboard_firstnode;
256     else
257         p = curpos->first_child;
258     while (p) {
259         if (p->key == e->xkey.keycode &&
260             p->state == e->xkey.state)
261         {
262             if (p->first_child != NULL) { /* part of a chain */
263                 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
264                 /* 5 second timeout for chains */
265                 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
266                                          chain_timeout, NULL, NULL);
267                 grab_keys(FALSE);
268                 curpos = p;
269                 grab_keys(TRUE);
270             } else {
271                 GSList *it;
272
273                 for (it = p->actions; it; it = it->next)
274                     action_run_key(it->data, client, e->xkey.state,
275                                    e->xkey.x_root, e->xkey.y_root);
276
277                 keyboard_reset_chains();
278             }
279             break;
280         }
281         p = p->next_sibling;
282     }
283 }
284
285 void keyboard_startup(gboolean reconfig)
286 {
287     grab_keys(TRUE);
288
289     if (!reconfig)
290         client_add_destructor(keyboard_interactive_end_client);
291 }
292
293 void keyboard_shutdown(gboolean reconfig)
294 {
295     GSList *it;
296
297     if (!reconfig)
298         client_remove_destructor(keyboard_interactive_end_client);
299
300     tree_destroy(keyboard_firstnode);
301     keyboard_firstnode = NULL;
302
303     for (it = interactive_states; it; it = g_slist_next(it))
304         g_free(it->data);
305     g_slist_free(interactive_states);
306     interactive_states = NULL;
307
308     ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
309     grab_keys(FALSE);
310     curpos = NULL;
311 }
312