]> icculus.org git repositories - manmower/openbox.git/blob - openbox/keyboard.c
1. remove some old event handling that was not used at all. yay less bandwidth.
[manmower/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 "gettext.h"
36
37 #include <glib.h>
38
39 KeyBindingTree *keyboard_firstnode;
40
41 typedef struct {
42     guint state;
43     ObClient *client;
44     GSList *actions;
45     ObFrameContext context;
46 } ObInteractiveState;
47
48 static GSList *interactive_states;
49
50 static KeyBindingTree *curpos;
51
52 static void grab_for_window(Window win, gboolean grab)
53 {
54     KeyBindingTree *p;
55
56     ungrab_all_keys(win);
57
58     if (grab) {
59         p = curpos ? curpos->first_child : keyboard_firstnode;
60         while (p) {
61             grab_key(p->key, p->state, win, GrabModeAsync);
62             p = p->next_sibling;
63         }
64         if (curpos)
65             grab_key(config_keyboard_reset_keycode,
66                      config_keyboard_reset_state,
67                      win, GrabModeAsync);
68     }
69 }
70
71 void keyboard_grab_for_client(ObClient *c, gboolean grab)
72 {
73     grab_for_window(c->window, grab);
74 }
75
76 static void grab_keys(gboolean grab)
77 {
78     GList *it;
79
80     grab_for_window(screen_support_win, grab);
81     for (it = client_list; it; it = g_list_next(it))
82         grab_for_window(((ObClient*)it->data)->window, grab);
83 }
84
85 static gboolean chain_timeout(gpointer data)
86 {
87     keyboard_reset_chains();
88
89     return FALSE; /* don't repeat */
90 }
91
92 void keyboard_reset_chains()
93 {
94     ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
95
96     if (curpos) {
97         grab_keys(FALSE);
98         curpos = NULL;
99         grab_keys(TRUE);
100     }
101 }
102
103 void keyboard_unbind_all()
104 {
105     tree_destroy(keyboard_firstnode);
106     keyboard_firstnode = NULL;
107     grab_keys(FALSE);
108     curpos = NULL;
109 }
110
111 gboolean keyboard_bind(GList *keylist, ObAction *action)
112 {
113     KeyBindingTree *tree, *t;
114     gboolean conflict;
115     gboolean mods = TRUE;
116
117     g_assert(keylist != NULL);
118     g_assert(action != NULL);
119
120     if (!(tree = tree_build(keylist)))
121         return FALSE;
122
123     if ((t = tree_find(tree, &conflict)) != NULL) {
124         /* already bound to something, use the existing tree */
125         tree_destroy(tree);
126         tree = NULL;
127     } else
128         t = tree;
129
130     if (conflict) {
131         g_message(_("Conflict with key binding in config file"));
132         tree_destroy(tree);
133         return FALSE;
134     }
135
136     /* find if every key in this chain has modifiers, and also find the
137        bottom node of the tree */
138     while (t->first_child) {
139         if (!t->state)
140             mods = FALSE;
141         t = t->first_child;
142     }
143
144     /* when there are no modifiers in the binding, then the action cannot
145        be interactive */
146     if (!mods && action->data.any.interactive) {
147         action->data.any.interactive = FALSE;
148         action->data.inter.final = TRUE;
149     }
150
151     /* set the action */
152     t->actions = g_slist_append(t->actions, action);
153     /* assimilate this built tree into the main tree. assimilation
154        destroys/uses the tree */
155     if (tree) tree_assimilate(tree);
156
157     return TRUE;
158 }
159
160 gboolean keyboard_interactive_grab(guint state, ObClient *client,
161                                    ObAction *action)
162 {
163     ObInteractiveState *s;
164
165     g_assert(action->data.any.interactive);
166
167     if (!interactive_states) {
168         if (!grab_keyboard(TRUE))
169             return FALSE;
170     }
171
172     s = g_new(ObInteractiveState, 1);
173
174     s->state = state;
175     s->client = client;
176     s->actions = g_slist_append(NULL, action);
177
178     interactive_states = g_slist_append(interactive_states, s);
179
180     return TRUE;
181 }
182
183 void keyboard_interactive_end(ObInteractiveState *s,
184                               guint state, gboolean cancel, Time time)
185 {
186     action_run_interactive(s->actions, s->client, state, time, cancel, TRUE);
187
188     g_slist_free(s->actions);
189     g_free(s);
190
191     interactive_states = g_slist_remove(interactive_states, s);
192
193     if (!interactive_states) {
194         grab_keyboard(FALSE);
195         keyboard_reset_chains();
196     }
197 }
198
199 void keyboard_interactive_end_client(ObClient *client, gpointer data)
200 {
201     GSList *it, *next;
202
203     for (it = interactive_states; it; it = next) {
204         ObInteractiveState *s = it->data;
205
206         next = g_slist_next(it);
207
208         if (s->client == client)
209             s->client = NULL;
210     }
211 }
212
213 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
214 {
215     GSList *it, *next;
216     gboolean handled = FALSE;
217     gboolean done = FALSE;
218     gboolean cancel = FALSE;
219
220     for (it = interactive_states; it; it = next) {
221         ObInteractiveState *s = it->data;
222
223         next = g_slist_next(it);
224         
225         if ((e->type == KeyRelease && 
226              !(s->state & e->xkey.state)))
227             done = TRUE;
228         else if (e->type == KeyPress) {
229             /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
230                 done = TRUE;
231             else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
232                 cancel = done = TRUE;
233         }
234         if (done) {
235             keyboard_interactive_end(s, e->xkey.state, cancel, e->xkey.time);
236
237             handled = TRUE;
238         } else
239             *client = s->client;
240     }
241
242     return handled;
243 }
244
245 void keyboard_event(ObClient *client, const XEvent *e)
246 {
247     KeyBindingTree *p;
248
249     g_assert(e->type == KeyPress);
250
251     if (e->xkey.keycode == config_keyboard_reset_keycode &&
252         e->xkey.state == config_keyboard_reset_state)
253     {
254         keyboard_reset_chains();
255         return;
256     }
257
258     if (curpos == NULL)
259         p = keyboard_firstnode;
260     else
261         p = curpos->first_child;
262     while (p) {
263         if (p->key == e->xkey.keycode &&
264             p->state == e->xkey.state)
265         {
266             if (p->first_child != NULL) { /* part of a chain */
267                 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
268                 /* 5 second timeout for chains */
269                 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
270                                          chain_timeout, NULL,
271                                          g_direct_equal, NULL);
272                 grab_keys(FALSE);
273                 curpos = p;
274                 grab_keys(TRUE);
275             } else {
276
277                 keyboard_reset_chains();
278
279                 action_run_key(p->actions, client, e->xkey.state,
280                                e->xkey.x_root, e->xkey.y_root,
281                                e->xkey.time);
282             }
283             break;
284         }
285         p = p->next_sibling;
286     }
287 }
288
289 gboolean keyboard_interactively_grabbed()
290 {
291     return !!interactive_states;
292 }
293
294 void keyboard_startup(gboolean reconfig)
295 {
296     grab_keys(TRUE);
297
298     if (!reconfig)
299         client_add_destructor(keyboard_interactive_end_client, NULL);
300 }
301
302 void keyboard_shutdown(gboolean reconfig)
303 {
304     GSList *it;
305
306     if (!reconfig)
307         client_remove_destructor(keyboard_interactive_end_client);
308
309     for (it = interactive_states; it; it = g_slist_next(it))
310         g_free(it->data);
311     g_slist_free(interactive_states);
312     interactive_states = NULL;
313
314     ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
315
316     keyboard_unbind_all();
317 }
318