]> icculus.org git repositories - dana/openbox.git/blob - src/bindings.cc
make some static functions members of OBBindings
[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
14 namespace ob {
15
16 #include <stdio.h>
17 static void print_branch(BindingTree *first, std::string str)
18 {
19   BindingTree *p = first;
20   
21   while (p) {
22     if (p->first_child)
23       print_branch(p->first_child, str + " " + p->text);
24     if (!p->chain)
25       printf("%d%s\n", p->id, (str + " " + p->text).c_str());
26     BindingTree *s = p->next_sibling;
27     delete p;
28     p = s;
29   }
30 }
31
32
33 void OBBindings::display()
34 {
35   if (_tree.first_child)
36     print_branch(_tree.first_child, "");
37 }
38
39
40
41 bool OBBindings::translate(const std::string &str, Binding &b)
42 {
43   std::string::size_type keybegin = str.find_last_of('-');
44   std::string key(str, keybegin != std::string::npos ? keybegin + 1 : 0);
45
46   // XXX: get some modifiers up in the hizzie
47   
48   KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
49   if (sym == NoSymbol) return false;
50   b.modifiers = Mod1Mask; // XXX: no way
51   b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
52   return b.key != 0;
53 }
54
55 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id)
56 {
57   if (keylist.empty()) return 0; // nothing in the list.. return 0
58
59   BindingTree *ret = new BindingTree(id), *p = 0;
60
61   StringVect::const_iterator it, end = keylist.end();
62   for (it = keylist.begin(); it != end; ++it) {
63     if (p)
64       p = p->first_child = new BindingTree(id);
65     else
66       p = ret; // the first node
67     
68     if (!translate(*it, p->binding))
69       break;
70     p->text = *it;
71   }
72   if (it != end) {
73     // build failed.. clean up and return 0
74     p = ret;
75     while (p->first_child) {
76       BindingTree *c = p->first_child;
77       delete p;
78       p = c;      
79     }
80     delete p;
81     return 0;
82   } else {
83     // set the proper chain status on the last node
84     p->chain = false;
85   }
86
87   printf("BUILDING:\n");
88   print_branch(ret, "");
89   
90   // successfully built a tree
91   return ret;
92 }
93
94 static void destroytree(BindingTree *tree)
95 {
96   while (tree) {
97     BindingTree *c = tree->first_child;
98     delete tree;
99     tree = c;
100   }
101 }
102
103 OBBindings::OBBindings()
104 {
105 }
106
107
108 OBBindings::~OBBindings()
109 {
110   remove_all();
111 }
112
113
114 static void assimilate(BindingTree *parent, BindingTree *node)
115 {
116   BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
117
118   if (!parent->first_child) {
119     // there are no nodes at this level yet
120     parent->first_child = node;
121     nextparent = node;
122   } else {
123     p = lastsib = parent->first_child;
124
125     while (p->next_sibling) {
126       p = p->next_sibling;
127       lastsib = p; // finds the last sibling
128       if (p->binding == node->binding) {
129         // found an identical binding..
130         assert(node->chain && p->chain);
131         delete node; // kill the one we aren't using
132         printf("using existing node\n");
133         break;
134       }
135     }
136     if (!p) {
137       // couldn't find an existing binding, use this new one, and insert it
138       // into the list
139       p = lastsib->next_sibling = node;
140       printf("inserting %s\n", p->text.c_str());
141     }
142     nextparent = p;
143   }
144
145   if (nextnode)
146     assimilate(nextparent, nextnode);
147 }
148
149
150 int OBBindings::find(BindingTree *search) {
151   BindingTree *a, *b;
152   a = _tree;
153   b = search;
154   while (a && b) {
155     if (a->binding != b->binding) {
156       a = a->next_sibling;
157     } else {
158       if (a->chain == b->chain) {
159         if (!a->chain)
160           return a->id; // found it! (return the actual id, not the search's)
161       } else
162           return -2; // the chain status' don't match (conflict!)
163       b = b->first_child;
164       a = a->first_child;
165     }
166   }
167   return -1; // it just isn't in here
168 }
169
170 /*
171 static int find(BindingTree *parent, BindingTree *node) {
172   BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
173
174   if (!parent->first_child)
175     return -1;
176
177   p = parent->first_child;
178   while (p) {
179     if (node->binding == p->binding) {
180       if (node->chain == p->chain) {
181         if (!node->chain) {
182           return p->id; // found it! (return the actual id, not the search's)
183         } else {
184           break; // go on to the next child in the chain
185         }
186       } else {
187         return -2; // the chain status' don't match (conflict!)
188       }
189     }
190     p = p->next_sibling;
191   }
192   if (!p) return -1; // doesn't exist
193
194   if (node->chain) {
195     assert(node->first_child);
196     return find(p, node->first_child);
197   } else
198     return -1; // it just isnt in here
199 }
200 */
201
202 bool OBBindings::add(const StringVect &keylist, int id)
203 {
204   BindingTree *tree;
205
206   if (!(tree = buildtree(keylist, id)))
207     return false; // invalid binding requested
208
209   if (find_bind(_tree.first_child, tree) < -1) {
210     // conflicts with another binding
211     destroytree(tree);
212     return false;
213   }
214
215   // assimilate this built tree into the main tree
216   assimilate(&_tree, tree); // assimilation destroys/uses the tree
217   return true;
218 }
219
220
221 int OBBindings::find(const StringVect &keylist)
222 {
223   BindingTree *tree;
224   bool ret;
225
226   if (!(tree = buildtree(keylist, 0)))
227     return false; // invalid binding requested
228
229   ret = find_bind(_tree.first_child, tree) >= 0;
230
231   destroytree(tree);
232
233   return ret;
234 }
235
236
237 int OBBindings::remove(const StringVect &keylist)
238 {
239   (void)keylist;
240   assert(false); // XXX: function not implemented yet
241 }
242
243
244 static void remove_branch(BindingTree *first)
245 {
246   BindingTree *p = first;
247
248   while (p) {
249     if (p->first_child)
250       remove_branch(p->first_child);
251     BindingTree *s = p->next_sibling;
252     delete p;
253     p = s;
254   }
255 }
256
257
258 void OBBindings::remove_all()
259 {
260   if (_tree.first_child)
261     remove_branch(_tree.first_child);
262 }
263
264 }