1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 keyboard.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
32 #include "translate.h"
33 #include "moveresize.h"
37 KeyBindingTree
*keyboard_firstnode
;
43 ObFrameContext context
;
46 static GSList
*interactive_states
;
48 static KeyBindingTree
*curpos
;
50 static void grab_for_window(Window win
, gboolean grab
)
57 p
= curpos
? curpos
->first_child
: keyboard_firstnode
;
59 grab_key(p
->key
, p
->state
, win
, GrabModeAsync
);
63 grab_key(config_keyboard_reset_keycode
,
64 config_keyboard_reset_state
,
69 void keyboard_grab_for_client(ObClient
*c
, gboolean grab
)
71 grab_for_window(c
->window
, grab
);
74 static void grab_keys(gboolean grab
)
78 grab_for_window(screen_support_win
, grab
);
79 for (it
= client_list
; it
; it
= g_list_next(it
))
80 grab_for_window(((ObClient
*)it
->data
)->window
, grab
);
83 static gboolean
chain_timeout(gpointer data
)
85 keyboard_reset_chains();
87 return FALSE
; /* don't repeat */
90 void keyboard_reset_chains()
92 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
101 void keyboard_unbind_all()
103 tree_destroy(keyboard_firstnode
);
104 keyboard_firstnode
= NULL
;
109 gboolean
keyboard_bind(GList
*keylist
, ObAction
*action
)
111 KeyBindingTree
*tree
, *t
;
113 gboolean mods
= TRUE
;
115 g_assert(keylist
!= NULL
);
116 g_assert(action
!= NULL
);
118 if (!(tree
= tree_build(keylist
)))
121 if ((t
= tree_find(tree
, &conflict
)) != NULL
) {
122 /* already bound to something, use the existing tree */
129 g_warning("conflict with binding");
134 /* find if every key in this chain has modifiers, and also find the
135 bottom node of the tree */
136 while (t
->first_child
) {
142 /* when there are no modifiers in the binding, then the action cannot
144 if (!mods
&& action
->data
.any
.interactive
) {
145 action
->data
.any
.interactive
= FALSE
;
146 action
->data
.inter
.final
= TRUE
;
150 t
->actions
= g_slist_append(t
->actions
, action
);
151 /* assimilate this built tree into the main tree. assimilation
152 destroys/uses the tree */
153 if (tree
) tree_assimilate(tree
);
158 gboolean
keyboard_interactive_grab(guint state
, ObClient
*client
,
161 ObInteractiveState
*s
;
163 g_assert(action
->data
.any
.interactive
);
165 if (!interactive_states
) {
166 if (!grab_keyboard(TRUE
))
168 if (!grab_pointer(TRUE
, OB_CURSOR_NONE
)) {
169 grab_keyboard(FALSE
);
174 s
= g_new(ObInteractiveState
, 1);
178 s
->actions
= g_slist_append(NULL
, action
);
180 interactive_states
= g_slist_append(interactive_states
, s
);
185 void keyboard_interactive_end(ObInteractiveState
*s
,
186 guint state
, gboolean cancel
)
188 action_run_interactive(s
->actions
, s
->client
, state
, cancel
, TRUE
);
190 g_slist_free(s
->actions
);
193 interactive_states
= g_slist_remove(interactive_states
, s
);
195 if (!interactive_states
) {
196 grab_keyboard(FALSE
);
197 grab_pointer(FALSE
, OB_CURSOR_NONE
);
198 keyboard_reset_chains();
202 void keyboard_interactive_end_client(ObClient
*client
, gpointer data
)
206 for (it
= interactive_states
; it
; it
= next
) {
207 ObInteractiveState
*s
= it
->data
;
209 next
= g_slist_next(it
);
211 if (s
->client
== client
)
216 gboolean
keyboard_process_interactive_grab(const XEvent
*e
, ObClient
**client
)
219 gboolean handled
= FALSE
;
220 gboolean done
= FALSE
;
221 gboolean cancel
= FALSE
;
223 for (it
= interactive_states
; it
; it
= next
) {
224 ObInteractiveState
*s
= it
->data
;
226 next
= g_slist_next(it
);
228 if ((e
->type
== KeyRelease
&&
229 !(s
->state
& e
->xkey
.state
)))
231 else if (e
->type
== KeyPress
) {
232 if (e
->xkey
.keycode
== ob_keycode(OB_KEY_RETURN
))
234 else if (e
->xkey
.keycode
== ob_keycode(OB_KEY_ESCAPE
))
235 cancel
= done
= TRUE
;
238 keyboard_interactive_end(s
, e
->xkey
.state
, cancel
);
248 void keyboard_event(ObClient
*client
, const XEvent
*e
)
252 g_assert(e
->type
== KeyPress
);
254 if (e
->xkey
.keycode
== config_keyboard_reset_keycode
&&
255 e
->xkey
.state
== config_keyboard_reset_state
)
257 keyboard_reset_chains();
262 p
= keyboard_firstnode
;
264 p
= curpos
->first_child
;
266 if (p
->key
== e
->xkey
.keycode
&&
267 p
->state
== e
->xkey
.state
)
269 if (p
->first_child
!= NULL
) { /* part of a chain */
270 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
271 /* 5 second timeout for chains */
272 ob_main_loop_timeout_add(ob_main_loop
, 5 * G_USEC_PER_SEC
,
273 chain_timeout
, NULL
, NULL
);
279 keyboard_reset_chains();
281 action_run_key(p
->actions
, client
, e
->xkey
.state
,
282 e
->xkey
.x_root
, e
->xkey
.y_root
);
290 void keyboard_startup(gboolean reconfig
)
295 client_add_destructor(keyboard_interactive_end_client
, NULL
);
298 void keyboard_shutdown(gboolean reconfig
)
303 client_remove_destructor(keyboard_interactive_end_client
);
305 for (it
= interactive_states
; it
; it
= g_slist_next(it
))
307 g_slist_free(interactive_states
);
308 interactive_states
= NULL
;
310 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
312 keyboard_unbind_all();