SDL_winrtapp_direct3d.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. /* HACK: prevent SDL from altering an app's .appxmanifest-set orientation
  110. * from being changed on startup, by detecting when SDL_HINT_ORIENTATIONS
  111. * is getting registered.
  112. *
  113. * TODO, WinRT: consider reading in an app's .appxmanifest file, and apply its orientation when 'newValue == NULL'.
  114. */
  115. if ((oldValue == NULL) && (newValue == NULL)) {
  116. return;
  117. }
  118. // Start with no orientation flags, then add each in as they're parsed
  119. // from newValue.
  120. unsigned int orientationFlags = 0;
  121. if (newValue) {
  122. std::istringstream tokenizer(newValue);
  123. while (!tokenizer.eof()) {
  124. std::string orientationName;
  125. std::getline(tokenizer, orientationName, ' ');
  126. if (orientationName == "LandscapeLeft") {
  127. orientationFlags |= (unsigned int) DisplayOrientations::LandscapeFlipped;
  128. } else if (orientationName == "LandscapeRight") {
  129. orientationFlags |= (unsigned int) DisplayOrientations::Landscape;
  130. } else if (orientationName == "Portrait") {
  131. orientationFlags |= (unsigned int) DisplayOrientations::Portrait;
  132. } else if (orientationName == "PortraitUpsideDown") {
  133. orientationFlags |= (unsigned int) DisplayOrientations::PortraitFlipped;
  134. }
  135. }
  136. }
  137. // If no valid orientation flags were specified, use a reasonable set of defaults:
  138. if (!orientationFlags) {
  139. // TODO, WinRT: consider seeing if an app's default orientation flags can be found out via some API call(s).
  140. orientationFlags = (unsigned int) ( \
  141. DisplayOrientations::Landscape |
  142. DisplayOrientations::LandscapeFlipped |
  143. DisplayOrientations::Portrait |
  144. DisplayOrientations::PortraitFlipped);
  145. }
  146. // Set the orientation/rotation preferences. Please note that this does
  147. // not constitute a 100%-certain lock of a given set of possible
  148. // orientations. According to Microsoft's documentation on WinRT [1]
  149. // when a device is not capable of being rotated, Windows may ignore
  150. // the orientation preferences, and stick to what the device is capable of
  151. // displaying.
  152. //
  153. // [1] Documentation on the 'InitialRotationPreference' setting for a
  154. // Windows app's manifest file describes how some orientation/rotation
  155. // preferences may be ignored. See
  156. // http://msdn.microsoft.com/en-us/library/windows/apps/hh700343.aspx
  157. // for details. Microsoft's "Display orientation sample" also gives an
  158. // outline of how Windows treats device rotation
  159. // (http://code.msdn.microsoft.com/Display-Orientation-Sample-19a58e93).
  160. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences) = (DisplayOrientations) orientationFlags;
  161. }
  162. static void
  163. WINRT_ProcessWindowSizeChange()
  164. {
  165. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  166. // Make the new window size be the one true fullscreen mode.
  167. // This change was initially done, in part, to allow the Direct3D 11.1
  168. // renderer to receive window-resize events as a device rotates.
  169. // Before, rotating a device from landscape, to portrait, and then
  170. // back to landscape would cause the Direct3D 11.1 swap buffer to
  171. // not get resized appropriately. SDL would, on the rotation from
  172. // landscape to portrait, re-resize the SDL window to it's initial
  173. // size (landscape). On the subsequent rotation, SDL would drop the
  174. // window-resize event as it appeared the SDL window didn't change
  175. // size, and the Direct3D 11.1 renderer wouldn't resize its swap
  176. // chain.
  177. SDL_DisplayMode newDisplayMode;
  178. if (WINRT_CalcDisplayModeUsingNativeWindow(&newDisplayMode) != 0) {
  179. return;
  180. }
  181. // Make note of the old display mode, and it's old driverdata.
  182. SDL_DisplayMode oldDisplayMode;
  183. SDL_zero(oldDisplayMode);
  184. if (_this) {
  185. oldDisplayMode = _this->displays[0].desktop_mode;
  186. }
  187. // Setup the new display mode in the appropriate spots.
  188. if (_this) {
  189. // Make a full copy of the display mode for display_modes[0],
  190. // one with with a separately malloced 'driverdata' field.
  191. // SDL_VideoQuit(), if called, will attempt to free the driverdata
  192. // fields in 'desktop_mode' and each entry in the 'display_modes'
  193. // array.
  194. if (_this->displays[0].display_modes[0].driverdata) {
  195. // Free the previous mode's memory
  196. SDL_free(_this->displays[0].display_modes[0].driverdata);
  197. _this->displays[0].display_modes[0].driverdata = NULL;
  198. }
  199. if (WINRT_DuplicateDisplayMode(&(_this->displays[0].display_modes[0]), &newDisplayMode) != 0) {
  200. // Uh oh, something went wrong. A malloc call probably failed.
  201. SDL_free(newDisplayMode.driverdata);
  202. return;
  203. }
  204. // Install 'newDisplayMode' into 'current_mode' and 'desktop_mode'.
  205. _this->displays[0].current_mode = newDisplayMode;
  206. _this->displays[0].desktop_mode = newDisplayMode;
  207. }
  208. if (WINRT_GlobalSDLWindow) {
  209. // If the window size changed, send a resize event to SDL and its host app:
  210. int window_w = 0;
  211. int window_h = 0;
  212. SDL_GetWindowSize(WINRT_GlobalSDLWindow, &window_w, &window_h);
  213. if ((window_w != newDisplayMode.w) || (window_h != newDisplayMode.h)) {
  214. SDL_SendWindowEvent(
  215. WINRT_GlobalSDLWindow,
  216. SDL_WINDOWEVENT_RESIZED,
  217. newDisplayMode.w,
  218. newDisplayMode.h);
  219. } else {
  220. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  221. // HACK: Make sure that orientation changes
  222. // lead to the Direct3D renderer's viewport getting updated:
  223. //
  224. // For some reason, this doesn't seem to need to be done on Windows 8.x,
  225. // even when going from Landscape to LandscapeFlipped. It only seems to
  226. // be needed on Windows Phone, at least when I tested on my devices.
  227. // I'm not currently sure why this is, but it seems to work fine. -- David L.
  228. //
  229. // TODO, WinRT: do more extensive research into why orientation changes on Win 8.x don't need D3D changes, or if they might, in some cases
  230. const DisplayOrientations oldOrientation = ((SDL_DisplayModeData *)oldDisplayMode.driverdata)->currentOrientation;
  231. const DisplayOrientations newOrientation = ((SDL_DisplayModeData *)newDisplayMode.driverdata)->currentOrientation;
  232. if (oldOrientation != newOrientation)
  233. {
  234. SDL_SendWindowEvent(
  235. WINRT_GlobalSDLWindow,
  236. SDL_WINDOWEVENT_SIZE_CHANGED,
  237. newDisplayMode.w,
  238. newDisplayMode.h);
  239. }
  240. #endif
  241. }
  242. }
  243. // Finally, free the 'driverdata' field of the old 'desktop_mode'.
  244. if (oldDisplayMode.driverdata) {
  245. SDL_free(oldDisplayMode.driverdata);
  246. oldDisplayMode.driverdata = NULL;
  247. }
  248. }
  249. SDL_WinRTApp::SDL_WinRTApp() :
  250. m_windowClosed(false),
  251. m_windowVisible(true)
  252. {
  253. }
  254. void SDL_WinRTApp::Initialize(CoreApplicationView^ applicationView)
  255. {
  256. applicationView->Activated +=
  257. ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &SDL_WinRTApp::OnActivated);
  258. CoreApplication::Suspending +=
  259. ref new EventHandler<SuspendingEventArgs^>(this, &SDL_WinRTApp::OnSuspending);
  260. CoreApplication::Resuming +=
  261. ref new EventHandler<Platform::Object^>(this, &SDL_WinRTApp::OnResuming);
  262. CoreApplication::Exiting +=
  263. ref new EventHandler<Platform::Object^>(this, &SDL_WinRTApp::OnExiting);
  264. }
  265. #if NTDDI_VERSION > NTDDI_WIN8
  266. void SDL_WinRTApp::OnOrientationChanged(DisplayInformation^ sender, Object^ args)
  267. #else
  268. void SDL_WinRTApp::OnOrientationChanged(Object^ sender)
  269. #endif
  270. {
  271. #if LOG_ORIENTATION_EVENTS==1
  272. CoreWindow^ window = CoreWindow::GetForCurrentThread();
  273. if (window) {
  274. SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, CoreWindow Size={%f,%f}\n",
  275. __FUNCTION__,
  276. WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  277. WINRT_DISPLAY_PROPERTY(NativeOrientation),
  278. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
  279. window->Bounds.Width,
  280. window->Bounds.Height);
  281. } else {
  282. SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d\n",
  283. __FUNCTION__,
  284. WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  285. WINRT_DISPLAY_PROPERTY(NativeOrientation),
  286. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences));
  287. }
  288. #endif
  289. WINRT_ProcessWindowSizeChange();
  290. }
  291. void SDL_WinRTApp::SetWindow(CoreWindow^ window)
  292. {
  293. #if LOG_WINDOW_EVENTS==1
  294. SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, window Size={%f,%f}\n",
  295. __FUNCTION__,
  296. WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  297. WINRT_DISPLAY_PROPERTY(NativeOrientation),
  298. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
  299. window->Bounds.Width,
  300. window->Bounds.Height);
  301. #endif
  302. window->SizeChanged +=
  303. ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &SDL_WinRTApp::OnWindowSizeChanged);
  304. window->VisibilityChanged +=
  305. ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &SDL_WinRTApp::OnVisibilityChanged);
  306. window->Closed +=
  307. ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &SDL_WinRTApp::OnWindowClosed);
  308. #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
  309. window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
  310. #endif
  311. window->PointerPressed +=
  312. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerPressed);
  313. window->PointerMoved +=
  314. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerMoved);
  315. window->PointerReleased +=
  316. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerReleased);
  317. window->PointerWheelChanged +=
  318. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerWheelChanged);
  319. #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
  320. // Retrieves relative-only mouse movements:
  321. Windows::Devices::Input::MouseDevice::GetForCurrentView()->MouseMoved +=
  322. ref new TypedEventHandler<MouseDevice^, MouseEventArgs^>(this, &SDL_WinRTApp::OnMouseMoved);
  323. #endif
  324. window->KeyDown +=
  325. ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyDown);
  326. window->KeyUp +=
  327. ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyUp);
  328. window->CharacterReceived +=
  329. ref new TypedEventHandler<CoreWindow^, CharacterReceivedEventArgs^>(this, &SDL_WinRTApp::OnCharacterReceived);
  330. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  331. HardwareButtons::BackPressed +=
  332. ref new EventHandler<BackPressedEventArgs^>(this, &SDL_WinRTApp::OnBackButtonPressed);
  333. #endif
  334. #if NTDDI_VERSION > NTDDI_WIN8
  335. DisplayInformation::GetForCurrentView()->OrientationChanged +=
  336. ref new TypedEventHandler<Windows::Graphics::Display::DisplayInformation^, Object^>(this, &SDL_WinRTApp::OnOrientationChanged);
  337. #else
  338. DisplayProperties::OrientationChanged +=
  339. ref new DisplayPropertiesEventHandler(this, &SDL_WinRTApp::OnOrientationChanged);
  340. #endif
  341. // Register the hint, SDL_HINT_ORIENTATIONS, with SDL.
  342. // 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.
  343. SDL_AddHintCallback(SDL_HINT_ORIENTATIONS, WINRT_SetDisplayOrientationsPreference, NULL);
  344. #if WINAPI_FAMILY == WINAPI_FAMILY_APP // for Windows 8/8.1/RT apps... (and not Phone apps)
  345. // Make sure we know when a user has opened the app's settings pane.
  346. // This is needed in order to display a privacy policy, which needs
  347. // to be done for network-enabled apps, as per Windows Store requirements.
  348. using namespace Windows::UI::ApplicationSettings;
  349. SettingsPane::GetForCurrentView()->CommandsRequested +=
  350. ref new TypedEventHandler<SettingsPane^, SettingsPaneCommandsRequestedEventArgs^>
  351. (this, &SDL_WinRTApp::OnSettingsPaneCommandsRequested);
  352. #endif
  353. }
  354. void SDL_WinRTApp::Load(Platform::String^ entryPoint)
  355. {
  356. }
  357. void SDL_WinRTApp::Run()
  358. {
  359. SDL_SetMainReady();
  360. if (WINRT_SDLAppEntryPoint)
  361. {
  362. // TODO, WinRT: pass the C-style main() a reasonably realistic
  363. // representation of command line arguments.
  364. int argc = 0;
  365. char **argv = NULL;
  366. WINRT_SDLAppEntryPoint(argc, argv);
  367. }
  368. }
  369. static bool IsSDLWindowEventPending(SDL_WindowEventID windowEventID)
  370. {
  371. SDL_Event events[128];
  372. const int count = SDL_PeepEvents(events, sizeof(events)/sizeof(SDL_Event), SDL_PEEKEVENT, SDL_WINDOWEVENT, SDL_WINDOWEVENT);
  373. for (int i = 0; i < count; ++i) {
  374. if (events[i].window.event == windowEventID) {
  375. return true;
  376. }
  377. }
  378. return false;
  379. }
  380. bool SDL_WinRTApp::ShouldWaitForAppResumeEvents()
  381. {
  382. /* Don't wait if the app is visible: */
  383. if (m_windowVisible) {
  384. return false;
  385. }
  386. /* Don't wait until the window-hide events finish processing.
  387. * Do note that if an app-suspend event is sent (as indicated
  388. * by SDL_APP_WILLENTERBACKGROUND and SDL_APP_DIDENTERBACKGROUND
  389. * events), then this code may be a moot point, as WinRT's
  390. * own event pump (aka ProcessEvents()) will pause regardless
  391. * of what we do here. This happens on Windows Phone 8, to note.
  392. * Windows 8.x apps, on the other hand, may get a chance to run
  393. * these.
  394. */
  395. if (IsSDLWindowEventPending(SDL_WINDOWEVENT_HIDDEN)) {
  396. return false;
  397. } else if (IsSDLWindowEventPending(SDL_WINDOWEVENT_FOCUS_LOST)) {
  398. return false;
  399. } else if (IsSDLWindowEventPending(SDL_WINDOWEVENT_MINIMIZED)) {
  400. return false;
  401. }
  402. return true;
  403. }
  404. void SDL_WinRTApp::PumpEvents()
  405. {
  406. if (!m_windowClosed) {
  407. if (!ShouldWaitForAppResumeEvents()) {
  408. /* This is the normal way in which events should be pumped.
  409. * 'ProcessAllIfPresent' will make ProcessEvents() process anywhere
  410. * from zero to N events, and will then return.
  411. */
  412. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
  413. } else {
  414. /* This style of event-pumping, with 'ProcessOneAndAllPending',
  415. * will cause anywhere from one to N events to be processed. If
  416. * at least one event is processed, the call will return. If
  417. * no events are pending, then the call will wait until one is
  418. * available, and will not return (to the caller) until this
  419. * happens! This should only occur when the app is hidden.
  420. */
  421. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
  422. }
  423. }
  424. }
  425. void SDL_WinRTApp::Uninitialize()
  426. {
  427. }
  428. #if WINAPI_FAMILY == WINAPI_FAMILY_APP
  429. void SDL_WinRTApp::OnSettingsPaneCommandsRequested(
  430. Windows::UI::ApplicationSettings::SettingsPane ^p,
  431. Windows::UI::ApplicationSettings::SettingsPaneCommandsRequestedEventArgs ^args)
  432. {
  433. using namespace Platform;
  434. using namespace Windows::UI::ApplicationSettings;
  435. using namespace Windows::UI::Popups;
  436. String ^privacyPolicyURL = nullptr; // a URL to an app's Privacy Policy
  437. String ^privacyPolicyLabel = nullptr; // label/link text
  438. const char *tmpHintValue = NULL; // SDL_GetHint-retrieved value, used immediately
  439. wchar_t *tmpStr = NULL; // used for UTF8 to UCS2 conversion
  440. // Setup a 'Privacy Policy' link, if one is available (via SDL_GetHint):
  441. tmpHintValue = SDL_GetHint(SDL_HINT_WINRT_PRIVACY_POLICY_URL);
  442. if (tmpHintValue && tmpHintValue[0] != '\0') {
  443. // Convert the privacy policy's URL to UCS2:
  444. tmpStr = WIN_UTF8ToString(tmpHintValue);
  445. privacyPolicyURL = ref new String(tmpStr);
  446. SDL_free(tmpStr);
  447. // Optionally retrieve custom label-text for the link. If this isn't
  448. // available, a default value will be used instead.
  449. tmpHintValue = SDL_GetHint(SDL_HINT_WINRT_PRIVACY_POLICY_LABEL);
  450. if (tmpHintValue && tmpHintValue[0] != '\0') {
  451. tmpStr = WIN_UTF8ToString(tmpHintValue);
  452. privacyPolicyLabel = ref new String(tmpStr);
  453. SDL_free(tmpStr);
  454. } else {
  455. privacyPolicyLabel = ref new String(L"Privacy Policy");
  456. }
  457. // Register the link, along with a handler to be called if and when it is
  458. // clicked:
  459. auto cmd = ref new SettingsCommand(L"privacyPolicy", privacyPolicyLabel,
  460. ref new UICommandInvokedHandler([=](IUICommand ^) {
  461. Windows::System::Launcher::LaunchUriAsync(ref new Uri(privacyPolicyURL));
  462. }));
  463. args->Request->ApplicationCommands->Append(cmd);
  464. }
  465. }
  466. #endif // if WINAPI_FAMILY == WINAPI_FAMILY_APP
  467. void SDL_WinRTApp::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
  468. {
  469. #if LOG_WINDOW_EVENTS==1
  470. SDL_Log("%s, size={%f,%f}, current orientation=%d, native orientation=%d, auto rot. pref=%d, WINRT_GlobalSDLWindow?=%s\n",
  471. __FUNCTION__,
  472. args->Size.Width, args->Size.Height,
  473. WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  474. WINRT_DISPLAY_PROPERTY(NativeOrientation),
  475. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
  476. (WINRT_GlobalSDLWindow ? "yes" : "no"));
  477. #endif
  478. WINRT_ProcessWindowSizeChange();
  479. }
  480. void SDL_WinRTApp::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
  481. {
  482. #if LOG_WINDOW_EVENTS==1
  483. SDL_Log("%s, visible?=%s, WINRT_GlobalSDLWindow?=%s\n",
  484. __FUNCTION__,
  485. (args->Visible ? "yes" : "no"),
  486. (WINRT_GlobalSDLWindow ? "yes" : "no"));
  487. #endif
  488. m_windowVisible = args->Visible;
  489. if (WINRT_GlobalSDLWindow) {
  490. SDL_bool wasSDLWindowSurfaceValid = WINRT_GlobalSDLWindow->surface_valid;
  491. if (args->Visible) {
  492. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_SHOWN, 0, 0);
  493. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
  494. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_RESTORED, 0, 0);
  495. } else {
  496. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_HIDDEN, 0, 0);
  497. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
  498. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
  499. }
  500. // HACK: Prevent SDL's window-hide handling code, which currently
  501. // triggers a fake window resize (possibly erronously), from
  502. // marking the SDL window's surface as invalid.
  503. //
  504. // A better solution to this probably involves figuring out if the
  505. // fake window resize can be prevented.
  506. WINRT_GlobalSDLWindow->surface_valid = wasSDLWindowSurfaceValid;
  507. }
  508. }
  509. void SDL_WinRTApp::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
  510. {
  511. #if LOG_WINDOW_EVENTS==1
  512. SDL_Log("%s\n", __FUNCTION__);
  513. #endif
  514. m_windowClosed = true;
  515. }
  516. void SDL_WinRTApp::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
  517. {
  518. CoreWindow::GetForCurrentThread()->Activate();
  519. }
  520. void SDL_WinRTApp::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
  521. {
  522. // Save app state asynchronously after requesting a deferral. Holding a deferral
  523. // indicates that the application is busy performing suspending operations. Be
  524. // aware that a deferral may not be held indefinitely. After about five seconds,
  525. // the app will be forced to exit.
  526. // ... but first, let the app know it's about to go to the background.
  527. // The separation of events may be important, given that the deferral
  528. // runs in a separate thread. This'll make SDL_APP_WILLENTERBACKGROUND
  529. // the only event among the two that runs in the main thread. Given
  530. // that a few WinRT operations can only be done from the main thread
  531. // (things that access the WinRT CoreWindow are one example of this),
  532. // this could be important.
  533. SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
  534. SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
  535. create_task([this, deferral]()
  536. {
  537. // Send an app did-enter-background event immediately to observers.
  538. // CoreDispatcher::ProcessEvents, which is the backbone on which
  539. // SDL_WinRTApp::PumpEvents is built, will not return to its caller
  540. // once it sends out a suspend event. Any events posted to SDL's
  541. // event queue won't get received until the WinRT app is resumed.
  542. // SDL_AddEventWatch() may be used to receive app-suspend events on
  543. // WinRT.
  544. SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
  545. // Let the Direct3D 11 renderer prepare for the app to be backgrounded.
  546. // This is necessary for Windows 8.1, possibly elsewhere in the future.
  547. // More details at: http://msdn.microsoft.com/en-us/library/windows/apps/Hh994929.aspx
  548. #if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
  549. if (WINRT_GlobalSDLWindow) {
  550. SDL_Renderer * renderer = SDL_GetRenderer(WINRT_GlobalSDLWindow);
  551. if (renderer && (SDL_strcmp(renderer->info.name, "direct3d11") == 0)) {
  552. D3D11_Trim(renderer);
  553. }
  554. }
  555. #endif
  556. deferral->Complete();
  557. });
  558. }
  559. void SDL_WinRTApp::OnResuming(Platform::Object^ sender, Platform::Object^ args)
  560. {
  561. // Restore any data or state that was unloaded on suspend. By default, data
  562. // and state are persisted when resuming from suspend. Note that these events
  563. // do not occur if the app was previously terminated.
  564. SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
  565. SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
  566. }
  567. void SDL_WinRTApp::OnExiting(Platform::Object^ sender, Platform::Object^ args)
  568. {
  569. SDL_SendAppEvent(SDL_APP_TERMINATING);
  570. }
  571. static void
  572. WINRT_LogPointerEvent(const char * header, Windows::UI::Core::PointerEventArgs ^ args, Windows::Foundation::Point transformedPoint)
  573. {
  574. Windows::UI::Input::PointerPoint ^ pt = args->CurrentPoint;
  575. SDL_Log("%s: Position={%f,%f}, Transformed Pos={%f, %f}, MouseWheelDelta=%d, FrameId=%d, PointerId=%d, SDL button=%d\n",
  576. header,
  577. pt->Position.X, pt->Position.Y,
  578. transformedPoint.X, transformedPoint.Y,
  579. pt->Properties->MouseWheelDelta,
  580. pt->FrameId,
  581. pt->PointerId,
  582. WINRT_GetSDLButtonForPointerPoint(pt));
  583. }
  584. void SDL_WinRTApp::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
  585. {
  586. #if LOG_POINTER_EVENTS
  587. WINRT_LogPointerEvent("pointer pressed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  588. #endif
  589. WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  590. }
  591. void SDL_WinRTApp::OnPointerMoved(CoreWindow^ sender, PointerEventArgs^ args)
  592. {
  593. #if LOG_POINTER_EVENTS
  594. WINRT_LogPointerEvent("pointer moved", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  595. #endif
  596. WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  597. }
  598. void SDL_WinRTApp::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
  599. {
  600. #if LOG_POINTER_EVENTS
  601. WINRT_LogPointerEvent("pointer released", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  602. #endif
  603. WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  604. }
  605. void SDL_WinRTApp::OnPointerWheelChanged(CoreWindow^ sender, PointerEventArgs^ args)
  606. {
  607. #if LOG_POINTER_EVENTS
  608. WINRT_LogPointerEvent("pointer wheel changed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  609. #endif
  610. WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  611. }
  612. void SDL_WinRTApp::OnMouseMoved(MouseDevice^ mouseDevice, MouseEventArgs^ args)
  613. {
  614. WINRT_ProcessMouseMovedEvent(WINRT_GlobalSDLWindow, args);
  615. }
  616. void SDL_WinRTApp::OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
  617. {
  618. WINRT_ProcessKeyDownEvent(args);
  619. }
  620. void SDL_WinRTApp::OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
  621. {
  622. WINRT_ProcessKeyUpEvent(args);
  623. }
  624. void SDL_WinRTApp::OnCharacterReceived(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CharacterReceivedEventArgs^ args)
  625. {
  626. WINRT_ProcessCharacterReceivedEvent(args);
  627. }
  628. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  629. void SDL_WinRTApp::OnBackButtonPressed(Platform::Object^ sender, Windows::Phone::UI::Input::BackPressedEventArgs^ args)
  630. {
  631. SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_AC_BACK);
  632. SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_AC_BACK);
  633. const char *hint = SDL_GetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON);
  634. if (hint) {
  635. if (*hint == '1') {
  636. args->Handled = true;
  637. }
  638. }
  639. }
  640. #endif