src/core/winrt/SDL_winrtapp_direct3d.cpp
author David Ludwig <dludwig@pobox.com>
Sun, 22 Sep 2013 12:26:53 -0400
changeset 8531 d481485a1b85
parent 8522 3ae9dc9c439f
child 8542 97dcef41e4a7
permissions -rw-r--r--
WinRT: unified the two, public, app-init functions

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