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