SDL_winrtmouse.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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. /*
  21. * Windows includes:
  22. */
  23. #include <Windows.h>
  24. #include <windows.ui.core.h>
  25. using namespace Windows::UI::Core;
  26. using Windows::UI::Core::CoreCursor;
  27. /*
  28. * SDL includes:
  29. */
  30. extern "C" {
  31. #include "SDL_assert.h"
  32. #include "../../events/SDL_mouse_c.h"
  33. #include "../../events/SDL_touch_c.h"
  34. #include "../SDL_sysvideo.h"
  35. #include "SDL_events.h"
  36. #include "SDL_log.h"
  37. }
  38. #include "../../core/winrt/SDL_winrtapp_direct3d.h"
  39. #include "SDL_winrtvideo_cpp.h"
  40. #include "SDL_winrtmouse_c.h"
  41. extern "C" SDL_bool WINRT_UsingRelativeMouseMode = SDL_FALSE;
  42. static SDL_Cursor *
  43. WINRT_CreateSystemCursor(SDL_SystemCursor id)
  44. {
  45. SDL_Cursor *cursor;
  46. CoreCursorType cursorType = CoreCursorType::Arrow;
  47. switch(id)
  48. {
  49. default:
  50. SDL_assert(0);
  51. return NULL;
  52. case SDL_SYSTEM_CURSOR_ARROW: cursorType = CoreCursorType::Arrow; break;
  53. case SDL_SYSTEM_CURSOR_IBEAM: cursorType = CoreCursorType::IBeam; break;
  54. case SDL_SYSTEM_CURSOR_WAIT: cursorType = CoreCursorType::Wait; break;
  55. case SDL_SYSTEM_CURSOR_CROSSHAIR: cursorType = CoreCursorType::Cross; break;
  56. case SDL_SYSTEM_CURSOR_WAITARROW: cursorType = CoreCursorType::Wait; break;
  57. case SDL_SYSTEM_CURSOR_SIZENWSE: cursorType = CoreCursorType::SizeNorthwestSoutheast; break;
  58. case SDL_SYSTEM_CURSOR_SIZENESW: cursorType = CoreCursorType::SizeNortheastSouthwest; break;
  59. case SDL_SYSTEM_CURSOR_SIZEWE: cursorType = CoreCursorType::SizeWestEast; break;
  60. case SDL_SYSTEM_CURSOR_SIZENS: cursorType = CoreCursorType::SizeNorthSouth; break;
  61. case SDL_SYSTEM_CURSOR_SIZEALL: cursorType = CoreCursorType::SizeAll; break;
  62. case SDL_SYSTEM_CURSOR_NO: cursorType = CoreCursorType::UniversalNo; break;
  63. case SDL_SYSTEM_CURSOR_HAND: cursorType = CoreCursorType::Hand; break;
  64. }
  65. cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
  66. if (cursor) {
  67. /* Create a pointer to a COM reference to a cursor. The extra
  68. pointer is used (on top of the COM reference) to allow the cursor
  69. to be referenced by the SDL_cursor's driverdata field, which is
  70. a void pointer.
  71. */
  72. CoreCursor ^* theCursor = new CoreCursor^(nullptr);
  73. *theCursor = ref new CoreCursor(cursorType, 0);
  74. cursor->driverdata = (void *) theCursor;
  75. } else {
  76. SDL_OutOfMemory();
  77. }
  78. return cursor;
  79. }
  80. static SDL_Cursor *
  81. WINRT_CreateDefaultCursor()
  82. {
  83. return WINRT_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
  84. }
  85. static void
  86. WINRT_FreeCursor(SDL_Cursor * cursor)
  87. {
  88. if (cursor->driverdata) {
  89. CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
  90. *theCursor = nullptr; // Release the COM reference to the CoreCursor
  91. delete theCursor; // Delete the pointer to the COM reference
  92. }
  93. SDL_free(cursor);
  94. }
  95. static int
  96. WINRT_ShowCursor(SDL_Cursor * cursor)
  97. {
  98. // TODO, WinRT, XAML: make WINRT_ShowCursor work when XAML support is enabled.
  99. if ( ! CoreWindow::GetForCurrentThread()) {
  100. return 0;
  101. }
  102. CoreWindow ^ coreWindow = CoreWindow::GetForCurrentThread();
  103. if (cursor) {
  104. CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
  105. coreWindow->PointerCursor = *theCursor;
  106. } else {
  107. // HACK ALERT: TL;DR - Hiding the cursor in WinRT/UWP apps is weird, and
  108. // a Win32-style cursor resource file must be directly included in apps,
  109. // otherwise hiding the cursor will cause mouse-motion data to never be
  110. // received.
  111. //
  112. // Here's the lengthy explanation:
  113. //
  114. // There are two ways to hide a cursor in WinRT/UWP apps.
  115. // Both involve setting the WinRT CoreWindow's (which is somewhat analogous
  116. // to a Win32 HWND) 'PointerCursor' property.
  117. //
  118. // The first way to hide a cursor sets PointerCursor to nullptr. This
  119. // is, arguably, the easiest to implement for an app. It does have an
  120. // unfortunate side-effect: it'll prevent mouse-motion events from being
  121. // sent to the app (via CoreWindow).
  122. //
  123. // The second way to hide a cursor sets PointerCursor to a transparent
  124. // cursor. This allows mouse-motion events to be sent to the app, but is
  125. // more difficult to set up, as:
  126. // 1. WinRT/UWP, while providing a few stock cursors, does not provide
  127. // a completely transparent cursor.
  128. // 2. WinRT/UWP allows apps to provide custom-built cursors, but *ONLY*
  129. // if they are linked directly inside the app, via Win32-style
  130. // cursor resource files. APIs to create cursors at runtime are
  131. // not provided to apps, and attempting to link-to or use Win32
  132. // cursor-creation APIs could cause an app to fail Windows Store
  133. // certification.
  134. //
  135. // SDL can use either means of hiding the cursor. It provides a Win32-style
  136. // set of cursor resource files in its source distribution, inside
  137. // src/main/winrt/. If those files are linked to an SDL-for-WinRT/UWP app
  138. // (by including them in a MSVC project, for example), SDL will attempt to
  139. // use those, if and when the cursor is hidden via SDL APIs. If those
  140. // files are not linked in, SDL will attempt to hide the cursor via the
  141. // 'set PointerCursor to nullptr' means (which, if you recall, causes
  142. // mouse-motion data to NOT be sent to the app!).
  143. //
  144. // Tech notes:
  145. // - SDL's blank cursor resource uses a resource ID of 5000.
  146. // - SDL's cursor resources consist of the following two files:
  147. // - src/main/winrt/SDL2-WinRTResource_BlankCursor.cur -- cursor pixel data
  148. // - src/main/winrt/SDL2-WinRTResources.rc -- declares the cursor resource, and its ID (of 5000)
  149. //
  150. const unsigned int win32CursorResourceID = 5000;
  151. CoreCursor ^ blankCursor = ref new CoreCursor(CoreCursorType::Custom, win32CursorResourceID);
  152. // Set 'PointerCursor' to 'blankCursor' in a way that shouldn't throw
  153. // an exception if the app hasn't loaded that resource.
  154. ABI::Windows::UI::Core::ICoreCursor * iblankCursor = reinterpret_cast<ABI::Windows::UI::Core::ICoreCursor *>(blankCursor);
  155. ABI::Windows::UI::Core::ICoreWindow * icoreWindow = reinterpret_cast<ABI::Windows::UI::Core::ICoreWindow *>(coreWindow);
  156. HRESULT hr = icoreWindow->put_PointerCursor(iblankCursor);
  157. if (FAILED(hr)) {
  158. // The app doesn't contain the cursor resource, or some other error
  159. // occurred. Just use the other, but mouse-motion-preventing, means of
  160. // hiding the cursor.
  161. coreWindow->PointerCursor = nullptr;
  162. }
  163. }
  164. return 0;
  165. }
  166. static int
  167. WINRT_SetRelativeMouseMode(SDL_bool enabled)
  168. {
  169. WINRT_UsingRelativeMouseMode = enabled;
  170. return 0;
  171. }
  172. void
  173. WINRT_InitMouse(_THIS)
  174. {
  175. SDL_Mouse *mouse = SDL_GetMouse();
  176. /* DLudwig, Dec 3, 2012: WinRT does not currently provide APIs for
  177. the following features, AFAIK:
  178. - custom cursors (multiple system cursors are, however, available)
  179. - programmatically moveable cursors
  180. */
  181. #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
  182. //mouse->CreateCursor = WINRT_CreateCursor;
  183. mouse->CreateSystemCursor = WINRT_CreateSystemCursor;
  184. mouse->ShowCursor = WINRT_ShowCursor;
  185. mouse->FreeCursor = WINRT_FreeCursor;
  186. //mouse->WarpMouse = WINRT_WarpMouse;
  187. mouse->SetRelativeMouseMode = WINRT_SetRelativeMouseMode;
  188. SDL_SetDefaultCursor(WINRT_CreateDefaultCursor());
  189. #endif
  190. }
  191. void
  192. WINRT_QuitMouse(_THIS)
  193. {
  194. }
  195. #endif /* SDL_VIDEO_DRIVER_WINRT */
  196. /* vi: set ts=4 sw=4 expandtab: */