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