+ 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();
+
+ KeyBindingTree *p = _curpos->first_child;
+ while (p) {
+ if (grab) {
+ otk::display->grabKey(p->binding.key, p->binding.modifiers,
+ root, false, GrabModeAsync, GrabModeAsync,
+ false);
+ }
+ else
+ otk::display->ungrabKey(p->binding.key, p->binding.modifiers,
+ root);
+ p = p->next_sibling;
+ }
+
+ if (_resetkey.key)
+ if (grab)
+ otk::display->grabKey(_resetkey.key, _resetkey.modifiers,
+ root, false, GrabModeAsync, GrabModeAsync,
+ false);
+ else
+ otk::display->ungrabKey(_resetkey.key, _resetkey.modifiers,
+ root);
+ }
+}
+
+
+bool Bindings::grabKeyboard(int screen, PyObject *callback)
+{
+ assert(callback);
+ if (_keybgrab_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;
+ // 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;
+ }