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 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
163 gboolean handled = FALSE;
164 gboolean done = FALSE;
165 gboolean cancel = FALSE;
167 for (it = interactive_states; it; it = next) {
168 ObInteractiveState *s = it->data;
170 next = g_slist_next(it);
172 if ((e->type == KeyRelease &&
173 !(s->state & e->xkey.state)))
175 else if (e->type == KeyPress) {
176 if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
178 else if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
179 cancel = done = TRUE;
182 action_run_interactive(s->action, s->client,
183 e->xkey.state, cancel, TRUE);
187 interactive_states = g_slist_delete_link(interactive_states, it);
188 if (!interactive_states) {
189 grab_keyboard(FALSE);
190 grab_pointer(FALSE, OB_CURSOR_NONE);
191 keyboard_reset_chains();
202 void keyboard_event(ObClient *client, const XEvent *e)
206 g_assert(e->type == KeyPress);
208 if (e->xkey.keycode == config_keyboard_reset_keycode &&
209 e->xkey.state == config_keyboard_reset_state)
211 keyboard_reset_chains();
216 p = keyboard_firstnode;
218 p = curpos->first_child;
220 if (p->key == e->xkey.keycode &&
221 p->state == e->xkey.state)
223 if (p->first_child != NULL) { /* part of a chain */
224 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
225 /* 5 second timeout for chains */
226 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
227 chain_timeout, NULL, NULL);
234 for (it = p->actions; it; it = it->next)
235 action_run_key(it->data, client, e->xkey.state,
236 e->xkey.x_root, e->xkey.y_root);
238 keyboard_reset_chains();
246 void keyboard_startup(gboolean reconfig)
251 void keyboard_shutdown(gboolean reconfig)
255 tree_destroy(keyboard_firstnode);
256 keyboard_firstnode = NULL;
258 for (it = interactive_states; it; it = g_slist_next(it))
260 g_slist_free(interactive_states);
261 interactive_states = NULL;
263 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);