1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
8 #include "otk/display.hh"
14 #define _(str) gettext(str)
20 static void print_branch(BindingTree *first, std::string str)
22 BindingTree *p = first;
26 print_branch(p->first_child, str + " " + p->text);
28 printf("%d%s\n", p->id, (str + " " + p->text).c_str());
29 BindingTree *s = p->next_sibling;
36 void OBBindings::display()
38 if (_tree.first_child)
39 print_branch(_tree.first_child, "");
44 bool OBBindings::translate(const std::string &str, Binding &b)
46 unsigned int mods = 0;
47 std::string modstring;
49 // parse out the base key name
50 std::string::size_type keybegin = str.find_last_of('-');
51 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
52 std::string key(str, keybegin);
54 // XXX: get some modifiers up in the hizzie
55 // parse out the requested modifier keys
56 std::string::size_type begin = 0, end;
57 while (begin != keybegin) {
58 end = str.find_first_of('-', begin);
60 std::string mod(str, begin, end-begin);
63 if (mod == "C") { // control
65 } else if (mod == "S") { // shift
67 } else if (mod == "A" || // alt/mod1
72 } else if (mod == "M2" || // mod2
75 } else if (mod == "M3" || // mod3
78 } else if (mod == "W" || // windows/mod4
82 } else if (mod == "M5" || // mod5
86 printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
92 printf("got modifier: %s\n", modstring.c_str());
94 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
95 if (sym == NoSymbol) return false;
97 b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
101 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id)
103 if (keylist.empty()) return 0; // nothing in the list.. return 0
105 BindingTree *ret = new BindingTree(id), *p = 0;
107 StringVect::const_iterator it, end = keylist.end();
108 for (it = keylist.begin(); it != end; ++it) {
110 p = p->first_child = new BindingTree(id);
112 p = ret; // the first node
114 if (!translate(*it, p->binding))
119 // build failed.. clean up and return 0
121 while (p->first_child) {
122 BindingTree *c = p->first_child;
129 // set the proper chain status on the last node
133 printf("BUILDING:\n");
134 print_branch(ret, "");
136 // successfully built a tree
140 static void destroytree(BindingTree *tree)
143 BindingTree *c = tree->first_child;
149 OBBindings::OBBindings()
154 OBBindings::~OBBindings()
160 void OBBindings::assimilate(BindingTree *node)
162 BindingTree *a, *b, *tmp, *last;
164 if (!_tree.first_child) {
165 // there are no nodes at this level yet
166 _tree.first_child = node;
169 a = _tree.first_child;
172 while (a->first_child) {
174 if (a->binding != b->binding) {
183 last->first_child = b->first_child;
189 int OBBindings::find(BindingTree *search) {
191 a = _tree.first_child;
194 if (a->binding != b->binding) {
197 if (a->chain == b->chain) {
199 return a->id; // found it! (return the actual id, not the search's)
201 return -2; // the chain status' don't match (conflict!)
206 return -1; // it just isn't in here
210 static int find(BindingTree *parent, BindingTree *node) {
211 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
213 if (!parent->first_child)
216 p = parent->first_child;
218 if (node->binding == p->binding) {
219 if (node->chain == p->chain) {
221 return p->id; // found it! (return the actual id, not the search's)
223 break; // go on to the next child in the chain
226 return -2; // the chain status' don't match (conflict!)
231 if (!p) return -1; // doesn't exist
234 assert(node->first_child);
235 return find(p, node->first_child);
237 return -1; // it just isnt in here
241 bool OBBindings::add(const StringVect &keylist, int id)
245 if (!(tree = buildtree(keylist, id)))
246 return false; // invalid binding requested
248 if (find(tree) < -1) {
249 // conflicts with another binding
254 // assimilate this built tree into the main tree
255 assimilate(tree); // assimilation destroys/uses the tree
260 int OBBindings::find(const StringVect &keylist)
265 if (!(tree = buildtree(keylist, 0)))
266 return false; // invalid binding requested
268 ret = find(tree) >= 0;
276 int OBBindings::remove(const StringVect &keylist)
279 assert(false); // XXX: function not implemented yet
283 static void remove_branch(BindingTree *first)
285 BindingTree *p = first;
289 remove_branch(p->first_child);
290 BindingTree *s = p->next_sibling;
297 void OBBindings::remove_all()
299 if (_tree.first_child)
300 remove_branch(_tree.first_child);