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

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