]> icculus.org git repositories - dana/openbox.git/blob - plugins/keyboard/keyboard.c
only let bind Move and Resize to Drags
[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         XAllowEvents(ob_display, AsyncKeyboard, CurrentTime);
91     } else {
92         KeyBindingTree *p;
93         if (curpos == NULL)
94             p = firstnode;
95         else
96             p = curpos->first_child;
97         while (p) {
98             if (p->key == e->data.x.e->xkey.keycode &&
99                 p->state == e->data.x.e->xkey.state) {
100                 if (p->first_child != NULL) { /* part of a chain */
101                     /* XXX TIMER */
102                     if (!grabbed) {
103                         grab_keyboard(TRUE);
104                         grabbed = TRUE;
105                     }
106                     curpos = p;
107                     XAllowEvents(ob_display, AsyncKeyboard, CurrentTime);
108                 } else {
109                     if (p->action->func != NULL) {
110                         p->action->data.any.c = focus_client;
111
112                         g_assert(!(p->action->func == action_move ||
113                                    p->action->func == action_resize));
114
115                         p->action->func(&p->action->data);
116                     }
117
118                     XAllowEvents(ob_display, AsyncKeyboard, CurrentTime);
119                     reset_chains();
120                 }
121                 break;
122             }
123             p = p->next_sibling;
124         }
125     }
126 }
127
128 static void binddef()
129 {
130     GList *list = g_list_append(NULL, NULL);
131     Action *a;
132
133     /* When creating an Action struct, all of the data elements in the
134        appropriate struct need to be set, except the Client*, which will be set
135        at call-time when then action function is used.
136     */
137
138     list->data = "A-Right";
139     a = action_new(action_next_desktop);
140     a->data.nextprevdesktop.wrap = TRUE;
141     kbind(list, a);
142
143     list->data = "A-Left";
144     a = action_new(action_previous_desktop);
145     a->data.nextprevdesktop.wrap = TRUE;
146     kbind(list, a);
147
148     list->data = "A-1";
149     a = action_new(action_desktop);
150     a->data.desktop.desk = 0;
151     kbind(list, a);
152
153     list->data = "A-2"; 
154     a = action_new(action_desktop);
155     a->data.desktop.desk = 1;
156     kbind(list, a);
157
158     list->data = "A-3";
159     a = action_new(action_desktop);
160     a->data.desktop.desk = 2;
161     kbind(list, a);
162
163     list->data = "A-4";
164     a = action_new(action_desktop);
165     a->data.desktop.desk = 3;
166     kbind(list, a);
167
168     list->data = "A-space";
169     a = action_new(action_execute);
170     a->data.execute.path = g_strdup("xterm");
171     kbind(list, a);
172
173     list->data = "C-A-Escape";
174     a = action_new(action_execute);
175     a->data.execute.path = g_strdup("xlock -nolock -mode puzzle");
176     kbind(list, a);
177 }
178
179 void plugin_startup()
180 {
181     dispatch_register(Event_X_KeyPress, (EventHandler)press, NULL);
182
183     /* XXX parse config file! */
184     binddef();
185 }
186
187 void plugin_shutdown()
188 {
189     dispatch_register(0, (EventHandler)press, NULL);
190
191     grab_keys(FALSE);
192     tree_destroy(firstnode);
193     firstnode = NULL;
194     grab_keys(TRUE);
195 }
196