+ if (!openbox->screen(screen))
+ return false; // the screen is not managed
+
+ Window root = otk::display->screenInfo(screen)->rootWindow();
+ if (XGrabKeyboard(**otk::display, root, false, GrabModeAsync,
+ GrabModeAsync, CurrentTime))
+ return false;
+ // the pointer grab causes pointer events during the keyboard grab to go away
+ XGrabPointer(**otk::display, root, false, 0, GrabModeAsync,
+ GrabModeAsync, None, None, CurrentTime);
+ _keybgrab_callback = callback;
+ return true;
+}
+
+
+void Bindings::ungrabKeyboard()
+{
+ if (!_keybgrab_callback) return; // not grabbed
+
+ _keybgrab_callback = 0;
+ XUngrabKeyboard(**otk::display, CurrentTime);
+ XUngrabPointer(**otk::display, CurrentTime);
+}
+
+
+void Bindings::fireKey(int screen, unsigned int modifiers, unsigned int key,
+ Time time, KeyAction::KA action)
+{
+ if (_keybgrab_callback) {
+ Client *c = openbox->focusedClient();
+ KeyData data(screen, c, time, modifiers, key, action);
+ python_callback(_keybgrab_callback, &data);
+ }
+
+ // KeyRelease events only occur during keyboard grabs
+ if (action == KeyAction::Release) return;
+
+ if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
+ resetChains(this);
+ } else {
+ KeyBindingTree *p = _curpos->first_child;
+ while (p) {
+ if (p->binding.key == key && p->binding.modifiers == modifiers) {
+ if (p->chain) {
+ if (_timer)
+ delete _timer;
+ _timer = new otk::Timer(5000, // 5 second timeout
+ (otk::Timer::TimeoutHandler)resetChains,
+ this);
+ // grab the server here to make sure no key pressed go missed
+ otk::display->grab();
+ grabKeys(false);
+ _curpos = p;
+ grabKeys(true);
+ otk::display->ungrab();
+ } else {
+ Client *c = openbox->focusedClient();
+ KeyData data(screen, c, time, modifiers, key, action);
+ CallbackList::iterator it, end = p->callbacks.end();
+ for (it = p->callbacks.begin(); it != end; ++it)
+ python_callback(*it, &data);
+ resetChains(this);
+ }
+ break;
+ }
+ p = p->next_sibling;
+ }
+ }
+}
+
+void Bindings::resetChains(Bindings *self)
+{
+ if (self->_timer) {
+ delete self->_timer;
+ self->_timer = (otk::Timer *) 0;
+ }
+ // grab the server here to make sure no key pressed go missed
+ otk::display->grab();
+ self->grabKeys(false);
+ self->_curpos = &self->_keytree;
+ self->grabKeys(true);
+ otk::display->ungrab();
+}
+
+
+bool Bindings::addButton(const std::string &but, MouseContext::MC context,
+ MouseAction::MA action, PyObject *callback)
+{
+ assert(context >= 0 && context < MouseContext::NUM_MOUSE_CONTEXT);
+
+ Binding b(0,0);
+ if (!translate(but, b, false))
+ return false;
+
+ ButtonBindingList::iterator it, end = _buttons[context].end();
+
+ // look for a duplicate binding
+ for (it = _buttons[context].begin(); it != end; ++it)
+ if ((*it)->binding.key == b.key &&
+ (*it)->binding.modifiers == b.modifiers) {