1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
13 #include "otk/display.hh"
19 #define _(str) gettext(str)
24 static bool buttonvalue(const std::string &button, unsigned int *val)
26 if (button == "1" || button == "Button1") {
28 } else if (button == "2" || button == "Button2") {
30 } else if (button == "3" || button == "Button3") {
32 } else if (button == "4" || button == "Button4") {
34 } else if (button == "5" || button == "Button5") {
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 == "Mod2" || // mod2
55 } else if (mod == "Mod3" || // mod3
58 } else if (mod == "W" || // windows/mod4
62 } else if (mod == "Mod5" || // mod5
71 bool OBBindings::translate(const std::string &str, Binding &b) const
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());
95 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
96 if (sym == NoSymbol) {
97 printf(_("Invalid Key name in key binding: %s\n"), key.c_str());
100 if (!(b.key = XKeysymToKeycode(otk::OBDisplay::display, sym)))
101 printf(_("No valid keycode for Key in key binding: %s\n"), key.c_str());
105 static void destroytree(BindingTree *tree)
108 BindingTree *c = tree->first_child;
114 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id) const
116 if (keylist.empty()) return 0; // nothing in the list.. return 0
118 BindingTree *ret = 0, *p;
120 StringVect::const_reverse_iterator it, end = keylist.rend();
121 for (it = keylist.rbegin(); it != end; ++it) {
123 ret = new BindingTree(id);
124 if (!p) ret->chain = false; // only the first built node
125 ret->first_child = p;
126 if (!translate(*it, ret->binding)) {
136 OBBindings::OBBindings()
139 _timer(Openbox::instance->timerManager(),
140 (otk::OBTimeoutHandler)reset, this)
142 _timer.setTimeout(5000); // chains reset after 5 seconds
144 setResetKey("C-g"); // set the default reset key
148 OBBindings::~OBBindings()
155 void OBBindings::assimilate(BindingTree *node)
157 BindingTree *a, *b, *tmp, *last;
159 if (!_tree.first_child) {
160 // there are no nodes at this level yet
161 _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;
187 int OBBindings::find(BindingTree *search) const {
189 a = _tree.first_child;
192 if (a->binding != b->binding) {
195 if (a->chain == b->chain) {
197 return a->id; // found it! (return the actual id, not the search's)
200 return -2; // the chain status' don't match (conflict!)
206 return -1; // it just isn't in here
210 bool OBBindings::add(const StringVect &keylist, int id)
214 if (!(tree = buildtree(keylist, id)))
215 return false; // invalid binding requested
217 if (find(tree) != -1) {
218 // conflicts with another binding
225 // assimilate this built tree into the main tree
226 assimilate(tree); // assimilation destroys/uses the tree
234 int OBBindings::find(const StringVect &keylist)
239 if (!(tree = buildtree(keylist, 0)))
240 return false; // invalid binding requested
242 ret = find(tree) >= 0;
250 int OBBindings::remove(const StringVect &keylist)
253 assert(false); // XXX: function not implemented yet
265 void OBBindings::setResetKey(const std::string &key)
268 if (translate(key, b)) {
270 _resetkey.key = b.key;
271 _resetkey.modifiers = b.modifiers;
277 static void remove_branch(BindingTree *first)
279 BindingTree *p = first;
283 remove_branch(p->first_child);
284 BindingTree *s = p->next_sibling;
291 void OBBindings::remove_all()
293 if (_tree.first_child) {
294 remove_branch(_tree.first_child);
295 _tree.first_child = 0;
300 void OBBindings::grabKeys(bool grab)
302 for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
303 Window root = otk::OBDisplay::screenInfo(i)->rootWindow();
305 BindingTree *p = _curpos->first_child;
308 otk::OBDisplay::grabKey(p->binding.key, p->binding.modifiers,
309 root, false, GrabModeAsync, GrabModeAsync,
313 otk::OBDisplay::ungrabKey(p->binding.key, p->binding.modifiers,
319 otk::OBDisplay::grabKey(_resetkey.key, _resetkey.modifiers,
320 root, true, GrabModeAsync, GrabModeAsync,
323 otk::OBDisplay::ungrabKey(_resetkey.key, _resetkey.modifiers,
329 void OBBindings::fire(Window window, unsigned int modifiers, unsigned int key,
332 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
335 BindingTree *p = _curpos->first_child;
337 if (p->binding.key == key && p->binding.modifiers == modifiers) {
339 _timer.start(); // start/restart the timer
344 python_callback_binding(p->id, window, modifiers, key, time);
354 void OBBindings::reset(OBBindings *self)
357 self->grabKeys(false);
358 self->_curpos = &self->_tree;
359 self->grabKeys(true);