src/core/winrt/SDL_winrtapp_direct3d.cpp
author David Ludwig <dludwig@pobox.com>
Sun, 26 Jan 2014 08:06:36 -0500
changeset 8577 462760a8b82e
parent 8576 8dd593afe2d7
child 8578 6517b6aca713
permissions -rw-r--r--
WinRT: simulate keyboard events on Windows Phone 8 back-button presses

Pressing the hardware back button on a Windows Phone 8 device will now cause SDL to emit a pair of key-down and key-up events, with the SDL scancode, SDL_SCANCODE_AC_BACK.

By default, if WinRT's native back-button-press events are not explicitly marked as 'handled', then Windows Phone will terminate the app. More details on Microsoft's reasoning behind this can be found on MSDN, at http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj247550(v=vs.105).aspx

To mark back-button-press events as 'handled', set SDL_HINT_WINRT_HANDLE_BACK_BUTTON to 1. Setting it to anything else will cause these events to not be marked as 'handled'.

Due to limitations in Windows Phone's APIs, SDL will emit a virtual key-up event immediately after the back button's key-down event is registered. Unfortunately, Windows Phone 8 only allows one to register for back-button-press events, and not back-button-release events.
     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 = WINRT_CalcDisplayModeUsingNativeWindow();
   161     if (resizedDisplayMode.w == 0 || resizedDisplayMode.h == 0) {
   162         return;
   163     }
   164 
   165     SDL_DisplayMode oldDisplayMode;
   166     SDL_zero(oldDisplayMode);
   167     if (WINRT_GlobalSDLVideoDevice) {
   168         oldDisplayMode = WINRT_GlobalSDLVideoDevice->displays[0].desktop_mode;
   169         WINRT_GlobalSDLVideoDevice->displays[0].current_mode = resizedDisplayMode;
   170         WINRT_GlobalSDLVideoDevice->displays[0].desktop_mode = resizedDisplayMode;
   171         WINRT_GlobalSDLVideoDevice->displays[0].display_modes[0] = resizedDisplayMode;
   172     }
   173 
   174     if (WINRT_GlobalSDLWindow) {
   175         // Send a window-resize event to the rest of SDL, and to apps:
   176         SDL_SendWindowEvent(
   177             WINRT_GlobalSDLWindow,
   178             SDL_WINDOWEVENT_RESIZED,
   179             resizedDisplayMode.w,
   180             resizedDisplayMode.h);
   181 
   182 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   183         // HACK: On Windows Phone, make sure that orientation changes from
   184         // Landscape to LandscapeFlipped, Portrait to PortraitFlipped,
   185         // or vice-versa on either of those two, lead to the Direct3D renderer
   186         // getting updated.
   187         const DisplayOrientations oldOrientation = (DisplayOrientations) (unsigned int) oldDisplayMode.driverdata;
   188         const DisplayOrientations newOrientation = (DisplayOrientations) (unsigned int) resizedDisplayMode.driverdata;
   189 
   190         if ((oldOrientation == DisplayOrientations::Landscape && newOrientation == DisplayOrientations::LandscapeFlipped) ||
   191             (oldOrientation == DisplayOrientations::LandscapeFlipped && newOrientation == DisplayOrientations::Landscape) ||
   192             (oldOrientation == DisplayOrientations::Portrait && newOrientation == DisplayOrientations::PortraitFlipped) ||
   193             (oldOrientation == DisplayOrientations::PortraitFlipped && newOrientation == DisplayOrientations::Portrait))
   194         {
   195             // One of the reasons this event is getting sent out is because SDL
   196             // will ignore requests to send out SDL_WINDOWEVENT_RESIZED events
   197             // if and when the event size doesn't change (and the Direct3D 11.1
   198             // renderer doesn't get the memo).
   199             //
   200             // Make sure that the display/window size really didn't change.  If
   201             // it did, then a SDL_WINDOWEVENT_SIZE_CHANGED event got sent, and
   202             // the Direct3D 11.1 renderer picked it up, presumably.
   203             if (oldDisplayMode.w == resizedDisplayMode.w &&
   204                 oldDisplayMode.h == resizedDisplayMode.h)
   205             {
   206                 SDL_SendWindowEvent(
   207                     WINRT_GlobalSDLWindow,
   208                     SDL_WINDOWEVENT_SIZE_CHANGED,
   209                     resizedDisplayMode.w,
   210                     resizedDisplayMode.h);
   211             }
   212         }
   213 #endif
   214     }
   215 }
   216 
   217 SDL_WinRTApp::SDL_WinRTApp() :
   218     m_windowClosed(false),
   219     m_windowVisible(true)
   220 {
   221 }
   222 
   223 void SDL_WinRTApp::Initialize(CoreApplicationView^ applicationView)
   224 {
   225     applicationView->Activated +=
   226         ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &SDL_WinRTApp::OnActivated);
   227 
   228     CoreApplication::Suspending +=
   229         ref new EventHandler<SuspendingEventArgs^>(this, &SDL_WinRTApp::OnSuspending);
   230 
   231     CoreApplication::Resuming +=
   232         ref new EventHandler<Platform::Object^>(this, &SDL_WinRTApp::OnResuming);
   233 
   234     DisplayProperties::OrientationChanged +=
   235         ref new DisplayPropertiesEventHandler(this, &SDL_WinRTApp::OnOrientationChanged);
   236 
   237     // Register the hint, SDL_HINT_ORIENTATIONS, with SDL.  This needs to be
   238     // done before the hint's callback is registered (as of Feb 22, 2013),
   239     // otherwise the hint callback won't get registered.
   240     //
   241     // 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.
   242     //SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight Portrait PortraitUpsideDown");   // DavidL: this is no longer needed (for SDL_AddHintCallback)
   243     SDL_AddHintCallback(SDL_HINT_ORIENTATIONS, WINRT_SetDisplayOrientationsPreference, NULL);
   244 }
   245 
   246 void SDL_WinRTApp::OnOrientationChanged(Object^ sender)
   247 {
   248 #if LOG_ORIENTATION_EVENTS==1
   249     CoreWindow^ window = CoreWindow::GetForCurrentThread();
   250     if (window) {
   251         SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, CoreWindow Size={%f,%f}\n",
   252             __FUNCTION__,
   253             (int)DisplayProperties::CurrentOrientation,
   254             (int)DisplayProperties::NativeOrientation,
   255             (int)DisplayProperties::AutoRotationPreferences,
   256             window->Bounds.Width,
   257             window->Bounds.Height);
   258     } else {
   259         SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d\n",
   260             __FUNCTION__,
   261             (int)DisplayProperties::CurrentOrientation,
   262             (int)DisplayProperties::NativeOrientation,
   263             (int)DisplayProperties::AutoRotationPreferences);
   264     }
   265 #endif
   266 
   267 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   268     // On Windows Phone, treat an orientation change as a change in window size.
   269     // The native window's size doesn't seem to change, however SDL will simulate
   270     // a window size change.
   271     WINRT_ProcessWindowSizeChange();
   272 #endif
   273 }
   274 
   275 void SDL_WinRTApp::SetWindow(CoreWindow^ window)
   276 {
   277 #if LOG_WINDOW_EVENTS==1
   278     SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, window Size={%f,%f}\n",
   279         __FUNCTION__,
   280         (int)DisplayProperties::CurrentOrientation,
   281         (int)DisplayProperties::NativeOrientation,
   282         (int)DisplayProperties::AutoRotationPreferences,
   283         window->Bounds.Width,
   284         window->Bounds.Height);
   285 #endif
   286 
   287     window->SizeChanged += 
   288         ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &SDL_WinRTApp::OnWindowSizeChanged);
   289 
   290     window->VisibilityChanged +=
   291         ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &SDL_WinRTApp::OnVisibilityChanged);
   292 
   293     window->Closed += 
   294         ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &SDL_WinRTApp::OnWindowClosed);
   295 
   296 #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
   297     window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
   298 #endif
   299 
   300     window->PointerPressed +=
   301         ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerPressed);
   302 
   303     window->PointerMoved +=
   304         ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerMoved);
   305 
   306     window->PointerReleased +=
   307         ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerReleased);
   308 
   309     window->PointerWheelChanged +=
   310         ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerWheelChanged);
   311 
   312 #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
   313     // Retrieves relative-only mouse movements:
   314     Windows::Devices::Input::MouseDevice::GetForCurrentView()->MouseMoved +=
   315         ref new TypedEventHandler<MouseDevice^, MouseEventArgs^>(this, &SDL_WinRTApp::OnMouseMoved);
   316 #endif
   317 
   318     window->KeyDown +=
   319         ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyDown);
   320 
   321     window->KeyUp +=
   322         ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyUp);
   323 
   324 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   325     HardwareButtons::BackPressed +=
   326         ref new EventHandler<BackPressedEventArgs^>(this, &SDL_WinRTApp::OnBackButtonPressed);
   327 #endif
   328 
   329 #if WINAPI_FAMILY == WINAPI_FAMILY_APP  // for Windows 8/8.1/RT apps... (and not Phone apps)
   330     // Make sure we know when a user has opened the app's settings pane.
   331     // This is needed in order to display a privacy policy, which needs
   332     // to be done for network-enabled apps, as per Windows Store requirements.
   333     using namespace Windows::UI::ApplicationSettings;
   334     SettingsPane::GetForCurrentView()->CommandsRequested +=
   335         ref new TypedEventHandler<SettingsPane^, SettingsPaneCommandsRequestedEventArgs^>
   336             (this, &SDL_WinRTApp::OnSettingsPaneCommandsRequested);
   337 #endif
   338 }
   339 
   340 void SDL_WinRTApp::Load(Platform::String^ entryPoint)
   341 {
   342 }
   343 
   344 void SDL_WinRTApp::Run()
   345 {
   346     SDL_SetMainReady();
   347     if (WINRT_SDLAppEntryPoint)
   348     {
   349         // TODO, WinRT: pass the C-style main() a reasonably realistic
   350         // representation of command line arguments.
   351         int argc = 0;
   352         char **argv = NULL;
   353         WINRT_SDLAppEntryPoint(argc, argv);
   354     }
   355 }
   356 
   357 void SDL_WinRTApp::PumpEvents()
   358 {
   359     if (!m_windowClosed)
   360     {
   361         if (m_windowVisible)
   362         {
   363             CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
   364         }
   365         else
   366         {
   367             CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
   368         }
   369     }
   370 }
   371 
   372 void SDL_WinRTApp::Uninitialize()
   373 {
   374 }
   375 
   376 #if WINAPI_FAMILY == WINAPI_FAMILY_APP
   377 void SDL_WinRTApp::OnSettingsPaneCommandsRequested(
   378     Windows::UI::ApplicationSettings::SettingsPane ^p,
   379     Windows::UI::ApplicationSettings::SettingsPaneCommandsRequestedEventArgs ^args)
   380 {
   381     using namespace Platform;
   382     using namespace Windows::UI::ApplicationSettings;
   383     using namespace Windows::UI::Popups;
   384 
   385     String ^privacyPolicyURL = nullptr;     // a URL to an app's Privacy Policy
   386     String ^privacyPolicyLabel = nullptr;   // label/link text
   387     const char *tmpHintValue = NULL;        // SDL_GetHint-retrieved value, used immediately
   388     wchar_t *tmpStr = NULL;                 // used for UTF8 to UCS2 conversion
   389 
   390     // Setup a 'Privacy Policy' link, if one is available (via SDL_GetHint):
   391     tmpHintValue = SDL_GetHint(SDL_HINT_WINRT_PRIVACY_POLICY_URL);
   392     if (tmpHintValue && tmpHintValue[0] != '\0') {
   393         // Convert the privacy policy's URL to UCS2:
   394         tmpStr = WIN_UTF8ToString(tmpHintValue);
   395         privacyPolicyURL = ref new String(tmpStr);
   396         SDL_free(tmpStr);
   397 
   398         // Optionally retrieve custom label-text for the link.  If this isn't
   399         // available, a default value will be used instead.
   400         tmpHintValue = SDL_GetHint(SDL_HINT_WINRT_PRIVACY_POLICY_LABEL);
   401         if (tmpHintValue && tmpHintValue[0] != '\0') {
   402             tmpStr = WIN_UTF8ToString(tmpHintValue);
   403             privacyPolicyLabel = ref new String(tmpStr);
   404             SDL_free(tmpStr);
   405         } else {
   406             privacyPolicyLabel = ref new String(L"Privacy Policy");
   407         }
   408 
   409         // Register the link, along with a handler to be called if and when it is
   410         // clicked:
   411         auto cmd = ref new SettingsCommand(L"privacyPolicy", privacyPolicyLabel,
   412             ref new UICommandInvokedHandler([=](IUICommand ^) {
   413                 Windows::System::Launcher::LaunchUriAsync(ref new Uri(privacyPolicyURL));
   414         }));
   415         args->Request->ApplicationCommands->Append(cmd);
   416     }
   417 }
   418 #endif // if WINAPI_FAMILY == WINAPI_FAMILY_APP
   419 
   420 void SDL_WinRTApp::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
   421 {
   422 #if LOG_WINDOW_EVENTS==1
   423     SDL_Log("%s, size={%f,%f}, current orientation=%d, native orientation=%d, auto rot. pref=%d, WINRT_GlobalSDLWindow?=%s\n",
   424         __FUNCTION__,
   425         args->Size.Width, args->Size.Height,
   426         (int)DisplayProperties::CurrentOrientation,
   427         (int)DisplayProperties::NativeOrientation,
   428         (int)DisplayProperties::AutoRotationPreferences,
   429         (WINRT_GlobalSDLWindow ? "yes" : "no"));
   430 #endif
   431 
   432     WINRT_ProcessWindowSizeChange();
   433 }
   434 
   435 void SDL_WinRTApp::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
   436 {
   437 #if LOG_WINDOW_EVENTS==1
   438     SDL_Log("%s, visible?=%s, WINRT_GlobalSDLWindow?=%s\n",
   439         __FUNCTION__,
   440         (args->Visible ? "yes" : "no"),
   441         (WINRT_GlobalSDLWindow ? "yes" : "no"));
   442 #endif
   443 
   444     m_windowVisible = args->Visible;
   445     if (WINRT_GlobalSDLWindow) {
   446         SDL_bool wasSDLWindowSurfaceValid = WINRT_GlobalSDLWindow->surface_valid;
   447 
   448         if (args->Visible) {
   449             SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_SHOWN, 0, 0);
   450         } else {
   451             SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   452         }
   453 
   454         // HACK: Prevent SDL's window-hide handling code, which currently
   455         // triggers a fake window resize (possibly erronously), from
   456         // marking the SDL window's surface as invalid.
   457         //
   458         // A better solution to this probably involves figuring out if the
   459         // fake window resize can be prevented.
   460         WINRT_GlobalSDLWindow->surface_valid = wasSDLWindowSurfaceValid;
   461     }
   462 }
   463 
   464 void SDL_WinRTApp::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
   465 {
   466 #if LOG_WINDOW_EVENTS==1
   467     SDL_Log("%s\n", __FUNCTION__);
   468 #endif
   469     m_windowClosed = true;
   470 }
   471 
   472 void SDL_WinRTApp::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
   473 {
   474     CoreWindow::GetForCurrentThread()->Activate();
   475 }
   476 
   477 static int SDLCALL RemoveAppSuspendAndResumeEvents(void * userdata, SDL_Event * event)
   478 {
   479     if (event->type == SDL_WINDOWEVENT)
   480     {
   481         switch (event->window.event)
   482         {
   483             case SDL_WINDOWEVENT_MINIMIZED:
   484             case SDL_WINDOWEVENT_RESTORED:
   485                 // Return 0 to indicate that the event should be removed from the
   486                 // event queue:
   487                 return 0;
   488             default:
   489                 break;
   490         }
   491     }
   492 
   493     // Return 1 to indicate that the event should stay in the event queue:
   494     return 1;
   495 }
   496 
   497 void SDL_WinRTApp::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
   498 {
   499     // Save app state asynchronously after requesting a deferral. Holding a deferral
   500     // indicates that the application is busy performing suspending operations. Be
   501     // aware that a deferral may not be held indefinitely. After about five seconds,
   502     // the app will be forced to exit.
   503     SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
   504     create_task([this, deferral]()
   505     {
   506         // Send a window-minimized event immediately to observers.
   507         // CoreDispatcher::ProcessEvents, which is the backbone on which
   508         // SDL_WinRTApp::PumpEvents is built, will not return to its caller
   509         // once it sends out a suspend event.  Any events posted to SDL's
   510         // event queue won't get received until the WinRT app is resumed.
   511         // SDL_AddEventWatch() may be used to receive app-suspend events on
   512         // WinRT.
   513         //
   514         // In order to prevent app-suspend events from being received twice:
   515         // first via a callback passed to SDL_AddEventWatch, and second via
   516         // SDL's event queue, the event will be sent to SDL, then immediately
   517         // removed from the queue.
   518         if (WINRT_GlobalSDLWindow)
   519         {
   520             SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_MINIMIZED, 0, 0);   // TODO: see if SDL_WINDOWEVENT_SIZE_CHANGED should be getting triggered here (it is, currently)
   521             SDL_FilterEvents(RemoveAppSuspendAndResumeEvents, 0);
   522         }
   523         deferral->Complete();
   524     });
   525 }
   526 
   527 void SDL_WinRTApp::OnResuming(Platform::Object^ sender, Platform::Object^ args)
   528 {
   529     // Restore any data or state that was unloaded on suspend. By default, data
   530     // and state are persisted when resuming from suspend. Note that this event
   531     // does not occur if the app was previously terminated.
   532     if (WINRT_GlobalSDLWindow)
   533     {
   534         SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_RESTORED, 0, 0);    // TODO: see if SDL_WINDOWEVENT_SIZE_CHANGED should be getting triggered here (it is, currently)
   535 
   536         // Remove the app-resume event from the queue, as is done with the
   537         // app-suspend event.
   538         //
   539         // TODO, WinRT: consider posting this event to the queue even though
   540         // its counterpart, the app-suspend event, effectively has to be
   541         // processed immediately.
   542         SDL_FilterEvents(RemoveAppSuspendAndResumeEvents, 0);
   543     }
   544 }
   545 
   546 static void
   547 WINRT_LogPointerEvent(const char * header, Windows::UI::Core::PointerEventArgs ^ args, Windows::Foundation::Point transformedPoint)
   548 {
   549     Windows::UI::Input::PointerPoint ^ pt = args->CurrentPoint;
   550     SDL_Log("%s: Position={%f,%f}, Transformed Pos={%f, %f}, MouseWheelDelta=%d, FrameId=%d, PointerId=%d, SDL button=%d\n",
   551         header,
   552         pt->Position.X, pt->Position.Y,
   553         transformedPoint.X, transformedPoint.Y,
   554         pt->Properties->MouseWheelDelta,
   555         pt->FrameId,
   556         pt->PointerId,
   557         WINRT_GetSDLButtonForPointerPoint(pt));
   558 }
   559 
   560 void SDL_WinRTApp::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
   561 {
   562 #if LOG_POINTER_EVENTS
   563     WINRT_LogPointerEvent("pointer pressed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
   564 #endif
   565 
   566     WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
   567 }
   568 
   569 void SDL_WinRTApp::OnPointerMoved(CoreWindow^ sender, PointerEventArgs^ args)
   570 {
   571 #if LOG_POINTER_EVENTS
   572     WINRT_LogPointerEvent("pointer moved", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
   573 #endif
   574 
   575     WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
   576 }
   577 
   578 void SDL_WinRTApp::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
   579 {
   580 #if LOG_POINTER_EVENTS
   581     WINRT_LogPointerEvent("pointer released", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
   582 #endif
   583 
   584     WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
   585 }
   586 
   587 void SDL_WinRTApp::OnPointerWheelChanged(CoreWindow^ sender, PointerEventArgs^ args)
   588 {
   589 #if LOG_POINTER_EVENTS
   590     WINRT_LogPointerEvent("pointer wheel changed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
   591 #endif
   592 
   593     WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
   594 }
   595 
   596 void SDL_WinRTApp::OnMouseMoved(MouseDevice^ mouseDevice, MouseEventArgs^ args)
   597 {
   598     WINRT_ProcessMouseMovedEvent(WINRT_GlobalSDLWindow, args);
   599 }
   600 
   601 void SDL_WinRTApp::OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
   602 {
   603     WINRT_ProcessKeyDownEvent(args);
   604 }
   605 
   606 void SDL_WinRTApp::OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
   607 {
   608     WINRT_ProcessKeyUpEvent(args);
   609 }
   610 
   611 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   612 void SDL_WinRTApp::OnBackButtonPressed(Platform::Object^ sender, Windows::Phone::UI::Input::BackPressedEventArgs^ args)
   613 {
   614     SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_AC_BACK);
   615     SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_AC_BACK);
   616 
   617     const char *hint = SDL_GetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON);
   618     if (hint) {
   619         if (*hint == '1') {
   620             args->Handled = true;
   621         }
   622     }
   623 }
   624 #endif
   625