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