little bit of an actions overhaul, added action_run* so that duplicated code can...
[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)->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     gboolean mods = TRUE;
87
88     g_assert(keylist != NULL);
89     g_assert(action != NULL);
90
91     if (!(tree = tree_build(keylist)))
92         return FALSE;
93
94     if ((t = tree_find(tree, &conflict)) != NULL) {
95         /* already bound to something, use the existing tree */
96         tree_destroy(tree);
97         tree = NULL;
98     } else
99         t = tree;
100
101     if (conflict) {
102         g_warning("conflict with binding");
103         tree_destroy(tree);
104         return FALSE;
105     }
106
107     /* find if every key in this chain has modifiers, and also find the
108        bottom node of the tree */
109     while (t->first_child) {
110         if (!t->state)
111             mods = FALSE;
112         t = t->first_child;
113     }
114
115     /* when there are no modifiers in the binding, then the action cannot
116        be interactive */
117     if (!mods && action->data.any.interactive) {
118         action->data.any.interactive = FALSE;
119         action->data.inter.final = TRUE;
120     }
121
122     /* set the action */
123     t->actions = g_slist_append(t->actions, action);
124     /* assimilate this built tree into the main tree. assimilation
125        destroys/uses the tree */
126     if (tree) tree_assimilate(tree);
127
128     return TRUE;
129 }
130
131 void keyboard_interactive_grab(guint state, ObClient *client,
132                                ObFrameContext context, ObAction *action)
133 {
134     ObInteractiveState *s;
135
136     g_assert(action->data.any.interactive);
137
138     if (!interactive_states) {
139         if (!grab_keyboard(TRUE))
140             return;
141         if (!grab_pointer(TRUE, OB_CURSOR_NONE)) {
142             grab_keyboard(FALSE);
143             return;
144         }
145     }
146
147     s = g_new(ObInteractiveState, 1);
148
149     s->state = state;
150     s->client = client;
151     s->action = action;
152     s->context = context;
153
154     interactive_states = g_slist_append(interactive_states, s);
155 }
156
157 gboolean keyboard_process_interactive_grab(const XEvent *e,
158                                            ObClient **client,
159                                            ObFrameContext *context)
160 {
161     GSList *it, *next;
162     gboolean handled = FALSE;
163     gboolean done = FALSE;
164     gboolean cancel = FALSE;
165
166     for (it = interactive_states; it; it = next) {
167         ObInteractiveState *s = it->data;
168
169         next = g_slist_next(it);
170         
171         *client = s->client;
172         *context = s->context;
173
174         if ((e->type == KeyRelease && 
175              !(s->state & e->xkey.state)))
176             done = TRUE;
177         else if (e->type == KeyPress) {
178             if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
179                 done = TRUE;
180             else if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
181                 cancel = done = TRUE;
182         }
183         if (done) {
184             action_run_interactive(s->action, s->client,
185                                    e->xkey.state, cancel, TRUE);
186
187             g_free(s);
188
189             interactive_states = g_slist_delete_link(interactive_states, it);
190             if (!interactive_states) {
191                 grab_keyboard(FALSE);
192                 grab_pointer(FALSE, OB_CURSOR_NONE);
193                 keyboard_reset_chains();
194             }
195
196             handled = TRUE;
197         }
198     }
199
200     return handled;
201 }
202
203 void keyboard_event(ObClient *client, const XEvent *e)
204 {
205     KeyBindingTree *p;
206
207     g_assert(e->type == KeyPress);
208
209     if (e->xkey.keycode == config_keyboard_reset_keycode &&
210         e->xkey.state == config_keyboard_reset_state)
211     {
212         keyboard_reset_chains();
213         return;
214     }
215
216     if (curpos == NULL)
217         p = keyboard_firstnode;
218     else
219         p = curpos->first_child;
220     while (p) {
221         if (p->key == e->xkey.keycode &&
222             p->state == e->xkey.state)
223         {
224             if (p->first_child != NULL) { /* part of a chain */
225                 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
226                 /* 5 second timeout for chains */
227                 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
228                                          chain_timeout, NULL, NULL);
229                 grab_keys(FALSE);
230                 curpos = p;
231                 grab_keys(TRUE);
232             } else {
233                 GSList *it;
234
235                 for (it = p->actions; it; it = it->next)
236                     action_run_key(it->data, client, e->xkey.state,
237                                    e->xkey.x_root, e->xkey.y_root);
238
239                 keyboard_reset_chains();
240             }
241             break;
242         }
243         p = p->next_sibling;
244     }
245 }
246
247 void keyboard_startup(gboolean reconfig)
248 {
249     grab_keys(TRUE);
250 }
251
252 void keyboard_shutdown(gboolean reconfig)
253 {
254     GSList *it;
255
256     tree_destroy(keyboard_firstnode);
257     keyboard_firstnode = NULL;
258
259     for (it = interactive_states; it; it = g_slist_next(it))
260         g_free(it->data);
261     g_slist_free(interactive_states);
262     interactive_states = NULL;
263
264     ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
265     grab_keys(FALSE);
266     curpos = NULL;
267 }
268