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);
82 gboolean keyboard_bind(GList *keylist, ObAction *action)
84 KeyBindingTree *tree, *t;
87 g_assert(keylist != NULL);
88 g_assert(action != NULL);
90 if (!(tree = tree_build(keylist)))
93 if ((t = tree_find(tree, &conflict)) != NULL) {
94 /* already bound to something, use the existing tree */
99 while (t->first_child) t = t->first_child;
102 g_warning("conflict with binding");
108 t->actions = g_slist_append(t->actions, action);
109 /* assimilate this built tree into the main tree. assimilation
110 destroys/uses the tree */
111 if (tree) tree_assimilate(tree);
116 void keyboard_interactive_grab(guint state, ObClient *client,
117 ObFrameContext context, ObAction *action)
119 ObInteractiveState *s;
121 if (!interactive_states) {
122 if (!grab_keyboard(TRUE))
124 if (!grab_pointer(TRUE, None)) {
125 grab_keyboard(FALSE);
130 s = g_new(ObInteractiveState, 1);
135 s->context = context;
137 interactive_states = g_slist_append(interactive_states, s);
140 gboolean keyboard_process_interactive_grab(const XEvent *e,
142 ObFrameContext *context)
145 gboolean handled = FALSE;
146 gboolean done = FALSE;
147 gboolean cancel = FALSE;
149 for (it = interactive_states; it; it = next) {
150 ObInteractiveState *s = it->data;
152 next = g_slist_next(it);
155 *context = s->context;
157 if ((e->type == KeyRelease &&
158 !(s->state & e->xkey.state)))
160 else if (e->type == KeyPress) {
161 if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
163 else if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
164 cancel = done = TRUE;
167 g_assert(s->action->data.any.interactive);
169 s->action->data.inter.cancel = cancel;
170 s->action->data.inter.final = TRUE;
172 s->action->func(&s->action->data);
174 grab_keyboard(FALSE);
175 grab_pointer(FALSE, None);
176 keyboard_reset_chains();
179 interactive_states = g_slist_delete_link(interactive_states, it);
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 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
209 /* 5 second timeout for chains */
210 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
211 chain_timeout, NULL, NULL);
217 for (it = p->actions; it; it = it->next) {
218 ObAction *act = it->data;
219 if (act->func != NULL) {
220 act->data.any.c = client;
222 if (act->func == action_moveresize) {
223 screen_pointer_pos(&act->data.moveresize.x,
224 &act->data.moveresize.y);
227 if (act->data.any.interactive) {
228 act->data.inter.cancel = FALSE;
229 act->data.inter.final = FALSE;
230 keyboard_interactive_grab(e->xkey.state, client,
234 if (act->func == action_showmenu) {
235 act->data.showmenu.x = e->xkey.x_root;
236 act->data.showmenu.y = e->xkey.y_root;
239 act->data.any.c = client;
240 act->func(&act->data);
244 keyboard_reset_chains();
252 void keyboard_startup()
257 void keyboard_shutdown()
259 tree_destroy(keyboard_firstnode);
260 keyboard_firstnode = NULL;