]> Dogcows Code - chaz/openbox/blob - openbox/keyboard.c
86d4f4324e1c58c9ecc56ac4298068077c3df15c
[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-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()
110 {
111 if (curpos)
112 set_curpos(NULL);
113 }
114
115 void keyboard_unbind_all()
116 {
117 tree_destroy(keyboard_firstnode);
118 keyboard_firstnode = NULL;
119 }
120
121 gboolean keyboard_bind(GList *keylist, ObAction *action)
122 {
123 KeyBindingTree *tree, *t;
124 gboolean conflict;
125 gboolean mods = TRUE;
126
127 g_assert(keylist != NULL);
128 g_assert(action != NULL);
129
130 if (!(tree = tree_build(keylist)))
131 return FALSE;
132
133 if ((t = tree_find(tree, &conflict)) != NULL) {
134 /* already bound to something, use the existing tree */
135 tree_destroy(tree);
136 tree = NULL;
137 } else
138 t = tree;
139
140 if (conflict) {
141 g_message(_("Conflict with key binding in config file"));
142 tree_destroy(tree);
143 return FALSE;
144 }
145
146 /* find if every key in this chain has modifiers, and also find the
147 bottom node of the tree */
148 while (t->first_child) {
149 if (!t->state)
150 mods = FALSE;
151 t = t->first_child;
152 }
153
154 /* when there are no modifiers in the binding, then the action cannot
155 be interactive */
156 if (!mods && action->data.any.interactive) {
157 action->data.any.interactive = FALSE;
158 action->data.inter.final = TRUE;
159 }
160
161 /* set the action */
162 t->actions = g_slist_append(t->actions, action);
163 /* assimilate this built tree into the main tree. assimilation
164 destroys/uses the tree */
165 if (tree) tree_assimilate(tree);
166
167 return TRUE;
168 }
169
170 gboolean keyboard_interactive_grab(guint state, ObClient *client,
171 ObAction *action)
172 {
173 ObInteractiveState *s;
174
175 g_assert(action->data.any.interactive);
176
177 if (!interactive_states) {
178 grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER);
179 if (!grab_keyboard(TRUE)) {
180 grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
181 return FALSE;
182 }
183 }
184
185 s = g_new(ObInteractiveState, 1);
186
187 s->state = state;
188 s->client = client;
189 s->actions = g_slist_append(NULL, action);
190
191 interactive_states = g_slist_append(interactive_states, s);
192
193 return TRUE;
194 }
195
196 void keyboard_interactive_end(ObInteractiveState *s,
197 guint state, gboolean cancel, Time time)
198 {
199 action_run_interactive(s->actions, s->client, state, time, cancel, TRUE);
200
201 g_slist_free(s->actions);
202 g_free(s);
203
204 interactive_states = g_slist_remove(interactive_states, s);
205
206 if (!interactive_states) {
207 grab_keyboard(FALSE);
208 grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
209 keyboard_reset_chains();
210 }
211 }
212
213 void keyboard_interactive_end_client(ObClient *client, gpointer data)
214 {
215 GSList *it, *next;
216
217 for (it = interactive_states; it; it = next) {
218 ObInteractiveState *s = it->data;
219
220 next = g_slist_next(it);
221
222 if (s->client == client)
223 s->client = NULL;
224 }
225 }
226
227 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
228 {
229 GSList *it, *next;
230 gboolean handled = FALSE;
231 gboolean done = FALSE;
232 gboolean cancel = FALSE;
233
234 for (it = interactive_states; it; it = next) {
235 ObInteractiveState *s = it->data;
236
237 next = g_slist_next(it);
238
239 if ((e->type == KeyRelease &&
240 !(s->state & e->xkey.state)))
241 done = TRUE;
242 else if (e->type == KeyPress) {
243 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
244 done = TRUE;
245 else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
246 cancel = done = TRUE;
247 } else if (e->type == ButtonPress)
248 cancel = done = TRUE;
249
250 if (done) {
251 keyboard_interactive_end(s, e->xkey.state, cancel, e->xkey.time);
252
253 handled = TRUE;
254 } else
255 *client = s->client;
256 }
257
258 return handled;
259 }
260
261 void keyboard_event(ObClient *client, const XEvent *e)
262 {
263 KeyBindingTree *p;
264
265 g_assert(e->type == KeyPress);
266
267 if (e->xkey.keycode == config_keyboard_reset_keycode &&
268 e->xkey.state == config_keyboard_reset_state)
269 {
270 keyboard_reset_chains();
271 return;
272 }
273
274 if (curpos == NULL)
275 p = keyboard_firstnode;
276 else
277 p = curpos->first_child;
278 while (p) {
279 if (p->key == e->xkey.keycode &&
280 p->state == e->xkey.state)
281 {
282 if (p->first_child != NULL) { /* part of a chain */
283 set_curpos(p);
284 } else {
285
286 keyboard_reset_chains();
287
288 action_run_key(p->actions, client, e->xkey.state,
289 e->xkey.x_root, e->xkey.y_root,
290 e->xkey.time);
291 }
292 break;
293 }
294 p = p->next_sibling;
295 }
296 }
297
298 gboolean keyboard_interactively_grabbed()
299 {
300 return !!interactive_states;
301 }
302
303 void keyboard_startup(gboolean reconfig)
304 {
305 grab_keys(TRUE);
306 popup = popup_new(FALSE);
307
308 if (!reconfig)
309 client_add_destructor(keyboard_interactive_end_client, NULL);
310 }
311
312 void keyboard_shutdown(gboolean reconfig)
313 {
314 GSList *it;
315
316 if (!reconfig)
317 client_remove_destructor(keyboard_interactive_end_client);
318
319 for (it = interactive_states; it; it = g_slist_next(it))
320 g_free(it->data);
321 g_slist_free(interactive_states);
322 interactive_states = NULL;
323
324 ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
325
326 keyboard_unbind_all();
327 set_curpos(NULL);
328
329 popup_free(popup);
330 popup = NULL;
331 }
332
This page took 0.046943 seconds and 3 git commands to generate.