14 #include "translate.h"
18 KeyBindingTree
*keyboard_firstnode
;
20 static KeyBindingTree
*curpos
;
21 static ObTimer
*chain_timer
;
22 static gboolean interactive_grab
;
23 static guint grabbed_state
;
24 static ObClient
*grabbed_client
;
25 static ObAction
*grabbed_action
;
26 static ObFrameContext grabbed_context
;
28 static void grab_for_window(Window win
, gboolean grab
)
35 p
= curpos
? curpos
->first_child
: keyboard_firstnode
;
37 grab_key(p
->key
, p
->state
, win
, GrabModeAsync
);
41 grab_key(config_keyboard_reset_keycode
,
42 config_keyboard_reset_state
,
47 void keyboard_grab_for_client(ObClient
*c
, gboolean grab
)
49 grab_for_window(c
->window
, grab
);
52 static void grab_keys(gboolean grab
)
56 grab_for_window(screen_support_win
, grab
);
57 for (it
= client_list
; it
; it
= g_list_next(it
))
58 grab_for_window(((ObClient
*)it
->data
)->frame
->window
, grab
);
61 void keyboard_reset_chains()
64 timer_stop(chain_timer
);
73 static void chain_timeout(ObTimer
*t
, void *data
)
75 keyboard_reset_chains();
78 gboolean
keyboard_bind(GList
*keylist
, ObAction
*action
)
80 KeyBindingTree
*tree
, *t
;
83 g_assert(keylist
!= NULL
);
84 g_assert(action
!= NULL
);
86 if (!(tree
= tree_build(keylist
)))
89 if ((t
= tree_find(tree
, &conflict
)) != NULL
) {
90 /* already bound to something, use the existing tree */
95 while (t
->first_child
) t
= t
->first_child
;
98 g_warning("conflict with binding");
104 t
->actions
= g_slist_append(t
->actions
, action
);
105 /* assimilate this built tree into the main tree. assimilation
106 destroys/uses the tree */
107 if (tree
) tree_assimilate(tree
);
112 void keyboard_interactive_grab(guint state
, ObClient
*client
,
113 ObFrameContext context
, ObAction
*action
)
115 if (!interactive_grab
) {
116 if (!grab_keyboard(TRUE
))
118 if (!grab_pointer(TRUE
, None
)) {
119 grab_keyboard(FALSE
);
122 interactive_grab
= TRUE
;
125 grabbed_state
= state
;
126 grabbed_client
= client
;
127 grabbed_action
= action
;
128 grabbed_context
= context
;
131 gboolean
keyboard_process_interactive_grab(const XEvent
*e
,
133 ObFrameContext
*context
)
135 gboolean handled
= FALSE
;
136 gboolean done
= FALSE
;
138 if (interactive_grab
) {
139 *client
= grabbed_client
;
140 *context
= grabbed_context
;
142 if ((e
->type
== KeyRelease
&&
143 !(grabbed_state
& e
->xkey
.state
)))
145 else if (e
->type
== KeyPress
) {
146 if (e
->xkey
.keycode
== ob_keycode(OB_KEY_RETURN
))
148 else if (e
->xkey
.keycode
== ob_keycode(OB_KEY_ESCAPE
)) {
149 if (grabbed_action
->func
== action_cycle_windows
) {
150 grabbed_action
->data
.cycle
.cancel
= TRUE
;
152 if (grabbed_action
->func
== action_desktop_dir
) {
153 grabbed_action
->data
.desktopdir
.cancel
= TRUE
;
155 if (grabbed_action
->func
== action_send_to_desktop_dir
)
157 grabbed_action
->data
.sendtodir
.cancel
= TRUE
;
163 if (grabbed_action
->func
== action_cycle_windows
) {
164 grabbed_action
->data
.cycle
.final
= TRUE
;
166 if (grabbed_action
->func
== action_desktop_dir
) {
167 grabbed_action
->data
.desktopdir
.final
= TRUE
;
169 if (grabbed_action
->func
== action_send_to_desktop_dir
) {
170 grabbed_action
->data
.sendtodir
.final
= TRUE
;
173 grabbed_action
->func(&grabbed_action
->data
);
175 interactive_grab
= FALSE
;
176 grab_keyboard(FALSE
);
177 grab_pointer(FALSE
, None
);
178 keyboard_reset_chains();
187 void keyboard_event(ObClient
*client
, const XEvent
*e
)
191 g_assert(e
->type
== KeyPress
);
193 if (e
->xkey
.keycode
== config_keyboard_reset_keycode
&&
194 e
->xkey
.state
== config_keyboard_reset_state
)
196 keyboard_reset_chains();
201 p
= keyboard_firstnode
;
203 p
= curpos
->first_child
;
205 if (p
->key
== e
->xkey
.keycode
&&
206 p
->state
== e
->xkey
.state
) {
207 if (p
->first_child
!= NULL
) { /* part of a chain */
208 if (chain_timer
) timer_stop(chain_timer
);
209 /* 5 second timeout for chains */
210 chain_timer
= timer_start(5000*1000, chain_timeout
,
216 for (it
= p
->actions
; it
; it
= it
->next
) {
217 ObAction
*act
= it
->data
;
218 if (act
->func
!= NULL
) {
219 act
->data
.any
.c
= client
;
221 if (act
->func
== action_cycle_windows
)
223 act
->data
.cycle
.final
= FALSE
;
224 act
->data
.cycle
.cancel
= FALSE
;
226 if (act
->func
== action_desktop_dir
)
228 act
->data
.desktopdir
.final
= FALSE
;
229 act
->data
.desktopdir
.cancel
= FALSE
;
231 if (act
->func
== action_send_to_desktop_dir
)
233 act
->data
.sendtodir
.final
= FALSE
;
234 act
->data
.sendtodir
.cancel
= FALSE
;
237 if (act
->func
== action_moveresize
)
239 screen_pointer_pos(&act
->data
.moveresize
.x
,
240 &act
->data
.moveresize
.y
);
243 if ((act
->func
== action_cycle_windows
||
244 act
->func
== action_desktop_dir
||
245 act
->func
== action_send_to_desktop_dir
))
247 keyboard_interactive_grab(e
->xkey
.state
, client
,
251 if (act
->func
== action_showmenu
)
253 act
->data
.showmenu
.x
= e
->xkey
.x_root
;
254 act
->data
.showmenu
.y
= e
->xkey
.y_root
;
257 act
->data
.any
.c
= client
;
258 act
->func(&act
->data
);
262 keyboard_reset_chains();
270 void keyboard_startup()
275 void keyboard_shutdown()
277 tree_destroy(keyboard_firstnode
);
278 keyboard_firstnode
= NULL
;