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 (_keytree.first_child) {
37 printf("Key Tree:\n");
38 print_branch(_keytree.first_child, "");
40 if (_mousetree.next_sibling) {
41 printf("Mouse Tree:\n");
42 BindingTree *p = _mousetree.next_sibling;
44 printf("%d %s\n", p->id, p->text.c_str());
51 static bool buttonvalue(const std::string &button, unsigned int *val)
53 if (button == "1" || button == "Button1") {
55 } else if (button == "2" || button == "Button2") {
57 } else if (button == "3" || button == "Button3") {
59 } else if (button == "4" || button == "Button4") {
61 } else if (button == "5" || button == "Button5") {
68 static bool modvalue(const std::string &mod, unsigned int *val)
70 if (mod == "C") { // control
72 } else if (mod == "S") { // shift
74 } else if (mod == "A" || // alt/mod1
79 } else if (mod == "Mod2" || // mod2
82 } else if (mod == "Mod3" || // mod3
85 } else if (mod == "W" || // windows/mod4
89 } else if (mod == "Mod5" || // mod5
98 bool OBBindings::translate(const std::string &str, Binding &b, bool askey)
100 // parse out the base key name
101 std::string::size_type keybegin = str.find_last_of('-');
102 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
103 std::string key(str, keybegin);
105 // parse out the requested modifier keys
106 unsigned int modval = 0;
107 std::string::size_type begin = 0, end;
108 while (begin != keybegin) {
109 end = str.find_first_of('-', begin);
111 std::string mod(str, begin, end-begin);
112 if (!modvalue(mod, &modval)) {
113 // printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
121 b.modifiers = modval;
123 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
124 if (sym == NoSymbol) return false;
125 b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
128 return buttonvalue(key, &b.key);
132 static void destroytree(BindingTree *tree)
135 BindingTree *c = tree->first_child;
141 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id)
143 if (keylist.empty()) return 0; // nothing in the list.. return 0
145 BindingTree *ret = 0, *p;
147 StringVect::const_reverse_iterator it, end = keylist.rend();
148 for (it = keylist.rbegin(); it != end; ++it) {
150 ret = new BindingTree(id);
151 if (!p) ret->chain = false;
152 ret->first_child = p;
153 if (!translate(*it, ret->binding, true)) {
158 ret->text = *it; // XXX: rm me
164 OBBindings::OBBindings()
170 OBBindings::~OBBindings()
176 bool OBBindings::add_mouse(const std::string &button, int id)
180 if (!translate(button, n.binding, false))
183 BindingTree *p = _mousetree.next_sibling, *last = &_mousetree;
185 if (p->binding == n.binding)
186 return false; // conflict
191 last->next_sibling = new BindingTree(id);
193 last->next_sibling->chain = false;
194 last->next_sibling->binding.key = n.binding.key;
195 last->next_sibling->binding.modifiers = n.binding.modifiers;
201 int OBBindings::remove_mouse(const std::string &button)
204 assert(false); // XXX: function not implemented yet
208 void OBBindings::assimilate(BindingTree *node)
210 BindingTree *a, *b, *tmp, *last;
212 if (!_keytree.first_child) {
213 // there are no nodes at this level yet
214 _keytree.first_child = node;
217 a = _keytree.first_child;
222 if (a->binding != b->binding) {
231 if (last->binding != b->binding)
232 last->next_sibling = b;
234 last->first_child = b->first_child;
240 int OBBindings::find_key(BindingTree *search) {
242 a = _keytree.first_child;
245 if (a->binding != b->binding) {
248 if (a->chain == b->chain) {
250 return a->id; // found it! (return the actual id, not the search's)
252 return -2; // the chain status' don't match (conflict!)
257 return -1; // it just isn't in here
260 bool OBBindings::add_key(const StringVect &keylist, int id)
264 if (!(tree = buildtree(keylist, id)))
265 return false; // invalid binding requested
267 if (find_key(tree) < -1) {
268 // conflicts with another binding
273 // assimilate this built tree into the main tree
274 assimilate(tree); // assimilation destroys/uses the tree
279 int OBBindings::find_key(const StringVect &keylist)
284 if (!(tree = buildtree(keylist, 0)))
285 return false; // invalid binding requested
287 ret = find_key(tree) >= 0;
295 int OBBindings::remove_key(const StringVect &keylist)
298 assert(false); // XXX: function not implemented yet
302 static void remove_branch(BindingTree *first)
304 BindingTree *p = first;
308 remove_branch(p->first_child);
309 BindingTree *s = p->next_sibling;
316 void OBBindings::remove_all()
318 if (_keytree.first_child) {
319 remove_branch(_keytree.first_child);
320 _keytree.first_child = 0;
322 BindingTree *p = _mousetree.next_sibling;
324 BindingTree *n = p->next_sibling;
328 _mousetree.next_sibling = 0;
332 void OBBindings::process(unsigned int modifiers, unsigned int key)
334 BindingTree *c = _curpos->first_child;
337 if (c->binding.key == key && c->binding.modifiers == modifiers) {
343 if (!_curpos->chain) {
344 // XXX execute command for _curpos->id
345 _curpos = &_keytree; // back to the start