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