1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
8 #include "otk/display.hh"
17 static void print_branch(BindingTree *first, std::string str)
19 BindingTree *p = first;
23 print_branch(p->first_child, str + " " + p->text);
25 printf("%d%s\n", p->id, (str + " " + p->text).c_str());
26 BindingTree *s = p->next_sibling;
33 void OBBindings::display()
35 if (_tree.first_child)
36 print_branch(_tree.first_child, "");
41 bool OBBindings::translate(const std::string &str, Binding &b)
43 unsigned int mods = 0;
44 std::string modstring;
46 // parse out the base key name
47 std::string::size_type keybegin = str.find_last_of('-');
48 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
49 std::string key(str, keybegin);
51 // XXX: get some modifiers up in the hizzie
52 // parse out the requested modifier keys
53 std::string::size_type begin = 0, end;
54 while (begin != keybegin) {
55 end = str.find_first_of('-', begin);
57 std::string mod(str, begin, end-begin);
60 if (mod == "C") { // control
62 } else if (mod == "S") { // shift
64 } else if (mod == "A" || // alt/mod1
69 } else if (mod == "M2" || // mod2
72 } else if (mod == "M3" || // mod3
75 } else if (mod == "W" || // windows/mod4
79 } else if (mod == "M5" || // mod5
88 printf("got modifier: %s\n", modstring.c_str());
90 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
91 if (sym == NoSymbol) return false;
93 b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
97 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id)
99 if (keylist.empty()) return 0; // nothing in the list.. return 0
101 BindingTree *ret = new BindingTree(id), *p = 0;
103 StringVect::const_iterator it, end = keylist.end();
104 for (it = keylist.begin(); it != end; ++it) {
106 p = p->first_child = new BindingTree(id);
108 p = ret; // the first node
110 if (!translate(*it, p->binding))
115 // build failed.. clean up and return 0
117 while (p->first_child) {
118 BindingTree *c = p->first_child;
125 // set the proper chain status on the last node
129 printf("BUILDING:\n");
130 print_branch(ret, "");
132 // successfully built a tree
136 static void destroytree(BindingTree *tree)
139 BindingTree *c = tree->first_child;
145 OBBindings::OBBindings()
150 OBBindings::~OBBindings()
156 void OBBindings::assimilate(BindingTree *node)
158 BindingTree *a, *b, *tmp, *last;
160 if (!_tree.first_child) {
161 // there are no nodes at this level yet
162 _tree.first_child = node;
165 a = _tree.first_child;
168 while (a->first_child) {
170 if (a->binding != b->binding) {
179 last->first_child = b->first_child;
185 int OBBindings::find(BindingTree *search) {
187 a = _tree.first_child;
190 if (a->binding != b->binding) {
193 if (a->chain == b->chain) {
195 return a->id; // found it! (return the actual id, not the search's)
197 return -2; // the chain status' don't match (conflict!)
202 return -1; // it just isn't in here
206 static int find(BindingTree *parent, BindingTree *node) {
207 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
209 if (!parent->first_child)
212 p = parent->first_child;
214 if (node->binding == p->binding) {
215 if (node->chain == p->chain) {
217 return p->id; // found it! (return the actual id, not the search's)
219 break; // go on to the next child in the chain
222 return -2; // the chain status' don't match (conflict!)
227 if (!p) return -1; // doesn't exist
230 assert(node->first_child);
231 return find(p, node->first_child);
233 return -1; // it just isnt in here
237 bool OBBindings::add(const StringVect &keylist, int id)
241 if (!(tree = buildtree(keylist, id)))
242 return false; // invalid binding requested
244 if (find(tree) < -1) {
245 // conflicts with another binding
250 // assimilate this built tree into the main tree
251 assimilate(tree); // assimilation destroys/uses the tree
256 int OBBindings::find(const StringVect &keylist)
261 if (!(tree = buildtree(keylist, 0)))
262 return false; // invalid binding requested
264 ret = find(tree) >= 0;
272 int OBBindings::remove(const StringVect &keylist)
275 assert(false); // XXX: function not implemented yet
279 static void remove_branch(BindingTree *first)
281 BindingTree *p = first;
285 remove_branch(p->first_child);
286 BindingTree *s = p->next_sibling;
293 void OBBindings::remove_all()
295 if (_tree.first_child)
296 remove_branch(_tree.first_child);