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