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(callback);
129 if (!p) ret->chain = false; // only the first built node
130 ret->first_child = p;
131 if (!translate(*it, ret->binding)) {
141 OBBindings::OBBindings()
142 : _curpos(&_keytree),
144 _timer(Openbox::instance->timerManager(),
145 (otk::OBTimeoutHandler)reset, this)
147 _timer.setTimeout(5000); // chains reset after 5 seconds
149 setResetKey("C-g"); // set the default reset key
153 OBBindings::~OBBindings()
160 void OBBindings::assimilate(KeyBindingTree *node)
162 KeyBindingTree *a, *b, *tmp, *last;
164 if (!_keytree.first_child) {
165 // there are no nodes at this level yet
166 _keytree.first_child = node;
168 a = _keytree.first_child;
173 if (a->binding != b->binding) {
182 if (last->binding != b->binding)
183 last->next_sibling = b;
185 last->first_child = b->first_child;
192 PyObject *OBBindings::find(KeyBindingTree *search, bool *conflict) const {
194 KeyBindingTree *a, *b;
195 a = _keytree.first_child;
198 if (a->binding != b->binding) {
201 if (a->chain == b->chain) {
203 // found it! (return the actual id, not the search's)
208 return 0; // the chain status' don't match (conflict!)
214 return 0; // it just isn't in here
218 bool OBBindings::add(const StringVect &keylist, PyObject *callback)
220 KeyBindingTree *tree;
223 if (!(tree = buildtree(keylist, callback)))
224 return false; // invalid binding requested
226 if (find(tree, &conflict) || conflict) {
227 // conflicts with another binding
234 // assimilate this built tree into the main tree
235 assimilate(tree); // assimilation destroys/uses the tree
245 bool OBBindings::remove(const StringVect &keylist)
247 assert(false); // XXX: function not implemented yet
249 KeyBindingTree *tree;
252 if (!(tree = buildtree(keylist, 0)))
253 return false; // invalid binding requested
255 PyObject *func = find(tree, &conflict);
261 // XXX do shit here ...
271 void OBBindings::setResetKey(const std::string &key)
274 if (translate(key, b)) {
276 _resetkey.key = b.key;
277 _resetkey.modifiers = b.modifiers;
283 static void remove_branch(KeyBindingTree *first)
285 KeyBindingTree *p = first;
289 remove_branch(p->first_child);
290 KeyBindingTree *s = p->next_sibling;
291 Py_XDECREF(p->callback);
298 void OBBindings::removeAll()
300 if (_keytree.first_child) {
301 remove_branch(_keytree.first_child);
302 _keytree.first_child = 0;
307 void OBBindings::grabKeys(bool grab)
309 for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
310 Window root = otk::OBDisplay::screenInfo(i)->rootWindow();
312 KeyBindingTree *p = _curpos->first_child;
315 otk::OBDisplay::grabKey(p->binding.key, p->binding.modifiers,
316 root, false, GrabModeAsync, GrabModeAsync,
320 otk::OBDisplay::ungrabKey(p->binding.key, p->binding.modifiers,
326 otk::OBDisplay::grabKey(_resetkey.key, _resetkey.modifiers,
327 root, true, GrabModeAsync, GrabModeAsync,
330 otk::OBDisplay::ungrabKey(_resetkey.key, _resetkey.modifiers,
336 void OBBindings::fire(unsigned int modifiers, unsigned int key, Time time)
338 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
341 KeyBindingTree *p = _curpos->first_child;
343 if (p->binding.key == key && p->binding.modifiers == modifiers) {
345 _timer.start(); // start/restart the timer
351 OBClient *c = Openbox::instance->focusedClient();
352 if (c) win = c->window();
353 python_callback(p->callback, win, modifiers, key, time);
363 void OBBindings::reset(OBBindings *self)
366 self->grabKeys(false);
367 self->_curpos = &self->_keytree;
368 self->grabKeys(true);
372 bool OBBindings::addButton(const std::string &but, MouseContext context,
373 MouseAction action, PyObject *callback)
375 assert(context >= 0 && context < NUM_MOUSE_CONTEXT);
378 if (!translate(but, b, false))
381 ButtonBindingList::iterator it, end = _buttons[context].end();
383 // look for a duplicate binding
384 for (it = _buttons[context].begin(); it != end; ++it)
385 if ((*it)->binding.key == b.key &&
386 (*it)->binding.modifiers == b.modifiers) {
387 ButtonBinding::CallbackList::iterator c_it,
388 c_end = (*it)->callback[action].end();
389 for (c_it = (*it)->callback[action].begin(); c_it != c_end; ++c_it)
390 if (*c_it == callback)
391 return true; // already bound
397 // the binding didnt exist yet, add it
399 bind = new ButtonBinding();
400 bind->binding.key = b.key;
401 bind->binding.modifiers = b.modifiers;
402 _buttons[context].push_back(bind);
403 printf("adding %d.%d to %d\n", b.key, b.modifiers, context);
404 // XXX GRAB the new button everywhere!
407 bind->callback[action].push_back(callback);
412 void OBBindings::grabButtons(bool grab, OBClient *client)
414 for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
415 Window win[3] = {0, 0, 0}; // at most 2 windows
418 win[0] = client->frame->window();
421 win[0] = client->frame->titlebar();
422 win[1] = client->frame->label();
425 win[0] = client->frame->plate();
427 case MC_MaximizeButton:
428 // win[0] = client->frame->button_max();
431 // win[0] = client->frame->button_close();
433 case MC_IconifyButton:
434 // win[0] = client->frame->button_iconify();
436 case MC_StickyButton:
437 // win[0] = client->frame->button_stick();
440 // win[0] = client->frame->grip_left();
441 // win[1] = client->frame->grip_right();
444 // win[0] = otk::OBDisplay::screenInfo(client->screen())->rootWindow();
449 assert(false); // invalid mouse context
451 ButtonBindingList::iterator it, end = _buttons[i].end();
452 for (it = _buttons[i].begin(); it != end; ++it)
453 for (Window *w = win; *w; ++w) {
455 otk::OBDisplay::grabButton((*it)->binding.key,
456 (*it)->binding.modifiers, *w, false,
457 ButtonPressMask | ButtonMotionMask |
458 ButtonReleaseMask, GrabModeAsync,
459 GrabModeAsync, None, None, false);
462 otk::OBDisplay::ungrabButton((*it)->binding.key,
463 (*it)->binding.modifiers, *w);
468 void OBBindings::fire(MouseAction action, OBWidget::WidgetType type,
469 Window win, unsigned int modifiers, unsigned int button,
470 int xroot, int yroot, Time time)
472 assert(action >= 0 && action < NUM_MOUSE_ACTION);
474 MouseContext context;
476 case OBWidget::Type_Frame:
477 context = MC_Frame; break;
478 case OBWidget::Type_Titlebar:
479 context = MC_Titlebar; break;
480 case OBWidget::Type_Handle:
481 context = MC_Frame; break;
482 case OBWidget::Type_Plate:
483 context = MC_Window; break;
484 case OBWidget::Type_Label:
485 context = MC_Titlebar; break;
486 case OBWidget::Type_MaximizeButton:
487 context = MC_MaximizeButton; break;
488 case OBWidget::Type_CloseButton:
489 context = MC_CloseButton; break;
490 case OBWidget::Type_IconifyButton:
491 context = MC_IconifyButton; break;
492 case OBWidget::Type_StickyButton:
493 context = MC_StickyButton; break;
494 case OBWidget::Type_LeftGrip:
495 context = MC_Grip; break;
496 case OBWidget::Type_RightGrip:
497 context = MC_Grip; break;
498 case OBWidget::Type_Client:
499 context = MC_Window; break;
500 case OBWidget::Type_Root:
501 context = MC_Root; break;
503 assert(false); // unhandled type
506 modifiers &= (ControlMask | ShiftMask | Mod1Mask | Mod2Mask | Mod3Mask |
507 Mod4Mask | Mod5Mask);
509 printf("but.mods %d.%d\n", button, modifiers);
511 ButtonBindingList::iterator it, end = _buttons[context].end();
512 for (it = _buttons[context].begin(); it != end; ++it)
513 if ((*it)->binding.key == button &&
514 (*it)->binding.modifiers == modifiers) {
515 ButtonBinding::CallbackList::iterator c_it,
516 c_end = (*it)->callback[action].end();
517 for (c_it = (*it)->callback[action].begin(); c_it != c_end; ++c_it)
518 python_callback(*c_it, action, win, context, modifiers,
519 button, xroot, yroot, time);