14 #include "translate.h"
15 #include "moveresize.h"
19 KeyBindingTree
*keyboard_firstnode
;
25 ObFrameContext context
;
28 static GSList
*interactive_states
;
30 static KeyBindingTree
*curpos
;
32 static void grab_for_window(Window win
, gboolean grab
)
39 p
= curpos
? curpos
->first_child
: keyboard_firstnode
;
41 grab_key(p
->key
, p
->state
, win
, GrabModeAsync
);
45 grab_key(config_keyboard_reset_keycode
,
46 config_keyboard_reset_state
,
51 void keyboard_grab_for_client(ObClient
*c
, gboolean grab
)
53 grab_for_window(c
->window
, grab
);
56 static void grab_keys(gboolean grab
)
60 grab_for_window(screen_support_win
, grab
);
61 for (it
= client_list
; it
; it
= g_list_next(it
))
62 grab_for_window(((ObClient
*)it
->data
)->window
, grab
);
65 static gboolean
chain_timeout(gpointer data
)
67 keyboard_reset_chains();
69 return FALSE
; /* don't repeat */
72 void keyboard_reset_chains()
74 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
83 gboolean
keyboard_bind(GList
*keylist
, ObAction
*action
)
85 KeyBindingTree
*tree
, *t
;
89 g_assert(keylist
!= NULL
);
90 g_assert(action
!= NULL
);
92 if (!(tree
= tree_build(keylist
)))
95 if ((t
= tree_find(tree
, &conflict
)) != NULL
) {
96 /* already bound to something, use the existing tree */
103 g_warning("conflict with binding");
108 /* find if every key in this chain has modifiers, and also find the
109 bottom node of the tree */
110 while (t
->first_child
) {
116 /* when there are no modifiers in the binding, then the action cannot
118 if (!mods
&& action
->data
.any
.interactive
) {
119 action
->data
.any
.interactive
= FALSE
;
120 action
->data
.inter
.final
= TRUE
;
124 t
->actions
= g_slist_append(t
->actions
, action
);
125 /* assimilate this built tree into the main tree. assimilation
126 destroys/uses the tree */
127 if (tree
) tree_assimilate(tree
);
132 void keyboard_interactive_grab(guint state
, ObClient
*client
,
135 ObInteractiveState
*s
;
137 g_assert(action
->data
.any
.interactive
);
139 if (moveresize_in_progress
)
140 moveresize_end(FALSE
);
142 if (!interactive_states
) {
143 if (!grab_keyboard(TRUE
))
145 if (!grab_pointer(TRUE
, OB_CURSOR_NONE
)) {
146 grab_keyboard(FALSE
);
151 s
= g_new(ObInteractiveState
, 1);
157 interactive_states
= g_slist_append(interactive_states
, s
);
160 void keyboard_interactive_end(ObInteractiveState
*s
,
161 guint state
, gboolean cancel
)
163 action_run_interactive(s
->action
, s
->client
, state
, cancel
, TRUE
);
167 interactive_states
= g_slist_remove(interactive_states
, s
);
169 if (!interactive_states
) {
170 grab_keyboard(FALSE
);
171 grab_pointer(FALSE
, OB_CURSOR_NONE
);
172 keyboard_reset_chains();
176 void keyboard_interactive_end_client(gpointer data
)
181 for (it
= interactive_states
; it
; it
= next
) {
182 ObInteractiveState
*s
= it
->data
;
184 next
= g_slist_next(it
);
187 keyboard_interactive_end(s
, 0, FALSE
);
191 gboolean
keyboard_process_interactive_grab(const XEvent
*e
, ObClient
**client
)
194 gboolean handled
= FALSE
;
195 gboolean done
= FALSE
;
196 gboolean cancel
= FALSE
;
198 for (it
= interactive_states
; it
; it
= next
) {
199 ObInteractiveState
*s
= it
->data
;
201 next
= g_slist_next(it
);
203 if ((e
->type
== KeyRelease
&&
204 !(s
->state
& e
->xkey
.state
)))
206 else if (e
->type
== KeyPress
) {
207 if (e
->xkey
.keycode
== ob_keycode(OB_KEY_RETURN
))
209 else if (e
->xkey
.keycode
== ob_keycode(OB_KEY_ESCAPE
))
210 cancel
= done
= TRUE
;
213 keyboard_interactive_end(s
, e
->xkey
.state
, cancel
);
223 void keyboard_event(ObClient
*client
, const XEvent
*e
)
227 g_assert(e
->type
== KeyPress
);
229 if (e
->xkey
.keycode
== config_keyboard_reset_keycode
&&
230 e
->xkey
.state
== config_keyboard_reset_state
)
232 keyboard_reset_chains();
237 p
= keyboard_firstnode
;
239 p
= curpos
->first_child
;
241 if (p
->key
== e
->xkey
.keycode
&&
242 p
->state
== e
->xkey
.state
)
244 if (p
->first_child
!= NULL
) { /* part of a chain */
245 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
246 /* 5 second timeout for chains */
247 ob_main_loop_timeout_add(ob_main_loop
, 5 * G_USEC_PER_SEC
,
248 chain_timeout
, NULL
, NULL
);
255 for (it
= p
->actions
; it
; it
= it
->next
)
256 action_run_key(it
->data
, client
, e
->xkey
.state
,
257 e
->xkey
.x_root
, e
->xkey
.y_root
);
259 keyboard_reset_chains();
267 void keyboard_startup(gboolean reconfig
)
272 client_add_destructor(keyboard_interactive_end_client
);
275 void keyboard_shutdown(gboolean reconfig
)
280 client_remove_destructor(keyboard_interactive_end_client
);
282 tree_destroy(keyboard_firstnode
);
283 keyboard_firstnode
= NULL
;
285 for (it
= interactive_states
; it
; it
= g_slist_next(it
))
287 g_slist_free(interactive_states
);
288 interactive_states
= NULL
;
290 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);