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
;
55 grab_key(p
->key
, p
->state
, RootWindow(ob_display
, ob_screen
),
60 grab_key(config_keyboard_reset_keycode
,
61 config_keyboard_reset_state
,
62 RootWindow(ob_display
, ob_screen
), GrabModeSync
);
66 static gboolean
chain_timeout(gpointer data
)
68 keyboard_reset_chains(0);
69 return FALSE
; /* don't repeat */
72 static void set_curpos(KeyBindingTree
*newpos
)
74 if (curpos
== newpos
) return;
85 for (it
= curpos
->keylist
; it
; it
= g_list_next(it
)) {
86 gchar
*oldtext
= text
;
88 text
= g_strdup(it
->data
);
90 text
= g_strconcat(text
, " - ", it
->data
, NULL
);
94 a
= screen_physical_area_active();
95 popup_position(popup
, NorthWestGravity
, a
->x
+ 10, a
->y
+ 10);
96 /* 1 second delay for the popup to show */
97 popup_delay_show(popup
, G_USEC_PER_SEC
, text
);
105 void keyboard_reset_chains(gint break_chroots
)
109 for (p
= curpos
; p
; p
= p
->parent
) {
111 if (break_chroots
== 0) break; /* stop here */
112 if (break_chroots
> 0)
119 void keyboard_unbind_all(void)
121 tree_destroy(keyboard_firstnode
);
122 keyboard_firstnode
= NULL
;
125 void keyboard_chroot(GList
*keylist
)
127 /* try do it in the existing tree. if we can't that means it is an empty
128 chroot binding. so add it to the tree then. */
129 if (!tree_chroot(keyboard_firstnode
, keylist
)) {
130 KeyBindingTree
*tree
;
131 if (!(tree
= tree_build(keylist
)))
133 tree_chroot(tree
, keylist
);
134 tree_assimilate(tree
);
138 gboolean
keyboard_bind(GList
*keylist
, ObActionsAct
*action
)
140 KeyBindingTree
*tree
, *t
;
143 g_assert(keylist
!= NULL
);
144 g_assert(action
!= NULL
);
146 if (!(tree
= tree_build(keylist
)))
149 if ((t
= tree_find(tree
, &conflict
)) != NULL
) {
150 /* already bound to something, use the existing tree */
157 g_message(_("Conflict with key binding in config file"));
162 /* find the bottom node */
163 for (; t
->first_child
; t
= t
->first_child
);
166 t
->actions
= g_slist_append(t
->actions
, action
);
167 /* assimilate this built tree into the main tree. assimilation
168 destroys/uses the tree */
169 if (tree
) tree_assimilate(tree
);
175 gboolean
keyboard_process_interactive_grab(const XEvent
*e
, ObClient
**client
)
177 gboolean handled
= FALSE
;
178 gboolean done
= FALSE
;
179 gboolean cancel
= FALSE
;
182 if ((e
->type
== KeyRelease
&& !(istate
.state
& e
->xkey
.state
))) {
185 } else if (e
->type
== KeyPress
) {
186 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
188 else */if (e
->xkey
.keycode
== ob_keycode(OB_KEY_ESCAPE
)) {
189 cancel
= done
= TRUE
;
192 } else if (e
->type
== ButtonPress
) {
199 keyboard_interactive_end(e
->xkey
.state
, cancel
, e
->xkey
.time
,TRUE
);
202 *client
= istate
.client
;
209 void keyboard_event(ObClient
*client
, const XEvent
*e
)
213 if (e
->type
== KeyRelease
) {
214 grab_key_passive_count(-1);
218 g_assert(e
->type
== KeyPress
);
219 grab_key_passive_count(1);
221 if (e
->xkey
.keycode
== config_keyboard_reset_keycode
&&
222 e
->xkey
.state
== config_keyboard_reset_state
)
224 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
225 keyboard_reset_chains(-1);
230 p
= keyboard_firstnode
;
232 p
= curpos
->first_child
;
234 if (p
->key
== e
->xkey
.keycode
&&
235 p
->state
== e
->xkey
.state
)
237 /* if we hit a key binding, then close any open menus and run it */
238 if (menu_frame_visible
)
239 menu_frame_hide_all();
241 if (p
->first_child
!= NULL
) { /* part of a chain */
242 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
243 /* 3 second timeout for chains */
244 ob_main_loop_timeout_add(ob_main_loop
, 3 * G_USEC_PER_SEC
,
246 g_direct_equal
, NULL
);
248 } else if (p
->chroot
) /* an empty chroot */
253 for (it
= p
->actions
; it
; it
= g_slist_next(it
))
254 if (actions_act_is_interactive(it
->data
)) break;
255 if (it
== NULL
) /* reset if the actions are not interactive */
256 keyboard_reset_chains(0);
258 actions_run_acts(p
->actions
, OB_USER_ACTION_KEYBOARD_KEY
,
259 e
->xkey
.state
, e
->xkey
.x_root
, e
->xkey
.y_root
,
260 0, OB_FRAME_CONTEXT_NONE
, client
);
267 XAllowEvents(ob_display
, AsyncKeyboard
, event_curtime
);
270 static void node_rebind(KeyBindingTree
*node
)
272 if (node
->first_child
) {
273 /* find leaf nodes */
274 node_rebind(node
->first_child
);
276 /* for internal nodes, add them to the tree if they
277 are a chroot, but do this after adding their
280 keyboard_chroot(node
->keylist
);
283 /* for leaf nodes, rebind each action assigned to it */
284 while (node
->actions
) {
285 /* add each action, and remove them from the original tree so
286 they don't get free'd on us */
287 keyboard_bind(node
->keylist
, node
->actions
->data
);
288 node
->actions
= g_slist_delete_link(node
->actions
, node
->actions
);
292 keyboard_chroot(node
->keylist
);
295 /* go through each sibling */
296 if (node
->next_sibling
) node_rebind(node
->next_sibling
);
299 void keyboard_rebind(void)
303 old
= keyboard_firstnode
;
304 keyboard_firstnode
= NULL
;
312 void keyboard_startup(gboolean reconfig
)
316 popup_set_text_align(popup
, RR_JUSTIFY_CENTER
);
319 void keyboard_shutdown(gboolean reconfig
)
321 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
323 keyboard_unbind_all();