+ grabKeys(true);
+}
+
+
+void Bindings::grabKeys(bool grab)
+{
+ for (int i = 0; i < ScreenCount(**otk::display); ++i) {
+ Screen *sc = openbox->screen(i);
+ if (!sc) continue; // not a managed screen
+ Window root = otk::display->screenInfo(i)->rootWindow();
+ if (!grab) {
+ otk::display->ungrabAllKeys(root);
+ continue;
+ }
+ KeyBindingTree *p = _keytree.first_child;
+ while (p) {
+ otk::display->grabKey(p->binding.key, p->binding.modifiers,
+ root, false, GrabModeAsync, GrabModeSync,
+ false);
+ p = p->next_sibling;
+ }
+ }
+}
+
+
+bool Bindings::grabKeyboard(int screen, KeyCallback callback, void *data)
+{
+ assert(callback);
+ if (_keybgrab_callback.callback) return false; // already grabbed
+
+ 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;
+ _keybgrab_callback.callback = callback;
+ _keybgrab_callback.data = data;
+ return true;
+}
+
+
+void Bindings::ungrabKeyboard()
+{
+ if (!_keybgrab_callback.callback) return; // not grabbed
+
+ _keybgrab_callback = KeyCallbackData(0, 0);
+ if (!_grabbed) /* don't release out from under keychains */
+ XUngrabKeyboard(**otk::display, CurrentTime);
+ XUngrabPointer(**otk::display, CurrentTime);
+}
+
+
+bool Bindings::grabPointer(int screen)
+{
+ if (!openbox->screen(screen))
+ return false; // the screen is not managed
+
+ Window root = otk::display->screenInfo(screen)->rootWindow();
+ XGrabPointer(**otk::display, root, false, 0, GrabModeAsync,
+ GrabModeAsync, None, None, CurrentTime);
+ return true;
+}
+
+
+void Bindings::ungrabPointer()
+{
+ XUngrabPointer(**otk::display, CurrentTime);
+}
+
+
+void Bindings::fireKey(int screen, unsigned int modifiers, unsigned int key,
+ Time time, KeyAction::KA action)
+{
+ if (_keybgrab_callback.callback) {
+ Client *c = openbox->focusedClient();
+ KeyData data(screen, c, time, modifiers, key, action);
+ _keybgrab_callback.fire(&data);
+ }
+
+ // KeyRelease events only occur during keyboard grabs
+ if (action == KeyAction::Release) return;
+
+ if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
+ resetChains(this);
+ XAllowEvents(**otk::display, AsyncKeyboard, CurrentTime);
+ } 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);
+ if (!_grabbed && !_keybgrab_callback.callback) {
+ Window root = otk::display->screenInfo(screen)->rootWindow();
+ //grab should never fail because we should have a sync grab at
+ //this point
+ XGrabKeyboard(**otk::display, root, 0, GrabModeAsync,
+ GrabModeSync, CurrentTime);
+ }
+ _grabbed = true;
+ _curpos = p;
+ XAllowEvents(**otk::display, AsyncKeyboard, CurrentTime);
+ } else {
+ Client *c = openbox->focusedClient();
+ KeyData data(screen, c, time, modifiers, key, action);
+ KeyCallbackList::iterator it, end = p->callbacks.end();
+ for (it = p->callbacks.begin(); it != end; ++it)
+ it->fire(&data);
+ XAllowEvents(**otk::display, AsyncKeyboard, CurrentTime);
+ resetChains(this);
+ }
+ break;
+ }
+ p = p->next_sibling;
+ }
+ }
+}
+
+void Bindings::resetChains(Bindings *self)
+{
+ if (self->_timer) {
+ delete self->_timer;
+ self->_timer = (otk::Timer *) 0;
+ }
+ self->_curpos = &self->_keytree;
+ if (self->_grabbed) {
+ self->_grabbed = false;
+ if (!self->_keybgrab_callback.callback)
+ XUngrabKeyboard(**otk::display, CurrentTime);