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(const BindingTree *first, std::string str)
22 const 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, "");
41 printf("Mouse Tree:\n");
42 BindingTree *p = _mousetree;
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,
101 // parse out the base key name
102 std::string::size_type keybegin = str.find_last_of('-');
103 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
104 std::string key(str, keybegin);
106 // parse out the requested modifier keys
107 unsigned int modval = 0;
108 std::string::size_type begin = 0, end;
109 while (begin != keybegin) {
110 end = str.find_first_of('-', begin);
112 std::string mod(str, begin, end-begin);
113 if (!modvalue(mod, &modval)) {
114 // printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
122 b.modifiers = modval;
124 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
125 if (sym == NoSymbol) return false;
126 b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
129 return buttonvalue(key, &b.key);
133 static void destroytree(BindingTree *tree)
136 BindingTree *c = tree->first_child;
142 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id) const
144 if (keylist.empty()) return 0; // nothing in the list.. return 0
146 BindingTree *ret = 0, *p;
148 StringVect::const_reverse_iterator it, end = keylist.rend();
149 for (it = keylist.rbegin(); it != end; ++it) {
151 ret = new BindingTree(id);
152 if (!p) ret->chain = false; // only the first built node
153 ret->first_child = p;
154 if (!translate(*it, ret->binding, true)) {
159 ret->text = *it; // XXX: rm me
165 OBBindings::OBBindings()
166 : _curpos(&_keytree), _mousetree(0)
171 OBBindings::~OBBindings()
177 bool OBBindings::add_mouse(const std::string &button, int id)
181 if (!translate(button, n.binding, false))
184 BindingTree *p = _mousetree, **newp = &_mousetree;
186 if (p->binding == n.binding)
187 return false; // conflict
189 newp = &p->next_sibling;
192 *newp = new BindingTree(id);
194 (*newp)->text = button;
195 (*newp)->chain = false;
196 (*newp)->binding.key = n.binding.key;
197 (*newp)->binding.modifiers = n.binding.modifiers;
203 int OBBindings::remove_mouse(const std::string &button)
206 assert(false); // XXX: function not implemented yet
210 void OBBindings::assimilate(BindingTree *node)
212 BindingTree *a, *b, *tmp, *last;
214 if (!_keytree.first_child) {
215 // there are no nodes at this level yet
216 _keytree.first_child = node;
218 a = _keytree.first_child;
223 if (a->binding != b->binding) {
232 if (last->binding != b->binding)
233 last->next_sibling = b;
235 last->first_child = b->first_child;
242 int OBBindings::find_key(BindingTree *search) const {
244 a = _keytree.first_child;
247 if (a->binding != b->binding) {
250 if (a->chain == b->chain) {
252 return a->id; // found it! (return the actual id, not the search's)
255 return -2; // the chain status' don't match (conflict!)
261 return -1; // it just isn't in here
264 bool OBBindings::add_key(const StringVect &keylist, int id)
268 if (!(tree = buildtree(keylist, id)))
269 return false; // invalid binding requested
271 if (find_key(tree) != -1) {
272 // conflicts with another binding
277 // assimilate this built tree into the main tree
278 assimilate(tree); // assimilation destroys/uses the tree
284 int OBBindings::find_key(const StringVect &keylist)
289 if (!(tree = buildtree(keylist, 0)))
290 return false; // invalid binding requested
292 ret = find_key(tree) >= 0;
300 int OBBindings::remove_key(const StringVect &keylist)
303 assert(false); // XXX: function not implemented yet
307 static void remove_branch(BindingTree *first)
309 BindingTree *p = first;
313 remove_branch(p->first_child);
314 BindingTree *s = p->next_sibling;
321 void OBBindings::remove_all()
323 if (_keytree.first_child) {
324 remove_branch(_keytree.first_child);
325 _keytree.first_child = 0;
327 BindingTree *p = _mousetree;
329 BindingTree *n = p->next_sibling;
337 void OBBindings::process(unsigned int modifiers, unsigned int key)
339 BindingTree *c = _curpos->first_child;
342 if (c->binding.key == key && c->binding.modifiers == modifiers) {
348 if (!_curpos->chain) {
349 // XXX execute command for _curpos->id
350 _curpos = &_keytree; // back to the start