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