src/video/winrt/SDL_winrtmouse.cpp
author David Ludwig <dludwig@pobox.com>
Tue, 20 Aug 2013 21:22:32 -0400
changeset 8483 d403ded25942
parent 8437 src/video/windowsrt/SDL_winrtmouse.cpp@d6a6c3ebdf3e
child 8489 7fd62b320c31
permissions -rw-r--r--
WinRT: file naming and placement cleanup
- moved SDL_WinRTApp.* from src/video/windowsrt/ to src/core/winrt/, and renamed them to SDL_winrtapp.* (to mimick case-sensitivity used elsewhere in SDL)
- renamed all "windowsrt" directories (in src) to "winrt", as the shorthand name is used more often (and, IMO, "WinRT" != "Windows RT", not entirely at least)
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 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 
    22 #include "SDL_config.h"
    23 
    24 #if SDL_VIDEO_DRIVER_WINRT
    25 
    26 extern "C" {
    27 #include "SDL_assert.h"
    28 #include "../../events/SDL_mouse_c.h"
    29 #include "../SDL_sysvideo.h"
    30 }
    31 
    32 #include "../../core/winrt/SDL_winrtapp.h"
    33 #include "SDL_winrtmouse.h"
    34 
    35 using namespace Windows::UI::Core;
    36 using Windows::UI::Core::CoreCursor;
    37 
    38 extern SDL_WinRTApp ^ SDL_WinRTGlobalApp;
    39 
    40 
    41 static SDL_Cursor *
    42 WINRT_CreateSystemCursor(SDL_SystemCursor id)
    43 {
    44     SDL_Cursor *cursor;
    45     CoreCursorType cursorType = CoreCursorType::Arrow;
    46 
    47     switch(id)
    48     {
    49     default:
    50         SDL_assert(0);
    51         return NULL;
    52     case SDL_SYSTEM_CURSOR_ARROW:     cursorType = CoreCursorType::Arrow; break;
    53     case SDL_SYSTEM_CURSOR_IBEAM:     cursorType = CoreCursorType::IBeam; break;
    54     case SDL_SYSTEM_CURSOR_WAIT:      cursorType = CoreCursorType::Wait; break;
    55     case SDL_SYSTEM_CURSOR_CROSSHAIR: cursorType = CoreCursorType::Cross; break;
    56     case SDL_SYSTEM_CURSOR_WAITARROW: cursorType = CoreCursorType::Wait; break;
    57     case SDL_SYSTEM_CURSOR_SIZENWSE:  cursorType = CoreCursorType::SizeNorthwestSoutheast; break;
    58     case SDL_SYSTEM_CURSOR_SIZENESW:  cursorType = CoreCursorType::SizeNortheastSouthwest; break;
    59     case SDL_SYSTEM_CURSOR_SIZEWE:    cursorType = CoreCursorType::SizeWestEast; break;
    60     case SDL_SYSTEM_CURSOR_SIZENS:    cursorType = CoreCursorType::SizeNorthSouth; break;
    61     case SDL_SYSTEM_CURSOR_SIZEALL:   cursorType = CoreCursorType::SizeAll; break;
    62     case SDL_SYSTEM_CURSOR_NO:        cursorType = CoreCursorType::UniversalNo; break;
    63     case SDL_SYSTEM_CURSOR_HAND:      cursorType = CoreCursorType::Hand; break;
    64     }
    65 
    66     cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
    67     if (cursor) {
    68         /* Create a pointer to a COM reference to a cursor.  The extra
    69            pointer is used (on top of the COM reference) to allow the cursor
    70            to be referenced by the SDL_cursor's driverdata field, which is
    71            a void pointer.
    72         */
    73         CoreCursor ^* theCursor = new CoreCursor^(nullptr);
    74         *theCursor = ref new CoreCursor(cursorType, 0);
    75         cursor->driverdata = (void *) theCursor;
    76     } else {
    77         SDL_OutOfMemory();
    78     }
    79 
    80     return cursor;
    81 }
    82 
    83 static SDL_Cursor *
    84 WINRT_CreateDefaultCursor()
    85 {
    86     return WINRT_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
    87 }
    88 
    89 static void
    90 WINRT_FreeCursor(SDL_Cursor * cursor)
    91 {
    92     if (cursor->driverdata) {
    93         CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
    94         *theCursor = nullptr;       // Release the COM reference to the CoreCursor
    95         delete theCursor;           // Delete the pointer to the COM reference
    96     }
    97     SDL_free(cursor);
    98 }
    99 
   100 static int
   101 WINRT_ShowCursor(SDL_Cursor * cursor)
   102 {
   103     if (cursor) {
   104         CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
   105         CoreWindow::GetForCurrentThread()->PointerCursor = *theCursor;
   106     } else {
   107         CoreWindow::GetForCurrentThread()->PointerCursor = nullptr;
   108     }
   109     return 0;
   110 }
   111 
   112 static int
   113 WINRT_SetRelativeMouseMode(SDL_bool enabled)
   114 {
   115     SDL_WinRTGlobalApp->SetRelativeMouseMode(enabled ? true : false);
   116     return 0;
   117 }
   118 
   119 void
   120 WINRT_InitMouse(_THIS)
   121 {
   122     SDL_Mouse *mouse = SDL_GetMouse();
   123 
   124     /* DLudwig, Dec 3, 2012: Windows RT does not currently provide APIs for
   125        the following features, AFAIK:
   126         - custom cursors  (multiple system cursors are, however, available)
   127         - programmatically moveable cursors
   128     */
   129 
   130 #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
   131     //mouse->CreateCursor = WINRT_CreateCursor;
   132     mouse->CreateSystemCursor = WINRT_CreateSystemCursor;
   133     mouse->ShowCursor = WINRT_ShowCursor;
   134     mouse->FreeCursor = WINRT_FreeCursor;
   135     //mouse->WarpMouse = WINRT_WarpMouse;
   136     mouse->SetRelativeMouseMode = WINRT_SetRelativeMouseMode;
   137 
   138     SDL_SetDefaultCursor(WINRT_CreateDefaultCursor());
   139 #endif
   140 }
   141 
   142 void
   143 WINRT_QuitMouse(_THIS)
   144 {
   145 }
   146 
   147 #endif /* SDL_VIDEO_DRIVER_WINRT */
   148 
   149 /* vi: set ts=4 sw=4 expandtab: */