1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 keyboard.c for the Openbox window manager
4 Copyright (c) 2004 Mikael Magnusson
5 Copyright (c) 2003 Ben 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.
33 #include "translate.h"
34 #include "moveresize.h"
38 KeyBindingTree
*keyboard_firstnode
;
44 ObFrameContext context
;
47 static GSList
*interactive_states
;
49 static KeyBindingTree
*curpos
;
51 static void grab_for_window(Window win
, gboolean grab
)
58 p
= curpos
? curpos
->first_child
: keyboard_firstnode
;
60 grab_key(p
->key
, p
->state
, win
, GrabModeAsync
);
64 grab_key(config_keyboard_reset_keycode
,
65 config_keyboard_reset_state
,
70 void keyboard_grab_for_client(ObClient
*c
, gboolean grab
)
72 grab_for_window(c
->window
, grab
);
75 static void grab_keys(gboolean grab
)
79 grab_for_window(screen_support_win
, grab
);
80 for (it
= client_list
; it
; it
= g_list_next(it
))
81 grab_for_window(((ObClient
*)it
->data
)->window
, grab
);
84 static gboolean
chain_timeout(gpointer data
)
86 keyboard_reset_chains();
88 return FALSE
; /* don't repeat */
91 void keyboard_reset_chains()
93 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
102 void keyboard_unbind_all()
104 tree_destroy(keyboard_firstnode
);
105 keyboard_firstnode
= NULL
;
110 gboolean
keyboard_bind(GList
*keylist
, ObAction
*action
)
112 KeyBindingTree
*tree
, *t
;
114 gboolean mods
= TRUE
;
116 g_assert(keylist
!= NULL
);
117 g_assert(action
!= NULL
);
119 if (!(tree
= tree_build(keylist
)))
122 if ((t
= tree_find(tree
, &conflict
)) != NULL
) {
123 /* already bound to something, use the existing tree */
130 g_warning("conflict with binding");
135 /* find if every key in this chain has modifiers, and also find the
136 bottom node of the tree */
137 while (t
->first_child
) {
143 /* when there are no modifiers in the binding, then the action cannot
145 if (!mods
&& action
->data
.any
.interactive
) {
146 action
->data
.any
.interactive
= FALSE
;
147 action
->data
.inter
.final
= TRUE
;
151 t
->actions
= g_slist_append(t
->actions
, action
);
152 /* assimilate this built tree into the main tree. assimilation
153 destroys/uses the tree */
154 if (tree
) tree_assimilate(tree
);
159 gboolean
keyboard_interactive_grab(guint state
, ObClient
*client
,
162 ObInteractiveState
*s
;
164 g_assert(action
->data
.any
.interactive
);
166 if (!interactive_states
) {
167 if (!grab_keyboard(TRUE
))
169 if (!grab_pointer(TRUE
, OB_CURSOR_NONE
)) {
170 grab_keyboard(FALSE
);
175 s
= g_new(ObInteractiveState
, 1);
179 s
->actions
= g_slist_append(NULL
, action
);
181 interactive_states
= g_slist_append(interactive_states
, s
);
186 void keyboard_interactive_end(ObInteractiveState
*s
,
187 guint state
, gboolean cancel
)
189 action_run_interactive(s
->actions
, s
->client
, state
, cancel
, TRUE
);
191 g_slist_free(s
->actions
);
194 interactive_states
= g_slist_remove(interactive_states
, s
);
196 if (!interactive_states
) {
197 grab_keyboard(FALSE
);
198 grab_pointer(FALSE
, OB_CURSOR_NONE
);
199 keyboard_reset_chains();
203 void keyboard_interactive_end_client(ObClient
*client
, gpointer data
)
207 for (it
= interactive_states
; it
; it
= next
) {
208 ObInteractiveState
*s
= it
->data
;
210 next
= g_slist_next(it
);
212 if (s
->client
== client
)
217 gboolean
keyboard_process_interactive_grab(const XEvent
*e
, ObClient
**client
)
220 gboolean handled
= FALSE
;
221 gboolean done
= FALSE
;
222 gboolean cancel
= FALSE
;
224 for (it
= interactive_states
; it
; it
= next
) {
225 ObInteractiveState
*s
= it
->data
;
227 next
= g_slist_next(it
);
229 if ((e
->type
== KeyRelease
&&
230 !(s
->state
& e
->xkey
.state
)))
232 else if (e
->type
== KeyPress
) {
233 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
235 else */if (e
->xkey
.keycode
== ob_keycode(OB_KEY_ESCAPE
))
236 cancel
= done
= TRUE
;
239 keyboard_interactive_end(s
, e
->xkey
.state
, cancel
);
249 void keyboard_event(ObClient
*client
, const XEvent
*e
)
253 g_assert(e
->type
== KeyPress
);
255 if (e
->xkey
.keycode
== config_keyboard_reset_keycode
&&
256 e
->xkey
.state
== config_keyboard_reset_state
)
258 keyboard_reset_chains();
263 p
= keyboard_firstnode
;
265 p
= curpos
->first_child
;
267 if (p
->key
== e
->xkey
.keycode
&&
268 p
->state
== e
->xkey
.state
)
270 if (p
->first_child
!= NULL
) { /* part of a chain */
271 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
272 /* 5 second timeout for chains */
273 ob_main_loop_timeout_add(ob_main_loop
, 5 * G_USEC_PER_SEC
,
274 chain_timeout
, NULL
, NULL
);
280 keyboard_reset_chains();
282 action_run_key(p
->actions
, client
, e
->xkey
.state
,
283 e
->xkey
.x_root
, e
->xkey
.y_root
);
291 void keyboard_startup(gboolean reconfig
)
296 client_add_destructor(keyboard_interactive_end_client
, NULL
);
299 void keyboard_shutdown(gboolean reconfig
)
304 client_remove_destructor(keyboard_interactive_end_client
);
306 for (it
= interactive_states
; it
; it
= g_slist_next(it
))
308 g_slist_free(interactive_states
);
309 interactive_states
= NULL
;
311 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
313 keyboard_unbind_all();