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