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.

touch.cxx 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* Copyright 2019-2020 Pierre Ossman <ossman@cendio.se> for Cendio AB
  2. * Copyright 2019 Aaron Sowry 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 <map>
  24. #if defined(WIN32)
  25. #include <windows.h>
  26. #include <commctrl.h>
  27. #elif !defined(__APPLE__)
  28. #include <X11/extensions/XInput2.h>
  29. #include <X11/extensions/XI2.h>
  30. #endif
  31. #include <FL/Fl.H>
  32. #include <FL/x.H>
  33. #include <rfb/Exception.h>
  34. #include <rfb/LogWriter.h>
  35. #include "i18n.h"
  36. #include "vncviewer.h"
  37. #include "BaseTouchHandler.h"
  38. #if defined(WIN32)
  39. #include "Win32TouchHandler.h"
  40. #elif !defined(__APPLE__)
  41. #include "XInputTouchHandler.h"
  42. #endif
  43. #include "touch.h"
  44. static rfb::LogWriter vlog("Touch");
  45. #if !defined(WIN32) && !defined(__APPLE__)
  46. static int xi_major;
  47. #endif
  48. typedef std::map<Window, class BaseTouchHandler*> HandlerMap;
  49. static HandlerMap handlers;
  50. #if defined(WIN32)
  51. LRESULT CALLBACK win32WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam,
  52. LPARAM lParam,
  53. UINT_PTR /*uIdSubclass*/,
  54. DWORD_PTR /*dwRefData*/)
  55. {
  56. bool handled = false;
  57. if (uMsg == WM_NCDESTROY) {
  58. delete handlers[hWnd];
  59. handlers.erase(hWnd);
  60. RemoveWindowSubclass(hWnd, &win32WindowProc, 1);
  61. } else {
  62. if (handlers.count(hWnd) == 0) {
  63. vlog.error(_("Got message (0x%x) for an unhandled window"), uMsg);
  64. } else {
  65. handled = dynamic_cast<Win32TouchHandler*>
  66. (handlers[hWnd])->processEvent(uMsg, wParam, lParam);
  67. }
  68. }
  69. // Only run the normal WndProc handlers for unhandled events
  70. if (handled)
  71. return 0;
  72. else
  73. return DefSubclassProc(hWnd, uMsg, wParam, lParam);
  74. }
  75. #elif !defined(__APPLE__)
  76. static void x11_change_touch_ownership(bool enable)
  77. {
  78. HandlerMap::const_iterator iter;
  79. XIEventMask *curmasks;
  80. int num_masks;
  81. XIEventMask newmask;
  82. unsigned char mask[XIMaskLen(XI_LASTEVENT)] = { 0 };
  83. newmask.mask = mask;
  84. newmask.mask_len = sizeof(mask);
  85. for (iter = handlers.begin(); iter != handlers.end(); ++iter) {
  86. curmasks = XIGetSelectedEvents(fl_display, iter->first, &num_masks);
  87. if (curmasks == NULL) {
  88. if (num_masks == -1)
  89. vlog.error(_("Unable to get X Input 2 event mask for window 0x%08lx"), iter->first);
  90. continue;
  91. }
  92. // Our windows should only have a single mask, which allows us to
  93. // simplify all the code handling the masks
  94. if (num_masks > 1) {
  95. vlog.error(_("Window 0x%08lx has more than one X Input 2 event mask"), iter->first);
  96. continue;
  97. }
  98. newmask.deviceid = curmasks[0].deviceid;
  99. assert(newmask.mask_len >= curmasks[0].mask_len);
  100. memcpy(newmask.mask, curmasks[0].mask, curmasks[0].mask_len);
  101. if (enable)
  102. XISetMask(newmask.mask, XI_TouchOwnership);
  103. else
  104. XIClearMask(newmask.mask, XI_TouchOwnership);
  105. XISelectEvents(fl_display, iter->first, &newmask, 1);
  106. XFree(curmasks);
  107. }
  108. }
  109. bool x11_grab_pointer(Window window)
  110. {
  111. bool ret;
  112. if (handlers.count(window) == 0) {
  113. vlog.error(_("Invalid window 0x%08lx specified for pointer grab"), window);
  114. return BadWindow;
  115. }
  116. // We need to remove XI_TouchOwnership from our event masks while
  117. // grabbing as otherwise we will get double events (one for the grab,
  118. // and one because of XI_TouchOwnership) with no way of telling them
  119. // apart. See XInputTouchHandler constructor for why we use this
  120. // event.
  121. x11_change_touch_ownership(false);
  122. ret = dynamic_cast<XInputTouchHandler*>(handlers[window])->grabPointer();
  123. if (!ret)
  124. x11_change_touch_ownership(true);
  125. return ret;
  126. }
  127. void x11_ungrab_pointer(Window window)
  128. {
  129. if (handlers.count(window) == 0) {
  130. vlog.error(_("Invalid window 0x%08lx specified for pointer grab"), window);
  131. return;
  132. }
  133. dynamic_cast<XInputTouchHandler*>(handlers[window])->ungrabPointer();
  134. // Restore XI_TouchOwnership now that the grab is gone
  135. x11_change_touch_ownership(true);
  136. }
  137. #endif
  138. static int handleTouchEvent(void *event, void* /*data*/)
  139. {
  140. #if defined(WIN32)
  141. MSG *msg = (MSG*)event;
  142. // Trigger on the first WM_PAINT event. We can't trigger on WM_CREATE
  143. // events since FLTK's system handlers trigger before WndProc.
  144. // WM_CREATE events are sent directly to WndProc.
  145. if (msg->message == WM_PAINT && handlers.count(msg->hwnd) == 0) {
  146. try {
  147. handlers[msg->hwnd] = new Win32TouchHandler(msg->hwnd);
  148. } catch (rfb::Exception& e) {
  149. vlog.error(_("Failed to create touch handler: %s"), e.str());
  150. abort_vncviewer(_("Failed to create touch handler: %s"), e.str());
  151. }
  152. // Add a special hook-in for handling events sent directly to WndProc
  153. if (!SetWindowSubclass(msg->hwnd, &win32WindowProc, 1, 0)) {
  154. vlog.error(_("Couldn't attach event handler to window (error 0x%x)"),
  155. (int)GetLastError());
  156. }
  157. }
  158. #elif defined(__APPLE__)
  159. // No touch support on macOS
  160. (void)event;
  161. #else
  162. XEvent *xevent = (XEvent*)event;
  163. if (xevent->type == MapNotify) {
  164. handlers[xevent->xmap.window] = new XInputTouchHandler(xevent->xmap.window);
  165. // Fall through as we don't want to interfere with whatever someone
  166. // else might want to do with this event
  167. } else if (xevent->type == UnmapNotify) {
  168. delete handlers[xevent->xunmap.window];
  169. handlers.erase(xevent->xunmap.window);
  170. } else if (xevent->type == DestroyNotify) {
  171. delete handlers[xevent->xdestroywindow.window];
  172. handlers.erase(xevent->xdestroywindow.window);
  173. } else if (xevent->type == GenericEvent) {
  174. if (xevent->xgeneric.extension == xi_major) {
  175. XIDeviceEvent *devev;
  176. if (!XGetEventData(fl_display, &xevent->xcookie)) {
  177. vlog.error(_("Failed to get event data for X Input event"));
  178. return 1;
  179. }
  180. devev = (XIDeviceEvent*)xevent->xcookie.data;
  181. if (handlers.count(devev->event) == 0) {
  182. // We get these when the mouse is grabbed implicitly, so just
  183. // ignore them
  184. // https://gitlab.freedesktop.org/xorg/xserver/-/issues/1026
  185. if ((devev->evtype == XI_Enter) || (devev->evtype == XI_Leave))
  186. ;
  187. else
  188. vlog.error(_("X Input event for unknown window"));
  189. XFreeEventData(fl_display, &xevent->xcookie);
  190. return 1;
  191. }
  192. dynamic_cast<XInputTouchHandler*>(handlers[devev->event])->processEvent(devev);
  193. XFreeEventData(fl_display, &xevent->xcookie);
  194. return 1;
  195. }
  196. }
  197. #endif
  198. return 0;
  199. }
  200. void enable_touch()
  201. {
  202. #if !defined(WIN32) && !defined(__APPLE__)
  203. int ev, err;
  204. int major_ver, minor_ver;
  205. fl_open_display();
  206. if (!XQueryExtension(fl_display, "XInputExtension", &xi_major, &ev, &err)) {
  207. abort_vncviewer(_("X Input extension not available."));
  208. return; // Not reached
  209. }
  210. major_ver = 2;
  211. minor_ver = 2;
  212. if (XIQueryVersion(fl_display, &major_ver, &minor_ver) != Success) {
  213. abort_vncviewer(_("X Input 2 (or newer) is not available."));
  214. return; // Not reached
  215. }
  216. if ((major_ver == 2) && (minor_ver < 2))
  217. vlog.error(_("X Input 2.2 (or newer) is not available. Touch gestures will not be supported."));
  218. #endif
  219. Fl::add_system_handler(handleTouchEvent, NULL);
  220. }
  221. void disable_touch()
  222. {
  223. Fl::remove_system_handler(handleTouchEvent);
  224. }