add the keyboard plugin into the build systems
[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 "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);
17 }
18
19 KeyBindingTree *firstnode;
20
21 static KeyBindingTree *curpos;
22 static guint reset_key, reset_state;
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     if ((t = tree_find(tree, &conflict)) != NULL) {
60         /* already bound to something */
61         g_warning("keychain is already bound");
62         tree_destroy(tree);
63         return FALSE;
64     }
65     if (conflict) {
66         g_warning("conflict with binding");
67         tree_destroy(tree);
68         return FALSE;
69     }
70
71     /* grab the server here to make sure no key presses go missed */
72     grab_server(TRUE);
73     grab_keys(FALSE);
74
75     /* set the action */
76     t = tree;
77     while (t->first_child) t = t->first_child;
78     t->action = action;
79     /* assimilate this built tree into the main tree. assimilation
80        destroys/uses the tree */
81     tree_assimilate(tree);
82
83     grab_keys(TRUE); 
84     grab_server(FALSE);
85
86     return TRUE;
87 }
88
89 static void press(ObEvent *e, void *foo)
90 {
91     if (e->data.x.e->xkey.keycode == reset_key &&
92         e->data.x.e->xkey.state == reset_state) {
93         reset_chains();
94     } else {
95         KeyBindingTree *p;
96         if (curpos == NULL)
97             p = firstnode;
98         else
99             p = curpos->first_child;
100         while (p) {
101             if (p->key == e->data.x.e->xkey.keycode &&
102                 p->state == e->data.x.e->xkey.state) {
103                 if (p->first_child != NULL) { /* part of a chain */
104                     /* XXX TIMER */
105                     if (!grabbed) {
106                         grab_keyboard(TRUE);
107                         grabbed = TRUE;
108                         XAllowEvents(ob_display, AsyncKeyboard,
109                                      event_lasttime);
110                     }
111                     curpos = p;
112                 } else {
113                     if (p->action->func != NULL) {
114                         p->action->data.any.c = focus_client;
115
116                         g_assert(!(p->action->func == action_move ||
117                                    p->action->func == action_resize));
118
119                         p->action->func(&p->action->data);
120                     }
121
122                     reset_chains();
123                 }
124                 break;
125             }
126             p = p->next_sibling;
127         }
128     }
129 }
130
131 void plugin_startup()
132 {
133     dispatch_register(Event_X_KeyPress, (EventHandler)press, NULL);
134
135     translate_key("C-g", &reset_state, &reset_key);
136 }
137
138 void plugin_shutdown()
139 {
140     dispatch_register(0, (EventHandler)press, NULL);
141
142     grab_keys(FALSE);
143     tree_destroy(firstnode);
144     firstnode = NULL;
145     grab_keys(TRUE);
146 }
147