src/core/winrt/SDL_winrtapp_direct3d.cpp
author Sam Lantinga <slouken@libsdl.org>
Sun, 12 Nov 2017 10:59:05 -0800
changeset 11701 d131f3193794
parent 11494 d167d43d74f2
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Fixed Android build error on older SDK
slouken@8616
     1
/*
slouken@8616
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@8616
     4
slouken@8616
     5
  This software is provided 'as-is', without any express or implied
slouken@8616
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@8616
     7
  arising from the use of this software.
slouken@8616
     8
slouken@8616
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@8616
    10
  including commercial applications, and to alter it and redistribute it
slouken@8616
    11
  freely, subject to the following restrictions:
slouken@8616
    12
slouken@8616
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@8616
    14
     claim that you wrote the original software. If you use this software
slouken@8616
    15
     in a product, an acknowledgment in the product documentation would be
slouken@8616
    16
     appreciated but is not required.
slouken@8616
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@8616
    18
     misrepresented as being the original software.
slouken@8616
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@8616
    20
*/
slouken@8616
    21
#include "../../SDL_internal.h"
slouken@8616
    22
slouken@8582
    23
/* Standard C++11 includes */
slouken@8582
    24
#include <functional>
slouken@8582
    25
#include <string>
slouken@8582
    26
#include <sstream>
slouken@8582
    27
using namespace std;
slouken@8582
    28
slouken@8582
    29
slouken@8582
    30
/* Windows includes */
slouken@8582
    31
#include "ppltasks.h"
slouken@8582
    32
using namespace concurrency;
slouken@8582
    33
using namespace Windows::ApplicationModel;
slouken@8582
    34
using namespace Windows::ApplicationModel::Core;
slouken@8582
    35
using namespace Windows::ApplicationModel::Activation;
slouken@8582
    36
using namespace Windows::Devices::Input;
slouken@8582
    37
using namespace Windows::Graphics::Display;
slouken@8582
    38
using namespace Windows::Foundation;
slouken@8582
    39
using namespace Windows::System;
slouken@8582
    40
using namespace Windows::UI::Core;
slouken@8582
    41
using namespace Windows::UI::Input;
slouken@8582
    42
slouken@8582
    43
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
slouken@8582
    44
using namespace Windows::Phone::UI::Input;
slouken@8582
    45
#endif
slouken@8582
    46
slouken@8582
    47
slouken@8582
    48
/* SDL includes */
slouken@8582
    49
extern "C" {
slouken@8582
    50
#include "SDL_assert.h"
slouken@8582
    51
#include "SDL_events.h"
slouken@8582
    52
#include "SDL_hints.h"
slouken@8582
    53
#include "SDL_log.h"
slouken@8582
    54
#include "SDL_main.h"
slouken@8582
    55
#include "SDL_stdinc.h"
slouken@8582
    56
#include "SDL_render.h"
slouken@8582
    57
#include "../../video/SDL_sysvideo.h"
slouken@8582
    58
//#include "../../SDL_hints_c.h"
slouken@8582
    59
#include "../../events/SDL_events_c.h"
slouken@8582
    60
#include "../../events/SDL_keyboard_c.h"
slouken@8582
    61
#include "../../events/SDL_mouse_c.h"
slouken@8582
    62
#include "../../events/SDL_windowevents_c.h"
slouken@8582
    63
#include "../../render/SDL_sysrender.h"
slouken@8582
    64
#include "../windows/SDL_windows.h"
slouken@8582
    65
}
slouken@8582
    66
slouken@8582
    67
#include "../../video/winrt/SDL_winrtevents_c.h"
slouken@8582
    68
#include "../../video/winrt/SDL_winrtvideo_cpp.h"
slouken@8582
    69
#include "SDL_winrtapp_common.h"
slouken@8582
    70
#include "SDL_winrtapp_direct3d.h"
slouken@8582
    71
dludwig@8679
    72
#if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
dludwig@8679
    73
/* Calling IDXGIDevice3::Trim on the active Direct3D 11.x device is necessary
dludwig@8679
    74
 * when Windows 8.1 apps are about to get suspended.
dludwig@8679
    75
 */
dludwig@8679
    76
extern "C" void D3D11_Trim(SDL_Renderer *);
dludwig@8679
    77
#endif
dludwig@8679
    78
slouken@8582
    79
slouken@8582
    80
// Compile-time debugging options:
slouken@8582
    81
// To enable, uncomment; to disable, comment them out.
slouken@8582
    82
//#define LOG_POINTER_EVENTS 1
slouken@8582
    83
//#define LOG_WINDOW_EVENTS 1
slouken@8582
    84
//#define LOG_ORIENTATION_EVENTS 1
slouken@8582
    85
slouken@8582
    86
slouken@8582
    87
// HACK, DLudwig: record a reference to the global, WinRT 'app'/view.
slouken@8582
    88
// SDL/WinRT will use this throughout its code.
slouken@8582
    89
//
slouken@8582
    90
// TODO, WinRT: consider replacing SDL_WinRTGlobalApp with something
slouken@8582
    91
// non-global, such as something created inside
slouken@8582
    92
// SDL_InitSubSystem(SDL_INIT_VIDEO), or something inside
slouken@8582
    93
// SDL_CreateWindow().
slouken@8582
    94
SDL_WinRTApp ^ SDL_WinRTGlobalApp = nullptr;
slouken@8582
    95
slouken@8582
    96
ref class SDLApplicationSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource
slouken@8582
    97
{
slouken@8582
    98
public:
slouken@8582
    99
    virtual Windows::ApplicationModel::Core::IFrameworkView^ CreateView();
slouken@8582
   100
};
slouken@8582
   101
slouken@8582
   102
IFrameworkView^ SDLApplicationSource::CreateView()
slouken@8582
   103
{
slouken@8582
   104
    // TODO, WinRT: see if this function (CreateView) can ever get called
slouken@8582
   105
    // more than once.  For now, just prevent it from ever assigning
slouken@8582
   106
    // SDL_WinRTGlobalApp more than once.
slouken@8582
   107
    SDL_assert(!SDL_WinRTGlobalApp);
slouken@8582
   108
    SDL_WinRTApp ^ app = ref new SDL_WinRTApp();
slouken@8582
   109
    if (!SDL_WinRTGlobalApp)
slouken@8582
   110
    {
slouken@8582
   111
        SDL_WinRTGlobalApp = app;
slouken@8582
   112
    }
slouken@8582
   113
    return app;
slouken@8582
   114
}
slouken@8582
   115
slouken@8582
   116
int SDL_WinRTInitNonXAMLApp(int (*mainFunction)(int, char **))
slouken@8582
   117
{
slouken@8582
   118
    WINRT_SDLAppEntryPoint = mainFunction;
slouken@8582
   119
    auto direct3DApplicationSource = ref new SDLApplicationSource();
slouken@8582
   120
    CoreApplication::Run(direct3DApplicationSource);
slouken@8582
   121
    return 0;
slouken@8582
   122
}
slouken@8582
   123
slouken@11284
   124
static void SDLCALL
slouken@11284
   125
WINRT_SetDisplayOrientationsPreference(void *userdata, const char *name, const char *oldValue, const char *newValue)
slouken@8582
   126
{
slouken@8582
   127
    SDL_assert(SDL_strcmp(name, SDL_HINT_ORIENTATIONS) == 0);
slouken@8582
   128
dludwig@9256
   129
    /* HACK: prevent SDL from altering an app's .appxmanifest-set orientation
dludwig@9256
   130
     * from being changed on startup, by detecting when SDL_HINT_ORIENTATIONS
dludwig@9256
   131
     * is getting registered.
dludwig@9256
   132
     *
dludwig@9256
   133
     * TODO, WinRT: consider reading in an app's .appxmanifest file, and apply its orientation when 'newValue == NULL'.
dludwig@9256
   134
     */
dludwig@9256
   135
    if ((oldValue == NULL) && (newValue == NULL)) {
dludwig@9256
   136
        return;
dludwig@9256
   137
    }
dludwig@9256
   138
slouken@8582
   139
    // Start with no orientation flags, then add each in as they're parsed
slouken@8582
   140
    // from newValue.
slouken@8582
   141
    unsigned int orientationFlags = 0;
slouken@8582
   142
    if (newValue) {
slouken@8582
   143
        std::istringstream tokenizer(newValue);
slouken@8582
   144
        while (!tokenizer.eof()) {
slouken@8582
   145
            std::string orientationName;
slouken@8582
   146
            std::getline(tokenizer, orientationName, ' ');
slouken@8582
   147
            if (orientationName == "LandscapeLeft") {
slouken@8582
   148
                orientationFlags |= (unsigned int) DisplayOrientations::LandscapeFlipped;
slouken@8582
   149
            } else if (orientationName == "LandscapeRight") {
slouken@8582
   150
                orientationFlags |= (unsigned int) DisplayOrientations::Landscape;
slouken@8582
   151
            } else if (orientationName == "Portrait") {
slouken@8582
   152
                orientationFlags |= (unsigned int) DisplayOrientations::Portrait;
slouken@8582
   153
            } else if (orientationName == "PortraitUpsideDown") {
slouken@8582
   154
                orientationFlags |= (unsigned int) DisplayOrientations::PortraitFlipped;
slouken@8582
   155
            }
slouken@8582
   156
        }
slouken@8582
   157
    }
slouken@8582
   158
slouken@8582
   159
    // If no valid orientation flags were specified, use a reasonable set of defaults:
slouken@8582
   160
    if (!orientationFlags) {
slouken@8582
   161
        // TODO, WinRT: consider seeing if an app's default orientation flags can be found out via some API call(s).
slouken@8582
   162
        orientationFlags = (unsigned int) ( \
slouken@8582
   163
            DisplayOrientations::Landscape |
slouken@8582
   164
            DisplayOrientations::LandscapeFlipped |
slouken@8582
   165
            DisplayOrientations::Portrait |
slouken@8582
   166
            DisplayOrientations::PortraitFlipped);
slouken@8582
   167
    }
slouken@8582
   168
slouken@8582
   169
    // Set the orientation/rotation preferences.  Please note that this does
slouken@8582
   170
    // not constitute a 100%-certain lock of a given set of possible
slouken@8582
   171
    // orientations.  According to Microsoft's documentation on WinRT [1]
slouken@8582
   172
    // when a device is not capable of being rotated, Windows may ignore
slouken@8582
   173
    // the orientation preferences, and stick to what the device is capable of
slouken@8582
   174
    // displaying.
slouken@8582
   175
    //
slouken@8582
   176
    // [1] Documentation on the 'InitialRotationPreference' setting for a
slouken@8582
   177
    // Windows app's manifest file describes how some orientation/rotation
slouken@8582
   178
    // preferences may be ignored.  See
slouken@8582
   179
    // http://msdn.microsoft.com/en-us/library/windows/apps/hh700343.aspx
slouken@8582
   180
    // for details.  Microsoft's "Display orientation sample" also gives an
slouken@8582
   181
    // outline of how Windows treats device rotation
slouken@8582
   182
    // (http://code.msdn.microsoft.com/Display-Orientation-Sample-19a58e93).
dludwig@8757
   183
    WINRT_DISPLAY_PROPERTY(AutoRotationPreferences) = (DisplayOrientations) orientationFlags;
slouken@8582
   184
}
slouken@8582
   185
slouken@8582
   186
static void
dludwig@9924
   187
WINRT_ProcessWindowSizeChange() // TODO: Pass an SDL_Window-identifying thing into WINRT_ProcessWindowSizeChange()
slouken@8582
   188
{
dludwig@9924
   189
    CoreWindow ^ coreWindow = CoreWindow::GetForCurrentThread();
dludwig@9924
   190
    if (coreWindow) {
dludwig@9924
   191
        if (WINRT_GlobalSDLWindow) {
dludwig@9924
   192
            SDL_Window * window = WINRT_GlobalSDLWindow;
dludwig@9924
   193
            SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8664
   194
dludwig@9924
   195
            int x = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Left);
dludwig@9924
   196
            int y = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Top);
dludwig@9924
   197
            int w = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Width);
dludwig@9924
   198
            int h = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Height);
slouken@8582
   199
dludwig@9924
   200
#if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP) && (NTDDI_VERSION == NTDDI_WIN8)
dludwig@9924
   201
            /* WinPhone 8.0 always keeps its native window size in portrait,
dludwig@9924
   202
               regardless of orientation.  This changes in WinPhone 8.1,
dludwig@9924
   203
               in which the native window's size changes along with
dludwig@9924
   204
               orientation.
slouken@8582
   205
dludwig@9924
   206
               Attempt to emulate WinPhone 8.1's behavior on WinPhone 8.0, with
dludwig@9924
   207
               regards to window size.  This fixes a rendering bug that occurs
dludwig@9924
   208
               when a WinPhone 8.0 app is rotated to either 90 or 270 degrees.
dludwig@9924
   209
            */
dludwig@9924
   210
            const DisplayOrientations currentOrientation = WINRT_DISPLAY_PROPERTY(CurrentOrientation);
dludwig@9924
   211
            switch (currentOrientation) {
dludwig@9924
   212
                case DisplayOrientations::Landscape:
dludwig@9924
   213
                case DisplayOrientations::LandscapeFlipped: {
dludwig@9924
   214
                    int tmp = w;
dludwig@9924
   215
                    w = h;
dludwig@9924
   216
                    h = tmp;
dludwig@9924
   217
                } break;
slouken@8582
   218
            }
dludwig@8755
   219
#endif
dludwig@9924
   220
dludwig@9971
   221
            const Uint32 latestFlags = WINRT_DetectWindowFlags(window);
dludwig@9971
   222
            if (latestFlags & SDL_WINDOW_MAXIMIZED) {
dludwig@9971
   223
                SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
dludwig@9992
   224
            } else {
dludwig@9992
   225
                SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
dludwig@9971
   226
            }
dludwig@9971
   227
dludwig@9971
   228
            WINRT_UpdateWindowFlags(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
dludwig@9924
   229
dludwig@9924
   230
            /* The window can move during a resize event, such as when maximizing
dludwig@9924
   231
               or resizing from a corner */
dludwig@9924
   232
            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
dludwig@9924
   233
            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@8582
   234
        }
slouken@8582
   235
    }
slouken@8582
   236
}
slouken@8582
   237
slouken@8582
   238
SDL_WinRTApp::SDL_WinRTApp() :
slouken@8582
   239
    m_windowClosed(false),
slouken@8582
   240
    m_windowVisible(true)
slouken@8582
   241
{
slouken@8582
   242
}
slouken@8582
   243
slouken@8582
   244
void SDL_WinRTApp::Initialize(CoreApplicationView^ applicationView)
slouken@8582
   245
{
slouken@8582
   246
    applicationView->Activated +=
dludwig@9924
   247
        ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &SDL_WinRTApp::OnAppActivated);
slouken@8582
   248
slouken@8582
   249
    CoreApplication::Suspending +=
slouken@8582
   250
        ref new EventHandler<SuspendingEventArgs^>(this, &SDL_WinRTApp::OnSuspending);
slouken@8582
   251
slouken@8582
   252
    CoreApplication::Resuming +=
slouken@8582
   253
        ref new EventHandler<Platform::Object^>(this, &SDL_WinRTApp::OnResuming);
slouken@8582
   254
slouken@8582
   255
    CoreApplication::Exiting +=
slouken@8582
   256
        ref new EventHandler<Platform::Object^>(this, &SDL_WinRTApp::OnExiting);
dludwig@10144
   257
dludwig@10144
   258
#if NTDDI_VERSION >= NTDDI_WIN10
dludwig@10144
   259
    /* HACK ALERT!  Xbox One doesn't seem to detect gamepads unless something
dludwig@10144
   260
       gets registered to receive Win10's Windows.Gaming.Input.Gamepad.GamepadAdded
dludwig@10144
   261
       events.  We'll register an event handler for these events here, to make
dludwig@10144
   262
       sure that gamepad detection works later on, if requested.
dludwig@10144
   263
    */
dludwig@10144
   264
    Windows::Gaming::Input::Gamepad::GamepadAdded +=
dludwig@10144
   265
        ref new Windows::Foundation::EventHandler<Windows::Gaming::Input::Gamepad^>(
dludwig@10144
   266
            this, &SDL_WinRTApp::OnGamepadAdded
dludwig@10144
   267
        );
dludwig@10144
   268
#endif
slouken@8582
   269
}
slouken@8582
   270
dludwig@8621
   271
#if NTDDI_VERSION > NTDDI_WIN8
dludwig@8621
   272
void SDL_WinRTApp::OnOrientationChanged(DisplayInformation^ sender, Object^ args)
dludwig@8621
   273
#else
slouken@8582
   274
void SDL_WinRTApp::OnOrientationChanged(Object^ sender)
dludwig@8621
   275
#endif
slouken@8582
   276
{
slouken@8582
   277
#if LOG_ORIENTATION_EVENTS==1
dludwig@9924
   278
    {
dludwig@9924
   279
        CoreWindow^ window = CoreWindow::GetForCurrentThread();
dludwig@9924
   280
        if (window) {
dludwig@9924
   281
            SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, CoreWindow Bounds={%f,%f,%f,%f}\n",
dludwig@9924
   282
                __FUNCTION__,
dludwig@9924
   283
                WINRT_DISPLAY_PROPERTY(CurrentOrientation),
dludwig@9924
   284
                WINRT_DISPLAY_PROPERTY(NativeOrientation),
dludwig@9924
   285
                WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
dludwig@9924
   286
                window->Bounds.X,
dludwig@9924
   287
                window->Bounds.Y,
dludwig@9924
   288
                window->Bounds.Width,
dludwig@9924
   289
                window->Bounds.Height);
dludwig@9924
   290
        } else {
dludwig@9924
   291
            SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d\n",
dludwig@9924
   292
                __FUNCTION__,
dludwig@9924
   293
                WINRT_DISPLAY_PROPERTY(CurrentOrientation),
dludwig@9924
   294
                WINRT_DISPLAY_PROPERTY(NativeOrientation),
dludwig@9924
   295
                WINRT_DISPLAY_PROPERTY(AutoRotationPreferences));
dludwig@9924
   296
        }
slouken@8582
   297
    }
slouken@8582
   298
#endif
slouken@8582
   299
slouken@8582
   300
    WINRT_ProcessWindowSizeChange();
dludwig@9924
   301
dludwig@9924
   302
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@9924
   303
    // HACK: Make sure that orientation changes
dludwig@9924
   304
    // lead to the Direct3D renderer's viewport getting updated:
dludwig@9924
   305
    //
dludwig@9924
   306
    // For some reason, this doesn't seem to need to be done on Windows 8.x,
dludwig@9924
   307
    // even when going from Landscape to LandscapeFlipped.  It only seems to
dludwig@9924
   308
    // be needed on Windows Phone, at least when I tested on my devices.
dludwig@9924
   309
    // I'm not currently sure why this is, but it seems to work fine. -- David L.
dludwig@9924
   310
    //
dludwig@9924
   311
    // TODO, WinRT: do more extensive research into why orientation changes on Win 8.x don't need D3D changes, or if they might, in some cases
dludwig@9924
   312
    SDL_Window * window = WINRT_GlobalSDLWindow;
dludwig@9924
   313
    if (window) {
dludwig@9924
   314
        SDL_WindowData * data = (SDL_WindowData *)window->driverdata;
dludwig@9924
   315
        int w = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Width);
dludwig@9924
   316
        int h = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Height);
dludwig@9924
   317
        SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_SIZE_CHANGED, w, h);
dludwig@9924
   318
    }
dludwig@9924
   319
#endif
dludwig@9924
   320
slouken@8582
   321
}
slouken@8582
   322
slouken@8582
   323
void SDL_WinRTApp::SetWindow(CoreWindow^ window)
slouken@8582
   324
{
slouken@8582
   325
#if LOG_WINDOW_EVENTS==1
dludwig@9924
   326
    SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, window bounds={%f, %f, %f,%f}\n",
slouken@8582
   327
        __FUNCTION__,
dludwig@8755
   328
        WINRT_DISPLAY_PROPERTY(CurrentOrientation),
dludwig@8755
   329
        WINRT_DISPLAY_PROPERTY(NativeOrientation),
dludwig@8755
   330
        WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
dludwig@9924
   331
        window->Bounds.X,
dludwig@9924
   332
        window->Bounds.Y,
slouken@8582
   333
        window->Bounds.Width,
slouken@8582
   334
        window->Bounds.Height);
slouken@8582
   335
#endif
slouken@8582
   336
slouken@8582
   337
    window->SizeChanged += 
slouken@8582
   338
        ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &SDL_WinRTApp::OnWindowSizeChanged);
slouken@8582
   339
slouken@8582
   340
    window->VisibilityChanged +=
slouken@8582
   341
        ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &SDL_WinRTApp::OnVisibilityChanged);
slouken@8582
   342
dludwig@9924
   343
    window->Activated +=
dludwig@9924
   344
        ref new TypedEventHandler<CoreWindow^, WindowActivatedEventArgs^>(this, &SDL_WinRTApp::OnWindowActivated);
dludwig@9924
   345
slouken@8582
   346
    window->Closed += 
slouken@8582
   347
        ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &SDL_WinRTApp::OnWindowClosed);
slouken@8582
   348
slouken@8582
   349
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
slouken@8582
   350
    window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
slouken@8582
   351
#endif
slouken@8582
   352
slouken@8582
   353
    window->PointerPressed +=
slouken@8582
   354
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerPressed);
slouken@8582
   355
slouken@8582
   356
    window->PointerMoved +=
slouken@8582
   357
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerMoved);
slouken@8582
   358
slouken@8582
   359
    window->PointerReleased +=
slouken@8582
   360
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerReleased);
slouken@8582
   361
dludwig@9924
   362
    window->PointerEntered +=
dludwig@9924
   363
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerEntered);
dludwig@9924
   364
dludwig@9924
   365
    window->PointerExited +=
dludwig@9924
   366
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerExited);
dludwig@9924
   367
slouken@8582
   368
    window->PointerWheelChanged +=
slouken@8582
   369
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerWheelChanged);
slouken@8582
   370
slouken@8582
   371
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
slouken@8582
   372
    // Retrieves relative-only mouse movements:
slouken@8582
   373
    Windows::Devices::Input::MouseDevice::GetForCurrentView()->MouseMoved +=
slouken@8582
   374
        ref new TypedEventHandler<MouseDevice^, MouseEventArgs^>(this, &SDL_WinRTApp::OnMouseMoved);
slouken@8582
   375
#endif
slouken@8582
   376
slouken@8582
   377
    window->KeyDown +=
slouken@8582
   378
        ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyDown);
slouken@8582
   379
slouken@8582
   380
    window->KeyUp +=
slouken@8582
   381
        ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyUp);
slouken@8582
   382
dludwig@9240
   383
    window->CharacterReceived +=
dludwig@9240
   384
        ref new TypedEventHandler<CoreWindow^, CharacterReceivedEventArgs^>(this, &SDL_WinRTApp::OnCharacterReceived);
dludwig@9240
   385
dludwig@9957
   386
#if NTDDI_VERSION >= NTDDI_WIN10
dludwig@9957
   387
    Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->BackRequested +=
dludwig@9957
   388
        ref new EventHandler<BackRequestedEventArgs^>(this, &SDL_WinRTApp::OnBackButtonPressed);
dludwig@9957
   389
#elif WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
slouken@8582
   390
    HardwareButtons::BackPressed +=
slouken@8582
   391
        ref new EventHandler<BackPressedEventArgs^>(this, &SDL_WinRTApp::OnBackButtonPressed);
slouken@8582
   392
#endif
slouken@8582
   393
dludwig@8621
   394
#if NTDDI_VERSION > NTDDI_WIN8
dludwig@8621
   395
    DisplayInformation::GetForCurrentView()->OrientationChanged +=
dludwig@8621
   396
        ref new TypedEventHandler<Windows::Graphics::Display::DisplayInformation^, Object^>(this, &SDL_WinRTApp::OnOrientationChanged);
dludwig@8621
   397
#else
dludwig@8621
   398
    DisplayProperties::OrientationChanged +=
dludwig@8621
   399
        ref new DisplayPropertiesEventHandler(this, &SDL_WinRTApp::OnOrientationChanged);
dludwig@8621
   400
#endif
dludwig@8621
   401
dludwig@8621
   402
    // Register the hint, SDL_HINT_ORIENTATIONS, with SDL.
dludwig@8621
   403
    // TODO, WinRT: see if an app's default orientation can be found out via WinRT API(s), then set the initial value of SDL_HINT_ORIENTATIONS accordingly.
dludwig@8621
   404
    SDL_AddHintCallback(SDL_HINT_ORIENTATIONS, WINRT_SetDisplayOrientationsPreference, NULL);
dludwig@8621
   405
dludwig@9931
   406
#if (WINAPI_FAMILY == WINAPI_FAMILY_APP) && (NTDDI_VERSION < NTDDI_WIN10)  // for Windows 8/8.1/RT apps... (and not Phone apps)
slouken@8582
   407
    // Make sure we know when a user has opened the app's settings pane.
slouken@8582
   408
    // This is needed in order to display a privacy policy, which needs
slouken@8582
   409
    // to be done for network-enabled apps, as per Windows Store requirements.
slouken@8582
   410
    using namespace Windows::UI::ApplicationSettings;
slouken@8582
   411
    SettingsPane::GetForCurrentView()->CommandsRequested +=
slouken@8582
   412
        ref new TypedEventHandler<SettingsPane^, SettingsPaneCommandsRequestedEventArgs^>
slouken@8582
   413
            (this, &SDL_WinRTApp::OnSettingsPaneCommandsRequested);
slouken@8582
   414
#endif
slouken@8582
   415
}
slouken@8582
   416
slouken@8582
   417
void SDL_WinRTApp::Load(Platform::String^ entryPoint)
slouken@8582
   418
{
slouken@8582
   419
}
slouken@8582
   420
slouken@8582
   421
void SDL_WinRTApp::Run()
slouken@8582
   422
{
slouken@8582
   423
    SDL_SetMainReady();
slouken@8582
   424
    if (WINRT_SDLAppEntryPoint)
slouken@8582
   425
    {
slouken@8582
   426
        // TODO, WinRT: pass the C-style main() a reasonably realistic
slouken@8582
   427
        // representation of command line arguments.
slouken@8582
   428
        int argc = 0;
slouken@8582
   429
        char **argv = NULL;
slouken@8582
   430
        WINRT_SDLAppEntryPoint(argc, argv);
slouken@8582
   431
    }
slouken@8582
   432
}
slouken@8582
   433
dludwig@8666
   434
static bool IsSDLWindowEventPending(SDL_WindowEventID windowEventID)
dludwig@8666
   435
{
dludwig@8666
   436
    SDL_Event events[128];
dludwig@8666
   437
    const int count = SDL_PeepEvents(events, sizeof(events)/sizeof(SDL_Event), SDL_PEEKEVENT, SDL_WINDOWEVENT, SDL_WINDOWEVENT);
dludwig@8666
   438
    for (int i = 0; i < count; ++i) {
dludwig@8666
   439
        if (events[i].window.event == windowEventID) {
dludwig@8666
   440
            return true;
dludwig@8666
   441
        }
dludwig@8666
   442
    }
dludwig@8666
   443
    return false;
dludwig@8666
   444
}
dludwig@8666
   445
dludwig@8666
   446
bool SDL_WinRTApp::ShouldWaitForAppResumeEvents()
dludwig@8666
   447
{
dludwig@8666
   448
    /* Don't wait if the app is visible: */
dludwig@8666
   449
    if (m_windowVisible) {
dludwig@8666
   450
        return false;
dludwig@8666
   451
    }
dludwig@8666
   452
    
dludwig@8666
   453
    /* Don't wait until the window-hide events finish processing.
dludwig@8666
   454
     * Do note that if an app-suspend event is sent (as indicated
dludwig@8666
   455
     * by SDL_APP_WILLENTERBACKGROUND and SDL_APP_DIDENTERBACKGROUND
dludwig@8666
   456
     * events), then this code may be a moot point, as WinRT's
dludwig@8666
   457
     * own event pump (aka ProcessEvents()) will pause regardless
dludwig@8666
   458
     * of what we do here.  This happens on Windows Phone 8, to note.
dludwig@8666
   459
     * Windows 8.x apps, on the other hand, may get a chance to run
dludwig@8666
   460
     * these.
dludwig@8666
   461
     */
dludwig@8666
   462
    if (IsSDLWindowEventPending(SDL_WINDOWEVENT_HIDDEN)) {
dludwig@8666
   463
        return false;
dludwig@8666
   464
    } else if (IsSDLWindowEventPending(SDL_WINDOWEVENT_FOCUS_LOST)) {
dludwig@8666
   465
        return false;
dludwig@8666
   466
    } else if (IsSDLWindowEventPending(SDL_WINDOWEVENT_MINIMIZED)) {
dludwig@8666
   467
        return false;
dludwig@8666
   468
    }
dludwig@8666
   469
dludwig@8666
   470
    return true;
dludwig@8666
   471
}
dludwig@8666
   472
slouken@8582
   473
void SDL_WinRTApp::PumpEvents()
slouken@8582
   474
{
dludwig@8666
   475
    if (!m_windowClosed) {
dludwig@8666
   476
        if (!ShouldWaitForAppResumeEvents()) {
dludwig@8666
   477
            /* This is the normal way in which events should be pumped.
dludwig@8666
   478
             * 'ProcessAllIfPresent' will make ProcessEvents() process anywhere
dludwig@8666
   479
             * from zero to N events, and will then return.
dludwig@8666
   480
             */
slouken@8582
   481
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
dludwig@8666
   482
        } else {
dludwig@8666
   483
            /* This style of event-pumping, with 'ProcessOneAndAllPending',
dludwig@8666
   484
             * will cause anywhere from one to N events to be processed.  If
dludwig@8666
   485
             * at least one event is processed, the call will return.  If
dludwig@8666
   486
             * no events are pending, then the call will wait until one is
dludwig@8666
   487
             * available, and will not return (to the caller) until this
dludwig@8666
   488
             * happens!  This should only occur when the app is hidden.
dludwig@8666
   489
             */
slouken@8582
   490
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
slouken@8582
   491
        }
slouken@8582
   492
    }
slouken@8582
   493
}
slouken@8582
   494
slouken@8582
   495
void SDL_WinRTApp::Uninitialize()
slouken@8582
   496
{
slouken@8582
   497
}
slouken@8582
   498
dludwig@9931
   499
#if (WINAPI_FAMILY == WINAPI_FAMILY_APP) && (NTDDI_VERSION < NTDDI_WIN10)
slouken@8582
   500
void SDL_WinRTApp::OnSettingsPaneCommandsRequested(
slouken@8582
   501
    Windows::UI::ApplicationSettings::SettingsPane ^p,
slouken@8582
   502
    Windows::UI::ApplicationSettings::SettingsPaneCommandsRequestedEventArgs ^args)
slouken@8582
   503
{
slouken@8582
   504
    using namespace Platform;
slouken@8582
   505
    using namespace Windows::UI::ApplicationSettings;
slouken@8582
   506
    using namespace Windows::UI::Popups;
slouken@8582
   507
slouken@8582
   508
    String ^privacyPolicyURL = nullptr;     // a URL to an app's Privacy Policy
slouken@8582
   509
    String ^privacyPolicyLabel = nullptr;   // label/link text
slouken@8582
   510
    const char *tmpHintValue = NULL;        // SDL_GetHint-retrieved value, used immediately
slouken@8582
   511
    wchar_t *tmpStr = NULL;                 // used for UTF8 to UCS2 conversion
slouken@8582
   512
slouken@8582
   513
    // Setup a 'Privacy Policy' link, if one is available (via SDL_GetHint):
slouken@8582
   514
    tmpHintValue = SDL_GetHint(SDL_HINT_WINRT_PRIVACY_POLICY_URL);
slouken@8582
   515
    if (tmpHintValue && tmpHintValue[0] != '\0') {
slouken@8582
   516
        // Convert the privacy policy's URL to UCS2:
slouken@8582
   517
        tmpStr = WIN_UTF8ToString(tmpHintValue);
slouken@8582
   518
        privacyPolicyURL = ref new String(tmpStr);
slouken@8582
   519
        SDL_free(tmpStr);
slouken@8582
   520
slouken@8582
   521
        // Optionally retrieve custom label-text for the link.  If this isn't
slouken@8582
   522
        // available, a default value will be used instead.
slouken@8582
   523
        tmpHintValue = SDL_GetHint(SDL_HINT_WINRT_PRIVACY_POLICY_LABEL);
slouken@8582
   524
        if (tmpHintValue && tmpHintValue[0] != '\0') {
slouken@8582
   525
            tmpStr = WIN_UTF8ToString(tmpHintValue);
slouken@8582
   526
            privacyPolicyLabel = ref new String(tmpStr);
slouken@8582
   527
            SDL_free(tmpStr);
slouken@8582
   528
        } else {
slouken@8582
   529
            privacyPolicyLabel = ref new String(L"Privacy Policy");
slouken@8582
   530
        }
slouken@8582
   531
slouken@8582
   532
        // Register the link, along with a handler to be called if and when it is
slouken@8582
   533
        // clicked:
slouken@8582
   534
        auto cmd = ref new SettingsCommand(L"privacyPolicy", privacyPolicyLabel,
slouken@8582
   535
            ref new UICommandInvokedHandler([=](IUICommand ^) {
slouken@8582
   536
                Windows::System::Launcher::LaunchUriAsync(ref new Uri(privacyPolicyURL));
slouken@8582
   537
        }));
slouken@8582
   538
        args->Request->ApplicationCommands->Append(cmd);
slouken@8582
   539
    }
slouken@8582
   540
}
dludwig@9931
   541
#endif // if (WINAPI_FAMILY == WINAPI_FAMILY_APP) && (NTDDI_VERSION < NTDDI_WIN10)
slouken@8582
   542
slouken@8582
   543
void SDL_WinRTApp::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
slouken@8582
   544
{
slouken@8582
   545
#if LOG_WINDOW_EVENTS==1
dludwig@9924
   546
    SDL_Log("%s, size={%f,%f}, bounds={%f,%f,%f,%f}, current orientation=%d, native orientation=%d, auto rot. pref=%d, WINRT_GlobalSDLWindow?=%s\n",
slouken@8582
   547
        __FUNCTION__,
slouken@8582
   548
        args->Size.Width, args->Size.Height,
dludwig@9924
   549
        sender->Bounds.X, sender->Bounds.Y, sender->Bounds.Width, sender->Bounds.Height,
dludwig@8755
   550
        WINRT_DISPLAY_PROPERTY(CurrentOrientation),
dludwig@8755
   551
        WINRT_DISPLAY_PROPERTY(NativeOrientation),
dludwig@8755
   552
        WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
slouken@8582
   553
        (WINRT_GlobalSDLWindow ? "yes" : "no"));
slouken@8582
   554
#endif
slouken@8582
   555
slouken@8582
   556
    WINRT_ProcessWindowSizeChange();
slouken@8582
   557
}
slouken@8582
   558
slouken@8582
   559
void SDL_WinRTApp::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
slouken@8582
   560
{
slouken@8582
   561
#if LOG_WINDOW_EVENTS==1
dludwig@9924
   562
    SDL_Log("%s, visible?=%s, bounds={%f,%f,%f,%f}, WINRT_GlobalSDLWindow?=%s\n",
slouken@8582
   563
        __FUNCTION__,
slouken@8582
   564
        (args->Visible ? "yes" : "no"),
dludwig@9924
   565
        sender->Bounds.X, sender->Bounds.Y,
dludwig@9924
   566
        sender->Bounds.Width, sender->Bounds.Height,
slouken@8582
   567
        (WINRT_GlobalSDLWindow ? "yes" : "no"));
slouken@8582
   568
#endif
slouken@8582
   569
slouken@8582
   570
    m_windowVisible = args->Visible;
slouken@8582
   571
    if (WINRT_GlobalSDLWindow) {
slouken@8582
   572
        SDL_bool wasSDLWindowSurfaceValid = WINRT_GlobalSDLWindow->surface_valid;
dludwig@9924
   573
        Uint32 latestWindowFlags = WINRT_DetectWindowFlags(WINRT_GlobalSDLWindow);
slouken@8582
   574
        if (args->Visible) {
slouken@8582
   575
            SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_SHOWN, 0, 0);
dludwig@8678
   576
            SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
dludwig@9924
   577
            if (latestWindowFlags & SDL_WINDOW_MAXIMIZED) {
dludwig@9924
   578
                SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
dludwig@9924
   579
            } else {
dludwig@9924
   580
                SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_RESTORED, 0, 0);
dludwig@9924
   581
            }
slouken@8582
   582
        } else {
slouken@8582
   583
            SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_HIDDEN, 0, 0);
dludwig@8678
   584
            SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
dludwig@8678
   585
            SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@8582
   586
        }
slouken@8582
   587
slouken@8582
   588
        // HACK: Prevent SDL's window-hide handling code, which currently
slouken@8582
   589
        // triggers a fake window resize (possibly erronously), from
slouken@8582
   590
        // marking the SDL window's surface as invalid.
slouken@8582
   591
        //
slouken@8582
   592
        // A better solution to this probably involves figuring out if the
slouken@8582
   593
        // fake window resize can be prevented.
slouken@8582
   594
        WINRT_GlobalSDLWindow->surface_valid = wasSDLWindowSurfaceValid;
slouken@8582
   595
    }
slouken@8582
   596
}
slouken@8582
   597
dludwig@9924
   598
void SDL_WinRTApp::OnWindowActivated(CoreWindow^ sender, WindowActivatedEventArgs^ args)
dludwig@9924
   599
{
dludwig@9924
   600
#if LOG_WINDOW_EVENTS==1
dludwig@9924
   601
    SDL_Log("%s, WINRT_GlobalSDLWindow?=%s\n\n",
dludwig@9924
   602
        __FUNCTION__,
dludwig@9924
   603
        (WINRT_GlobalSDLWindow ? "yes" : "no"));
dludwig@9924
   604
#endif
dludwig@9924
   605
dludwig@9924
   606
    /* There's no property in Win 8.x to tell whether a window is active or
dludwig@9924
   607
       not.  [De]activation events are, however, sent to the app.  We'll just
dludwig@9924
   608
       record those, in case the CoreWindow gets wrapped by an SDL_Window at
dludwig@9924
   609
       some future time.
dludwig@9924
   610
    */
dludwig@9924
   611
    sender->CustomProperties->Insert("SDLHelperWindowActivationState", args->WindowActivationState);
dludwig@9924
   612
dludwig@9924
   613
    SDL_Window * window = WINRT_GlobalSDLWindow;
dludwig@9924
   614
    if (window) {
dludwig@9924
   615
        if (args->WindowActivationState != CoreWindowActivationState::Deactivated) {
dludwig@9924
   616
            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
dludwig@9924
   617
            if (SDL_GetKeyboardFocus() != window) {
dludwig@9924
   618
                SDL_SetKeyboardFocus(window);
dludwig@9924
   619
            }
dludwig@9924
   620
        
dludwig@9924
   621
            /* Send a mouse-motion event as appropriate.
dludwig@9924
   622
               This doesn't work when called from OnPointerEntered, at least
dludwig@9924
   623
               not in WinRT CoreWindow apps (as OnPointerEntered doesn't
dludwig@9924
   624
               appear to be called after window-reactivation, at least not
dludwig@9924
   625
               in Windows 10, Build 10586.3 (November 2015 update, non-beta).
dludwig@9924
   626
dludwig@9924
   627
               Don't do it on WinPhone 8.0 though, as CoreWindow's 'PointerPosition'
dludwig@9924
   628
               property isn't available.
dludwig@9924
   629
             */
dludwig@9924
   630
#if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION >= NTDDI_WINBLUE)
dludwig@9924
   631
            Point cursorPos = WINRT_TransformCursorPosition(window, sender->PointerPosition, TransformToSDLWindowSize);
dludwig@9924
   632
            SDL_SendMouseMotion(window, 0, 0, (int)cursorPos.X, (int)cursorPos.Y);
dludwig@9924
   633
#endif
dludwig@9924
   634
dludwig@9924
   635
            /* TODO, WinRT: see if the Win32 bugfix from https://hg.libsdl.org/SDL/rev/d278747da408 needs to be applied (on window activation) */
dludwig@9924
   636
            //WIN_CheckAsyncMouseRelease(data);
dludwig@9924
   637
dludwig@9924
   638
            /* TODO, WinRT: implement clipboard support, if possible */
dludwig@9924
   639
            ///*
dludwig@9924
   640
            // * FIXME: Update keyboard state
dludwig@9924
   641
            // */
dludwig@9924
   642
            //WIN_CheckClipboardUpdate(data->videodata);
dludwig@9991
   643
dludwig@9991
   644
            // HACK: Resetting the mouse-cursor here seems to fix
dludwig@9991
   645
            // https://bugzilla.libsdl.org/show_bug.cgi?id=3217, whereby a
dludwig@9991
   646
            // WinRT app's mouse cursor may switch to Windows' 'wait' cursor,
dludwig@9991
   647
            // after a user alt-tabs back into a full-screened SDL app.
dludwig@9991
   648
            // This bug does not appear to reproduce 100% of the time.
dludwig@9991
   649
            // It may be a bug in Windows itself (v.10.0.586.36, as tested,
dludwig@9991
   650
            // and the most-recent as of this writing).
dludwig@9991
   651
            SDL_SetCursor(NULL);
dludwig@9924
   652
        } else {
dludwig@9924
   653
            if (SDL_GetKeyboardFocus() == window) {
dludwig@9924
   654
                SDL_SetKeyboardFocus(NULL);
dludwig@9924
   655
            }
dludwig@9924
   656
        }
dludwig@9924
   657
    }
dludwig@9924
   658
}
dludwig@9924
   659
slouken@8582
   660
void SDL_WinRTApp::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
slouken@8582
   661
{
slouken@8582
   662
#if LOG_WINDOW_EVENTS==1
slouken@8582
   663
    SDL_Log("%s\n", __FUNCTION__);
slouken@8582
   664
#endif
slouken@8582
   665
    m_windowClosed = true;
slouken@8582
   666
}
slouken@8582
   667
dludwig@9924
   668
void SDL_WinRTApp::OnAppActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
slouken@8582
   669
{
slouken@8582
   670
    CoreWindow::GetForCurrentThread()->Activate();
slouken@8582
   671
}
slouken@8582
   672
slouken@8582
   673
void SDL_WinRTApp::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
slouken@8582
   674
{
slouken@8582
   675
    // Save app state asynchronously after requesting a deferral. Holding a deferral
slouken@8582
   676
    // indicates that the application is busy performing suspending operations. Be
slouken@8582
   677
    // aware that a deferral may not be held indefinitely. After about five seconds,
slouken@8582
   678
    // the app will be forced to exit.
dludwig@8665
   679
dludwig@8665
   680
    // ... but first, let the app know it's about to go to the background.
dludwig@8665
   681
    // The separation of events may be important, given that the deferral
dludwig@8665
   682
    // runs in a separate thread.  This'll make SDL_APP_WILLENTERBACKGROUND
dludwig@8665
   683
    // the only event among the two that runs in the main thread.  Given
dludwig@8665
   684
    // that a few WinRT operations can only be done from the main thread
dludwig@8665
   685
    // (things that access the WinRT CoreWindow are one example of this),
dludwig@8665
   686
    // this could be important.
dludwig@8665
   687
    SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
dludwig@8665
   688
slouken@8582
   689
    SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
slouken@8582
   690
    create_task([this, deferral]()
slouken@8582
   691
    {
dludwig@8666
   692
        // Send an app did-enter-background event immediately to observers.
slouken@8582
   693
        // CoreDispatcher::ProcessEvents, which is the backbone on which
slouken@8582
   694
        // SDL_WinRTApp::PumpEvents is built, will not return to its caller
slouken@8582
   695
        // once it sends out a suspend event.  Any events posted to SDL's
slouken@8582
   696
        // event queue won't get received until the WinRT app is resumed.
slouken@8582
   697
        // SDL_AddEventWatch() may be used to receive app-suspend events on
slouken@8582
   698
        // WinRT.
slouken@8582
   699
        SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
slouken@8582
   700
dludwig@8679
   701
        // Let the Direct3D 11 renderer prepare for the app to be backgrounded.
dludwig@8679
   702
        // This is necessary for Windows 8.1, possibly elsewhere in the future.
dludwig@8679
   703
        // More details at: http://msdn.microsoft.com/en-us/library/windows/apps/Hh994929.aspx
dludwig@8679
   704
#if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
dludwig@8679
   705
        if (WINRT_GlobalSDLWindow) {
dludwig@8679
   706
            SDL_Renderer * renderer = SDL_GetRenderer(WINRT_GlobalSDLWindow);
dludwig@8679
   707
            if (renderer && (SDL_strcmp(renderer->info.name, "direct3d11") == 0)) {
dludwig@8679
   708
                D3D11_Trim(renderer);
dludwig@8679
   709
            }
dludwig@8679
   710
        }
dludwig@8679
   711
#endif
dludwig@8679
   712
slouken@8582
   713
        deferral->Complete();
slouken@8582
   714
    });
slouken@8582
   715
}
slouken@8582
   716
slouken@8582
   717
void SDL_WinRTApp::OnResuming(Platform::Object^ sender, Platform::Object^ args)
slouken@8582
   718
{
dludwig@8666
   719
    // Restore any data or state that was unloaded on suspend. By default, data
dludwig@8666
   720
    // and state are persisted when resuming from suspend. Note that these events
dludwig@8666
   721
    // do not occur if the app was previously terminated.
slouken@8582
   722
    SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
slouken@8582
   723
    SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
slouken@8582
   724
}
slouken@8582
   725
slouken@8582
   726
void SDL_WinRTApp::OnExiting(Platform::Object^ sender, Platform::Object^ args)
slouken@8582
   727
{
slouken@8582
   728
    SDL_SendAppEvent(SDL_APP_TERMINATING);
slouken@8582
   729
}
slouken@8582
   730
slouken@8582
   731
static void
slouken@8582
   732
WINRT_LogPointerEvent(const char * header, Windows::UI::Core::PointerEventArgs ^ args, Windows::Foundation::Point transformedPoint)
slouken@8582
   733
{
slouken@8582
   734
    Windows::UI::Input::PointerPoint ^ pt = args->CurrentPoint;
slouken@8582
   735
    SDL_Log("%s: Position={%f,%f}, Transformed Pos={%f, %f}, MouseWheelDelta=%d, FrameId=%d, PointerId=%d, SDL button=%d\n",
slouken@8582
   736
        header,
slouken@8582
   737
        pt->Position.X, pt->Position.Y,
slouken@8582
   738
        transformedPoint.X, transformedPoint.Y,
slouken@8582
   739
        pt->Properties->MouseWheelDelta,
slouken@8582
   740
        pt->FrameId,
slouken@8582
   741
        pt->PointerId,
slouken@8582
   742
        WINRT_GetSDLButtonForPointerPoint(pt));
slouken@8582
   743
}
slouken@8582
   744
slouken@8582
   745
void SDL_WinRTApp::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
slouken@8582
   746
{
slouken@8582
   747
#if LOG_POINTER_EVENTS
slouken@8582
   748
    WINRT_LogPointerEvent("pointer pressed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
slouken@8582
   749
#endif
slouken@8582
   750
slouken@8582
   751
    WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
slouken@8582
   752
}
slouken@8582
   753
slouken@8582
   754
void SDL_WinRTApp::OnPointerMoved(CoreWindow^ sender, PointerEventArgs^ args)
slouken@8582
   755
{
slouken@8582
   756
#if LOG_POINTER_EVENTS
slouken@8582
   757
    WINRT_LogPointerEvent("pointer moved", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
slouken@8582
   758
#endif
slouken@8582
   759
slouken@8582
   760
    WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
slouken@8582
   761
}
slouken@8582
   762
slouken@8582
   763
void SDL_WinRTApp::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
slouken@8582
   764
{
slouken@8582
   765
#if LOG_POINTER_EVENTS
slouken@8582
   766
    WINRT_LogPointerEvent("pointer released", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
slouken@8582
   767
#endif
dludwig@9924
   768
    
dludwig@9924
   769
    WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
dludwig@9924
   770
}
slouken@8582
   771
dludwig@9924
   772
void SDL_WinRTApp::OnPointerEntered(CoreWindow^ sender, PointerEventArgs^ args)
dludwig@9924
   773
{
dludwig@9924
   774
#if LOG_POINTER_EVENTS
dludwig@9924
   775
    WINRT_LogPointerEvent("pointer entered", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
dludwig@9924
   776
#endif
dludwig@9924
   777
dludwig@9924
   778
    WINRT_ProcessPointerEnteredEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
dludwig@9924
   779
}
dludwig@9924
   780
dludwig@9924
   781
void SDL_WinRTApp::OnPointerExited(CoreWindow^ sender, PointerEventArgs^ args)
dludwig@9924
   782
{
dludwig@9924
   783
#if LOG_POINTER_EVENTS
dludwig@9924
   784
    WINRT_LogPointerEvent("pointer exited", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
dludwig@9924
   785
#endif
dludwig@9924
   786
dludwig@9924
   787
    WINRT_ProcessPointerExitedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
slouken@8582
   788
}
slouken@8582
   789
slouken@8582
   790
void SDL_WinRTApp::OnPointerWheelChanged(CoreWindow^ sender, PointerEventArgs^ args)
slouken@8582
   791
{
slouken@8582
   792
#if LOG_POINTER_EVENTS
slouken@8582
   793
    WINRT_LogPointerEvent("pointer wheel changed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
slouken@8582
   794
#endif
slouken@8582
   795
slouken@8582
   796
    WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
slouken@8582
   797
}
slouken@8582
   798
slouken@8582
   799
void SDL_WinRTApp::OnMouseMoved(MouseDevice^ mouseDevice, MouseEventArgs^ args)
slouken@8582
   800
{
slouken@8582
   801
    WINRT_ProcessMouseMovedEvent(WINRT_GlobalSDLWindow, args);
slouken@8582
   802
}
slouken@8582
   803
slouken@8582
   804
void SDL_WinRTApp::OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
slouken@8582
   805
{
slouken@8582
   806
    WINRT_ProcessKeyDownEvent(args);
slouken@8582
   807
}
slouken@8582
   808
slouken@8582
   809
void SDL_WinRTApp::OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
slouken@8582
   810
{
slouken@8582
   811
    WINRT_ProcessKeyUpEvent(args);
slouken@8582
   812
}
slouken@8582
   813
dludwig@9240
   814
void SDL_WinRTApp::OnCharacterReceived(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CharacterReceivedEventArgs^ args)
dludwig@9240
   815
{
dludwig@9240
   816
    WINRT_ProcessCharacterReceivedEvent(args);
dludwig@9240
   817
}
dludwig@9240
   818
dludwig@9957
   819
template <typename BackButtonEventArgs>
dludwig@9957
   820
static void WINRT_OnBackButtonPressed(BackButtonEventArgs ^ args)
slouken@8582
   821
{
slouken@8582
   822
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_AC_BACK);
slouken@8582
   823
    SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_AC_BACK);
slouken@8582
   824
slouken@10499
   825
    if (SDL_GetHintBoolean(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, SDL_FALSE)) {
slouken@10499
   826
        args->Handled = true;
slouken@8582
   827
    }
slouken@8582
   828
}
dludwig@9957
   829
dludwig@11494
   830
#if NTDDI_VERSION >= NTDDI_WIN10
dludwig@9957
   831
void SDL_WinRTApp::OnBackButtonPressed(Platform::Object^ sender, Windows::UI::Core::BackRequestedEventArgs^ args)
dludwig@9957
   832
dludwig@9957
   833
{
dludwig@9957
   834
    WINRT_OnBackButtonPressed(args);
dludwig@9957
   835
}
dludwig@9957
   836
#elif WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@9957
   837
void SDL_WinRTApp::OnBackButtonPressed(Platform::Object^ sender, Windows::Phone::UI::Input::BackPressedEventArgs^ args)
dludwig@9957
   838
dludwig@9957
   839
{
dludwig@9957
   840
    WINRT_OnBackButtonPressed(args);
dludwig@9957
   841
}
slouken@8582
   842
#endif
slouken@8582
   843
dludwig@10144
   844
#if NTDDI_VERSION >= NTDDI_WIN10
dludwig@10144
   845
void SDL_WinRTApp::OnGamepadAdded(Platform::Object ^sender, Windows::Gaming::Input::Gamepad ^gamepad)
dludwig@10144
   846
{
dludwig@10144
   847
    /* HACK ALERT: Nothing needs to be done here, as this method currently
dludwig@10144
   848
       only exists to allow something to be registered with Win10's
dludwig@10144
   849
       GamepadAdded event, an operation that seems to be necessary to get
dludwig@10144
   850
       Xinput-based detection to work on Xbox One.
dludwig@10144
   851
    */
dludwig@10144
   852
}
dludwig@10144
   853
#endif
slouken@10499
   854
slouken@10499
   855
/* vi: set ts=4 sw=4 expandtab: */