only grab C-g while in a chain
[dana/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     if (curpos)
38         grab_key(reset_key, reset_state, GrabModeAsync);
39 }
40
41 static void reset_chains()
42 {
43     if (chain_timer) {
44         timer_stop(chain_timer);
45         chain_timer = NULL;
46     }
47     if (curpos) {
48         curpos = NULL;
49         grab_keys();
50     }
51 }
52
53 static void chain_timeout(void *data)
54 {
55     reset_chains();
56 }
57
58 gboolean kbind(GList *keylist, Action *action)
59 {
60     KeyBindingTree *tree, *t;
61     gboolean conflict;
62
63     g_assert(keylist != NULL);
64     g_assert(action != NULL);
65
66     if (!(tree = tree_build(keylist)))
67         return FALSE;
68
69     if ((t = tree_find(tree, &conflict)) != NULL) {
70         /* already bound to something, use the existing tree */
71         tree_destroy(tree);
72         tree = NULL;
73     } else
74         t = tree;
75     while (t->first_child) t = t->first_child;
76
77     if (conflict) {
78         g_message("conflict with binding");
79         tree_destroy(tree);
80         return FALSE;
81     }
82
83     /* set the action */
84     t->actions = g_slist_append(t->actions, action);
85     /* assimilate this built tree into the main tree. assimilation
86        destroys/uses the tree */
87     if (tree) tree_assimilate(tree);
88
89     return TRUE;
90 }
91
92 static void event(ObEvent *e, void *foo)
93 {
94     static KeyBindingTree *grabbed_key = NULL;
95
96     if (grabbed_key) {
97         gboolean done = FALSE;
98
99         if ((e->type == Event_X_KeyRelease && 
100              !(grabbed_key->state & e->data.x.e->xkey.state)))
101             done = TRUE;
102         else if (e->type == Event_X_KeyPress) {
103             if (e->data.x.e->xkey.keycode == button_return)
104                 done = TRUE;
105             else if (e->data.x.e->xkey.keycode == button_escape) {
106                 GSList *it;
107                 for (it = grabbed_key->actions; it; it = it->next) {
108                     Action *act = it->data;
109                     act->data.cycle.cancel = TRUE;
110                 }
111                 done = TRUE;
112             }
113         }
114         if (done) { 
115             GSList *it;
116             for (it = grabbed_key->actions; it; it = it->next) {
117                 Action *act = it->data;
118                 act->data.cycle.final = TRUE;
119                 act->func(&act->data);
120                 grabbed_key = NULL;
121                 grab_keyboard(FALSE);
122                 reset_chains();
123                 return;
124             }
125         }
126     }
127     if (e->type == Event_X_KeyRelease)
128         return;
129
130     g_assert(e->type == Event_X_KeyPress);
131
132     if (e->data.x.e->xkey.keycode == reset_key &&
133         e->data.x.e->xkey.state == reset_state) {
134         reset_chains();
135     } else {
136         KeyBindingTree *p;
137         if (curpos == NULL)
138             p = firstnode;
139         else
140             p = curpos->first_child;
141         while (p) {
142             if (p->key == e->data.x.e->xkey.keycode &&
143                 p->state == e->data.x.e->xkey.state) {
144                 if (p->first_child != NULL) { /* part of a chain */
145                     if (chain_timer) timer_stop(chain_timer);
146                     /* 5 second timeout for chains */
147                     chain_timer = timer_start(5000*1000, chain_timeout,
148                                               NULL);
149                     curpos = p;
150                     grab_keys();
151                 } else {
152                     GSList *it;
153                     for (it = p->actions; it; it = it->next) {
154                         Action *act = it->data;
155                         if (act->func != NULL) {
156                             act->data.any.c = focus_client;
157
158                             if (act->func == action_cycle_windows) {
159                                 act->data.cycle.final = FALSE;
160                                 act->data.cycle.cancel = FALSE;
161                             }
162
163                             act->func(&act->data);
164
165                             if (act->func == action_cycle_windows &&
166                                 !grabbed_key) {
167                                 grabbed_key = p;
168                                 grab_keyboard(TRUE);
169                                 break;
170                             }
171                         }
172                     }
173
174                     reset_chains();
175                 }
176                 break;
177             }
178             p = p->next_sibling;
179         }
180     }
181 }
182
183 void plugin_startup()
184 {
185     guint i;
186
187     curpos = NULL;
188     chain_timer = NULL;
189
190     dispatch_register(Event_X_KeyPress | Event_X_KeyRelease,
191                       (EventHandler)event, NULL);
192
193     translate_key("C-g", &reset_state, &reset_key);
194     translate_key("Escape", &i, &button_escape);
195     translate_key("Return", &i, &button_return);
196
197     grab_keys();
198 }
199
200 void plugin_shutdown()
201 {
202     dispatch_register(0, (EventHandler)event, NULL);
203
204     tree_destroy(firstnode);
205     firstnode = NULL;
206     ungrab_all_keys();
207 }
208