]> icculus.org git repositories - dana/openbox.git/blob - openbox/keyboard.c
no more interactive mouse actions, they are evil etc. actions now "feel" much more...
[dana/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 #include "moveresize.h"
16
17 #include <glib.h>
18
19 KeyBindingTree *keyboard_firstnode;
20
21 typedef struct {
22     guint state;
23     ObClient *client;
24     ObAction *action;
25     ObFrameContext context;
26 } ObInteractiveState;
27
28 static GSList *interactive_states;
29
30 static KeyBindingTree *curpos;
31
32 static void grab_for_window(Window win, gboolean grab)
33 {
34     KeyBindingTree *p;
35
36     ungrab_all_keys(win);
37
38     if (grab) {
39         p = curpos ? curpos->first_child : keyboard_firstnode;
40         while (p) {
41             grab_key(p->key, p->state, win, GrabModeAsync);
42             p = p->next_sibling;
43         }
44         if (curpos)
45             grab_key(config_keyboard_reset_keycode,
46                      config_keyboard_reset_state,
47                      win, GrabModeAsync);
48     }
49 }
50
51 void keyboard_grab_for_client(ObClient *c, gboolean grab)
52 {
53     grab_for_window(c->window, grab);
54 }
55
56 static void grab_keys(gboolean grab)
57 {
58     GList *it;
59
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);
63 }
64
65 static gboolean chain_timeout(gpointer data)
66 {
67     keyboard_reset_chains();
68
69     return FALSE; /* don't repeat */
70 }
71
72 void keyboard_reset_chains()
73 {
74     ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
75
76     if (curpos) {
77         grab_keys(FALSE);
78         curpos = NULL;
79         grab_keys(TRUE);
80     }
81 }
82
83 gboolean keyboard_bind(GList *keylist, ObAction *action)
84 {
85     KeyBindingTree *tree, *t;
86     gboolean conflict;
87     gboolean mods = TRUE;
88
89     g_assert(keylist != NULL);
90     g_assert(action != NULL);
91
92     if (!(tree = tree_build(keylist)))
93         return FALSE;
94
95     if ((t = tree_find(tree, &conflict)) != NULL) {
96         /* already bound to something, use the existing tree */
97         tree_destroy(tree);
98         tree = NULL;
99     } else
100         t = tree;
101
102     if (conflict) {
103         g_warning("conflict with binding");
104         tree_destroy(tree);
105         return FALSE;
106     }
107
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) {
111         if (!t->state)
112             mods = FALSE;
113         t = t->first_child;
114     }
115
116     /* when there are no modifiers in the binding, then the action cannot
117        be interactive */
118     if (!mods && action->data.any.interactive) {
119         action->data.any.interactive = FALSE;
120         action->data.inter.final = TRUE;
121     }
122
123     /* set the action */
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);
128
129     return TRUE;
130 }
131
132 void keyboard_interactive_grab(guint state, ObClient *client,
133                                ObAction *action)
134 {
135     ObInteractiveState *s;
136
137     g_assert(action->data.any.interactive);
138
139     if (moveresize_in_progress)
140         moveresize_end(FALSE);
141
142     if (!interactive_states) {
143         if (!grab_keyboard(TRUE))
144             return;
145         if (!grab_pointer(TRUE, OB_CURSOR_NONE)) {
146             grab_keyboard(FALSE);
147             return;
148         }
149     }
150
151     s = g_new(ObInteractiveState, 1);
152
153     s->state = state;
154     s->client = client;
155     s->action = action;
156
157     interactive_states = g_slist_append(interactive_states, s);
158 }
159
160 gboolean keyboard_process_interactive_grab(const XEvent *e)
161 {
162     GSList *it, *next;
163     gboolean handled = FALSE;
164     gboolean done = FALSE;
165     gboolean cancel = FALSE;
166
167     for (it = interactive_states; it; it = next) {
168         ObInteractiveState *s = it->data;
169
170         next = g_slist_next(it);
171         
172         if ((e->type == KeyRelease && 
173              !(s->state & e->xkey.state)))
174             done = TRUE;
175         else if (e->type == KeyPress) {
176             if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
177                 done = TRUE;
178             else if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
179                 cancel = done = TRUE;
180         }
181         if (done) {
182             action_run_interactive(s->action, s->client,
183                                    e->xkey.state, cancel, TRUE);
184
185             g_free(s);
186
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();
192             }
193
194             handled = TRUE;
195         }
196     }
197
198     return handled;
199 }
200
201 void keyboard_event(ObClient *client, const XEvent *e)
202 {
203     KeyBindingTree *p;
204
205     g_assert(e->type == KeyPress);
206
207     if (e->xkey.keycode == config_keyboard_reset_keycode &&
208         e->xkey.state == config_keyboard_reset_state)
209     {
210         keyboard_reset_chains();
211         return;
212     }
213
214     if (curpos == NULL)
215         p = keyboard_firstnode;
216     else
217         p = curpos->first_child;
218     while (p) {
219         if (p->key == e->xkey.keycode &&
220             p->state == e->xkey.state)
221         {
222             if (p->first_child != NULL) { /* part of a chain */
223                 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
224                 /* 5 second timeout for chains */
225                 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
226                                          chain_timeout, NULL, NULL);
227                 grab_keys(FALSE);
228                 curpos = p;
229                 grab_keys(TRUE);
230             } else {
231                 GSList *it;
232
233                 for (it = p->actions; it; it = it->next)
234                     action_run_key(it->data, client, e->xkey.state,
235                                    e->xkey.x_root, e->xkey.y_root);
236
237                 keyboard_reset_chains();
238             }
239             break;
240         }
241         p = p->next_sibling;
242     }
243 }
244
245 void keyboard_startup(gboolean reconfig)
246 {
247     grab_keys(TRUE);
248 }
249
250 void keyboard_shutdown(gboolean reconfig)
251 {
252     GSList *it;
253
254     tree_destroy(keyboard_firstnode);
255     keyboard_firstnode = NULL;
256
257     for (it = interactive_states; it; it = g_slist_next(it))
258         g_free(it->data);
259     g_slist_free(interactive_states);
260     interactive_states = NULL;
261
262     ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
263     grab_keys(FALSE);
264     curpos = NULL;
265 }
266