14 #include "translate.h"
18 KeyBindingTree
*keyboard_firstnode
;
24 ObFrameContext context
;
27 static GSList
*interactive_states
;
29 static KeyBindingTree
*curpos
;
31 static void grab_for_window(Window win
, gboolean grab
)
38 p
= curpos
? curpos
->first_child
: keyboard_firstnode
;
40 grab_key(p
->key
, p
->state
, win
, GrabModeAsync
);
44 grab_key(config_keyboard_reset_keycode
,
45 config_keyboard_reset_state
,
50 void keyboard_grab_for_client(ObClient
*c
, gboolean grab
)
52 grab_for_window(c
->window
, grab
);
55 static void grab_keys(gboolean grab
)
59 grab_for_window(screen_support_win
, grab
);
60 for (it
= client_list
; it
; it
= g_list_next(it
))
61 grab_for_window(((ObClient
*)it
->data
)->frame
->window
, grab
);
64 static gboolean
chain_timeout(gpointer data
)
66 keyboard_reset_chains();
68 return FALSE
; /* don't repeat */
71 void keyboard_reset_chains()
73 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
81 gboolean
keyboard_bind(GList
*keylist
, ObAction
*action
)
83 KeyBindingTree
*tree
, *t
;
86 g_assert(keylist
!= NULL
);
87 g_assert(action
!= NULL
);
89 if (!(tree
= tree_build(keylist
)))
92 if ((t
= tree_find(tree
, &conflict
)) != NULL
) {
93 /* already bound to something, use the existing tree */
98 while (t
->first_child
) t
= t
->first_child
;
101 g_warning("conflict with binding");
107 t
->actions
= g_slist_append(t
->actions
, action
);
108 /* assimilate this built tree into the main tree. assimilation
109 destroys/uses the tree */
110 if (tree
) tree_assimilate(tree
);
115 void keyboard_interactive_grab(guint state
, ObClient
*client
,
116 ObFrameContext context
, ObAction
*action
)
118 ObInteractiveState
*s
;
120 if (!interactive_states
) {
121 if (!grab_keyboard(TRUE
))
123 if (!grab_pointer(TRUE
, None
)) {
124 grab_keyboard(FALSE
);
129 s
= g_new(ObInteractiveState
, 1);
134 s
->context
= context
;
136 interactive_states
= g_slist_append(interactive_states
, s
);
139 gboolean
keyboard_process_interactive_grab(const XEvent
*e
,
141 ObFrameContext
*context
)
144 gboolean handled
= FALSE
;
145 gboolean done
= FALSE
;
146 gboolean cancel
= FALSE
;
148 for (it
= interactive_states
; it
; it
= next
) {
149 ObInteractiveState
*s
= it
->data
;
151 next
= g_slist_next(it
);
154 *context
= s
->context
;
156 if ((e
->type
== KeyRelease
&&
157 !(s
->state
& e
->xkey
.state
)))
159 else if (e
->type
== KeyPress
) {
160 if (e
->xkey
.keycode
== ob_keycode(OB_KEY_RETURN
))
162 else if (e
->xkey
.keycode
== ob_keycode(OB_KEY_ESCAPE
))
163 cancel
= done
= TRUE
;
166 g_assert(s
->action
->data
.any
.interactive
);
168 s
->action
->data
.inter
.cancel
= cancel
;
169 s
->action
->data
.inter
.final
= TRUE
;
171 s
->action
->func(&s
->action
->data
);
173 grab_keyboard(FALSE
);
174 grab_pointer(FALSE
, None
);
175 keyboard_reset_chains();
178 interactive_states
= g_slist_delete_link(interactive_states
, it
);
186 void keyboard_event(ObClient
*client
, const XEvent
*e
)
190 g_assert(e
->type
== KeyPress
);
192 if (e
->xkey
.keycode
== config_keyboard_reset_keycode
&&
193 e
->xkey
.state
== config_keyboard_reset_state
)
195 keyboard_reset_chains();
200 p
= keyboard_firstnode
;
202 p
= curpos
->first_child
;
204 if (p
->key
== e
->xkey
.keycode
&&
205 p
->state
== e
->xkey
.state
) {
206 if (p
->first_child
!= NULL
) { /* part of a chain */
207 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
208 /* 5 second timeout for chains */
209 ob_main_loop_timeout_add(ob_main_loop
, 5 * G_USEC_PER_SEC
,
210 chain_timeout
, NULL
, NULL
);
215 for (it
= p
->actions
; it
; it
= it
->next
) {
216 ObAction
*act
= it
->data
;
217 if (act
->func
!= NULL
) {
218 act
->data
.any
.c
= client
;
220 if (act
->func
== action_moveresize
) {
221 screen_pointer_pos(&act
->data
.moveresize
.x
,
222 &act
->data
.moveresize
.y
);
225 if (act
->data
.any
.interactive
) {
226 act
->data
.inter
.cancel
= FALSE
;
227 act
->data
.inter
.final
= FALSE
;
228 keyboard_interactive_grab(e
->xkey
.state
, client
,
232 if (act
->func
== action_showmenu
) {
233 act
->data
.showmenu
.x
= e
->xkey
.x_root
;
234 act
->data
.showmenu
.y
= e
->xkey
.y_root
;
237 act
->data
.any
.c
= client
;
238 act
->func(&act
->data
);
242 keyboard_reset_chains();
250 void keyboard_startup()
255 void keyboard_shutdown()
257 tree_destroy(keyboard_firstnode
);
258 keyboard_firstnode
= NULL
;