SDL_winrtapp_direct3d.cpp 28 KB

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