]> Dogcows Code - chaz/openbox/blob - openbox/keyboard.c
add a popup dialog that shows where you are in a keychain
[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 "config.h"
31 #include "keytree.h"
32 #include "keyboard.h"
33 #include "translate.h"
34 #include "moveresize.h"
35 #include "popup.h"
36 #include "gettext.h"
37
38 #include <glib.h>
39
40 typedef struct {
41 guint state;
42 ObClient *client;
43 GSList *actions;
44 ObFrameContext context;
45 } ObInteractiveState;
46
47 KeyBindingTree *keyboard_firstnode = NULL;
48 static ObPopup *popup = NULL;
49 static GSList *interactive_states;
50 static KeyBindingTree *curpos;
51
52 static void grab_keys(gboolean grab)
53 {
54 KeyBindingTree *p;
55
56 ungrab_all_keys(RootWindow(ob_display, ob_screen));
57
58 if (grab) {
59 p = curpos ? curpos->first_child : keyboard_firstnode;
60 while (p) {
61 grab_key(p->key, p->state, RootWindow(ob_display, ob_screen),
62 GrabModeAsync);
63 p = p->next_sibling;
64 }
65 if (curpos)
66 grab_key(config_keyboard_reset_keycode,
67 config_keyboard_reset_state,
68 RootWindow(ob_display, ob_screen), GrabModeAsync);
69 }
70 }
71
72 static gboolean chain_timeout(gpointer data)
73 {
74 keyboard_reset_chains();
75
76 return FALSE; /* don't repeat */
77 }
78
79 static void set_curpos(KeyBindingTree *newpos)
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
89 for (it = curpos->keylist; it; it = g_list_next(it))
90 text = g_strconcat((text ? text : ""), it->data, "-", NULL);
91
92 popup_position(popup, NorthWestGravity, 10, 10);
93 popup_show(popup, text);
94 g_free(text);
95 } else
96 popup_hide(popup);
97 }
98
99 void keyboard_reset_chains()
100 {
101 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
102
103 if (curpos)
104 set_curpos(NULL);
105 }
106
107 void keyboard_unbind_all()
108 {
109 tree_destroy(keyboard_firstnode);
110 keyboard_firstnode = NULL;
111 }
112
113 gboolean keyboard_bind(GList *keylist, ObAction *action)
114 {
115 KeyBindingTree *tree, *t;
116 gboolean conflict;
117 gboolean mods = TRUE;
118
119 g_assert(keylist != NULL);
120 g_assert(action != NULL);
121
122 if (!(tree = tree_build(keylist)))
123 return FALSE;
124
125 if ((t = tree_find(tree, &conflict)) != NULL) {
126 /* already bound to something, use the existing tree */
127 tree_destroy(tree);
128 tree = NULL;
129 } else
130 t = tree;
131
132 if (conflict) {
133 g_message(_("Conflict with key binding in config file"));
134 tree_destroy(tree);
135 return FALSE;
136 }
137
138 /* find if every key in this chain has modifiers, and also find the
139 bottom node of the tree */
140 while (t->first_child) {
141 if (!t->state)
142 mods = FALSE;
143 t = t->first_child;
144 }
145
146 /* when there are no modifiers in the binding, then the action cannot
147 be interactive */
148 if (!mods && action->data.any.interactive) {
149 action->data.any.interactive = FALSE;
150 action->data.inter.final = TRUE;
151 }
152
153 /* set the action */
154 t->actions = g_slist_append(t->actions, action);
155 /* assimilate this built tree into the main tree. assimilation
156 destroys/uses the tree */
157 if (tree) tree_assimilate(tree);
158
159 return TRUE;
160 }
161
162 gboolean keyboard_interactive_grab(guint state, ObClient *client,
163 ObAction *action)
164 {
165 ObInteractiveState *s;
166
167 g_assert(action->data.any.interactive);
168
169 if (!interactive_states) {
170 grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER);
171 if (!grab_keyboard(TRUE)) {
172 grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
173 return FALSE;
174 }
175 }
176
177 s = g_new(ObInteractiveState, 1);
178
179 s->state = state;
180 s->client = client;
181 s->actions = g_slist_append(NULL, action);
182
183 interactive_states = g_slist_append(interactive_states, s);
184
185 return TRUE;
186 }
187
188 void keyboard_interactive_end(ObInteractiveState *s,
189 guint state, gboolean cancel, Time time)
190 {
191 action_run_interactive(s->actions, s->client, state, time, cancel, TRUE);
192
193 g_slist_free(s->actions);
194 g_free(s);
195
196 interactive_states = g_slist_remove(interactive_states, s);
197
198 if (!interactive_states) {
199 grab_keyboard(FALSE);
200 grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
201 keyboard_reset_chains();
202 }
203 }
204
205 void keyboard_interactive_end_client(ObClient *client, gpointer data)
206 {
207 GSList *it, *next;
208
209 for (it = interactive_states; it; it = next) {
210 ObInteractiveState *s = it->data;
211
212 next = g_slist_next(it);
213
214 if (s->client == client)
215 s->client = NULL;
216 }
217 }
218
219 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
220 {
221 GSList *it, *next;
222 gboolean handled = FALSE;
223 gboolean done = FALSE;
224 gboolean cancel = FALSE;
225
226 for (it = interactive_states; it; it = next) {
227 ObInteractiveState *s = it->data;
228
229 next = g_slist_next(it);
230
231 if ((e->type == KeyRelease &&
232 !(s->state & e->xkey.state)))
233 done = TRUE;
234 else if (e->type == KeyPress) {
235 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
236 done = TRUE;
237 else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
238 cancel = done = TRUE;
239 } else if (e->type == ButtonPress)
240 cancel = done = TRUE;
241
242 if (done) {
243 keyboard_interactive_end(s, e->xkey.state, cancel, e->xkey.time);
244
245 handled = TRUE;
246 } else
247 *client = s->client;
248 }
249
250 return handled;
251 }
252
253 void keyboard_event(ObClient *client, const XEvent *e)
254 {
255 KeyBindingTree *p;
256
257 g_assert(e->type == KeyPress);
258
259 if (e->xkey.keycode == config_keyboard_reset_keycode &&
260 e->xkey.state == config_keyboard_reset_state)
261 {
262 keyboard_reset_chains();
263 return;
264 }
265
266 if (curpos == NULL)
267 p = keyboard_firstnode;
268 else
269 p = curpos->first_child;
270 while (p) {
271 if (p->key == e->xkey.keycode &&
272 p->state == e->xkey.state)
273 {
274 if (p->first_child != NULL) { /* part of a chain */
275 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
276 /* 5 second timeout for chains */
277 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
278 chain_timeout, NULL,
279 g_direct_equal, NULL);
280 set_curpos(p);
281 } else {
282
283 keyboard_reset_chains();
284
285 action_run_key(p->actions, client, e->xkey.state,
286 e->xkey.x_root, e->xkey.y_root,
287 e->xkey.time);
288 }
289 break;
290 }
291 p = p->next_sibling;
292 }
293 }
294
295 gboolean keyboard_interactively_grabbed()
296 {
297 return !!interactive_states;
298 }
299
300 void keyboard_startup(gboolean reconfig)
301 {
302 grab_keys(TRUE);
303 popup = popup_new(FALSE);
304
305 if (!reconfig)
306 client_add_destructor(keyboard_interactive_end_client, NULL);
307 }
308
309 void keyboard_shutdown(gboolean reconfig)
310 {
311 GSList *it;
312
313 if (!reconfig)
314 client_remove_destructor(keyboard_interactive_end_client);
315
316 for (it = interactive_states; it; it = g_slist_next(it))
317 g_free(it->data);
318 g_slist_free(interactive_states);
319 interactive_states = NULL;
320
321 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
322
323 keyboard_unbind_all();
324 set_curpos(NULL);
325
326 popup_free(popup);
327 popup = NULL;
328 }
329
This page took 0.054663 seconds and 5 git commands to generate.