1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 keyboard.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
33 #include "translate.h"
34 #include "moveresize.h"
39 KeyBindingTree *keyboard_firstnode;
45 ObFrameContext context;
48 static GSList *interactive_states;
50 static KeyBindingTree *curpos;
52 static void grab_for_window(Window win, gboolean grab)
59 p = curpos ? curpos->first_child : keyboard_firstnode;
61 grab_key(p->key, p->state, win, GrabModeAsync);
65 grab_key(config_keyboard_reset_keycode,
66 config_keyboard_reset_state,
71 static void grab_keys(gboolean grab)
75 grab_for_window(screen_support_win, grab);
76 grab_for_window(RootWindow(ob_display, ob_screen), grab);
79 static gboolean chain_timeout(gpointer data)
81 keyboard_reset_chains();
83 return FALSE; /* don't repeat */
86 void keyboard_reset_chains()
88 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
97 void keyboard_unbind_all()
99 tree_destroy(keyboard_firstnode);
100 keyboard_firstnode = NULL;
105 gboolean keyboard_bind(GList *keylist, ObAction *action)
107 KeyBindingTree *tree, *t;
109 gboolean mods = TRUE;
111 g_assert(keylist != NULL);
112 g_assert(action != NULL);
114 if (!(tree = tree_build(keylist)))
117 if ((t = tree_find(tree, &conflict)) != NULL) {
118 /* already bound to something, use the existing tree */
125 g_message(_("Conflict with key binding in config file"));
130 /* find if every key in this chain has modifiers, and also find the
131 bottom node of the tree */
132 while (t->first_child) {
138 /* when there are no modifiers in the binding, then the action cannot
140 if (!mods && action->data.any.interactive) {
141 action->data.any.interactive = FALSE;
142 action->data.inter.final = TRUE;
146 t->actions = g_slist_append(t->actions, action);
147 /* assimilate this built tree into the main tree. assimilation
148 destroys/uses the tree */
149 if (tree) tree_assimilate(tree);
154 gboolean keyboard_interactive_grab(guint state, ObClient *client,
157 ObInteractiveState *s;
159 g_assert(action->data.any.interactive);
161 if (!interactive_states) {
162 grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER);
163 if (!grab_keyboard(TRUE)) {
164 grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
169 s = g_new(ObInteractiveState, 1);
173 s->actions = g_slist_append(NULL, action);
175 interactive_states = g_slist_append(interactive_states, s);
180 void keyboard_interactive_end(ObInteractiveState *s,
181 guint state, gboolean cancel, Time time)
183 action_run_interactive(s->actions, s->client, state, time, cancel, TRUE);
185 g_slist_free(s->actions);
188 interactive_states = g_slist_remove(interactive_states, s);
190 if (!interactive_states) {
191 grab_keyboard(FALSE);
192 grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
193 keyboard_reset_chains();
197 void keyboard_interactive_end_client(ObClient *client, gpointer data)
201 for (it = interactive_states; it; it = next) {
202 ObInteractiveState *s = it->data;
204 next = g_slist_next(it);
206 if (s->client == client)
211 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
214 gboolean handled = FALSE;
215 gboolean done = FALSE;
216 gboolean cancel = FALSE;
218 for (it = interactive_states; it; it = next) {
219 ObInteractiveState *s = it->data;
221 next = g_slist_next(it);
223 if ((e->type == KeyRelease &&
224 !(s->state & e->xkey.state)))
226 else if (e->type == KeyPress) {
227 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
229 else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
230 cancel = done = TRUE;
231 } else if (e->type == ButtonPress)
232 cancel = done = TRUE;
235 keyboard_interactive_end(s, e->xkey.state, cancel, e->xkey.time);
245 void keyboard_event(ObClient *client, const XEvent *e)
249 g_assert(e->type == KeyPress);
251 if (e->xkey.keycode == config_keyboard_reset_keycode &&
252 e->xkey.state == config_keyboard_reset_state)
254 keyboard_reset_chains();
259 p = keyboard_firstnode;
261 p = curpos->first_child;
263 if (p->key == e->xkey.keycode &&
264 p->state == e->xkey.state)
266 if (p->first_child != NULL) { /* part of a chain */
267 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
268 /* 5 second timeout for chains */
269 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
271 g_direct_equal, NULL);
277 keyboard_reset_chains();
279 action_run_key(p->actions, client, e->xkey.state,
280 e->xkey.x_root, e->xkey.y_root,
289 gboolean keyboard_interactively_grabbed()
291 return !!interactive_states;
294 void keyboard_startup(gboolean reconfig)
299 client_add_destructor(keyboard_interactive_end_client, NULL);
302 void keyboard_shutdown(gboolean reconfig)
307 client_remove_destructor(keyboard_interactive_end_client);
309 for (it = interactive_states; it; it = g_slist_next(it))
311 g_slist_free(interactive_states);
312 interactive_states = NULL;
314 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
316 keyboard_unbind_all();