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