SDL_winrtapp.cpp 22 KB

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