]> Dogcows Code - chaz/openbox/blob - src/bindings.cc
5ab5f66de45e2ba265a71c7d955b6566496c27a0
[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(const BindingTree *first, std::string str)
21 {
22 const 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,
99 bool askey) const
100 {
101 // parse out the base key name
102 std::string::size_type keybegin = str.find_last_of('-');
103 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
104 std::string key(str, keybegin);
105
106 // parse out the requested modifier keys
107 unsigned int modval = 0;
108 std::string::size_type begin = 0, end;
109 while (begin != keybegin) {
110 end = str.find_first_of('-', begin);
111
112 std::string mod(str, begin, end-begin);
113 if (!modvalue(mod, &modval)) {
114 // printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
115 return false;
116 }
117
118 begin = end + 1;
119 }
120
121 // set the binding
122 b.modifiers = modval;
123 if (askey) {
124 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
125 if (sym == NoSymbol) return false;
126 b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
127 return b.key != 0;
128 } else {
129 return buttonvalue(key, &b.key);
130 }
131 }
132
133 static void destroytree(BindingTree *tree)
134 {
135 while (tree) {
136 BindingTree *c = tree->first_child;
137 delete tree;
138 tree = c;
139 }
140 }
141
142 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id) const
143 {
144 if (keylist.empty()) return 0; // nothing in the list.. return 0
145
146 BindingTree *ret = 0, *p;
147
148 StringVect::const_reverse_iterator it, end = keylist.rend();
149 for (it = keylist.rbegin(); it != end; ++it) {
150 p = ret;
151 ret = new BindingTree(id);
152 if (!p) ret->chain = false; // only the first built node
153 ret->first_child = p;
154 if (!translate(*it, ret->binding, true)) {
155 destroytree(ret);
156 ret = 0;
157 break;
158 }
159 ret->text = *it; // XXX: rm me
160 }
161 return ret;
162 }
163
164
165 OBBindings::OBBindings()
166 : _curpos(&_keytree), _mousetree(0)
167 {
168 }
169
170
171 OBBindings::~OBBindings()
172 {
173 remove_all();
174 }
175
176
177 bool OBBindings::add_mouse(const std::string &button, int id)
178 {
179 BindingTree n;
180
181 if (!translate(button, n.binding, false))
182 return false;
183
184 BindingTree *p = _mousetree, **newp = &_mousetree;
185 while (p) {
186 if (p->binding == n.binding)
187 return false; // conflict
188 p = p->next_sibling;
189 newp = &p->next_sibling;
190 }
191 display();
192 *newp = new BindingTree(id);
193 display();
194 (*newp)->text = button;
195 (*newp)->chain = false;
196 (*newp)->binding.key = n.binding.key;
197 (*newp)->binding.modifiers = n.binding.modifiers;
198
199 return true;
200 }
201
202
203 int OBBindings::remove_mouse(const std::string &button)
204 {
205 (void)button;
206 assert(false); // XXX: function not implemented yet
207 }
208
209
210 void OBBindings::assimilate(BindingTree *node)
211 {
212 BindingTree *a, *b, *tmp, *last;
213
214 printf("node=%lx\n", (long)node);
215 if (!_keytree.first_child) {
216 // there are no nodes at this level yet
217 _keytree.first_child = node;
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 tmp = b;
229 b = b->first_child;
230 delete tmp;
231 a = a->first_child;
232 }
233 }
234 printf("after while.. b=%lx\n", (long)b);
235 if (last->binding != b->binding)
236 last->next_sibling = b;
237 else
238 last->first_child = b->first_child;
239 delete b;
240 }
241 }
242
243
244 int OBBindings::find_key(BindingTree *search) const {
245 BindingTree *a, *b;
246 print_branch(&_keytree, " Searching:");
247 print_branch(search, " for...");
248 a = _keytree.first_child;
249 b = search;
250 while (a && b) {
251 if (a->binding != b->binding) {
252 a = a->next_sibling;
253 } else {
254 if (a->chain == b->chain) {
255 if (!a->chain) {
256 printf("Match found with %s\n", a->text.c_str());
257 return a->id; // found it! (return the actual id, not the search's)
258 }
259 } else {
260 printf("Conflict found with %s\n", a->text.c_str());
261 return -2; // the chain status' don't match (conflict!)
262 }
263 b = b->first_child;
264 a = a->first_child;
265 }
266 }
267 return -1; // it just isn't in here
268 }
269
270 bool OBBindings::add_key(const StringVect &keylist, int id)
271 {
272 BindingTree *tree;
273
274 if (!(tree = buildtree(keylist, id)))
275 return false; // invalid binding requested
276
277 print_branch(tree, " Adding: ");
278
279 if (find_key(tree) != -1) {
280 // conflicts with another binding
281 printf("Conflict\n");
282 destroytree(tree);
283 return false;
284 }
285
286 // assimilate this built tree into the main tree
287 assimilate(tree); // assimilation destroys/uses the tree
288
289 printf("Added!\n");
290 print_branch(&_keytree, "");
291 printf("\n");
292
293 return true;
294 }
295
296
297 int OBBindings::find_key(const StringVect &keylist)
298 {
299 BindingTree *tree;
300 bool ret;
301
302 if (!(tree = buildtree(keylist, 0)))
303 return false; // invalid binding requested
304
305 ret = find_key(tree) >= 0;
306
307 destroytree(tree);
308
309 return ret;
310 }
311
312
313 int OBBindings::remove_key(const StringVect &keylist)
314 {
315 (void)keylist;
316 assert(false); // XXX: function not implemented yet
317 }
318
319
320 static void remove_branch(BindingTree *first)
321 {
322 BindingTree *p = first;
323
324 while (p) {
325 if (p->first_child)
326 remove_branch(p->first_child);
327 BindingTree *s = p->next_sibling;
328 delete p;
329 p = s;
330 }
331 }
332
333
334 void OBBindings::remove_all()
335 {
336 if (_keytree.first_child) {
337 remove_branch(_keytree.first_child);
338 _keytree.first_child = 0;
339 }
340 BindingTree *p = _mousetree;
341 while (p) {
342 BindingTree *n = p->next_sibling;
343 delete p;
344 p = n;
345 }
346 _mousetree = 0;
347 }
348
349
350 void OBBindings::process(unsigned int modifiers, unsigned int key)
351 {
352 BindingTree *c = _curpos->first_child;
353
354 while (c) {
355 if (c->binding.key == key && c->binding.modifiers == modifiers) {
356 _curpos = c;
357 break;
358 }
359 }
360 if (c) {
361 if (!_curpos->chain) {
362 // XXX execute command for _curpos->id
363 _curpos = &_keytree; // back to the start
364 }
365 }
366 }
367
368 }
This page took 0.055603 seconds and 4 git commands to generate.