]> Dogcows Code - chaz/openbox/blob - plugins/keyboard/keyboard.c
0fc43e83e1d72c00554dfb4d3c135bee8e020fba
[chaz/openbox] / plugins / keyboard / keyboard.c
1 #include "kernel/focus.h"
2 #include "kernel/frame.h"
3 #include "kernel/dispatch.h"
4 #include "kernel/openbox.h"
5 #include "kernel/event.h"
6 #include "kernel/grab.h"
7 #include "kernel/client.h"
8 #include "kernel/action.h"
9 #include "kernel/prop.h"
10 #include "kernel/timer.h"
11 #include "parser/parse.h"
12 #include "tree.h"
13 #include "keyboard.h"
14 #include "translate.h"
15 #include <glib.h>
16
17 /*
18
19 <keybind key="C-x">
20 <action name="ChangeDesktop">
21 <desktop>3</desktop>
22 </action>
23 </keybind>
24
25 */
26
27 static void parse_key(xmlDocPtr doc, xmlNodePtr node, GList *keylist)
28 {
29 char *key;
30 Action *action;
31 xmlNodePtr n, nact;
32 GList *it;
33
34 n = parse_find_node("keybind", node);
35 while (n) {
36 if (parse_attr_string("key", n, &key)) {
37 keylist = g_list_append(keylist, key);
38
39 parse_key(doc, n->xmlChildrenNode, keylist);
40
41 it = g_list_last(keylist);
42 g_free(it->data);
43 keylist = g_list_delete_link(keylist, it);
44 }
45 n = parse_find_node("keybind", n->next);
46 }
47 if (keylist) {
48 nact = parse_find_node("action", node);
49 while (nact) {
50 if ((action = action_parse(doc, nact))) {
51 /* validate that its okay for a key binding */
52 if (action->func == action_moveresize &&
53 action->data.moveresize.corner !=
54 prop_atoms.net_wm_moveresize_move_keyboard &&
55 action->data.moveresize.corner !=
56 prop_atoms.net_wm_moveresize_size_keyboard) {
57 action_free(action);
58 action = NULL;
59 }
60
61 if (action)
62 kbind(keylist, action);
63 }
64 nact = parse_find_node("action", nact->next);
65 }
66 }
67 }
68
69 static void parse_xml(xmlDocPtr doc, xmlNodePtr node, void *d)
70 {
71 parse_key(doc, node, NULL);
72 }
73
74 void plugin_setup_config()
75 {
76 parse_register("keyboard", parse_xml, NULL);
77 }
78
79 KeyBindingTree *firstnode = NULL;
80
81 static KeyBindingTree *curpos;
82 static guint reset_key, reset_state;
83 static ObTimer *chain_timer;
84
85 static void grab_for_window(Window win, gboolean grab)
86 {
87 KeyBindingTree *p;
88
89 ungrab_all_keys(win);
90
91 if (grab) {
92 p = curpos ? curpos->first_child : firstnode;
93 while (p) {
94 grab_key(p->key, p->state, win, GrabModeAsync);
95 p = p->next_sibling;
96 }
97 if (curpos)
98 grab_key(reset_key, reset_state, win, GrabModeAsync);
99 }
100 }
101
102 static void grab_keys(gboolean grab)
103 {
104 GList *it;
105
106 grab_for_window(focus_backup, grab);
107 for (it = client_list; it; it = g_list_next(it))
108 grab_for_window(((ObClient*)it->data)->frame->window, grab);
109 }
110
111 static void reset_chains()
112 {
113 if (chain_timer) {
114 timer_stop(chain_timer);
115 chain_timer = NULL;
116 }
117 if (curpos) {
118 curpos = NULL;
119 grab_keys(TRUE);
120 }
121 }
122
123 static void chain_timeout(void *data)
124 {
125 reset_chains();
126 }
127
128 gboolean kbind(GList *keylist, Action *action)
129 {
130 KeyBindingTree *tree, *t;
131 gboolean conflict;
132
133 g_assert(keylist != NULL);
134 g_assert(action != NULL);
135
136 if (!(tree = tree_build(keylist)))
137 return FALSE;
138
139 if ((t = tree_find(tree, &conflict)) != NULL) {
140 /* already bound to something, use the existing tree */
141 tree_destroy(tree);
142 tree = NULL;
143 } else
144 t = tree;
145 while (t->first_child) t = t->first_child;
146
147 if (conflict) {
148 g_message("conflict with binding");
149 tree_destroy(tree);
150 return FALSE;
151 }
152
153 /* set the action */
154 t->actions = g_slist_append(t->actions, action);
155 /* assimilate this built tree into the main tree. assimilation
156 destroys/uses the tree */
157 if (tree) tree_assimilate(tree);
158
159 return TRUE;
160 }
161
162 static void event(ObEvent *e, void *foo)
163 {
164 static KeyBindingTree *grabbed_key = NULL;
165
166 if (e->type == Event_Client_Mapped) {
167 grab_for_window(e->data.c.client->window, TRUE);
168 return;
169 } else if (e->type == Event_Client_Destroy) {
170 grab_for_window(e->data.c.client->window, FALSE);
171 return;
172 }
173
174 if (grabbed_key) {
175 gboolean done = FALSE;
176
177 if ((e->type == Event_X_KeyRelease &&
178 !(grabbed_key->state & e->data.x.e->xkey.state)))
179 done = TRUE;
180 else if (e->type == Event_X_KeyPress) {
181 if (e->data.x.e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
182 done = TRUE;
183 else if (e->data.x.e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
184 GSList *it;
185 for (it = grabbed_key->actions; it; it = it->next) {
186 Action *act = it->data;
187 act->data.cycle.cancel = TRUE;
188 }
189 done = TRUE;
190 }
191 }
192 if (done) {
193 GSList *it;
194 for (it = grabbed_key->actions; it; it = it->next) {
195 Action *act = it->data;
196 act->data.cycle.final = TRUE;
197 act->func(&act->data);
198 }
199 grabbed_key = NULL;
200 grab_keyboard(FALSE);
201 reset_chains();
202 return;
203 }
204 }
205 if (e->type == Event_X_KeyRelease)
206 return;
207
208 g_assert(e->type == Event_X_KeyPress);
209
210 if (e->data.x.e->xkey.keycode == reset_key &&
211 e->data.x.e->xkey.state == reset_state) {
212 reset_chains();
213 } else {
214 KeyBindingTree *p;
215 if (curpos == NULL)
216 p = firstnode;
217 else
218 p = curpos->first_child;
219 while (p) {
220 if (p->key == e->data.x.e->xkey.keycode &&
221 p->state == e->data.x.e->xkey.state) {
222 if (p->first_child != NULL) { /* part of a chain */
223 if (chain_timer) timer_stop(chain_timer);
224 /* 5 second timeout for chains */
225 chain_timer = timer_start(5000*1000, chain_timeout,
226 NULL);
227 curpos = p;
228 grab_keys(TRUE);
229 } else {
230 GSList *it;
231 for (it = p->actions; it; it = it->next) {
232 Action *act = it->data;
233 if (act->func != NULL) {
234 act->data.any.c = focus_client;
235
236 if (act->func == action_cycle_windows) {
237 act->data.cycle.final = FALSE;
238 act->data.cycle.cancel = FALSE;
239 }
240
241 if (act->func == action_cycle_windows &&
242 !grabbed_key && grab_keyboard(TRUE)) {
243 grabbed_key = p;
244 }
245
246 act->data.any.c = focus_client;
247 act->func(&act->data);
248 }
249 }
250
251 reset_chains();
252 }
253 break;
254 }
255 p = p->next_sibling;
256 }
257 }
258 }
259
260 void plugin_startup()
261 {
262 curpos = NULL;
263 chain_timer = NULL;
264
265 dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
266 Event_X_KeyPress | Event_X_KeyRelease,
267 (EventHandler)event, NULL);
268
269 translate_key("C-g", &reset_state, &reset_key);
270
271 grab_keys(TRUE);
272 }
273
274 void plugin_shutdown()
275 {
276 dispatch_register(0, (EventHandler)event, NULL);
277
278 tree_destroy(firstnode);
279 firstnode = NULL;
280 grab_keys(FALSE);
281 }
282
This page took 0.043062 seconds and 3 git commands to generate.