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 std::string::size_type keybegin = str.find_last_of('-');
44 std::string key(str, keybegin != std::string::npos ? keybegin + 1 : 0);
46 // XXX: get some modifiers up in the hizzie
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);
55 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id)
57 if (keylist.empty()) return 0; // nothing in the list.. return 0
59 BindingTree *ret = new BindingTree(id), *p = 0;
61 StringVect::const_iterator it, end = keylist.end();
62 for (it = keylist.begin(); it != end; ++it) {
64 p = p->first_child = new BindingTree(id);
66 p = ret; // the first node
68 if (!translate(*it, p->binding))
73 // build failed.. clean up and return 0
75 while (p->first_child) {
76 BindingTree *c = p->first_child;
83 // set the proper chain status on the last node
87 printf("BUILDING:\n");
88 print_branch(ret, "");
90 // successfully built a tree
94 static void destroytree(BindingTree *tree)
97 BindingTree *c = tree->first_child;
103 OBBindings::OBBindings()
108 OBBindings::~OBBindings()
114 static void assimilate(BindingTree *parent, BindingTree *node)
116 BindingTree *a, *b, *tmp, *last;
118 if (!parent->first_child) {
119 // there are no nodes at this level yet
120 parent->first_child = node;
123 a = parent->first_child;
126 while (a->first_child) {
128 if (a->binding != b->binding) {
137 last->first_child = b->first_child;
143 int OBBindings::find(BindingTree *search) {
145 a = _tree.first_child;
148 if (a->binding != b->binding) {
151 if (a->chain == b->chain) {
153 return a->id; // found it! (return the actual id, not the search's)
155 return -2; // the chain status' don't match (conflict!)
160 return -1; // it just isn't in here
164 static int find(BindingTree *parent, BindingTree *node) {
165 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
167 if (!parent->first_child)
170 p = parent->first_child;
172 if (node->binding == p->binding) {
173 if (node->chain == p->chain) {
175 return p->id; // found it! (return the actual id, not the search's)
177 break; // go on to the next child in the chain
180 return -2; // the chain status' don't match (conflict!)
185 if (!p) return -1; // doesn't exist
188 assert(node->first_child);
189 return find(p, node->first_child);
191 return -1; // it just isnt in here
195 bool OBBindings::add(const StringVect &keylist, int id)
199 if (!(tree = buildtree(keylist, id)))
200 return false; // invalid binding requested
202 if (find(tree) < -1) {
203 // conflicts with another binding
208 // assimilate this built tree into the main tree
209 assimilate(&_tree, tree); // assimilation destroys/uses the tree
214 int OBBindings::find(const StringVect &keylist)
219 if (!(tree = buildtree(keylist, 0)))
220 return false; // invalid binding requested
222 ret = find(tree) >= 0;
230 int OBBindings::remove(const StringVect &keylist)
233 assert(false); // XXX: function not implemented yet
237 static void remove_branch(BindingTree *first)
239 BindingTree *p = first;
243 remove_branch(p->first_child);
244 BindingTree *s = p->next_sibling;
251 void OBBindings::remove_all()
253 if (_tree.first_child)
254 remove_branch(_tree.first_child);