2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_WINRT
29 #include <windows.ui.core.h>
30 using namespace Windows::UI::Core;
31 using Windows::UI::Core::CoreCursor;
37 #include "../../events/SDL_mouse_c.h"
38 #include "../../events/SDL_touch_c.h"
39 #include "../SDL_sysvideo.h"
40 #include "SDL_events.h"
43 #include "../../core/winrt/SDL_winrtapp_direct3d.h"
44 #include "SDL_winrtvideo_cpp.h"
45 #include "SDL_winrtmouse_c.h"
48 extern "C" SDL_bool WINRT_UsingRelativeMouseMode = SDL_FALSE;
52 WINRT_CreateSystemCursor(SDL_SystemCursor id)
55 CoreCursorType cursorType = CoreCursorType::Arrow;
62 case SDL_SYSTEM_CURSOR_ARROW: cursorType = CoreCursorType::Arrow; break;
63 case SDL_SYSTEM_CURSOR_IBEAM: cursorType = CoreCursorType::IBeam; break;
64 case SDL_SYSTEM_CURSOR_WAIT: cursorType = CoreCursorType::Wait; break;
65 case SDL_SYSTEM_CURSOR_CROSSHAIR: cursorType = CoreCursorType::Cross; break;
66 case SDL_SYSTEM_CURSOR_WAITARROW: cursorType = CoreCursorType::Wait; break;
67 case SDL_SYSTEM_CURSOR_SIZENWSE: cursorType = CoreCursorType::SizeNorthwestSoutheast; break;
68 case SDL_SYSTEM_CURSOR_SIZENESW: cursorType = CoreCursorType::SizeNortheastSouthwest; break;
69 case SDL_SYSTEM_CURSOR_SIZEWE: cursorType = CoreCursorType::SizeWestEast; break;
70 case SDL_SYSTEM_CURSOR_SIZENS: cursorType = CoreCursorType::SizeNorthSouth; break;
71 case SDL_SYSTEM_CURSOR_SIZEALL: cursorType = CoreCursorType::SizeAll; break;
72 case SDL_SYSTEM_CURSOR_NO: cursorType = CoreCursorType::UniversalNo; break;
73 case SDL_SYSTEM_CURSOR_HAND: cursorType = CoreCursorType::Hand; break;
76 cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
78 /* Create a pointer to a COM reference to a cursor. The extra
79 pointer is used (on top of the COM reference) to allow the cursor
80 to be referenced by the SDL_cursor's driverdata field, which is
83 CoreCursor ^* theCursor = new CoreCursor^(nullptr);
84 *theCursor = ref new CoreCursor(cursorType, 0);
85 cursor->driverdata = (void *) theCursor;
94 WINRT_CreateDefaultCursor()
96 return WINRT_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
100 WINRT_FreeCursor(SDL_Cursor * cursor)
102 if (cursor->driverdata) {
103 CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
104 *theCursor = nullptr; // Release the COM reference to the CoreCursor
105 delete theCursor; // Delete the pointer to the COM reference
111 WINRT_ShowCursor(SDL_Cursor * cursor)
113 // TODO, WinRT, XAML: make WINRT_ShowCursor work when XAML support is enabled.
114 if ( ! CoreWindow::GetForCurrentThread()) {
118 CoreWindow ^ coreWindow = CoreWindow::GetForCurrentThread();
120 CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
121 coreWindow->PointerCursor = *theCursor;
123 // HACK ALERT: TL;DR - Hiding the cursor in WinRT/UWP apps is weird, and
124 // a Win32-style cursor resource file must be directly included in apps,
125 // otherwise hiding the cursor will cause mouse-motion data to never be
128 // Here's the lengthy explanation:
130 // There are two ways to hide a cursor in WinRT/UWP apps.
131 // Both involve setting the WinRT CoreWindow's (which is somewhat analogous
132 // to a Win32 HWND) 'PointerCursor' property.
134 // The first way to hide a cursor sets PointerCursor to nullptr. This
135 // is, arguably, the easiest to implement for an app. It does have an
136 // unfortunate side-effect: it'll prevent mouse-motion events from being
137 // sent to the app (via CoreWindow).
139 // The second way to hide a cursor sets PointerCursor to a transparent
140 // cursor. This allows mouse-motion events to be sent to the app, but is
141 // more difficult to set up, as:
142 // 1. WinRT/UWP, while providing a few stock cursors, does not provide
143 // a completely transparent cursor.
144 // 2. WinRT/UWP allows apps to provide custom-built cursors, but *ONLY*
145 // if they are linked directly inside the app, via Win32-style
146 // cursor resource files. APIs to create cursors at runtime are
147 // not provided to apps, and attempting to link-to or use Win32
148 // cursor-creation APIs could cause an app to fail Windows Store
151 // SDL can use either means of hiding the cursor. It provides a Win32-style
152 // set of cursor resource files in its source distribution, inside
153 // src/main/winrt/. If those files are linked to an SDL-for-WinRT/UWP app
154 // (by including them in a MSVC project, for example), SDL will attempt to
155 // use those, if and when the cursor is hidden via SDL APIs. If those
156 // files are not linked in, SDL will attempt to hide the cursor via the
157 // 'set PointerCursor to nullptr' means (which, if you recall, causes
158 // mouse-motion data to NOT be sent to the app!).
161 // - SDL's blank cursor resource uses a resource ID of 5000.
162 // - SDL's cursor resources consist of the following two files:
163 // - src/main/winrt/SDL2-WinRTResource_BlankCursor.cur -- cursor pixel data
164 // - src/main/winrt/SDL2-WinRTResources.rc -- declares the cursor resource, and its ID (of 5000)
167 const unsigned int win32CursorResourceID = 5000;
168 CoreCursor ^ blankCursor = ref new CoreCursor(CoreCursorType::Custom, win32CursorResourceID);
170 // Set 'PointerCursor' to 'blankCursor' in a way that shouldn't throw
171 // an exception if the app hasn't loaded that resource.
172 ABI::Windows::UI::Core::ICoreCursor * iblankCursor = reinterpret_cast<ABI::Windows::UI::Core::ICoreCursor *>(blankCursor);
173 ABI::Windows::UI::Core::ICoreWindow * icoreWindow = reinterpret_cast<ABI::Windows::UI::Core::ICoreWindow *>(coreWindow);
174 HRESULT hr = icoreWindow->put_PointerCursor(iblankCursor);
176 // The app doesn't contain the cursor resource, or some other error
177 // occurred. Just use the other, but mouse-motion-preventing, means of
178 // hiding the cursor.
179 coreWindow->PointerCursor = nullptr;
186 WINRT_SetRelativeMouseMode(SDL_bool enabled)
188 WINRT_UsingRelativeMouseMode = enabled;
193 WINRT_InitMouse(_THIS)
195 SDL_Mouse *mouse = SDL_GetMouse();
197 /* DLudwig, Dec 3, 2012: WinRT does not currently provide APIs for
198 the following features, AFAIK:
199 - custom cursors (multiple system cursors are, however, available)
200 - programmatically moveable cursors
203 #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
204 //mouse->CreateCursor = WINRT_CreateCursor;
205 mouse->CreateSystemCursor = WINRT_CreateSystemCursor;
206 mouse->ShowCursor = WINRT_ShowCursor;
207 mouse->FreeCursor = WINRT_FreeCursor;
208 //mouse->WarpMouse = WINRT_WarpMouse;
209 mouse->SetRelativeMouseMode = WINRT_SetRelativeMouseMode;
211 SDL_SetDefaultCursor(WINRT_CreateDefaultCursor());
216 WINRT_QuitMouse(_THIS)
220 #endif /* SDL_VIDEO_DRIVER_WINRT */
222 /* vi: set ts=4 sw=4 expandtab: */