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);
24 printf("%d%s\n", p->id, (str + " " + p->text).c_str());
25 BindingTree *s = p->next_sibling;
32 void OBBindings::display()
34 if (_bindings.first_child)
35 print_branch(_bindings.first_child, "");
40 static bool translate(const std::string str, Binding &b)
42 std::string::size_type keybegin = str.find_last_of('-');
43 std::string key(str, keybegin != std::string::npos ? keybegin + 1 : 0);
45 // XXX: get some modifiers up in the hizzie
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);
54 static BindingTree *buildtree(const OBBindings::StringVect &keylist, int id)
56 if (keylist.empty()) return 0; // nothing in the list.. return 0
58 BindingTree *ret = new BindingTree(id), *p = 0;
60 OBBindings::StringVect::const_iterator it, end = keylist.end();
61 for (it = keylist.begin(); it != end; ++it) {
63 p = p->first_child = new BindingTree(id);
65 p = ret; // the first node
67 if (!translate(*it, p->binding))
72 // build failed.. clean up and return 0
74 while (p->first_child) {
75 BindingTree *c = p->first_child;
82 // set the proper chain status on the last node
86 printf("<BUILDING>\n");
87 print_branch(ret, "");
88 printf("</BUILDING>\n");
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
136 // couldn't find an existing binding, use this new one, and insert it
138 p = lastsib->next_sibling = node;
144 assimilate(nextparent, nextnode);
148 static int find_bind(BindingTree *tree, BindingTree *search) {
153 if (a->binding != b->binding) {
156 if (a->chain == b->chain) {
158 return a->id; // found it! (return the actual id, not the search's)
160 return -2; // the chain status' don't match (conflict!)
165 return -1; // it just isn't in here
169 static int find(BindingTree *parent, BindingTree *node) {
170 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
172 if (!parent->first_child)
175 p = parent->first_child;
177 if (node->binding == p->binding) {
178 if (node->chain == p->chain) {
180 return p->id; // found it! (return the actual id, not the search's)
182 break; // go on to the next child in the chain
185 return -2; // the chain status' don't match (conflict!)
190 if (!p) return -1; // doesn't exist
193 assert(node->first_child);
194 return find(p, node->first_child);
196 return -1; // it just isnt in here
200 bool OBBindings::add(const StringVect &keylist, int id)
204 if (!(tree = buildtree(keylist, id)))
205 return false; // invalid binding requested
207 if (find_bind(_bindings.first_child, tree) < -1) {
208 // conflicts with another binding
213 // assimilate this built tree into the main tree
214 assimilate(&_bindings, tree); // assimilation destroys/uses the tree
219 int OBBindings::find(const StringVect &keylist)
224 if (!(tree = buildtree(keylist, 0)))
225 return false; // invalid binding requested
227 ret = find_bind(_bindings.first_child, tree) >= 0;
235 int OBBindings::remove(const StringVect &keylist)
238 assert(false); // XXX: function not implemented yet
242 static void remove_branch(BindingTree *first)
244 BindingTree *p = first;
248 remove_branch(p->first_child);
249 BindingTree *s = p->next_sibling;
256 void OBBindings::remove_all()
258 if (_bindings.first_child)
259 remove_branch(_bindings.first_child);