src/video/winrt/SDL_winrtmouse.cpp
author David Ludwig <dludwig@pobox.com>
Tue, 27 Aug 2013 21:21:09 -0400
changeset 8505 a76a3842c9eb
parent 8500 f25ef28d61e8
child 8511 326b43e1276b
permissions -rw-r--r--
WinRT: experimental and preliminary support for XAML-based overlays on Windows 8/RT

The XAML support here is still rudimentary. Bugs do exist. You've been warned.

XAML support in Windows Phone 8 is not yet available (in SDL/WinRT).
     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 "../SDL_sysvideo.h"
    40 #include "SDL_events.h"
    41 #include "SDL_log.h"
    42 }
    43 
    44 #include "../../core/winrt/SDL_winrtapp.h"
    45 #include "SDL_winrtmouse.h"
    46 
    47 
    48 static SDL_bool WINRT_UseRelativeMouseMode = SDL_FALSE;
    49 
    50 
    51 static SDL_Cursor *
    52 WINRT_CreateSystemCursor(SDL_SystemCursor id)
    53 {
    54     SDL_Cursor *cursor;
    55     CoreCursorType cursorType = CoreCursorType::Arrow;
    56 
    57     switch(id)
    58     {
    59     default:
    60         SDL_assert(0);
    61         return NULL;
    62     case SDL_SYSTEM_CURSOR_ARROW:     cursorType = CoreCursorType::Arrow; break;
    63     case SDL_SYSTEM_CURSOR_IBEAM:     cursorType = CoreCursorType::IBeam; break;
    64     case SDL_SYSTEM_CURSOR_WAIT:      cursorType = CoreCursorType::Wait; break;
    65     case SDL_SYSTEM_CURSOR_CROSSHAIR: cursorType = CoreCursorType::Cross; break;
    66     case SDL_SYSTEM_CURSOR_WAITARROW: cursorType = CoreCursorType::Wait; break;
    67     case SDL_SYSTEM_CURSOR_SIZENWSE:  cursorType = CoreCursorType::SizeNorthwestSoutheast; break;
    68     case SDL_SYSTEM_CURSOR_SIZENESW:  cursorType = CoreCursorType::SizeNortheastSouthwest; break;
    69     case SDL_SYSTEM_CURSOR_SIZEWE:    cursorType = CoreCursorType::SizeWestEast; break;
    70     case SDL_SYSTEM_CURSOR_SIZENS:    cursorType = CoreCursorType::SizeNorthSouth; break;
    71     case SDL_SYSTEM_CURSOR_SIZEALL:   cursorType = CoreCursorType::SizeAll; break;
    72     case SDL_SYSTEM_CURSOR_NO:        cursorType = CoreCursorType::UniversalNo; break;
    73     case SDL_SYSTEM_CURSOR_HAND:      cursorType = CoreCursorType::Hand; break;
    74     }
    75 
    76     cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
    77     if (cursor) {
    78         /* Create a pointer to a COM reference to a cursor.  The extra
    79            pointer is used (on top of the COM reference) to allow the cursor
    80            to be referenced by the SDL_cursor's driverdata field, which is
    81            a void pointer.
    82         */
    83         CoreCursor ^* theCursor = new CoreCursor^(nullptr);
    84         *theCursor = ref new CoreCursor(cursorType, 0);
    85         cursor->driverdata = (void *) theCursor;
    86     } else {
    87         SDL_OutOfMemory();
    88     }
    89 
    90     return cursor;
    91 }
    92 
    93 static SDL_Cursor *
    94 WINRT_CreateDefaultCursor()
    95 {
    96     return WINRT_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
    97 }
    98 
    99 static void
   100 WINRT_FreeCursor(SDL_Cursor * cursor)
   101 {
   102     if (cursor->driverdata) {
   103         CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
   104         *theCursor = nullptr;       // Release the COM reference to the CoreCursor
   105         delete theCursor;           // Delete the pointer to the COM reference
   106     }
   107     SDL_free(cursor);
   108 }
   109 
   110 static int
   111 WINRT_ShowCursor(SDL_Cursor * cursor)
   112 {
   113     // TODO, WinRT, XAML: make WINRT_ShowCursor work when XAML support is enabled.
   114     if ( ! CoreWindow::GetForCurrentThread()) {
   115         return 0;
   116     }
   117 
   118     if (cursor) {
   119         CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
   120         CoreWindow::GetForCurrentThread()->PointerCursor = *theCursor;
   121     } else {
   122         CoreWindow::GetForCurrentThread()->PointerCursor = nullptr;
   123     }
   124     return 0;
   125 }
   126 
   127 static int
   128 WINRT_SetRelativeMouseMode(SDL_bool enabled)
   129 {
   130     WINRT_UseRelativeMouseMode = enabled;
   131     return 0;
   132 }
   133 
   134 void
   135 WINRT_InitMouse(_THIS)
   136 {
   137     SDL_Mouse *mouse = SDL_GetMouse();
   138 
   139     /* DLudwig, Dec 3, 2012: WinRT does not currently provide APIs for
   140        the following features, AFAIK:
   141         - custom cursors  (multiple system cursors are, however, available)
   142         - programmatically moveable cursors
   143     */
   144 
   145 #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
   146     //mouse->CreateCursor = WINRT_CreateCursor;
   147     mouse->CreateSystemCursor = WINRT_CreateSystemCursor;
   148     mouse->ShowCursor = WINRT_ShowCursor;
   149     mouse->FreeCursor = WINRT_FreeCursor;
   150     //mouse->WarpMouse = WINRT_WarpMouse;
   151     mouse->SetRelativeMouseMode = WINRT_SetRelativeMouseMode;
   152 
   153     SDL_SetDefaultCursor(WINRT_CreateDefaultCursor());
   154 #endif
   155 }
   156 
   157 void
   158 WINRT_QuitMouse(_THIS)
   159 {
   160 }
   161 
   162 // Applies necessary geometric transformations to raw cursor positions:
   163 static Windows::Foundation::Point
   164 TransformCursor(SDL_Window * window, Windows::Foundation::Point rawPosition)
   165 {
   166     if (!window) {
   167         return rawPosition;
   168     }
   169     CoreWindow ^ nativeWindow = CoreWindow::GetForCurrentThread();
   170     Windows::Foundation::Point outputPosition;
   171     outputPosition.X = rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
   172     outputPosition.Y = rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
   173     return outputPosition;
   174 }
   175 
   176 static inline int
   177 _lround(float arg)
   178 {
   179     if (arg >= 0.0f) {
   180         return (int)floor(arg + 0.5f);
   181     } else {
   182         return (int)ceil(arg - 0.5f);
   183     }
   184 }
   185 
   186 void
   187 WINRT_ProcessMouseMovedEvent(SDL_Window * window, Windows::Devices::Input::MouseEventArgs ^args)
   188 {
   189     if (!window || !WINRT_UseRelativeMouseMode) {
   190         return;
   191     }
   192 
   193     // DLudwig, 2012-12-28: On some systems, namely Visual Studio's Windows
   194     // Simulator, as well as Windows 8 in a Parallels 8 VM, MouseEventArgs'
   195     // MouseDelta field often reports very large values.  More information
   196     // on this can be found at the following pages on MSDN:
   197     //  - http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/a3c789fa-f1c5-49c4-9c0a-7db88d0f90f8
   198     //  - https://connect.microsoft.com/VisualStudio/Feedback/details/756515
   199     //
   200     // The values do not appear to be as large when running on some systems,
   201     // most notably a Surface RT.  Furthermore, the values returned by
   202     // CoreWindow's PointerMoved event, and sent to this class' OnPointerMoved
   203     // method, do not ever appear to be large, even when MouseEventArgs'
   204     // MouseDelta is reporting to the contrary.
   205     //
   206     // On systems with the large-values behavior, it appears that the values
   207     // get reported as if the screen's size is 65536 units in both the X and Y
   208     // dimensions.  This can be viewed by using Windows' now-private, "Raw Input"
   209     // APIs.  (GetRawInputData, RegisterRawInputDevices, WM_INPUT, etc.)
   210     //
   211     // MSDN's documentation on MouseEventArgs' MouseDelta field (at
   212     // http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.input.mouseeventargs.mousedelta ),
   213     // does not seem to indicate (to me) that its values should be so large.  It
   214     // says that its values should be a "change in screen location".  I could
   215     // be misinterpreting this, however a post on MSDN from a Microsoft engineer (see: 
   216     // http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/09a9868e-95bb-4858-ba1a-cb4d2c298d62 ),
   217     // indicates that these values are in DIPs, which is the same unit used
   218     // by CoreWindow's PointerMoved events (via the Position field in its CurrentPoint
   219     // property.  See http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.input.pointerpoint.position.aspx
   220     // for details.)
   221     //
   222     // To note, PointerMoved events are sent a 'RawPosition' value (via the
   223     // CurrentPoint property in MouseEventArgs), however these do not seem
   224     // to exhibit the same large-value behavior.
   225     //
   226     // The values passed via PointerMoved events can't always be used for relative
   227     // mouse motion, unfortunately.  Its values are bound to the cursor's position,
   228     // which stops when it hits one of the screen's edges.  This can be a problem in
   229     // first person shooters, whereby it is normal for mouse motion to travel far
   230     // along any one axis for a period of time.  MouseMoved events do not have the
   231     // screen-bounding limitation, and can be used regardless of where the system's
   232     // cursor is.
   233     //
   234     // One possible workaround would be to programmatically set the cursor's
   235     // position to the screen's center (when SDL's relative mouse mode is enabled),
   236     // however WinRT does not yet seem to have the ability to set the cursor's
   237     // position via a public API.  Win32 did this via an API call, SetCursorPos,
   238     // however WinRT makes this function be private.  Apps that use it won't get
   239     // approved for distribution in the Windows Store.  I've yet to be able to find
   240     // a suitable, store-friendly counterpart for WinRT.
   241     //
   242     // There may be some room for a workaround whereby OnPointerMoved's values
   243     // are compared to the values from OnMouseMoved in order to detect
   244     // when this bug is active.  A suitable transformation could then be made to
   245     // OnMouseMoved's values.  For now, however, the system-reported values are sent
   246     // to SDL with minimal transformation: from native screen coordinates (in DIPs)
   247     // to SDL window coordinates.
   248     //
   249     const Windows::Foundation::Point mouseDeltaInDIPs((float)args->MouseDelta.X, (float)args->MouseDelta.Y);
   250     const Windows::Foundation::Point mouseDeltaInSDLWindowCoords = TransformCursor(window, mouseDeltaInDIPs);
   251     SDL_SendMouseMotion(
   252         window,
   253         0,
   254         1,
   255         _lround(mouseDeltaInSDLWindowCoords.X),
   256         _lround(mouseDeltaInSDLWindowCoords.Y));
   257 }
   258 
   259 static Uint8
   260 WINRT_GetSDLButtonForPointerPoint(Windows::UI::Input::PointerPoint ^pt)
   261 {
   262     using namespace Windows::UI::Input;
   263 
   264     switch (pt->Properties->PointerUpdateKind)
   265     {
   266         case PointerUpdateKind::LeftButtonPressed:
   267         case PointerUpdateKind::LeftButtonReleased:
   268             return SDL_BUTTON_LEFT;
   269 
   270         case PointerUpdateKind::RightButtonPressed:
   271         case PointerUpdateKind::RightButtonReleased:
   272             return SDL_BUTTON_RIGHT;
   273 
   274         case PointerUpdateKind::MiddleButtonPressed:
   275         case PointerUpdateKind::MiddleButtonReleased:
   276             return SDL_BUTTON_MIDDLE;
   277 
   278         case PointerUpdateKind::XButton1Pressed:
   279         case PointerUpdateKind::XButton1Released:
   280             return SDL_BUTTON_X1;
   281 
   282         case PointerUpdateKind::XButton2Pressed:
   283         case PointerUpdateKind::XButton2Released:
   284             return SDL_BUTTON_X2;
   285 
   286         default:
   287             break;
   288     }
   289 
   290     return 0;
   291 }
   292 
   293 static const char *
   294 WINRT_ConvertPointerUpdateKindToString(Windows::UI::Input::PointerUpdateKind kind)
   295 {
   296     using namespace Windows::UI::Input;
   297 
   298     switch (kind)
   299     {
   300         case PointerUpdateKind::Other:
   301             return "Other";
   302         case PointerUpdateKind::LeftButtonPressed:
   303             return "LeftButtonPressed";
   304         case PointerUpdateKind::LeftButtonReleased:
   305             return "LeftButtonReleased";
   306         case PointerUpdateKind::RightButtonPressed:
   307             return "RightButtonPressed";
   308         case PointerUpdateKind::RightButtonReleased:
   309             return "RightButtonReleased";
   310         case PointerUpdateKind::MiddleButtonPressed:
   311             return "MiddleButtonPressed";
   312         case PointerUpdateKind::MiddleButtonReleased:
   313             return "MiddleButtonReleased";
   314         case PointerUpdateKind::XButton1Pressed:
   315             return "XButton1Pressed";
   316         case PointerUpdateKind::XButton1Released:
   317             return "XButton1Released";
   318         case PointerUpdateKind::XButton2Pressed:
   319             return "XButton2Pressed";
   320         case PointerUpdateKind::XButton2Released:
   321             return "XButton2Released";
   322     }
   323 
   324     return "";
   325 }
   326 
   327 static void
   328 WINRT_LogPointerEvent(const char * header, PointerEventArgs ^ args, Windows::Foundation::Point transformedPoint)
   329 {
   330     Windows::UI::Input::PointerPoint ^ pt = args->CurrentPoint;
   331     SDL_Log("%s: Position={%f,%f}, Transformed Pos={%f, %f}, MouseWheelDelta=%d, FrameId=%d, PointerId=%d, PointerUpdateKind=%s\n",
   332         header,
   333         pt->Position.X, pt->Position.Y,
   334         transformedPoint.X, transformedPoint.Y,
   335         pt->Properties->MouseWheelDelta,
   336         pt->FrameId,
   337         pt->PointerId,
   338         WINRT_ConvertPointerUpdateKindToString(args->CurrentPoint->Properties->PointerUpdateKind));
   339 }
   340 
   341 void
   342 WINRT_ProcessPointerMovedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
   343 {
   344 #if LOG_POINTER_EVENTS
   345     WINRT_LogPointerEvent("pointer moved", args, TransformCursor(pointerPoint->Position));
   346 #endif
   347 
   348     if (!window || WINRT_UseRelativeMouseMode) {
   349         return;
   350     }
   351 
   352     Windows::Foundation::Point transformedPoint = TransformCursor(window, pointerPoint->Position);
   353     SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
   354 }
   355 
   356 void
   357 WINRT_ProcessPointerWheelChangedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
   358 {
   359 #if LOG_POINTER_EVENTS
   360     WINRT_LogPointerEvent("wheel changed", args, TransformCursor(pointerPoint->Position));
   361 #endif
   362 
   363     if (!window) {
   364         return;
   365     }
   366 
   367     // FIXME: This may need to accumulate deltas up to WHEEL_DELTA
   368     short motion = pointerPoint->Properties->MouseWheelDelta / WHEEL_DELTA;
   369     SDL_SendMouseWheel(window, 0, 0, motion);
   370 }
   371 
   372 void WINRT_ProcessPointerReleasedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
   373 {
   374 #if LOG_POINTER_EVENTS
   375     WINRT_LogPointerEvent("mouse up", args, TransformCursor(args->CurrentPoint->Position));
   376 #endif
   377 
   378     if (!window) {
   379         return;
   380     }
   381 
   382     Uint8 button = WINRT_GetSDLButtonForPointerPoint(pointerPoint);
   383     if (button) {
   384         SDL_SendMouseButton(window, 0, SDL_RELEASED, button);
   385     }
   386 }
   387 
   388 void WINRT_ProcessPointerPressedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
   389 {
   390 #if LOG_POINTER_EVENTS
   391     WINRT_LogPointerEvent("mouse down", args, TransformCursor(args->CurrentPoint->Position));
   392 #endif
   393 
   394     if (!window) {
   395         return;
   396     }
   397 
   398     Uint8 button = WINRT_GetSDLButtonForPointerPoint(pointerPoint);
   399     if (button) {
   400         SDL_SendMouseButton(window, 0, SDL_PRESSED, button);
   401     }
   402 }
   403 
   404 #endif /* SDL_VIDEO_DRIVER_WINRT */
   405 
   406 /* vi: set ts=4 sw=4 expandtab: */