+
+ 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;