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,bool askey) 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());
96 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
97 if (sym == NoSymbol) {
98 printf(_("Invalid Key name in key binding: %s\n"), key.c_str());
101 if (!(b.key = XKeysymToKeycode(otk::OBDisplay::display, sym)))
102 printf(_("No valid keycode for Key in key binding: %s\n"), key.c_str());
105 return buttonvalue(key, &b.key);
109 static void destroytree(KeyBindingTree *tree)
112 KeyBindingTree *c = tree->first_child;
118 KeyBindingTree *OBBindings::buildtree(const StringVect &keylist,
119 PyObject *callback) const
121 if (keylist.empty()) return 0; // nothing in the list.. return 0
123 KeyBindingTree *ret = 0, *p;
125 StringVect::const_reverse_iterator it, end = keylist.rend();
126 for (it = keylist.rbegin(); it != end; ++it) {
128 ret = new KeyBindingTree();
130 // this is the first built node, the bottom node of the tree
132 ret->callbacks.push_back(callback);
134 ret->first_child = p;
135 if (!translate(*it, ret->binding)) {
145 OBBindings::OBBindings()
146 : _curpos(&_keytree),
148 _timer(Openbox::instance->timerManager(),
149 (otk::OBTimeoutHandler)resetChains, this)
151 _timer.setTimeout(5000); // chains reset after 5 seconds
153 // setResetKey("C-g"); // set the default reset key
157 OBBindings::~OBBindings()
166 void OBBindings::assimilate(KeyBindingTree *node)
168 KeyBindingTree *a, *b, *tmp, *last;
170 if (!_keytree.first_child) {
171 // there are no nodes at this level yet
172 _keytree.first_child = node;
174 a = _keytree.first_child;
179 if (a->binding != b->binding) {
188 if (last->binding != b->binding)
189 last->next_sibling = b;
191 last->first_child = b->first_child;
198 KeyBindingTree *OBBindings::find(KeyBindingTree *search,
199 bool *conflict) const {
201 KeyBindingTree *a, *b;
202 a = _keytree.first_child;
205 if (a->binding != b->binding) {
208 if (a->chain == b->chain) {
210 // found it! (return the actual id, not the search's)
215 return 0; // the chain status' don't match (conflict!)
221 return 0; // it just isn't in here
225 bool OBBindings::addKey(const StringVect &keylist, PyObject *callback)
227 KeyBindingTree *tree, *t;
230 if (!(tree = buildtree(keylist, callback)))
231 return false; // invalid binding requested
233 t = find(tree, &conflict);
235 // conflicts with another binding
241 // already bound to something
242 // XXX: look if callback is already bound to this key?
243 t->callbacks.push_back(callback);
248 // assimilate this built tree into the main tree
249 assimilate(tree); // assimilation destroys/uses the tree
260 bool OBBindings::removeKey(const StringVect &keylist, PyObject *callback)
262 assert(false); // XXX: function not implemented yet
264 KeyBindingTree *tree;
267 if (!(tree = buildtree(keylist, 0)))
268 return false; // invalid binding requested
270 KeyBindingTree *t = find(tree, &conflict);
272 CallbackList::iterator it = std::find(t->callbacks.begin(),
275 if (it != t->callbacks.end()) {
280 // XXX do shit here ...
291 void OBBindings::setResetKey(const std::string &key)
294 if (translate(key, b)) {
296 _resetkey.key = b.key;
297 _resetkey.modifiers = b.modifiers;
303 static void remove_branch(KeyBindingTree *first)
305 KeyBindingTree *p = first;
309 remove_branch(p->first_child);
310 KeyBindingTree *s = p->next_sibling;
311 while(!p->callbacks.empty()) {
312 Py_XDECREF(p->callbacks.front());
313 p->callbacks.pop_front();
321 void OBBindings::removeAllKeys()
324 if (_keytree.first_child) {
325 remove_branch(_keytree.first_child);
326 _keytree.first_child = 0;
332 void OBBindings::grabKeys(bool grab)
334 for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
335 Window root = otk::OBDisplay::screenInfo(i)->rootWindow();
337 KeyBindingTree *p = _curpos->first_child;
340 otk::OBDisplay::grabKey(p->binding.key, p->binding.modifiers,
341 root, false, GrabModeAsync, GrabModeAsync,
345 otk::OBDisplay::ungrabKey(p->binding.key, p->binding.modifiers,
352 otk::OBDisplay::grabKey(_resetkey.key, _resetkey.modifiers,
353 root, false, GrabModeAsync, GrabModeAsync,
356 otk::OBDisplay::ungrabKey(_resetkey.key, _resetkey.modifiers,
362 void OBBindings::fireKey(unsigned int modifiers, unsigned int key, Time time)
364 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
367 KeyBindingTree *p = _curpos->first_child;
369 if (p->binding.key == key && p->binding.modifiers == modifiers) {
371 _timer.start(); // start/restart the timer
377 OBClient *c = Openbox::instance->focusedClient();
378 if (c) win = c->window();
379 KeyData *data = new_key_data(win, time, modifiers, key);
380 CallbackList::iterator it, end = p->callbacks.end();
381 for (it = p->callbacks.begin(); it != end; ++it)
382 python_callback(*it, (PyObject*)data);
383 Py_XDECREF((PyObject*)data);
393 void OBBindings::resetChains(OBBindings *self)
396 self->grabKeys(false);
397 self->_curpos = &self->_keytree;
398 self->grabKeys(true);
402 bool OBBindings::addButton(const std::string &but, MouseContext context,
403 MouseAction action, PyObject *callback)
405 assert(context >= 0 && context < NUM_MOUSE_CONTEXT);
408 if (!translate(but, b, false))
411 ButtonBindingList::iterator it, end = _buttons[context].end();
413 // look for a duplicate binding
414 for (it = _buttons[context].begin(); it != end; ++it)
415 if ((*it)->binding.key == b.key &&
416 (*it)->binding.modifiers == b.modifiers) {
422 // the binding didnt exist yet, add it
424 bind = new ButtonBinding();
425 bind->binding.key = b.key;
426 bind->binding.modifiers = b.modifiers;
427 _buttons[context].push_back(bind);
428 // grab the button on all clients
429 for (int sn = 0; sn < Openbox::instance->screenCount(); ++sn) {
430 OBScreen *s = Openbox::instance->screen(sn);
431 OBClient::List::iterator c_it, c_end = s->clients.end();
432 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
433 grabButton(true, bind->binding, context, *c_it);
438 bind->callbacks[action].push_back(callback);
443 void OBBindings::removeAllButtons()
445 for (int i = i; i < NUM_MOUSE_CONTEXT; ++i) {
446 ButtonBindingList::iterator it, end = _buttons[i].end();
447 for (it = _buttons[i].begin(); it != end; ++it) {
448 for (int a = 0; a < NUM_MOUSE_ACTION; ++a) {
449 while (!(*it)->callbacks[a].empty()) {
450 Py_XDECREF((*it)->callbacks[a].front());
451 (*it)->callbacks[a].pop_front();
454 // ungrab the button on all clients
455 for (int sn = 0; sn < Openbox::instance->screenCount(); ++sn) {
456 OBScreen *s = Openbox::instance->screen(sn);
457 OBClient::List::iterator c_it, c_end = s->clients.end();
458 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
459 grabButton(false, (*it)->binding, (MouseContext)i, *c_it);
466 void OBBindings::grabButton(bool grab, const Binding &b, MouseContext context,
470 int mode = GrabModeAsync;
473 win = client->frame->window();
476 win = client->frame->plate();
477 mode = GrabModeSync; // this is handled in fireButton
480 // any other elements already get button events, don't grab on them
484 otk::OBDisplay::grabButton(b.key, b.modifiers, win, false,
485 ButtonPressMask | ButtonMotionMask |
486 ButtonReleaseMask, mode, GrabModeAsync,
489 otk::OBDisplay::ungrabButton(b.key, b.modifiers, win);
492 void OBBindings::grabButtons(bool grab, OBClient *client)
494 for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
495 ButtonBindingList::iterator it, end = _buttons[i].end();
496 for (it = _buttons[i].begin(); it != end; ++it)
497 grabButton(grab, (*it)->binding, (MouseContext)i, client);
501 void OBBindings::fireButton(ButtonData *data)
503 if (data->context == MC_Window) {
504 // these are grabbed in Sync mode to allow the press to be normal to the
506 XAllowEvents(otk::OBDisplay::display, ReplayPointer, data->time);
509 ButtonBindingList::iterator it, end = _buttons[data->context].end();
510 for (it = _buttons[data->context].begin(); it != end; ++it)
511 if ((*it)->binding.key == data->button &&
512 (*it)->binding.modifiers == data->state) {
513 CallbackList::iterator c_it,c_end = (*it)->callbacks[data->action].end();
514 for (c_it = (*it)->callbacks[data->action].begin();
515 c_it != c_end; ++c_it)
516 python_callback(*c_it, (PyObject*)data);
521 bool OBBindings::addEvent(EventAction action, PyObject *callback)
523 if (action < 0 || action >= NUM_EVENTS) {
527 _eventlist[action].push_back(callback);
532 bool OBBindings::removeEvent(EventAction action, PyObject *callback)
534 if (action < 0 || action >= NUM_EVENTS) {
538 CallbackList::iterator it = std::find(_eventlist[action].begin(),
539 _eventlist[action].end(),
541 if (it != _eventlist[action].end()) {
543 _eventlist[action].erase(it);
549 void OBBindings::removeAllEvents()
551 for (int i = 0; i < NUM_EVENTS; ++i) {
552 while (!_eventlist[i].empty()) {
553 Py_XDECREF(_eventlist[i].front());
554 _eventlist[i].pop_front();
559 void OBBindings::fireEvent(EventData *data)
561 CallbackList::iterator c_it, c_end = _eventlist[data->action].end();
562 for (c_it = _eventlist[data->action].begin(); c_it != c_end; ++c_it)
563 python_callback(*c_it, (PyObject*)data);