123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- /* Copyright 2019 Aaron Sowry for Cendio AB
- * Copyright 2019-2020 Pierre Ossman for Cendio AB
- *
- * This is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this software; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
- * USA.
- */
-
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
-
- #include <assert.h>
- #include <string.h>
-
- #include <X11/extensions/XInput2.h>
- #include <X11/extensions/XI2.h>
- #include <X11/XKBlib.h>
-
- #include <FL/x.H>
-
- #ifndef XK_MISCELLANY
- #define XK_MISCELLANY
- #include <rfb/keysymdef.h>
- #endif
- #include <rfb/LogWriter.h>
-
- #include "i18n.h"
- #include "XInputTouchHandler.h"
-
- static rfb::LogWriter vlog("XInputTouchHandler");
-
- static bool grabbed = false;
-
- XInputTouchHandler::XInputTouchHandler(Window wnd)
- : wnd(wnd), fakeStateMask(0)
- {
- XIEventMask eventmask;
- unsigned char flags[XIMaskLen(XI_LASTEVENT)] = { 0 };
-
- // Event delivery is broken when somebody else does a pointer grab,
- // so we need to listen to all devices and do filtering of master
- // devices manually
- eventmask.deviceid = XIAllDevices;
- eventmask.mask_len = sizeof(flags);
- eventmask.mask = flags;
-
- XISetMask(flags, XI_ButtonPress);
- XISetMask(flags, XI_Motion);
- XISetMask(flags, XI_ButtonRelease);
-
- XISetMask(flags, XI_TouchBegin);
- XISetMask(flags, XI_TouchUpdate);
- XISetMask(flags, XI_TouchEnd);
-
- // If something has a passive grab of touches (e.g. the window
- // manager wants to have its own gestures) then we won't get the
- // touch events until everyone who has a grab has indicated they
- // don't want these touches (via XIAllowTouchEvents()).
- // Unfortunately the touches are then replayed one touch point at
- // a time, meaning things will be delayed and out of order,
- // completely screwing up our gesture detection. Listening for
- // XI_TouchOwnership has the effect of giving us the touch events
- // right away, even if grabbing clients are also getting them.
- //
- // FIXME: We should really wait for the XI_TouchOwnership event
- // before it is safe to react to the gesture, otherwise we
- // might react to something that the window manager will
- // also react to.
- //
- if (!grabbed)
- XISetMask(flags, XI_TouchOwnership);
-
- XISelectEvents(fl_display, wnd, &eventmask, 1);
- }
-
- bool XInputTouchHandler::grabPointer()
- {
- XIEventMask *curmasks;
- int num_masks;
-
- int ret, ndevices;
-
- XIDeviceInfo *devices, *device;
- bool gotGrab;
-
- // We grab for the same events as the window is currently interested in
- curmasks = XIGetSelectedEvents(fl_display, wnd, &num_masks);
- if (curmasks == NULL) {
- if (num_masks == -1)
- vlog.error(_("Unable to get X Input 2 event mask for window 0x%08lx"), wnd);
- else
- vlog.error(_("Window 0x%08lx has no X Input 2 event mask"), wnd);
-
- return false;
- }
-
- // Our windows should only have a single mask, which allows us to
- // simplify all the code handling the masks
- if (num_masks > 1) {
- vlog.error(_("Window 0x%08lx has more than one X Input 2 event mask"), wnd);
- return false;
- }
-
- devices = XIQueryDevice(fl_display, XIAllMasterDevices, &ndevices);
-
- // Iterate through available devices to find those which
- // provide pointer input, and attempt to grab all such devices.
- gotGrab = false;
- for (int i = 0; i < ndevices; i++) {
- device = &devices[i];
-
- if (device->use != XIMasterPointer)
- continue;
-
- curmasks[0].deviceid = device->deviceid;
-
- ret = XIGrabDevice(fl_display,
- device->deviceid,
- wnd,
- CurrentTime,
- None,
- XIGrabModeAsync,
- XIGrabModeAsync,
- True,
- &(curmasks[0]));
-
- if (ret) {
- if (ret == XIAlreadyGrabbed) {
- continue;
- } else {
- vlog.error(_("Failure grabbing device %i"), device->deviceid);
- continue;
- }
- }
-
- gotGrab = true;
- }
-
- XIFreeDeviceInfo(devices);
-
- // Did we not even grab a single device?
- if (!gotGrab)
- return false;
-
- grabbed = true;
-
- return true;
- }
-
- void XInputTouchHandler::ungrabPointer()
- {
- int ndevices;
- XIDeviceInfo *devices, *device;
-
- devices = XIQueryDevice(fl_display, XIAllMasterDevices, &ndevices);
-
- // Release all devices, hoping they are the same as when we
- // grabbed things
- for (int i = 0; i < ndevices; i++) {
- device = &devices[i];
-
- if (device->use != XIMasterPointer)
- continue;
-
- XIUngrabDevice(fl_display, device->deviceid, CurrentTime);
- }
-
- XIFreeDeviceInfo(devices);
-
- grabbed = false;
- }
-
- void XInputTouchHandler::processEvent(const XIDeviceEvent* devev)
- {
- // FLTK doesn't understand X Input events, and we've stopped
- // delivery of Core events by enabling the X Input ones. Make
- // FLTK happy by faking Core events based on the X Input ones.
-
- bool isMaster = devev->deviceid != devev->sourceid;
-
- // We're only interested in events from master devices
- if (!isMaster) {
- // However we need to accept TouchEnd from slave devices as they
- // might not get delivered if there is a pointer grab, see:
- // https://gitlab.freedesktop.org/xorg/xserver/-/issues/1016
- if (devev->evtype != XI_TouchEnd)
- return;
- }
-
- // Avoid duplicate TouchEnd events, see above
- // FIXME: Doesn't handle floating slave devices
- if (isMaster && devev->evtype == XI_TouchEnd)
- return;
-
- if (devev->flags & XIPointerEmulated) {
- // We still want the server to do the scroll wheel to button thing
- // though, so keep those
- if (((devev->evtype == XI_ButtonPress) ||
- (devev->evtype == XI_ButtonRelease)) &&
- (devev->detail >= 4) && (devev->detail <= 7)) {
- ;
- } else {
- // Sometimes the server incorrectly sends us various events with
- // this flag set, see:
- // https://gitlab.freedesktop.org/xorg/xserver/-/issues/1027
- return;
- }
- }
-
- switch (devev->evtype) {
- case XI_Enter:
- case XI_Leave:
- // We get these when the mouse is grabbed implicitly, so just ignore them
- // https://gitlab.freedesktop.org/xorg/xserver/-/issues/1026
- break;
- case XI_Motion:
- // FIXME: We also get XI_Motion for scroll wheel events, which
- // we might want to ignore
- fakeMotionEvent(devev);
- break;
- case XI_ButtonPress:
- fakeButtonEvent(true, devev->detail, devev);
- break;
- case XI_ButtonRelease:
- fakeButtonEvent(false, devev->detail, devev);
- break;
- case XI_TouchBegin:
- // XInput2 wants us to explicitly accept touch sequences
- // for grabbed devices before it will pass events
- if (grabbed) {
- XIAllowTouchEvents(fl_display,
- devev->deviceid,
- devev->detail,
- devev->event,
- XIAcceptTouch);
- }
-
- handleTouchBegin(devev->detail, devev->event_x, devev->event_y);
- break;
- case XI_TouchUpdate:
- handleTouchUpdate(devev->detail, devev->event_x, devev->event_y);
- break;
- case XI_TouchEnd:
- handleTouchEnd(devev->detail);
- break;
- case XI_TouchOwnership:
- // FIXME: Currently ignored, see constructor
- break;
- }
- }
-
- void XInputTouchHandler::preparePointerEvent(XEvent* dst, const XIDeviceEvent* src)
- {
- // XButtonEvent and XMotionEvent are almost identical, so we
- // don't have to care which it is for these fields
- dst->xbutton.serial = src->serial;
- dst->xbutton.display = src->display;
- dst->xbutton.window = src->event;
- dst->xbutton.root = src->root;
- dst->xbutton.subwindow = src->child;
- dst->xbutton.time = src->time;
- dst->xbutton.x = src->event_x;
- dst->xbutton.y = src->event_y;
- dst->xbutton.x_root = src->root_x;
- dst->xbutton.y_root = src->root_y;
- dst->xbutton.state = src->mods.effective;
- dst->xbutton.state |= ((src->buttons.mask[0] >> 1) & 0x1f) << 8;
- dst->xbutton.same_screen = True;
- }
-
- void XInputTouchHandler::fakeMotionEvent(const XIDeviceEvent* origEvent)
- {
- XEvent fakeEvent;
-
- memset(&fakeEvent, 0, sizeof(XEvent));
-
- fakeEvent.type = MotionNotify;
- fakeEvent.xmotion.is_hint = False;
- preparePointerEvent(&fakeEvent, origEvent);
-
- pushFakeEvent(&fakeEvent);
- }
-
- void XInputTouchHandler::fakeButtonEvent(bool press, int button,
- const XIDeviceEvent* origEvent)
- {
- XEvent fakeEvent;
-
- memset(&fakeEvent, 0, sizeof(XEvent));
-
- fakeEvent.type = press ? ButtonPress : ButtonRelease;
- fakeEvent.xbutton.button = button;
-
- // Apply the fake mask before pushing so it will be in sync
- fakeEvent.xbutton.state |= fakeStateMask;
- preparePointerEvent(&fakeEvent, origEvent);
-
- pushFakeEvent(&fakeEvent);
- }
-
- void XInputTouchHandler::preparePointerEvent(XEvent* dst, const GestureEvent src)
- {
- Window root, child;
- int rootX, rootY;
- XkbStateRec state;
-
- // We don't have a real event to steal things from, so we'll have
- // to fake these events based on the current state of things
-
- root = XDefaultRootWindow(fl_display);
- XTranslateCoordinates(fl_display, wnd, root,
- src.eventX,
- src.eventY,
- &rootX, &rootY, &child);
- XkbGetState(fl_display, XkbUseCoreKbd, &state);
-
- // XButtonEvent and XMotionEvent are almost identical, so we
- // don't have to care which it is for these fields
- dst->xbutton.serial = XLastKnownRequestProcessed(fl_display);
- dst->xbutton.display = fl_display;
- dst->xbutton.window = wnd;
- dst->xbutton.root = root;
- dst->xbutton.subwindow = None;
- dst->xbutton.time = fl_event_time;
- dst->xbutton.x = src.eventX;
- dst->xbutton.y = src.eventY;
- dst->xbutton.x_root = rootX;
- dst->xbutton.y_root = rootY;
- dst->xbutton.state = state.mods;
- dst->xbutton.state |= ((state.ptr_buttons >> 1) & 0x1f) << 8;
- dst->xbutton.same_screen = True;
- }
-
- void XInputTouchHandler::fakeMotionEvent(const GestureEvent origEvent)
- {
- XEvent fakeEvent;
-
- memset(&fakeEvent, 0, sizeof(XEvent));
-
- fakeEvent.type = MotionNotify;
- fakeEvent.xmotion.is_hint = False;
- preparePointerEvent(&fakeEvent, origEvent);
-
- fakeEvent.xbutton.state |= fakeStateMask;
-
- pushFakeEvent(&fakeEvent);
- }
-
- void XInputTouchHandler::fakeButtonEvent(bool press, int button,
- const GestureEvent origEvent)
- {
- XEvent fakeEvent;
-
- memset(&fakeEvent, 0, sizeof(XEvent));
-
- fakeEvent.type = press ? ButtonPress : ButtonRelease;
- fakeEvent.xbutton.button = button;
- preparePointerEvent(&fakeEvent, origEvent);
-
- fakeEvent.xbutton.state |= fakeStateMask;
-
- // The button mask should indicate the button state just prior to
- // the event, we update the button mask after pushing
- pushFakeEvent(&fakeEvent);
-
- // Set/unset the bit for the correct button
- if (press) {
- fakeStateMask |= (1<<(7+button));
- } else {
- fakeStateMask &= ~(1<<(7+button));
- }
- }
-
- void XInputTouchHandler::fakeKeyEvent(bool press, int keysym,
- const GestureEvent origEvent)
- {
- XEvent fakeEvent;
-
- Window root, child;
- int rootX, rootY;
- XkbStateRec state;
-
- int modmask;
-
- root = XDefaultRootWindow(fl_display);
- XTranslateCoordinates(fl_display, wnd, root,
- origEvent.eventX,
- origEvent.eventY,
- &rootX, &rootY, &child);
- XkbGetState(fl_display, XkbUseCoreKbd, &state);
-
- KeyCode kc = XKeysymToKeycode(fl_display, keysym);
-
- memset(&fakeEvent, 0, sizeof(XEvent));
-
- fakeEvent.type = press ? KeyPress : KeyRelease;
- fakeEvent.xkey.type = press ? KeyPress : KeyRelease;
- fakeEvent.xkey.keycode = kc;
- fakeEvent.xkey.serial = XLastKnownRequestProcessed(fl_display);
- fakeEvent.xkey.display = fl_display;
- fakeEvent.xkey.window = wnd;
- fakeEvent.xkey.root = root;
- fakeEvent.xkey.subwindow = None;
- fakeEvent.xkey.time = fl_event_time;
- fakeEvent.xkey.x = origEvent.eventX;
- fakeEvent.xkey.y = origEvent.eventY;
- fakeEvent.xkey.x_root = rootX;
- fakeEvent.xkey.y_root = rootY;
- fakeEvent.xkey.state = state.mods;
- fakeEvent.xkey.state |= ((state.ptr_buttons >> 1) & 0x1f) << 8;
- fakeEvent.xkey.same_screen = True;
-
- // Apply our fake mask
- fakeEvent.xkey.state |= fakeStateMask;
-
- pushFakeEvent(&fakeEvent);
-
- switch(keysym) {
- case XK_Shift_L:
- case XK_Shift_R:
- modmask = ShiftMask;
- break;
- case XK_Caps_Lock:
- modmask = LockMask;
- break;
- case XK_Control_L:
- case XK_Control_R:
- modmask = ControlMask;
- break;
- default:
- modmask = 0;
- }
-
- if (press)
- fakeStateMask |= modmask;
- else
- fakeStateMask &= ~modmask;
- }
-
- void XInputTouchHandler::handleGestureEvent(const GestureEvent& event)
- {
- BaseTouchHandler::handleGestureEvent(event);
- }
-
- void XInputTouchHandler::pushFakeEvent(XEvent* event)
- {
- // Perhaps use XPutBackEvent() to avoid round trip latency?
- XSendEvent(fl_display, event->xany.window, true, 0, event);
- }
|