+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);
+ }
+}
+
+
+bool Bindings::addButton(const std::string &but, MouseContext::MC context,
+ MouseAction::MA action, MouseCallback callback,
+ void *data)
+{
+ assert(context >= 0 && context < MouseContext::NUM_MOUSE_CONTEXT);
+ assert(action >= 0 && action < MouseAction::NUM_MOUSE_ACTION);
+
+ 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) {
+ break;
+ }
+
+ ButtonBinding *bind;
+
+ // the binding didnt exist yet, add it
+ if (it == end) {
+ bind = new ButtonBinding();
+ bind->binding.key = b.key;
+ bind->binding.modifiers = b.modifiers;
+ _buttons[context].push_back(bind);
+ // grab the button on all clients
+ for (int sn = 0; sn < ScreenCount(**otk::display); ++sn) {
+ Screen *s = openbox->screen(sn);
+ if (!s) continue; // not managed
+ Client::List::iterator c_it, c_end = s->clients.end();
+ for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
+ grabButton(true, bind->binding, context, *c_it);
+ }
+ }
+ } else
+ bind = *it;
+ bind->callbacks[action].push_back(MouseCallbackData(callback, data));
+ return true;
+}
+
+void Bindings::removeAllButtons()
+{
+ for (int i = 0; i < MouseContext::NUM_MOUSE_CONTEXT; ++i) {
+ ButtonBindingList::iterator it, end = _buttons[i].end();
+ for (it = _buttons[i].begin(); it != end; ++it) {
+ for (int a = 0; a < MouseAction::NUM_MOUSE_ACTION; ++a) {
+ while (!(*it)->callbacks[a].empty()) {
+ (*it)->callbacks[a].pop_front();
+ }
+ }
+ // ungrab the button on all clients
+ for (int sn = 0; sn < ScreenCount(**otk::display); ++sn) {
+ Screen *s = openbox->screen(sn);
+ if (!s) continue; // not managed
+ Client::List::iterator c_it, c_end = s->clients.end();
+ for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
+ grabButton(false, (*it)->binding, (MouseContext::MC)i, *c_it);
+ }
+ }
+ }
+ }
+}
+
+void Bindings::grabButton(bool grab, const Binding &b,
+ MouseContext::MC context, Client *client)
+{
+ Window win;
+ int mode = GrabModeAsync;
+ unsigned int mask;
+ switch(context) {
+ case MouseContext::Frame:
+ win = client->frame->window();
+ mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
+ break;
+ case MouseContext::Window:
+ win = client->frame->plate();
+ mode = GrabModeSync; // this is handled in fireButton
+ mask = ButtonPressMask; // can't catch more than this with Sync mode
+ // the release event is manufactured by the
+ // master buttonPressHandler
+ break;
+ default:
+ // any other elements already get button events, don't grab on them
+ return;
+ }
+ if (grab)
+ otk::display->grabButton(b.key, b.modifiers, win, false, mask, mode,
+ GrabModeAsync, None, None, false);
+ else
+ otk::display->ungrabButton(b.key, b.modifiers, win);
+}
+
+void Bindings::grabButtons(bool grab, Client *client)
+{
+ for (int i = 0; i < MouseContext::NUM_MOUSE_CONTEXT; ++i) {
+ ButtonBindingList::iterator it, end = _buttons[i].end();
+ for (it = _buttons[i].begin(); it != end; ++it)
+ grabButton(grab, (*it)->binding, (MouseContext::MC)i, client);
+ }
+}
+
+void Bindings::fireButton(MouseData *data)
+{
+ if (data->context == MouseContext::Window) {
+ // Replay the event, so it goes to the client
+ XAllowEvents(**otk::display, ReplayPointer, data->time);
+ }
+
+ ButtonBindingList::iterator it, end = _buttons[data->context].end();
+ for (it = _buttons[data->context].begin(); it != end; ++it)
+ if ((*it)->binding.key == data->button &&
+ (*it)->binding.modifiers == data->state) {
+ MouseCallbackList::iterator c_it,
+ c_end = (*it)->callbacks[data->action].end();
+ for (c_it = (*it)->callbacks[data->action].begin();
+ c_it != c_end; ++c_it)
+ c_it->fire(data);
+ }
+}
+
+
+bool Bindings::addEvent(EventAction::EA action, EventCallback callback,
+ void *data)
+{
+ if (action < 0 || action >= EventAction::NUM_EVENT_ACTION) {
+ return false;
+ }
+#ifdef XKB
+ if (action == EventAction::Bell && _eventlist[action].empty())
+ XkbSelectEvents(**otk::display, XkbUseCoreKbd,
+ XkbBellNotifyMask, XkbBellNotifyMask);
+#endif // XKB
+ _eventlist[action].push_back(EventCallbackData(callback, data));
+ return true;
+}
+
+bool Bindings::removeEvent(EventAction::EA action, EventCallback callback,
+ void *data)
+{
+ if (action < 0 || action >= EventAction::NUM_EVENT_ACTION) {
+ return false;
+ }
+
+ EventCallbackList::iterator it = std::find(_eventlist[action].begin(),
+ _eventlist[action].end(),
+ EventCallbackData(callback,
+ data));
+ if (it != _eventlist[action].end()) {
+ _eventlist[action].erase(it);
+#ifdef XKB
+ if (action == EventAction::Bell && _eventlist[action].empty())
+ XkbSelectEvents(**otk::display, XkbUseCoreKbd,
+ XkbBellNotifyMask, 0);
+#endif // XKB
+ return true;
+ }
+ return false;
+}
+
+void Bindings::removeAllEvents()
+{
+ for (int i = 0; i < EventAction::NUM_EVENT_ACTION; ++i) {
+ while (!_eventlist[i].empty()) {
+ _eventlist[i].pop_front();
+ }
+ }
+}
+
+void Bindings::fireEvent(EventData *data)