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