]> Dogcows Code - chaz/openbox/blob - openbox/keyboard.c
provide a way to remove all bindings
[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) 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 void keyboard_unbind_all()
102 {
103 tree_destroy(keyboard_firstnode);
104 keyboard_firstnode = NULL;
105 grab_keys(FALSE);
106 curpos = NULL;
107 }
108
109 gboolean keyboard_bind(GList *keylist, ObAction *action)
110 {
111 KeyBindingTree *tree, *t;
112 gboolean conflict;
113 gboolean mods = TRUE;
114
115 g_assert(keylist != NULL);
116 g_assert(action != NULL);
117
118 if (!(tree = tree_build(keylist)))
119 return FALSE;
120
121 if ((t = tree_find(tree, &conflict)) != NULL) {
122 /* already bound to something, use the existing tree */
123 tree_destroy(tree);
124 tree = NULL;
125 } else
126 t = tree;
127
128 if (conflict) {
129 g_warning("conflict with binding");
130 tree_destroy(tree);
131 return FALSE;
132 }
133
134 /* find if every key in this chain has modifiers, and also find the
135 bottom node of the tree */
136 while (t->first_child) {
137 if (!t->state)
138 mods = FALSE;
139 t = t->first_child;
140 }
141
142 /* when there are no modifiers in the binding, then the action cannot
143 be interactive */
144 if (!mods && action->data.any.interactive) {
145 action->data.any.interactive = FALSE;
146 action->data.inter.final = TRUE;
147 }
148
149 /* set the action */
150 t->actions = g_slist_append(t->actions, action);
151 /* assimilate this built tree into the main tree. assimilation
152 destroys/uses the tree */
153 if (tree) tree_assimilate(tree);
154
155 return TRUE;
156 }
157
158 void keyboard_interactive_grab(guint state, ObClient *client,
159 ObAction *action)
160 {
161 ObInteractiveState *s;
162
163 g_assert(action->data.any.interactive);
164
165 if (moveresize_in_progress)
166 moveresize_end(FALSE);
167
168 if (!interactive_states) {
169 if (!grab_keyboard(TRUE))
170 return;
171 if (!grab_pointer(TRUE, OB_CURSOR_NONE)) {
172 grab_keyboard(FALSE);
173 return;
174 }
175 }
176
177 s = g_new(ObInteractiveState, 1);
178
179 s->state = state;
180 s->client = client;
181 s->action = action;
182
183 interactive_states = g_slist_append(interactive_states, s);
184 }
185
186 void keyboard_interactive_end(ObInteractiveState *s,
187 guint state, gboolean cancel)
188 {
189 action_run_interactive(s->action, s->client, state, cancel, TRUE);
190
191 g_free(s);
192
193 interactive_states = g_slist_remove(interactive_states, s);
194
195 if (!interactive_states) {
196 grab_keyboard(FALSE);
197 grab_pointer(FALSE, OB_CURSOR_NONE);
198 keyboard_reset_chains();
199 }
200 }
201
202 void keyboard_interactive_end_client(gpointer data)
203 {
204 GSList *it, *next;
205 ObClient *c = data;
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 == c)
213 keyboard_interactive_end(s, 0, FALSE);
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);
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, NULL);
275 grab_keys(FALSE);
276 curpos = p;
277 grab_keys(TRUE);
278 } else {
279 GSList *it;
280
281 for (it = p->actions; it; it = it->next)
282 action_run_key(it->data, client, e->xkey.state,
283 e->xkey.x_root, e->xkey.y_root);
284
285 keyboard_reset_chains();
286 }
287 break;
288 }
289 p = p->next_sibling;
290 }
291 }
292
293 void keyboard_startup(gboolean reconfig)
294 {
295 grab_keys(TRUE);
296
297 if (!reconfig)
298 client_add_destructor(keyboard_interactive_end_client);
299 }
300
301 void keyboard_shutdown(gboolean reconfig)
302 {
303 GSList *it;
304
305 if (!reconfig)
306 client_remove_destructor(keyboard_interactive_end_client);
307
308 for (it = interactive_states; it; it = g_slist_next(it))
309 g_free(it->data);
310 g_slist_free(interactive_states);
311 interactive_states = NULL;
312
313 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
314
315 keyboard_unbind_all();
316 }
317
This page took 0.04624 seconds and 5 git commands to generate.