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