]> Dogcows Code - chaz/openbox/blob - src/bindings.cc
4cffb559bcada1e449a0d97856882fa22af48c0c
[chaz/openbox] / src / bindings.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include "bindings.hh"
8 #include "otk/display.hh"
9
10 extern "C" {
11 #include <X11/Xlib.h>
12
13 #include "gettext.h"
14 #define _(str) gettext(str)
15 }
16
17 namespace ob {
18
19 #include <stdio.h>
20 static void print_branch(BindingTree *first, std::string str)
21 {
22 BindingTree *p = first;
23
24 while (p) {
25 if (p->first_child)
26 print_branch(p->first_child, str + " " + p->text);
27 if (!p->chain)
28 printf("%d%s\n", p->id, (str + " " + p->text).c_str());
29 p = p->next_sibling;
30 }
31 }
32
33
34 void OBBindings::display()
35 {
36 if (_keytree.first_child) {
37 printf("Key Tree:\n");
38 print_branch(_keytree.first_child, "");
39 }
40 if (_mousetree) {
41 printf("Mouse Tree:\n");
42 BindingTree *p = _mousetree;
43 while (p) {
44 printf("%d %s\n", p->id, p->text.c_str());
45 p = p->next_sibling;
46 }
47 }
48 }
49
50
51 static bool buttonvalue(const std::string &button, unsigned int *val)
52 {
53 if (button == "1" || button == "Button1") {
54 *val |= Button1;
55 } else if (button == "2" || button == "Button2") {
56 *val |= Button2;
57 } else if (button == "3" || button == "Button3") {
58 *val |= Button3;
59 } else if (button == "4" || button == "Button4") {
60 *val |= Button4;
61 } else if (button == "5" || button == "Button5") {
62 *val |= Button5;
63 } else
64 return false;
65 return true;
66 }
67
68 static bool modvalue(const std::string &mod, unsigned int *val)
69 {
70 if (mod == "C") { // control
71 *val |= ControlMask;
72 } else if (mod == "S") { // shift
73 *val |= ShiftMask;
74 } else if (mod == "A" || // alt/mod1
75 mod == "M" ||
76 mod == "Mod1" ||
77 mod == "M1") {
78 *val |= Mod1Mask;
79 } else if (mod == "Mod2" || // mod2
80 mod == "M2") {
81 *val |= Mod2Mask;
82 } else if (mod == "Mod3" || // mod3
83 mod == "M3") {
84 *val |= Mod3Mask;
85 } else if (mod == "W" || // windows/mod4
86 mod == "Mod4" ||
87 mod == "M4") {
88 *val |= Mod4Mask;
89 } else if (mod == "Mod5" || // mod5
90 mod == "M5") {
91 *val |= Mod5Mask;
92 } else { // invalid
93 return false;
94 }
95 return true;
96 }
97
98 bool OBBindings::translate(const std::string &str, Binding &b, bool askey)
99 {
100 // parse out the base key name
101 std::string::size_type keybegin = str.find_last_of('-');
102 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
103 std::string key(str, keybegin);
104
105 // parse out the requested modifier keys
106 unsigned int modval = 0;
107 std::string::size_type begin = 0, end;
108 while (begin != keybegin) {
109 end = str.find_first_of('-', begin);
110
111 std::string mod(str, begin, end-begin);
112 if (!modvalue(mod, &modval)) {
113 // printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
114 return false;
115 }
116
117 begin = end + 1;
118 }
119
120 // set the binding
121 b.modifiers = modval;
122 if (askey) {
123 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
124 if (sym == NoSymbol) return false;
125 b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
126 return b.key != 0;
127 } else {
128 return buttonvalue(key, &b.key);
129 }
130 }
131
132 static void destroytree(BindingTree *tree)
133 {
134 while (tree) {
135 BindingTree *c = tree->first_child;
136 delete tree;
137 tree = c;
138 }
139 }
140
141 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id)
142 {
143 if (keylist.empty()) return 0; // nothing in the list.. return 0
144
145 BindingTree *ret = 0, *p;
146
147 StringVect::const_reverse_iterator it, end = keylist.rend();
148 for (it = keylist.rbegin(); it != end; ++it) {
149 p = ret;
150 ret = new BindingTree(id);
151 if (!p) ret->chain = false;
152 ret->first_child = p;
153 if (!translate(*it, ret->binding, true)) {
154 destroytree(ret);
155 ret = 0;
156 break;
157 }
158 ret->text = *it; // XXX: rm me
159 }
160 return ret;
161 }
162
163
164 OBBindings::OBBindings()
165 : _curpos(&_keytree), _mousetree(0)
166 {
167 }
168
169
170 OBBindings::~OBBindings()
171 {
172 remove_all();
173 }
174
175
176 bool OBBindings::add_mouse(const std::string &button, int id)
177 {
178 BindingTree n;
179
180 if (!translate(button, n.binding, false))
181 return false;
182
183 BindingTree *p = _mousetree, **newp = &_mousetree;
184 while (p) {
185 if (p->binding == n.binding)
186 return false; // conflict
187 p = p->next_sibling;
188 newp = &p->next_sibling;
189 }
190 display();
191 *newp = new BindingTree(id);
192 display();
193 (*newp)->text = button;
194 (*newp)->chain = false;
195 (*newp)->binding.key = n.binding.key;
196 (*newp)->binding.modifiers = n.binding.modifiers;
197
198 return true;
199 }
200
201
202 int OBBindings::remove_mouse(const std::string &button)
203 {
204 (void)button;
205 assert(false); // XXX: function not implemented yet
206 }
207
208
209 void OBBindings::assimilate(BindingTree *node)
210 {
211 BindingTree *a, *b, *tmp, *last;
212
213 printf("node=%lx\n", (long)node);
214 if (!_keytree.first_child) {
215 // there are no nodes at this level yet
216 _keytree.first_child = node;
217 return;
218 } else {
219 a = _keytree.first_child;
220 last = a;
221 b = node;
222 while (a) {
223 printf("in while.. b=%lx\n", (long)b);
224 last = a;
225 if (a->binding != b->binding) {
226 a = a->next_sibling;
227 } else {
228 printf("a: %s %d %d\n", a->text.c_str(), a->binding.key, a->binding.modifiers);
229 printf("b: %s %d %d\n", b->text.c_str(), b->binding.key, b->binding.modifiers);
230 printf("moving up one in b\n");
231 tmp = b;
232 b = b->first_child;
233 delete tmp;
234 a = a->first_child;
235 }
236 }
237 printf("after while.. b=%lx\n", (long)b);
238 if (last->binding != b->binding)
239 last->next_sibling = b;
240 else
241 last->first_child = b->first_child;
242 delete b;
243 }
244 }
245
246
247 int OBBindings::find_key(BindingTree *search) {
248 BindingTree *a, *b;
249 a = _keytree.first_child;
250 b = search;
251 while (a && b) {
252 if (a->binding != b->binding) {
253 a = a->next_sibling;
254 } else {
255 if (a->chain == b->chain) {
256 if (!a->chain)
257 return a->id; // found it! (return the actual id, not the search's)
258 } else
259 return -2; // the chain status' don't match (conflict!)
260 b = b->first_child;
261 a = a->first_child;
262 }
263 }
264 return -1; // it just isn't in here
265 }
266
267 bool OBBindings::add_key(const StringVect &keylist, int id)
268 {
269 BindingTree *tree;
270
271 if (!(tree = buildtree(keylist, id)))
272 return false; // invalid binding requested
273
274 if (find_key(tree) != -1) {
275 // conflicts with another binding
276 destroytree(tree);
277 return false;
278 }
279
280 // assimilate this built tree into the main tree
281 assimilate(tree); // assimilation destroys/uses the tree
282 return true;
283 }
284
285
286 int OBBindings::find_key(const StringVect &keylist)
287 {
288 BindingTree *tree;
289 bool ret;
290
291 if (!(tree = buildtree(keylist, 0)))
292 return false; // invalid binding requested
293
294 ret = find_key(tree) >= 0;
295
296 destroytree(tree);
297
298 return ret;
299 }
300
301
302 int OBBindings::remove_key(const StringVect &keylist)
303 {
304 (void)keylist;
305 assert(false); // XXX: function not implemented yet
306 }
307
308
309 static void remove_branch(BindingTree *first)
310 {
311 BindingTree *p = first;
312
313 while (p) {
314 if (p->first_child)
315 remove_branch(p->first_child);
316 BindingTree *s = p->next_sibling;
317 delete p;
318 p = s;
319 }
320 }
321
322
323 void OBBindings::remove_all()
324 {
325 if (_keytree.first_child) {
326 remove_branch(_keytree.first_child);
327 _keytree.first_child = 0;
328 }
329 BindingTree *p = _mousetree;
330 while (p) {
331 BindingTree *n = p->next_sibling;
332 delete p;
333 p = n;
334 }
335 _mousetree = 0;
336 }
337
338
339 void OBBindings::process(unsigned int modifiers, unsigned int key)
340 {
341 BindingTree *c = _curpos->first_child;
342
343 while (c) {
344 if (c->binding.key == key && c->binding.modifiers == modifiers) {
345 _curpos = c;
346 break;
347 }
348 }
349 if (c) {
350 if (!_curpos->chain) {
351 // XXX execute command for _curpos->id
352 _curpos = &_keytree; // back to the start
353 }
354 }
355 }
356
357 }
This page took 0.053884 seconds and 4 git commands to generate.