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