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