SDL_winrtpointerinput.cpp 15 KB

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