]> icculus.org git repositories - dana/openbox.git/blob - openbox/keyboard.c
using the ObMainLoop, which rulz the planet
[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
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             if (s->action->func == action_cycle_windows) {
167                 s->action->data.cycle.cancel = cancel;
168                 s->action->data.cycle.final = TRUE;
169             }
170             if (s->action->func == action_desktop_dir) {
171                 s->action->data.desktopdir.cancel = cancel;
172                 s->action->data.desktopdir.final = TRUE;
173             }
174             if (s->action->func == action_send_to_desktop_dir) {
175                 s->action->data.sendtodir.cancel = cancel;
176                 s->action->data.sendtodir.final = TRUE;
177             }
178
179             s->action->func(&s->action->data);
180
181             grab_keyboard(FALSE);
182             grab_pointer(FALSE, None);
183             keyboard_reset_chains();
184
185             g_free(s);
186             interactive_states = g_slist_delete_link(interactive_states, it);
187             handled = TRUE;
188         }
189     }
190
191     return handled;
192 }
193
194 void keyboard_event(ObClient *client, const XEvent *e)
195 {
196     KeyBindingTree *p;
197
198     g_assert(e->type == KeyPress);
199
200     if (e->xkey.keycode == config_keyboard_reset_keycode &&
201         e->xkey.state == config_keyboard_reset_state)
202     {
203         keyboard_reset_chains();
204         return;
205     }
206
207     if (curpos == NULL)
208         p = keyboard_firstnode;
209     else
210         p = curpos->first_child;
211     while (p) {
212         if (p->key == e->xkey.keycode &&
213             p->state == e->xkey.state) {
214             if (p->first_child != NULL) { /* part of a chain */
215                 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
216                 /* 5 second timeout for chains */
217                 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
218                                          chain_timeout, NULL, NULL);
219                 curpos = p;
220                 grab_keys(TRUE);
221             } else {
222                 GSList *it;
223                 for (it = p->actions; it; it = it->next) {
224                     ObAction *act = it->data;
225                     if (act->func != NULL) {
226                         act->data.any.c = client;
227
228                         if (act->func == action_cycle_windows)
229                         {
230                             act->data.cycle.final = FALSE;
231                             act->data.cycle.cancel = FALSE;
232                         }
233                         if (act->func == action_desktop_dir)
234                         {
235                             act->data.desktopdir.final = FALSE;
236                             act->data.desktopdir.cancel = FALSE;
237                         }
238                         if (act->func == action_send_to_desktop_dir)
239                         {
240                             act->data.sendtodir.final = FALSE;
241                             act->data.sendtodir.cancel = FALSE;
242                         }
243
244                         if (act->func == action_moveresize)
245                         {
246                             screen_pointer_pos(&act->data.moveresize.x,
247                                                &act->data.moveresize.y);
248                         }
249
250                         if ((act->func == action_cycle_windows ||
251                              act->func == action_desktop_dir ||
252                              act->func == action_send_to_desktop_dir))
253                         {
254                             keyboard_interactive_grab(e->xkey.state, client,
255                                                       0, act);
256                         }
257
258                         if (act->func == action_showmenu)
259                         {
260                             act->data.showmenu.x = e->xkey.x_root;
261                             act->data.showmenu.y = e->xkey.y_root;
262                         }
263
264                         act->data.any.c = client;
265                         act->func(&act->data);
266                     }
267                 }
268
269                 keyboard_reset_chains();
270             }
271             break;
272         }
273         p = p->next_sibling;
274     }
275 }
276
277 void keyboard_startup()
278 {
279     grab_keys(TRUE);
280 }
281
282 void keyboard_shutdown()
283 {
284     tree_destroy(keyboard_firstnode);
285     keyboard_firstnode = NULL;
286     grab_keys(FALSE);
287 }
288