src/video/winrt/SDL_winrtmouse.cpp
changeset 8489 7fd62b320c31
parent 8483 d403ded25942
child 8500 f25ef28d61e8
     1.1 --- a/src/video/winrt/SDL_winrtmouse.cpp	Tue Aug 20 22:18:48 2013 -0400
     1.2 +++ b/src/video/winrt/SDL_winrtmouse.cpp	Mon Aug 26 17:17:53 2013 -0400
     1.3 @@ -23,19 +23,29 @@
     1.4  
     1.5  #if SDL_VIDEO_DRIVER_WINRT
     1.6  
     1.7 +/*
     1.8 + * Windows includes:
     1.9 + */
    1.10 +#include <Windows.h>
    1.11 +using namespace Windows::UI::Core;
    1.12 +using Windows::UI::Core::CoreCursor;
    1.13 +
    1.14 +/*
    1.15 + * SDL includes:
    1.16 + */
    1.17  extern "C" {
    1.18  #include "SDL_assert.h"
    1.19  #include "../../events/SDL_mouse_c.h"
    1.20  #include "../SDL_sysvideo.h"
    1.21 +#include "SDL_events.h"
    1.22 +#include "SDL_log.h"
    1.23  }
    1.24  
    1.25  #include "../../core/winrt/SDL_winrtapp.h"
    1.26  #include "SDL_winrtmouse.h"
    1.27  
    1.28 -using namespace Windows::UI::Core;
    1.29 -using Windows::UI::Core::CoreCursor;
    1.30  
    1.31 -extern SDL_WinRTApp ^ SDL_WinRTGlobalApp;
    1.32 +static SDL_bool WINRT_UseRelativeMouseMode = SDL_FALSE;
    1.33  
    1.34  
    1.35  static SDL_Cursor *
    1.36 @@ -112,7 +122,7 @@
    1.37  static int
    1.38  WINRT_SetRelativeMouseMode(SDL_bool enabled)
    1.39  {
    1.40 -    SDL_WinRTGlobalApp->SetRelativeMouseMode(enabled ? true : false);
    1.41 +    WINRT_UseRelativeMouseMode = enabled;
    1.42      return 0;
    1.43  }
    1.44  
    1.45 @@ -144,6 +154,248 @@
    1.46  {
    1.47  }
    1.48  
    1.49 +// Applies necessary geometric transformations to raw cursor positions:
    1.50 +static Windows::Foundation::Point
    1.51 +TransformCursor(SDL_Window * window, Windows::Foundation::Point rawPosition)
    1.52 +{
    1.53 +    if (!window) {
    1.54 +        return rawPosition;
    1.55 +    }
    1.56 +    CoreWindow ^ nativeWindow = CoreWindow::GetForCurrentThread();
    1.57 +    Windows::Foundation::Point outputPosition;
    1.58 +    outputPosition.X = rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
    1.59 +    outputPosition.Y = rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
    1.60 +    return outputPosition;
    1.61 +}
    1.62 +
    1.63 +static inline int
    1.64 +_lround(float arg)
    1.65 +{
    1.66 +    if (arg >= 0.0f) {
    1.67 +        return (int)floor(arg + 0.5f);
    1.68 +    } else {
    1.69 +        return (int)ceil(arg - 0.5f);
    1.70 +    }
    1.71 +}
    1.72 +
    1.73 +void
    1.74 +WINRT_ProcessMouseMovedEvent(SDL_Window * window, Windows::Devices::Input::MouseEventArgs ^args)
    1.75 +{
    1.76 +    if (!window || !WINRT_UseRelativeMouseMode) {
    1.77 +        return;
    1.78 +    }
    1.79 +
    1.80 +    // DLudwig, 2012-12-28: On some systems, namely Visual Studio's Windows
    1.81 +    // Simulator, as well as Windows 8 in a Parallels 8 VM, MouseEventArgs'
    1.82 +    // MouseDelta field often reports very large values.  More information
    1.83 +    // on this can be found at the following pages on MSDN:
    1.84 +    //  - http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/a3c789fa-f1c5-49c4-9c0a-7db88d0f90f8
    1.85 +    //  - https://connect.microsoft.com/VisualStudio/Feedback/details/756515
    1.86 +    //
    1.87 +    // The values do not appear to be as large when running on some systems,
    1.88 +    // most notably a Surface RT.  Furthermore, the values returned by
    1.89 +    // CoreWindow's PointerMoved event, and sent to this class' OnPointerMoved
    1.90 +    // method, do not ever appear to be large, even when MouseEventArgs'
    1.91 +    // MouseDelta is reporting to the contrary.
    1.92 +    //
    1.93 +    // On systems with the large-values behavior, it appears that the values
    1.94 +    // get reported as if the screen's size is 65536 units in both the X and Y
    1.95 +    // dimensions.  This can be viewed by using Windows' now-private, "Raw Input"
    1.96 +    // APIs.  (GetRawInputData, RegisterRawInputDevices, WM_INPUT, etc.)
    1.97 +    //
    1.98 +    // MSDN's documentation on MouseEventArgs' MouseDelta field (at
    1.99 +    // http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.input.mouseeventargs.mousedelta ),
   1.100 +    // does not seem to indicate (to me) that its values should be so large.  It
   1.101 +    // says that its values should be a "change in screen location".  I could
   1.102 +    // be misinterpreting this, however a post on MSDN from a Microsoft engineer (see: 
   1.103 +    // http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/09a9868e-95bb-4858-ba1a-cb4d2c298d62 ),
   1.104 +    // indicates that these values are in DIPs, which is the same unit used
   1.105 +    // by CoreWindow's PointerMoved events (via the Position field in its CurrentPoint
   1.106 +    // property.  See http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.input.pointerpoint.position.aspx
   1.107 +    // for details.)
   1.108 +    //
   1.109 +    // To note, PointerMoved events are sent a 'RawPosition' value (via the
   1.110 +    // CurrentPoint property in MouseEventArgs), however these do not seem
   1.111 +    // to exhibit the same large-value behavior.
   1.112 +    //
   1.113 +    // The values passed via PointerMoved events can't always be used for relative
   1.114 +    // mouse motion, unfortunately.  Its values are bound to the cursor's position,
   1.115 +    // which stops when it hits one of the screen's edges.  This can be a problem in
   1.116 +    // first person shooters, whereby it is normal for mouse motion to travel far
   1.117 +    // along any one axis for a period of time.  MouseMoved events do not have the
   1.118 +    // screen-bounding limitation, and can be used regardless of where the system's
   1.119 +    // cursor is.
   1.120 +    //
   1.121 +    // One possible workaround would be to programmatically set the cursor's
   1.122 +    // position to the screen's center (when SDL's relative mouse mode is enabled),
   1.123 +    // however Windows RT does not yet seem to have the ability to set the cursor's
   1.124 +    // position via a public API.  Win32 did this via an API call, SetCursorPos,
   1.125 +    // however WinRT makes this function be private.  Apps that use it won't get
   1.126 +    // approved for distribution in the Windows Store.  I've yet to be able to find
   1.127 +    // a suitable, store-friendly counterpart for WinRT.
   1.128 +    //
   1.129 +    // There may be some room for a workaround whereby OnPointerMoved's values
   1.130 +    // are compared to the values from OnMouseMoved in order to detect
   1.131 +    // when this bug is active.  A suitable transformation could then be made to
   1.132 +    // OnMouseMoved's values.  For now, however, the system-reported values are sent
   1.133 +    // to SDL with minimal transformation: from native screen coordinates (in DIPs)
   1.134 +    // to SDL window coordinates.
   1.135 +    //
   1.136 +    const Windows::Foundation::Point mouseDeltaInDIPs((float)args->MouseDelta.X, (float)args->MouseDelta.Y);
   1.137 +    const Windows::Foundation::Point mouseDeltaInSDLWindowCoords = TransformCursor(window, mouseDeltaInDIPs);
   1.138 +    SDL_SendMouseMotion(
   1.139 +        window,
   1.140 +        0,
   1.141 +        1,
   1.142 +        _lround(mouseDeltaInSDLWindowCoords.X),
   1.143 +        _lround(mouseDeltaInSDLWindowCoords.Y));
   1.144 +}
   1.145 +
   1.146 +static Uint8
   1.147 +WINRT_GetSDLButtonForPointerPoint(Windows::UI::Input::PointerPoint ^pt)
   1.148 +{
   1.149 +    using namespace Windows::UI::Input;
   1.150 +
   1.151 +    switch (pt->Properties->PointerUpdateKind)
   1.152 +    {
   1.153 +        case PointerUpdateKind::LeftButtonPressed:
   1.154 +        case PointerUpdateKind::LeftButtonReleased:
   1.155 +            return SDL_BUTTON_LEFT;
   1.156 +
   1.157 +        case PointerUpdateKind::RightButtonPressed:
   1.158 +        case PointerUpdateKind::RightButtonReleased:
   1.159 +            return SDL_BUTTON_RIGHT;
   1.160 +
   1.161 +        case PointerUpdateKind::MiddleButtonPressed:
   1.162 +        case PointerUpdateKind::MiddleButtonReleased:
   1.163 +            return SDL_BUTTON_MIDDLE;
   1.164 +
   1.165 +        case PointerUpdateKind::XButton1Pressed:
   1.166 +        case PointerUpdateKind::XButton1Released:
   1.167 +            return SDL_BUTTON_X1;
   1.168 +
   1.169 +        case PointerUpdateKind::XButton2Pressed:
   1.170 +        case PointerUpdateKind::XButton2Released:
   1.171 +            return SDL_BUTTON_X2;
   1.172 +
   1.173 +        default:
   1.174 +            break;
   1.175 +    }
   1.176 +
   1.177 +    return 0;
   1.178 +}
   1.179 +
   1.180 +static const char *
   1.181 +WINRT_ConvertPointerUpdateKindToString(Windows::UI::Input::PointerUpdateKind kind)
   1.182 +{
   1.183 +    using namespace Windows::UI::Input;
   1.184 +
   1.185 +    switch (kind)
   1.186 +    {
   1.187 +        case PointerUpdateKind::Other:
   1.188 +            return "Other";
   1.189 +        case PointerUpdateKind::LeftButtonPressed:
   1.190 +            return "LeftButtonPressed";
   1.191 +        case PointerUpdateKind::LeftButtonReleased:
   1.192 +            return "LeftButtonReleased";
   1.193 +        case PointerUpdateKind::RightButtonPressed:
   1.194 +            return "RightButtonPressed";
   1.195 +        case PointerUpdateKind::RightButtonReleased:
   1.196 +            return "RightButtonReleased";
   1.197 +        case PointerUpdateKind::MiddleButtonPressed:
   1.198 +            return "MiddleButtonPressed";
   1.199 +        case PointerUpdateKind::MiddleButtonReleased:
   1.200 +            return "MiddleButtonReleased";
   1.201 +        case PointerUpdateKind::XButton1Pressed:
   1.202 +            return "XButton1Pressed";
   1.203 +        case PointerUpdateKind::XButton1Released:
   1.204 +            return "XButton1Released";
   1.205 +        case PointerUpdateKind::XButton2Pressed:
   1.206 +            return "XButton2Pressed";
   1.207 +        case PointerUpdateKind::XButton2Released:
   1.208 +            return "XButton2Released";
   1.209 +    }
   1.210 +
   1.211 +    return "";
   1.212 +}
   1.213 +
   1.214 +static void
   1.215 +WINRT_LogPointerEvent(const char * header, PointerEventArgs ^ args, Windows::Foundation::Point transformedPoint)
   1.216 +{
   1.217 +    Windows::UI::Input::PointerPoint ^ pt = args->CurrentPoint;
   1.218 +    SDL_Log("%s: Position={%f,%f}, Transformed Pos={%f, %f}, MouseWheelDelta=%d, FrameId=%d, PointerId=%d, PointerUpdateKind=%s\n",
   1.219 +        header,
   1.220 +        pt->Position.X, pt->Position.Y,
   1.221 +        transformedPoint.X, transformedPoint.Y,
   1.222 +        pt->Properties->MouseWheelDelta,
   1.223 +        pt->FrameId,
   1.224 +        pt->PointerId,
   1.225 +        WINRT_ConvertPointerUpdateKindToString(args->CurrentPoint->Properties->PointerUpdateKind));
   1.226 +}
   1.227 +
   1.228 +void
   1.229 +WINRT_ProcessPointerMovedEvent(SDL_Window *window, Windows::UI::Core::PointerEventArgs ^args)
   1.230 +{
   1.231 +#if LOG_POINTER_EVENTS
   1.232 +    WINRT_LogPointerEvent("pointer moved", args, TransformCursor(args->CurrentPoint->Position));
   1.233 +#endif
   1.234 +
   1.235 +    if (!window || WINRT_UseRelativeMouseMode) {
   1.236 +        return;
   1.237 +    }
   1.238 +
   1.239 +    Windows::Foundation::Point transformedPoint = TransformCursor(window, args->CurrentPoint->Position);
   1.240 +    SDL_SendMouseMotion(window, 0, 0, (int)transformedPoint.X, (int)transformedPoint.Y);
   1.241 +}
   1.242 +
   1.243 +void
   1.244 +WINRT_ProcessPointerWheelChangedEvent(SDL_Window *window, Windows::UI::Core::PointerEventArgs ^args)
   1.245 +{
   1.246 +#if LOG_POINTER_EVENTS
   1.247 +    WINRT_LogPointerEvent("wheel changed", args, TransformCursor(args->CurrentPoint->Position));
   1.248 +#endif
   1.249 +
   1.250 +    if (!window) {
   1.251 +        return;
   1.252 +    }
   1.253 +
   1.254 +    // FIXME: This may need to accumulate deltas up to WHEEL_DELTA
   1.255 +    short motion = args->CurrentPoint->Properties->MouseWheelDelta / WHEEL_DELTA;
   1.256 +    SDL_SendMouseWheel(window, 0, 0, motion);
   1.257 +}
   1.258 +
   1.259 +void WINRT_ProcessPointerReleasedEvent(SDL_Window *window, Windows::UI::Core::PointerEventArgs ^args)
   1.260 +{
   1.261 +#if LOG_POINTER_EVENTS
   1.262 +    WINRT_LogPointerEvent("mouse up", args, TransformCursor(args->CurrentPoint->Position));
   1.263 +#endif
   1.264 +
   1.265 +    if (!window) {
   1.266 +        return;
   1.267 +    }
   1.268 +
   1.269 +    Uint8 button = WINRT_GetSDLButtonForPointerPoint(args->CurrentPoint);
   1.270 +    if (button) {
   1.271 +        SDL_SendMouseButton(window, 0, SDL_RELEASED, button);
   1.272 +    }
   1.273 +}
   1.274 +
   1.275 +void WINRT_ProcessPointerPressedEvent(SDL_Window *window, Windows::UI::Core::PointerEventArgs ^args)
   1.276 +{
   1.277 +#if LOG_POINTER_EVENTS
   1.278 +    WINRT_LogPointerEvent("mouse down", args, TransformCursor(args->CurrentPoint->Position));
   1.279 +#endif
   1.280 +
   1.281 +    if (!window) {
   1.282 +        return;
   1.283 +    }
   1.284 +
   1.285 +    Uint8 button = WINRT_GetSDLButtonForPointerPoint(args->CurrentPoint);
   1.286 +    if (button) {
   1.287 +        SDL_SendMouseButton(window, 0, SDL_PRESSED, button);
   1.288 +    }
   1.289 +}
   1.290 +
   1.291  #endif /* SDL_VIDEO_DRIVER_WINRT */
   1.292  
   1.293  /* vi: set ts=4 sw=4 expandtab: */