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"
48 KeyBindingTree
*keyboard_firstnode
= NULL
;
49 static ObPopup
*popup
= NULL
;
50 static ObInteractiveState istate
;
51 static KeyBindingTree
*curpos
;
53 static void grab_keys(gboolean grab
)
57 ungrab_all_keys(RootWindow(ob_display
, ob_screen
));
60 p
= curpos
? curpos
->first_child
: keyboard_firstnode
;
62 grab_key(p
->key
, p
->state
, RootWindow(ob_display
, ob_screen
),
67 grab_key(config_keyboard_reset_keycode
,
68 config_keyboard_reset_state
,
69 RootWindow(ob_display
, ob_screen
), GrabModeAsync
);
73 static gboolean
chain_timeout(gpointer data
)
75 keyboard_reset_chains(0);
76 return FALSE
; /* don't repeat */
79 static void set_curpos(KeyBindingTree
*newpos
)
89 for (it
= curpos
->keylist
; it
; it
= g_list_next(it
)) {
90 gchar
*oldtext
= text
;
92 text
= g_strdup(it
->data
);
94 text
= g_strconcat(text
, " - ", it
->data
, NULL
);
98 popup_position(popup
, NorthWestGravity
, 10, 10);
99 /* 1 second delay for the popup to show */
100 popup_delay_show(popup
, G_USEC_PER_SEC
, text
);
107 void keyboard_reset_chains(gint break_chroots
)
111 for (p
= curpos
; p
; p
= p
->parent
) {
113 if (break_chroots
== 0) break; /* stop here */
114 if (break_chroots
> 0)
121 void keyboard_unbind_all()
123 tree_destroy(keyboard_firstnode
);
124 keyboard_firstnode
= NULL
;
127 void keyboard_chroot(GList
*keylist
)
129 /* try do it in the existing tree. if we can't that means it is an empty
130 chroot binding. so add it to the tree then. */
131 if (!tree_chroot(keyboard_firstnode
, keylist
)) {
132 KeyBindingTree
*tree
;
133 if (!(tree
= tree_build(keylist
)))
135 tree_chroot(tree
, keylist
);
136 tree_assimilate(tree
);
140 gboolean
keyboard_bind(GList
*keylist
, ObAction
*action
)
142 KeyBindingTree
*tree
, *t
;
144 gboolean mods
= TRUE
;
146 g_assert(keylist
!= NULL
);
147 g_assert(action
!= NULL
);
149 if (!(tree
= tree_build(keylist
)))
152 if ((t
= tree_find(tree
, &conflict
)) != NULL
) {
153 /* already bound to something, use the existing tree */
160 g_message(_("Conflict with key binding in config file"));
165 /* find if every key in this chain has modifiers, and also find the
166 bottom node of the tree */
167 while (t
->first_child
) {
173 /* when there are no modifiers in the binding, then the action cannot
175 if (!mods
&& action
->data
.any
.interactive
) {
176 action
->data
.any
.interactive
= FALSE
;
177 action
->data
.inter
.final
= TRUE
;
181 t
->actions
= g_slist_append(t
->actions
, action
);
182 /* assimilate this built tree into the main tree. assimilation
183 destroys/uses the tree */
184 if (tree
) tree_assimilate(tree
);
189 static void keyboard_interactive_end(guint state
, gboolean cancel
, Time time
,
194 g_assert(istate
.active
);
196 alist
= g_slist_append(NULL
, istate
.action
);
197 action_run_interactive(alist
, istate
.client
, state
, time
, cancel
, TRUE
);
200 istate
.active
= FALSE
;
203 grab_keyboard(FALSE
);
204 grab_pointer(FALSE
, FALSE
, OB_CURSOR_NONE
);
208 static void keyboard_interactive_end_client(ObClient
*client
, gpointer data
)
210 if (istate
.active
&& istate
.client
== client
)
211 istate
.client
= NULL
;
214 gboolean
keyboard_interactive_grab(guint state
, ObClient
*client
,
217 g_assert(action
->data
.any
.interactive
);
219 if (!istate
.active
) {
220 grab_pointer(TRUE
, FALSE
, OB_CURSOR_POINTER
);
221 if (!grab_keyboard(TRUE
)) {
222 grab_pointer(FALSE
, FALSE
, OB_CURSOR_NONE
);
225 } else if (action
->func
!= istate
.action
->func
) {
226 keyboard_interactive_end(state
, FALSE
, action
->data
.any
.time
, FALSE
);
229 istate
.active
= TRUE
;
230 istate
.state
= state
;
231 istate
.client
= client
;
232 istate
.action
= action
;
237 gboolean
keyboard_process_interactive_grab(const XEvent
*e
, ObClient
**client
)
239 gboolean handled
= FALSE
;
240 gboolean done
= FALSE
;
241 gboolean cancel
= FALSE
;
244 if ((e
->type
== KeyRelease
&& !(istate
.state
& e
->xkey
.state
)))
246 else if (e
->type
== KeyPress
) {
247 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
249 else */if (e
->xkey
.keycode
== ob_keycode(OB_KEY_ESCAPE
))
250 cancel
= done
= TRUE
;
251 } else if (e
->type
== ButtonPress
)
252 cancel
= done
= TRUE
;
255 keyboard_interactive_end(e
->xkey
.state
, cancel
, e
->xkey
.time
,TRUE
);
259 *client
= istate
.client
;
265 void keyboard_event(ObClient
*client
, const XEvent
*e
)
269 g_assert(e
->type
== KeyPress
);
271 if (e
->xkey
.keycode
== config_keyboard_reset_keycode
&&
272 e
->xkey
.state
== config_keyboard_reset_state
)
274 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
275 keyboard_reset_chains(-1);
280 p
= keyboard_firstnode
;
282 p
= curpos
->first_child
;
284 if (p
->key
== e
->xkey
.keycode
&&
285 p
->state
== e
->xkey
.state
)
287 /* if we hit a key binding, then close any open menus and run it */
288 if (menu_frame_visible
)
289 menu_frame_hide_all();
291 if (p
->first_child
!= NULL
) { /* part of a chain */
292 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
293 /* 3 second timeout for chains */
294 ob_main_loop_timeout_add(ob_main_loop
, 3 * G_USEC_PER_SEC
,
296 g_direct_equal
, NULL
);
298 } else if (p
->chroot
) /* an empty chroot */
301 keyboard_reset_chains(0);
303 action_run_key(p
->actions
, client
, e
->xkey
.state
,
304 e
->xkey
.x_root
, e
->xkey
.y_root
,
313 gboolean
keyboard_interactively_grabbed()
315 return istate
.active
;
318 void keyboard_startup(gboolean reconfig
)
321 popup
= popup_new(FALSE
);
324 client_add_destructor(keyboard_interactive_end_client
, NULL
);
327 void keyboard_shutdown(gboolean reconfig
)
330 client_remove_destructor(keyboard_interactive_end_client
);
333 keyboard_interactive_end(0, TRUE
, 0, TRUE
);
335 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
337 keyboard_unbind_all();