SDL_winrtpointerinput.cpp 15 KB

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