]> icculus.org git repositories - dana/openbox.git/blob - openbox/keyboard.c
Make warnings about parse problems in .desktop files "debug" messages. Most people...
[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 "action_list_run.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 #include "obt/keyboard.h"
39
40 #include <glib.h>
41
42 KeyBindingTree *keyboard_firstnode = NULL;
43 static ObPopup *popup = NULL;
44 static KeyBindingTree *curpos;
45 static guint chain_timer = 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)
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)))
138             return;
139         tree_chroot(tree, keylist);
140         tree_assimilate(tree);
141     }
142 }
143
144 gboolean keyboard_bind(GList *keylist, ObActionList *actions)
145 {
146     KeyBindingTree *tree, *t;
147     gboolean conflict;
148
149     g_assert(keylist != NULL);
150
151     if (!actions)
152         return TRUE;
153
154     if (!(tree = tree_build(keylist)))
155         return FALSE;
156
157     if ((t = tree_find(tree, &conflict)) != NULL) {
158         /* already bound to something, use the existing tree */
159         tree_destroy(tree);
160         tree = NULL;
161     } else
162         t = tree;
163
164     if (conflict) {
165         g_message(_("Conflict with key binding in config file"));
166         tree_destroy(tree);
167         return FALSE;
168     }
169
170     /* find the bottom node */
171     for (; t->first_child; t = t->first_child);
172
173     /* set the action */
174     action_list_ref(actions);
175     t->actions = action_list_concat(t->actions, actions);
176
177     /* assimilate this built tree into the main tree. assimilation
178        destroys/uses the tree */
179     if (tree) tree_assimilate(tree);
180
181     return TRUE;
182 }
183
184 #if 0
185 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
186 {
187     gboolean handled = FALSE;
188     gboolean done = FALSE;
189     gboolean cancel = FALSE;
190     guint mods;
191
192     mods = obt_keyboard_only_modmasks(ev->xkey.state);
193
194     if (istate.active) {
195         if ((e->type == KeyRelease && !(istate.state & mods))) {
196             done = TRUE;
197             handled = TRUE;
198         } else if (e->type == KeyPress) {
199             /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
200               done = TRUE;
201               else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
202                   cancel = done = TRUE;
203                   handled = TRUE;
204               }
205         } else if (e->type == ButtonPress) {
206             cancel = TRUE;
207             done = TRUE;
208             handled = FALSE;
209         }
210
211         if (done)
212             keyboard_interactive_end(e->xkey.state, cancel, e->xkey.time,TRUE);
213
214         if (handled)
215             *client = istate.client;
216     }
217
218     return handled;
219 }
220 #endif
221
222 gboolean keyboard_event(ObClient *client, const XEvent *e)
223 {
224     KeyBindingTree *p;
225     gboolean used;
226     guint mods;
227
228     if (e->type == KeyRelease) {
229         grab_key_passive_count(-1);
230         return FALSE;
231     }
232
233     g_assert(e->type == KeyPress);
234     grab_key_passive_count(1);
235
236     mods = obt_keyboard_only_modmasks(e->xkey.state);
237
238     if (e->xkey.keycode == config_keyboard_reset_keycode &&
239         mods == config_keyboard_reset_state)
240     {
241         if (chain_timer) g_source_remove(chain_timer);
242         keyboard_reset_chains(-1);
243         return TRUE;
244     }
245
246     used = FALSE;
247     if (curpos == NULL)
248         p = keyboard_firstnode;
249     else
250         p = curpos->first_child;
251     while (p) {
252         if (p->key == e->xkey.keycode && p->state == mods) {
253             /* if we hit a key binding, then close any open menus and run it */
254             if (menu_frame_visible)
255                 menu_frame_hide_all();
256
257             if (p->first_child != NULL) { /* part of a chain */
258                 if (chain_timer) g_source_remove(chain_timer);
259                 /* 3 second timeout for chains */
260                 chain_timer =
261                     g_timeout_add_full(G_PRIORITY_DEFAULT,
262                                        3000, chain_timeout, NULL,
263                                        chain_done);
264                 set_curpos(p);
265             } else if (p->chroot)         /* an empty chroot */
266                 set_curpos(p);
267             else {
268                 gboolean i;
269
270                 i = action_list_run(p->actions, OB_USER_ACTION_KEYBOARD_KEY,
271                                     e->xkey.state,
272                                     e->xkey.x_root, e->xkey.y_root,
273                                     0, OB_FRAME_CONTEXT_NONE, client);
274                 if (!i) /* reset if an interactive was not run */
275                     keyboard_reset_chains(0);
276             }
277             break;
278             used = TRUE;
279         }
280         p = p->next_sibling;
281     }
282     return used;
283 }
284
285 static void node_rebind(KeyBindingTree *node)
286 {
287     if (node->first_child) {
288         /* find leaf nodes */
289         node_rebind(node->first_child);
290
291         /* for internal nodes, add them to the tree if they
292            are a chroot, but do this after adding their
293            children */
294         if (node->chroot)
295             keyboard_chroot(node->keylist);
296     }
297     else {
298         /* for leaf nodes, rebind each action assigned to it */
299         keyboard_bind(node->keylist, node->actions);
300
301         if (node->chroot)
302             keyboard_chroot(node->keylist);
303     }
304
305     /* go through each sibling */
306     if (node->next_sibling) node_rebind(node->next_sibling);
307 }
308
309 void keyboard_rebind(void)
310 {
311     KeyBindingTree *old;
312
313     old = keyboard_firstnode;
314     keyboard_firstnode = NULL;
315     if (old)
316         node_rebind(old);
317
318     tree_destroy(old);
319     set_curpos(NULL);
320     grab_keys(TRUE);
321 }
322
323 void keyboard_startup(gboolean reconfig)
324 {
325     grab_keys(TRUE);
326     popup = popup_new();
327     popup_set_text_align(popup, RR_JUSTIFY_CENTER);
328 }
329
330 void keyboard_shutdown(gboolean reconfig)
331 {
332     if (chain_timer) g_source_remove(chain_timer);
333
334     keyboard_unbind_all();
335     set_curpos(NULL);
336
337     popup_free(popup);
338     popup = NULL;
339 }