]> icculus.org git repositories - dana/openbox.git/blob - plugins/keyboard/keyboard.c
XAllowEvents with a timestamp, otherwise we end up with a grab in place that we dont...
[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/grab.h"
5 #include "../../kernel/action.h"
6 #include "tree.h"
7 #include "keyboard.h"
8 #include <glib.h>
9
10 void plugin_setup_config()
11 {
12 }
13
14 KeyBindingTree *firstnode;
15
16 static KeyBindingTree *curpos;
17 static guint reset_key, reset_state;
18 static gboolean grabbed;
19
20 static void grab_keys(gboolean grab)
21 {
22     if (!grab) {
23         ungrab_all_keys();
24     } else {
25         KeyBindingTree *p = firstnode;
26         while (p) {
27             grab_key(p->key, p->state, GrabModeSync);
28             p = p->next_sibling;
29         }
30     }
31 }
32
33 static void reset_chains()
34 {
35     /* XXX kill timer */
36     curpos = NULL;
37     if (grabbed) {
38         grabbed = FALSE;
39         grab_keyboard(FALSE);
40     }
41 }
42
43 static gboolean kbind(GList *keylist, Action *action)
44 {
45     KeyBindingTree *tree, *t;
46     gboolean conflict;
47
48     g_assert(keylist != NULL);
49     g_assert(action != NULL);
50
51     if (!(tree = tree_build(keylist))) {
52         g_warning("invalid binding");
53         return FALSE;
54     }
55     if ((t = tree_find(tree, &conflict)) != NULL) {
56         /* already bound to something */
57         g_warning("keychain is already bound");
58         tree_destroy(tree);
59         return FALSE;
60     }
61     if (conflict) {
62         g_warning("conflict with binding");
63         tree_destroy(tree);
64         return FALSE;
65     }
66
67     /* grab the server here to make sure no key presses go missed */
68     grab_server(TRUE);
69     grab_keys(FALSE);
70
71     /* set the action */
72     t = tree;
73     while (t->first_child) t = t->first_child;
74     t->action = action;
75     /* assimilate this built tree into the main tree. assimilation
76        destroys/uses the tree */
77     tree_assimilate(tree);
78
79     grab_keys(TRUE); 
80     grab_server(FALSE);
81
82     return TRUE;
83 }
84
85 static void press(ObEvent *e, void *foo)
86 {
87     if (e->data.x.e->xkey.keycode == reset_key &&
88         e->data.x.e->xkey.state == reset_state) {
89         reset_chains();
90     } else {
91         KeyBindingTree *p;
92         if (curpos == NULL)
93             p = firstnode;
94         else
95             p = curpos->first_child;
96         while (p) {
97             if (p->key == e->data.x.e->xkey.keycode &&
98                 p->state == e->data.x.e->xkey.state) {
99                 if (p->first_child != NULL) { /* part of a chain */
100                     /* XXX TIMER */
101                     if (!grabbed) {
102                         grab_keyboard(TRUE);
103                         grabbed = TRUE;
104                     }
105                     curpos = p;
106                 } else {
107                     if (p->action->func != NULL) {
108                         p->action->data.any.c = focus_client;
109
110                         g_assert(!(p->action->func == action_move ||
111                                    p->action->func == action_resize));
112
113                         p->action->func(&p->action->data);
114                     }
115
116                     reset_chains();
117                 }
118                 break;
119             }
120             p = p->next_sibling;
121         }
122     }
123     XAllowEvents(ob_display, AsyncKeyboard, e->data.x.e->xkey.time);
124 }
125
126 static void binddef()
127 {
128     GList *list = g_list_append(NULL, NULL);
129     Action *a;
130
131     /* When creating an Action struct, all of the data elements in the
132        appropriate struct need to be set, except the Client*, which will be set
133        at call-time when then action function is used.
134     */
135
136     list->data = "A-Right";
137     a = action_new(action_next_desktop);
138     a->data.nextprevdesktop.wrap = TRUE;
139     kbind(list, a);
140
141     list->data = "A-Left";
142     a = action_new(action_previous_desktop);
143     a->data.nextprevdesktop.wrap = TRUE;
144     kbind(list, a);
145
146     list->data = "A-1";
147     a = action_new(action_desktop);
148     a->data.desktop.desk = 0;
149     kbind(list, a);
150
151     list->data = "A-2"; 
152     a = action_new(action_desktop);
153     a->data.desktop.desk = 1;
154     kbind(list, a);
155
156     list->data = "A-3";
157     a = action_new(action_desktop);
158     a->data.desktop.desk = 2;
159     kbind(list, a);
160
161     list->data = "A-4";
162     a = action_new(action_desktop);
163     a->data.desktop.desk = 3;
164     kbind(list, a);
165
166     list->data = "A-space";
167     a = action_new(action_execute);
168     a->data.execute.path = g_strdup("xterm");
169     kbind(list, a);
170
171     list->data = "C-A-Escape";
172     a = action_new(action_execute);
173     a->data.execute.path = g_strdup("xlock -nolock -mode puzzle");
174     kbind(list, a);
175 }
176
177 void plugin_startup()
178 {
179     dispatch_register(Event_X_KeyPress, (EventHandler)press, NULL);
180
181     /* XXX parse config file! */
182     binddef();
183 }
184
185 void plugin_shutdown()
186 {
187     dispatch_register(0, (EventHandler)press, NULL);
188
189     grab_keys(FALSE);
190     tree_destroy(firstnode);
191     firstnode = NULL;
192     grab_keys(TRUE);
193 }
194