]> Dogcows Code - chaz/openbox/blob - plugins/keyboard/keyboard.c
bf670c49f89ba41807925a6c1d992d39032afc7e
[chaz/openbox] / 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 if ((t = tree_find(tree, &conflict)) != NULL) {
60 /* already bound to something */
61 g_message("keychain is already bound");
62 tree_destroy(tree);
63 return FALSE;
64 }
65 if (conflict) {
66 g_message("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 event(ObEvent *e, void *foo)
90 {
91 static KeyBindingTree *grabbed_key = NULL;
92
93 if (grabbed_key) {
94 gboolean done = FALSE;
95
96 if ((e->type == Event_X_KeyRelease &&
97 !(grabbed_key->state & e->data.x.e->xkey.state)))
98 done = TRUE;
99 else if (e->type == Event_X_KeyPress) {
100 if (e->data.x.e->xkey.keycode == button_return)
101 done = TRUE;
102 else if (e->data.x.e->xkey.keycode == button_escape) {
103 grabbed_key->action->data.cycle.cancel = TRUE;
104 done = TRUE;
105 }
106 }
107 if (done) {
108 grabbed_key->action->data.cycle.final = TRUE;
109 grabbed_key->action->func(&grabbed_key->action->data);
110 grab_keyboard(FALSE);
111 grabbed_key = NULL;
112 reset_chains();
113 return;
114 }
115 }
116 if (e->type == Event_X_KeyRelease)
117 return;
118
119 g_assert(e->type == Event_X_KeyPress);
120
121 if (e->data.x.e->xkey.keycode == reset_key &&
122 e->data.x.e->xkey.state == reset_state) {
123 reset_chains();
124 } else {
125 KeyBindingTree *p;
126 if (curpos == NULL)
127 p = firstnode;
128 else
129 p = curpos->first_child;
130 while (p) {
131 if (p->key == e->data.x.e->xkey.keycode &&
132 p->state == e->data.x.e->xkey.state) {
133 if (p->first_child != NULL) { /* part of a chain */
134 /* XXX TIMER */
135 if (!grabbed) {
136 grab_keyboard(TRUE);
137 grabbed = TRUE;
138 XAllowEvents(ob_display, AsyncKeyboard,
139 event_lasttime);
140 }
141 curpos = p;
142 } else {
143 if (p->action->func != NULL) {
144 p->action->data.any.c = focus_client;
145
146 if (p->action->func == action_cycle_windows) {
147 p->action->data.cycle.final = FALSE;
148 p->action->data.cycle.cancel = FALSE;
149 }
150
151 p->action->func(&p->action->data);
152
153 if (p->action->func == action_cycle_windows &&
154 !grabbed_key) {
155 grab_keyboard(TRUE);
156 grabbed_key = p;
157 }
158 }
159
160 reset_chains();
161 }
162 break;
163 }
164 p = p->next_sibling;
165 }
166 }
167 }
168
169 void plugin_startup()
170 {
171 guint i;
172
173 curpos = NULL;
174 grabbed = FALSE;
175
176 dispatch_register(Event_X_KeyPress | Event_X_KeyRelease, (EventHandler)event, NULL);
177
178 translate_key("C-g", &reset_state, &reset_key);
179 translate_key("Escape", &i, &button_escape);
180 translate_key("Return", &i, &button_return);
181 }
182
183 void plugin_shutdown()
184 {
185 dispatch_register(0, (EventHandler)event, NULL);
186
187 grab_keys(FALSE);
188 tree_destroy(firstnode);
189 firstnode = NULL;
190 grab_keys(TRUE);
191 }
192
This page took 0.044663 seconds and 3 git commands to generate.