src/video/winrt/SDL_winrtmouse.cpp
author David Ludwig <dludwig@pobox.com>
Wed, 28 Aug 2013 16:14:27 -0400
changeset 8511 326b43e1276b
parent 8505 a76a3842c9eb
child 8512 5b345a756965
permissions -rw-r--r--
WinRT: made simulated-mouse (via touch) input work on Windows Phone in Portrait mode

Proper SDL_MOUSE* event support for non-Portrait orientations in Windows Phone is pending.
     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 Windows::Foundation::Point
   164 WINRT_TransformCursorPosition(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 = WINRT_TransformCursorPosition(window, mouseDeltaInDIPs);
   251     SDL_SendMouseMotion(
   252         window,
   253         0,
   254         1,
   255         _lround(mouseDeltaInSDLWindowCoords.X),
   256         _lround(mouseDeltaInSDLWindowCoords.Y));
   257 }
   258 
   259 Uint8
   260 WINRT_GetSDLButtonForPointerPoint(Windows::UI::Input::PointerPoint ^pt)
   261 {
   262     using namespace Windows::UI::Input;
   263 
   264 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   265     return SDL_BUTTON_LEFT;
   266 #else
   267     switch (pt->Properties->PointerUpdateKind)
   268     {
   269         case PointerUpdateKind::LeftButtonPressed:
   270         case PointerUpdateKind::LeftButtonReleased:
   271             return SDL_BUTTON_LEFT;
   272 
   273         case PointerUpdateKind::RightButtonPressed:
   274         case PointerUpdateKind::RightButtonReleased:
   275             return SDL_BUTTON_RIGHT;
   276 
   277         case PointerUpdateKind::MiddleButtonPressed:
   278         case PointerUpdateKind::MiddleButtonReleased:
   279             return SDL_BUTTON_MIDDLE;
   280 
   281         case PointerUpdateKind::XButton1Pressed:
   282         case PointerUpdateKind::XButton1Released:
   283             return SDL_BUTTON_X1;
   284 
   285         case PointerUpdateKind::XButton2Pressed:
   286         case PointerUpdateKind::XButton2Released:
   287             return SDL_BUTTON_X2;
   288 
   289         default:
   290             break;
   291     }
   292 #endif
   293 
   294     return 0;
   295 }
   296 
   297 //const char *
   298 //WINRT_ConvertPointerUpdateKindToString(Windows::UI::Input::PointerUpdateKind kind)
   299 //{
   300 //    using namespace Windows::UI::Input;
   301 //
   302 //    switch (kind)
   303 //    {
   304 //        case PointerUpdateKind::Other:
   305 //            return "Other";
   306 //        case PointerUpdateKind::LeftButtonPressed:
   307 //            return "LeftButtonPressed";
   308 //        case PointerUpdateKind::LeftButtonReleased:
   309 //            return "LeftButtonReleased";
   310 //        case PointerUpdateKind::RightButtonPressed:
   311 //            return "RightButtonPressed";
   312 //        case PointerUpdateKind::RightButtonReleased:
   313 //            return "RightButtonReleased";
   314 //        case PointerUpdateKind::MiddleButtonPressed:
   315 //            return "MiddleButtonPressed";
   316 //        case PointerUpdateKind::MiddleButtonReleased:
   317 //            return "MiddleButtonReleased";
   318 //        case PointerUpdateKind::XButton1Pressed:
   319 //            return "XButton1Pressed";
   320 //        case PointerUpdateKind::XButton1Released:
   321 //            return "XButton1Released";
   322 //        case PointerUpdateKind::XButton2Pressed:
   323 //            return "XButton2Pressed";
   324 //        case PointerUpdateKind::XButton2Released:
   325 //            return "XButton2Released";
   326 //    }
   327 //
   328 //    return "";
   329 //}
   330 
   331 void
   332 WINRT_ProcessPointerMovedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
   333 {
   334     if (!window || WINRT_UseRelativeMouseMode) {
   335         return;
   336     }
   337 
   338     Windows::Foundation::Point transformedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position);
   339     SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
   340 }
   341 
   342 void
   343 WINRT_ProcessPointerWheelChangedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
   344 {
   345     if (!window) {
   346         return;
   347     }
   348 
   349     // FIXME: This may need to accumulate deltas up to WHEEL_DELTA
   350     short motion = pointerPoint->Properties->MouseWheelDelta / WHEEL_DELTA;
   351     SDL_SendMouseWheel(window, 0, 0, motion);
   352 }
   353 
   354 void WINRT_ProcessPointerReleasedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
   355 {
   356     if (!window) {
   357         return;
   358     }
   359 
   360     Uint8 button = WINRT_GetSDLButtonForPointerPoint(pointerPoint);
   361     if (button) {
   362         SDL_SendMouseButton(window, 0, SDL_RELEASED, button);
   363     }
   364 }
   365 
   366 void WINRT_ProcessPointerPressedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
   367 {
   368     if (!window) {
   369         return;
   370     }
   371 
   372     Uint8 button = WINRT_GetSDLButtonForPointerPoint(pointerPoint);
   373     if (button) {
   374 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   375         Windows::Foundation::Point transformedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position);
   376         SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
   377 #endif
   378         SDL_SendMouseButton(window, 0, SDL_PRESSED, button);
   379     }
   380 }
   381 
   382 #endif /* SDL_VIDEO_DRIVER_WINRT */
   383 
   384 /* vi: set ts=4 sw=4 expandtab: */