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 *p, *lastsib, *nextparent, *nextnode = node->first_child;
118 if (!parent->first_child) {
119 // there are no nodes at this level yet
120 parent->first_child = node;
123 p = lastsib = parent->first_child;
125 while (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");
137 // couldn't find an existing binding, use this new one, and insert it
139 p = lastsib->next_sibling = node;
140 printf("inserting %s\n", p->text.c_str());
146 assimilate(nextparent, nextnode);
150 int OBBindings::find(BindingTree *search) {
155 if (a->binding != b->binding) {
158 if (a->chain == b->chain) {
160 return a->id; // found it! (return the actual id, not the search's)
162 return -2; // the chain status' don't match (conflict!)
167 return -1; // it just isn't in here
171 static int find(BindingTree *parent, BindingTree *node) {
172 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
174 if (!parent->first_child)
177 p = parent->first_child;
179 if (node->binding == p->binding) {
180 if (node->chain == p->chain) {
182 return p->id; // found it! (return the actual id, not the search's)
184 break; // go on to the next child in the chain
187 return -2; // the chain status' don't match (conflict!)
192 if (!p) return -1; // doesn't exist
195 assert(node->first_child);
196 return find(p, node->first_child);
198 return -1; // it just isnt in here
202 bool OBBindings::add(const StringVect &keylist, int id)
206 if (!(tree = buildtree(keylist, id)))
207 return false; // invalid binding requested
209 if (find_bind(_tree.first_child, tree) < -1) {
210 // conflicts with another binding
215 // assimilate this built tree into the main tree
216 assimilate(&_tree, tree); // assimilation destroys/uses the tree
221 int OBBindings::find(const StringVect &keylist)
226 if (!(tree = buildtree(keylist, 0)))
227 return false; // invalid binding requested
229 ret = find_bind(_tree.first_child, tree) >= 0;
237 int OBBindings::remove(const StringVect &keylist)
240 assert(false); // XXX: function not implemented yet
244 static void remove_branch(BindingTree *first)
246 BindingTree *p = first;
250 remove_branch(p->first_child);
251 BindingTree *s = p->next_sibling;
258 void OBBindings::remove_all()
260 if (_tree.first_child)
261 remove_branch(_tree.first_child);