]> icculus.org git repositories - mikachu/openbox.git/blob - plugins/keyboard/keyboard.c
Indentation stuff
[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 "tree.h"
9 #include "keyboard.h"
10 #include "keyparse.h"
11 #include "translate.h"
12 #include <glib.h>
13
14 void plugin_setup_config()
15 {
16     parse_reg_section("keyboard", keyparse, NULL);
17 }
18
19 KeyBindingTree *firstnode = NULL;
20
21 static KeyBindingTree *curpos;
22 static guint reset_key, reset_state, button_return, button_escape;
23 static gboolean grabbed;
24
25 static void grab_keys(gboolean grab)
26 {
27     if (!grab) {
28         ungrab_all_keys();
29     } else {
30         KeyBindingTree *p = firstnode;
31         while (p) {
32             grab_key(p->key, p->state, GrabModeSync);
33             p = p->next_sibling;
34         }
35     }
36 }
37
38 static void reset_chains()
39 {
40     /* XXX kill timer */
41     curpos = NULL;
42     if (grabbed) {
43         grabbed = FALSE;
44         grab_keyboard(FALSE);
45     } else
46         XAllowEvents(ob_display, AsyncKeyboard, event_lasttime);
47 }
48
49 gboolean kbind(GList *keylist, Action *action)
50 {
51     KeyBindingTree *tree, *t;
52     gboolean conflict;
53
54     g_assert(keylist != NULL);
55     g_assert(action != NULL);
56
57     if (!(tree = tree_build(keylist)))
58         return FALSE;
59
60     if ((t = tree_find(tree, &conflict)) != NULL) {
61         /* already bound to something, use the existing tree */
62         tree_destroy(tree);
63         tree = NULL;
64     } else
65         t = tree;
66     while (t->first_child) t = t->first_child;
67
68     if (conflict) {
69         g_message("conflict with binding");
70         tree_destroy(tree);
71         return FALSE;
72     }
73
74     /* grab the server here to make sure no key presses go missed */
75     grab_server(TRUE);
76     grab_keys(FALSE);
77
78     /* set the action */
79     t->actions = g_slist_append(t->actions, action);
80     /* assimilate this built tree into the main tree. assimilation
81        destroys/uses the tree */
82     if (tree) tree_assimilate(tree);
83
84     grab_keys(TRUE); 
85     grab_server(FALSE);
86
87     return TRUE;
88 }
89
90 static void event(ObEvent *e, void *foo)
91 {
92     static KeyBindingTree *grabbed_key = NULL;
93
94     if (grabbed_key) {
95         gboolean done = FALSE;
96
97         if ((e->type == Event_X_KeyRelease && 
98              !(grabbed_key->state & e->data.x.e->xkey.state)))
99             done = TRUE;
100         else if (e->type == Event_X_KeyPress) {
101             if (e->data.x.e->xkey.keycode == button_return)
102                 done = TRUE;
103             else if (e->data.x.e->xkey.keycode == button_escape) {
104                 GSList *it;
105                 for (it = grabbed_key->actions; it; it = it->next) {
106                     Action *act = it->data;
107                     act->data.cycle.cancel = TRUE;
108                 }
109                 done = TRUE;
110             }
111         }
112         if (done) { 
113             GSList *it;
114             for (it = grabbed_key->actions; it; it = it->next) {
115                 Action *act = it->data;
116                 act->data.cycle.final = TRUE;
117                 act->func(&act->data);
118                 grab_keyboard(FALSE);
119                 grabbed_key = NULL;
120                 reset_chains();
121                 return;
122             }
123         }
124     }
125     if (e->type == Event_X_KeyRelease)
126         return;
127
128     g_assert(e->type == Event_X_KeyPress);
129
130     if (e->data.x.e->xkey.keycode == reset_key &&
131         e->data.x.e->xkey.state == reset_state) {
132         reset_chains();
133     } else {
134         KeyBindingTree *p;
135         if (curpos == NULL)
136             p = firstnode;
137         else
138             p = curpos->first_child;
139         while (p) {
140             if (p->key == e->data.x.e->xkey.keycode &&
141                 p->state == e->data.x.e->xkey.state) {
142                 if (p->first_child != NULL) { /* part of a chain */
143                     /* XXX TIMER */
144                     if (!grabbed) {
145                         grab_keyboard(TRUE);
146                         grabbed = TRUE;
147                         XAllowEvents(ob_display, AsyncKeyboard,
148                                      event_lasttime);
149                     }
150                     curpos = p;
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                                 grab_keyboard(TRUE);
168                                 grabbed_key = p;
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     grabbed = FALSE;
188
189     dispatch_register(Event_X_KeyPress | Event_X_KeyRelease, (EventHandler)event, NULL);
190
191     translate_key("C-g", &reset_state, &reset_key);
192     translate_key("Escape", &i, &button_escape);
193     translate_key("Return", &i, &button_return);
194 }
195
196 void plugin_shutdown()
197 {
198     dispatch_register(0, (EventHandler)event, NULL);
199
200     grab_keys(FALSE);
201     tree_destroy(firstnode);
202     firstnode = NULL;
203     grab_keys(TRUE);
204 }
205