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 if (s
->action
->func
== action_cycle_windows
) {
167 s
->action
->data
.cycle
.cancel
= cancel
;
168 s
->action
->data
.cycle
.final
= TRUE
;
170 if (s
->action
->func
== action_desktop_dir
) {
171 s
->action
->data
.desktopdir
.cancel
= cancel
;
172 s
->action
->data
.desktopdir
.final
= TRUE
;
174 if (s
->action
->func
== action_send_to_desktop_dir
) {
175 s
->action
->data
.sendtodir
.cancel
= cancel
;
176 s
->action
->data
.sendtodir
.final
= TRUE
;
179 s
->action
->func(&s
->action
->data
);
181 grab_keyboard(FALSE
);
182 grab_pointer(FALSE
, None
);
183 keyboard_reset_chains();
186 interactive_states
= g_slist_delete_link(interactive_states
, it
);
194 void keyboard_event(ObClient
*client
, const XEvent
*e
)
198 g_assert(e
->type
== KeyPress
);
200 if (e
->xkey
.keycode
== config_keyboard_reset_keycode
&&
201 e
->xkey
.state
== config_keyboard_reset_state
)
203 keyboard_reset_chains();
208 p
= keyboard_firstnode
;
210 p
= curpos
->first_child
;
212 if (p
->key
== e
->xkey
.keycode
&&
213 p
->state
== e
->xkey
.state
) {
214 if (p
->first_child
!= NULL
) { /* part of a chain */
215 ob_main_loop_timeout_remove(ob_main_loop
, chain_timeout
);
216 /* 5 second timeout for chains */
217 ob_main_loop_timeout_add(ob_main_loop
, 5 * G_USEC_PER_SEC
,
218 chain_timeout
, NULL
, NULL
);
223 for (it
= p
->actions
; it
; it
= it
->next
) {
224 ObAction
*act
= it
->data
;
225 if (act
->func
!= NULL
) {
226 act
->data
.any
.c
= client
;
228 if (act
->func
== action_cycle_windows
)
230 act
->data
.cycle
.final
= FALSE
;
231 act
->data
.cycle
.cancel
= FALSE
;
233 if (act
->func
== action_desktop_dir
)
235 act
->data
.desktopdir
.final
= FALSE
;
236 act
->data
.desktopdir
.cancel
= FALSE
;
238 if (act
->func
== action_send_to_desktop_dir
)
240 act
->data
.sendtodir
.final
= FALSE
;
241 act
->data
.sendtodir
.cancel
= FALSE
;
244 if (act
->func
== action_moveresize
)
246 screen_pointer_pos(&act
->data
.moveresize
.x
,
247 &act
->data
.moveresize
.y
);
250 if ((act
->func
== action_cycle_windows
||
251 act
->func
== action_desktop_dir
||
252 act
->func
== action_send_to_desktop_dir
))
254 keyboard_interactive_grab(e
->xkey
.state
, client
,
258 if (act
->func
== action_showmenu
)
260 act
->data
.showmenu
.x
= e
->xkey
.x_root
;
261 act
->data
.showmenu
.y
= e
->xkey
.y_root
;
264 act
->data
.any
.c
= client
;
265 act
->func(&act
->data
);
269 keyboard_reset_chains();
277 void keyboard_startup()
282 void keyboard_shutdown()
284 tree_destroy(keyboard_firstnode
);
285 keyboard_firstnode
= NULL
;