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 == "Left" || button == "1" || button == "Button1") {
28 } else if (button == "Middle" || button == "2" || button == "Button2") {
30 } else if (button == "Right" || button == "3" || button == "Button3") {
32 } else if (button == "Up" || button == "4" || button == "Button4") {
34 } else if (button == "Down" || 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 Bindings::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::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 *Bindings::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)) {
146 : _curpos(&_keytree),
148 _timer((otk::Timer *) 0)
150 // setResetKey("C-g"); // set the default reset key
154 Bindings::~Bindings()
161 // removeAllButtons(); XXX
166 void Bindings::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 *Bindings::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 Bindings::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);
246 // grab the server here to make sure no key pressed go missed
247 otk::display->grab();
250 // assimilate this built tree into the main tree
251 assimilate(tree); // assimilation destroys/uses the tree
254 otk::display->ungrab();
263 bool Bindings::removeKey(const StringVect &keylist, PyObject *callback)
265 assert(false); // XXX: function not implemented yet
267 KeyBindingTree *tree;
270 if (!(tree = buildtree(keylist, 0)))
271 return false; // invalid binding requested
273 KeyBindingTree *t = find(tree, &conflict);
275 CallbackList::iterator it = std::find(t->callbacks.begin(),
278 if (it != t->callbacks.end()) {
279 // grab the server here to make sure no key pressed go missed
280 otk::display->grab();
285 // XXX do shit here ...
289 otk::display->ungrab();
298 void Bindings::setResetKey(const std::string &key)
301 if (translate(key, b)) {
302 // grab the server here to make sure no key pressed go missed
303 otk::display->grab();
305 _resetkey.key = b.key;
306 _resetkey.modifiers = b.modifiers;
308 otk::display->ungrab();
313 static void remove_branch(KeyBindingTree *first)
315 KeyBindingTree *p = first;
319 remove_branch(p->first_child);
320 KeyBindingTree *s = p->next_sibling;
321 while(!p->callbacks.empty()) {
322 Py_XDECREF(p->callbacks.front());
323 p->callbacks.pop_front();
331 void Bindings::removeAllKeys()
334 if (_keytree.first_child) {
335 remove_branch(_keytree.first_child);
336 _keytree.first_child = 0;
342 void Bindings::grabKeys(bool grab)
344 for (int i = 0; i < openbox->screenCount(); ++i) {
345 Window root = otk::display->screenInfo(i)->rootWindow();
347 KeyBindingTree *p = _curpos->first_child;
350 otk::display->grabKey(p->binding.key, p->binding.modifiers,
351 root, false, GrabModeAsync, GrabModeAsync,
355 otk::display->ungrabKey(p->binding.key, p->binding.modifiers,
362 otk::display->grabKey(_resetkey.key, _resetkey.modifiers,
363 root, false, GrabModeAsync, GrabModeAsync,
366 otk::display->ungrabKey(_resetkey.key, _resetkey.modifiers,
372 void Bindings::fireKey(int screen, unsigned int modifiers, unsigned int key,
375 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
378 KeyBindingTree *p = _curpos->first_child;
380 if (p->binding.key == key && p->binding.modifiers == modifiers) {
384 _timer = new otk::Timer(5000, // 5 second timeout
385 (otk::Timer::TimeoutHandler)resetChains,
387 // grab the server here to make sure no key pressed go missed
388 otk::display->grab();
392 otk::display->ungrab();
394 Client *c = openbox->focusedClient();
395 KeyData data(screen, c, time, modifiers, key);
396 CallbackList::iterator it, end = p->callbacks.end();
397 for (it = p->callbacks.begin(); it != end; ++it)
398 python_callback(*it, &data);
408 void Bindings::resetChains(Bindings *self)
412 self->_timer = (otk::Timer *) 0;
414 // grab the server here to make sure no key pressed go missed
415 otk::display->grab();
416 self->grabKeys(false);
417 self->_curpos = &self->_keytree;
418 self->grabKeys(true);
419 otk::display->ungrab();
423 bool Bindings::addButton(const std::string &but, MouseContext context,
424 MouseAction action, PyObject *callback)
426 assert(context >= 0 && context < NUM_MOUSE_CONTEXT);
429 if (!translate(but, b, false))
432 ButtonBindingList::iterator it, end = _buttons[context].end();
434 // look for a duplicate binding
435 for (it = _buttons[context].begin(); it != end; ++it)
436 if ((*it)->binding.key == b.key &&
437 (*it)->binding.modifiers == b.modifiers) {
443 // the binding didnt exist yet, add it
445 bind = new ButtonBinding();
446 bind->binding.key = b.key;
447 bind->binding.modifiers = b.modifiers;
448 _buttons[context].push_back(bind);
449 // grab the button on all clients
450 for (int sn = 0; sn < openbox->screenCount(); ++sn) {
451 Screen *s = openbox->screen(sn);
452 Client::List::iterator c_it, c_end = s->clients.end();
453 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
454 grabButton(true, bind->binding, context, *c_it);
459 bind->callbacks[action].push_back(callback);
464 void Bindings::removeAllButtons()
466 for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
467 ButtonBindingList::iterator it, end = _buttons[i].end();
468 for (it = _buttons[i].begin(); it != end; ++it) {
469 for (int a = 0; a < NUM_MOUSE_ACTION; ++a) {
470 while (!(*it)->callbacks[a].empty()) {
471 Py_XDECREF((*it)->callbacks[a].front());
472 (*it)->callbacks[a].pop_front();
475 // ungrab the button on all clients
476 for (int sn = 0; sn < openbox->screenCount(); ++sn) {
477 Screen *s = openbox->screen(sn);
478 Client::List::iterator c_it, c_end = s->clients.end();
479 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
480 grabButton(false, (*it)->binding, (MouseContext)i, *c_it);
487 void Bindings::grabButton(bool grab, const Binding &b, MouseContext context,
491 int mode = GrabModeAsync;
495 win = client->frame->window();
496 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
499 win = client->frame->plate();
500 mode = GrabModeSync; // this is handled in fireButton
501 mask = ButtonPressMask; // can't catch more than this with Sync mode
502 // the release event is manufactured by the
503 // master buttonPressHandler
506 // any other elements already get button events, don't grab on them
510 otk::display->grabButton(b.key, b.modifiers, win, false, mask, mode,
511 GrabModeAsync, None, None, false);
513 otk::display->ungrabButton(b.key, b.modifiers, win);
516 void Bindings::grabButtons(bool grab, Client *client)
518 for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
519 ButtonBindingList::iterator it, end = _buttons[i].end();
520 for (it = _buttons[i].begin(); it != end; ++it)
521 grabButton(grab, (*it)->binding, (MouseContext)i, client);
525 void Bindings::fireButton(MouseData *data)
527 if (data->context == MC_Window) {
528 // Replay the event, so it goes to the client, and ungrab the device.
529 XAllowEvents(**otk::display, ReplayPointer, data->time);
532 ButtonBindingList::iterator it, end = _buttons[data->context].end();
533 for (it = _buttons[data->context].begin(); it != end; ++it)
534 if ((*it)->binding.key == data->button &&
535 (*it)->binding.modifiers == data->state) {
536 CallbackList::iterator c_it,c_end = (*it)->callbacks[data->action].end();
537 for (c_it = (*it)->callbacks[data->action].begin();
538 c_it != c_end; ++c_it)
539 python_callback(*c_it, data);
544 bool Bindings::addEvent(EventAction action, PyObject *callback)
546 if (action < 0 || action >= NUM_EVENTS) {
550 if (action == EventBell && _eventlist[action].empty())
551 XkbSelectEvents(**otk::display, XkbUseCoreKbd,
552 XkbBellNotifyMask, XkbBellNotifyMask);
554 _eventlist[action].push_back(callback);
559 bool Bindings::removeEvent(EventAction action, PyObject *callback)
561 if (action < 0 || action >= NUM_EVENTS) {
565 CallbackList::iterator it = std::find(_eventlist[action].begin(),
566 _eventlist[action].end(),
568 if (it != _eventlist[action].end()) {
570 _eventlist[action].erase(it);
572 if (action == EventBell && _eventlist[action].empty())
573 XkbSelectEvents(**otk::display, XkbUseCoreKbd,
574 XkbBellNotifyMask, 0);
581 void Bindings::removeAllEvents()
583 for (int i = 0; i < NUM_EVENTS; ++i) {
584 while (!_eventlist[i].empty()) {
585 Py_XDECREF(_eventlist[i].front());
586 _eventlist[i].pop_front();
591 void Bindings::fireEvent(EventData *data)
593 CallbackList::iterator c_it, c_end = _eventlist[data->action].end();
594 for (c_it = _eventlist[data->action].begin(); c_it != c_end; ++c_it)
595 python_callback(*c_it, data);