]> Dogcows Code - chaz/openbox/blob - openbox/keyboard.c
make mouse use the new action stuff
[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 "actions.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 ObActionsAct *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) return;
82
83 grab_keys(FALSE);
84 curpos = newpos;
85 grab_keys(TRUE);
86
87 if (curpos != NULL) {
88 gchar *text = NULL;
89 GList *it;
90 Rect *a;
91
92 for (it = curpos->keylist; it; it = g_list_next(it)) {
93 gchar *oldtext = text;
94 if (text == NULL)
95 text = g_strdup(it->data);
96 else
97 text = g_strconcat(text, " - ", it->data, NULL);
98 g_free(oldtext);
99 }
100
101 a = screen_physical_area_active();
102 popup_position(popup, NorthWestGravity, a->x + 10, a->y + 10);
103 /* 1 second delay for the popup to show */
104 popup_delay_show(popup, G_USEC_PER_SEC, text);
105 g_free(text);
106 g_free(a);
107 } else {
108 popup_hide(popup);
109 }
110 }
111
112 void keyboard_reset_chains(gint break_chroots)
113 {
114 KeyBindingTree *p;
115
116 for (p = curpos; p; p = p->parent) {
117 if (p->chroot) {
118 if (break_chroots == 0) break; /* stop here */
119 if (break_chroots > 0)
120 --break_chroots;
121 }
122 }
123 set_curpos(p);
124 }
125
126 void keyboard_unbind_all()
127 {
128 tree_destroy(keyboard_firstnode);
129 keyboard_firstnode = NULL;
130 }
131
132 void keyboard_chroot(GList *keylist)
133 {
134 /* try do it in the existing tree. if we can't that means it is an empty
135 chroot binding. so add it to the tree then. */
136 if (!tree_chroot(keyboard_firstnode, keylist)) {
137 KeyBindingTree *tree;
138 if (!(tree = tree_build(keylist)))
139 return;
140 tree_chroot(tree, keylist);
141 tree_assimilate(tree);
142 }
143 }
144
145 gboolean keyboard_bind(GList *keylist, ObActionsAct *action)
146 {
147 KeyBindingTree *tree, *t;
148 gboolean conflict;
149
150 g_assert(keylist != NULL);
151 g_assert(action != NULL);
152
153 if (!(tree = tree_build(keylist)))
154 return FALSE;
155
156 if ((t = tree_find(tree, &conflict)) != NULL) {
157 /* already bound to something, use the existing tree */
158 tree_destroy(tree);
159 tree = NULL;
160 } else
161 t = tree;
162
163 if (conflict) {
164 g_message(_("Conflict with key binding in config file"));
165 tree_destroy(tree);
166 return FALSE;
167 }
168
169 /* find the bottom node */
170 for (; t->first_child; t = t->first_child);
171
172 /* when there are no modifiers in the binding, then the action cannot
173 be interactive */
174 if (!t->state && action->data.any.interactive) {
175 g_print("not interactive\n");
176 action->data.any.interactive = FALSE;
177 action->data.inter.final = TRUE;
178 }
179
180 /* set the action */
181 t->actions = g_slist_append(t->actions, action);
182 /* assimilate this built tree into the main tree. assimilation
183 destroys/uses the tree */
184 if (tree) tree_assimilate(tree);
185
186 return TRUE;
187 }
188
189 static void keyboard_interactive_end(guint state, gboolean cancel, Time time,
190 gboolean ungrab)
191 {
192 GSList *alist;
193
194 g_assert(istate.active);
195
196 /* ungrab first so they won't be NotifyWhileGrabbed */
197 if (ungrab)
198 ungrab_keyboard();
199
200 /* set this before running the actions so they know the keyboard is not
201 grabbed */
202 istate.active = FALSE;
203
204 alist = g_slist_append(NULL, istate.action);
205 action_run_interactive(alist, istate.client, state, time, cancel, TRUE);
206 g_slist_free(alist);
207
208 keyboard_reset_chains(0);
209 }
210
211 static void keyboard_interactive_end_client(ObClient *client, gpointer data)
212 {
213 if (istate.active && istate.client == client)
214 istate.client = NULL;
215 }
216
217
218 void keyboard_interactive_cancel()
219 {
220 keyboard_interactive_end(0, TRUE, event_curtime, TRUE);
221 }
222
223 gboolean keyboard_interactive_grab(guint state, ObClient *client,
224 ObAction *action)
225 {
226 g_assert(action->data.any.interactive);
227
228 if (!istate.active) {
229 if (!grab_keyboard())
230 return FALSE;
231 } else if (action->func != istate.action->func) {
232 keyboard_interactive_end(state, TRUE, action->data.any.time, FALSE);
233 }
234
235 istate.active = TRUE;
236 istate.state = state;
237 istate.client = client;
238 istate.action = action;
239
240 return TRUE;
241 }
242
243 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
244 {
245 gboolean handled = FALSE;
246 gboolean done = FALSE;
247 gboolean cancel = FALSE;
248
249 if (istate.active) {
250 if ((e->type == KeyRelease && !(istate.state & e->xkey.state))) {
251 done = TRUE;
252 handled = TRUE;
253 } else if (e->type == KeyPress) {
254 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
255 done = TRUE;
256 else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
257 cancel = done = TRUE;
258 handled = TRUE;
259 }
260 } else if (e->type == ButtonPress) {
261 cancel = TRUE;
262 done = TRUE;
263 handled = FALSE;
264 }
265
266 if (done)
267 keyboard_interactive_end(e->xkey.state, cancel, e->xkey.time,TRUE);
268
269 if (handled)
270 *client = istate.client;
271 }
272
273 return handled;
274 }
275
276 void keyboard_event(ObClient *client, const XEvent *e)
277 {
278 KeyBindingTree *p;
279
280 if (e->type == KeyRelease) {
281 grab_key_passive_count(-1);
282 return;
283 }
284
285 g_assert(e->type == KeyPress);
286 grab_key_passive_count(1);
287
288 if (e->xkey.keycode == config_keyboard_reset_keycode &&
289 e->xkey.state == config_keyboard_reset_state)
290 {
291 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
292 keyboard_reset_chains(-1);
293 return;
294 }
295
296 if (curpos == NULL)
297 p = keyboard_firstnode;
298 else
299 p = curpos->first_child;
300 while (p) {
301 if (p->key == e->xkey.keycode &&
302 p->state == e->xkey.state)
303 {
304 /* if we hit a key binding, then close any open menus and run it */
305 if (menu_frame_visible)
306 menu_frame_hide_all();
307
308 if (p->first_child != NULL) { /* part of a chain */
309 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
310 /* 3 second timeout for chains */
311 ob_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
312 chain_timeout, NULL,
313 g_direct_equal, NULL);
314 set_curpos(p);
315 } else if (p->chroot) /* an empty chroot */
316 set_curpos(p);
317 else {
318 GSList *it;
319 gboolean inter = FALSE;
320
321 for (it = p->actions; it && !inter; it = g_slist_next(it))
322 if (((ObAction*)it->data)->data.any.interactive)
323 inter = TRUE;
324 if (!inter) /* don't reset if the action is interactive */
325 keyboard_reset_chains(0);
326
327 action_run_key(p->actions, client, e->xkey.state,
328 e->xkey.x_root, e->xkey.y_root,
329 e->xkey.time);
330 }
331 break;
332 }
333 p = p->next_sibling;
334 }
335 }
336
337 gboolean keyboard_interactively_grabbed()
338 {
339 return istate.active;
340 }
341
342 void keyboard_startup(gboolean reconfig)
343 {
344 grab_keys(TRUE);
345 popup = popup_new(FALSE);
346 popup_set_text_align(popup, RR_JUSTIFY_CENTER);
347
348 if (!reconfig)
349 client_add_destroy_notify(keyboard_interactive_end_client, NULL);
350 }
351
352 void keyboard_shutdown(gboolean reconfig)
353 {
354 if (!reconfig)
355 client_remove_destroy_notify(keyboard_interactive_end_client);
356
357 if (istate.active)
358 keyboard_interactive_cancel();
359
360 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
361
362 keyboard_unbind_all();
363 set_curpos(NULL);
364
365 popup_free(popup);
366 popup = NULL;
367 }
368
This page took 0.055664 seconds and 5 git commands to generate.