ungrab keys before grabbnig keys at the next chain level
[mikachu/openbox.git] / openbox / keyboard.c
1 #include "mainloop.h"
2 #include "focus.h"
3 #include "screen.h"
4 #include "frame.h"
5 #include "openbox.h"
6 #include "event.h"
7 #include "grab.h"
8 #include "client.h"
9 #include "action.h"
10 #include "prop.h"
11 #include "config.h"
12 #include "keytree.h"
13 #include "keyboard.h"
14 #include "translate.h"
15
16 #include <glib.h>
17
18 KeyBindingTree *keyboard_firstnode;
19
20 typedef struct {
21     guint state;
22     ObClient *client;
23     ObAction *action;
24     ObFrameContext context;
25 } ObInteractiveState;
26
27 static GSList *interactive_states;
28
29 static KeyBindingTree *curpos;
30
31 static void grab_for_window(Window win, gboolean grab)
32 {
33     KeyBindingTree *p;
34
35     ungrab_all_keys(win);
36
37     if (grab) {
38         p = curpos ? curpos->first_child : keyboard_firstnode;
39         while (p) {
40             grab_key(p->key, p->state, win, GrabModeAsync);
41             p = p->next_sibling;
42         }
43         if (curpos)
44             grab_key(config_keyboard_reset_keycode,
45                      config_keyboard_reset_state,
46                      win, GrabModeAsync);
47     }
48 }
49
50 void keyboard_grab_for_client(ObClient *c, gboolean grab)
51 {
52     grab_for_window(c->window, grab);
53 }
54
55 static void grab_keys(gboolean grab)
56 {
57     GList *it;
58
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);
62 }
63
64 static gboolean chain_timeout(gpointer data)
65 {
66     keyboard_reset_chains();
67
68     return FALSE; /* don't repeat */
69 }
70
71 void keyboard_reset_chains()
72 {
73     ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
74
75     if (curpos) {
76         grab_keys(FALSE);
77         curpos = NULL;
78         grab_keys(TRUE);
79     }
80 }
81
82 gboolean keyboard_bind(GList *keylist, ObAction *action)
83 {
84     KeyBindingTree *tree, *t;
85     gboolean conflict;
86
87     g_assert(keylist != NULL);
88     g_assert(action != NULL);
89
90     if (!(tree = tree_build(keylist)))
91         return FALSE;
92
93     if ((t = tree_find(tree, &conflict)) != NULL) {
94         /* already bound to something, use the existing tree */
95         tree_destroy(tree);
96         tree = NULL;
97     } else
98         t = tree;
99     while (t->first_child) t = t->first_child;
100
101     if (conflict) {
102         g_warning("conflict with binding");
103         tree_destroy(tree);
104         return FALSE;
105     }
106
107     /* set the action */
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);
112
113     return TRUE;
114 }
115
116 void keyboard_interactive_grab(guint state, ObClient *client,
117                                ObFrameContext context, ObAction *action)
118 {
119     ObInteractiveState *s;
120
121     if (!interactive_states) {
122         if (!grab_keyboard(TRUE))
123             return;
124         if (!grab_pointer(TRUE, None)) {
125             grab_keyboard(FALSE);
126             return;
127         }
128     }
129
130     s = g_new(ObInteractiveState, 1);
131
132     s->state = state;
133     s->client = client;
134     s->action = action;
135     s->context = context;
136
137     interactive_states = g_slist_append(interactive_states, s);
138 }
139
140 gboolean keyboard_process_interactive_grab(const XEvent *e,
141                                            ObClient **client,
142                                            ObFrameContext *context)
143 {
144     GSList *it, *next;
145     gboolean handled = FALSE;
146     gboolean done = FALSE;
147     gboolean cancel = FALSE;
148
149     for (it = interactive_states; it; it = next) {
150         ObInteractiveState *s = it->data;
151
152         next = g_slist_next(it);
153         
154         *client = s->client;
155         *context = s->context;
156
157         if ((e->type == KeyRelease && 
158              !(s->state & e->xkey.state)))
159             done = TRUE;
160         else if (e->type == KeyPress) {
161             if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
162                 done = TRUE;
163             else if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
164                 cancel = done = TRUE;
165         }
166         if (done) {
167             g_assert(s->action->data.any.interactive);
168
169             s->action->data.inter.cancel = cancel;
170             s->action->data.inter.final = TRUE;
171
172             s->action->func(&s->action->data);
173
174             grab_keyboard(FALSE);
175             grab_pointer(FALSE, None);
176             keyboard_reset_chains();
177
178             g_free(s);
179             interactive_states = g_slist_delete_link(interactive_states, it);
180             handled = TRUE;
181         }
182     }
183
184     return handled;
185 }
186
187 void keyboard_event(ObClient *client, const XEvent *e)
188 {
189     KeyBindingTree *p;
190
191     g_assert(e->type == KeyPress);
192
193     if (e->xkey.keycode == config_keyboard_reset_keycode &&
194         e->xkey.state == config_keyboard_reset_state)
195     {
196         keyboard_reset_chains();
197         return;
198     }
199
200     if (curpos == NULL)
201         p = keyboard_firstnode;
202     else
203         p = curpos->first_child;
204     while (p) {
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);
212                 grab_keys(FALSE);
213                 curpos = p;
214                 grab_keys(TRUE);
215             } else {
216                 GSList *it;
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;
221
222                         if (act->func == action_moveresize) {
223                             screen_pointer_pos(&act->data.moveresize.x,
224                                                &act->data.moveresize.y);
225                         }
226
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,
231                                                       0, act);
232                         }
233
234                         if (act->func == action_showmenu) {
235                             act->data.showmenu.x = e->xkey.x_root;
236                             act->data.showmenu.y = e->xkey.y_root;
237                         }
238
239                         act->data.any.c = client;
240                         act->func(&act->data);
241                     }
242                 }
243
244                 keyboard_reset_chains();
245             }
246             break;
247         }
248         p = p->next_sibling;
249     }
250 }
251
252 void keyboard_startup()
253 {
254     grab_keys(TRUE);
255 }
256
257 void keyboard_shutdown()
258 {
259     tree_destroy(keyboard_firstnode);
260     keyboard_firstnode = NULL;
261     grab_keys(FALSE);
262 }
263