src/video/winrt/SDL_winrtmouse.cpp
author Sam Lantinga
Fri, 27 Jan 2017 21:23:27 -0800
changeset 10861 71d8f9afb690
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Fixed bug 3569 - GL_UpdateViewport leaves PROJECTION matrix selected

Tom Seddon

GL_ActivateRenderer may call GL_UpdateViewport, which leaves the GL_PROJECTION matrix selected. But after GL_ResetState, the GL_MODELVIEW matrix is selected, suggesting that's the intended default state.

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