X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fbindings.cc;h=d6bf85d0bbf49a6f3bea5214abc2fb802fa148b8;hb=579ed5aa8d1b33a69476c2182a98ebc30557f096;hp=1a102b5452b78c30e65a05f6716bbc36f9e885d9;hpb=db1fb534b8f5a1f19710ac77ef2ce85c4c1270ec;p=chaz%2Fopenbox diff --git a/src/bindings.cc b/src/bindings.cc index 1a102b54..d6bf85d0 100644 --- a/src/bindings.cc +++ b/src/bindings.cc @@ -5,6 +5,11 @@ #endif #include "bindings.hh" +#include "screen.hh" +#include "openbox.hh" +#include "client.hh" +#include "frame.hh" +#include "python.hh" #include "otk/display.hh" extern "C" { @@ -16,38 +21,6 @@ extern "C" { namespace ob { -#include -static void print_branch(const BindingTree *first, std::string str) -{ - const BindingTree *p = first; - - while (p) { - if (p->first_child) - print_branch(p->first_child, str + " " + p->text); - if (!p->chain) - printf("%d%s\n", p->id, (str + " " + p->text).c_str()); - p = p->next_sibling; - } -} - - -void OBBindings::display() -{ - if (_keytree.first_child) { - printf("Key Tree:\n"); - print_branch(_keytree.first_child, ""); - } - if (_mousetree) { - printf("Mouse Tree:\n"); - BindingTree *p = _mousetree; - while (p) { - printf("%d %s\n", p->id, p->text.c_str()); - p = p->next_sibling; - } - } -} - - static bool buttonvalue(const std::string &button, unsigned int *val) { if (button == "1" || button == "Button1") { @@ -95,8 +68,7 @@ static bool modvalue(const std::string &mod, unsigned int *val) return true; } -bool OBBindings::translate(const std::string &str, Binding &b, - bool askey) const +bool OBBindings::translate(const std::string &str, Binding &b,bool askey) const { // parse out the base key name std::string::size_type keybegin = str.find_last_of('-'); @@ -111,7 +83,7 @@ bool OBBindings::translate(const std::string &str, Binding &b, std::string mod(str, begin, end-begin); if (!modvalue(mod, &modval)) { -// printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str()); + printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str()); return false; } @@ -122,8 +94,12 @@ bool OBBindings::translate(const std::string &str, Binding &b, b.modifiers = modval; if (askey) { KeySym sym = XStringToKeysym(const_cast(key.c_str())); - if (sym == NoSymbol) return false; - b.key = XKeysymToKeycode(otk::OBDisplay::display, sym); + if (sym == NoSymbol) { + printf(_("Invalid Key name in key binding: %s\n"), key.c_str()); + return false; + } + if (!(b.key = XKeysymToKeycode(otk::OBDisplay::display, sym))) + printf(_("No valid keycode for Key in key binding: %s\n"), key.c_str()); return b.key != 0; } else { return buttonvalue(key, &b.key); @@ -139,7 +115,8 @@ static void destroytree(BindingTree *tree) } } -BindingTree *OBBindings::buildtree(const StringVect &keylist, int id) const +BindingTree *OBBindings::buildtree(const StringVect &keylist, + PyObject *callback) const { if (keylist.empty()) return 0; // nothing in the list.. return 0 @@ -148,62 +125,35 @@ BindingTree *OBBindings::buildtree(const StringVect &keylist, int id) const StringVect::const_reverse_iterator it, end = keylist.rend(); for (it = keylist.rbegin(); it != end; ++it) { p = ret; - ret = new BindingTree(id); + ret = new BindingTree(callback); if (!p) ret->chain = false; // only the first built node ret->first_child = p; - if (!translate(*it, ret->binding, true)) { + if (!translate(*it, ret->binding)) { destroytree(ret); ret = 0; break; } - ret->text = *it; // XXX: rm me } return ret; } OBBindings::OBBindings() - : _curpos(&_keytree), _mousetree(0) + : _curpos(&_tree), + _resetkey(0,0), + _timer(Openbox::instance->timerManager(), + (otk::OBTimeoutHandler)reset, this) { + _timer.setTimeout(5000); // chains reset after 5 seconds + + setResetKey("C-g"); // set the default reset key } OBBindings::~OBBindings() { - remove_all(); -} - - -bool OBBindings::add_mouse(const std::string &button, int id) -{ - BindingTree n; - - if (!translate(button, n.binding, false)) - return false; - - BindingTree *p = _mousetree, **newp = &_mousetree; - while (p) { - if (p->binding == n.binding) - return false; // conflict - p = p->next_sibling; - newp = &p->next_sibling; - } - display(); - *newp = new BindingTree(id); - display(); - (*newp)->text = button; - (*newp)->chain = false; - (*newp)->binding.key = n.binding.key; - (*newp)->binding.modifiers = n.binding.modifiers; - - return true; -} - - -int OBBindings::remove_mouse(const std::string &button) -{ - (void)button; - assert(false); // XXX: function not implemented yet + grabKeys(false); + removeAll(); } @@ -211,11 +161,11 @@ void OBBindings::assimilate(BindingTree *node) { BindingTree *a, *b, *tmp, *last; - if (!_keytree.first_child) { + if (!_tree.first_child) { // there are no nodes at this level yet - _keytree.first_child = node; + _tree.first_child = node; } else { - a = _keytree.first_child; + a = _tree.first_child; last = a; b = node; while (a) { @@ -239,9 +189,10 @@ void OBBindings::assimilate(BindingTree *node) } -int OBBindings::find_key(BindingTree *search) const { +PyObject *OBBindings::find(BindingTree *search, bool *conflict) const { + *conflict = false; BindingTree *a, *b; - a = _keytree.first_child; + a = _tree.first_child; b = search; while (a && b) { if (a->binding != b->binding) { @@ -249,58 +200,83 @@ int OBBindings::find_key(BindingTree *search) const { } else { if (a->chain == b->chain) { if (!a->chain) { - return a->id; // found it! (return the actual id, not the search's) + // found it! (return the actual id, not the search's) + return a->callback; } } else { - return -2; // the chain status' don't match (conflict!) + *conflict = true; + return 0; // the chain status' don't match (conflict!) } b = b->first_child; a = a->first_child; } } - return -1; // it just isn't in here + return 0; // it just isn't in here } -bool OBBindings::add_key(const StringVect &keylist, int id) + +bool OBBindings::add(const StringVect &keylist, PyObject *callback) { BindingTree *tree; + bool conflict; - if (!(tree = buildtree(keylist, id))) + if (!(tree = buildtree(keylist, callback))) return false; // invalid binding requested - if (find_key(tree) != -1) { + if (find(tree, &conflict) || conflict) { // conflicts with another binding destroytree(tree); return false; } + grabKeys(false); + // assimilate this built tree into the main tree assimilate(tree); // assimilation destroys/uses the tree + Py_INCREF(callback); + + grabKeys(true); + return true; } -int OBBindings::find_key(const StringVect &keylist) +bool OBBindings::remove(const StringVect &keylist) { + assert(false); // XXX: function not implemented yet + BindingTree *tree; - bool ret; + bool conflict; if (!(tree = buildtree(keylist, 0))) return false; // invalid binding requested - ret = find_key(tree) >= 0; + PyObject *func = find(tree, &conflict); + if (func) { + grabKeys(false); - destroytree(tree); - - return ret; + _curpos = &_tree; + + // XXX do shit here ... + Py_DECREF(func); + + grabKeys(true); + return true; + } + return false; } -int OBBindings::remove_key(const StringVect &keylist) +void OBBindings::setResetKey(const std::string &key) { - (void)keylist; - assert(false); // XXX: function not implemented yet + Binding b(0, 0); + if (translate(key, b)) { + grabKeys(false); + _resetkey.key = b.key; + _resetkey.modifiers = b.modifiers; + grabKeys(true); + } } @@ -312,44 +288,84 @@ static void remove_branch(BindingTree *first) if (p->first_child) remove_branch(p->first_child); BindingTree *s = p->next_sibling; + Py_XDECREF(p->callback); delete p; p = s; } } -void OBBindings::remove_all() +void OBBindings::removeAll() { - if (_keytree.first_child) { - remove_branch(_keytree.first_child); - _keytree.first_child = 0; - } - BindingTree *p = _mousetree; - while (p) { - BindingTree *n = p->next_sibling; - delete p; - p = n; + if (_tree.first_child) { + remove_branch(_tree.first_child); + _tree.first_child = 0; } - _mousetree = 0; } -void OBBindings::process(unsigned int modifiers, unsigned int key) +void OBBindings::grabKeys(bool grab) { - BindingTree *c = _curpos->first_child; + for (int i = 0; i < Openbox::instance->screenCount(); ++i) { + Window root = otk::OBDisplay::screenInfo(i)->rootWindow(); - while (c) { - if (c->binding.key == key && c->binding.modifiers == modifiers) { - _curpos = c; - break; + BindingTree *p = _curpos->first_child; + while (p) { + if (grab) { + otk::OBDisplay::grabKey(p->binding.key, p->binding.modifiers, + root, false, GrabModeAsync, GrabModeAsync, + false); + } + else + otk::OBDisplay::ungrabKey(p->binding.key, p->binding.modifiers, + root); + p = p->next_sibling; } + + if (grab) + otk::OBDisplay::grabKey(_resetkey.key, _resetkey.modifiers, + root, true, GrabModeAsync, GrabModeAsync, + false); + else + otk::OBDisplay::ungrabKey(_resetkey.key, _resetkey.modifiers, + root); } - if (c) { - if (!_curpos->chain) { - // XXX execute command for _curpos->id - _curpos = &_keytree; // back to the start +} + + +void OBBindings::fire(unsigned int modifiers, unsigned int key, Time time) +{ + if (key == _resetkey.key && modifiers == _resetkey.modifiers) { + reset(this); + } else { + BindingTree *p = _curpos->first_child; + while (p) { + if (p->binding.key == key && p->binding.modifiers == modifiers) { + if (p->chain) { + _timer.start(); // start/restart the timer + grabKeys(false); + _curpos = p; + grabKeys(true); + } else { + Window win = None; + OBClient *c = Openbox::instance->focusedClient(); + if (c) win = c->window(); + python_callback(p->callback, win, modifiers, key, time); + reset(this); + } + break; + } + p = p->next_sibling; } } } +void OBBindings::reset(OBBindings *self) +{ + self->_timer.stop(); + self->grabKeys(false); + self->_curpos = &self->_tree; + self->grabKeys(true); +} + }