src/video/winrt/SDL_winrtmouse.cpp
author David Ludwig <dludwig@pobox.com>
Mon, 02 Sep 2013 15:29:46 -0400
changeset 8516 f3e0e381bdcd
parent 8515 bc6cf9201dab
child 8522 3ae9dc9c439f
permissions -rw-r--r--
WinRT: renamed a mouse-related header file for naming-consistency's sake
dludwig@8500
     1
/*
dludwig@8500
     2
  Simple DirectMedia Layer
dludwig@8500
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
dludwig@8500
     4
dludwig@8500
     5
  This software is provided 'as-is', without any express or implied
dludwig@8500
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8500
     7
  arising from the use of this software.
dludwig@8500
     8
dludwig@8500
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8500
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8500
    11
  freely, subject to the following restrictions:
dludwig@8500
    12
dludwig@8500
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8500
    14
     claim that you wrote the original software. If you use this software
dludwig@8500
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8500
    16
     appreciated but is not required.
dludwig@8500
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8500
    18
     misrepresented as being the original software.
dludwig@8500
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8500
    20
*/
dludwig@8500
    21
dludwig@8500
    22
#include "SDL_config.h"
dludwig@8500
    23
dludwig@8500
    24
#if SDL_VIDEO_DRIVER_WINRT
dludwig@8500
    25
dludwig@8500
    26
/*
dludwig@8500
    27
 * Windows includes:
dludwig@8500
    28
 */
dludwig@8500
    29
#include <Windows.h>
dludwig@8500
    30
using namespace Windows::UI::Core;
dludwig@8500
    31
using Windows::UI::Core::CoreCursor;
dludwig@8500
    32
dludwig@8500
    33
/*
dludwig@8500
    34
 * SDL includes:
dludwig@8500
    35
 */
dludwig@8500
    36
extern "C" {
dludwig@8500
    37
#include "SDL_assert.h"
dludwig@8500
    38
#include "../../events/SDL_mouse_c.h"
dludwig@8513
    39
#include "../../events/SDL_touch_c.h"
dludwig@8500
    40
#include "../SDL_sysvideo.h"
dludwig@8500
    41
#include "SDL_events.h"
dludwig@8500
    42
#include "SDL_log.h"
dludwig@8500
    43
}
dludwig@8500
    44
dludwig@8500
    45
#include "../../core/winrt/SDL_winrtapp.h"
dludwig@8512
    46
#include "SDL_winrtvideo_cpp.h"
dludwig@8516
    47
#include "SDL_winrtmouse_c.h"
dludwig@8500
    48
dludwig@8500
    49
dludwig@8515
    50
extern "C" SDL_bool WINRT_UsingRelativeMouseMode = SDL_FALSE;
dludwig@8500
    51
dludwig@8500
    52
dludwig@8500
    53
static SDL_Cursor *
dludwig@8500
    54
WINRT_CreateSystemCursor(SDL_SystemCursor id)
dludwig@8500
    55
{
dludwig@8500
    56
    SDL_Cursor *cursor;
dludwig@8500
    57
    CoreCursorType cursorType = CoreCursorType::Arrow;
dludwig@8500
    58
dludwig@8500
    59
    switch(id)
dludwig@8500
    60
    {
dludwig@8500
    61
    default:
dludwig@8500
    62
        SDL_assert(0);
dludwig@8500
    63
        return NULL;
dludwig@8500
    64
    case SDL_SYSTEM_CURSOR_ARROW:     cursorType = CoreCursorType::Arrow; break;
dludwig@8500
    65
    case SDL_SYSTEM_CURSOR_IBEAM:     cursorType = CoreCursorType::IBeam; break;
dludwig@8500
    66
    case SDL_SYSTEM_CURSOR_WAIT:      cursorType = CoreCursorType::Wait; break;
dludwig@8500
    67
    case SDL_SYSTEM_CURSOR_CROSSHAIR: cursorType = CoreCursorType::Cross; break;
dludwig@8500
    68
    case SDL_SYSTEM_CURSOR_WAITARROW: cursorType = CoreCursorType::Wait; break;
dludwig@8500
    69
    case SDL_SYSTEM_CURSOR_SIZENWSE:  cursorType = CoreCursorType::SizeNorthwestSoutheast; break;
dludwig@8500
    70
    case SDL_SYSTEM_CURSOR_SIZENESW:  cursorType = CoreCursorType::SizeNortheastSouthwest; break;
dludwig@8500
    71
    case SDL_SYSTEM_CURSOR_SIZEWE:    cursorType = CoreCursorType::SizeWestEast; break;
dludwig@8500
    72
    case SDL_SYSTEM_CURSOR_SIZENS:    cursorType = CoreCursorType::SizeNorthSouth; break;
dludwig@8500
    73
    case SDL_SYSTEM_CURSOR_SIZEALL:   cursorType = CoreCursorType::SizeAll; break;
dludwig@8500
    74
    case SDL_SYSTEM_CURSOR_NO:        cursorType = CoreCursorType::UniversalNo; break;
dludwig@8500
    75
    case SDL_SYSTEM_CURSOR_HAND:      cursorType = CoreCursorType::Hand; break;
dludwig@8500
    76
    }
dludwig@8500
    77
dludwig@8500
    78
    cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
dludwig@8500
    79
    if (cursor) {
dludwig@8500
    80
        /* Create a pointer to a COM reference to a cursor.  The extra
dludwig@8500
    81
           pointer is used (on top of the COM reference) to allow the cursor
dludwig@8500
    82
           to be referenced by the SDL_cursor's driverdata field, which is
dludwig@8500
    83
           a void pointer.
dludwig@8500
    84
        */
dludwig@8500
    85
        CoreCursor ^* theCursor = new CoreCursor^(nullptr);
dludwig@8500
    86
        *theCursor = ref new CoreCursor(cursorType, 0);
dludwig@8500
    87
        cursor->driverdata = (void *) theCursor;
dludwig@8500
    88
    } else {
dludwig@8500
    89
        SDL_OutOfMemory();
dludwig@8500
    90
    }
dludwig@8500
    91
dludwig@8500
    92
    return cursor;
dludwig@8500
    93
}
dludwig@8500
    94
dludwig@8500
    95
static SDL_Cursor *
dludwig@8500
    96
WINRT_CreateDefaultCursor()
dludwig@8500
    97
{
dludwig@8500
    98
    return WINRT_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
dludwig@8500
    99
}
dludwig@8500
   100
dludwig@8500
   101
static void
dludwig@8500
   102
WINRT_FreeCursor(SDL_Cursor * cursor)
dludwig@8500
   103
{
dludwig@8500
   104
    if (cursor->driverdata) {
dludwig@8500
   105
        CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
dludwig@8500
   106
        *theCursor = nullptr;       // Release the COM reference to the CoreCursor
dludwig@8500
   107
        delete theCursor;           // Delete the pointer to the COM reference
dludwig@8500
   108
    }
dludwig@8500
   109
    SDL_free(cursor);
dludwig@8500
   110
}
dludwig@8500
   111
dludwig@8500
   112
static int
dludwig@8500
   113
WINRT_ShowCursor(SDL_Cursor * cursor)
dludwig@8500
   114
{
dludwig@8505
   115
    // TODO, WinRT, XAML: make WINRT_ShowCursor work when XAML support is enabled.
dludwig@8505
   116
    if ( ! CoreWindow::GetForCurrentThread()) {
dludwig@8505
   117
        return 0;
dludwig@8505
   118
    }
dludwig@8505
   119
dludwig@8500
   120
    if (cursor) {
dludwig@8500
   121
        CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
dludwig@8500
   122
        CoreWindow::GetForCurrentThread()->PointerCursor = *theCursor;
dludwig@8500
   123
    } else {
dludwig@8500
   124
        CoreWindow::GetForCurrentThread()->PointerCursor = nullptr;
dludwig@8500
   125
    }
dludwig@8500
   126
    return 0;
dludwig@8500
   127
}
dludwig@8500
   128
dludwig@8500
   129
static int
dludwig@8500
   130
WINRT_SetRelativeMouseMode(SDL_bool enabled)
dludwig@8500
   131
{
dludwig@8515
   132
    WINRT_UsingRelativeMouseMode = enabled;
dludwig@8500
   133
    return 0;
dludwig@8500
   134
}
dludwig@8500
   135
dludwig@8500
   136
void
dludwig@8500
   137
WINRT_InitMouse(_THIS)
dludwig@8500
   138
{
dludwig@8500
   139
    SDL_Mouse *mouse = SDL_GetMouse();
dludwig@8500
   140
dludwig@8500
   141
    /* DLudwig, Dec 3, 2012: WinRT does not currently provide APIs for
dludwig@8500
   142
       the following features, AFAIK:
dludwig@8500
   143
        - custom cursors  (multiple system cursors are, however, available)
dludwig@8500
   144
        - programmatically moveable cursors
dludwig@8500
   145
    */
dludwig@8500
   146
dludwig@8500
   147
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
dludwig@8500
   148
    //mouse->CreateCursor = WINRT_CreateCursor;
dludwig@8500
   149
    mouse->CreateSystemCursor = WINRT_CreateSystemCursor;
dludwig@8500
   150
    mouse->ShowCursor = WINRT_ShowCursor;
dludwig@8500
   151
    mouse->FreeCursor = WINRT_FreeCursor;
dludwig@8500
   152
    //mouse->WarpMouse = WINRT_WarpMouse;
dludwig@8500
   153
    mouse->SetRelativeMouseMode = WINRT_SetRelativeMouseMode;
dludwig@8500
   154
dludwig@8500
   155
    SDL_SetDefaultCursor(WINRT_CreateDefaultCursor());
dludwig@8500
   156
#endif
dludwig@8500
   157
}
dludwig@8500
   158
dludwig@8500
   159
void
dludwig@8500
   160
WINRT_QuitMouse(_THIS)
dludwig@8500
   161
{
dludwig@8500
   162
}
dludwig@8500
   163
dludwig@8500
   164
#endif /* SDL_VIDEO_DRIVER_WINRT */
dludwig@8500
   165
dludwig@8500
   166
/* vi: set ts=4 sw=4 expandtab: */