src/video/winrt/SDL_winrtmouse.cpp
author Sam Lantinga <slouken@libsdl.org>
Thu, 13 Mar 2014 00:40:08 -0700
changeset 8615 097646deaef2
parent 8609 d59f0f12bf07
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Fixed the copyright date on files contributed by David Ludwig
slouken@8582
     1
/*
slouken@8582
     2
  Simple DirectMedia Layer
slouken@8615
     3
  Copyright (C) 1997-2014 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>
slouken@8582
    29
using namespace Windows::UI::Core;
slouken@8582
    30
using Windows::UI::Core::CoreCursor;
slouken@8582
    31
slouken@8582
    32
/*
slouken@8582
    33
 * SDL includes:
slouken@8582
    34
 */
slouken@8582
    35
extern "C" {
slouken@8582
    36
#include "SDL_assert.h"
slouken@8582
    37
#include "../../events/SDL_mouse_c.h"
slouken@8582
    38
#include "../../events/SDL_touch_c.h"
slouken@8582
    39
#include "../SDL_sysvideo.h"
slouken@8582
    40
#include "SDL_events.h"
slouken@8582
    41
#include "SDL_log.h"
slouken@8582
    42
}
slouken@8582
    43
slouken@8582
    44
#include "../../core/winrt/SDL_winrtapp_direct3d.h"
slouken@8582
    45
#include "SDL_winrtvideo_cpp.h"
slouken@8582
    46
#include "SDL_winrtmouse_c.h"
slouken@8582
    47
slouken@8582
    48
slouken@8582
    49
extern "C" SDL_bool WINRT_UsingRelativeMouseMode = SDL_FALSE;
slouken@8582
    50
slouken@8582
    51
slouken@8582
    52
static SDL_Cursor *
slouken@8582
    53
WINRT_CreateSystemCursor(SDL_SystemCursor id)
slouken@8582
    54
{
slouken@8582
    55
    SDL_Cursor *cursor;
slouken@8582
    56
    CoreCursorType cursorType = CoreCursorType::Arrow;
slouken@8582
    57
slouken@8582
    58
    switch(id)
slouken@8582
    59
    {
slouken@8582
    60
    default:
slouken@8582
    61
        SDL_assert(0);
slouken@8582
    62
        return NULL;
slouken@8582
    63
    case SDL_SYSTEM_CURSOR_ARROW:     cursorType = CoreCursorType::Arrow; break;
slouken@8582
    64
    case SDL_SYSTEM_CURSOR_IBEAM:     cursorType = CoreCursorType::IBeam; break;
slouken@8582
    65
    case SDL_SYSTEM_CURSOR_WAIT:      cursorType = CoreCursorType::Wait; break;
slouken@8582
    66
    case SDL_SYSTEM_CURSOR_CROSSHAIR: cursorType = CoreCursorType::Cross; break;
slouken@8582
    67
    case SDL_SYSTEM_CURSOR_WAITARROW: cursorType = CoreCursorType::Wait; break;
slouken@8582
    68
    case SDL_SYSTEM_CURSOR_SIZENWSE:  cursorType = CoreCursorType::SizeNorthwestSoutheast; break;
slouken@8582
    69
    case SDL_SYSTEM_CURSOR_SIZENESW:  cursorType = CoreCursorType::SizeNortheastSouthwest; break;
slouken@8582
    70
    case SDL_SYSTEM_CURSOR_SIZEWE:    cursorType = CoreCursorType::SizeWestEast; break;
slouken@8582
    71
    case SDL_SYSTEM_CURSOR_SIZENS:    cursorType = CoreCursorType::SizeNorthSouth; break;
slouken@8582
    72
    case SDL_SYSTEM_CURSOR_SIZEALL:   cursorType = CoreCursorType::SizeAll; break;
slouken@8582
    73
    case SDL_SYSTEM_CURSOR_NO:        cursorType = CoreCursorType::UniversalNo; break;
slouken@8582
    74
    case SDL_SYSTEM_CURSOR_HAND:      cursorType = CoreCursorType::Hand; break;
slouken@8582
    75
    }
slouken@8582
    76
slouken@8582
    77
    cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
slouken@8582
    78
    if (cursor) {
slouken@8582
    79
        /* Create a pointer to a COM reference to a cursor.  The extra
slouken@8582
    80
           pointer is used (on top of the COM reference) to allow the cursor
slouken@8582
    81
           to be referenced by the SDL_cursor's driverdata field, which is
slouken@8582
    82
           a void pointer.
slouken@8582
    83
        */
slouken@8582
    84
        CoreCursor ^* theCursor = new CoreCursor^(nullptr);
slouken@8582
    85
        *theCursor = ref new CoreCursor(cursorType, 0);
slouken@8582
    86
        cursor->driverdata = (void *) theCursor;
slouken@8582
    87
    } else {
slouken@8582
    88
        SDL_OutOfMemory();
slouken@8582
    89
    }
slouken@8582
    90
slouken@8582
    91
    return cursor;
slouken@8582
    92
}
slouken@8582
    93
slouken@8582
    94
static SDL_Cursor *
slouken@8582
    95
WINRT_CreateDefaultCursor()
slouken@8582
    96
{
slouken@8582
    97
    return WINRT_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
slouken@8582
    98
}
slouken@8582
    99
slouken@8582
   100
static void
slouken@8582
   101
WINRT_FreeCursor(SDL_Cursor * cursor)
slouken@8582
   102
{
slouken@8582
   103
    if (cursor->driverdata) {
slouken@8582
   104
        CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
slouken@8582
   105
        *theCursor = nullptr;       // Release the COM reference to the CoreCursor
slouken@8582
   106
        delete theCursor;           // Delete the pointer to the COM reference
slouken@8582
   107
    }
slouken@8582
   108
    SDL_free(cursor);
slouken@8582
   109
}
slouken@8582
   110
slouken@8582
   111
static int
slouken@8582
   112
WINRT_ShowCursor(SDL_Cursor * cursor)
slouken@8582
   113
{
slouken@8582
   114
    // TODO, WinRT, XAML: make WINRT_ShowCursor work when XAML support is enabled.
slouken@8582
   115
    if ( ! CoreWindow::GetForCurrentThread()) {
slouken@8582
   116
        return 0;
slouken@8582
   117
    }
slouken@8582
   118
slouken@8582
   119
    if (cursor) {
slouken@8582
   120
        CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
slouken@8582
   121
        CoreWindow::GetForCurrentThread()->PointerCursor = *theCursor;
slouken@8582
   122
    } else {
slouken@8582
   123
        CoreWindow::GetForCurrentThread()->PointerCursor = nullptr;
slouken@8582
   124
    }
slouken@8582
   125
    return 0;
slouken@8582
   126
}
slouken@8582
   127
slouken@8582
   128
static int
slouken@8582
   129
WINRT_SetRelativeMouseMode(SDL_bool enabled)
slouken@8582
   130
{
slouken@8582
   131
    WINRT_UsingRelativeMouseMode = enabled;
slouken@8582
   132
    return 0;
slouken@8582
   133
}
slouken@8582
   134
slouken@8582
   135
void
slouken@8582
   136
WINRT_InitMouse(_THIS)
slouken@8582
   137
{
slouken@8582
   138
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@8582
   139
slouken@8582
   140
    /* DLudwig, Dec 3, 2012: WinRT does not currently provide APIs for
slouken@8582
   141
       the following features, AFAIK:
slouken@8582
   142
        - custom cursors  (multiple system cursors are, however, available)
slouken@8582
   143
        - programmatically moveable cursors
slouken@8582
   144
    */
slouken@8582
   145
slouken@8582
   146
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
slouken@8582
   147
    //mouse->CreateCursor = WINRT_CreateCursor;
slouken@8582
   148
    mouse->CreateSystemCursor = WINRT_CreateSystemCursor;
slouken@8582
   149
    mouse->ShowCursor = WINRT_ShowCursor;
slouken@8582
   150
    mouse->FreeCursor = WINRT_FreeCursor;
slouken@8582
   151
    //mouse->WarpMouse = WINRT_WarpMouse;
slouken@8582
   152
    mouse->SetRelativeMouseMode = WINRT_SetRelativeMouseMode;
slouken@8582
   153
slouken@8582
   154
    SDL_SetDefaultCursor(WINRT_CreateDefaultCursor());
slouken@8582
   155
#endif
slouken@8582
   156
}
slouken@8582
   157
slouken@8582
   158
void
slouken@8582
   159
WINRT_QuitMouse(_THIS)
slouken@8582
   160
{
slouken@8582
   161
}
slouken@8582
   162
slouken@8582
   163
#endif /* SDL_VIDEO_DRIVER_WINRT */
slouken@8582
   164
slouken@8582
   165
/* vi: set ts=4 sw=4 expandtab: */