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