Imported Upstream version 2.0.14
[platform/upstream/SDL.git] / src / video / winrt / SDL_winrtmouse.cpp
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 "../../events/SDL_mouse_c.h"
38 #include "../../events/SDL_touch_c.h"
39 #include "../SDL_sysvideo.h"
40 #include "SDL_events.h"
41 }
42
43 #include "../../core/winrt/SDL_winrtapp_direct3d.h"
44 #include "SDL_winrtvideo_cpp.h"
45 #include "SDL_winrtmouse_c.h"
46
47
48 extern "C" SDL_bool WINRT_UsingRelativeMouseMode = SDL_FALSE;
49
50
51 static SDL_Cursor *
52 WINRT_CreateSystemCursor(SDL_SystemCursor id)
53 {
54     SDL_Cursor *cursor;
55     CoreCursorType cursorType = CoreCursorType::Arrow;
56
57     switch(id)
58     {
59     default:
60         SDL_assert(0);
61         return NULL;
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;
74     }
75
76     cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
77     if (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
81            a void pointer.
82         */
83         CoreCursor ^* theCursor = new CoreCursor^(nullptr);
84         *theCursor = ref new CoreCursor(cursorType, 0);
85         cursor->driverdata = (void *) theCursor;
86     } else {
87         SDL_OutOfMemory();
88     }
89
90     return cursor;
91 }
92
93 static SDL_Cursor *
94 WINRT_CreateDefaultCursor()
95 {
96     return WINRT_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
97 }
98
99 static void
100 WINRT_FreeCursor(SDL_Cursor * cursor)
101 {
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
106     }
107     SDL_free(cursor);
108 }
109
110 static int
111 WINRT_ShowCursor(SDL_Cursor * cursor)
112 {
113     // TODO, WinRT, XAML: make WINRT_ShowCursor work when XAML support is enabled.
114     if ( ! CoreWindow::GetForCurrentThread()) {
115         return 0;
116     }
117
118     CoreWindow ^ coreWindow = CoreWindow::GetForCurrentThread();
119     if (cursor) {
120         CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
121         coreWindow->PointerCursor = *theCursor;
122     } else {
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
126         //   received.
127         //
128         // Here's the lengthy explanation:
129         //
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.
133         //
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).
138         //
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
149         //      certification.
150         //
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!).
159         //
160         // Tech notes:
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)
165         //
166
167         const unsigned int win32CursorResourceID = 5000;  
168         CoreCursor ^ blankCursor = ref new CoreCursor(CoreCursorType::Custom, win32CursorResourceID);
169
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);
175         if (FAILED(hr)) {
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;
180         }
181     }
182     return 0;
183 }
184
185 static int
186 WINRT_SetRelativeMouseMode(SDL_bool enabled)
187 {
188     WINRT_UsingRelativeMouseMode = enabled;
189     return 0;
190 }
191
192 void
193 WINRT_InitMouse(_THIS)
194 {
195     SDL_Mouse *mouse = SDL_GetMouse();
196
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
201     */
202
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;
210
211     SDL_SetDefaultCursor(WINRT_CreateDefaultCursor());
212 #endif
213 }
214
215 void
216 WINRT_QuitMouse(_THIS)
217 {
218 }
219
220 #endif /* SDL_VIDEO_DRIVER_WINRT */
221
222 /* vi: set ts=4 sw=4 expandtab: */