SDL_winrtapp_direct3d.cpp 29 KB

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