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, "");
41 static bool modvalue(const std::string &mod, unsigned int *val)
43 if (mod == "C") { // control
45 } else if (mod == "S") { // shift
47 } else if (mod == "A" || // alt/mod1
52 } else if (mod == "M2" || // mod2
55 } else if (mod == "M3" || // mod3
58 } else if (mod == "W" || // windows/mod4
62 } else if (mod == "M5" || // mod5
71 bool OBBindings::translate(const std::string &str, Binding &b)
73 // parse out the base key name
74 std::string::size_type keybegin = str.find_last_of('-');
75 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
76 std::string key(str, keybegin);
78 // parse out the requested modifier keys
79 unsigned int modval = 0;
80 std::string::size_type begin = 0, end;
81 while (begin != keybegin) {
82 end = str.find_first_of('-', begin);
84 std::string mod(str, begin, end-begin);
85 if (!modvalue(mod, &modval)) {
86 printf(_("Invalid modifier element in key binding: %s\n"), mod.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 static void destroytree(BindingTree *tree)
104 BindingTree *c = tree->first_child;
110 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id)
112 if (keylist.empty()) return 0; // nothing in the list.. return 0
114 BindingTree *ret = 0, *p;
116 StringVect::const_reverse_iterator it, end = keylist.rend();
117 for (it = keylist.rbegin(); it != end; ++it) {
119 ret = new BindingTree(id);
120 if (!p) ret->chain = false;
121 ret->first_child = p;
122 if (!translate(*it, ret->binding)) {
127 ret->text = *it; // XXX: rm me
133 OBBindings::OBBindings()
138 OBBindings::~OBBindings()
144 void OBBindings::assimilate(BindingTree *node)
146 BindingTree *a, *b, *tmp, *last;
148 if (!_tree.first_child) {
149 // there are no nodes at this level yet
150 _tree.first_child = node;
153 a = _tree.first_child;
158 if (a->binding != b->binding) {
167 if (last->binding != b->binding)
168 last->next_sibling = b;
170 last->first_child = b->first_child;
176 int OBBindings::find(BindingTree *search) {
178 a = _tree.first_child;
181 if (a->binding != b->binding) {
184 if (a->chain == b->chain) {
186 return a->id; // found it! (return the actual id, not the search's)
188 return -2; // the chain status' don't match (conflict!)
193 return -1; // it just isn't in here
197 static int find(BindingTree *parent, BindingTree *node) {
198 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
200 if (!parent->first_child)
203 p = parent->first_child;
205 if (node->binding == p->binding) {
206 if (node->chain == p->chain) {
208 return p->id; // found it! (return the actual id, not the search's)
210 break; // go on to the next child in the chain
213 return -2; // the chain status' don't match (conflict!)
218 if (!p) return -1; // doesn't exist
221 assert(node->first_child);
222 return find(p, node->first_child);
224 return -1; // it just isnt in here
228 bool OBBindings::add(const StringVect &keylist, int id)
232 if (!(tree = buildtree(keylist, id)))
233 return false; // invalid binding requested
235 if (find(tree) < -1) {
236 // conflicts with another binding
241 // assimilate this built tree into the main tree
242 assimilate(tree); // assimilation destroys/uses the tree
247 int OBBindings::find(const StringVect &keylist)
252 if (!(tree = buildtree(keylist, 0)))
253 return false; // invalid binding requested
255 ret = find(tree) >= 0;
263 int OBBindings::remove(const StringVect &keylist)
266 assert(false); // XXX: function not implemented yet
270 static void remove_branch(BindingTree *first)
272 BindingTree *p = first;
276 remove_branch(p->first_child);
277 BindingTree *s = p->next_sibling;
284 void OBBindings::remove_all()
286 if (_tree.first_child)
287 remove_branch(_tree.first_child);