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