Merge branch 'backport' into work
[dana/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
37 #include <glib.h>
38
39 KeyBindingTree *keyboard_firstnode = NULL;
40 static ObPopup *popup = NULL;
41 static KeyBindingTree *curpos;
42
43 static void grab_keys(gboolean grab)
44 {
45     KeyBindingTree *p;
46
47     ungrab_all_keys(obt_root(ob_screen));
48
49     if (grab) {
50         p = curpos ? curpos->first_child : keyboard_firstnode;
51         while (p) {
52             if (p->key)
53                 grab_key(p->key, p->state, obt_root(ob_screen),
54                          GrabModeAsync);
55             p = p->next_sibling;
56         }
57         if (curpos)
58             grab_key(config_keyboard_reset_keycode,
59                      config_keyboard_reset_state,
60                      obt_root(ob_screen), GrabModeAsync);
61     }
62 }
63
64 static gboolean chain_timeout(gpointer data)
65 {
66     keyboard_reset_chains(0);
67     return FALSE; /* don't repeat */
68 }
69
70 static void set_curpos(KeyBindingTree *newpos)
71 {
72     if (curpos == newpos) return;
73
74     grab_keys(FALSE);
75     curpos = newpos;
76     grab_keys(TRUE);
77
78     if (curpos != NULL) {
79         gchar *text = NULL;
80         GList *it;
81         Rect *a;
82
83         for (it = curpos->keylist; it; it = g_list_next(it)) {
84             gchar *oldtext = text;
85             if (text == NULL)
86                 text = g_strdup(it->data);
87             else
88                 text = g_strconcat(text, " - ", it->data, NULL);
89             g_free(oldtext);
90         }
91
92         a = screen_physical_area_primary();
93         popup_position(popup, NorthWestGravity, a->x + 10, a->y + 10);
94         /* 1 second delay for the popup to show */
95         popup_delay_show(popup, G_USEC_PER_SEC, text);
96         g_free(text);
97         g_free(a);
98     } else {
99         popup_hide(popup);
100     }
101 }
102
103 void keyboard_reset_chains(gint break_chroots)
104 {
105     KeyBindingTree *p;
106
107     for (p = curpos; p; p = p->parent) {
108         if (p->chroot) {
109             if (break_chroots == 0) break; /* stop here */
110             if (break_chroots > 0)
111                 --break_chroots;
112         }
113     }
114     set_curpos(p);
115 }
116
117 void keyboard_unbind_all(void)
118 {
119     tree_destroy(keyboard_firstnode);
120     keyboard_firstnode = NULL;
121 }
122
123 void keyboard_chroot(GList *keylist)
124 {
125     /* try do it in the existing tree. if we can't that means it is an empty
126        chroot binding. so add it to the tree then. */
127     if (!tree_chroot(keyboard_firstnode, keylist)) {
128         KeyBindingTree *tree;
129         if (!(tree = tree_build(keylist)))
130             return;
131         tree_chroot(tree, keylist);
132         tree_assimilate(tree);
133     }
134 }
135
136 gboolean keyboard_bind(GList *keylist, ObActionsAct *action)
137 {
138     KeyBindingTree *tree, *t;
139     gboolean conflict;
140
141     g_assert(keylist != NULL);
142     g_assert(action != NULL);
143
144     if (!(tree = tree_build(keylist)))
145         return FALSE;
146
147     if ((t = tree_find(tree, &conflict)) != NULL) {
148         /* already bound to something, use the existing tree */
149         tree_destroy(tree);
150         tree = NULL;
151     } else
152         t = tree;
153
154     if (conflict) {
155         g_message(_("Conflict with key binding in config file"));
156         tree_destroy(tree);
157         return FALSE;
158     }
159
160     /* find the bottom node */
161     for (; t->first_child; t = t->first_child);
162
163     /* set the action */
164     t->actions = g_slist_append(t->actions, action);
165     /* assimilate this built tree into the main tree. assimilation
166        destroys/uses the tree */
167     if (tree) tree_assimilate(tree);
168
169     return TRUE;
170 }
171
172 #if 0
173 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
174 {
175     gboolean handled = FALSE;
176     gboolean done = FALSE;
177     gboolean cancel = FALSE;
178
179     if (istate.active) {
180         if ((e->type == KeyRelease && !(istate.state & e->xkey.state))) {
181             done = TRUE;
182             handled = TRUE;
183         } else if (e->type == KeyPress) {
184             /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
185               done = TRUE;
186               else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
187                   cancel = done = TRUE;
188                   handled = TRUE;
189               }
190         } else if (e->type == ButtonPress) {
191             cancel = TRUE;
192             done = TRUE;
193             handled = FALSE;
194         }
195
196         if (done)
197             keyboard_interactive_end(e->xkey.state, cancel, e->xkey.time,TRUE);
198
199         if (handled)
200             *client = istate.client;
201     }
202
203     return handled;
204 }
205 #endif
206
207 void keyboard_event(ObClient *client, const XEvent *e)
208 {
209     KeyBindingTree *p;
210
211     if (e->type == KeyRelease) {
212         grab_key_passive_count(-1);
213         return;
214     }
215
216     g_assert(e->type == KeyPress);
217     grab_key_passive_count(1);
218
219     if (e->xkey.keycode == config_keyboard_reset_keycode &&
220         e->xkey.state == config_keyboard_reset_state)
221     {
222         obt_main_loop_timeout_remove(ob_main_loop, chain_timeout);
223         keyboard_reset_chains(-1);
224         return;
225     }
226
227     if (curpos == NULL)
228         p = keyboard_firstnode;
229     else
230         p = curpos->first_child;
231     while (p) {
232         if (p->key == e->xkey.keycode &&
233             p->state == e->xkey.state)
234         {
235             /* if we hit a key binding, then close any open menus and run it */
236             if (menu_frame_visible)
237                 menu_frame_hide_all();
238
239             if (p->first_child != NULL) { /* part of a chain */
240                 obt_main_loop_timeout_remove(ob_main_loop, chain_timeout);
241                 /* 3 second timeout for chains */
242                 obt_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
243                                           chain_timeout, NULL,
244                                           g_direct_equal, NULL);
245                 set_curpos(p);
246             } else if (p->chroot)         /* an empty chroot */
247                 set_curpos(p);
248             else {
249                 GSList *it;
250
251                 for (it = p->actions; it; it = g_slist_next(it))
252                     if (actions_act_is_interactive(it->data)) break;
253                 if (it == NULL) /* reset if the actions are not interactive */
254                     keyboard_reset_chains(0);
255
256                 actions_run_acts(p->actions, OB_USER_ACTION_KEYBOARD_KEY,
257                                  e->xkey.state, e->xkey.x_root, e->xkey.y_root,
258                                  0, OB_FRAME_CONTEXT_NONE, client);
259             }
260             break;
261         }
262         p = p->next_sibling;
263     }
264 }
265
266 static void node_rebind(KeyBindingTree *node)
267 {
268     if (node->first_child) {
269         /* find leaf nodes */
270         node_rebind(node->first_child);
271
272         /* for internal nodes, add them to the tree if they
273            are a chroot, but do this after adding their
274            children */
275         if (node->chroot)
276             keyboard_chroot(node->keylist);
277     }
278     else {
279         /* for leaf nodes, rebind each action assigned to it */
280         while (node->actions) {
281             /* add each action, and remove them from the original tree so
282                they don't get free'd on us */
283             keyboard_bind(node->keylist, node->actions->data);
284             node->actions = g_slist_delete_link(node->actions, node->actions);
285         }
286
287         if (node->chroot)
288             keyboard_chroot(node->keylist);
289     }
290
291     /* go through each sibling */
292     if (node->next_sibling) node_rebind(node->next_sibling);
293 }
294
295 void keyboard_rebind(void)
296 {
297     KeyBindingTree *old;
298
299     old = keyboard_firstnode;
300     keyboard_firstnode = NULL;
301     node_rebind(old);
302
303     tree_destroy(old);
304     set_curpos(NULL);
305     grab_keys(TRUE);
306 }
307
308 void keyboard_startup(gboolean reconfig)
309 {
310     grab_keys(TRUE);
311     popup = popup_new();
312     popup_set_text_align(popup, RR_JUSTIFY_CENTER);
313 }
314
315 void keyboard_shutdown(gboolean reconfig)
316 {
317     obt_main_loop_timeout_remove(ob_main_loop, chain_timeout);
318
319     keyboard_unbind_all();
320     set_curpos(NULL);
321
322     popup_free(popup);
323     popup = NULL;
324 }