src/core/winrt/SDL_winrtapp_direct3d.cpp
author David Ludwig <dludwig@pobox.com>
Sat, 01 Mar 2014 16:08:16 -0500
changeset 8578 6517b6aca713
parent 8577 462760a8b82e
child 8579 ee7126c459d0
permissions -rw-r--r--
WinRT: fixed a crash in SDL_Quit

SDL was expected that each SDL_DisplayMode had a driverdata field that was SDL_malloc'ed, and was calling SDL_free on them. This change moves WinRT's driverdata content into a SDL_malloc'ed field.
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@8578
   160
    SDL_DisplayMode resizedDisplayMode;
dludwig@8578
   161
    if (WINRT_CalcDisplayModeUsingNativeWindow(&resizedDisplayMode) != 0) {
dludwig@8578
   162
        return;
dludwig@8578
   163
    }
dludwig@8578
   164
    
dludwig@8510
   165
    if (resizedDisplayMode.w == 0 || resizedDisplayMode.h == 0) {
dludwig@8578
   166
        if (resizedDisplayMode.driverdata) {
dludwig@8578
   167
            SDL_free(resizedDisplayMode.driverdata);
dludwig@8578
   168
        }
dludwig@8510
   169
        return;
dludwig@8510
   170
    }
dludwig@8510
   171
dludwig@8510
   172
    SDL_DisplayMode oldDisplayMode;
dludwig@8510
   173
    SDL_zero(oldDisplayMode);
dludwig@8510
   174
    if (WINRT_GlobalSDLVideoDevice) {
dludwig@8510
   175
        oldDisplayMode = WINRT_GlobalSDLVideoDevice->displays[0].desktop_mode;
dludwig@8578
   176
        if (WINRT_DuplicateDisplayMode(&(WINRT_GlobalSDLVideoDevice->displays[0].desktop_mode), &resizedDisplayMode) != 0) {
dludwig@8578
   177
            SDL_free(resizedDisplayMode.driverdata);
dludwig@8578
   178
            return;
dludwig@8578
   179
        }
dludwig@8510
   180
        WINRT_GlobalSDLVideoDevice->displays[0].current_mode = resizedDisplayMode;
dludwig@8578
   181
        if (WINRT_GlobalSDLVideoDevice->displays[0].display_modes[0].driverdata) {
dludwig@8578
   182
            SDL_free(WINRT_GlobalSDLVideoDevice->displays[0].display_modes[0].driverdata);
dludwig@8578
   183
        }
dludwig@8510
   184
        WINRT_GlobalSDLVideoDevice->displays[0].display_modes[0] = resizedDisplayMode;
dludwig@8510
   185
    }
dludwig@8510
   186
dludwig@8510
   187
    if (WINRT_GlobalSDLWindow) {
dludwig@8510
   188
        // Send a window-resize event to the rest of SDL, and to apps:
dludwig@8510
   189
        SDL_SendWindowEvent(
dludwig@8510
   190
            WINRT_GlobalSDLWindow,
dludwig@8510
   191
            SDL_WINDOWEVENT_RESIZED,
dludwig@8510
   192
            resizedDisplayMode.w,
dludwig@8510
   193
            resizedDisplayMode.h);
dludwig@8510
   194
dludwig@8510
   195
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8510
   196
        // HACK: On Windows Phone, make sure that orientation changes from
dludwig@8510
   197
        // Landscape to LandscapeFlipped, Portrait to PortraitFlipped,
dludwig@8510
   198
        // or vice-versa on either of those two, lead to the Direct3D renderer
dludwig@8510
   199
        // getting updated.
dludwig@8578
   200
        const DisplayOrientations oldOrientation = ((SDL_DisplayModeData *)oldDisplayMode.driverdata)->currentOrientation;
dludwig@8578
   201
        const DisplayOrientations newOrientation = ((SDL_DisplayModeData *)resizedDisplayMode.driverdata)->currentOrientation;
dludwig@8510
   202
dludwig@8510
   203
        if ((oldOrientation == DisplayOrientations::Landscape && newOrientation == DisplayOrientations::LandscapeFlipped) ||
dludwig@8510
   204
            (oldOrientation == DisplayOrientations::LandscapeFlipped && newOrientation == DisplayOrientations::Landscape) ||
dludwig@8510
   205
            (oldOrientation == DisplayOrientations::Portrait && newOrientation == DisplayOrientations::PortraitFlipped) ||
dludwig@8510
   206
            (oldOrientation == DisplayOrientations::PortraitFlipped && newOrientation == DisplayOrientations::Portrait))
dludwig@8510
   207
        {
dludwig@8510
   208
            // One of the reasons this event is getting sent out is because SDL
dludwig@8510
   209
            // will ignore requests to send out SDL_WINDOWEVENT_RESIZED events
dludwig@8510
   210
            // if and when the event size doesn't change (and the Direct3D 11.1
dludwig@8510
   211
            // renderer doesn't get the memo).
dludwig@8510
   212
            //
dludwig@8510
   213
            // Make sure that the display/window size really didn't change.  If
dludwig@8510
   214
            // it did, then a SDL_WINDOWEVENT_SIZE_CHANGED event got sent, and
dludwig@8510
   215
            // the Direct3D 11.1 renderer picked it up, presumably.
dludwig@8510
   216
            if (oldDisplayMode.w == resizedDisplayMode.w &&
dludwig@8510
   217
                oldDisplayMode.h == resizedDisplayMode.h)
dludwig@8510
   218
            {
dludwig@8510
   219
                SDL_SendWindowEvent(
dludwig@8510
   220
                    WINRT_GlobalSDLWindow,
dludwig@8510
   221
                    SDL_WINDOWEVENT_SIZE_CHANGED,
dludwig@8510
   222
                    resizedDisplayMode.w,
dludwig@8510
   223
                    resizedDisplayMode.h);
dludwig@8510
   224
            }
dludwig@8510
   225
        }
dludwig@8510
   226
#endif
dludwig@8510
   227
    }
dludwig@8578
   228
    
dludwig@8578
   229
    if (oldDisplayMode.driverdata) {
dludwig@8578
   230
        SDL_free(oldDisplayMode.driverdata);
dludwig@8578
   231
    }
dludwig@8510
   232
}
dludwig@8510
   233
dludwig@8322
   234
SDL_WinRTApp::SDL_WinRTApp() :
dludwig@8384
   235
    m_windowClosed(false),
dludwig@8498
   236
    m_windowVisible(true)
dludwig@8322
   237
{
dludwig@8322
   238
}
dludwig@8322
   239
dludwig@8322
   240
void SDL_WinRTApp::Initialize(CoreApplicationView^ applicationView)
dludwig@8322
   241
{
dludwig@8384
   242
    applicationView->Activated +=
dludwig@8322
   243
        ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &SDL_WinRTApp::OnActivated);
dludwig@8322
   244
dludwig@8384
   245
    CoreApplication::Suspending +=
dludwig@8322
   246
        ref new EventHandler<SuspendingEventArgs^>(this, &SDL_WinRTApp::OnSuspending);
dludwig@8322
   247
dludwig@8384
   248
    CoreApplication::Resuming +=
dludwig@8322
   249
        ref new EventHandler<Platform::Object^>(this, &SDL_WinRTApp::OnResuming);
dludwig@8435
   250
dludwig@8474
   251
    DisplayProperties::OrientationChanged +=
dludwig@8474
   252
        ref new DisplayPropertiesEventHandler(this, &SDL_WinRTApp::OnOrientationChanged);
dludwig@8474
   253
dludwig@8435
   254
    // Register the hint, SDL_HINT_ORIENTATIONS, with SDL.  This needs to be
dludwig@8435
   255
    // done before the hint's callback is registered (as of Feb 22, 2013),
dludwig@8435
   256
    // otherwise the hint callback won't get registered.
dludwig@8435
   257
    //
dludwig@8504
   258
    // 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
   259
    //SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight Portrait PortraitUpsideDown");   // DavidL: this is no longer needed (for SDL_AddHintCallback)
dludwig@8478
   260
    SDL_AddHintCallback(SDL_HINT_ORIENTATIONS, WINRT_SetDisplayOrientationsPreference, NULL);
dludwig@8322
   261
}
dludwig@8322
   262
dludwig@8474
   263
void SDL_WinRTApp::OnOrientationChanged(Object^ sender)
dludwig@8474
   264
{
dludwig@8474
   265
#if LOG_ORIENTATION_EVENTS==1
dludwig@8474
   266
    CoreWindow^ window = CoreWindow::GetForCurrentThread();
dludwig@8474
   267
    if (window) {
dludwig@8474
   268
        SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, CoreWindow Size={%f,%f}\n",
dludwig@8474
   269
            __FUNCTION__,
dludwig@8474
   270
            (int)DisplayProperties::CurrentOrientation,
dludwig@8474
   271
            (int)DisplayProperties::NativeOrientation,
dludwig@8474
   272
            (int)DisplayProperties::AutoRotationPreferences,
dludwig@8474
   273
            window->Bounds.Width,
dludwig@8474
   274
            window->Bounds.Height);
dludwig@8474
   275
    } else {
dludwig@8474
   276
        SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d\n",
dludwig@8474
   277
            __FUNCTION__,
dludwig@8474
   278
            (int)DisplayProperties::CurrentOrientation,
dludwig@8474
   279
            (int)DisplayProperties::NativeOrientation,
dludwig@8474
   280
            (int)DisplayProperties::AutoRotationPreferences);
dludwig@8474
   281
    }
dludwig@8474
   282
#endif
dludwig@8510
   283
dludwig@8510
   284
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8510
   285
    // On Windows Phone, treat an orientation change as a change in window size.
dludwig@8510
   286
    // The native window's size doesn't seem to change, however SDL will simulate
dludwig@8510
   287
    // a window size change.
dludwig@8510
   288
    WINRT_ProcessWindowSizeChange();
dludwig@8510
   289
#endif
dludwig@8474
   290
}
dludwig@8474
   291
dludwig@8322
   292
void SDL_WinRTApp::SetWindow(CoreWindow^ window)
dludwig@8322
   293
{
dludwig@8474
   294
#if LOG_WINDOW_EVENTS==1
dludwig@8474
   295
    SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, window Size={%f,%f}\n",
dludwig@8474
   296
        __FUNCTION__,
dludwig@8474
   297
        (int)DisplayProperties::CurrentOrientation,
dludwig@8474
   298
        (int)DisplayProperties::NativeOrientation,
dludwig@8474
   299
        (int)DisplayProperties::AutoRotationPreferences,
dludwig@8474
   300
        window->Bounds.Width,
dludwig@8474
   301
        window->Bounds.Height);
dludwig@8474
   302
#endif
dludwig@8474
   303
dludwig@8384
   304
    window->SizeChanged += 
dludwig@8322
   305
        ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &SDL_WinRTApp::OnWindowSizeChanged);
dludwig@8322
   306
dludwig@8384
   307
    window->VisibilityChanged +=
dludwig@8384
   308
        ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &SDL_WinRTApp::OnVisibilityChanged);
dludwig@8322
   309
dludwig@8384
   310
    window->Closed += 
dludwig@8322
   311
        ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &SDL_WinRTApp::OnWindowClosed);
dludwig@8322
   312
dludwig@8399
   313
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
dludwig@8384
   314
    window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
dludwig@8399
   315
#endif
dludwig@8322
   316
dludwig@8384
   317
    window->PointerPressed +=
dludwig@8384
   318
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerPressed);
dludwig@8322
   319
dludwig@8521
   320
    window->PointerMoved +=
dludwig@8521
   321
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerMoved);
dludwig@8521
   322
dludwig@8332
   323
    window->PointerReleased +=
dludwig@8384
   324
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerReleased);
dludwig@8332
   325
dludwig@8422
   326
    window->PointerWheelChanged +=
dludwig@8422
   327
        ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerWheelChanged);
dludwig@8422
   328
dludwig@8399
   329
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
dludwig@8378
   330
    // Retrieves relative-only mouse movements:
dludwig@8378
   331
    Windows::Devices::Input::MouseDevice::GetForCurrentView()->MouseMoved +=
dludwig@8378
   332
        ref new TypedEventHandler<MouseDevice^, MouseEventArgs^>(this, &SDL_WinRTApp::OnMouseMoved);
dludwig@8399
   333
#endif
dludwig@8378
   334
dludwig@8336
   335
    window->KeyDown +=
dludwig@8384
   336
        ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyDown);
dludwig@8336
   337
dludwig@8384
   338
    window->KeyUp +=
dludwig@8384
   339
        ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyUp);
dludwig@8576
   340
dludwig@8577
   341
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8577
   342
    HardwareButtons::BackPressed +=
dludwig@8577
   343
        ref new EventHandler<BackPressedEventArgs^>(this, &SDL_WinRTApp::OnBackButtonPressed);
dludwig@8577
   344
#endif
dludwig@8577
   345
dludwig@8576
   346
#if WINAPI_FAMILY == WINAPI_FAMILY_APP  // for Windows 8/8.1/RT apps... (and not Phone apps)
dludwig@8576
   347
    // Make sure we know when a user has opened the app's settings pane.
dludwig@8576
   348
    // This is needed in order to display a privacy policy, which needs
dludwig@8576
   349
    // to be done for network-enabled apps, as per Windows Store requirements.
dludwig@8576
   350
    using namespace Windows::UI::ApplicationSettings;
dludwig@8576
   351
    SettingsPane::GetForCurrentView()->CommandsRequested +=
dludwig@8576
   352
        ref new TypedEventHandler<SettingsPane^, SettingsPaneCommandsRequestedEventArgs^>
dludwig@8576
   353
            (this, &SDL_WinRTApp::OnSettingsPaneCommandsRequested);
dludwig@8576
   354
#endif
dludwig@8322
   355
}
dludwig@8322
   356
dludwig@8322
   357
void SDL_WinRTApp::Load(Platform::String^ entryPoint)
dludwig@8322
   358
{
dludwig@8322
   359
}
dludwig@8322
   360
dludwig@8322
   361
void SDL_WinRTApp::Run()
dludwig@8322
   362
{
dludwig@8481
   363
    SDL_SetMainReady();
dludwig@8531
   364
    if (WINRT_SDLAppEntryPoint)
dludwig@8324
   365
    {
dludwig@8324
   366
        // TODO, WinRT: pass the C-style main() a reasonably realistic
dludwig@8324
   367
        // representation of command line arguments.
dludwig@8324
   368
        int argc = 0;
dludwig@8324
   369
        char **argv = NULL;
dludwig@8531
   370
        WINRT_SDLAppEntryPoint(argc, argv);
dludwig@8324
   371
    }
dludwig@8331
   372
}
dludwig@8324
   373
dludwig@8331
   374
void SDL_WinRTApp::PumpEvents()
dludwig@8331
   375
{
dludwig@8384
   376
    if (!m_windowClosed)
dludwig@8384
   377
    {
dludwig@8384
   378
        if (m_windowVisible)
dludwig@8384
   379
        {
dludwig@8384
   380
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
dludwig@8384
   381
        }
dludwig@8384
   382
        else
dludwig@8384
   383
        {
dludwig@8384
   384
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
dludwig@8384
   385
        }
dludwig@8384
   386
    }
dludwig@8322
   387
}
dludwig@8322
   388
dludwig@8322
   389
void SDL_WinRTApp::Uninitialize()
dludwig@8322
   390
{
dludwig@8322
   391
}
dludwig@8322
   392
dludwig@8576
   393
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8576
   394
void SDL_WinRTApp::OnSettingsPaneCommandsRequested(
dludwig@8576
   395
    Windows::UI::ApplicationSettings::SettingsPane ^p,
dludwig@8576
   396
    Windows::UI::ApplicationSettings::SettingsPaneCommandsRequestedEventArgs ^args)
dludwig@8576
   397
{
dludwig@8576
   398
    using namespace Platform;
dludwig@8576
   399
    using namespace Windows::UI::ApplicationSettings;
dludwig@8576
   400
    using namespace Windows::UI::Popups;
dludwig@8576
   401
dludwig@8576
   402
    String ^privacyPolicyURL = nullptr;     // a URL to an app's Privacy Policy
dludwig@8576
   403
    String ^privacyPolicyLabel = nullptr;   // label/link text
dludwig@8576
   404
    const char *tmpHintValue = NULL;        // SDL_GetHint-retrieved value, used immediately
dludwig@8576
   405
    wchar_t *tmpStr = NULL;                 // used for UTF8 to UCS2 conversion
dludwig@8576
   406
dludwig@8576
   407
    // Setup a 'Privacy Policy' link, if one is available (via SDL_GetHint):
dludwig@8576
   408
    tmpHintValue = SDL_GetHint(SDL_HINT_WINRT_PRIVACY_POLICY_URL);
dludwig@8576
   409
    if (tmpHintValue && tmpHintValue[0] != '\0') {
dludwig@8576
   410
        // Convert the privacy policy's URL to UCS2:
dludwig@8576
   411
        tmpStr = WIN_UTF8ToString(tmpHintValue);
dludwig@8576
   412
        privacyPolicyURL = ref new String(tmpStr);
dludwig@8576
   413
        SDL_free(tmpStr);
dludwig@8576
   414
dludwig@8576
   415
        // Optionally retrieve custom label-text for the link.  If this isn't
dludwig@8576
   416
        // available, a default value will be used instead.
dludwig@8576
   417
        tmpHintValue = SDL_GetHint(SDL_HINT_WINRT_PRIVACY_POLICY_LABEL);
dludwig@8576
   418
        if (tmpHintValue && tmpHintValue[0] != '\0') {
dludwig@8576
   419
            tmpStr = WIN_UTF8ToString(tmpHintValue);
dludwig@8576
   420
            privacyPolicyLabel = ref new String(tmpStr);
dludwig@8576
   421
            SDL_free(tmpStr);
dludwig@8576
   422
        } else {
dludwig@8576
   423
            privacyPolicyLabel = ref new String(L"Privacy Policy");
dludwig@8576
   424
        }
dludwig@8576
   425
dludwig@8576
   426
        // Register the link, along with a handler to be called if and when it is
dludwig@8576
   427
        // clicked:
dludwig@8576
   428
        auto cmd = ref new SettingsCommand(L"privacyPolicy", privacyPolicyLabel,
dludwig@8576
   429
            ref new UICommandInvokedHandler([=](IUICommand ^) {
dludwig@8576
   430
                Windows::System::Launcher::LaunchUriAsync(ref new Uri(privacyPolicyURL));
dludwig@8576
   431
        }));
dludwig@8576
   432
        args->Request->ApplicationCommands->Append(cmd);
dludwig@8576
   433
    }
dludwig@8576
   434
}
dludwig@8576
   435
#endif // if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8576
   436
dludwig@8322
   437
void SDL_WinRTApp::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
dludwig@8322
   438
{
dludwig@8474
   439
#if LOG_WINDOW_EVENTS==1
dludwig@8497
   440
    SDL_Log("%s, size={%f,%f}, current orientation=%d, native orientation=%d, auto rot. pref=%d, WINRT_GlobalSDLWindow?=%s\n",
dludwig@8474
   441
        __FUNCTION__,
dludwig@8474
   442
        args->Size.Width, args->Size.Height,
dludwig@8474
   443
        (int)DisplayProperties::CurrentOrientation,
dludwig@8474
   444
        (int)DisplayProperties::NativeOrientation,
dludwig@8474
   445
        (int)DisplayProperties::AutoRotationPreferences,
dludwig@8497
   446
        (WINRT_GlobalSDLWindow ? "yes" : "no"));
dludwig@8433
   447
#endif
dludwig@8433
   448
dludwig@8510
   449
    WINRT_ProcessWindowSizeChange();
dludwig@8322
   450
}
dludwig@8322
   451
dludwig@8322
   452
void SDL_WinRTApp::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
dludwig@8322
   453
{
dludwig@8474
   454
#if LOG_WINDOW_EVENTS==1
dludwig@8497
   455
    SDL_Log("%s, visible?=%s, WINRT_GlobalSDLWindow?=%s\n",
dludwig@8474
   456
        __FUNCTION__,
dludwig@8474
   457
        (args->Visible ? "yes" : "no"),
dludwig@8497
   458
        (WINRT_GlobalSDLWindow ? "yes" : "no"));
dludwig@8474
   459
#endif
dludwig@8474
   460
dludwig@8384
   461
    m_windowVisible = args->Visible;
dludwig@8497
   462
    if (WINRT_GlobalSDLWindow) {
dludwig@8497
   463
        SDL_bool wasSDLWindowSurfaceValid = WINRT_GlobalSDLWindow->surface_valid;
dludwig@8390
   464
dludwig@8385
   465
        if (args->Visible) {
dludwig@8497
   466
            SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_SHOWN, 0, 0);
dludwig@8385
   467
        } else {
dludwig@8497
   468
            SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_HIDDEN, 0, 0);
dludwig@8385
   469
        }
dludwig@8390
   470
dludwig@8390
   471
        // HACK: Prevent SDL's window-hide handling code, which currently
dludwig@8390
   472
        // triggers a fake window resize (possibly erronously), from
dludwig@8390
   473
        // marking the SDL window's surface as invalid.
dludwig@8390
   474
        //
dludwig@8390
   475
        // A better solution to this probably involves figuring out if the
dludwig@8390
   476
        // fake window resize can be prevented.
dludwig@8497
   477
        WINRT_GlobalSDLWindow->surface_valid = wasSDLWindowSurfaceValid;
dludwig@8385
   478
    }
dludwig@8322
   479
}
dludwig@8322
   480
dludwig@8322
   481
void SDL_WinRTApp::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
dludwig@8322
   482
{
dludwig@8474
   483
#if LOG_WINDOW_EVENTS==1
dludwig@8474
   484
    SDL_Log("%s\n", __FUNCTION__);
dludwig@8474
   485
#endif
dludwig@8384
   486
    m_windowClosed = true;
dludwig@8322
   487
}
dludwig@8322
   488
dludwig@8322
   489
void SDL_WinRTApp::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
dludwig@8322
   490
{
dludwig@8384
   491
    CoreWindow::GetForCurrentThread()->Activate();
dludwig@8322
   492
}
dludwig@8322
   493
dludwig@8383
   494
static int SDLCALL RemoveAppSuspendAndResumeEvents(void * userdata, SDL_Event * event)
dludwig@8383
   495
{
dludwig@8383
   496
    if (event->type == SDL_WINDOWEVENT)
dludwig@8383
   497
    {
dludwig@8383
   498
        switch (event->window.event)
dludwig@8383
   499
        {
dludwig@8383
   500
            case SDL_WINDOWEVENT_MINIMIZED:
dludwig@8383
   501
            case SDL_WINDOWEVENT_RESTORED:
dludwig@8383
   502
                // Return 0 to indicate that the event should be removed from the
dludwig@8383
   503
                // event queue:
dludwig@8383
   504
                return 0;
dludwig@8383
   505
            default:
dludwig@8383
   506
                break;
dludwig@8383
   507
        }
dludwig@8383
   508
    }
dludwig@8383
   509
dludwig@8383
   510
    // Return 1 to indicate that the event should stay in the event queue:
dludwig@8383
   511
    return 1;
dludwig@8383
   512
}
dludwig@8383
   513
dludwig@8322
   514
void SDL_WinRTApp::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
dludwig@8322
   515
{
dludwig@8384
   516
    // Save app state asynchronously after requesting a deferral. Holding a deferral
dludwig@8384
   517
    // indicates that the application is busy performing suspending operations. Be
dludwig@8384
   518
    // aware that a deferral may not be held indefinitely. After about five seconds,
dludwig@8384
   519
    // the app will be forced to exit.
dludwig@8384
   520
    SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
dludwig@8384
   521
    create_task([this, deferral]()
dludwig@8384
   522
    {
dludwig@8383
   523
        // Send a window-minimized event immediately to observers.
dludwig@8383
   524
        // CoreDispatcher::ProcessEvents, which is the backbone on which
dludwig@8383
   525
        // SDL_WinRTApp::PumpEvents is built, will not return to its caller
dludwig@8383
   526
        // once it sends out a suspend event.  Any events posted to SDL's
dludwig@8383
   527
        // event queue won't get received until the WinRT app is resumed.
dludwig@8383
   528
        // SDL_AddEventWatch() may be used to receive app-suspend events on
dludwig@8383
   529
        // WinRT.
dludwig@8383
   530
        //
dludwig@8383
   531
        // In order to prevent app-suspend events from being received twice:
dludwig@8383
   532
        // first via a callback passed to SDL_AddEventWatch, and second via
dludwig@8383
   533
        // SDL's event queue, the event will be sent to SDL, then immediately
dludwig@8383
   534
        // removed from the queue.
dludwig@8497
   535
        if (WINRT_GlobalSDLWindow)
dludwig@8383
   536
        {
dludwig@8497
   537
            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
   538
            SDL_FilterEvents(RemoveAppSuspendAndResumeEvents, 0);
dludwig@8383
   539
        }
dludwig@8384
   540
        deferral->Complete();
dludwig@8384
   541
    });
dludwig@8322
   542
}
dludwig@8383
   543
dludwig@8322
   544
void SDL_WinRTApp::OnResuming(Platform::Object^ sender, Platform::Object^ args)
dludwig@8322
   545
{
dludwig@8384
   546
    // Restore any data or state that was unloaded on suspend. By default, data
dludwig@8384
   547
    // and state are persisted when resuming from suspend. Note that this event
dludwig@8384
   548
    // does not occur if the app was previously terminated.
dludwig@8497
   549
    if (WINRT_GlobalSDLWindow)
dludwig@8383
   550
    {
dludwig@8497
   551
        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
   552
dludwig@8383
   553
        // Remove the app-resume event from the queue, as is done with the
dludwig@8383
   554
        // app-suspend event.
dludwig@8383
   555
        //
dludwig@8383
   556
        // TODO, WinRT: consider posting this event to the queue even though
dludwig@8383
   557
        // its counterpart, the app-suspend event, effectively has to be
dludwig@8383
   558
        // processed immediately.
dludwig@8383
   559
        SDL_FilterEvents(RemoveAppSuspendAndResumeEvents, 0);
dludwig@8383
   560
    }
dludwig@8322
   561
}
dludwig@8521
   562
dludwig@8521
   563
static void
dludwig@8521
   564
WINRT_LogPointerEvent(const char * header, Windows::UI::Core::PointerEventArgs ^ args, Windows::Foundation::Point transformedPoint)
dludwig@8521
   565
{
dludwig@8521
   566
    Windows::UI::Input::PointerPoint ^ pt = args->CurrentPoint;
dludwig@8521
   567
    SDL_Log("%s: Position={%f,%f}, Transformed Pos={%f, %f}, MouseWheelDelta=%d, FrameId=%d, PointerId=%d, SDL button=%d\n",
dludwig@8521
   568
        header,
dludwig@8521
   569
        pt->Position.X, pt->Position.Y,
dludwig@8521
   570
        transformedPoint.X, transformedPoint.Y,
dludwig@8521
   571
        pt->Properties->MouseWheelDelta,
dludwig@8521
   572
        pt->FrameId,
dludwig@8521
   573
        pt->PointerId,
dludwig@8521
   574
        WINRT_GetSDLButtonForPointerPoint(pt));
dludwig@8521
   575
}
dludwig@8521
   576
dludwig@8521
   577
void SDL_WinRTApp::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
dludwig@8521
   578
{
dludwig@8521
   579
#if LOG_POINTER_EVENTS
dludwig@8542
   580
    WINRT_LogPointerEvent("pointer pressed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
dludwig@8521
   581
#endif
dludwig@8521
   582
dludwig@8521
   583
    WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
dludwig@8521
   584
}
dludwig@8521
   585
dludwig@8521
   586
void SDL_WinRTApp::OnPointerMoved(CoreWindow^ sender, PointerEventArgs^ args)
dludwig@8521
   587
{
dludwig@8521
   588
#if LOG_POINTER_EVENTS
dludwig@8542
   589
    WINRT_LogPointerEvent("pointer moved", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
dludwig@8521
   590
#endif
dludwig@8521
   591
dludwig@8521
   592
    WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
dludwig@8521
   593
}
dludwig@8521
   594
dludwig@8521
   595
void SDL_WinRTApp::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
dludwig@8521
   596
{
dludwig@8521
   597
#if LOG_POINTER_EVENTS
dludwig@8542
   598
    WINRT_LogPointerEvent("pointer released", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
dludwig@8521
   599
#endif
dludwig@8521
   600
dludwig@8521
   601
    WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
dludwig@8521
   602
}
dludwig@8521
   603
dludwig@8521
   604
void SDL_WinRTApp::OnPointerWheelChanged(CoreWindow^ sender, PointerEventArgs^ args)
dludwig@8521
   605
{
dludwig@8521
   606
#if LOG_POINTER_EVENTS
dludwig@8542
   607
    WINRT_LogPointerEvent("pointer wheel changed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
dludwig@8521
   608
#endif
dludwig@8521
   609
dludwig@8521
   610
    WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
dludwig@8521
   611
}
dludwig@8521
   612
dludwig@8521
   613
void SDL_WinRTApp::OnMouseMoved(MouseDevice^ mouseDevice, MouseEventArgs^ args)
dludwig@8521
   614
{
dludwig@8521
   615
    WINRT_ProcessMouseMovedEvent(WINRT_GlobalSDLWindow, args);
dludwig@8521
   616
}
dludwig@8521
   617
dludwig@8521
   618
void SDL_WinRTApp::OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
dludwig@8521
   619
{
dludwig@8521
   620
    WINRT_ProcessKeyDownEvent(args);
dludwig@8521
   621
}
dludwig@8521
   622
dludwig@8521
   623
void SDL_WinRTApp::OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
dludwig@8521
   624
{
dludwig@8521
   625
    WINRT_ProcessKeyUpEvent(args);
dludwig@8521
   626
}
dludwig@8577
   627
dludwig@8577
   628
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8577
   629
void SDL_WinRTApp::OnBackButtonPressed(Platform::Object^ sender, Windows::Phone::UI::Input::BackPressedEventArgs^ args)
dludwig@8577
   630
{
dludwig@8577
   631
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_AC_BACK);
dludwig@8577
   632
    SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_AC_BACK);
dludwig@8577
   633
dludwig@8577
   634
    const char *hint = SDL_GetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON);
dludwig@8577
   635
    if (hint) {
dludwig@8577
   636
        if (*hint == '1') {
dludwig@8577
   637
            args->Handled = true;
dludwig@8577
   638
        }
dludwig@8577
   639
    }
dludwig@8577
   640
}
dludwig@8577
   641
#endif
dludwig@8577
   642