]> Dogcows Code - chaz/openbox/blob - src/bindings.cc
nicer translate()
[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 (_tree.first_child)
37 print_branch(_tree.first_child, "");
38 }
39
40
41 static bool modvalue(const std::string &mod, unsigned int *val)
42 {
43 if (mod == "C") { // control
44 *val |= ControlMask;
45 } else if (mod == "S") { // shift
46 *val |= ShiftMask;
47 } else if (mod == "A" || // alt/mod1
48 mod == "M" ||
49 mod == "M1" ||
50 mod == "Mod1") {
51 *val |= Mod1Mask;
52 } else if (mod == "M2" || // mod2
53 mod == "Mod2") {
54 *val |= Mod2Mask;
55 } else if (mod == "M3" || // mod3
56 mod == "Mod3") {
57 *val |= Mod3Mask;
58 } else if (mod == "W" || // windows/mod4
59 mod == "M4" ||
60 mod == "Mod4") {
61 *val |= Mod4Mask;
62 } else if (mod == "M5" || // mod5
63 mod == "Mod5") {
64 *val |= Mod5Mask;
65 } else { // invalid
66 return false;
67 }
68 return true;
69 }
70
71 bool OBBindings::translate(const std::string &str, Binding &b)
72 {
73 // parse out the base key name
74 std::string::size_type keybegin = str.find_last_of('-');
75 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
76 std::string key(str, keybegin);
77
78 // parse out the requested modifier keys
79 unsigned int modval = 0;
80 std::string::size_type begin = 0, end;
81 while (begin != keybegin) {
82 end = str.find_first_of('-', begin);
83
84 std::string mod(str, begin, end-begin);
85 if (!modvalue(mod, &modval)) {
86 printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
87 return false;
88 }
89
90 begin = end + 1;
91 }
92
93 // set the binding
94 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
95 if (sym == NoSymbol) return false;
96 b.modifiers = modval;
97 b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
98 return b.key != 0;
99 }
100
101 static void destroytree(BindingTree *tree)
102 {
103 while (tree) {
104 BindingTree *c = tree->first_child;
105 delete tree;
106 tree = c;
107 }
108 }
109
110 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id)
111 {
112 if (keylist.empty()) return 0; // nothing in the list.. return 0
113
114 BindingTree *ret = 0, *p;
115
116 StringVect::const_reverse_iterator it, end = keylist.rend();
117 for (it = keylist.rbegin(); it != end; ++it) {
118 p = ret;
119 ret = new BindingTree(id);
120 if (!p) ret->chain = false;
121 ret->first_child = p;
122 if (!translate(*it, ret->binding)) {
123 destroytree(ret);
124 ret = 0;
125 break;
126 }
127 ret->text = *it; // XXX: rm me
128 }
129 return ret;
130 }
131
132
133 OBBindings::OBBindings()
134 {
135 }
136
137
138 OBBindings::~OBBindings()
139 {
140 remove_all();
141 }
142
143
144 void OBBindings::assimilate(BindingTree *node)
145 {
146 BindingTree *a, *b, *tmp, *last;
147
148 if (!_tree.first_child) {
149 // there are no nodes at this level yet
150 _tree.first_child = node;
151 return;
152 } else {
153 a = _tree.first_child;
154 last = a;
155 b = node;
156 while (a) {
157 last = a;
158 if (a->binding != b->binding) {
159 a = a->next_sibling;
160 } else {
161 tmp = b;
162 b = b->first_child;
163 delete tmp;
164 a = a->first_child;
165 }
166 }
167 if (last->binding != b->binding)
168 last->next_sibling = b;
169 else
170 last->first_child = b->first_child;
171 delete b;
172 }
173 }
174
175
176 int OBBindings::find(BindingTree *search) {
177 BindingTree *a, *b;
178 a = _tree.first_child;
179 b = search;
180 while (a && b) {
181 if (a->binding != b->binding) {
182 a = a->next_sibling;
183 } else {
184 if (a->chain == b->chain) {
185 if (!a->chain)
186 return a->id; // found it! (return the actual id, not the search's)
187 } else
188 return -2; // the chain status' don't match (conflict!)
189 b = b->first_child;
190 a = a->first_child;
191 }
192 }
193 return -1; // it just isn't in here
194 }
195
196 /*
197 static int find(BindingTree *parent, BindingTree *node) {
198 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
199
200 if (!parent->first_child)
201 return -1;
202
203 p = parent->first_child;
204 while (p) {
205 if (node->binding == p->binding) {
206 if (node->chain == p->chain) {
207 if (!node->chain) {
208 return p->id; // found it! (return the actual id, not the search's)
209 } else {
210 break; // go on to the next child in the chain
211 }
212 } else {
213 return -2; // the chain status' don't match (conflict!)
214 }
215 }
216 p = p->next_sibling;
217 }
218 if (!p) return -1; // doesn't exist
219
220 if (node->chain) {
221 assert(node->first_child);
222 return find(p, node->first_child);
223 } else
224 return -1; // it just isnt in here
225 }
226 */
227
228 bool OBBindings::add(const StringVect &keylist, int id)
229 {
230 BindingTree *tree;
231
232 if (!(tree = buildtree(keylist, id)))
233 return false; // invalid binding requested
234
235 if (find(tree) < -1) {
236 // conflicts with another binding
237 destroytree(tree);
238 return false;
239 }
240
241 // assimilate this built tree into the main tree
242 assimilate(tree); // assimilation destroys/uses the tree
243 return true;
244 }
245
246
247 int OBBindings::find(const StringVect &keylist)
248 {
249 BindingTree *tree;
250 bool ret;
251
252 if (!(tree = buildtree(keylist, 0)))
253 return false; // invalid binding requested
254
255 ret = find(tree) >= 0;
256
257 destroytree(tree);
258
259 return ret;
260 }
261
262
263 int OBBindings::remove(const StringVect &keylist)
264 {
265 (void)keylist;
266 assert(false); // XXX: function not implemented yet
267 }
268
269
270 static void remove_branch(BindingTree *first)
271 {
272 BindingTree *p = first;
273
274 while (p) {
275 if (p->first_child)
276 remove_branch(p->first_child);
277 BindingTree *s = p->next_sibling;
278 delete p;
279 p = s;
280 }
281 }
282
283
284 void OBBindings::remove_all()
285 {
286 if (_tree.first_child)
287 remove_branch(_tree.first_child);
288 }
289
290 }
This page took 0.05255 seconds and 5 git commands to generate.