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.
30 #include "menuframe.h"
34 #include "translate.h"
35 #include "moveresize.h"
41 KeyBindingTree *keyboard_firstnode = NULL;
42 static ObPopup *popup = NULL;
43 static KeyBindingTree *curpos;
45 static void grab_keys(gboolean grab)
49 ungrab_all_keys(RootWindow(ob_display, ob_screen));
52 p = curpos ? curpos->first_child : keyboard_firstnode;
54 grab_key(p->key, p->state, RootWindow(ob_display, ob_screen),
59 grab_key(config_keyboard_reset_keycode,
60 config_keyboard_reset_state,
61 RootWindow(ob_display, 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_active();
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);
104 void keyboard_reset_chains(gint break_chroots)
108 for (p = curpos; p; p = p->parent) {
110 if (break_chroots == 0) break; /* stop here */
111 if (break_chroots > 0)
118 void keyboard_unbind_all()
120 tree_destroy(keyboard_firstnode);
121 keyboard_firstnode = NULL;
124 void keyboard_chroot(GList *keylist)
126 /* try do it in the existing tree. if we can't that means it is an empty
127 chroot binding. so add it to the tree then. */
128 if (!tree_chroot(keyboard_firstnode, keylist)) {
129 KeyBindingTree *tree;
130 if (!(tree = tree_build(keylist)))
132 tree_chroot(tree, keylist);
133 tree_assimilate(tree);
137 gboolean keyboard_bind(GList *keylist, ObActionsAct *action)
139 KeyBindingTree *tree, *t;
142 g_assert(keylist != NULL);
143 g_assert(action != NULL);
145 if (!(tree = tree_build(keylist)))
148 if ((t = tree_find(tree, &conflict)) != NULL) {
149 /* already bound to something, use the existing tree */
156 g_message(_("Conflict with key binding in config file"));
161 /* find the bottom node */
162 for (; t->first_child; t = t->first_child);
165 t->actions = g_slist_append(t->actions, action);
166 /* assimilate this built tree into the main tree. assimilation
167 destroys/uses the tree */
168 if (tree) tree_assimilate(tree);
174 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
176 gboolean handled = FALSE;
177 gboolean done = FALSE;
178 gboolean cancel = FALSE;
181 if ((e->type == KeyRelease && !(istate.state & e->xkey.state))) {
184 } else if (e->type == KeyPress) {
185 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
187 else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
188 cancel = done = TRUE;
191 } else if (e->type == ButtonPress) {
198 keyboard_interactive_end(e->xkey.state, cancel, e->xkey.time,TRUE);
201 *client = istate.client;
208 void keyboard_event(ObClient *client, const XEvent *e)
212 if (e->type == KeyRelease) {
213 grab_key_passive_count(-1);
217 g_assert(e->type == KeyPress);
218 grab_key_passive_count(1);
220 if (e->xkey.keycode == config_keyboard_reset_keycode &&
221 e->xkey.state == config_keyboard_reset_state)
223 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
224 keyboard_reset_chains(-1);
229 p = keyboard_firstnode;
231 p = curpos->first_child;
233 if (p->key == e->xkey.keycode &&
234 p->state == e->xkey.state)
236 /* if we hit a key binding, then close any open menus and run it */
237 if (menu_frame_visible)
238 menu_frame_hide_all();
240 if (p->first_child != NULL) { /* part of a chain */
241 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
242 /* 3 second timeout for chains */
243 ob_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
245 g_direct_equal, NULL);
247 } else if (p->chroot) /* an empty chroot */
252 for (it = p->actions; it; it = g_slist_next(it))
253 if (actions_act_is_interactive(it->data)) break;
254 if (it == NULL) /* reset if the actions are not interactive */
255 keyboard_reset_chains(0);
257 actions_run_acts(p->actions, OB_USER_ACTION_KEYBOARD_KEY,
258 e->xkey.state, e->xkey.x_root, e->xkey.y_root,
259 0, OB_FRAME_CONTEXT_NONE, client);
267 void keyboard_startup(gboolean reconfig)
270 popup = popup_new(FALSE);
271 popup_set_text_align(popup, RR_JUSTIFY_CENTER);
274 void keyboard_shutdown(gboolean reconfig)
276 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
278 keyboard_unbind_all();