You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XInputTouchHandler.cxx 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* Copyright 2019 Aaron Sowry for Cendio AB
  2. * Copyright 2019-2020 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <assert.h>
  23. #include <string.h>
  24. #include <X11/extensions/XInput2.h>
  25. #include <X11/extensions/XI2.h>
  26. #include <FL/x.H>
  27. #include <rfb/LogWriter.h>
  28. #include "i18n.h"
  29. #include "XInputTouchHandler.h"
  30. static rfb::LogWriter vlog("XInputTouchHandler");
  31. static bool grabbed = false;
  32. XInputTouchHandler::XInputTouchHandler(Window wnd)
  33. : wnd(wnd), fakeStateMask(0), trackingTouch(false)
  34. {
  35. XIEventMask eventmask;
  36. unsigned char flags[XIMaskLen(XI_LASTEVENT)] = { 0 };
  37. // Event delivery is broken when somebody else does a pointer grab,
  38. // so we need to listen to all devices and do filtering of master
  39. // devices manually
  40. eventmask.deviceid = XIAllDevices;
  41. eventmask.mask_len = sizeof(flags);
  42. eventmask.mask = flags;
  43. XISetMask(flags, XI_ButtonPress);
  44. XISetMask(flags, XI_Motion);
  45. XISetMask(flags, XI_ButtonRelease);
  46. XISetMask(flags, XI_TouchBegin);
  47. XISetMask(flags, XI_TouchUpdate);
  48. XISetMask(flags, XI_TouchEnd);
  49. // If something has a passive grab of touches (e.g. the window
  50. // manager wants to have its own gestures) then we won't get the
  51. // touch events until everyone who has a grab has indicated they
  52. // don't want these touches (via XIAllowTouchEvents()).
  53. // Unfortunately the touches are then replayed one touch point at
  54. // a time, meaning things will be delayed and out of order,
  55. // completely screwing up our gesture detection. Listening for
  56. // XI_TouchOwnership has the effect of giving us the touch events
  57. // right away, even if grabbing clients are also getting them.
  58. //
  59. // FIXME: We should really wait for the XI_TouchOwnership event
  60. // before it is safe to react to the gesture, otherwise we
  61. // might react to something that the window manager will
  62. // also react to.
  63. //
  64. if (!grabbed)
  65. XISetMask(flags, XI_TouchOwnership);
  66. XISelectEvents(fl_display, wnd, &eventmask, 1);
  67. }
  68. bool XInputTouchHandler::grabPointer()
  69. {
  70. XIEventMask *curmasks;
  71. int num_masks;
  72. int ret, ndevices;
  73. XIDeviceInfo *devices, *device;
  74. bool gotGrab;
  75. // We grab for the same events as the window is currently interested in
  76. curmasks = XIGetSelectedEvents(fl_display, wnd, &num_masks);
  77. if (curmasks == NULL) {
  78. if (num_masks == -1)
  79. vlog.error(_("Unable to get X Input 2 event mask for window 0x%08lu"), wnd);
  80. else
  81. vlog.error(_("Window 0x%08lu has no X Input 2 event mask"), wnd);
  82. return false;
  83. }
  84. // Our windows should only have a single mask, which allows us to
  85. // simplify all the code handling the masks
  86. if (num_masks > 1) {
  87. vlog.error(_("Window 0x%08lu has more than one X Input 2 event mask"), wnd);
  88. return false;
  89. }
  90. devices = XIQueryDevice(fl_display, XIAllMasterDevices, &ndevices);
  91. // Iterate through available devices to find those which
  92. // provide pointer input, and attempt to grab all such devices.
  93. gotGrab = false;
  94. for (int i = 0; i < ndevices; i++) {
  95. device = &devices[i];
  96. if (device->use != XIMasterPointer)
  97. continue;
  98. curmasks[0].deviceid = device->deviceid;
  99. ret = XIGrabDevice(fl_display,
  100. device->deviceid,
  101. wnd,
  102. CurrentTime,
  103. None,
  104. XIGrabModeAsync,
  105. XIGrabModeAsync,
  106. True,
  107. &(curmasks[0]));
  108. if (ret) {
  109. if (ret == XIAlreadyGrabbed) {
  110. continue;
  111. } else {
  112. vlog.error(_("Failure grabbing device %i"), device->deviceid);
  113. continue;
  114. }
  115. }
  116. gotGrab = true;
  117. }
  118. XIFreeDeviceInfo(devices);
  119. // Did we not even grab a single device?
  120. if (!gotGrab)
  121. return false;
  122. grabbed = true;
  123. return true;
  124. }
  125. void XInputTouchHandler::ungrabPointer()
  126. {
  127. int ndevices;
  128. XIDeviceInfo *devices, *device;
  129. devices = XIQueryDevice(fl_display, XIAllMasterDevices, &ndevices);
  130. // Release all devices, hoping they are the same as when we
  131. // grabbed things
  132. for (int i = 0; i < ndevices; i++) {
  133. device = &devices[i];
  134. if (device->use != XIMasterPointer)
  135. continue;
  136. XIUngrabDevice(fl_display, device->deviceid, CurrentTime);
  137. }
  138. XIFreeDeviceInfo(devices);
  139. grabbed = false;
  140. }
  141. void XInputTouchHandler::processEvent(const XIDeviceEvent* devev)
  142. {
  143. // FLTK doesn't understand X Input events, and we've stopped
  144. // delivery of Core events by enabling the X Input ones. Make
  145. // FLTK happy by faking Core events based on the X Input ones.
  146. bool isMaster = devev->deviceid != devev->sourceid;
  147. // We're only interested in events from master devices
  148. if (!isMaster) {
  149. // However we need to accept TouchEnd from slave devices as they
  150. // might not get delivered if there is a pointer grab, see:
  151. // https://gitlab.freedesktop.org/xorg/xserver/-/issues/1016
  152. if (devev->evtype != XI_TouchEnd)
  153. return;
  154. }
  155. // Avoid duplicate TouchEnd events, see above
  156. // FIXME: Doesn't handle floating slave devices
  157. if (isMaster && devev->evtype == XI_TouchEnd)
  158. return;
  159. if (devev->flags & XIPointerEmulated) {
  160. // We still want the server to do the scroll wheel to button thing
  161. // though, so keep those
  162. if (((devev->evtype == XI_ButtonPress) ||
  163. (devev->evtype == XI_ButtonRelease)) &&
  164. (devev->detail >= 4) && (devev->detail <= 7)) {
  165. ;
  166. } else {
  167. // Sometimes the server incorrectly sends us various events with
  168. // this flag set, see:
  169. // https://gitlab.freedesktop.org/xorg/xserver/-/issues/1027
  170. return;
  171. }
  172. }
  173. switch (devev->evtype) {
  174. case XI_Enter:
  175. case XI_Leave:
  176. // We get these when the mouse is grabbed implicitly, so just ignore them
  177. // https://gitlab.freedesktop.org/xorg/xserver/-/issues/1026
  178. break;
  179. case XI_Motion:
  180. // FIXME: We also get XI_Motion for scroll wheel events, which
  181. // we might want to ignore
  182. fakeMotionEvent(devev);
  183. break;
  184. case XI_ButtonPress:
  185. fakeButtonEvent(true, devev->detail, devev);
  186. break;
  187. case XI_ButtonRelease:
  188. fakeButtonEvent(false, devev->detail, devev);
  189. break;
  190. case XI_TouchBegin:
  191. if (trackingTouch)
  192. break;
  193. // XInput2 wants us to explicitly accept touch sequences
  194. // for grabbed devices before it will pass events
  195. if (grabbed) {
  196. XIAllowTouchEvents(fl_display,
  197. devev->deviceid,
  198. devev->detail,
  199. devev->event,
  200. XIAcceptTouch);
  201. }
  202. trackingTouch = true;
  203. trackedTouchPoint = devev->detail;
  204. fakeMotionEvent(devev);
  205. fakeButtonEvent(true, Button1, devev);
  206. break;
  207. case XI_TouchUpdate:
  208. if (!trackingTouch || (devev->detail != trackedTouchPoint))
  209. break;
  210. fakeMotionEvent(devev);
  211. break;
  212. case XI_TouchEnd:
  213. if (!trackingTouch || (devev->detail != trackedTouchPoint))
  214. break;
  215. fakeButtonEvent(false, Button1, devev);
  216. trackingTouch = false;
  217. break;
  218. case XI_TouchOwnership:
  219. // FIXME: Currently ignored, see constructor
  220. break;
  221. }
  222. }
  223. void XInputTouchHandler::preparePointerEvent(XEvent* dst, const XIDeviceEvent* src)
  224. {
  225. // XButtonEvent and XMotionEvent are almost identical, so we
  226. // don't have to care which it is for these fields
  227. dst->xbutton.serial = src->serial;
  228. dst->xbutton.display = src->display;
  229. dst->xbutton.window = src->event;
  230. dst->xbutton.root = src->root;
  231. dst->xbutton.subwindow = src->child;
  232. dst->xbutton.time = src->time;
  233. dst->xbutton.x = src->event_x;
  234. dst->xbutton.y = src->event_y;
  235. dst->xbutton.x_root = src->root_x;
  236. dst->xbutton.y_root = src->root_y;
  237. dst->xbutton.state = src->mods.effective;
  238. dst->xbutton.state |= ((src->buttons.mask[0] >> 1) & 0x1f) << 8;
  239. dst->xbutton.same_screen = True;
  240. }
  241. void XInputTouchHandler::fakeMotionEvent(const XIDeviceEvent* origEvent)
  242. {
  243. XEvent fakeEvent;
  244. memset(&fakeEvent, 0, sizeof(XEvent));
  245. fakeEvent.type = MotionNotify;
  246. fakeEvent.xmotion.is_hint = False;
  247. preparePointerEvent(&fakeEvent, origEvent);
  248. fakeEvent.xbutton.state |= fakeStateMask;
  249. pushFakeEvent(&fakeEvent);
  250. }
  251. void XInputTouchHandler::fakeButtonEvent(bool press, int button,
  252. const XIDeviceEvent* origEvent)
  253. {
  254. XEvent fakeEvent;
  255. memset(&fakeEvent, 0, sizeof(XEvent));
  256. fakeEvent.type = press ? ButtonPress : ButtonRelease;
  257. fakeEvent.xbutton.button = button;
  258. preparePointerEvent(&fakeEvent, origEvent);
  259. fakeEvent.xbutton.state |= fakeStateMask;
  260. pushFakeEvent(&fakeEvent);
  261. // Set/unset the bit for the correct button
  262. if (press) {
  263. fakeStateMask |= (1<<(7+button));
  264. } else {
  265. fakeStateMask &= ~(1<<(7+button));
  266. }
  267. }
  268. void XInputTouchHandler::pushFakeEvent(XEvent* event)
  269. {
  270. // Perhaps use XPutBackEvent() to avoid round trip latency?
  271. XSendEvent(fl_display, event->xany.window, true, 0, event);
  272. }