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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 <X11/XKBlib.h>
  27. #include <FL/x.H>
  28. #ifndef XK_MISCELLANY
  29. #define XK_MISCELLANY
  30. #include <rfb/keysymdef.h>
  31. #endif
  32. #include <rfb/LogWriter.h>
  33. #include "i18n.h"
  34. #include "XInputTouchHandler.h"
  35. static rfb::LogWriter vlog("XInputTouchHandler");
  36. static bool grabbed = false;
  37. XInputTouchHandler::XInputTouchHandler(Window wnd)
  38. : wnd(wnd), fakeStateMask(0)
  39. {
  40. XIEventMask eventmask;
  41. unsigned char flags[XIMaskLen(XI_LASTEVENT)] = { 0 };
  42. // Event delivery is broken when somebody else does a pointer grab,
  43. // so we need to listen to all devices and do filtering of master
  44. // devices manually
  45. eventmask.deviceid = XIAllDevices;
  46. eventmask.mask_len = sizeof(flags);
  47. eventmask.mask = flags;
  48. XISetMask(flags, XI_ButtonPress);
  49. XISetMask(flags, XI_Motion);
  50. XISetMask(flags, XI_ButtonRelease);
  51. XISetMask(flags, XI_TouchBegin);
  52. XISetMask(flags, XI_TouchUpdate);
  53. XISetMask(flags, XI_TouchEnd);
  54. // If something has a passive grab of touches (e.g. the window
  55. // manager wants to have its own gestures) then we won't get the
  56. // touch events until everyone who has a grab has indicated they
  57. // don't want these touches (via XIAllowTouchEvents()).
  58. // Unfortunately the touches are then replayed one touch point at
  59. // a time, meaning things will be delayed and out of order,
  60. // completely screwing up our gesture detection. Listening for
  61. // XI_TouchOwnership has the effect of giving us the touch events
  62. // right away, even if grabbing clients are also getting them.
  63. //
  64. // FIXME: We should really wait for the XI_TouchOwnership event
  65. // before it is safe to react to the gesture, otherwise we
  66. // might react to something that the window manager will
  67. // also react to.
  68. //
  69. if (!grabbed)
  70. XISetMask(flags, XI_TouchOwnership);
  71. XISelectEvents(fl_display, wnd, &eventmask, 1);
  72. }
  73. bool XInputTouchHandler::grabPointer()
  74. {
  75. XIEventMask *curmasks;
  76. int num_masks;
  77. int ret, ndevices;
  78. XIDeviceInfo *devices, *device;
  79. bool gotGrab;
  80. // We grab for the same events as the window is currently interested in
  81. curmasks = XIGetSelectedEvents(fl_display, wnd, &num_masks);
  82. if (curmasks == NULL) {
  83. if (num_masks == -1)
  84. vlog.error(_("Unable to get X Input 2 event mask for window 0x%08lx"), wnd);
  85. else
  86. vlog.error(_("Window 0x%08lx has no X Input 2 event mask"), wnd);
  87. return false;
  88. }
  89. // Our windows should only have a single mask, which allows us to
  90. // simplify all the code handling the masks
  91. if (num_masks > 1) {
  92. vlog.error(_("Window 0x%08lx has more than one X Input 2 event mask"), wnd);
  93. return false;
  94. }
  95. devices = XIQueryDevice(fl_display, XIAllMasterDevices, &ndevices);
  96. // Iterate through available devices to find those which
  97. // provide pointer input, and attempt to grab all such devices.
  98. gotGrab = false;
  99. for (int i = 0; i < ndevices; i++) {
  100. device = &devices[i];
  101. if (device->use != XIMasterPointer)
  102. continue;
  103. curmasks[0].deviceid = device->deviceid;
  104. ret = XIGrabDevice(fl_display,
  105. device->deviceid,
  106. wnd,
  107. CurrentTime,
  108. None,
  109. XIGrabModeAsync,
  110. XIGrabModeAsync,
  111. True,
  112. &(curmasks[0]));
  113. if (ret) {
  114. if (ret == XIAlreadyGrabbed) {
  115. continue;
  116. } else {
  117. vlog.error(_("Failure grabbing device %i"), device->deviceid);
  118. continue;
  119. }
  120. }
  121. gotGrab = true;
  122. }
  123. XIFreeDeviceInfo(devices);
  124. // Did we not even grab a single device?
  125. if (!gotGrab)
  126. return false;
  127. grabbed = true;
  128. return true;
  129. }
  130. void XInputTouchHandler::ungrabPointer()
  131. {
  132. int ndevices;
  133. XIDeviceInfo *devices, *device;
  134. devices = XIQueryDevice(fl_display, XIAllMasterDevices, &ndevices);
  135. // Release all devices, hoping they are the same as when we
  136. // grabbed things
  137. for (int i = 0; i < ndevices; i++) {
  138. device = &devices[i];
  139. if (device->use != XIMasterPointer)
  140. continue;
  141. XIUngrabDevice(fl_display, device->deviceid, CurrentTime);
  142. }
  143. XIFreeDeviceInfo(devices);
  144. grabbed = false;
  145. }
  146. void XInputTouchHandler::processEvent(const XIDeviceEvent* devev)
  147. {
  148. // FLTK doesn't understand X Input events, and we've stopped
  149. // delivery of Core events by enabling the X Input ones. Make
  150. // FLTK happy by faking Core events based on the X Input ones.
  151. bool isMaster = devev->deviceid != devev->sourceid;
  152. // We're only interested in events from master devices
  153. if (!isMaster) {
  154. // However we need to accept TouchEnd from slave devices as they
  155. // might not get delivered if there is a pointer grab, see:
  156. // https://gitlab.freedesktop.org/xorg/xserver/-/issues/1016
  157. if (devev->evtype != XI_TouchEnd)
  158. return;
  159. }
  160. // Avoid duplicate TouchEnd events, see above
  161. // FIXME: Doesn't handle floating slave devices
  162. if (isMaster && devev->evtype == XI_TouchEnd)
  163. return;
  164. if (devev->flags & XIPointerEmulated) {
  165. // We still want the server to do the scroll wheel to button thing
  166. // though, so keep those
  167. if (((devev->evtype == XI_ButtonPress) ||
  168. (devev->evtype == XI_ButtonRelease)) &&
  169. (devev->detail >= 4) && (devev->detail <= 7)) {
  170. ;
  171. } else {
  172. // Sometimes the server incorrectly sends us various events with
  173. // this flag set, see:
  174. // https://gitlab.freedesktop.org/xorg/xserver/-/issues/1027
  175. return;
  176. }
  177. }
  178. switch (devev->evtype) {
  179. case XI_Enter:
  180. case XI_Leave:
  181. // We get these when the mouse is grabbed implicitly, so just ignore them
  182. // https://gitlab.freedesktop.org/xorg/xserver/-/issues/1026
  183. break;
  184. case XI_Motion:
  185. // FIXME: We also get XI_Motion for scroll wheel events, which
  186. // we might want to ignore
  187. fakeMotionEvent(devev);
  188. break;
  189. case XI_ButtonPress:
  190. fakeButtonEvent(true, devev->detail, devev);
  191. break;
  192. case XI_ButtonRelease:
  193. fakeButtonEvent(false, devev->detail, devev);
  194. break;
  195. case XI_TouchBegin:
  196. // XInput2 wants us to explicitly accept touch sequences
  197. // for grabbed devices before it will pass events
  198. if (grabbed) {
  199. XIAllowTouchEvents(fl_display,
  200. devev->deviceid,
  201. devev->detail,
  202. devev->event,
  203. XIAcceptTouch);
  204. }
  205. handleTouchBegin(devev->detail, devev->event_x, devev->event_y);
  206. break;
  207. case XI_TouchUpdate:
  208. handleTouchUpdate(devev->detail, devev->event_x, devev->event_y);
  209. break;
  210. case XI_TouchEnd:
  211. handleTouchEnd(devev->detail);
  212. break;
  213. case XI_TouchOwnership:
  214. // FIXME: Currently ignored, see constructor
  215. break;
  216. }
  217. }
  218. void XInputTouchHandler::preparePointerEvent(XEvent* dst, const XIDeviceEvent* src)
  219. {
  220. // XButtonEvent and XMotionEvent are almost identical, so we
  221. // don't have to care which it is for these fields
  222. dst->xbutton.serial = src->serial;
  223. dst->xbutton.display = src->display;
  224. dst->xbutton.window = src->event;
  225. dst->xbutton.root = src->root;
  226. dst->xbutton.subwindow = src->child;
  227. dst->xbutton.time = src->time;
  228. dst->xbutton.x = src->event_x;
  229. dst->xbutton.y = src->event_y;
  230. dst->xbutton.x_root = src->root_x;
  231. dst->xbutton.y_root = src->root_y;
  232. dst->xbutton.state = src->mods.effective;
  233. dst->xbutton.state |= ((src->buttons.mask[0] >> 1) & 0x1f) << 8;
  234. dst->xbutton.same_screen = True;
  235. }
  236. void XInputTouchHandler::fakeMotionEvent(const XIDeviceEvent* origEvent)
  237. {
  238. XEvent fakeEvent;
  239. memset(&fakeEvent, 0, sizeof(XEvent));
  240. fakeEvent.type = MotionNotify;
  241. fakeEvent.xmotion.is_hint = False;
  242. preparePointerEvent(&fakeEvent, origEvent);
  243. pushFakeEvent(&fakeEvent);
  244. }
  245. void XInputTouchHandler::fakeButtonEvent(bool press, int button,
  246. const XIDeviceEvent* origEvent)
  247. {
  248. XEvent fakeEvent;
  249. memset(&fakeEvent, 0, sizeof(XEvent));
  250. fakeEvent.type = press ? ButtonPress : ButtonRelease;
  251. fakeEvent.xbutton.button = button;
  252. // Apply the fake mask before pushing so it will be in sync
  253. fakeEvent.xbutton.state |= fakeStateMask;
  254. preparePointerEvent(&fakeEvent, origEvent);
  255. pushFakeEvent(&fakeEvent);
  256. }
  257. void XInputTouchHandler::preparePointerEvent(XEvent* dst, const GestureEvent src)
  258. {
  259. Window root, child;
  260. int rootX, rootY;
  261. XkbStateRec state;
  262. // We don't have a real event to steal things from, so we'll have
  263. // to fake these events based on the current state of things
  264. root = XDefaultRootWindow(fl_display);
  265. XTranslateCoordinates(fl_display, wnd, root,
  266. src.eventX,
  267. src.eventY,
  268. &rootX, &rootY, &child);
  269. XkbGetState(fl_display, XkbUseCoreKbd, &state);
  270. // XButtonEvent and XMotionEvent are almost identical, so we
  271. // don't have to care which it is for these fields
  272. dst->xbutton.serial = XLastKnownRequestProcessed(fl_display);
  273. dst->xbutton.display = fl_display;
  274. dst->xbutton.window = wnd;
  275. dst->xbutton.root = root;
  276. dst->xbutton.subwindow = None;
  277. dst->xbutton.time = fl_event_time;
  278. dst->xbutton.x = src.eventX;
  279. dst->xbutton.y = src.eventY;
  280. dst->xbutton.x_root = rootX;
  281. dst->xbutton.y_root = rootY;
  282. dst->xbutton.state = state.mods;
  283. dst->xbutton.state |= ((state.ptr_buttons >> 1) & 0x1f) << 8;
  284. dst->xbutton.same_screen = True;
  285. }
  286. void XInputTouchHandler::fakeMotionEvent(const GestureEvent origEvent)
  287. {
  288. XEvent fakeEvent;
  289. memset(&fakeEvent, 0, sizeof(XEvent));
  290. fakeEvent.type = MotionNotify;
  291. fakeEvent.xmotion.is_hint = False;
  292. preparePointerEvent(&fakeEvent, origEvent);
  293. fakeEvent.xbutton.state |= fakeStateMask;
  294. pushFakeEvent(&fakeEvent);
  295. }
  296. void XInputTouchHandler::fakeButtonEvent(bool press, int button,
  297. const GestureEvent origEvent)
  298. {
  299. XEvent fakeEvent;
  300. memset(&fakeEvent, 0, sizeof(XEvent));
  301. fakeEvent.type = press ? ButtonPress : ButtonRelease;
  302. fakeEvent.xbutton.button = button;
  303. preparePointerEvent(&fakeEvent, origEvent);
  304. fakeEvent.xbutton.state |= fakeStateMask;
  305. // The button mask should indicate the button state just prior to
  306. // the event, we update the button mask after pushing
  307. pushFakeEvent(&fakeEvent);
  308. // Set/unset the bit for the correct button
  309. if (press) {
  310. fakeStateMask |= (1<<(7+button));
  311. } else {
  312. fakeStateMask &= ~(1<<(7+button));
  313. }
  314. }
  315. void XInputTouchHandler::fakeKeyEvent(bool press, int keysym,
  316. const GestureEvent origEvent)
  317. {
  318. XEvent fakeEvent;
  319. Window root, child;
  320. int rootX, rootY;
  321. XkbStateRec state;
  322. int modmask;
  323. root = XDefaultRootWindow(fl_display);
  324. XTranslateCoordinates(fl_display, wnd, root,
  325. origEvent.eventX,
  326. origEvent.eventY,
  327. &rootX, &rootY, &child);
  328. XkbGetState(fl_display, XkbUseCoreKbd, &state);
  329. KeyCode kc = XKeysymToKeycode(fl_display, keysym);
  330. memset(&fakeEvent, 0, sizeof(XEvent));
  331. fakeEvent.type = press ? KeyPress : KeyRelease;
  332. fakeEvent.xkey.type = press ? KeyPress : KeyRelease;
  333. fakeEvent.xkey.keycode = kc;
  334. fakeEvent.xkey.serial = XLastKnownRequestProcessed(fl_display);
  335. fakeEvent.xkey.display = fl_display;
  336. fakeEvent.xkey.window = wnd;
  337. fakeEvent.xkey.root = root;
  338. fakeEvent.xkey.subwindow = None;
  339. fakeEvent.xkey.time = fl_event_time;
  340. fakeEvent.xkey.x = origEvent.eventX;
  341. fakeEvent.xkey.y = origEvent.eventY;
  342. fakeEvent.xkey.x_root = rootX;
  343. fakeEvent.xkey.y_root = rootY;
  344. fakeEvent.xkey.state = state.mods;
  345. fakeEvent.xkey.state |= ((state.ptr_buttons >> 1) & 0x1f) << 8;
  346. fakeEvent.xkey.same_screen = True;
  347. // Apply our fake mask
  348. fakeEvent.xkey.state |= fakeStateMask;
  349. pushFakeEvent(&fakeEvent);
  350. switch(keysym) {
  351. case XK_Shift_L:
  352. case XK_Shift_R:
  353. modmask = ShiftMask;
  354. break;
  355. case XK_Caps_Lock:
  356. modmask = LockMask;
  357. break;
  358. case XK_Control_L:
  359. case XK_Control_R:
  360. modmask = ControlMask;
  361. break;
  362. default:
  363. modmask = 0;
  364. }
  365. if (press)
  366. fakeStateMask |= modmask;
  367. else
  368. fakeStateMask &= ~modmask;
  369. }
  370. void XInputTouchHandler::handleGestureEvent(const GestureEvent& event)
  371. {
  372. BaseTouchHandler::handleGestureEvent(event);
  373. }
  374. void XInputTouchHandler::pushFakeEvent(XEvent* event)
  375. {
  376. // Perhaps use XPutBackEvent() to avoid round trip latency?
  377. XSendEvent(fl_display, event->xany.window, true, 0, event);
  378. }