]> Dogcows Code - chaz/openbox/blob - src/bindings.cc
Trying to make an iterative assimilate()
[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
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 *a, *b, *tmp, *last;
117
118 if (!parent->first_child) {
119 // there are no nodes at this level yet
120 parent->first_child = node;
121 return;
122 } else {
123 a = parent->first_child;
124 last = a;
125 b = node;
126 while (a->first_child) {
127 last = a;
128 if (a->binding != b->binding) {
129 a = a->next_sibling;
130 } else {
131 tmp = b;
132 b = b->first_child;
133 delete tmp;
134 a = a->first_child;
135 }
136 }
137 last->first_child = b->first_child;
138 delete b;
139 }
140 }
141
142
143 int OBBindings::find(BindingTree *search) {
144 BindingTree *a, *b;
145 a = _tree.first_child;
146 b = search;
147 while (a && b) {
148 if (a->binding != b->binding) {
149 a = a->next_sibling;
150 } else {
151 if (a->chain == b->chain) {
152 if (!a->chain)
153 return a->id; // found it! (return the actual id, not the search's)
154 } else
155 return -2; // the chain status' don't match (conflict!)
156 b = b->first_child;
157 a = a->first_child;
158 }
159 }
160 return -1; // it just isn't in here
161 }
162
163 /*
164 static int find(BindingTree *parent, BindingTree *node) {
165 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
166
167 if (!parent->first_child)
168 return -1;
169
170 p = parent->first_child;
171 while (p) {
172 if (node->binding == p->binding) {
173 if (node->chain == p->chain) {
174 if (!node->chain) {
175 return p->id; // found it! (return the actual id, not the search's)
176 } else {
177 break; // go on to the next child in the chain
178 }
179 } else {
180 return -2; // the chain status' don't match (conflict!)
181 }
182 }
183 p = p->next_sibling;
184 }
185 if (!p) return -1; // doesn't exist
186
187 if (node->chain) {
188 assert(node->first_child);
189 return find(p, node->first_child);
190 } else
191 return -1; // it just isnt in here
192 }
193 */
194
195 bool OBBindings::add(const StringVect &keylist, int id)
196 {
197 BindingTree *tree;
198
199 if (!(tree = buildtree(keylist, id)))
200 return false; // invalid binding requested
201
202 if (find(tree) < -1) {
203 // conflicts with another binding
204 destroytree(tree);
205 return false;
206 }
207
208 // assimilate this built tree into the main tree
209 assimilate(&_tree, tree); // assimilation destroys/uses the tree
210 return true;
211 }
212
213
214 int OBBindings::find(const StringVect &keylist)
215 {
216 BindingTree *tree;
217 bool ret;
218
219 if (!(tree = buildtree(keylist, 0)))
220 return false; // invalid binding requested
221
222 ret = find(tree) >= 0;
223
224 destroytree(tree);
225
226 return ret;
227 }
228
229
230 int OBBindings::remove(const StringVect &keylist)
231 {
232 (void)keylist;
233 assert(false); // XXX: function not implemented yet
234 }
235
236
237 static void remove_branch(BindingTree *first)
238 {
239 BindingTree *p = first;
240
241 while (p) {
242 if (p->first_child)
243 remove_branch(p->first_child);
244 BindingTree *s = p->next_sibling;
245 delete p;
246 p = s;
247 }
248 }
249
250
251 void OBBindings::remove_all()
252 {
253 if (_tree.first_child)
254 remove_branch(_tree.first_child);
255 }
256
257 }
This page took 0.049292 seconds and 5 git commands to generate.