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 gboolean popup_show_timeout(gpointer data)
81 popup_show(popup, text);
83 return FALSE; /* don't repeat */
86 static void set_curpos(KeyBindingTree *newpos)
96 for (it = curpos->keylist; it; it = g_list_next(it)) {
97 gchar *oldtext = text;
99 text = g_strdup(it->data);
101 text = g_strconcat(text, " - ", it->data, NULL);
105 popup_position(popup, NorthWestGravity, 10, 10);
107 popup_show_timeout(text);
110 ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
111 /* 1 second delay for the popup to show */
112 ob_main_loop_timeout_add(ob_main_loop, G_USEC_PER_SEC,
113 popup_show_timeout, text,
114 g_direct_equal, g_free);
118 ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
122 void keyboard_reset_chains(gint break_chroots)
126 for (p = curpos; p; p = p->parent) {
128 if (break_chroots == 0) break; /* stop here */
129 if (break_chroots > 0)
136 void keyboard_unbind_all()
138 tree_destroy(keyboard_firstnode);
139 keyboard_firstnode = NULL;
142 void keyboard_chroot(GList *keylist)
144 /* try do it in the existing tree. if we can't that means it is an empty
145 chroot binding. so add it to the tree then. */
146 if (!tree_chroot(keyboard_firstnode, keylist)) {
147 KeyBindingTree *tree;
148 if (!(tree = tree_build(keylist)))
150 tree_chroot(tree, keylist);
151 tree_assimilate(tree);
155 gboolean keyboard_bind(GList *keylist, ObAction *action)
157 KeyBindingTree *tree, *t;
159 gboolean mods = TRUE;
161 g_assert(keylist != NULL);
162 g_assert(action != NULL);
164 if (!(tree = tree_build(keylist)))
167 if ((t = tree_find(tree, &conflict)) != NULL) {
168 /* already bound to something, use the existing tree */
175 g_message(_("Conflict with key binding in config file"));
180 /* find if every key in this chain has modifiers, and also find the
181 bottom node of the tree */
182 while (t->first_child) {
188 /* when there are no modifiers in the binding, then the action cannot
190 if (!mods && action->data.any.interactive) {
191 action->data.any.interactive = FALSE;
192 action->data.inter.final = TRUE;
196 t->actions = g_slist_append(t->actions, action);
197 /* assimilate this built tree into the main tree. assimilation
198 destroys/uses the tree */
199 if (tree) tree_assimilate(tree);
204 gboolean keyboard_interactive_grab(guint state, ObClient *client,
207 ObInteractiveState *s;
209 g_assert(action->data.any.interactive);
211 if (!interactive_states) {
212 grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER);
213 if (!grab_keyboard(TRUE)) {
214 grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
219 s = g_new(ObInteractiveState, 1);
223 s->actions = g_slist_append(NULL, action);
225 interactive_states = g_slist_append(interactive_states, s);
230 void keyboard_interactive_end(ObInteractiveState *s,
231 guint state, gboolean cancel, Time time)
233 action_run_interactive(s->actions, s->client, state, time, cancel, TRUE);
235 g_slist_free(s->actions);
238 interactive_states = g_slist_remove(interactive_states, s);
240 if (!interactive_states) {
241 grab_keyboard(FALSE);
242 grab_pointer(FALSE, FALSE, OB_CURSOR_NONE);
246 void keyboard_interactive_end_client(ObClient *client, gpointer data)
250 for (it = interactive_states; it; it = next) {
251 ObInteractiveState *s = it->data;
253 next = g_slist_next(it);
255 if (s->client == client)
260 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
263 gboolean handled = FALSE;
264 gboolean done = FALSE;
265 gboolean cancel = FALSE;
267 for (it = interactive_states; it; it = next) {
268 ObInteractiveState *s = it->data;
270 next = g_slist_next(it);
272 if ((e->type == KeyRelease &&
273 !(s->state & e->xkey.state)))
275 else if (e->type == KeyPress) {
276 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
278 else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
279 cancel = done = TRUE;
280 } else if (e->type == ButtonPress)
281 cancel = done = TRUE;
284 keyboard_interactive_end(s, e->xkey.state, cancel, e->xkey.time);
294 void keyboard_event(ObClient *client, const XEvent *e)
298 g_assert(e->type == KeyPress);
300 if (e->xkey.keycode == config_keyboard_reset_keycode &&
301 e->xkey.state == config_keyboard_reset_state)
303 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
304 keyboard_reset_chains(-1);
309 p = keyboard_firstnode;
311 p = curpos->first_child;
313 if (p->key == e->xkey.keycode &&
314 p->state == e->xkey.state)
316 if (p->first_child != NULL) { /* part of a chain */
317 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
318 /* 3 second timeout for chains */
319 ob_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
321 g_direct_equal, NULL);
323 } else if (p->chroot) /* an empty chroot */
326 keyboard_reset_chains(0);
328 action_run_key(p->actions, client, e->xkey.state,
329 e->xkey.x_root, e->xkey.y_root,
338 gboolean keyboard_interactively_grabbed()
340 return !!interactive_states;
343 void keyboard_startup(gboolean reconfig)
346 popup = popup_new(FALSE);
349 client_add_destructor(keyboard_interactive_end_client, NULL);
352 void keyboard_shutdown(gboolean reconfig)
357 client_remove_destructor(keyboard_interactive_end_client);
359 for (it = interactive_states; it; it = g_slist_next(it))
361 g_slist_free(interactive_states);
362 interactive_states = NULL;
364 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
365 ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
367 keyboard_unbind_all();