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

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