src/core/winrt/SDL_winrtapp_direct3d.cpp
author David Ludwig <dludwig@pobox.com>
Sun, 26 Jan 2014 08:06:36 -0500
changeset 8577 462760a8b82e
parent 8576 8dd593afe2d7
child 8578 6517b6aca713
permissions -rw-r--r--
WinRT: simulate keyboard events on Windows Phone 8 back-button presses

Pressing the hardware back button on a Windows Phone 8 device will now cause SDL to emit a pair of key-down and key-up events, with the SDL scancode, SDL_SCANCODE_AC_BACK.

By default, if WinRT's native back-button-press events are not explicitly marked as 'handled', then Windows Phone will terminate the app. More details on Microsoft's reasoning behind this can be found on MSDN, at http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj247550(v=vs.105).aspx

To mark back-button-press events as 'handled', set SDL_HINT_WINRT_HANDLE_BACK_BUTTON to 1. Setting it to anything else will cause these events to not be marked as 'handled'.

Due to limitations in Windows Phone's APIs, SDL will emit a virtual key-up event immediately after the back button's key-down event is registered. Unfortunately, Windows Phone 8 only allows one to register for back-button-press events, and not back-button-release events.
dludwig@8443
     1

dludwig@8494
     2
/* Standard C++11 includes */
dludwig@8489
     3
#include <functional>
dludwig@8443
     4
#include <string>
dludwig@8443
     5
#include <sstream>
dludwig@8494
     6
using namespace std;
dludwig@8443
     7
dludwig@8494
     8
dludwig@8494
     9
/* Windows includes */
dludwig@8417
    10
#include "ppltasks.h"
dludwig@8494
    11
using namespace concurrency;
dludwig@8494
    12
using namespace Windows::ApplicationModel;
dludwig@8494
    13
using namespace Windows::ApplicationModel::Core;
dludwig@8494
    14
using namespace Windows::ApplicationModel::Activation;
dludwig@8494
    15
using namespace Windows::Devices::Input;
dludwig@8494
    16
using namespace Windows::Graphics::Display;
dludwig@8494
    17
using namespace Windows::Foundation;
dludwig@8494
    18
using namespace Windows::System;
dludwig@8494
    19
using namespace Windows::UI::Core;
dludwig@8494
    20
using namespace Windows::UI::Input;
dludwig@8322
    21
dludwig@8577
    22
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8577
    23
using namespace Windows::Phone::UI::Input;
dludwig@8577
    24
#endif
dludwig@8577
    25
dludwig@8494
    26
dludwig@8494
    27
/* SDL includes */
dludwig@8329
    28
extern "C" {
dludwig@8329
    29
#include "SDL_assert.h"
dludwig@8443
    30
#include "SDL_events.h"
dludwig@8443
    31
#include "SDL_hints.h"
dludwig@8443
    32
#include "SDL_log.h"
dludwig@8481
    33
#include "SDL_main.h"
dludwig@8329
    34
#include "SDL_stdinc.h"
dludwig@8443
    35
#include "SDL_render.h"
dludwig@8483
    36
#include "../../video/SDL_sysvideo.h"
dludwig@8478
    37
//#include "../../SDL_hints_c.h"
dludwig@8577
    38
#include "../../events/SDL_keyboard_c.h"
dludwig@8332
    39
#include "../../events/SDL_mouse_c.h"
dludwig@8383
    40
#include "../../events/SDL_windowevents_c.h"
dludwig@8434
    41
#include "../../render/SDL_sysrender.h"
dludwig@8576
    42
#include "../windows/SDL_windows.h"
dludwig@8329
    43
}
dludwig@8329
    44
dludwig@8489
    45
#include "../../video/winrt/SDL_winrtevents_c.h"
dludwig@8515
    46
#include "../../video/winrt/SDL_winrtvideo_cpp.h"
dludwig@8531
    47
#include "SDL_winrtapp_common.h"
dludwig@8522
    48
#include "SDL_winrtapp_direct3d.h"
dludwig@8435
    49
dludwig@8342
    50
dludwig@8468
    51
// Compile-time debugging options:
dludwig@8468
    52
// To enable, uncomment; to disable, comment them out.
dludwig@8468
    53
//#define LOG_POINTER_EVENTS 1
dludwig@8474
    54
//#define LOG_WINDOW_EVENTS 1
dludwig@8474
    55
//#define LOG_ORIENTATION_EVENTS 1
dludwig@8474
    56
dludwig@8468
    57
dludwig@8500
    58
// HACK, DLudwig: record a reference to the global, WinRT 'app'/view.
dludwig@8436
    59
// SDL/WinRT will use this throughout its code.
dludwig@8436
    60
//
dludwig@8436
    61
// TODO, WinRT: consider replacing SDL_WinRTGlobalApp with something
dludwig@8436
    62
// non-global, such as something created inside
dludwig@8436
    63
// SDL_InitSubSystem(SDL_INIT_VIDEO), or something inside
dludwig@8436
    64
// SDL_CreateWindow().
dludwig@8436
    65
SDL_WinRTApp ^ SDL_WinRTGlobalApp = nullptr;
dludwig@8436
    66
dludwig@8437
    67
ref class SDLApplicationSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource
dludwig@8437
    68
{
dludwig@8437
    69
public:
dludwig@8437
    70
    virtual Windows::ApplicationModel::Core::IFrameworkView^ CreateView();
dludwig@8437
    71
};
dludwig@8437
    72
dludwig@8437
    73
IFrameworkView^ SDLApplicationSource::CreateView()
dludwig@8437
    74
{
dludwig@8437
    75
    // TODO, WinRT: see if this function (CreateView) can ever get called
dludwig@8437
    76
    // more than once.  For now, just prevent it from ever assigning
dludwig@8437
    77
    // SDL_WinRTGlobalApp more than once.
dludwig@8437
    78
    SDL_assert(!SDL_WinRTGlobalApp);
dludwig@8437
    79
    SDL_WinRTApp ^ app = ref new SDL_WinRTApp();
dludwig@8437
    80
    if (!SDL_WinRTGlobalApp)
dludwig@8437
    81
    {
dludwig@8437
    82
        SDL_WinRTGlobalApp = app;
dludwig@8437
    83
    }
dludwig@8437
    84
    return app;
dludwig@8437
    85
}
dludwig@8437
    86
dludwig@8531
    87
int SDL_WinRTInitNonXAMLApp(int (*mainFunction)(int, char **))
dludwig@8437
    88
{
dludwig@8531
    89
    WINRT_SDLAppEntryPoint = mainFunction;
dludwig@8437
    90
    auto direct3DApplicationSource = ref new SDLApplicationSource();
dludwig@8437
    91
    CoreApplication::Run(direct3DApplicationSource);
dludwig@8437
    92
    return 0;
dludwig@8437
    93
}
dludwig@8437
    94
dludwig@8478
    95
static void WINRT_SetDisplayOrientationsPreference(void *userdata, const char *name, const char *oldValue, const char *newValue)
dludwig@8435
    96
{
dludwig@8435
    97
    SDL_assert(SDL_strcmp(name, SDL_HINT_ORIENTATIONS) == 0);
dludwig@8435
    98
dludwig@8435
    99
    // Start with no orientation flags, then add each in as they're parsed
dludwig@8435
   100
    // from newValue.
dludwig@8435
   101
    unsigned int orientationFlags = 0;
dludwig@8481
   102
    if (newValue) {
dludwig@8481
   103
        std::istringstream tokenizer(newValue);
dludwig@8481
   104
        while (!tokenizer.eof()) {
dludwig@8481
   105
            std::string orientationName;
dludwig@8481
   106
            std::getline(tokenizer, orientationName, ' ');
dludwig@8481
   107
            if (orientationName == "LandscapeLeft") {
dludwig@8481
   108
                orientationFlags |= (unsigned int) DisplayOrientations::LandscapeFlipped;
dludwig@8481
   109
            } else if (orientationName == "LandscapeRight") {
dludwig@8481
   110
                orientationFlags |= (unsigned int) DisplayOrientations::Landscape;
dludwig@8481
   111
            } else if (orientationName == "Portrait") {
dludwig@8481
   112
                orientationFlags |= (unsigned int) DisplayOrientations::Portrait;
dludwig@8481
   113
            } else if (orientationName == "PortraitUpsideDown") {
dludwig@8481
   114
                orientationFlags |= (unsigned int) DisplayOrientations::PortraitFlipped;
dludwig@8481
   115
            }
dludwig@8435
   116
        }
dludwig@8435
   117
    }
dludwig@8435
   118
dludwig@8435
   119
    // If no valid orientation flags were specified, use a reasonable set of defaults:
dludwig@8435
   120
    if (!orientationFlags) {
dludwig@8435
   121
        // TODO, WinRT: consider seeing if an app's default orientation flags can be found out via some API call(s).
dludwig@8435
   122
        orientationFlags = (unsigned int) ( \
dludwig@8435
   123
            DisplayOrientations::Landscape |
dludwig@8435
   124
            DisplayOrientations::LandscapeFlipped |
dludwig@8435
   125
            DisplayOrientations::Portrait |
dludwig@8435
   126
            DisplayOrientations::PortraitFlipped);
dludwig@8435
   127
    }
dludwig@8435
   128
dludwig@8435
   129
    // Set the orientation/rotation preferences.  Please note that this does
dludwig@8435
   130
    // not constitute a 100%-certain lock of a given set of possible
dludwig@8500
   131
    // orientations.  According to Microsoft's documentation on WinRT [1]
dludwig@8435
   132
    // when a device is not capable of being rotated, Windows may ignore
dludwig@8435
   133
    // the orientation preferences, and stick to what the device is capable of
dludwig@8435
   134
    // displaying.
dludwig@8435
   135
    //
dludwig@8435
   136
    // [1] Documentation on the 'InitialRotationPreference' setting for a
dludwig@8435
   137
    // Windows app's manifest file describes how some orientation/rotation
dludwig@8435
   138
    // preferences may be ignored.  See
dludwig@8435
   139
    // http://msdn.microsoft.com/en-us/library/windows/apps/hh700343.aspx
dludwig@8435
   140
    // for details.  Microsoft's "Display orientation sample" also gives an
dludwig@8435
   141
    // outline of how Windows treats device rotation
dludwig@8435
   142
    // (http://code.msdn.microsoft.com/Display-Orientation-Sample-19a58e93).
dludwig@8435
   143
    DisplayProperties::AutoRotationPreferences = (DisplayOrientations) orientationFlags;
dludwig@8435
   144
}
dludwig@8435
   145
dludwig@8510
   146
static void
dludwig@8510
   147
WINRT_ProcessWindowSizeChange()
dludwig@8510
   148
{
dludwig@8510
   149
    // Make the new window size be the one true fullscreen mode.
dludwig@8510
   150
    // This change was initially done, in part, to allow the Direct3D 11.1
dludwig@8510
   151
    // renderer to receive window-resize events as a device rotates.
dludwig@8510
   152
    // Before, rotating a device from landscape, to portrait, and then
dludwig@8510
   153
    // back to landscape would cause the Direct3D 11.1 swap buffer to
dludwig@8510
   154
    // not get resized appropriately.  SDL would, on the rotation from
dludwig@8510
   155
    // landscape to portrait, re-resize the SDL window to it's initial
dludwig@8510
   156
    // size (landscape).  On the subsequent rotation, SDL would drop the
dludwig@8510
   157
    // window-resize event as it appeared the SDL window didn't change
dludwig@8510
   158
    // size, and the Direct3D 11.1 renderer wouldn't resize its swap
dludwig@8510
   159
    // chain.
dludwig@8510
   160
    SDL_DisplayMode resizedDisplayMode = WINRT_CalcDisplayModeUsingNativeWindow();
dludwig@8510
   161
    if (resizedDisplayMode.w == 0 || resizedDisplayMode.h == 0) {
dludwig@8510
   162
        return;
dludwig@8510
   163
    }
dludwig@8510
   164
dludwig@8510
   165
    SDL_DisplayMode oldDisplayMode;
dludwig@8510
   166
    SDL_zero(oldDisplayMode);
dludwig@8510
   167
    if (WINRT_GlobalSDLVideoDevice) {
dludwig@8510
   168
        oldDisplayMode = WINRT_GlobalSDLVideoDevice->displays[0].desktop_mode;
dludwig@8510
   169
        WINRT_GlobalSDLVideoDevice->displays[0].current_mode = resizedDisplayMode;
dludwig@8510
   170
        WINRT_GlobalSDLVideoDevice->displays[0].desktop_mode = resizedDisplayMode;
dludwig@8510
   171
        WINRT_GlobalSDLVideoDevice->displays[0].display_modes[0] = resizedDisplayMode;
dludwig@8510
   172
    }
dludwig@8510
   173
dludwig@8510
   174
    if (WINRT_GlobalSDLWindow) {
dludwig@8510
   175
        // Send a window-resize event to the rest of SDL, and to apps:
dludwig@8510
   176
        SDL_SendWindowEvent(
dludwig@8510
   177
            WINRT_GlobalSDLWindow,
dludwig@8510
   178
            SDL_WINDOWEVENT_RESIZED,
dludwig@8510
   179
            resizedDisplayMode.w,
dludwig@8510
   180
            resizedDisplayMode.h);
dludwig@8510
   181
dludwig@8510
   182
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8510
   183
        // HACK: On Windows Phone, make sure that orientation changes from
dludwig@8510
   184
        // Landscape to LandscapeFlipped, Portrait to PortraitFlipped,
dludwig@8510
   185
        // or vice-versa on either of those two, lead to the Direct3D renderer
dludwig@8510
   186
        // getting updated.
dludwig@8510
   187
        const DisplayOrientations oldOrientation = (DisplayOrientations) (unsigned int) oldDisplayMode.driverdata;
dludwig@8510
   188
        const DisplayOrientations newOrientation = (DisplayOrientations) (unsigned int) resizedDisplayMode.driverdata;
dludwig@8510
   189
dludwig@8510
   190
        if ((oldOrientation == DisplayOrientations::Landscape && newOrientation == DisplayOrientations::LandscapeFlipped) ||
dludwig@8510
   191
            (oldOrientation == DisplayOrientations::LandscapeFlipped && newOrientation == DisplayOrientations::Landscape) ||
dludwig@8510
   192
            (oldOrientation == DisplayOrientations::Portrait && newOrientation == DisplayOrientations::PortraitFlipped) ||
dludwig@8510
   193
            (oldOrientation == DisplayOrientations::PortraitFlipped && newOrientation == DisplayOrientations::Portrait))
dludwig@8510
   194
        {
dludwig@8510
   195
            // One of the reasons this event is getting sent out is because SDL
dludwig@8510
   196
            // will ignore requests to send out SDL_WINDOWEVENT_RESIZED events
dludwig@8510
   197
            // if and when the event size doesn't change (and the Direct3D 11.1
dludwig@8510
   198
            // renderer doesn't get the memo).
dludwig@8510
   199
            //
dludwig@8510
   200
            // Make sure that the display/window size really didn't change.  If
dludwig@8510
   201
            // it did, then a SDL_WINDOWEVENT_SIZE_CHANGED event got sent, and
dludwig@8510
   202
            // the Direct3D 11.1 renderer picked it up, presumably.
dludwig@8510
   203
            if (oldDisplayMode.w == resizedDisplayMode.w &&
dludwig@8510
   204
                oldDisplayMode.h == resizedDisplayMode.h)
dludwig@8510
   205
            {
dludwig@8510
   206
                SDL_SendWindowEvent(
dludwig@8510
   207
                    WINRT_GlobalSDLWindow,
dludwig@8510
   208
                    SDL_WINDOWEVENT_SIZE_CHANGED,
dludwig@8510
   209
                    resizedDisplayMode.w,
dludwig@8510
   210
                    resizedDisplayMode.h);
dludwig@8510
   211
            }
dludwig@8510
   212
        }
dludwig@8510
   213
#endif
dludwig@8510
   214
    }
dludwig@8510
   215
}
dludwig@8510
   216
dludwig@8322
   217
SDL_WinRTApp::SDL_WinRTApp() :
dludwig@8384
   218
    m_windowClosed(false),
dludwig@8498
   219
    m_windowVisible(true)
dludwig@8322
   220
{
dludwig@8322
   221
}
dludwig@8322
   222
dludwig@8322
   223
void SDL_WinRTApp::Initialize(CoreApplicationView^ applicationView)
dludwig@8322
   224
{
dludwig@8384
   225
    applicationView->Activated +=
dludwig@8322
   226
        ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &SDL_WinRTApp::OnActivated);
dludwig@8322
   227
dludwig@8384
   228
    CoreApplication::Suspending +=
dludwig@8322
   229
        ref new EventHandler<SuspendingEventArgs^>(this, &SDL_WinRTApp::OnSuspending);
dludwig@8322
   230
dludwig@8384
   231
    CoreApplication::Resuming +=
dludwig@8322
   232
        ref new EventHandler<Platform::Object^>(this, &SDL_WinRTApp::OnResuming);
dludwig@8435
   233
dludwig@8474
   234
    DisplayProperties::OrientationChanged +=
dludwig@8474
   235
        ref new DisplayPropertiesEventHandler(this, &SDL_WinRTApp::OnOrientationChanged);
dludwig@8474
   236
dludwig@8435
   237
    // Register the hint, SDL_HINT_ORIENTATIONS, with SDL.  This needs to be
dludwig@8435
   238
    // done before the hint's callback is registered (as of Feb 22, 2013),
dludwig@8435
   239
    // otherwise the hint callback won't get registered.
dludwig@8435
   240
    //
dludwig@8504
   241
    // 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@8478
   242
    //SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight Portrait PortraitUpsideDown");   // DavidL: this is no longer needed (for SDL_AddHintCallback)
dludwig@8478
   243
    SDL_AddHintCallback(SDL_HINT_ORIENTATIONS, WINRT_SetDisplayOrientationsPreference, NULL);
dludwig@8322
   244
}
dludwig@8322
   245
dludwig@8474
   246
void SDL_WinRTApp::OnOrientationChanged(Object^ sender)
dludwig@8474
   247
{
dludwig@8474
   248
#if LOG_ORIENTATION_EVENTS==1
dludwig@8474
   249
    CoreWindow^ window = CoreWindow::GetForCurrentThread();
dludwig@8474
   250
    if (window) {
dludwig@8474
   251
        SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, CoreWindow Size={%f,%f}\n",
dludwig@8474
   252
            __FUNCTION__,
dludwig@8474
   253
            (int)DisplayProperties::CurrentOrientation,
dludwig@8474
   254
            (int)DisplayProperties::NativeOrientation,
dludwig@8474
   255
            (int)DisplayProperties::AutoRotationPreferences,
dludwig@8474
   256
            window->Bounds.Width,
dludwig@8474
   257
            window->Bounds.Height);
dludwig@8474
   258
    } else {
dludwig@8474
   259
        SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d\n",
dludwig@8474
   260
            __FUNCTION__,
dludwig@8474
   261
            (int)DisplayProperties::CurrentOrientation,
dludwig@8474
   262
            (int)DisplayProperties::NativeOrientation,
dludwig@8474
   263
            (int)DisplayProperties::AutoRotationPreferences);
dludwig@8474
   264
    }
dludwig@8474
   265
#endif
dludwig@8510
   266
dludwig@8510
   267
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8510
   268
    // On Windows Phone, treat an orientation change as a change in window size.
dludwig@8510
   269
    // The native window's size doesn't seem to change, however SDL will simulate
dludwig@8510
   270
    // a window size change.
dludwig@8510
   271
    WINRT_ProcessWindowSizeChange();
dludwig@8510
   272
#endif
dludwig@8474
   273
}
dludwig@8474
   274
dludwig@8322
   275
void SDL_WinRTApp::SetWindow(CoreWindow^ window)
dludwig@8322
   276
{
dludwig@8474
   277
#if LOG_WINDOW_EVENTS==1
dludwig@8474
   278
    SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, window Size={%f,%f}\n",
dludwig@8474
   279
        __FUNCTION__,
dludwig@8474
   280
        (int)DisplayProperties::CurrentOrientation,
dludwig@8474
   281
        (int)DisplayProperties::NativeOrientation,
dludwig@8474
   282
        (int)DisplayProperties::AutoRotationPreferences,
dludwig@8474
   283
        window->Bounds.Width,
dludwig@8474
   284
        window->Bounds.Height);
dludwig@8474
   285
#endif
dludwig@8474
   286
dludwig@8384
   287
    window->SizeChanged += 
dludwig@8322
   288
        ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &SDL_WinRTApp::OnWindowSizeChanged);
dludwig@8322
   289
dludwig@8384
   290
    window->VisibilityChanged +=
dludwig@8384
   291
        ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &SDL_WinRTApp::OnVisibilityChanged);
dludwig@8322
   292
dludwig@8384
   293
    window->Closed += 
dludwig@8322
   294
        ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &SDL_WinRTApp::OnWindowClosed);
dludwig@8322
   295
dludwig@8399
   296
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
dludwig@8384
   297
    window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
dludwig@8399
   298
#endif
dludwig@8322
   299
dludwig@8384
   300
    window->PointerPressed +=
dludwig@8384
   301
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerPressed);
dludwig@8322
   302
dludwig@8521
   303
    window->PointerMoved +=
dludwig@8521
   304
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerMoved);
dludwig@8521
   305
dludwig@8332
   306
    window->PointerReleased +=
dludwig@8384
   307
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerReleased);
dludwig@8332
   308
dludwig@8422
   309
    window->PointerWheelChanged +=
dludwig@8422
   310
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerWheelChanged);
dludwig@8422
   311
dludwig@8399
   312
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
dludwig@8378
   313
    // Retrieves relative-only mouse movements:
dludwig@8378
   314
    Windows::Devices::Input::MouseDevice::GetForCurrentView()->MouseMoved +=
dludwig@8378
   315
        ref new TypedEventHandler<MouseDevice^, MouseEventArgs^>(this, &SDL_WinRTApp::OnMouseMoved);
dludwig@8399
   316
#endif
dludwig@8378
   317
dludwig@8336
   318
    window->KeyDown +=
dludwig@8384
   319
        ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyDown);
dludwig@8336
   320
dludwig@8384
   321
    window->KeyUp +=
dludwig@8384
   322
        ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyUp);
dludwig@8576
   323
dludwig@8577
   324
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8577
   325
    HardwareButtons::BackPressed +=
dludwig@8577
   326
        ref new EventHandler<BackPressedEventArgs^>(this, &SDL_WinRTApp::OnBackButtonPressed);
dludwig@8577
   327
#endif
dludwig@8577
   328
dludwig@8576
   329
#if WINAPI_FAMILY == WINAPI_FAMILY_APP  // for Windows 8/8.1/RT apps... (and not Phone apps)
dludwig@8576
   330
    // Make sure we know when a user has opened the app's settings pane.
dludwig@8576
   331
    // This is needed in order to display a privacy policy, which needs
dludwig@8576
   332
    // to be done for network-enabled apps, as per Windows Store requirements.
dludwig@8576
   333
    using namespace Windows::UI::ApplicationSettings;
dludwig@8576
   334
    SettingsPane::GetForCurrentView()->CommandsRequested +=
dludwig@8576
   335
        ref new TypedEventHandler<SettingsPane^, SettingsPaneCommandsRequestedEventArgs^>
dludwig@8576
   336
            (this, &SDL_WinRTApp::OnSettingsPaneCommandsRequested);
dludwig@8576
   337
#endif
dludwig@8322
   338
}
dludwig@8322
   339
dludwig@8322
   340
void SDL_WinRTApp::Load(Platform::String^ entryPoint)
dludwig@8322
   341
{
dludwig@8322
   342
}
dludwig@8322
   343
dludwig@8322
   344
void SDL_WinRTApp::Run()
dludwig@8322
   345
{
dludwig@8481
   346
    SDL_SetMainReady();
dludwig@8531
   347
    if (WINRT_SDLAppEntryPoint)
dludwig@8324
   348
    {
dludwig@8324
   349
        // TODO, WinRT: pass the C-style main() a reasonably realistic
dludwig@8324
   350
        // representation of command line arguments.
dludwig@8324
   351
        int argc = 0;
dludwig@8324
   352
        char **argv = NULL;
dludwig@8531
   353
        WINRT_SDLAppEntryPoint(argc, argv);
dludwig@8324
   354
    }
dludwig@8331
   355
}
dludwig@8324
   356
dludwig@8331
   357
void SDL_WinRTApp::PumpEvents()
dludwig@8331
   358
{
dludwig@8384
   359
    if (!m_windowClosed)
dludwig@8384
   360
    {
dludwig@8384
   361
        if (m_windowVisible)
dludwig@8384
   362
        {
dludwig@8384
   363
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
dludwig@8384
   364
        }
dludwig@8384
   365
        else
dludwig@8384
   366
        {
dludwig@8384
   367
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
dludwig@8384
   368
        }
dludwig@8384
   369
    }
dludwig@8322
   370
}
dludwig@8322
   371
dludwig@8322
   372
void SDL_WinRTApp::Uninitialize()
dludwig@8322
   373
{
dludwig@8322
   374
}
dludwig@8322
   375
dludwig@8576
   376
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8576
   377
void SDL_WinRTApp::OnSettingsPaneCommandsRequested(
dludwig@8576
   378
    Windows::UI::ApplicationSettings::SettingsPane ^p,
dludwig@8576
   379
    Windows::UI::ApplicationSettings::SettingsPaneCommandsRequestedEventArgs ^args)
dludwig@8576
   380
{
dludwig@8576
   381
    using namespace Platform;
dludwig@8576
   382
    using namespace Windows::UI::ApplicationSettings;
dludwig@8576
   383
    using namespace Windows::UI::Popups;
dludwig@8576
   384
dludwig@8576
   385
    String ^privacyPolicyURL = nullptr;     // a URL to an app's Privacy Policy
dludwig@8576
   386
    String ^privacyPolicyLabel = nullptr;   // label/link text
dludwig@8576
   387
    const char *tmpHintValue = NULL;        // SDL_GetHint-retrieved value, used immediately
dludwig@8576
   388
    wchar_t *tmpStr = NULL;                 // used for UTF8 to UCS2 conversion
dludwig@8576
   389
dludwig@8576
   390
    // Setup a 'Privacy Policy' link, if one is available (via SDL_GetHint):
dludwig@8576
   391
    tmpHintValue = SDL_GetHint(SDL_HINT_WINRT_PRIVACY_POLICY_URL);
dludwig@8576
   392
    if (tmpHintValue && tmpHintValue[0] != '\0') {
dludwig@8576
   393
        // Convert the privacy policy's URL to UCS2:
dludwig@8576
   394
        tmpStr = WIN_UTF8ToString(tmpHintValue);
dludwig@8576
   395
        privacyPolicyURL = ref new String(tmpStr);
dludwig@8576
   396
        SDL_free(tmpStr);
dludwig@8576
   397
dludwig@8576
   398
        // Optionally retrieve custom label-text for the link.  If this isn't
dludwig@8576
   399
        // available, a default value will be used instead.
dludwig@8576
   400
        tmpHintValue = SDL_GetHint(SDL_HINT_WINRT_PRIVACY_POLICY_LABEL);
dludwig@8576
   401
        if (tmpHintValue && tmpHintValue[0] != '\0') {
dludwig@8576
   402
            tmpStr = WIN_UTF8ToString(tmpHintValue);
dludwig@8576
   403
            privacyPolicyLabel = ref new String(tmpStr);
dludwig@8576
   404
            SDL_free(tmpStr);
dludwig@8576
   405
        } else {
dludwig@8576
   406
            privacyPolicyLabel = ref new String(L"Privacy Policy");
dludwig@8576
   407
        }
dludwig@8576
   408
dludwig@8576
   409
        // Register the link, along with a handler to be called if and when it is
dludwig@8576
   410
        // clicked:
dludwig@8576
   411
        auto cmd = ref new SettingsCommand(L"privacyPolicy", privacyPolicyLabel,
dludwig@8576
   412
            ref new UICommandInvokedHandler([=](IUICommand ^) {
dludwig@8576
   413
                Windows::System::Launcher::LaunchUriAsync(ref new Uri(privacyPolicyURL));
dludwig@8576
   414
        }));
dludwig@8576
   415
        args->Request->ApplicationCommands->Append(cmd);
dludwig@8576
   416
    }
dludwig@8576
   417
}
dludwig@8576
   418
#endif // if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8576
   419
dludwig@8322
   420
void SDL_WinRTApp::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
dludwig@8322
   421
{
dludwig@8474
   422
#if LOG_WINDOW_EVENTS==1
dludwig@8497
   423
    SDL_Log("%s, size={%f,%f}, current orientation=%d, native orientation=%d, auto rot. pref=%d, WINRT_GlobalSDLWindow?=%s\n",
dludwig@8474
   424
        __FUNCTION__,
dludwig@8474
   425
        args->Size.Width, args->Size.Height,
dludwig@8474
   426
        (int)DisplayProperties::CurrentOrientation,
dludwig@8474
   427
        (int)DisplayProperties::NativeOrientation,
dludwig@8474
   428
        (int)DisplayProperties::AutoRotationPreferences,
dludwig@8497
   429
        (WINRT_GlobalSDLWindow ? "yes" : "no"));
dludwig@8433
   430
#endif
dludwig@8433
   431
dludwig@8510
   432
    WINRT_ProcessWindowSizeChange();
dludwig@8322
   433
}
dludwig@8322
   434
dludwig@8322
   435
void SDL_WinRTApp::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
dludwig@8322
   436
{
dludwig@8474
   437
#if LOG_WINDOW_EVENTS==1
dludwig@8497
   438
    SDL_Log("%s, visible?=%s, WINRT_GlobalSDLWindow?=%s\n",
dludwig@8474
   439
        __FUNCTION__,
dludwig@8474
   440
        (args->Visible ? "yes" : "no"),
dludwig@8497
   441
        (WINRT_GlobalSDLWindow ? "yes" : "no"));
dludwig@8474
   442
#endif
dludwig@8474
   443
dludwig@8384
   444
    m_windowVisible = args->Visible;
dludwig@8497
   445
    if (WINRT_GlobalSDLWindow) {
dludwig@8497
   446
        SDL_bool wasSDLWindowSurfaceValid = WINRT_GlobalSDLWindow->surface_valid;
dludwig@8390
   447
dludwig@8385
   448
        if (args->Visible) {
dludwig@8497
   449
            SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_SHOWN, 0, 0);
dludwig@8385
   450
        } else {
dludwig@8497
   451
            SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_HIDDEN, 0, 0);
dludwig@8385
   452
        }
dludwig@8390
   453
dludwig@8390
   454
        // HACK: Prevent SDL's window-hide handling code, which currently
dludwig@8390
   455
        // triggers a fake window resize (possibly erronously), from
dludwig@8390
   456
        // marking the SDL window's surface as invalid.
dludwig@8390
   457
        //
dludwig@8390
   458
        // A better solution to this probably involves figuring out if the
dludwig@8390
   459
        // fake window resize can be prevented.
dludwig@8497
   460
        WINRT_GlobalSDLWindow->surface_valid = wasSDLWindowSurfaceValid;
dludwig@8385
   461
    }
dludwig@8322
   462
}
dludwig@8322
   463
dludwig@8322
   464
void SDL_WinRTApp::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
dludwig@8322
   465
{
dludwig@8474
   466
#if LOG_WINDOW_EVENTS==1
dludwig@8474
   467
    SDL_Log("%s\n", __FUNCTION__);
dludwig@8474
   468
#endif
dludwig@8384
   469
    m_windowClosed = true;
dludwig@8322
   470
}
dludwig@8322
   471
dludwig@8322
   472
void SDL_WinRTApp::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
dludwig@8322
   473
{
dludwig@8384
   474
    CoreWindow::GetForCurrentThread()->Activate();
dludwig@8322
   475
}
dludwig@8322
   476
dludwig@8383
   477
static int SDLCALL RemoveAppSuspendAndResumeEvents(void * userdata, SDL_Event * event)
dludwig@8383
   478
{
dludwig@8383
   479
    if (event->type == SDL_WINDOWEVENT)
dludwig@8383
   480
    {
dludwig@8383
   481
        switch (event->window.event)
dludwig@8383
   482
        {
dludwig@8383
   483
            case SDL_WINDOWEVENT_MINIMIZED:
dludwig@8383
   484
            case SDL_WINDOWEVENT_RESTORED:
dludwig@8383
   485
                // Return 0 to indicate that the event should be removed from the
dludwig@8383
   486
                // event queue:
dludwig@8383
   487
                return 0;
dludwig@8383
   488
            default:
dludwig@8383
   489
                break;
dludwig@8383
   490
        }
dludwig@8383
   491
    }
dludwig@8383
   492
dludwig@8383
   493
    // Return 1 to indicate that the event should stay in the event queue:
dludwig@8383
   494
    return 1;
dludwig@8383
   495
}
dludwig@8383
   496
dludwig@8322
   497
void SDL_WinRTApp::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
dludwig@8322
   498
{
dludwig@8384
   499
    // Save app state asynchronously after requesting a deferral. Holding a deferral
dludwig@8384
   500
    // indicates that the application is busy performing suspending operations. Be
dludwig@8384
   501
    // aware that a deferral may not be held indefinitely. After about five seconds,
dludwig@8384
   502
    // the app will be forced to exit.
dludwig@8384
   503
    SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
dludwig@8384
   504
    create_task([this, deferral]()
dludwig@8384
   505
    {
dludwig@8383
   506
        // Send a window-minimized event immediately to observers.
dludwig@8383
   507
        // CoreDispatcher::ProcessEvents, which is the backbone on which
dludwig@8383
   508
        // SDL_WinRTApp::PumpEvents is built, will not return to its caller
dludwig@8383
   509
        // once it sends out a suspend event.  Any events posted to SDL's
dludwig@8383
   510
        // event queue won't get received until the WinRT app is resumed.
dludwig@8383
   511
        // SDL_AddEventWatch() may be used to receive app-suspend events on
dludwig@8383
   512
        // WinRT.
dludwig@8383
   513
        //
dludwig@8383
   514
        // In order to prevent app-suspend events from being received twice:
dludwig@8383
   515
        // first via a callback passed to SDL_AddEventWatch, and second via
dludwig@8383
   516
        // SDL's event queue, the event will be sent to SDL, then immediately
dludwig@8383
   517
        // removed from the queue.
dludwig@8497
   518
        if (WINRT_GlobalSDLWindow)
dludwig@8383
   519
        {
dludwig@8497
   520
            SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_MINIMIZED, 0, 0);   // TODO: see if SDL_WINDOWEVENT_SIZE_CHANGED should be getting triggered here (it is, currently)
dludwig@8383
   521
            SDL_FilterEvents(RemoveAppSuspendAndResumeEvents, 0);
dludwig@8383
   522
        }
dludwig@8384
   523
        deferral->Complete();
dludwig@8384
   524
    });
dludwig@8322
   525
}
dludwig@8383
   526
dludwig@8322
   527
void SDL_WinRTApp::OnResuming(Platform::Object^ sender, Platform::Object^ args)
dludwig@8322
   528
{
dludwig@8384
   529
    // Restore any data or state that was unloaded on suspend. By default, data
dludwig@8384
   530
    // and state are persisted when resuming from suspend. Note that this event
dludwig@8384
   531
    // does not occur if the app was previously terminated.
dludwig@8497
   532
    if (WINRT_GlobalSDLWindow)
dludwig@8383
   533
    {
dludwig@8497
   534
        SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_RESTORED, 0, 0);    // TODO: see if SDL_WINDOWEVENT_SIZE_CHANGED should be getting triggered here (it is, currently)
dludwig@8383
   535
dludwig@8383
   536
        // Remove the app-resume event from the queue, as is done with the
dludwig@8383
   537
        // app-suspend event.
dludwig@8383
   538
        //
dludwig@8383
   539
        // TODO, WinRT: consider posting this event to the queue even though
dludwig@8383
   540
        // its counterpart, the app-suspend event, effectively has to be
dludwig@8383
   541
        // processed immediately.
dludwig@8383
   542
        SDL_FilterEvents(RemoveAppSuspendAndResumeEvents, 0);
dludwig@8383
   543
    }
dludwig@8322
   544
}
dludwig@8521
   545
dludwig@8521
   546
static void
dludwig@8521
   547
WINRT_LogPointerEvent(const char * header, Windows::UI::Core::PointerEventArgs ^ args, Windows::Foundation::Point transformedPoint)
dludwig@8521
   548
{
dludwig@8521
   549
    Windows::UI::Input::PointerPoint ^ pt = args->CurrentPoint;
dludwig@8521
   550
    SDL_Log("%s: Position={%f,%f}, Transformed Pos={%f, %f}, MouseWheelDelta=%d, FrameId=%d, PointerId=%d, SDL button=%d\n",
dludwig@8521
   551
        header,
dludwig@8521
   552
        pt->Position.X, pt->Position.Y,
dludwig@8521
   553
        transformedPoint.X, transformedPoint.Y,
dludwig@8521
   554
        pt->Properties->MouseWheelDelta,
dludwig@8521
   555
        pt->FrameId,
dludwig@8521
   556
        pt->PointerId,
dludwig@8521
   557
        WINRT_GetSDLButtonForPointerPoint(pt));
dludwig@8521
   558
}
dludwig@8521
   559
dludwig@8521
   560
void SDL_WinRTApp::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
dludwig@8521
   561
{
dludwig@8521
   562
#if LOG_POINTER_EVENTS
dludwig@8542
   563
    WINRT_LogPointerEvent("pointer pressed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
dludwig@8521
   564
#endif
dludwig@8521
   565
dludwig@8521
   566
    WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
dludwig@8521
   567
}
dludwig@8521
   568
dludwig@8521
   569
void SDL_WinRTApp::OnPointerMoved(CoreWindow^ sender, PointerEventArgs^ args)
dludwig@8521
   570
{
dludwig@8521
   571
#if LOG_POINTER_EVENTS
dludwig@8542
   572
    WINRT_LogPointerEvent("pointer moved", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
dludwig@8521
   573
#endif
dludwig@8521
   574
dludwig@8521
   575
    WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
dludwig@8521
   576
}
dludwig@8521
   577
dludwig@8521
   578
void SDL_WinRTApp::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
dludwig@8521
   579
{
dludwig@8521
   580
#if LOG_POINTER_EVENTS
dludwig@8542
   581
    WINRT_LogPointerEvent("pointer released", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
dludwig@8521
   582
#endif
dludwig@8521
   583
dludwig@8521
   584
    WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
dludwig@8521
   585
}
dludwig@8521
   586
dludwig@8521
   587
void SDL_WinRTApp::OnPointerWheelChanged(CoreWindow^ sender, PointerEventArgs^ args)
dludwig@8521
   588
{
dludwig@8521
   589
#if LOG_POINTER_EVENTS
dludwig@8542
   590
    WINRT_LogPointerEvent("pointer wheel changed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
dludwig@8521
   591
#endif
dludwig@8521
   592
dludwig@8521
   593
    WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
dludwig@8521
   594
}
dludwig@8521
   595
dludwig@8521
   596
void SDL_WinRTApp::OnMouseMoved(MouseDevice^ mouseDevice, MouseEventArgs^ args)
dludwig@8521
   597
{
dludwig@8521
   598
    WINRT_ProcessMouseMovedEvent(WINRT_GlobalSDLWindow, args);
dludwig@8521
   599
}
dludwig@8521
   600
dludwig@8521
   601
void SDL_WinRTApp::OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
dludwig@8521
   602
{
dludwig@8521
   603
    WINRT_ProcessKeyDownEvent(args);
dludwig@8521
   604
}
dludwig@8521
   605
dludwig@8521
   606
void SDL_WinRTApp::OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
dludwig@8521
   607
{
dludwig@8521
   608
    WINRT_ProcessKeyUpEvent(args);
dludwig@8521
   609
}
dludwig@8577
   610
dludwig@8577
   611
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8577
   612
void SDL_WinRTApp::OnBackButtonPressed(Platform::Object^ sender, Windows::Phone::UI::Input::BackPressedEventArgs^ args)
dludwig@8577
   613
{
dludwig@8577
   614
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_AC_BACK);
dludwig@8577
   615
    SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_AC_BACK);
dludwig@8577
   616
dludwig@8577
   617
    const char *hint = SDL_GetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON);
dludwig@8577
   618
    if (hint) {
dludwig@8577
   619
        if (*hint == '1') {
dludwig@8577
   620
            args->Handled = true;
dludwig@8577
   621
        }
dludwig@8577
   622
    }
dludwig@8577
   623
}
dludwig@8577
   624
#endif
dludwig@8577
   625