src/video/winrt/SDL_winrtmouse.cpp
author Sam Lantinga
Sun, 09 Mar 2014 11:06:11 -0700
changeset 8582 c3e9a2b93517
parent 8522 3ae9dc9c439f
child 8600 092802455aed
permissions -rw-r--r--
Fixed line endings on WinRT source code
slouken@8582
     1
/*
slouken@8582
     2
  Simple DirectMedia Layer
slouken@8582
     3
  Copyright (C) 1997-2012 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
*/
slouken@8582
    21
slouken@8582
    22
#include "SDL_config.h"
slouken@8582
    23
slouken@8582
    24
#if SDL_VIDEO_DRIVER_WINRT
slouken@8582
    25
slouken@8582
    26
/*
slouken@8582
    27
 * Windows includes:
slouken@8582
    28
 */
slouken@8582
    29
#include <Windows.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
slouken@8582
   120
    if (cursor) {
slouken@8582
   121
        CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
slouken@8582
   122
        CoreWindow::GetForCurrentThread()->PointerCursor = *theCursor;
slouken@8582
   123
    } else {
slouken@8582
   124
        CoreWindow::GetForCurrentThread()->PointerCursor = nullptr;
slouken@8582
   125
    }
slouken@8582
   126
    return 0;
slouken@8582
   127
}
slouken@8582
   128
slouken@8582
   129
static int
slouken@8582
   130
WINRT_SetRelativeMouseMode(SDL_bool enabled)
slouken@8582
   131
{
slouken@8582
   132
    WINRT_UsingRelativeMouseMode = enabled;
slouken@8582
   133
    return 0;
slouken@8582
   134
}
slouken@8582
   135
slouken@8582
   136
void
slouken@8582
   137
WINRT_InitMouse(_THIS)
slouken@8582
   138
{
slouken@8582
   139
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@8582
   140
slouken@8582
   141
    /* DLudwig, Dec 3, 2012: WinRT does not currently provide APIs for
slouken@8582
   142
       the following features, AFAIK:
slouken@8582
   143
        - custom cursors  (multiple system cursors are, however, available)
slouken@8582
   144
        - programmatically moveable cursors
slouken@8582
   145
    */
slouken@8582
   146
slouken@8582
   147
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
slouken@8582
   148
    //mouse->CreateCursor = WINRT_CreateCursor;
slouken@8582
   149
    mouse->CreateSystemCursor = WINRT_CreateSystemCursor;
slouken@8582
   150
    mouse->ShowCursor = WINRT_ShowCursor;
slouken@8582
   151
    mouse->FreeCursor = WINRT_FreeCursor;
slouken@8582
   152
    //mouse->WarpMouse = WINRT_WarpMouse;
slouken@8582
   153
    mouse->SetRelativeMouseMode = WINRT_SetRelativeMouseMode;
slouken@8582
   154
slouken@8582
   155
    SDL_SetDefaultCursor(WINRT_CreateDefaultCursor());
slouken@8582
   156
#endif
slouken@8582
   157
}
slouken@8582
   158
slouken@8582
   159
void
slouken@8582
   160
WINRT_QuitMouse(_THIS)
slouken@8582
   161
{
slouken@8582
   162
}
slouken@8582
   163
slouken@8582
   164
#endif /* SDL_VIDEO_DRIVER_WINRT */
slouken@8582
   165
slouken@8582
   166
/* vi: set ts=4 sw=4 expandtab: */