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