]> Dogcows Code - chaz/openbox/blob - src/bindings.cc
can build trees with modifiers
[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 printf("%d%s\n", p->id, (str + " " + p->text).c_str());
25 BindingTree *s = p->next_sibling;
26 delete p;
27 p = s;
28 }
29 }
30
31
32 void OBBindings::display()
33 {
34 if (_bindings.first_child)
35 print_branch(_bindings.first_child, "");
36 }
37
38
39
40 static bool translate(const std::string str, Binding &b)
41 {
42 std::string::size_type keybegin = str.find_last_of('-');
43 std::string key(str, keybegin != std::string::npos ? keybegin + 1 : 0);
44
45 // XXX: get some modifiers up in the hizzie
46
47 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
48 if (sym == NoSymbol) return false;
49 b.modifiers = Mod1Mask; // XXX: no way
50 b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
51 return b.key != 0;
52 }
53
54 static BindingTree *buildtree(const OBBindings::StringVect &keylist, int id)
55 {
56 if (keylist.empty()) return 0; // nothing in the list.. return 0
57
58 BindingTree *ret = new BindingTree(id), *p = 0;
59
60 OBBindings::StringVect::const_iterator it, end = keylist.end();
61 for (it = keylist.begin(); it != end; ++it) {
62 if (p)
63 p = p->first_child = new BindingTree(id);
64 else
65 p = ret; // the first node
66
67 if (!translate(*it, p->binding))
68 break;
69 p->text = *it;
70 }
71 if (it != end) {
72 // build failed.. clean up and return 0
73 p = ret;
74 while (p->first_child) {
75 BindingTree *c = p->first_child;
76 delete p;
77 p = c;
78 }
79 delete p;
80 return 0;
81 } else {
82 // set the proper chain status on the last node
83 p->chain = false;
84 }
85
86 printf("<BUILDING>\n");
87 print_branch(ret, "");
88 printf("</BUILDING>\n");
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 break;
133 }
134 }
135 if (!p) {
136 // couldn't find an existing binding, use this new one, and insert it
137 // into the list
138 p = lastsib->next_sibling = node;
139 }
140 nextparent = p;
141 }
142
143 if (nextnode)
144 assimilate(nextparent, nextnode);
145 }
146
147
148 static int find_bind(BindingTree *tree, BindingTree *search) {
149 BindingTree *a, *b;
150 a = tree;
151 b = search;
152 while (a && b) {
153 if (a->binding != b->binding) {
154 a = a->next_sibling;
155 } else {
156 if (a->chain == b->chain) {
157 if (!a->chain)
158 return a->id; // found it! (return the actual id, not the search's)
159 } else
160 return -2; // the chain status' don't match (conflict!)
161 b = b->first_child;
162 a = a->first_child;
163 }
164 }
165 return -1; // it just isn't in here
166 }
167
168 /*
169 static int find(BindingTree *parent, BindingTree *node) {
170 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
171
172 if (!parent->first_child)
173 return -1;
174
175 p = parent->first_child;
176 while (p) {
177 if (node->binding == p->binding) {
178 if (node->chain == p->chain) {
179 if (!node->chain) {
180 return p->id; // found it! (return the actual id, not the search's)
181 } else {
182 break; // go on to the next child in the chain
183 }
184 } else {
185 return -2; // the chain status' don't match (conflict!)
186 }
187 }
188 p = p->next_sibling;
189 }
190 if (!p) return -1; // doesn't exist
191
192 if (node->chain) {
193 assert(node->first_child);
194 return find(p, node->first_child);
195 } else
196 return -1; // it just isnt in here
197 }
198 */
199
200 bool OBBindings::add(const StringVect &keylist, int id)
201 {
202 BindingTree *tree;
203
204 if (!(tree = buildtree(keylist, id)))
205 return false; // invalid binding requested
206
207 if (find_bind(_bindings.first_child, tree) < -1) {
208 // conflicts with another binding
209 destroytree(tree);
210 return false;
211 }
212
213 // assimilate this built tree into the main tree
214 assimilate(&_bindings, tree); // assimilation destroys/uses the tree
215 return true;
216 }
217
218
219 int OBBindings::find(const StringVect &keylist)
220 {
221 BindingTree *tree;
222 bool ret;
223
224 if (!(tree = buildtree(keylist, 0)))
225 return false; // invalid binding requested
226
227 ret = find_bind(_bindings.first_child, tree) >= 0;
228
229 destroytree(tree);
230
231 return ret;
232 }
233
234
235 int OBBindings::remove(const StringVect &keylist)
236 {
237 (void)keylist;
238 assert(false); // XXX: function not implemented yet
239 }
240
241
242 static void remove_branch(BindingTree *first)
243 {
244 BindingTree *p = first;
245
246 while (p) {
247 if (p->first_child)
248 remove_branch(p->first_child);
249 BindingTree *s = p->next_sibling;
250 delete p;
251 p = s;
252 }
253 }
254
255
256 void OBBindings::remove_all()
257 {
258 if (_bindings.first_child)
259 remove_branch(_bindings.first_child);
260 }
261
262 }
This page took 0.049021 seconds and 5 git commands to generate.