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 "menuframe.h"
32 #include "translate.h"
33 #include "moveresize.h"
36 #include "obt/keyboard.h"
40 KeyBindingTree *keyboard_firstnode = NULL;
41 static ObPopup *popup = NULL;
42 static KeyBindingTree *curpos;
44 static void grab_keys(gboolean grab)
48 ungrab_all_keys(obt_root(ob_screen));
51 p = curpos ? curpos->first_child : keyboard_firstnode;
54 grab_key(p->key, p->state, obt_root(ob_screen),
59 grab_key(config_keyboard_reset_keycode,
60 config_keyboard_reset_state,
61 obt_root(ob_screen), GrabModeAsync);
65 static gboolean chain_timeout(gpointer data)
67 keyboard_reset_chains(0);
68 return FALSE; /* don't repeat */
71 static void set_curpos(KeyBindingTree *newpos)
73 if (curpos == newpos) return;
84 for (it = curpos->keylist; it; it = g_list_next(it)) {
85 gchar *oldtext = text;
87 text = g_strdup(it->data);
89 text = g_strconcat(text, " - ", it->data, NULL);
93 a = screen_physical_area_primary(FALSE);
94 popup_position(popup, NorthWestGravity, a->x + 10, a->y + 10);
95 /* 1 second delay for the popup to show */
96 popup_delay_show(popup, G_USEC_PER_SEC, text);
103 void keyboard_reset_chains(gint break_chroots)
107 for (p = curpos; p; p = p->parent) {
109 if (break_chroots == 0) break; /* stop here */
110 if (break_chroots > 0)
117 void keyboard_unbind_all(void)
119 tree_destroy(keyboard_firstnode);
120 keyboard_firstnode = NULL;
123 void keyboard_chroot(GList *keylist)
125 /* try do it in the existing tree. if we can't that means it is an empty
126 chroot binding. so add it to the tree then. */
127 if (!tree_chroot(keyboard_firstnode, keylist)) {
128 KeyBindingTree *tree;
129 if (!(tree = tree_build(keylist)))
131 tree_chroot(tree, keylist);
132 tree_assimilate(tree);
136 gboolean keyboard_bind(GList *keylist, ObActionsAct *action)
138 KeyBindingTree *tree, *t;
141 g_assert(keylist != NULL);
142 g_assert(action != NULL);
144 if (!(tree = tree_build(keylist)))
147 if ((t = tree_find(tree, &conflict)) != NULL) {
148 /* already bound to something, use the existing tree */
155 g_message(_("Conflict with key binding in config file"));
160 /* find the bottom node */
161 for (; t->first_child; t = t->first_child);
164 t->actions = g_slist_append(t->actions, action);
165 /* assimilate this built tree into the main tree. assimilation
166 destroys/uses the tree */
167 if (tree) tree_assimilate(tree);
173 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
175 gboolean handled = FALSE;
176 gboolean done = FALSE;
177 gboolean cancel = FALSE;
180 mods = obt_keyboard_only_modmasks(ev->xkey.state);
183 if ((e->type == KeyRelease && !(istate.state & mods))) {
186 } else if (e->type == KeyPress) {
187 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
189 else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
190 cancel = done = TRUE;
193 } else if (e->type == ButtonPress) {
200 keyboard_interactive_end(e->xkey.state, cancel, e->xkey.time,TRUE);
203 *client = istate.client;
210 gboolean keyboard_event(ObClient *client, const XEvent *e)
216 if (e->type == KeyRelease) {
217 grab_key_passive_count(-1);
221 g_assert(e->type == KeyPress);
222 grab_key_passive_count(1);
224 mods = obt_keyboard_only_modmasks(e->xkey.state);
226 if (e->xkey.keycode == config_keyboard_reset_keycode &&
227 mods == config_keyboard_reset_state)
229 obt_main_loop_timeout_remove(ob_main_loop, chain_timeout);
230 keyboard_reset_chains(-1);
236 p = keyboard_firstnode;
238 p = curpos->first_child;
240 if (p->key == e->xkey.keycode && p->state == mods) {
241 /* if we hit a key binding, then close any open menus and run it */
242 if (menu_frame_visible)
243 menu_frame_hide_all();
245 if (p->first_child != NULL) { /* part of a chain */
246 obt_main_loop_timeout_remove(ob_main_loop, chain_timeout);
247 /* 3 second timeout for chains */
248 obt_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
250 g_direct_equal, NULL);
252 } else if (p->chroot) /* an empty chroot */
257 for (it = p->actions; it; it = g_slist_next(it))
258 if (actions_act_is_interactive(it->data)) break;
259 if (it == NULL) /* reset if the actions are not interactive */
260 keyboard_reset_chains(0);
262 actions_run_acts(p->actions, OB_USER_ACTION_KEYBOARD_KEY,
263 e->xkey.state, e->xkey.x_root, e->xkey.y_root,
264 0, OB_FRAME_CONTEXT_NONE, client);
274 static void node_rebind(KeyBindingTree *node)
276 if (node->first_child) {
277 /* find leaf nodes */
278 node_rebind(node->first_child);
280 /* for internal nodes, add them to the tree if they
281 are a chroot, but do this after adding their
284 keyboard_chroot(node->keylist);
287 /* for leaf nodes, rebind each action assigned to it */
288 while (node->actions) {
289 /* add each action, and remove them from the original tree so
290 they don't get free'd on us */
291 keyboard_bind(node->keylist, node->actions->data);
292 node->actions = g_slist_delete_link(node->actions, node->actions);
296 keyboard_chroot(node->keylist);
299 /* go through each sibling */
300 if (node->next_sibling) node_rebind(node->next_sibling);
303 void keyboard_rebind(void)
307 old = keyboard_firstnode;
308 keyboard_firstnode = NULL;
316 void keyboard_startup(gboolean reconfig)
320 popup_set_text_align(popup, RR_JUSTIFY_CENTER);
323 void keyboard_shutdown(gboolean reconfig)
325 obt_main_loop_timeout_remove(ob_main_loop, chain_timeout);
327 keyboard_unbind_all();