src/video/winrt/SDL_winrtpointerinput.cpp
author David Ludwig <dludwig@pobox.com>
Fri, 06 Sep 2013 21:00:52 -0400
changeset 8521 cefdaf414ead
parent 8516 f3e0e381bdcd
child 8534 50177f518fdb
permissions -rw-r--r--
WinRT: minor code cleanup regarding events

Some event handling functions got sorted in a somewhat consistent manner, and
in some cases, were also segregated a bit from app-lifecycle code.
     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 #include "SDL_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_WINRT
    24 
    25 /* SDL includes */
    26 #include "SDL_winrtevents_c.h"
    27 #include "SDL_winrtmouse_c.h"
    28 #include "SDL_winrtvideo_cpp.h"
    29 #include "SDL_assert.h"
    30 #include "SDL_system.h"
    31 
    32 extern "C" {
    33 #include "../SDL_sysvideo.h"
    34 #include "../../events/SDL_events_c.h"
    35 #include "../../events/SDL_mouse_c.h"
    36 #include "../../events/SDL_touch_c.h"
    37 }
    38 
    39 /* File-specific globals: */
    40 static SDL_TouchID WINRT_TouchID = 1;
    41 static unsigned int WINRT_LeftFingerDown = 0;
    42 
    43 
    44 void
    45 WINRT_InitTouch(_THIS)
    46 {
    47     SDL_AddTouch(WINRT_TouchID, "");
    48 }
    49 
    50 // Applies necessary geometric transformations to raw cursor positions:
    51 Windows::Foundation::Point
    52 WINRT_TransformCursorPosition(SDL_Window * window, Windows::Foundation::Point rawPosition)
    53 {
    54     using namespace Windows::UI::Core;
    55     using namespace Windows::Graphics::Display;
    56 
    57     if (!window) {
    58         return rawPosition;
    59     }
    60 
    61     SDL_WindowData * windowData = (SDL_WindowData *) window->driverdata;
    62     if (windowData->coreWindow == nullptr) {
    63         // For some reason, the window isn't associated with a CoreWindow.
    64         // This might end up being the case as XAML support is extended.
    65         // For now, if there's no CoreWindow attached to the SDL_Window,
    66         // don't do any transforms.
    67         return rawPosition;
    68     }
    69 
    70     // The CoreWindow can only be accessed on certain thread(s).
    71     SDL_assert(CoreWindow::GetForCurrentThread() != nullptr);
    72 
    73     CoreWindow ^ nativeWindow = windowData->coreWindow.Get();
    74     Windows::Foundation::Point outputPosition;
    75 
    76 #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
    77     outputPosition.X = rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
    78     outputPosition.Y = rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
    79 #else
    80     switch (DisplayProperties::CurrentOrientation)
    81     {
    82         case DisplayOrientations::Portrait:
    83             outputPosition.X = rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
    84             outputPosition.Y = rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
    85             break;
    86         case DisplayOrientations::PortraitFlipped:
    87             outputPosition.X = (float32)window->w - rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
    88             outputPosition.Y = (float32)window->h - rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
    89             break;
    90         case DisplayOrientations::Landscape:
    91             outputPosition.X = rawPosition.Y * (((float32)window->w) / nativeWindow->Bounds.Height);
    92             outputPosition.Y = (float32)window->h - rawPosition.X * (((float32)window->h) / nativeWindow->Bounds.Width);
    93             break;
    94         case DisplayOrientations::LandscapeFlipped:
    95             outputPosition.X = (float32)window->w - rawPosition.Y * (((float32)window->w) / nativeWindow->Bounds.Height);
    96             outputPosition.Y = rawPosition.X * (((float32)window->h) / nativeWindow->Bounds.Width);
    97             break;
    98         default:
    99             break;
   100     }
   101 #endif
   102 
   103     return outputPosition;
   104 }
   105 
   106 static inline int
   107 _lround(float arg)
   108 {
   109     if (arg >= 0.0f) {
   110         return (int)floor(arg + 0.5f);
   111     } else {
   112         return (int)ceil(arg - 0.5f);
   113     }
   114 }
   115 
   116 Uint8
   117 WINRT_GetSDLButtonForPointerPoint(Windows::UI::Input::PointerPoint ^pt)
   118 {
   119     using namespace Windows::UI::Input;
   120 
   121 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   122     return SDL_BUTTON_LEFT;
   123 #else
   124     switch (pt->Properties->PointerUpdateKind)
   125     {
   126         case PointerUpdateKind::LeftButtonPressed:
   127         case PointerUpdateKind::LeftButtonReleased:
   128             return SDL_BUTTON_LEFT;
   129 
   130         case PointerUpdateKind::RightButtonPressed:
   131         case PointerUpdateKind::RightButtonReleased:
   132             return SDL_BUTTON_RIGHT;
   133 
   134         case PointerUpdateKind::MiddleButtonPressed:
   135         case PointerUpdateKind::MiddleButtonReleased:
   136             return SDL_BUTTON_MIDDLE;
   137 
   138         case PointerUpdateKind::XButton1Pressed:
   139         case PointerUpdateKind::XButton1Released:
   140             return SDL_BUTTON_X1;
   141 
   142         case PointerUpdateKind::XButton2Pressed:
   143         case PointerUpdateKind::XButton2Released:
   144             return SDL_BUTTON_X2;
   145 
   146         default:
   147             break;
   148     }
   149 #endif
   150 
   151     return 0;
   152 }
   153 
   154 //const char *
   155 //WINRT_ConvertPointerUpdateKindToString(Windows::UI::Input::PointerUpdateKind kind)
   156 //{
   157 //    using namespace Windows::UI::Input;
   158 //
   159 //    switch (kind)
   160 //    {
   161 //        case PointerUpdateKind::Other:
   162 //            return "Other";
   163 //        case PointerUpdateKind::LeftButtonPressed:
   164 //            return "LeftButtonPressed";
   165 //        case PointerUpdateKind::LeftButtonReleased:
   166 //            return "LeftButtonReleased";
   167 //        case PointerUpdateKind::RightButtonPressed:
   168 //            return "RightButtonPressed";
   169 //        case PointerUpdateKind::RightButtonReleased:
   170 //            return "RightButtonReleased";
   171 //        case PointerUpdateKind::MiddleButtonPressed:
   172 //            return "MiddleButtonPressed";
   173 //        case PointerUpdateKind::MiddleButtonReleased:
   174 //            return "MiddleButtonReleased";
   175 //        case PointerUpdateKind::XButton1Pressed:
   176 //            return "XButton1Pressed";
   177 //        case PointerUpdateKind::XButton1Released:
   178 //            return "XButton1Released";
   179 //        case PointerUpdateKind::XButton2Pressed:
   180 //            return "XButton2Pressed";
   181 //        case PointerUpdateKind::XButton2Released:
   182 //            return "XButton2Released";
   183 //    }
   184 //
   185 //    return "";
   186 //}
   187 
   188 static bool
   189 WINRT_IsTouchEvent(Windows::UI::Input::PointerPoint ^pointerPoint)
   190 {
   191 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   192     return true;
   193 #else
   194     using namespace Windows::Devices::Input;
   195     switch (pointerPoint->PointerDevice->PointerDeviceType) {
   196         case PointerDeviceType::Touch:
   197         case PointerDeviceType::Pen:
   198             return true;
   199         default:
   200             return false;
   201     }
   202 #endif
   203 }
   204 
   205 void WINRT_ProcessPointerPressedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
   206 {
   207     if (!window) {
   208         return;
   209     }
   210 
   211     Windows::Foundation::Point transformedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position);
   212 
   213     if (!WINRT_LeftFingerDown) {
   214         Uint8 button = WINRT_GetSDLButtonForPointerPoint(pointerPoint);
   215         if (button) {
   216 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   217             SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
   218 #endif
   219             SDL_SendMouseButton(window, 0, SDL_PRESSED, button);
   220         }
   221 
   222         WINRT_LeftFingerDown = pointerPoint->PointerId;
   223     }
   224 
   225     if (WINRT_IsTouchEvent(pointerPoint)) {
   226         SDL_SendTouch(
   227             WINRT_TouchID,
   228             (SDL_FingerID) pointerPoint->PointerId,
   229             SDL_TRUE,
   230             transformedPoint.X,
   231             transformedPoint.Y,
   232             pointerPoint->Properties->Pressure);
   233     }
   234 }
   235 
   236 void
   237 WINRT_ProcessPointerMovedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
   238 {
   239     if (!window || WINRT_UsingRelativeMouseMode) {
   240         return;
   241     }
   242 
   243     Windows::Foundation::Point transformedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position);
   244 
   245     if (pointerPoint->PointerId == WINRT_LeftFingerDown) {
   246         SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
   247     }
   248 
   249     if (WINRT_IsTouchEvent(pointerPoint)) {
   250         SDL_SendTouchMotion(
   251             WINRT_TouchID,
   252             (SDL_FingerID) pointerPoint->PointerId,
   253             transformedPoint.X,
   254             transformedPoint.Y,
   255             pointerPoint->Properties->Pressure);
   256     }
   257 }
   258 
   259 void WINRT_ProcessPointerReleasedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
   260 {
   261     if (!window) {
   262         return;
   263     }
   264 
   265     Windows::Foundation::Point transformedPoint = WINRT_TransformCursorPosition(window, pointerPoint->Position);
   266 
   267     if (WINRT_LeftFingerDown == pointerPoint->PointerId) {
   268         Uint8 button = WINRT_GetSDLButtonForPointerPoint(pointerPoint);
   269         if (button) {
   270             SDL_SendMouseButton(window, 0, SDL_RELEASED, button);
   271         }
   272         WINRT_LeftFingerDown = 0;
   273     }
   274 
   275     if (WINRT_IsTouchEvent(pointerPoint)) {
   276         SDL_SendTouch(
   277             WINRT_TouchID,
   278             (SDL_FingerID) pointerPoint->PointerId,
   279             SDL_FALSE,
   280             transformedPoint.X,
   281             transformedPoint.Y,
   282             pointerPoint->Properties->Pressure);
   283     }
   284 }
   285 
   286 void
   287 WINRT_ProcessPointerWheelChangedEvent(SDL_Window *window, Windows::UI::Input::PointerPoint ^pointerPoint)
   288 {
   289     if (!window) {
   290         return;
   291     }
   292 
   293     // FIXME: This may need to accumulate deltas up to WHEEL_DELTA
   294     short motion = pointerPoint->Properties->MouseWheelDelta / WHEEL_DELTA;
   295     SDL_SendMouseWheel(window, 0, 0, motion);
   296 }
   297 
   298 void
   299 WINRT_ProcessMouseMovedEvent(SDL_Window * window, Windows::Devices::Input::MouseEventArgs ^args)
   300 {
   301     if (!window || !WINRT_UsingRelativeMouseMode) {
   302         return;
   303     }
   304 
   305     // DLudwig, 2012-12-28: On some systems, namely Visual Studio's Windows
   306     // Simulator, as well as Windows 8 in a Parallels 8 VM, MouseEventArgs'
   307     // MouseDelta field often reports very large values.  More information
   308     // on this can be found at the following pages on MSDN:
   309     //  - http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/a3c789fa-f1c5-49c4-9c0a-7db88d0f90f8
   310     //  - https://connect.microsoft.com/VisualStudio/Feedback/details/756515
   311     //
   312     // The values do not appear to be as large when running on some systems,
   313     // most notably a Surface RT.  Furthermore, the values returned by
   314     // CoreWindow's PointerMoved event, and sent to this class' OnPointerMoved
   315     // method, do not ever appear to be large, even when MouseEventArgs'
   316     // MouseDelta is reporting to the contrary.
   317     //
   318     // On systems with the large-values behavior, it appears that the values
   319     // get reported as if the screen's size is 65536 units in both the X and Y
   320     // dimensions.  This can be viewed by using Windows' now-private, "Raw Input"
   321     // APIs.  (GetRawInputData, RegisterRawInputDevices, WM_INPUT, etc.)
   322     //
   323     // MSDN's documentation on MouseEventArgs' MouseDelta field (at
   324     // http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.input.mouseeventargs.mousedelta ),
   325     // does not seem to indicate (to me) that its values should be so large.  It
   326     // says that its values should be a "change in screen location".  I could
   327     // be misinterpreting this, however a post on MSDN from a Microsoft engineer (see: 
   328     // http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/09a9868e-95bb-4858-ba1a-cb4d2c298d62 ),
   329     // indicates that these values are in DIPs, which is the same unit used
   330     // by CoreWindow's PointerMoved events (via the Position field in its CurrentPoint
   331     // property.  See http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.input.pointerpoint.position.aspx
   332     // for details.)
   333     //
   334     // To note, PointerMoved events are sent a 'RawPosition' value (via the
   335     // CurrentPoint property in MouseEventArgs), however these do not seem
   336     // to exhibit the same large-value behavior.
   337     //
   338     // The values passed via PointerMoved events can't always be used for relative
   339     // mouse motion, unfortunately.  Its values are bound to the cursor's position,
   340     // which stops when it hits one of the screen's edges.  This can be a problem in
   341     // first person shooters, whereby it is normal for mouse motion to travel far
   342     // along any one axis for a period of time.  MouseMoved events do not have the
   343     // screen-bounding limitation, and can be used regardless of where the system's
   344     // cursor is.
   345     //
   346     // One possible workaround would be to programmatically set the cursor's
   347     // position to the screen's center (when SDL's relative mouse mode is enabled),
   348     // however WinRT does not yet seem to have the ability to set the cursor's
   349     // position via a public API.  Win32 did this via an API call, SetCursorPos,
   350     // however WinRT makes this function be private.  Apps that use it won't get
   351     // approved for distribution in the Windows Store.  I've yet to be able to find
   352     // a suitable, store-friendly counterpart for WinRT.
   353     //
   354     // There may be some room for a workaround whereby OnPointerMoved's values
   355     // are compared to the values from OnMouseMoved in order to detect
   356     // when this bug is active.  A suitable transformation could then be made to
   357     // OnMouseMoved's values.  For now, however, the system-reported values are sent
   358     // to SDL with minimal transformation: from native screen coordinates (in DIPs)
   359     // to SDL window coordinates.
   360     //
   361     const Windows::Foundation::Point mouseDeltaInDIPs((float)args->MouseDelta.X, (float)args->MouseDelta.Y);
   362     const Windows::Foundation::Point mouseDeltaInSDLWindowCoords = WINRT_TransformCursorPosition(window, mouseDeltaInDIPs);
   363     SDL_SendMouseMotion(
   364         window,
   365         0,
   366         1,
   367         _lround(mouseDeltaInSDLWindowCoords.X),
   368         _lround(mouseDeltaInSDLWindowCoords.Y));
   369 }
   370 
   371 #endif // SDL_VIDEO_DRIVER_WINRT