src/video/winrt/SDL_winrtmouse.cpp
changeset 8500 f25ef28d61e8
parent 8489 7fd62b320c31
child 8505 a76a3842c9eb
     1.1 --- a/src/video/winrt/SDL_winrtmouse.cpp	Tue Aug 27 12:16:42 2013 -0400
     1.2 +++ b/src/video/winrt/SDL_winrtmouse.cpp	Tue Aug 27 12:20:35 2013 -0400
     1.3 @@ -1,159 +1,159 @@
     1.4 -/*
     1.5 -  Simple DirectMedia Layer
     1.6 -  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     1.7 -
     1.8 -  This software is provided 'as-is', without any express or implied
     1.9 -  warranty.  In no event will the authors be held liable for any damages
    1.10 -  arising from the use of this software.
    1.11 -
    1.12 -  Permission is granted to anyone to use this software for any purpose,
    1.13 -  including commercial applications, and to alter it and redistribute it
    1.14 -  freely, subject to the following restrictions:
    1.15 -
    1.16 -  1. The origin of this software must not be misrepresented; you must not
    1.17 -     claim that you wrote the original software. If you use this software
    1.18 -     in a product, an acknowledgment in the product documentation would be
    1.19 -     appreciated but is not required.
    1.20 -  2. Altered source versions must be plainly marked as such, and must not be
    1.21 -     misrepresented as being the original software.
    1.22 -  3. This notice may not be removed or altered from any source distribution.
    1.23 -*/
    1.24 -
    1.25 -#include "SDL_config.h"
    1.26 -
    1.27 -#if SDL_VIDEO_DRIVER_WINRT
    1.28 -
    1.29 -/*
    1.30 - * Windows includes:
    1.31 - */
    1.32 -#include <Windows.h>
    1.33 -using namespace Windows::UI::Core;
    1.34 -using Windows::UI::Core::CoreCursor;
    1.35 -
    1.36 -/*
    1.37 - * SDL includes:
    1.38 - */
    1.39 -extern "C" {
    1.40 -#include "SDL_assert.h"
    1.41 -#include "../../events/SDL_mouse_c.h"
    1.42 -#include "../SDL_sysvideo.h"
    1.43 -#include "SDL_events.h"
    1.44 -#include "SDL_log.h"
    1.45 -}
    1.46 -
    1.47 -#include "../../core/winrt/SDL_winrtapp.h"
    1.48 -#include "SDL_winrtmouse.h"
    1.49 -
    1.50 -
    1.51 -static SDL_bool WINRT_UseRelativeMouseMode = SDL_FALSE;
    1.52 -
    1.53 -
    1.54 -static SDL_Cursor *
    1.55 -WINRT_CreateSystemCursor(SDL_SystemCursor id)
    1.56 -{
    1.57 -    SDL_Cursor *cursor;
    1.58 -    CoreCursorType cursorType = CoreCursorType::Arrow;
    1.59 -
    1.60 -    switch(id)
    1.61 -    {
    1.62 -    default:
    1.63 -        SDL_assert(0);
    1.64 -        return NULL;
    1.65 -    case SDL_SYSTEM_CURSOR_ARROW:     cursorType = CoreCursorType::Arrow; break;
    1.66 -    case SDL_SYSTEM_CURSOR_IBEAM:     cursorType = CoreCursorType::IBeam; break;
    1.67 -    case SDL_SYSTEM_CURSOR_WAIT:      cursorType = CoreCursorType::Wait; break;
    1.68 -    case SDL_SYSTEM_CURSOR_CROSSHAIR: cursorType = CoreCursorType::Cross; break;
    1.69 -    case SDL_SYSTEM_CURSOR_WAITARROW: cursorType = CoreCursorType::Wait; break;
    1.70 -    case SDL_SYSTEM_CURSOR_SIZENWSE:  cursorType = CoreCursorType::SizeNorthwestSoutheast; break;
    1.71 -    case SDL_SYSTEM_CURSOR_SIZENESW:  cursorType = CoreCursorType::SizeNortheastSouthwest; break;
    1.72 -    case SDL_SYSTEM_CURSOR_SIZEWE:    cursorType = CoreCursorType::SizeWestEast; break;
    1.73 -    case SDL_SYSTEM_CURSOR_SIZENS:    cursorType = CoreCursorType::SizeNorthSouth; break;
    1.74 -    case SDL_SYSTEM_CURSOR_SIZEALL:   cursorType = CoreCursorType::SizeAll; break;
    1.75 -    case SDL_SYSTEM_CURSOR_NO:        cursorType = CoreCursorType::UniversalNo; break;
    1.76 -    case SDL_SYSTEM_CURSOR_HAND:      cursorType = CoreCursorType::Hand; break;
    1.77 -    }
    1.78 -
    1.79 -    cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
    1.80 -    if (cursor) {
    1.81 -        /* Create a pointer to a COM reference to a cursor.  The extra
    1.82 -           pointer is used (on top of the COM reference) to allow the cursor
    1.83 -           to be referenced by the SDL_cursor's driverdata field, which is
    1.84 -           a void pointer.
    1.85 -        */
    1.86 -        CoreCursor ^* theCursor = new CoreCursor^(nullptr);
    1.87 -        *theCursor = ref new CoreCursor(cursorType, 0);
    1.88 -        cursor->driverdata = (void *) theCursor;
    1.89 -    } else {
    1.90 -        SDL_OutOfMemory();
    1.91 -    }
    1.92 -
    1.93 -    return cursor;
    1.94 -}
    1.95 -
    1.96 -static SDL_Cursor *
    1.97 -WINRT_CreateDefaultCursor()
    1.98 -{
    1.99 -    return WINRT_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
   1.100 -}
   1.101 -
   1.102 -static void
   1.103 -WINRT_FreeCursor(SDL_Cursor * cursor)
   1.104 -{
   1.105 -    if (cursor->driverdata) {
   1.106 -        CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
   1.107 -        *theCursor = nullptr;       // Release the COM reference to the CoreCursor
   1.108 -        delete theCursor;           // Delete the pointer to the COM reference
   1.109 -    }
   1.110 -    SDL_free(cursor);
   1.111 -}
   1.112 -
   1.113 -static int
   1.114 -WINRT_ShowCursor(SDL_Cursor * cursor)
   1.115 -{
   1.116 -    if (cursor) {
   1.117 -        CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
   1.118 -        CoreWindow::GetForCurrentThread()->PointerCursor = *theCursor;
   1.119 -    } else {
   1.120 -        CoreWindow::GetForCurrentThread()->PointerCursor = nullptr;
   1.121 -    }
   1.122 -    return 0;
   1.123 -}
   1.124 -
   1.125 -static int
   1.126 -WINRT_SetRelativeMouseMode(SDL_bool enabled)
   1.127 -{
   1.128 -    WINRT_UseRelativeMouseMode = enabled;
   1.129 -    return 0;
   1.130 -}
   1.131 -
   1.132 -void
   1.133 -WINRT_InitMouse(_THIS)
   1.134 -{
   1.135 -    SDL_Mouse *mouse = SDL_GetMouse();
   1.136 -
   1.137 -    /* DLudwig, Dec 3, 2012: Windows RT does not currently provide APIs for
   1.138 -       the following features, AFAIK:
   1.139 -        - custom cursors  (multiple system cursors are, however, available)
   1.140 -        - programmatically moveable cursors
   1.141 -    */
   1.142 -
   1.143 -#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
   1.144 -    //mouse->CreateCursor = WINRT_CreateCursor;
   1.145 -    mouse->CreateSystemCursor = WINRT_CreateSystemCursor;
   1.146 -    mouse->ShowCursor = WINRT_ShowCursor;
   1.147 -    mouse->FreeCursor = WINRT_FreeCursor;
   1.148 -    //mouse->WarpMouse = WINRT_WarpMouse;
   1.149 -    mouse->SetRelativeMouseMode = WINRT_SetRelativeMouseMode;
   1.150 -
   1.151 -    SDL_SetDefaultCursor(WINRT_CreateDefaultCursor());
   1.152 -#endif
   1.153 -}
   1.154 -
   1.155 -void
   1.156 -WINRT_QuitMouse(_THIS)
   1.157 -{
   1.158 -}
   1.159 -
   1.160 +/*
   1.161 +  Simple DirectMedia Layer
   1.162 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
   1.163 +
   1.164 +  This software is provided 'as-is', without any express or implied
   1.165 +  warranty.  In no event will the authors be held liable for any damages
   1.166 +  arising from the use of this software.
   1.167 +
   1.168 +  Permission is granted to anyone to use this software for any purpose,
   1.169 +  including commercial applications, and to alter it and redistribute it
   1.170 +  freely, subject to the following restrictions:
   1.171 +
   1.172 +  1. The origin of this software must not be misrepresented; you must not
   1.173 +     claim that you wrote the original software. If you use this software
   1.174 +     in a product, an acknowledgment in the product documentation would be
   1.175 +     appreciated but is not required.
   1.176 +  2. Altered source versions must be plainly marked as such, and must not be
   1.177 +     misrepresented as being the original software.
   1.178 +  3. This notice may not be removed or altered from any source distribution.
   1.179 +*/
   1.180 +
   1.181 +#include "SDL_config.h"
   1.182 +
   1.183 +#if SDL_VIDEO_DRIVER_WINRT
   1.184 +
   1.185 +/*
   1.186 + * Windows includes:
   1.187 + */
   1.188 +#include <Windows.h>
   1.189 +using namespace Windows::UI::Core;
   1.190 +using Windows::UI::Core::CoreCursor;
   1.191 +
   1.192 +/*
   1.193 + * SDL includes:
   1.194 + */
   1.195 +extern "C" {
   1.196 +#include "SDL_assert.h"
   1.197 +#include "../../events/SDL_mouse_c.h"
   1.198 +#include "../SDL_sysvideo.h"
   1.199 +#include "SDL_events.h"
   1.200 +#include "SDL_log.h"
   1.201 +}
   1.202 +
   1.203 +#include "../../core/winrt/SDL_winrtapp.h"
   1.204 +#include "SDL_winrtmouse.h"
   1.205 +
   1.206 +
   1.207 +static SDL_bool WINRT_UseRelativeMouseMode = SDL_FALSE;
   1.208 +
   1.209 +
   1.210 +static SDL_Cursor *
   1.211 +WINRT_CreateSystemCursor(SDL_SystemCursor id)
   1.212 +{
   1.213 +    SDL_Cursor *cursor;
   1.214 +    CoreCursorType cursorType = CoreCursorType::Arrow;
   1.215 +
   1.216 +    switch(id)
   1.217 +    {
   1.218 +    default:
   1.219 +        SDL_assert(0);
   1.220 +        return NULL;
   1.221 +    case SDL_SYSTEM_CURSOR_ARROW:     cursorType = CoreCursorType::Arrow; break;
   1.222 +    case SDL_SYSTEM_CURSOR_IBEAM:     cursorType = CoreCursorType::IBeam; break;
   1.223 +    case SDL_SYSTEM_CURSOR_WAIT:      cursorType = CoreCursorType::Wait; break;
   1.224 +    case SDL_SYSTEM_CURSOR_CROSSHAIR: cursorType = CoreCursorType::Cross; break;
   1.225 +    case SDL_SYSTEM_CURSOR_WAITARROW: cursorType = CoreCursorType::Wait; break;
   1.226 +    case SDL_SYSTEM_CURSOR_SIZENWSE:  cursorType = CoreCursorType::SizeNorthwestSoutheast; break;
   1.227 +    case SDL_SYSTEM_CURSOR_SIZENESW:  cursorType = CoreCursorType::SizeNortheastSouthwest; break;
   1.228 +    case SDL_SYSTEM_CURSOR_SIZEWE:    cursorType = CoreCursorType::SizeWestEast; break;
   1.229 +    case SDL_SYSTEM_CURSOR_SIZENS:    cursorType = CoreCursorType::SizeNorthSouth; break;
   1.230 +    case SDL_SYSTEM_CURSOR_SIZEALL:   cursorType = CoreCursorType::SizeAll; break;
   1.231 +    case SDL_SYSTEM_CURSOR_NO:        cursorType = CoreCursorType::UniversalNo; break;
   1.232 +    case SDL_SYSTEM_CURSOR_HAND:      cursorType = CoreCursorType::Hand; break;
   1.233 +    }
   1.234 +
   1.235 +    cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
   1.236 +    if (cursor) {
   1.237 +        /* Create a pointer to a COM reference to a cursor.  The extra
   1.238 +           pointer is used (on top of the COM reference) to allow the cursor
   1.239 +           to be referenced by the SDL_cursor's driverdata field, which is
   1.240 +           a void pointer.
   1.241 +        */
   1.242 +        CoreCursor ^* theCursor = new CoreCursor^(nullptr);
   1.243 +        *theCursor = ref new CoreCursor(cursorType, 0);
   1.244 +        cursor->driverdata = (void *) theCursor;
   1.245 +    } else {
   1.246 +        SDL_OutOfMemory();
   1.247 +    }
   1.248 +
   1.249 +    return cursor;
   1.250 +}
   1.251 +
   1.252 +static SDL_Cursor *
   1.253 +WINRT_CreateDefaultCursor()
   1.254 +{
   1.255 +    return WINRT_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
   1.256 +}
   1.257 +
   1.258 +static void
   1.259 +WINRT_FreeCursor(SDL_Cursor * cursor)
   1.260 +{
   1.261 +    if (cursor->driverdata) {
   1.262 +        CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
   1.263 +        *theCursor = nullptr;       // Release the COM reference to the CoreCursor
   1.264 +        delete theCursor;           // Delete the pointer to the COM reference
   1.265 +    }
   1.266 +    SDL_free(cursor);
   1.267 +}
   1.268 +
   1.269 +static int
   1.270 +WINRT_ShowCursor(SDL_Cursor * cursor)
   1.271 +{
   1.272 +    if (cursor) {
   1.273 +        CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
   1.274 +        CoreWindow::GetForCurrentThread()->PointerCursor = *theCursor;
   1.275 +    } else {
   1.276 +        CoreWindow::GetForCurrentThread()->PointerCursor = nullptr;
   1.277 +    }
   1.278 +    return 0;
   1.279 +}
   1.280 +
   1.281 +static int
   1.282 +WINRT_SetRelativeMouseMode(SDL_bool enabled)
   1.283 +{
   1.284 +    WINRT_UseRelativeMouseMode = enabled;
   1.285 +    return 0;
   1.286 +}
   1.287 +
   1.288 +void
   1.289 +WINRT_InitMouse(_THIS)
   1.290 +{
   1.291 +    SDL_Mouse *mouse = SDL_GetMouse();
   1.292 +
   1.293 +    /* DLudwig, Dec 3, 2012: WinRT does not currently provide APIs for
   1.294 +       the following features, AFAIK:
   1.295 +        - custom cursors  (multiple system cursors are, however, available)
   1.296 +        - programmatically moveable cursors
   1.297 +    */
   1.298 +
   1.299 +#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
   1.300 +    //mouse->CreateCursor = WINRT_CreateCursor;
   1.301 +    mouse->CreateSystemCursor = WINRT_CreateSystemCursor;
   1.302 +    mouse->ShowCursor = WINRT_ShowCursor;
   1.303 +    mouse->FreeCursor = WINRT_FreeCursor;
   1.304 +    //mouse->WarpMouse = WINRT_WarpMouse;
   1.305 +    mouse->SetRelativeMouseMode = WINRT_SetRelativeMouseMode;
   1.306 +
   1.307 +    SDL_SetDefaultCursor(WINRT_CreateDefaultCursor());
   1.308 +#endif
   1.309 +}
   1.310 +
   1.311 +void
   1.312 +WINRT_QuitMouse(_THIS)
   1.313 +{
   1.314 +}
   1.315 +
   1.316  // Applies necessary geometric transformations to raw cursor positions:
   1.317  static Windows::Foundation::Point
   1.318  TransformCursor(SDL_Window * window, Windows::Foundation::Point rawPosition)
   1.319 @@ -166,8 +166,8 @@
   1.320      outputPosition.X = rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
   1.321      outputPosition.Y = rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
   1.322      return outputPosition;
   1.323 -}
   1.324 -
   1.325 +}
   1.326 +
   1.327  static inline int
   1.328  _lround(float arg)
   1.329  {
   1.330 @@ -176,15 +176,15 @@
   1.331      } else {
   1.332          return (int)ceil(arg - 0.5f);
   1.333      }
   1.334 -}
   1.335 -
   1.336 -void
   1.337 -WINRT_ProcessMouseMovedEvent(SDL_Window * window, Windows::Devices::Input::MouseEventArgs ^args)
   1.338 -{
   1.339 -    if (!window || !WINRT_UseRelativeMouseMode) {
   1.340 -        return;
   1.341 -    }
   1.342 -
   1.343 +}
   1.344 +
   1.345 +void
   1.346 +WINRT_ProcessMouseMovedEvent(SDL_Window * window, Windows::Devices::Input::MouseEventArgs ^args)
   1.347 +{
   1.348 +    if (!window || !WINRT_UseRelativeMouseMode) {
   1.349 +        return;
   1.350 +    }
   1.351 +
   1.352      // DLudwig, 2012-12-28: On some systems, namely Visual Studio's Windows
   1.353      // Simulator, as well as Windows 8 in a Parallels 8 VM, MouseEventArgs'
   1.354      // MouseDelta field often reports very large values.  More information
   1.355 @@ -228,7 +228,7 @@
   1.356      //
   1.357      // One possible workaround would be to programmatically set the cursor's
   1.358      // position to the screen's center (when SDL's relative mouse mode is enabled),
   1.359 -    // however Windows RT does not yet seem to have the ability to set the cursor's
   1.360 +    // however WinRT does not yet seem to have the ability to set the cursor's
   1.361      // position via a public API.  Win32 did this via an API call, SetCursorPos,
   1.362      // however WinRT makes this function be private.  Apps that use it won't get
   1.363      // approved for distribution in the Windows Store.  I've yet to be able to find
   1.364 @@ -249,8 +249,8 @@
   1.365          1,
   1.366          _lround(mouseDeltaInSDLWindowCoords.X),
   1.367          _lround(mouseDeltaInSDLWindowCoords.Y));
   1.368 -}
   1.369 -
   1.370 +}
   1.371 +
   1.372  static Uint8
   1.373  WINRT_GetSDLButtonForPointerPoint(Windows::UI::Input::PointerPoint ^pt)
   1.374  {
   1.375 @@ -331,10 +331,10 @@
   1.376          pt->FrameId,
   1.377          pt->PointerId,
   1.378          WINRT_ConvertPointerUpdateKindToString(args->CurrentPoint->Properties->PointerUpdateKind));
   1.379 -}
   1.380 -
   1.381 -void
   1.382 -WINRT_ProcessPointerMovedEvent(SDL_Window *window, Windows::UI::Core::PointerEventArgs ^args)
   1.383 +}
   1.384 +
   1.385 +void
   1.386 +WINRT_ProcessPointerMovedEvent(SDL_Window *window, Windows::UI::Core::PointerEventArgs ^args)
   1.387  {
   1.388  #if LOG_POINTER_EVENTS
   1.389      WINRT_LogPointerEvent("pointer moved", args, TransformCursor(args->CurrentPoint->Position));
   1.390 @@ -346,9 +346,9 @@
   1.391  
   1.392      Windows::Foundation::Point transformedPoint = TransformCursor(window, args->CurrentPoint->Position);
   1.393      SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
   1.394 -}
   1.395 -
   1.396 -void
   1.397 +}
   1.398 +
   1.399 +void
   1.400  WINRT_ProcessPointerWheelChangedEvent(SDL_Window *window, Windows::UI::Core::PointerEventArgs ^args)
   1.401  {
   1.402  #if LOG_POINTER_EVENTS
   1.403 @@ -362,8 +362,8 @@
   1.404      // FIXME: This may need to accumulate deltas up to WHEEL_DELTA
   1.405      short motion = args->CurrentPoint->Properties->MouseWheelDelta / WHEEL_DELTA;
   1.406      SDL_SendMouseWheel(window, 0, 0, motion);
   1.407 -}
   1.408 -
   1.409 +}
   1.410 +
   1.411  void WINRT_ProcessPointerReleasedEvent(SDL_Window *window, Windows::UI::Core::PointerEventArgs ^args)
   1.412  {
   1.413  #if LOG_POINTER_EVENTS
   1.414 @@ -378,8 +378,8 @@
   1.415      if (button) {
   1.416          SDL_SendMouseButton(window, 0, SDL_RELEASED, button);
   1.417      }
   1.418 -}
   1.419 -
   1.420 +}
   1.421 +
   1.422  void WINRT_ProcessPointerPressedEvent(SDL_Window *window, Windows::UI::Core::PointerEventArgs ^args)
   1.423  {
   1.424  #if LOG_POINTER_EVENTS
   1.425 @@ -394,8 +394,8 @@
   1.426      if (button) {
   1.427          SDL_SendMouseButton(window, 0, SDL_PRESSED, button);
   1.428      }
   1.429 -}
   1.430 -
   1.431 -#endif /* SDL_VIDEO_DRIVER_WINRT */
   1.432 -
   1.433 -/* vi: set ts=4 sw=4 expandtab: */
   1.434 +}
   1.435 +
   1.436 +#endif /* SDL_VIDEO_DRIVER_WINRT */
   1.437 +
   1.438 +/* vi: set ts=4 sw=4 expandtab: */