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