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"
44 ObFrameContext context;
47 KeyBindingTree *keyboard_firstnode = NULL;
48 static ObPopup *popup = NULL;
49 static GSList *interactive_states;
50 static KeyBindingTree *curpos;
52 static void grab_keys(gboolean grab)
56 ungrab_all_keys(RootWindow(ob_display, ob_screen));
59 p = curpos ? curpos->first_child : keyboard_firstnode;
61 grab_key(p->key, p->state, RootWindow(ob_display, ob_screen),
66 grab_key(config_keyboard_reset_keycode,
67 config_keyboard_reset_state,
68 RootWindow(ob_display, ob_screen), GrabModeAsync);
72 static gboolean chain_timeout(gpointer data)
74 keyboard_reset_chains(0);
75 return FALSE; /* don't repeat */
78 static void set_curpos(KeyBindingTree *newpos)
88 for (it = curpos->keylist; it; it = g_list_next(it)) {
89 gchar *oldtext = text;
91 text = g_strdup(it->data);
93 text = g_strconcat(text, " - ", it->data, NULL);
97 popup_position(popup, NorthWestGravity, 10, 10);
98 /* 1 second delay for the popup to show */
99 popup_delay_show(popup, G_USEC_PER_SEC, text);
106 void keyboard_reset_chains(gint break_chroots)
110 for (p = curpos; p; p = p->parent) {
112 if (break_chroots == 0) break; /* stop here */
113 if (break_chroots > 0)
120 void keyboard_unbind_all()
122 tree_destroy(keyboard_firstnode);
123 keyboard_firstnode = NULL;
126 void keyboard_chroot(GList *keylist)
128 /* try do it in the existing tree. if we can't that means it is an empty
129 chroot binding. so add it to the tree then. */
130 if (!tree_chroot(keyboard_firstnode, keylist)) {
131 KeyBindingTree *tree;
132 if (!(tree = tree_build(keylist)))
134 tree_chroot(tree, keylist);
135 tree_assimilate(tree);
139 gboolean keyboard_bind(GList *keylist, ObAction *action)
141 KeyBindingTree *tree, *t;
143 gboolean mods = TRUE;
145 g_assert(keylist != NULL);
146 g_assert(action != NULL);
148 if (!(tree = tree_build(keylist)))
151 if ((t = tree_find(tree, &conflict)) != NULL) {
152 /* already bound to something, use the existing tree */
159 g_message(_("Conflict with key binding in config file"));
164 /* find if every key in this chain has modifiers, and also find the
165 bottom node of the tree */
166 while (t->first_child) {
172 /* when there are no modifiers in the binding, then the action cannot
174 if (!mods && action->data.any.interactive) {
175 action->data.any.interactive = FALSE;
176 action->data.inter.final = TRUE;
180 t->actions = g_slist_append(t->actions, action);
181 /* assimilate this built tree into the main tree. assimilation
182 destroys/uses the tree */
183 if (tree) tree_assimilate(tree);
188 gboolean keyboard_interactive_grab(guint state, ObClient *client,
191 ObInteractiveState *s;
193 g_assert(action->data.any.interactive);
195 if (!interactive_states) {
196 grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER);
197 if (!grab_keyboard(TRUE)) {
198 grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
203 s = g_new(ObInteractiveState, 1);
207 s->actions = g_slist_append(NULL, action);
209 interactive_states = g_slist_append(interactive_states, s);
214 void keyboard_interactive_end(ObInteractiveState *s,
215 guint state, gboolean cancel, Time time)
217 action_run_interactive(s->actions, s->client, state, time, cancel, TRUE);
219 g_slist_free(s->actions);
222 interactive_states = g_slist_remove(interactive_states, s);
224 if (!interactive_states) {
225 grab_keyboard(FALSE);
226 grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
230 void keyboard_interactive_end_client(ObClient *client, gpointer data)
234 for (it = interactive_states; it; it = next) {
235 ObInteractiveState *s = it->data;
237 next = g_slist_next(it);
239 if (s->client == client)
244 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
247 gboolean handled = FALSE;
248 gboolean done = FALSE;
249 gboolean cancel = FALSE;
251 for (it = interactive_states; it; it = next) {
252 ObInteractiveState *s = it->data;
254 next = g_slist_next(it);
256 if ((e->type == KeyRelease &&
257 !(s->state & e->xkey.state)))
259 else if (e->type == KeyPress) {
260 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
262 else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
263 cancel = done = TRUE;
264 } else if (e->type == ButtonPress)
265 cancel = done = TRUE;
268 keyboard_interactive_end(s, e->xkey.state, cancel, e->xkey.time);
278 void keyboard_event(ObClient *client, const XEvent *e)
282 g_assert(e->type == KeyPress);
284 if (e->xkey.keycode == config_keyboard_reset_keycode &&
285 e->xkey.state == config_keyboard_reset_state)
287 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
288 keyboard_reset_chains(-1);
293 p = keyboard_firstnode;
295 p = curpos->first_child;
297 if (p->key == e->xkey.keycode &&
298 p->state == e->xkey.state)
300 if (p->first_child != NULL) { /* part of a chain */
301 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
302 /* 3 second timeout for chains */
303 ob_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
305 g_direct_equal, NULL);
307 } else if (p->chroot) /* an empty chroot */
310 keyboard_reset_chains(0);
312 action_run_key(p->actions, client, e->xkey.state,
313 e->xkey.x_root, e->xkey.y_root,
322 gboolean keyboard_interactively_grabbed()
324 return !!interactive_states;
327 void keyboard_startup(gboolean reconfig)
330 popup = popup_new(FALSE);
333 client_add_destructor(keyboard_interactive_end_client, NULL);
336 void keyboard_shutdown(gboolean reconfig)
341 client_remove_destructor(keyboard_interactive_end_client);
343 for (it = interactive_states; it; it = g_slist_next(it))
345 g_slist_free(interactive_states);
346 interactive_states = NULL;
348 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
350 keyboard_unbind_all();