]> icculus.org git repositories - mikachu/openbox.git/blob - plugins/keyboard/keyboard.c
async key grabs to avoid race conditions with the sync stuff. there are still possibl...
[mikachu/openbox.git] / plugins / keyboard / keyboard.c
1 #include "kernel/focus.h"
2 #include "kernel/dispatch.h"
3 #include "kernel/openbox.h"
4 #include "kernel/event.h"
5 #include "kernel/grab.h"
6 #include "kernel/action.h"
7 #include "kernel/parse.h"
8 #include "kernel/timer.h"
9 #include "tree.h"
10 #include "keyboard.h"
11 #include "keyparse.h"
12 #include "translate.h"
13 #include <glib.h>
14
15 void plugin_setup_config()
16 {
17     parse_reg_section("keyboard", keyparse, NULL);
18 }
19
20 KeyBindingTree *firstnode = NULL;
21
22 static KeyBindingTree *curpos;
23 static guint reset_key, reset_state, button_return, button_escape;
24 static Timer *chain_timer;
25
26 static void grab_keys()
27 {
28     KeyBindingTree *p;
29
30     ungrab_all_keys();
31
32     p = curpos ? curpos->first_child : firstnode;
33     while (p) {
34         grab_key(p->key, p->state, GrabModeAsync);
35         p = p->next_sibling;
36     }
37     grab_key(reset_key, reset_state, GrabModeAsync);
38 }
39
40 static void reset_chains()
41 {
42     if (chain_timer) {
43         timer_stop(chain_timer);
44         chain_timer = NULL;
45     }
46     if (curpos) {
47         curpos = NULL;
48         grab_keys();
49     }
50 }
51
52 static void chain_timeout(void *data)
53 {
54     reset_chains();
55 }
56
57 gboolean kbind(GList *keylist, Action *action)
58 {
59     KeyBindingTree *tree, *t;
60     gboolean conflict;
61
62     g_assert(keylist != NULL);
63     g_assert(action != NULL);
64
65     if (!(tree = tree_build(keylist)))
66         return FALSE;
67
68     if ((t = tree_find(tree, &conflict)) != NULL) {
69         /* already bound to something, use the existing tree */
70         tree_destroy(tree);
71         tree = NULL;
72     } else
73         t = tree;
74     while (t->first_child) t = t->first_child;
75
76     if (conflict) {
77         g_message("conflict with binding");
78         tree_destroy(tree);
79         return FALSE;
80     }
81
82     /* set the action */
83     t->actions = g_slist_append(t->actions, action);
84     /* assimilate this built tree into the main tree. assimilation
85        destroys/uses the tree */
86     if (tree) tree_assimilate(tree);
87
88     return TRUE;
89 }
90
91 static void event(ObEvent *e, void *foo)
92 {
93     static KeyBindingTree *grabbed_key = NULL;
94
95     if (grabbed_key) {
96         gboolean done = FALSE;
97
98         if ((e->type == Event_X_KeyRelease && 
99              !(grabbed_key->state & e->data.x.e->xkey.state)))
100             done = TRUE;
101         else if (e->type == Event_X_KeyPress) {
102             if (e->data.x.e->xkey.keycode == button_return)
103                 done = TRUE;
104             else if (e->data.x.e->xkey.keycode == button_escape) {
105                 GSList *it;
106                 for (it = grabbed_key->actions; it; it = it->next) {
107                     Action *act = it->data;
108                     act->data.cycle.cancel = TRUE;
109                 }
110                 done = TRUE;
111             }
112         }
113         if (done) { 
114             GSList *it;
115             for (it = grabbed_key->actions; it; it = it->next) {
116                 Action *act = it->data;
117                 act->data.cycle.final = TRUE;
118                 act->func(&act->data);
119                 grabbed_key = NULL;
120                 grab_keyboard(FALSE);
121                 reset_chains();
122                 return;
123             }
124         }
125     }
126     if (e->type == Event_X_KeyRelease)
127         return;
128
129     g_assert(e->type == Event_X_KeyPress);
130
131     if (e->data.x.e->xkey.keycode == reset_key &&
132         e->data.x.e->xkey.state == reset_state) {
133         reset_chains();
134     } else {
135         KeyBindingTree *p;
136         if (curpos == NULL)
137             p = firstnode;
138         else
139             p = curpos->first_child;
140         while (p) {
141             if (p->key == e->data.x.e->xkey.keycode &&
142                 p->state == e->data.x.e->xkey.state) {
143                 if (p->first_child != NULL) { /* part of a chain */
144                     if (chain_timer) timer_stop(chain_timer);
145                     /* 5 second timeout for chains */
146                     chain_timer = timer_start(5000*1000, chain_timeout,
147                                               NULL);
148                     curpos = p;
149                     grab_keys();
150                 } else {
151                     GSList *it;
152                     for (it = p->actions; it; it = it->next) {
153                         Action *act = it->data;
154                         if (act->func != NULL) {
155                             act->data.any.c = focus_client;
156
157                             if (act->func == action_cycle_windows) {
158                                 act->data.cycle.final = FALSE;
159                                 act->data.cycle.cancel = FALSE;
160                             }
161
162                             act->func(&act->data);
163
164                             if (act->func == action_cycle_windows &&
165                                 !grabbed_key) {
166                                 grabbed_key = p;
167                                 grab_keyboard(TRUE);
168                                 break;
169                             }
170                         }
171                     }
172
173                     reset_chains();
174                 }
175                 break;
176             }
177             p = p->next_sibling;
178         }
179     }
180 }
181
182 void plugin_startup()
183 {
184     guint i;
185
186     curpos = NULL;
187     chain_timer = NULL;
188
189     dispatch_register(Event_X_KeyPress | Event_X_KeyRelease,
190                       (EventHandler)event, NULL);
191
192     translate_key("C-g", &reset_state, &reset_key);
193     translate_key("Escape", &i, &button_escape);
194     translate_key("Return", &i, &button_return);
195
196     grab_keys();
197 }
198
199 void plugin_shutdown()
200 {
201     dispatch_register(0, (EventHandler)event, NULL);
202
203     tree_destroy(firstnode);
204     firstnode = NULL;
205     ungrab_all_keys();
206 }
207