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)
26 static bool buttonvalue(const std::string &button, unsigned int *val)
28 if (button == "Left" || button == "1" || button == "Button1") {
30 } else if (button == "Middle" || button == "2" || button == "Button2") {
32 } else if (button == "Right" || button == "3" || button == "Button3") {
34 } else if (button == "Up" || button == "4" || button == "Button4") {
36 } else if (button == "Down" || button == "5" || button == "Button5") {
43 static bool modvalue(const std::string &mod, unsigned int *val)
45 if (mod == "C") { // control
47 } else if (mod == "S") { // shift
49 } else if (mod == "A" || // alt/mod1
54 } else if (mod == "Mod2" || // mod2
57 } else if (mod == "Mod3" || // mod3
60 } else if (mod == "W" || // windows/mod4
64 } else if (mod == "Mod5" || // mod5
73 bool Bindings::translate(const std::string &str, Binding &b,bool askey) const
75 // parse out the base key name
76 std::string::size_type keybegin = str.find_last_of('-');
77 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
78 std::string key(str, keybegin);
80 // parse out the requested modifier keys
81 unsigned int modval = 0;
82 std::string::size_type begin = 0, end;
83 while (begin != keybegin) {
84 end = str.find_first_of('-', begin);
86 std::string mod(str, begin, end-begin);
87 if (!modvalue(mod, &modval)) {
88 printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
98 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
99 if (sym == NoSymbol) {
100 printf(_("Invalid Key name in key binding: %s\n"), key.c_str());
103 if (!(b.key = XKeysymToKeycode(**otk::display, sym)))
104 printf(_("No valid keycode for Key in key binding: %s\n"), key.c_str());
107 return buttonvalue(key, &b.key);
111 static void destroytree(KeyBindingTree *tree)
114 KeyBindingTree *c = tree->first_child;
120 KeyBindingTree *Bindings::buildtree(const StringVect &keylist,
121 PyObject *callback) const
123 if (keylist.empty()) return 0; // nothing in the list.. return 0
125 KeyBindingTree *ret = 0, *p;
127 StringVect::const_reverse_iterator it, end = keylist.rend();
128 for (it = keylist.rbegin(); it != end; ++it) {
130 ret = new KeyBindingTree();
132 // this is the first built node, the bottom node of the tree
134 ret->callbacks.push_back(callback);
136 ret->first_child = p;
137 if (!translate(*it, ret->binding)) {
148 : _curpos(&_keytree),
150 _timer((otk::Timer *) 0)
152 // setResetKey("C-g"); // set the default reset key
156 Bindings::~Bindings()
163 // removeAllButtons(); XXX
168 void Bindings::assimilate(KeyBindingTree *node)
170 KeyBindingTree *a, *b, *tmp, *last;
172 if (!_keytree.first_child) {
173 // there are no nodes at this level yet
174 _keytree.first_child = node;
176 a = _keytree.first_child;
181 if (a->binding != b->binding) {
190 if (last->binding != b->binding)
191 last->next_sibling = b;
193 last->first_child = b->first_child;
200 KeyBindingTree *Bindings::find(KeyBindingTree *search,
201 bool *conflict) const {
203 KeyBindingTree *a, *b;
204 a = _keytree.first_child;
207 if (a->binding != b->binding) {
210 if (a->chain == b->chain) {
212 // found it! (return the actual id, not the search's)
217 return 0; // the chain status' don't match (conflict!)
223 return 0; // it just isn't in here
227 bool Bindings::addKey(const StringVect &keylist, PyObject *callback)
229 KeyBindingTree *tree, *t;
232 if (!(tree = buildtree(keylist, callback)))
233 return false; // invalid binding requested
235 t = find(tree, &conflict);
237 // conflicts with another binding
243 // already bound to something
244 // XXX: look if callback is already bound to this key?
245 t->callbacks.push_back(callback);
248 // grab the server here to make sure no key pressed go missed
249 otk::display->grab();
252 // assimilate this built tree into the main tree
253 assimilate(tree); // assimilation destroys/uses the tree
256 otk::display->ungrab();
265 bool Bindings::removeKey(const StringVect &keylist, PyObject *callback)
267 assert(false); // XXX: function not implemented yet
269 KeyBindingTree *tree;
272 if (!(tree = buildtree(keylist, 0)))
273 return false; // invalid binding requested
275 KeyBindingTree *t = find(tree, &conflict);
277 CallbackList::iterator it = std::find(t->callbacks.begin(),
280 if (it != t->callbacks.end()) {
281 // grab the server here to make sure no key pressed go missed
282 otk::display->grab();
287 // XXX do shit here ...
291 otk::display->ungrab();
300 void Bindings::setResetKey(const std::string &key)
303 if (translate(key, b)) {
304 // grab the server here to make sure no key pressed go missed
305 otk::display->grab();
307 _resetkey.key = b.key;
308 _resetkey.modifiers = b.modifiers;
310 otk::display->ungrab();
315 static void remove_branch(KeyBindingTree *first)
317 KeyBindingTree *p = first;
321 remove_branch(p->first_child);
322 KeyBindingTree *s = p->next_sibling;
323 while(!p->callbacks.empty()) {
324 Py_XDECREF(p->callbacks.front());
325 p->callbacks.pop_front();
333 void Bindings::removeAllKeys()
336 if (_keytree.first_child) {
337 remove_branch(_keytree.first_child);
338 _keytree.first_child = 0;
344 void Bindings::grabKeys(bool grab)
346 for (int i = 0; i < openbox->screenCount(); ++i) {
347 Window root = otk::display->screenInfo(i)->rootWindow();
349 KeyBindingTree *p = _curpos->first_child;
352 otk::display->grabKey(p->binding.key, p->binding.modifiers,
353 root, false, GrabModeAsync, GrabModeAsync,
357 otk::display->ungrabKey(p->binding.key, p->binding.modifiers,
364 otk::display->grabKey(_resetkey.key, _resetkey.modifiers,
365 root, false, GrabModeAsync, GrabModeAsync,
368 otk::display->ungrabKey(_resetkey.key, _resetkey.modifiers,
374 void Bindings::fireKey(int screen, unsigned int modifiers, unsigned int key,
377 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
380 KeyBindingTree *p = _curpos->first_child;
382 if (p->binding.key == key && p->binding.modifiers == modifiers) {
386 _timer = new otk::Timer(5000, // 5 second timeout
387 (otk::Timer::TimeoutHandler)resetChains,
389 // grab the server here to make sure no key pressed go missed
390 otk::display->grab();
394 otk::display->ungrab();
396 Client *c = openbox->focusedClient();
397 KeyData data(screen, c, time, modifiers, key);
398 CallbackList::iterator it, end = p->callbacks.end();
399 for (it = p->callbacks.begin(); it != end; ++it)
400 python_callback(*it, &data);
410 void Bindings::resetChains(Bindings *self)
414 self->_timer = (otk::Timer *) 0;
416 // grab the server here to make sure no key pressed go missed
417 otk::display->grab();
418 self->grabKeys(false);
419 self->_curpos = &self->_keytree;
420 self->grabKeys(true);
421 otk::display->ungrab();
425 bool Bindings::addButton(const std::string &but, MouseContext context,
426 MouseAction action, PyObject *callback)
428 assert(context >= 0 && context < NUM_MOUSE_CONTEXT);
431 if (!translate(but, b, false))
434 ButtonBindingList::iterator it, end = _buttons[context].end();
436 // look for a duplicate binding
437 for (it = _buttons[context].begin(); it != end; ++it)
438 if ((*it)->binding.key == b.key &&
439 (*it)->binding.modifiers == b.modifiers) {
445 // the binding didnt exist yet, add it
447 bind = new ButtonBinding();
448 bind->binding.key = b.key;
449 bind->binding.modifiers = b.modifiers;
450 _buttons[context].push_back(bind);
451 // grab the button on all clients
452 for (int sn = 0; sn < openbox->screenCount(); ++sn) {
453 Screen *s = openbox->screen(sn);
454 Client::List::iterator c_it, c_end = s->clients.end();
455 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
456 grabButton(true, bind->binding, context, *c_it);
461 bind->callbacks[action].push_back(callback);
466 void Bindings::removeAllButtons()
468 for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
469 ButtonBindingList::iterator it, end = _buttons[i].end();
470 for (it = _buttons[i].begin(); it != end; ++it) {
471 for (int a = 0; a < NUM_MOUSE_ACTION; ++a) {
472 while (!(*it)->callbacks[a].empty()) {
473 Py_XDECREF((*it)->callbacks[a].front());
474 (*it)->callbacks[a].pop_front();
477 // ungrab the button on all clients
478 for (int sn = 0; sn < openbox->screenCount(); ++sn) {
479 Screen *s = openbox->screen(sn);
480 Client::List::iterator c_it, c_end = s->clients.end();
481 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
482 grabButton(false, (*it)->binding, (MouseContext)i, *c_it);
489 void Bindings::grabButton(bool grab, const Binding &b, MouseContext context,
493 int mode = GrabModeAsync;
497 win = client->frame->window();
498 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
501 win = client->frame->plate();
502 mode = GrabModeSync; // this is handled in fireButton
503 mask = ButtonPressMask; // can't catch more than this with Sync mode
504 // the release event is manufactured by the
505 // master buttonPressHandler
508 // any other elements already get button events, don't grab on them
512 otk::display->grabButton(b.key, b.modifiers, win, false, mask, mode,
513 GrabModeAsync, None, None, false);
515 otk::display->ungrabButton(b.key, b.modifiers, win);
518 void Bindings::grabButtons(bool grab, Client *client)
520 for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
521 ButtonBindingList::iterator it, end = _buttons[i].end();
522 for (it = _buttons[i].begin(); it != end; ++it)
523 grabButton(grab, (*it)->binding, (MouseContext)i, client);
527 void Bindings::fireButton(MouseData *data)
529 if (data->context == MC_Window) {
530 // Replay the event, so it goes to the client, and ungrab the device.
531 XAllowEvents(**otk::display, ReplayPointer, data->time);
534 ButtonBindingList::iterator it, end = _buttons[data->context].end();
535 for (it = _buttons[data->context].begin(); it != end; ++it)
536 if ((*it)->binding.key == data->button &&
537 (*it)->binding.modifiers == data->state) {
538 CallbackList::iterator c_it,c_end = (*it)->callbacks[data->action].end();
539 for (c_it = (*it)->callbacks[data->action].begin();
540 c_it != c_end; ++c_it)
541 python_callback(*c_it, data);
546 bool Bindings::addEvent(EventAction action, PyObject *callback)
548 if (action < 0 || action >= NUM_EVENTS) {
552 if (action == EventBell && _eventlist[action].empty())
553 XkbSelectEvents(**otk::display, XkbUseCoreKbd,
554 XkbBellNotifyMask, XkbBellNotifyMask);
556 _eventlist[action].push_back(callback);
561 bool Bindings::removeEvent(EventAction action, PyObject *callback)
563 if (action < 0 || action >= NUM_EVENTS) {
567 CallbackList::iterator it = std::find(_eventlist[action].begin(),
568 _eventlist[action].end(),
570 if (it != _eventlist[action].end()) {
572 _eventlist[action].erase(it);
574 if (action == EventBell && _eventlist[action].empty())
575 XkbSelectEvents(**otk::display, XkbUseCoreKbd,
576 XkbBellNotifyMask, 0);
583 void Bindings::removeAllEvents()
585 for (int i = 0; i < NUM_EVENTS; ++i) {
586 while (!_eventlist[i].empty()) {
587 Py_XDECREF(_eventlist[i].front());
588 _eventlist[i].pop_front();
593 void Bindings::fireEvent(EventData *data)
595 CallbackList::iterator c_it, c_end = _eventlist[data->action].end();
596 for (c_it = _eventlist[data->action].begin(); c_it != c_end; ++c_it)
597 python_callback(*c_it, data);