src/core/winrt/SDL_winrtapp_direct3d.cpp
author David Ludwig <dludwig@pobox.com>
Mon, 10 Mar 2014 21:21:35 -0400
changeset 8600 092802455aed
parent 8582 c3e9a2b93517
child 8616 ec011c16e2fd
permissions -rw-r--r--
build fixes for most WinRT-related files

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