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