make interactive actions a type and not special cases.
[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         curpos = NULL;
77         grab_keys(TRUE);
78     }
79 }
80
81 gboolean keyboard_bind(GList *keylist, ObAction *action)
82 {
83     KeyBindingTree *tree, *t;
84     gboolean conflict;
85
86     g_assert(keylist != NULL);
87     g_assert(action != NULL);
88
89     if (!(tree = tree_build(keylist)))
90         return FALSE;
91
92     if ((t = tree_find(tree, &conflict)) != NULL) {
93         /* already bound to something, use the existing tree */
94         tree_destroy(tree);
95         tree = NULL;
96     } else
97         t = tree;
98     while (t->first_child) t = t->first_child;
99
100     if (conflict) {
101         g_warning("conflict with binding");
102         tree_destroy(tree);
103         return FALSE;
104     }
105
106     /* set the action */
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);
111
112     return TRUE;
113 }
114
115 void keyboard_interactive_grab(guint state, ObClient *client,
116                                ObFrameContext context, ObAction *action)
117 {
118     ObInteractiveState *s;
119
120     if (!interactive_states) {
121         if (!grab_keyboard(TRUE))
122             return;
123         if (!grab_pointer(TRUE, None)) {
124             grab_keyboard(FALSE);
125             return;
126         }
127     }
128
129     s = g_new(ObInteractiveState, 1);
130
131     s->state = state;
132     s->client = client;
133     s->action = action;
134     s->context = context;
135
136     interactive_states = g_slist_append(interactive_states, s);
137 }
138
139 gboolean keyboard_process_interactive_grab(const XEvent *e,
140                                            ObClient **client,
141                                            ObFrameContext *context)
142 {
143     GSList *it, *next;
144     gboolean handled = FALSE;
145     gboolean done = FALSE;
146     gboolean cancel = FALSE;
147
148     for (it = interactive_states; it; it = next) {
149         ObInteractiveState *s = it->data;
150
151         next = g_slist_next(it);
152         
153         *client = s->client;
154         *context = s->context;
155
156         if ((e->type == KeyRelease && 
157              !(s->state & e->xkey.state)))
158             done = TRUE;
159         else if (e->type == KeyPress) {
160             if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
161                 done = TRUE;
162             else if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
163                 cancel = done = TRUE;
164         }
165         if (done) {
166             g_assert(s->action->data.any.interactive);
167
168             s->action->data.inter.cancel = cancel;
169             s->action->data.inter.final = TRUE;
170
171             s->action->func(&s->action->data);
172
173             grab_keyboard(FALSE);
174             grab_pointer(FALSE, None);
175             keyboard_reset_chains();
176
177             g_free(s);
178             interactive_states = g_slist_delete_link(interactive_states, it);
179             handled = TRUE;
180         }
181     }
182
183     return handled;
184 }
185
186 void keyboard_event(ObClient *client, const XEvent *e)
187 {
188     KeyBindingTree *p;
189
190     g_assert(e->type == KeyPress);
191
192     if (e->xkey.keycode == config_keyboard_reset_keycode &&
193         e->xkey.state == config_keyboard_reset_state)
194     {
195         keyboard_reset_chains();
196         return;
197     }
198
199     if (curpos == NULL)
200         p = keyboard_firstnode;
201     else
202         p = curpos->first_child;
203     while (p) {
204         if (p->key == e->xkey.keycode &&
205             p->state == e->xkey.state) {
206             if (p->first_child != NULL) { /* part of a chain */
207                 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
208                 /* 5 second timeout for chains */
209                 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
210                                          chain_timeout, NULL, NULL);
211                 curpos = p;
212                 grab_keys(TRUE);
213             } else {
214                 GSList *it;
215                 for (it = p->actions; it; it = it->next) {
216                     ObAction *act = it->data;
217                     if (act->func != NULL) {
218                         act->data.any.c = client;
219
220                         if (act->func == action_moveresize) {
221                             screen_pointer_pos(&act->data.moveresize.x,
222                                                &act->data.moveresize.y);
223                         }
224
225                         if (act->data.any.interactive) {
226                             act->data.inter.cancel = FALSE;
227                             act->data.inter.final = FALSE;
228                             keyboard_interactive_grab(e->xkey.state, client,
229                                                       0, act);
230                         }
231
232                         if (act->func == action_showmenu) {
233                             act->data.showmenu.x = e->xkey.x_root;
234                             act->data.showmenu.y = e->xkey.y_root;
235                         }
236
237                         act->data.any.c = client;
238                         act->func(&act->data);
239                     }
240                 }
241
242                 keyboard_reset_chains();
243             }
244             break;
245         }
246         p = p->next_sibling;
247     }
248 }
249
250 void keyboard_startup()
251 {
252     grab_keys(TRUE);
253 }
254
255 void keyboard_shutdown()
256 {
257     tree_destroy(keyboard_firstnode);
258     keyboard_firstnode = NULL;
259     grab_keys(FALSE);
260 }
261