SDL_winrtpointerinput.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_WINRT
  20. /* SDL includes */
  21. #include "SDL_winrtevents_c.h"
  22. #include "SDL_winrtmouse_c.h"
  23. #include "SDL_winrtvideo_cpp.h"
  24. #include "SDL_system.h"
  25. extern "C" {
  26. #include "../SDL_sysvideo.h"
  27. #include "../../events/SDL_events_c.h"
  28. #include "../../events/SDL_mouse_c.h"
  29. #include "../../events/SDL_touch_c.h"
  30. }
  31. /* File-specific globals: */
  32. static SDL_TouchID WINRT_TouchID = 1;
  33. void
  34. WINRT_InitTouch(_THIS)
  35. {
  36. SDL_AddTouch(WINRT_TouchID, SDL_TOUCH_DEVICE_DIRECT, "");
  37. }
  38. //
  39. // Applies necessary geometric transformations to raw cursor positions:
  40. //
  41. Windows::Foundation::Point
  42. WINRT_TransformCursorPosition(SDL_Window * window,
  43. Windows::Foundation::Point rawPosition,
  44. WINRT_CursorNormalizationType normalization)
  45. {
  46. using namespace Windows::UI::Core;
  47. using namespace Windows::Graphics::Display;
  48. if (!window) {
  49. return rawPosition;
  50. }
  51. SDL_WindowData * windowData = (SDL_WindowData *) window->driverdata;
  52. if (windowData->coreWindow == nullptr) {
  53. // For some reason, the window isn't associated with a CoreWindow.
  54. // This might end up being the case as XAML support is extended.
  55. // For now, if there's no CoreWindow attached to the SDL_Window,
  56. // don't do any transforms.
  57. // TODO, WinRT: make sure touch input coordinate ranges are correct when using XAML support
  58. return rawPosition;
  59. }
  60. // The CoreWindow can only be accessed on certain thread(s).
  61. SDL_assert(CoreWindow::GetForCurrentThread() != nullptr);
  62. CoreWindow ^ nativeWindow = windowData->coreWindow.Get();
  63. Windows::Foundation::Point outputPosition;
  64. // Compute coordinates normalized from 0..1.
  65. // If the coordinates need to be sized to the SDL window,
  66. // we'll do that after.
  67. #if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION > NTDDI_WIN8)
  68. outputPosition.X = rawPosition.X / nativeWindow->Bounds.Width;
  69. outputPosition.Y = rawPosition.Y / nativeWindow->Bounds.Height;
  70. #else
  71. switch (WINRT_DISPLAY_PROPERTY(CurrentOrientation))
  72. {
  73. case DisplayOrientations::Portrait:
  74. outputPosition.X = rawPosition.X / nativeWindow->Bounds.Width;
  75. outputPosition.Y = rawPosition.Y / nativeWindow->Bounds.Height;
  76. break;
  77. case DisplayOrientations::PortraitFlipped:
  78. outputPosition.X = 1.0f - (rawPosition.X / nativeWindow->Bounds.Width);
  79. outputPosition.Y = 1.0f - (rawPosition.Y / nativeWindow->Bounds.Height);
  80. break;
  81. case DisplayOrientations::Landscape:
  82. outputPosition.X = rawPosition.Y / nativeWindow->Bounds.Height;
  83. outputPosition.Y = 1.0f - (rawPosition.X / nativeWindow->Bounds.Width);
  84. break;
  85. case DisplayOrientations::LandscapeFlipped:
  86. outputPosition.X = 1.0f - (rawPosition.Y / nativeWindow->Bounds.Height);
  87. outputPosition.Y = rawPosition.X / nativeWindow->Bounds.Width;
  88. break;
  89. default:
  90. break;
  91. }
  92. #endif
  93. if (normalization == TransformToSDLWindowSize) {
  94. outputPosition.X *= ((float32) window->w);
  95. outputPosition.Y *= ((float32) window->h);
  96. }
  97. return outputPosition;
  98. }
  99. SDL_bool
  100. WINRT_GetSDLButtonForPointerPoint(Windows::UI::Input::PointerPoint ^pt, Uint8 *button, Uint8 *pressed)
  101. {
  102. using namespace Windows::UI::Input;
  103. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  104. *button = SDL_BUTTON_LEFT;
  105. return SDL_TRUE;
  106. #else
  107. switch (pt->Properties->PointerUpdateKind)
  108. {
  109. case PointerUpdateKind::LeftButtonPressed:
  110. case PointerUpdateKind::LeftButtonReleased:
  111. *button = SDL_BUTTON_LEFT;
  112. *pressed = (pt->Properties->PointerUpdateKind == PointerUpdateKind::LeftButtonPressed);
  113. return SDL_TRUE;
  114. case PointerUpdateKind::RightButtonPressed:
  115. case PointerUpdateKind::RightButtonReleased:
  116. *button = SDL_BUTTON_RIGHT;
  117. *pressed = (pt->Properties->PointerUpdateKind == PointerUpdateKind::RightButtonPressed);
  118. return SDL_TRUE;
  119. case PointerUpdateKind::MiddleButtonPressed:
  120. case PointerUpdateKind::MiddleButtonReleased:
  121. *button = SDL_BUTTON_MIDDLE;
  122. *pressed = (pt->Properties->PointerUpdateKind == PointerUpdateKind::MiddleButtonPressed);
  123. return SDL_TRUE;
  124. case PointerUpdateKind::XButton1Pressed:
  125. case PointerUpdateKind::XButton1Released:
  126. *button = SDL_BUTTON_X1;
  127. *pressed = (pt->Properties->PointerUpdateKind == PointerUpdateKind::XButton1Pressed);
  128. return SDL_TRUE;
  129. case PointerUpdateKind::XButton2Pressed:
  130. case PointerUpdateKind::XButton2Released:
  131. *button = SDL_BUTTON_X2;
  132. *pressed = (pt->Properties->PointerUpdateKind == PointerUpdateKind::XButton2Pressed);
  133. return SDL_TRUE;
  134. default:
  135. break;
  136. }
  137. #endif
  138. *button = 0;
  139. *pressed = 0;
  140. return SDL_FALSE;
  141. }
  142. //const char *
  143. //WINRT_ConvertPointerUpdateKindToString(Windows::UI::Input::PointerUpdateKind kind)
  144. //{
  145. // using namespace Windows::UI::Input;
  146. //
  147. // switch (kind)
  148. // {
  149. // case PointerUpdateKind::Other:
  150. // return "Other";
  151. // case PointerUpdateKind::LeftButtonPressed:
  152. // return "LeftButtonPressed";
  153. // case PointerUpdateKind::LeftButtonReleased:
  154. // return "LeftButtonReleased";
  155. // case PointerUpdateKind::RightButtonPressed:
  156. // return "RightButtonPressed";
  157. // case PointerUpdateKind::RightButtonReleased:
  158. // return "RightButtonReleased";
  159. // case PointerUpdateKind::MiddleButtonPressed:
  160. // return "MiddleButtonPressed";
  161. // case PointerUpdateKind::MiddleButtonReleased:
  162. // return "MiddleButtonReleased";
  163. // case PointerUpdateKind::XButton1Pressed:
  164. // return "XButton1Pressed";
  165. // case PointerUpdateKind::XButton1Released:
  166. // return "XButton1Released";
  167. // case PointerUpdateKind::XButton2Pressed:
  168. // return "XButton2Pressed";
  169. // case PointerUpdateKind::XButton2Released:
  170. // return "XButton2Released";
  171. // }
  172. //
  173. // return "";
  174. //}
  175. static bool
  176. WINRT_IsTouchEvent(Windows::UI::Input::PointerPoint ^pointerPoint)
  177. {
  178. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  179. return true;
  180. #else
  181. using namespace Windows::Devices::Input;
  182. switch (pointerPoint->PointerDevice->PointerDeviceType) {
  183. case PointerDeviceType::Touch:
  184. case PointerDeviceType::Pen:
  185. return true;
  186. default:
  187. return false;
  188. }
  189. #endif
  190. }
  191. void WINRT_ProcessPointerPressedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
  192. {
  193. if (!window) {
  194. return;
  195. }
  196. if ( ! WINRT_IsTouchEvent(pointerPoint)) {
  197. Uint8 button, pressed;
  198. WINRT_GetSDLButtonForPointerPoint(pointerPoint, &button, &pressed);
  199. SDL_assert(pressed == 1);
  200. SDL_SendMouseButton(window, 0, SDL_PRESSED, button);
  201. } else {
  202. Windows::Foundation::Point normalizedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, NormalizeZeroToOne);
  203. Windows::Foundation::Point windowPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, TransformToSDLWindowSize);
  204. SDL_SendTouch(
  205. WINRT_TouchID,
  206. (SDL_FingerID) pointerPoint->PointerId,
  207. window,
  208. SDL_TRUE,
  209. normalizedPoint.X,
  210. normalizedPoint.Y,
  211. pointerPoint->Properties->Pressure);
  212. }
  213. }
  214. void
  215. WINRT_ProcessPointerMovedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
  216. {
  217. if (!window || WINRT_UsingRelativeMouseMode) {
  218. return;
  219. }
  220. Windows::Foundation::Point normalizedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, NormalizeZeroToOne);
  221. Windows::Foundation::Point windowPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, TransformToSDLWindowSize);
  222. if ( ! WINRT_IsTouchEvent(pointerPoint)) {
  223. /* For some odd reason Moved events are used for multiple mouse buttons */
  224. Uint8 button, pressed;
  225. if (WINRT_GetSDLButtonForPointerPoint(pointerPoint, &button, &pressed)) {
  226. SDL_SendMouseButton(window, 0, pressed, button);
  227. }
  228. SDL_SendMouseMotion(window, 0, 0, (int)windowPoint.X, (int)windowPoint.Y);
  229. } else {
  230. SDL_SendTouchMotion(
  231. WINRT_TouchID,
  232. (SDL_FingerID) pointerPoint->PointerId,
  233. window,
  234. normalizedPoint.X,
  235. normalizedPoint.Y,
  236. pointerPoint->Properties->Pressure);
  237. }
  238. }
  239. void WINRT_ProcessPointerReleasedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
  240. {
  241. if (!window) {
  242. return;
  243. }
  244. if (!WINRT_IsTouchEvent(pointerPoint)) {
  245. Uint8 button, pressed;
  246. WINRT_GetSDLButtonForPointerPoint(pointerPoint, &button, &pressed);
  247. SDL_assert(pressed == 0);
  248. SDL_SendMouseButton(window, 0, SDL_RELEASED, button);
  249. } else {
  250. Windows::Foundation::Point normalizedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position, NormalizeZeroToOne);
  251. SDL_SendTouch(
  252. WINRT_TouchID,
  253. (SDL_FingerID) pointerPoint->PointerId,
  254. window,
  255. SDL_FALSE,
  256. normalizedPoint.X,
  257. normalizedPoint.Y,
  258. pointerPoint->Properties->Pressure);
  259. }
  260. }
  261. void WINRT_ProcessPointerEnteredEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
  262. {
  263. if (!window) {
  264. return;
  265. }
  266. if (!WINRT_IsTouchEvent(pointerPoint)) {
  267. SDL_SetMouseFocus(window);
  268. }
  269. }
  270. void WINRT_ProcessPointerExitedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
  271. {
  272. if (!window) {
  273. return;
  274. }
  275. if (!WINRT_IsTouchEvent(pointerPoint)) {
  276. SDL_SetMouseFocus(NULL);
  277. }
  278. }
  279. void
  280. WINRT_ProcessPointerWheelChangedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
  281. {
  282. if (!window) {
  283. return;
  284. }
  285. float motion = (float) pointerPoint->Properties->MouseWheelDelta / WHEEL_DELTA;
  286. SDL_SendMouseWheel(window, 0, 0, (float) motion, SDL_MOUSEWHEEL_NORMAL);
  287. }
  288. void
  289. WINRT_ProcessMouseMovedEvent(SDL_Window * window, Windows::Devices::Input::MouseEventArgs ^args)
  290. {
  291. if (!window || !WINRT_UsingRelativeMouseMode) {
  292. return;
  293. }
  294. // DLudwig, 2012-12-28: On some systems, namely Visual Studio's Windows
  295. // Simulator, as well as Windows 8 in a Parallels 8 VM, MouseEventArgs'
  296. // MouseDelta field often reports very large values. More information
  297. // on this can be found at the following pages on MSDN:
  298. // - http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/a3c789fa-f1c5-49c4-9c0a-7db88d0f90f8
  299. // - https://connect.microsoft.com/VisualStudio/Feedback/details/756515
  300. //
  301. // The values do not appear to be as large when running on some systems,
  302. // most notably a Surface RT. Furthermore, the values returned by
  303. // CoreWindow's PointerMoved event, and sent to this class' OnPointerMoved
  304. // method, do not ever appear to be large, even when MouseEventArgs'
  305. // MouseDelta is reporting to the contrary.
  306. //
  307. // On systems with the large-values behavior, it appears that the values
  308. // get reported as if the screen's size is 65536 units in both the X and Y
  309. // dimensions. This can be viewed by using Windows' now-private, "Raw Input"
  310. // APIs. (GetRawInputData, RegisterRawInputDevices, WM_INPUT, etc.)
  311. //
  312. // MSDN's documentation on MouseEventArgs' MouseDelta field (at
  313. // http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.input.mouseeventargs.mousedelta ),
  314. // does not seem to indicate (to me) that its values should be so large. It
  315. // says that its values should be a "change in screen location". I could
  316. // be misinterpreting this, however a post on MSDN from a Microsoft engineer (see:
  317. // http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/09a9868e-95bb-4858-ba1a-cb4d2c298d62 ),
  318. // indicates that these values are in DIPs, which is the same unit used
  319. // by CoreWindow's PointerMoved events (via the Position field in its CurrentPoint
  320. // property. See http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.input.pointerpoint.position.aspx
  321. // for details.)
  322. //
  323. // To note, PointerMoved events are sent a 'RawPosition' value (via the
  324. // CurrentPoint property in MouseEventArgs), however these do not seem
  325. // to exhibit the same large-value behavior.
  326. //
  327. // The values passed via PointerMoved events can't always be used for relative
  328. // mouse motion, unfortunately. Its values are bound to the cursor's position,
  329. // which stops when it hits one of the screen's edges. This can be a problem in
  330. // first person shooters, whereby it is normal for mouse motion to travel far
  331. // along any one axis for a period of time. MouseMoved events do not have the
  332. // screen-bounding limitation, and can be used regardless of where the system's
  333. // cursor is.
  334. //
  335. // One possible workaround would be to programmatically set the cursor's
  336. // position to the screen's center (when SDL's relative mouse mode is enabled),
  337. // however WinRT does not yet seem to have the ability to set the cursor's
  338. // position via a public API. Win32 did this via an API call, SetCursorPos,
  339. // however WinRT makes this function be private. Apps that use it won't get
  340. // approved for distribution in the Windows Store. I've yet to be able to find
  341. // a suitable, store-friendly counterpart for WinRT.
  342. //
  343. // There may be some room for a workaround whereby OnPointerMoved's values
  344. // are compared to the values from OnMouseMoved in order to detect
  345. // when this bug is active. A suitable transformation could then be made to
  346. // OnMouseMoved's values. For now, however, the system-reported values are sent
  347. // to SDL with minimal transformation: from native screen coordinates (in DIPs)
  348. // to SDL window coordinates.
  349. //
  350. const Windows::Foundation::Point mouseDeltaInDIPs((float)args->MouseDelta.X, (float)args->MouseDelta.Y);
  351. const Windows::Foundation::Point mouseDeltaInSDLWindowCoords = WINRT_TransformCursorPosition(window, mouseDeltaInDIPs, TransformToSDLWindowSize);
  352. SDL_SendMouseMotion(
  353. window,
  354. 0,
  355. 1,
  356. SDL_lroundf(mouseDeltaInSDLWindowCoords.X),
  357. SDL_lroundf(mouseDeltaInSDLWindowCoords.Y));
  358. }
  359. #endif // SDL_VIDEO_DRIVER_WINRT