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),
151 _keybgrab_callback(0)
153 // setResetKey("C-g"); // set the default reset key
157 Bindings::~Bindings()
163 //removeAllButtons(); // this is done by each client as they are unmanaged
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 t->callbacks.push_back(callback);
247 // grab the server here to make sure no key pressed go missed
248 otk::display->grab();
251 // assimilate this built tree into the main tree
252 assimilate(tree); // assimilation destroys/uses the tree
255 otk::display->ungrab();
264 bool Bindings::removeKey(const StringVect &keylist, PyObject *callback)
266 assert(false); // XXX: function not implemented yet
268 KeyBindingTree *tree;
271 if (!(tree = buildtree(keylist, 0)))
272 return false; // invalid binding requested
274 KeyBindingTree *t = find(tree, &conflict);
276 CallbackList::iterator it = std::find(t->callbacks.begin(),
279 if (it != t->callbacks.end()) {
280 // grab the server here to make sure no key pressed go missed
281 otk::display->grab();
286 // XXX do shit here ...
290 otk::display->ungrab();
299 void Bindings::setResetKey(const std::string &key)
302 if (translate(key, b)) {
303 // grab the server here to make sure no key pressed go missed
304 otk::display->grab();
306 _resetkey.key = b.key;
307 _resetkey.modifiers = b.modifiers;
309 otk::display->ungrab();
314 static void remove_branch(KeyBindingTree *first)
316 KeyBindingTree *p = first;
320 remove_branch(p->first_child);
321 KeyBindingTree *s = p->next_sibling;
322 while(!p->callbacks.empty()) {
323 Py_XDECREF(p->callbacks.front());
324 p->callbacks.pop_front();
332 void Bindings::removeAllKeys()
335 if (_keytree.first_child) {
336 remove_branch(_keytree.first_child);
337 _keytree.first_child = 0;
343 void Bindings::grabKeys(bool grab)
345 for (int i = 0; i < ScreenCount(**otk::display); ++i) {
346 Screen *sc = openbox->screen(i);
347 if (!sc) continue; // not a managed screen
348 Window root = otk::display->screenInfo(i)->rootWindow();
350 KeyBindingTree *p = _curpos->first_child;
353 otk::display->grabKey(p->binding.key, p->binding.modifiers,
354 root, false, GrabModeAsync, GrabModeAsync,
358 otk::display->ungrabKey(p->binding.key, p->binding.modifiers,
365 otk::display->grabKey(_resetkey.key, _resetkey.modifiers,
366 root, false, GrabModeAsync, GrabModeAsync,
369 otk::display->ungrabKey(_resetkey.key, _resetkey.modifiers,
375 bool Bindings::grabKeyboard(int screen, PyObject *callback)
378 if (_keybgrab_callback) return false; // already grabbed
380 if (!openbox->screen(screen))
381 return false; // the screen is not managed
383 Window root = otk::display->screenInfo(screen)->rootWindow();
384 if (XGrabKeyboard(**otk::display, root, false, GrabModeAsync,
385 GrabModeAsync, CurrentTime))
387 _keybgrab_callback = callback;
392 void Bindings::ungrabKeyboard()
394 if (!_keybgrab_callback) return; // not grabbed
396 _keybgrab_callback = 0;
397 XUngrabKeyboard(**otk::display, CurrentTime);
398 XUngrabPointer(**otk::display, CurrentTime);
402 bool Bindings::grabPointer(int screen)
404 if (!openbox->screen(screen))
405 return false; // the screen is not managed
407 Window root = otk::display->screenInfo(screen)->rootWindow();
408 XGrabPointer(**otk::display, root, false, 0, GrabModeAsync,
409 GrabModeAsync, None, None, CurrentTime);
414 void Bindings::ungrabPointer()
416 XUngrabPointer(**otk::display, CurrentTime);
420 void Bindings::fireKey(int screen, unsigned int modifiers, unsigned int key,
421 Time time, KeyAction::KA action)
423 if (_keybgrab_callback) {
424 Client *c = openbox->focusedClient();
425 KeyData data(screen, c, time, modifiers, key, action);
426 python_callback(_keybgrab_callback, &data);
429 // KeyRelease events only occur during keyboard grabs
430 if (action == KeyAction::Release) return;
432 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
435 KeyBindingTree *p = _curpos->first_child;
437 if (p->binding.key == key && p->binding.modifiers == modifiers) {
441 _timer = new otk::Timer(5000, // 5 second timeout
442 (otk::Timer::TimeoutHandler)resetChains,
444 // grab the server here to make sure no key presses get missed
445 otk::display->grab();
449 otk::display->ungrab();
451 Client *c = openbox->focusedClient();
452 KeyData data(screen, c, time, modifiers, key, action);
453 CallbackList::iterator it, end = p->callbacks.end();
454 for (it = p->callbacks.begin(); it != end; ++it)
455 python_callback(*it, &data);
465 void Bindings::resetChains(Bindings *self)
469 self->_timer = (otk::Timer *) 0;
471 // grab the server here to make sure no key pressed go missed
472 otk::display->grab();
473 self->grabKeys(false);
474 self->_curpos = &self->_keytree;
475 self->grabKeys(true);
476 otk::display->ungrab();
480 bool Bindings::addButton(const std::string &but, MouseContext::MC context,
481 MouseAction::MA action, PyObject *callback)
483 assert(context >= 0 && context < MouseContext::NUM_MOUSE_CONTEXT);
484 assert(action >= 0 && action < MouseAction::NUM_MOUSE_ACTION);
487 if (!translate(but, b, false))
490 ButtonBindingList::iterator it, end = _buttons[context].end();
492 // look for a duplicate binding
493 for (it = _buttons[context].begin(); it != end; ++it)
494 if ((*it)->binding.key == b.key &&
495 (*it)->binding.modifiers == b.modifiers) {
501 // the binding didnt exist yet, add it
503 bind = new ButtonBinding();
504 bind->binding.key = b.key;
505 bind->binding.modifiers = b.modifiers;
506 _buttons[context].push_back(bind);
507 // grab the button on all clients
508 for (int sn = 0; sn < ScreenCount(**otk::display); ++sn) {
509 Screen *s = openbox->screen(sn);
510 if (!s) continue; // not managed
511 Client::List::iterator c_it, c_end = s->clients.end();
512 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
513 grabButton(true, bind->binding, context, *c_it);
518 bind->callbacks[action].push_back(callback);
523 void Bindings::removeAllButtons()
525 for (int i = 0; i < MouseContext::NUM_MOUSE_CONTEXT; ++i) {
526 ButtonBindingList::iterator it, end = _buttons[i].end();
527 for (it = _buttons[i].begin(); it != end; ++it) {
528 for (int a = 0; a < MouseAction::NUM_MOUSE_ACTION; ++a) {
529 while (!(*it)->callbacks[a].empty()) {
530 Py_XDECREF((*it)->callbacks[a].front());
531 (*it)->callbacks[a].pop_front();
534 // ungrab the button on all clients
535 for (int sn = 0; sn < ScreenCount(**otk::display); ++sn) {
536 Screen *s = openbox->screen(sn);
537 if (!s) continue; // not managed
538 Client::List::iterator c_it, c_end = s->clients.end();
539 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
540 grabButton(false, (*it)->binding, (MouseContext::MC)i, *c_it);
547 void Bindings::grabButton(bool grab, const Binding &b,
548 MouseContext::MC context, Client *client)
551 int mode = GrabModeAsync;
554 case MouseContext::Frame:
555 win = client->frame->window();
556 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
558 case MouseContext::Window:
559 win = client->frame->plate();
560 mode = GrabModeSync; // this is handled in fireButton
561 mask = ButtonPressMask; // can't catch more than this with Sync mode
562 // the release event is manufactured by the
563 // master buttonPressHandler
566 // any other elements already get button events, don't grab on them
570 otk::display->grabButton(b.key, b.modifiers, win, false, mask, mode,
571 GrabModeAsync, None, None, false);
573 otk::display->ungrabButton(b.key, b.modifiers, win);
576 void Bindings::grabButtons(bool grab, Client *client)
578 for (int i = 0; i < MouseContext::NUM_MOUSE_CONTEXT; ++i) {
579 ButtonBindingList::iterator it, end = _buttons[i].end();
580 for (it = _buttons[i].begin(); it != end; ++it)
581 grabButton(grab, (*it)->binding, (MouseContext::MC)i, client);
585 void Bindings::fireButton(MouseData *data)
587 if (data->context == MouseContext::Window) {
588 // Replay the event, so it goes to the client
589 XAllowEvents(**otk::display, ReplayPointer, data->time);
592 ButtonBindingList::iterator it, end = _buttons[data->context].end();
593 for (it = _buttons[data->context].begin(); it != end; ++it)
594 if ((*it)->binding.key == data->button &&
595 (*it)->binding.modifiers == data->state) {
596 CallbackList::iterator c_it,c_end = (*it)->callbacks[data->action].end();
597 for (c_it = (*it)->callbacks[data->action].begin();
598 c_it != c_end; ++c_it)
599 python_callback(*c_it, data);
604 bool Bindings::addEvent(EventAction::EA action, PyObject *callback)
606 if (action < 0 || action >= EventAction::NUM_EVENTS) {
610 if (action == EventAction::Bell && _eventlist[action].empty())
611 XkbSelectEvents(**otk::display, XkbUseCoreKbd,
612 XkbBellNotifyMask, XkbBellNotifyMask);
614 _eventlist[action].push_back(callback);
619 bool Bindings::removeEvent(EventAction::EA action, PyObject *callback)
621 if (action < 0 || action >= EventAction::NUM_EVENTS) {
625 CallbackList::iterator it = std::find(_eventlist[action].begin(),
626 _eventlist[action].end(),
628 if (it != _eventlist[action].end()) {
630 _eventlist[action].erase(it);
632 if (action == EventAction::Bell && _eventlist[action].empty())
633 XkbSelectEvents(**otk::display, XkbUseCoreKbd,
634 XkbBellNotifyMask, 0);
641 void Bindings::removeAllEvents()
643 for (int i = 0; i < EventAction::NUM_EVENTS; ++i) {
644 while (!_eventlist[i].empty()) {
645 Py_XDECREF(_eventlist[i].front());
646 _eventlist[i].pop_front();
651 void Bindings::fireEvent(EventData *data)
653 CallbackList::iterator c_it, c_end = _eventlist[data->action].end();
654 for (c_it = _eventlist[data->action].begin(); c_it != c_end; ++c_it)
655 python_callback(*c_it, data);