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