]> Dogcows Code - chaz/openbox/blob - openbox/keyboard.c
better menu keyboard handling.
[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 "menuframe.h"
31 #include "config.h"
32 #include "keytree.h"
33 #include "keyboard.h"
34 #include "translate.h"
35 #include "moveresize.h"
36 #include "popup.h"
37 #include "gettext.h"
38
39 #include <glib.h>
40
41 typedef struct {
42 guint state;
43 ObClient *client;
44 GSList *actions;
45 ObFrameContext context;
46 } ObInteractiveState;
47
48 KeyBindingTree *keyboard_firstnode = NULL;
49 static ObPopup *popup = NULL;
50 static GSList *interactive_states;
51 static KeyBindingTree *curpos;
52
53 static void grab_keys(gboolean grab)
54 {
55 KeyBindingTree *p;
56
57 ungrab_all_keys(RootWindow(ob_display, ob_screen));
58
59 if (grab) {
60 p = curpos ? curpos->first_child : keyboard_firstnode;
61 while (p) {
62 grab_key(p->key, p->state, RootWindow(ob_display, ob_screen),
63 GrabModeAsync);
64 p = p->next_sibling;
65 }
66 if (curpos)
67 grab_key(config_keyboard_reset_keycode,
68 config_keyboard_reset_state,
69 RootWindow(ob_display, ob_screen), GrabModeAsync);
70 }
71 }
72
73 static gboolean chain_timeout(gpointer data)
74 {
75 keyboard_reset_chains(0);
76 return FALSE; /* don't repeat */
77 }
78
79 static void set_curpos(KeyBindingTree *newpos)
80 {
81 grab_keys(FALSE);
82 curpos = newpos;
83 grab_keys(TRUE);
84
85 if (curpos != NULL) {
86 gchar *text = NULL;
87 GList *it;
88
89 for (it = curpos->keylist; it; it = g_list_next(it)) {
90 gchar *oldtext = text;
91 if (text == NULL)
92 text = g_strdup(it->data);
93 else
94 text = g_strconcat(text, " - ", it->data, NULL);
95 g_free(oldtext);
96 }
97
98 popup_position(popup, NorthWestGravity, 10, 10);
99 /* 1 second delay for the popup to show */
100 popup_delay_show(popup, G_USEC_PER_SEC, text);
101 g_free(text);
102 } else {
103 popup_hide(popup);
104 }
105 }
106
107 void keyboard_reset_chains(gint break_chroots)
108 {
109 KeyBindingTree *p;
110
111 for (p = curpos; p; p = p->parent) {
112 if (p->chroot) {
113 if (break_chroots == 0) break; /* stop here */
114 if (break_chroots > 0)
115 --break_chroots;
116 }
117 }
118 set_curpos(p);
119 }
120
121 void keyboard_unbind_all()
122 {
123 tree_destroy(keyboard_firstnode);
124 keyboard_firstnode = NULL;
125 }
126
127 void keyboard_chroot(GList *keylist)
128 {
129 /* try do it in the existing tree. if we can't that means it is an empty
130 chroot binding. so add it to the tree then. */
131 if (!tree_chroot(keyboard_firstnode, keylist)) {
132 KeyBindingTree *tree;
133 if (!(tree = tree_build(keylist)))
134 return;
135 tree_chroot(tree, keylist);
136 tree_assimilate(tree);
137 }
138 }
139
140 gboolean keyboard_bind(GList *keylist, ObAction *action)
141 {
142 KeyBindingTree *tree, *t;
143 gboolean conflict;
144 gboolean mods = TRUE;
145
146 g_assert(keylist != NULL);
147 g_assert(action != NULL);
148
149 if (!(tree = tree_build(keylist)))
150 return FALSE;
151
152 if ((t = tree_find(tree, &conflict)) != NULL) {
153 /* already bound to something, use the existing tree */
154 tree_destroy(tree);
155 tree = NULL;
156 } else
157 t = tree;
158
159 if (conflict) {
160 g_message(_("Conflict with key binding in config file"));
161 tree_destroy(tree);
162 return FALSE;
163 }
164
165 /* find if every key in this chain has modifiers, and also find the
166 bottom node of the tree */
167 while (t->first_child) {
168 if (!t->state)
169 mods = FALSE;
170 t = t->first_child;
171 }
172
173 /* when there are no modifiers in the binding, then the action cannot
174 be interactive */
175 if (!mods && action->data.any.interactive) {
176 action->data.any.interactive = FALSE;
177 action->data.inter.final = TRUE;
178 }
179
180 /* set the action */
181 t->actions = g_slist_append(t->actions, action);
182 /* assimilate this built tree into the main tree. assimilation
183 destroys/uses the tree */
184 if (tree) tree_assimilate(tree);
185
186 return TRUE;
187 }
188
189 gboolean keyboard_interactive_grab(guint state, ObClient *client,
190 ObAction *action)
191 {
192 ObInteractiveState *s;
193
194 g_assert(action->data.any.interactive);
195
196 if (!interactive_states) {
197 grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER);
198 if (!grab_keyboard(TRUE)) {
199 grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
200 return FALSE;
201 }
202 }
203
204 s = g_new(ObInteractiveState, 1);
205
206 s->state = state;
207 s->client = client;
208 s->actions = g_slist_append(NULL, action);
209
210 interactive_states = g_slist_append(interactive_states, s);
211
212 return TRUE;
213 }
214
215 void keyboard_interactive_end(ObInteractiveState *s,
216 guint state, gboolean cancel, Time time)
217 {
218 action_run_interactive(s->actions, s->client, state, time, cancel, TRUE);
219
220 g_slist_free(s->actions);
221 g_free(s);
222
223 interactive_states = g_slist_remove(interactive_states, s);
224
225 if (!interactive_states) {
226 grab_keyboard(FALSE);
227 grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
228 }
229 }
230
231 void keyboard_interactive_end_client(ObClient *client, gpointer data)
232 {
233 GSList *it, *next;
234
235 for (it = interactive_states; it; it = next) {
236 ObInteractiveState *s = it->data;
237
238 next = g_slist_next(it);
239
240 if (s->client == client)
241 s->client = NULL;
242 }
243 }
244
245 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
246 {
247 GSList *it, *next;
248 gboolean handled = FALSE;
249 gboolean done = FALSE;
250 gboolean cancel = FALSE;
251
252 for (it = interactive_states; it; it = next) {
253 ObInteractiveState *s = it->data;
254
255 next = g_slist_next(it);
256
257 if ((e->type == KeyRelease &&
258 !(s->state & e->xkey.state)))
259 done = TRUE;
260 else if (e->type == KeyPress) {
261 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
262 done = TRUE;
263 else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
264 cancel = done = TRUE;
265 } else if (e->type == ButtonPress)
266 cancel = done = TRUE;
267
268 if (done) {
269 keyboard_interactive_end(s, e->xkey.state, cancel, e->xkey.time);
270
271 handled = TRUE;
272 } else
273 *client = s->client;
274 }
275
276 return handled;
277 }
278
279 void keyboard_event(ObClient *client, const XEvent *e)
280 {
281 KeyBindingTree *p;
282
283 g_assert(e->type == KeyPress);
284
285 if (e->xkey.keycode == config_keyboard_reset_keycode &&
286 e->xkey.state == config_keyboard_reset_state)
287 {
288 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
289 keyboard_reset_chains(-1);
290 return;
291 }
292
293 if (curpos == NULL)
294 p = keyboard_firstnode;
295 else
296 p = curpos->first_child;
297 while (p) {
298 if (p->key == e->xkey.keycode &&
299 p->state == e->xkey.state)
300 {
301 /* if we hit a key binding, then close any open menus and run it */
302 if (menu_frame_visible)
303 menu_frame_hide_all();
304
305 if (p->first_child != NULL) { /* part of a chain */
306 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
307 /* 3 second timeout for chains */
308 ob_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
309 chain_timeout, NULL,
310 g_direct_equal, NULL);
311 set_curpos(p);
312 } else if (p->chroot) /* an empty chroot */
313 set_curpos(p);
314 else {
315 keyboard_reset_chains(0);
316
317 action_run_key(p->actions, client, e->xkey.state,
318 e->xkey.x_root, e->xkey.y_root,
319 e->xkey.time);
320 }
321 break;
322 }
323 p = p->next_sibling;
324 }
325 }
326
327 gboolean keyboard_interactively_grabbed()
328 {
329 return !!interactive_states;
330 }
331
332 void keyboard_startup(gboolean reconfig)
333 {
334 grab_keys(TRUE);
335 popup = popup_new(FALSE);
336
337 if (!reconfig)
338 client_add_destructor(keyboard_interactive_end_client, NULL);
339 }
340
341 void keyboard_shutdown(gboolean reconfig)
342 {
343 GSList *it;
344
345 if (!reconfig)
346 client_remove_destructor(keyboard_interactive_end_client);
347
348 for (it = interactive_states; it; it = g_slist_next(it))
349 g_free(it->data);
350 g_slist_free(interactive_states);
351 interactive_states = NULL;
352
353 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
354
355 keyboard_unbind_all();
356 set_curpos(NULL);
357
358 popup_free(popup);
359 popup = NULL;
360 }
361
This page took 0.048822 seconds and 5 git commands to generate.