src/core/winrt/SDL_winrtapp_direct3d.cpp
author David Ludwig <dludwig@pobox.com>
Wed, 01 Jan 2014 16:05:37 -0500
changeset 8576 8dd593afe2d7
parent 8542 97dcef41e4a7
child 8577 462760a8b82e
permissions -rw-r--r--
WinRT: added a means to display a privacy policy link via the Settings charm

This change is only relevant for Windows 8, 8.1, and RT apps, and only for those that are network-enabled. Such apps must feature a link to a privacy policy, which must be displayed via the Windows Settings charm. This is needed to pass Windows Store app-certification.

Using SDL_SetHint, along with SDL_HINT_WINRT_PRIVACY_POLICY_URL and optionally SDL_HINT_WINRT_PRIVACY_POLICY_LABEL, will cause SDL/WinRT to create a link inside the Windows Settings charm, as invoked from within an SDL-based app.

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