]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/keyboard.c
add a slight delay to the focus/desktop switch dialogs. so if you hit the key really...
[mikachu/openbox.git] / 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(0);
75     return FALSE; /* don't repeat */
76 }
77
78 static void set_curpos(KeyBindingTree *newpos)
79 {
80     grab_keys(FALSE);
81     curpos = newpos;
82     grab_keys(TRUE);
83
84     if (curpos != NULL) {
85         gchar *text = NULL;
86         GList *it;
87
88         for (it = curpos->keylist; it; it = g_list_next(it)) {
89             gchar *oldtext = text;
90             if (text == NULL)
91                 text = g_strdup(it->data);
92             else
93                 text = g_strconcat(text, " - ", it->data, NULL);
94             g_free(oldtext);
95         }
96
97         popup_position(popup, NorthWestGravity, 10, 10);
98         /* 1 second delay for the popup to show */
99         popup_delay_show(popup, G_USEC_PER_SEC, text);
100         g_free(text);
101     } else {
102         popup_hide(popup);
103     }
104 }
105
106 void keyboard_reset_chains(gint break_chroots)
107 {
108     KeyBindingTree *p;
109
110     for (p = curpos; p; p = p->parent) {
111         if (p->chroot) {
112             if (break_chroots == 0) break; /* stop here */
113             if (break_chroots > 0)
114                 --break_chroots;
115         }
116     }
117     set_curpos(p);
118 }
119
120 void keyboard_unbind_all()
121 {
122     tree_destroy(keyboard_firstnode);
123     keyboard_firstnode = NULL;
124 }
125
126 void keyboard_chroot(GList *keylist)
127 {
128     /* try do it in the existing tree. if we can't that means it is an empty
129        chroot binding. so add it to the tree then. */
130     if (!tree_chroot(keyboard_firstnode, keylist)) {
131         KeyBindingTree *tree;
132         if (!(tree = tree_build(keylist)))
133             return;
134         tree_chroot(tree, keylist);
135         tree_assimilate(tree);
136     }
137 }
138
139 gboolean keyboard_bind(GList *keylist, ObAction *action)
140 {
141     KeyBindingTree *tree, *t;
142     gboolean conflict;
143     gboolean mods = TRUE;
144
145     g_assert(keylist != NULL);
146     g_assert(action != NULL);
147
148     if (!(tree = tree_build(keylist)))
149         return FALSE;
150
151     if ((t = tree_find(tree, &conflict)) != NULL) {
152         /* already bound to something, use the existing tree */
153         tree_destroy(tree);
154         tree = NULL;
155     } else
156         t = tree;
157
158     if (conflict) {
159         g_message(_("Conflict with key binding in config file"));
160         tree_destroy(tree);
161         return FALSE;
162     }
163
164     /* find if every key in this chain has modifiers, and also find the
165        bottom node of the tree */
166     while (t->first_child) {
167         if (!t->state)
168             mods = FALSE;
169         t = t->first_child;
170     }
171
172     /* when there are no modifiers in the binding, then the action cannot
173        be interactive */
174     if (!mods && action->data.any.interactive) {
175         action->data.any.interactive = FALSE;
176         action->data.inter.final = TRUE;
177     }
178
179     /* set the action */
180     t->actions = g_slist_append(t->actions, action);
181     /* assimilate this built tree into the main tree. assimilation
182        destroys/uses the tree */
183     if (tree) tree_assimilate(tree);
184
185     return TRUE;
186 }
187
188 gboolean keyboard_interactive_grab(guint state, ObClient *client,
189                                    ObAction *action)
190 {
191     ObInteractiveState *s;
192
193     g_assert(action->data.any.interactive);
194
195     if (!interactive_states) {
196         grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER);
197         if (!grab_keyboard(TRUE)) {
198             grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
199             return FALSE;
200         }
201     }
202
203     s = g_new(ObInteractiveState, 1);
204
205     s->state = state;
206     s->client = client;
207     s->actions = g_slist_append(NULL, action);
208
209     interactive_states = g_slist_append(interactive_states, s);
210
211     return TRUE;
212 }
213
214 void keyboard_interactive_end(ObInteractiveState *s,
215                               guint state, gboolean cancel, Time time)
216 {
217     action_run_interactive(s->actions, s->client, state, time, cancel, TRUE);
218
219     g_slist_free(s->actions);
220     g_free(s);
221
222     interactive_states = g_slist_remove(interactive_states, s);
223
224     if (!interactive_states) {
225         grab_keyboard(FALSE);
226         grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
227     }
228 }
229
230 void keyboard_interactive_end_client(ObClient *client, gpointer data)
231 {
232     GSList *it, *next;
233
234     for (it = interactive_states; it; it = next) {
235         ObInteractiveState *s = it->data;
236
237         next = g_slist_next(it);
238
239         if (s->client == client)
240             s->client = NULL;
241     }
242 }
243
244 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
245 {
246     GSList *it, *next;
247     gboolean handled = FALSE;
248     gboolean done = FALSE;
249     gboolean cancel = FALSE;
250
251     for (it = interactive_states; it; it = next) {
252         ObInteractiveState *s = it->data;
253
254         next = g_slist_next(it);
255         
256         if ((e->type == KeyRelease && 
257              !(s->state & e->xkey.state)))
258             done = TRUE;
259         else if (e->type == KeyPress) {
260             /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
261                 done = TRUE;
262             else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
263                 cancel = done = TRUE;
264         } else if (e->type == ButtonPress)
265             cancel = done = TRUE;
266
267         if (done) {
268             keyboard_interactive_end(s, e->xkey.state, cancel, e->xkey.time);
269
270             handled = TRUE;
271         } else
272             *client = s->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 (p->first_child != NULL) { /* part of a chain */
301                 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
302                 /* 3 second timeout for chains */
303                 ob_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
304                                          chain_timeout, NULL,
305                                          g_direct_equal, NULL);
306                 set_curpos(p);
307             } else if (p->chroot)         /* an empty chroot */
308                 set_curpos(p);
309             else {
310                 keyboard_reset_chains(0);
311
312                 action_run_key(p->actions, client, e->xkey.state,
313                                e->xkey.x_root, e->xkey.y_root,
314                                e->xkey.time);
315             }
316             break;
317         }
318         p = p->next_sibling;
319     }
320 }
321
322 gboolean keyboard_interactively_grabbed()
323 {
324     return !!interactive_states;
325 }
326
327 void keyboard_startup(gboolean reconfig)
328 {
329     grab_keys(TRUE);
330     popup = popup_new(FALSE);
331
332     if (!reconfig)
333         client_add_destructor(keyboard_interactive_end_client, NULL);
334 }
335
336 void keyboard_shutdown(gboolean reconfig)
337 {
338     GSList *it;
339
340     if (!reconfig)
341         client_remove_destructor(keyboard_interactive_end_client);
342
343     for (it = interactive_states; it; it = g_slist_next(it))
344         g_free(it->data);
345     g_slist_free(interactive_states);
346     interactive_states = NULL;
347
348     ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
349
350     keyboard_unbind_all();
351     set_curpos(NULL);
352
353     popup_free(popup);
354     popup = NULL;
355 }
356