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
86 printf("got modifier: %s\n", modstring.c_str());
88 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
89 if (sym == NoSymbol) return false;
91 b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
95 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id)
97 if (keylist.empty()) return 0; // nothing in the list.. return 0
99 BindingTree *ret = new BindingTree(id), *p = 0;
101 StringVect::const_iterator it, end = keylist.end();
102 for (it = keylist.begin(); it != end; ++it) {
104 p = p->first_child = new BindingTree(id);
106 p = ret; // the first node
108 if (!translate(*it, p->binding))
113 // build failed.. clean up and return 0
115 while (p->first_child) {
116 BindingTree *c = p->first_child;
123 // set the proper chain status on the last node
127 printf("BUILDING:\n");
128 print_branch(ret, "");
130 // successfully built a tree
134 static void destroytree(BindingTree *tree)
137 BindingTree *c = tree->first_child;
143 OBBindings::OBBindings()
148 OBBindings::~OBBindings()
154 void OBBindings::assimilate(BindingTree *node)
156 BindingTree *a, *b, *tmp, *last;
158 if (!_tree.first_child) {
159 // there are no nodes at this level yet
160 _tree.first_child = node;
163 a = _tree.first_child;
166 while (a->first_child) {
168 if (a->binding != b->binding) {
177 last->first_child = b->first_child;
183 int OBBindings::find(BindingTree *search) {
185 a = _tree.first_child;
188 if (a->binding != b->binding) {
191 if (a->chain == b->chain) {
193 return a->id; // found it! (return the actual id, not the search's)
195 return -2; // the chain status' don't match (conflict!)
200 return -1; // it just isn't in here
204 static int find(BindingTree *parent, BindingTree *node) {
205 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
207 if (!parent->first_child)
210 p = parent->first_child;
212 if (node->binding == p->binding) {
213 if (node->chain == p->chain) {
215 return p->id; // found it! (return the actual id, not the search's)
217 break; // go on to the next child in the chain
220 return -2; // the chain status' don't match (conflict!)
225 if (!p) return -1; // doesn't exist
228 assert(node->first_child);
229 return find(p, node->first_child);
231 return -1; // it just isnt in here
235 bool OBBindings::add(const StringVect &keylist, int id)
239 if (!(tree = buildtree(keylist, id)))
240 return false; // invalid binding requested
242 if (find(tree) < -1) {
243 // conflicts with another binding
248 // assimilate this built tree into the main tree
249 assimilate(tree); // assimilation destroys/uses the tree
254 int OBBindings::find(const StringVect &keylist)
259 if (!(tree = buildtree(keylist, 0)))
260 return false; // invalid binding requested
262 ret = find(tree) >= 0;
270 int OBBindings::remove(const StringVect &keylist)
273 assert(false); // XXX: function not implemented yet
277 static void remove_branch(BindingTree *first)
279 BindingTree *p = first;
283 remove_branch(p->first_child);
284 BindingTree *s = p->next_sibling;
291 void OBBindings::remove_all()
293 if (_tree.first_child)
294 remove_branch(_tree.first_child);