SDL_winrtapp_direct3d.cpp 27 KB

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