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;
45 // parse out the base key name
46 std::string::size_type keybegin = str.find_last_of('-');
47 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
48 std::string key(str, keybegin);
50 // XXX: get some modifiers up in the hizzie
51 // parse out the requested modifier keys
52 std::string::size_type begin = 0, end;
53 while (begin != keybegin) {
54 end = str.find_first_of('-', begin);
56 std::string mod(str, begin, end-begin);
58 if (mod == "C") { // control
60 } else if (mod == "S") { // shift
62 } else if (mod == "A" || // alt/mod1
67 } else if (mod == "M2" || // mod2
70 } else if (mod == "M3" || // mod3
73 } else if (mod == "W" || // windows/mod4
77 } else if (mod == "M5" || // mod5
81 printf("got modifier: got modifier: %s\n", mod.c_str());
86 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
87 if (sym == NoSymbol) return false;
89 b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
93 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id)
95 if (keylist.empty()) return 0; // nothing in the list.. return 0
97 BindingTree *ret = new BindingTree(id), *p = 0;
99 StringVect::const_iterator it, end = keylist.end();
100 for (it = keylist.begin(); it != end; ++it) {
102 p = p->first_child = new BindingTree(id);
104 p = ret; // the first node
106 if (!translate(*it, p->binding))
111 // build failed.. clean up and return 0
113 while (p->first_child) {
114 BindingTree *c = p->first_child;
121 // set the proper chain status on the last node
125 printf("BUILDING:\n");
126 print_branch(ret, "");
128 // successfully built a tree
132 static void destroytree(BindingTree *tree)
135 BindingTree *c = tree->first_child;
141 OBBindings::OBBindings()
146 OBBindings::~OBBindings()
152 void OBBindings::assimilate(BindingTree *node)
154 BindingTree *a, *b, *tmp, *last;
156 if (!parent->first_child) {
157 // there are no nodes at this level yet
158 parent->first_child = node;
161 a = parent->first_child;
164 while (a->first_child) {
166 if (a->binding != b->binding) {
175 last->first_child = b->first_child;
181 int OBBindings::find(BindingTree *search) {
183 a = _tree.first_child;
186 if (a->binding != b->binding) {
189 if (a->chain == b->chain) {
191 return a->id; // found it! (return the actual id, not the search's)
193 return -2; // the chain status' don't match (conflict!)
198 return -1; // it just isn't in here
202 static int find(BindingTree *parent, BindingTree *node) {
203 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
205 if (!parent->first_child)
208 p = parent->first_child;
210 if (node->binding == p->binding) {
211 if (node->chain == p->chain) {
213 return p->id; // found it! (return the actual id, not the search's)
215 break; // go on to the next child in the chain
218 return -2; // the chain status' don't match (conflict!)
223 if (!p) return -1; // doesn't exist
226 assert(node->first_child);
227 return find(p, node->first_child);
229 return -1; // it just isnt in here
233 bool OBBindings::add(const StringVect &keylist, int id)
237 if (!(tree = buildtree(keylist, id)))
238 return false; // invalid binding requested
240 if (find(tree) < -1) {
241 // conflicts with another binding
246 // assimilate this built tree into the main tree
247 assimilate(tree); // assimilation destroys/uses the tree
252 int OBBindings::find(const StringVect &keylist)
257 if (!(tree = buildtree(keylist, 0)))
258 return false; // invalid binding requested
260 ret = find(tree) >= 0;
268 int OBBindings::remove(const StringVect &keylist)
271 assert(false); // XXX: function not implemented yet
275 static void remove_branch(BindingTree *first)
277 BindingTree *p = first;
281 remove_branch(p->first_child);
282 BindingTree *s = p->next_sibling;
289 void OBBindings::remove_all()
291 if (_tree.first_child)
292 remove_branch(_tree.first_child);