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());
34 void OBBindings::display()
36 if (_tree.first_child)
37 print_branch(_tree.first_child, "");
42 bool OBBindings::translate(const std::string &str, Binding &b)
44 unsigned int mods = 0;
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);
59 if (mod == "C") { // control
61 } else if (mod == "S") { // shift
63 } else if (mod == "A" || // alt/mod1
68 } else if (mod == "M2" || // mod2
71 } else if (mod == "M3" || // mod3
74 } else if (mod == "W" || // windows/mod4
78 } else if (mod == "M5" || // mod5
82 printf(_("Invalid modifier element in key binding: %s\n"), mod.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;
168 if (a->binding != b->binding) {
177 if (last->binding != b->binding)
178 last->next_sibling = b;
180 last->first_child = b->first_child;
186 int OBBindings::find(BindingTree *search) {
188 a = _tree.first_child;
191 if (a->binding != b->binding) {
194 if (a->chain == b->chain) {
196 return a->id; // found it! (return the actual id, not the search's)
198 return -2; // the chain status' don't match (conflict!)
203 return -1; // it just isn't in here
207 static int find(BindingTree *parent, BindingTree *node) {
208 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
210 if (!parent->first_child)
213 p = parent->first_child;
215 if (node->binding == p->binding) {
216 if (node->chain == p->chain) {
218 return p->id; // found it! (return the actual id, not the search's)
220 break; // go on to the next child in the chain
223 return -2; // the chain status' don't match (conflict!)
228 if (!p) return -1; // doesn't exist
231 assert(node->first_child);
232 return find(p, node->first_child);
234 return -1; // it just isnt in here
238 bool OBBindings::add(const StringVect &keylist, int id)
242 if (!(tree = buildtree(keylist, id)))
243 return false; // invalid binding requested
245 if (find(tree) < -1) {
246 // conflicts with another binding
251 // assimilate this built tree into the main tree
252 assimilate(tree); // assimilation destroys/uses the tree
257 int OBBindings::find(const StringVect &keylist)
262 if (!(tree = buildtree(keylist, 0)))
263 return false; // invalid binding requested
265 ret = find(tree) >= 0;
273 int OBBindings::remove(const StringVect &keylist)
276 assert(false); // XXX: function not implemented yet
280 static void remove_branch(BindingTree *first)
282 BindingTree *p = first;
286 remove_branch(p->first_child);
287 BindingTree *s = p->next_sibling;
294 void OBBindings::remove_all()
296 if (_tree.first_child)
297 remove_branch(_tree.first_child);