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.
28 #include "action_list.h"
29 #include "action_list_run.h"
30 #include "menuframe.h"
34 #include "translate.h"
35 #include "moveresize.h"
38 #include "obt/keyboard.h"
42 KeyBindingTree *keyboard_firstnode = NULL;
43 static ObPopup *popup = NULL;
44 static KeyBindingTree *curpos;
45 static guint chain_timer = 0;
47 static void grab_keys(gboolean grab)
51 ungrab_all_keys(obt_root(ob_screen));
54 p = curpos ? curpos->first_child : keyboard_firstnode;
57 grab_key(p->key, p->state, obt_root(ob_screen),
62 grab_key(config_keyboard_reset_keycode,
63 config_keyboard_reset_state,
64 obt_root(ob_screen), GrabModeAsync);
68 static gboolean chain_timeout(gpointer data)
70 keyboard_reset_chains(0);
71 return FALSE; /* don't repeat */
74 static void chain_done(gpointer data)
79 static void set_curpos(KeyBindingTree *newpos)
81 if (curpos == newpos) return;
92 for (it = curpos->keylist; it; it = g_list_next(it)) {
93 gchar *oldtext = text;
95 text = g_strdup(it->data);
97 text = g_strconcat(text, " - ", it->data, NULL);
101 a = screen_physical_area_primary(FALSE);
102 popup_position(popup, NorthWestGravity, a->x + 10, a->y + 10);
103 /* 1 second delay for the popup to show */
104 popup_delay_show(popup, 1000, text);
111 void keyboard_reset_chains(gint break_chroots)
115 for (p = curpos; p; p = p->parent) {
117 if (break_chroots == 0) break; /* stop here */
118 if (break_chroots > 0)
125 void keyboard_unbind_all(void)
127 tree_destroy(keyboard_firstnode);
128 keyboard_firstnode = NULL;
131 void keyboard_chroot(GList *keylist)
133 /* try do it in the existing tree. if we can't that means it is an empty
134 chroot binding. so add it to the tree then. */
135 if (!tree_chroot(keyboard_firstnode, keylist)) {
136 KeyBindingTree *tree;
137 if (!(tree = tree_build(keylist)))
139 tree_chroot(tree, keylist);
140 tree_assimilate(tree);
144 gboolean keyboard_bind(GList *keylist, ObActionList *actions)
146 KeyBindingTree *tree, *t;
149 g_assert(keylist != NULL);
154 if (!(tree = tree_build(keylist)))
157 if ((t = tree_find(tree, &conflict)) != NULL) {
158 /* already bound to something, use the existing tree */
165 g_message(_("Conflict with key binding in config file"));
170 /* find the bottom node */
171 for (; t->first_child; t = t->first_child);
174 action_list_ref(actions);
175 t->actions = action_list_concat(t->actions, actions);
177 /* assimilate this built tree into the main tree. assimilation
178 destroys/uses the tree */
179 if (tree) tree_assimilate(tree);
185 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
187 gboolean handled = FALSE;
188 gboolean done = FALSE;
189 gboolean cancel = FALSE;
192 mods = obt_keyboard_only_modmasks(ev->xkey.state);
195 if ((e->type == KeyRelease && !(istate.state & mods))) {
198 } else if (e->type == KeyPress) {
199 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
201 else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
202 cancel = done = TRUE;
205 } else if (e->type == ButtonPress) {
212 keyboard_interactive_end(e->xkey.state, cancel, e->xkey.time,TRUE);
215 *client = istate.client;
222 gboolean keyboard_event(ObClient *client, const XEvent *e)
228 if (e->type == KeyRelease) {
229 grab_key_passive_count(-1);
233 g_assert(e->type == KeyPress);
234 grab_key_passive_count(1);
236 mods = obt_keyboard_only_modmasks(e->xkey.state);
238 if (e->xkey.keycode == config_keyboard_reset_keycode &&
239 mods == config_keyboard_reset_state)
241 if (chain_timer) g_source_remove(chain_timer);
242 keyboard_reset_chains(-1);
248 p = keyboard_firstnode;
250 p = curpos->first_child;
252 if (p->key == e->xkey.keycode && p->state == mods) {
253 /* if we hit a key binding, then close any open menus and run it */
254 if (menu_frame_visible)
255 menu_frame_hide_all();
257 if (p->first_child != NULL) { /* part of a chain */
258 if (chain_timer) g_source_remove(chain_timer);
259 /* 3 second timeout for chains */
261 g_timeout_add_full(G_PRIORITY_DEFAULT,
262 3000, chain_timeout, NULL,
265 } else if (p->chroot) /* an empty chroot */
270 i = action_list_run(p->actions, OB_USER_ACTION_KEYBOARD_KEY,
272 e->xkey.x_root, e->xkey.y_root,
273 0, OB_FRAME_CONTEXT_NONE, client);
274 if (!i) /* reset if an interactive was not run */
275 keyboard_reset_chains(0);
285 static void node_rebind(KeyBindingTree *node)
287 if (node->first_child) {
288 /* find leaf nodes */
289 node_rebind(node->first_child);
291 /* for internal nodes, add them to the tree if they
292 are a chroot, but do this after adding their
295 keyboard_chroot(node->keylist);
298 /* for leaf nodes, rebind each action assigned to it */
299 keyboard_bind(node->keylist, node->actions);
302 keyboard_chroot(node->keylist);
305 /* go through each sibling */
306 if (node->next_sibling) node_rebind(node->next_sibling);
309 void keyboard_rebind(void)
313 old = keyboard_firstnode;
314 keyboard_firstnode = NULL;
323 void keyboard_startup(gboolean reconfig)
327 popup_set_text_align(popup, RR_JUSTIFY_CENTER);
330 void keyboard_shutdown(gboolean reconfig)
332 if (chain_timer) g_source_remove(chain_timer);
334 keyboard_unbind_all();