1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 keyboard.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
32 #include "translate.h"
33 #include "moveresize.h"
37 KeyBindingTree *keyboard_firstnode;
43 ObFrameContext context;
46 static GSList *interactive_states;
48 static KeyBindingTree *curpos;
50 static void grab_for_window(Window win, gboolean grab)
57 p = curpos ? curpos->first_child : keyboard_firstnode;
59 grab_key(p->key, p->state, win, GrabModeAsync);
63 grab_key(config_keyboard_reset_keycode,
64 config_keyboard_reset_state,
69 void keyboard_grab_for_client(ObClient *c, gboolean grab)
71 grab_for_window(c->window, grab);
74 static void grab_keys(gboolean grab)
78 grab_for_window(screen_support_win, grab);
79 for (it = client_list; it; it = g_list_next(it))
80 grab_for_window(((ObClient*)it->data)->window, grab);
83 static gboolean chain_timeout(gpointer data)
85 keyboard_reset_chains();
87 return FALSE; /* don't repeat */
90 void keyboard_reset_chains()
92 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
101 gboolean keyboard_bind(GList *keylist, ObAction *action)
103 KeyBindingTree *tree, *t;
105 gboolean mods = TRUE;
107 g_assert(keylist != NULL);
108 g_assert(action != NULL);
110 if (!(tree = tree_build(keylist)))
113 if ((t = tree_find(tree, &conflict)) != NULL) {
114 /* already bound to something, use the existing tree */
121 g_warning("conflict with binding");
126 /* find if every key in this chain has modifiers, and also find the
127 bottom node of the tree */
128 while (t->first_child) {
134 /* when there are no modifiers in the binding, then the action cannot
136 if (!mods && action->data.any.interactive) {
137 action->data.any.interactive = FALSE;
138 action->data.inter.final = TRUE;
142 t->actions = g_slist_append(t->actions, action);
143 /* assimilate this built tree into the main tree. assimilation
144 destroys/uses the tree */
145 if (tree) tree_assimilate(tree);
150 void keyboard_interactive_grab(guint state, ObClient *client,
153 ObInteractiveState *s;
155 g_assert(action->data.any.interactive);
157 if (moveresize_in_progress)
158 moveresize_end(FALSE);
160 if (!interactive_states) {
161 if (!grab_keyboard(TRUE))
163 if (!grab_pointer(TRUE, OB_CURSOR_NONE)) {
164 grab_keyboard(FALSE);
169 s = g_new(ObInteractiveState, 1);
175 interactive_states = g_slist_append(interactive_states, s);
178 void keyboard_interactive_end(ObInteractiveState *s,
179 guint state, gboolean cancel)
181 action_run_interactive(s->action, s->client, state, cancel, TRUE);
185 interactive_states = g_slist_remove(interactive_states, s);
187 if (!interactive_states) {
188 grab_keyboard(FALSE);
189 grab_pointer(FALSE, OB_CURSOR_NONE);
190 keyboard_reset_chains();
194 void keyboard_interactive_end_client(gpointer data)
199 for (it = interactive_states; it; it = next) {
200 ObInteractiveState *s = it->data;
202 next = g_slist_next(it);
205 keyboard_interactive_end(s, 0, FALSE);
209 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
212 gboolean handled = FALSE;
213 gboolean done = FALSE;
214 gboolean cancel = FALSE;
216 for (it = interactive_states; it; it = next) {
217 ObInteractiveState *s = it->data;
219 next = g_slist_next(it);
221 if ((e->type == KeyRelease &&
222 !(s->state & e->xkey.state)))
224 else if (e->type == KeyPress) {
225 if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
227 else if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
228 cancel = done = TRUE;
231 keyboard_interactive_end(s, e->xkey.state, cancel);
241 void keyboard_event(ObClient *client, const XEvent *e)
245 g_assert(e->type == KeyPress);
247 if (e->xkey.keycode == config_keyboard_reset_keycode &&
248 e->xkey.state == config_keyboard_reset_state)
250 keyboard_reset_chains();
255 p = keyboard_firstnode;
257 p = curpos->first_child;
259 if (p->key == e->xkey.keycode &&
260 p->state == e->xkey.state)
262 if (p->first_child != NULL) { /* part of a chain */
263 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
264 /* 5 second timeout for chains */
265 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
266 chain_timeout, NULL, NULL);
273 for (it = p->actions; it; it = it->next)
274 action_run_key(it->data, client, e->xkey.state,
275 e->xkey.x_root, e->xkey.y_root);
277 keyboard_reset_chains();
285 void keyboard_startup(gboolean reconfig)
290 client_add_destructor(keyboard_interactive_end_client);
293 void keyboard_shutdown(gboolean reconfig)
298 client_remove_destructor(keyboard_interactive_end_client);
300 tree_destroy(keyboard_firstnode);
301 keyboard_firstnode = NULL;
303 for (it = interactive_states; it; it = g_slist_next(it))
305 g_slist_free(interactive_states);
306 interactive_states = NULL;
308 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);