]> icculus.org git repositories - dana/openbox.git/blob - openbox/keyboard.c
Rename ObActions* to ObAction* and remove more 3.4-compat action stuff that was missed.
[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 "action.h"
28 #include "action_list.h"
29 #include "menuframe.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 #include "obt/keyboard.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
46 static void grab_keys(gboolean grab)
47 {
48     KeyBindingTree *p;
49
50     ungrab_all_keys(obt_root(ob_screen));
51
52     if (grab) {
53         p = curpos ? curpos->first_child : keyboard_firstnode;
54         while (p) {
55             if (p->key)
56                 grab_key(p->key, p->state, obt_root(ob_screen),
57                          GrabModeAsync);
58             p = p->next_sibling;
59         }
60         if (curpos)
61             grab_key(config_keyboard_reset_keycode,
62                      config_keyboard_reset_state,
63                      obt_root(ob_screen), GrabModeAsync);
64     }
65 }
66
67 static gboolean chain_timeout(gpointer data)
68 {
69     keyboard_reset_chains(0);
70     return FALSE; /* don't repeat */
71 }
72
73 static void chain_done(gpointer data)
74 {
75     chain_timer = 0;
76 }
77
78 static void set_curpos(KeyBindingTree *newpos)
79 {
80     if (curpos == newpos) return;
81
82     grab_keys(FALSE);
83     curpos = newpos;
84     grab_keys(TRUE);
85
86     if (curpos != NULL) {
87         gchar *text = NULL;
88         GList *it;
89         const Rect *a;
90
91         for (it = curpos->keylist; it; it = g_list_next(it)) {
92             gchar *oldtext = text;
93             if (text == NULL)
94                 text = g_strdup(it->data);
95             else
96                 text = g_strconcat(text, " - ", it->data, NULL);
97             g_free(oldtext);
98         }
99
100         a = screen_physical_area_primary(FALSE);
101         popup_position(popup, NorthWestGravity, a->x + 10, a->y + 10);
102         /* 1 second delay for the popup to show */
103         popup_delay_show(popup, 1000, text);
104         g_free(text);
105     } else {
106         popup_hide(popup);
107     }
108 }
109
110 void keyboard_reset_chains(gint break_chroots)
111 {
112     KeyBindingTree *p;
113
114     for (p = curpos; p; p = p->parent) {
115         if (p->chroot) {
116             if (break_chroots == 0) break; /* stop here */
117             if (break_chroots > 0)
118                 --break_chroots;
119         }
120     }
121     set_curpos(p);
122 }
123
124 void keyboard_unbind_all(void)
125 {
126     tree_destroy(keyboard_firstnode);
127     keyboard_firstnode = NULL;
128 }
129
130 void keyboard_chroot(GList *keylist)
131 {
132     /* try do it in the existing tree. if we can't that means it is an empty
133        chroot binding. so add it to the tree then. */
134     if (!tree_chroot(keyboard_firstnode, keylist)) {
135         KeyBindingTree *tree;
136         if (!(tree = tree_build(keylist)))
137             return;
138         tree_chroot(tree, keylist);
139         tree_assimilate(tree);
140     }
141 }
142
143 gboolean keyboard_bind(GList *keylist, ObActionList *actions)
144 {
145     KeyBindingTree *tree, *t;
146     gboolean conflict;
147
148     g_assert(keylist != NULL);
149
150     if (!actions)
151         return TRUE;
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     /* set the action */
173     action_list_ref(actions);
174     t->actions = action_list_concat(t->actions, actions);
175
176     /* assimilate this built tree into the main tree. assimilation
177        destroys/uses the tree */
178     if (tree) tree_assimilate(tree);
179
180     return TRUE;
181 }
182
183 #if 0
184 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
185 {
186     gboolean handled = FALSE;
187     gboolean done = FALSE;
188     gboolean cancel = FALSE;
189     guint mods;
190
191     mods = obt_keyboard_only_modmasks(ev->xkey.state);
192
193     if (istate.active) {
194         if ((e->type == KeyRelease && !(istate.state & mods))) {
195             done = TRUE;
196             handled = TRUE;
197         } else if (e->type == KeyPress) {
198             /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
199               done = TRUE;
200               else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
201                   cancel = done = TRUE;
202                   handled = TRUE;
203               }
204         } else if (e->type == ButtonPress) {
205             cancel = TRUE;
206             done = TRUE;
207             handled = FALSE;
208         }
209
210         if (done)
211             keyboard_interactive_end(e->xkey.state, cancel, e->xkey.time,TRUE);
212
213         if (handled)
214             *client = istate.client;
215     }
216
217     return handled;
218 }
219 #endif
220
221 gboolean keyboard_event(ObClient *client, const XEvent *e)
222 {
223     KeyBindingTree *p;
224     gboolean used;
225     guint mods;
226
227     if (e->type == KeyRelease) {
228         grab_key_passive_count(-1);
229         return FALSE;
230     }
231
232     g_assert(e->type == KeyPress);
233     grab_key_passive_count(1);
234
235     mods = obt_keyboard_only_modmasks(e->xkey.state);
236
237     if (e->xkey.keycode == config_keyboard_reset_keycode &&
238         mods == config_keyboard_reset_state)
239     {
240         if (chain_timer) g_source_remove(chain_timer);
241         keyboard_reset_chains(-1);
242         return TRUE;
243     }
244
245     used = FALSE;
246     if (curpos == NULL)
247         p = keyboard_firstnode;
248     else
249         p = curpos->first_child;
250     while (p) {
251         if (p->key == e->xkey.keycode && p->state == mods) {
252             /* if we hit a key binding, then close any open menus and run it */
253             if (menu_frame_visible)
254                 menu_frame_hide_all();
255
256             if (p->first_child != NULL) { /* part of a chain */
257                 if (chain_timer) g_source_remove(chain_timer);
258                 /* 3 second timeout for chains */
259                 chain_timer =
260                     g_timeout_add_full(G_PRIORITY_DEFAULT,
261                                        3000, chain_timeout, NULL,
262                                        chain_done);
263                 set_curpos(p);
264             } else if (p->chroot)         /* an empty chroot */
265                 set_curpos(p);
266             else {
267                 gboolean i;
268
269                 i = action_run_acts(p->actions, OB_USER_ACTION_KEYBOARD_KEY,
270                                     e->xkey.state,
271                                     e->xkey.x_root, e->xkey.y_root,
272                                     0, OB_FRAME_CONTEXT_NONE, client);
273                 if (!i) /* reset if an interactive was not run */
274                     keyboard_reset_chains(0);
275             }
276             break;
277             used = TRUE;
278         }
279         p = p->next_sibling;
280     }
281     return used;
282 }
283
284 static void node_rebind(KeyBindingTree *node)
285 {
286     if (node->first_child) {
287         /* find leaf nodes */
288         node_rebind(node->first_child);
289
290         /* for internal nodes, add them to the tree if they
291            are a chroot, but do this after adding their
292            children */
293         if (node->chroot)
294             keyboard_chroot(node->keylist);
295     }
296     else {
297         /* for leaf nodes, rebind each action assigned to it */
298         keyboard_bind(node->keylist, node->actions);
299
300         if (node->chroot)
301             keyboard_chroot(node->keylist);
302     }
303
304     /* go through each sibling */
305     if (node->next_sibling) node_rebind(node->next_sibling);
306 }
307
308 void keyboard_rebind(void)
309 {
310     KeyBindingTree *old;
311
312     old = keyboard_firstnode;
313     keyboard_firstnode = NULL;
314     node_rebind(old);
315
316     tree_destroy(old);
317     set_curpos(NULL);
318     grab_keys(TRUE);
319 }
320
321 void keyboard_startup(gboolean reconfig)
322 {
323     grab_keys(TRUE);
324     popup = popup_new();
325     popup_set_text_align(popup, RR_JUSTIFY_CENTER);
326 }
327
328 void keyboard_shutdown(gboolean reconfig)
329 {
330     if (chain_timer) g_source_remove(chain_timer);
331
332     keyboard_unbind_all();
333     set_curpos(NULL);
334
335     popup_free(popup);
336     popup = NULL;
337 }