]> Dogcows Code - chaz/openbox/blob - openbox/keyboard.c
change the "handle" context to "bottom". add a "top" context. make the top
[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 /* ungrab first so they won't be NotifyWhileGrabbed */
199 if (ungrab)
200 grab_keyboard(FALSE);
201
202 alist = g_slist_append(NULL, istate.action);
203 action_run_interactive(alist, istate.client, state, time, cancel, TRUE);
204 g_slist_free(alist);
205
206 istate.active = FALSE;
207 }
208
209 static void keyboard_interactive_end_client(ObClient *client, gpointer data)
210 {
211 if (istate.active && istate.client == client)
212 istate.client = NULL;
213 }
214
215 gboolean keyboard_interactive_grab(guint state, ObClient *client,
216 ObAction *action)
217 {
218 g_assert(action->data.any.interactive);
219
220 if (!istate.active) {
221 if (!grab_keyboard(TRUE))
222 return FALSE;
223 } else if (action->func != istate.action->func) {
224 keyboard_interactive_end(state, FALSE, action->data.any.time, FALSE);
225 }
226
227 istate.active = TRUE;
228 istate.state = state;
229 istate.client = client;
230 istate.action = action;
231
232 return TRUE;
233 }
234
235 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
236 {
237 gboolean handled = FALSE;
238 gboolean done = FALSE;
239 gboolean cancel = FALSE;
240
241 if (istate.active) {
242 if ((e->type == KeyRelease && !(istate.state & e->xkey.state))) {
243 done = TRUE;
244 handled = TRUE;
245 } else if (e->type == KeyPress) {
246 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
247 done = TRUE;
248 else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
249 cancel = done = TRUE;
250 handled = TRUE;
251 }
252 } else if (e->type == ButtonPress) {
253 cancel = TRUE;
254 done = TRUE;
255 handled = FALSE;
256 }
257
258 if (done)
259 keyboard_interactive_end(e->xkey.state, cancel, e->xkey.time,TRUE);
260
261 if (handled)
262 *client = istate.client;
263 }
264
265 return handled;
266 }
267
268 void keyboard_event(ObClient *client, const XEvent *e)
269 {
270 KeyBindingTree *p;
271
272 g_assert(e->type == KeyPress);
273
274 if (e->xkey.keycode == config_keyboard_reset_keycode &&
275 e->xkey.state == config_keyboard_reset_state)
276 {
277 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
278 keyboard_reset_chains(-1);
279 return;
280 }
281
282 if (curpos == NULL)
283 p = keyboard_firstnode;
284 else
285 p = curpos->first_child;
286 while (p) {
287 if (p->key == e->xkey.keycode &&
288 p->state == e->xkey.state)
289 {
290 /* if we hit a key binding, then close any open menus and run it */
291 if (menu_frame_visible)
292 menu_frame_hide_all();
293
294 if (p->first_child != NULL) { /* part of a chain */
295 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
296 /* 3 second timeout for chains */
297 ob_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
298 chain_timeout, NULL,
299 g_direct_equal, NULL);
300 set_curpos(p);
301 } else if (p->chroot) /* an empty chroot */
302 set_curpos(p);
303 else {
304 keyboard_reset_chains(0);
305
306 action_run_key(p->actions, client, e->xkey.state,
307 e->xkey.x_root, e->xkey.y_root,
308 e->xkey.time);
309 }
310 break;
311 }
312 p = p->next_sibling;
313 }
314 }
315
316 gboolean keyboard_interactively_grabbed()
317 {
318 return istate.active;
319 }
320
321 void keyboard_startup(gboolean reconfig)
322 {
323 grab_keys(TRUE);
324 popup = popup_new(FALSE);
325
326 if (!reconfig)
327 client_add_destructor(keyboard_interactive_end_client, NULL);
328 }
329
330 void keyboard_shutdown(gboolean reconfig)
331 {
332 if (!reconfig)
333 client_remove_destructor(keyboard_interactive_end_client);
334
335 if (istate.active)
336 keyboard_interactive_end(0, TRUE, 0, TRUE);
337
338 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
339
340 keyboard_unbind_all();
341 set_curpos(NULL);
342
343 popup_free(popup);
344 popup = NULL;
345 }
346
This page took 0.052141 seconds and 4 git commands to generate.