+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)
+{
+ EventCallbackList::iterator c_it, c_end = _eventlist[data->action].end();
+ for (c_it = _eventlist[data->action].begin(); c_it != c_end; ++c_it)
+ c_it->fire(data);
+}