SDL_winrtapp_direct3d.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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_events.h"
  42. #include "SDL_hints.h"
  43. #include "SDL_main.h"
  44. #include "SDL_stdinc.h"
  45. #include "SDL_render.h"
  46. #include "../../video/SDL_sysvideo.h"
  47. //#include "../../SDL_hints_c.h"
  48. #include "../../events/SDL_events_c.h"
  49. #include "../../events/SDL_keyboard_c.h"
  50. #include "../../events/SDL_mouse_c.h"
  51. #include "../../events/SDL_windowevents_c.h"
  52. #include "../../render/SDL_sysrender.h"
  53. #include "../windows/SDL_windows.h"
  54. }
  55. #include "../../video/winrt/SDL_winrtevents_c.h"
  56. #include "../../video/winrt/SDL_winrtvideo_cpp.h"
  57. #include "SDL_winrtapp_common.h"
  58. #include "SDL_winrtapp_direct3d.h"
  59. #if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
  60. /* Calling IDXGIDevice3::Trim on the active Direct3D 11.x device is necessary
  61. * when Windows 8.1 apps are about to get suspended.
  62. */
  63. extern "C" void D3D11_Trim(SDL_Renderer *);
  64. #endif
  65. // Compile-time debugging options:
  66. // To enable, uncomment; to disable, comment them out.
  67. //#define LOG_POINTER_EVENTS 1
  68. //#define LOG_WINDOW_EVENTS 1
  69. //#define LOG_ORIENTATION_EVENTS 1
  70. // HACK, DLudwig: record a reference to the global, WinRT 'app'/view.
  71. // SDL/WinRT will use this throughout its code.
  72. //
  73. // TODO, WinRT: consider replacing SDL_WinRTGlobalApp with something
  74. // non-global, such as something created inside
  75. // SDL_InitSubSystem(SDL_INIT_VIDEO), or something inside
  76. // SDL_CreateWindow().
  77. SDL_WinRTApp ^ SDL_WinRTGlobalApp = nullptr;
  78. ref class SDLApplicationSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource
  79. {
  80. public:
  81. virtual Windows::ApplicationModel::Core::IFrameworkView^ CreateView();
  82. };
  83. IFrameworkView^ SDLApplicationSource::CreateView()
  84. {
  85. // TODO, WinRT: see if this function (CreateView) can ever get called
  86. // more than once. For now, just prevent it from ever assigning
  87. // SDL_WinRTGlobalApp more than once.
  88. SDL_assert(!SDL_WinRTGlobalApp);
  89. SDL_WinRTApp ^ app = ref new SDL_WinRTApp();
  90. if (!SDL_WinRTGlobalApp)
  91. {
  92. SDL_WinRTGlobalApp = app;
  93. }
  94. return app;
  95. }
  96. int SDL_WinRTInitNonXAMLApp(int (*mainFunction)(int, char **))
  97. {
  98. WINRT_SDLAppEntryPoint = mainFunction;
  99. auto direct3DApplicationSource = ref new SDLApplicationSource();
  100. CoreApplication::Run(direct3DApplicationSource);
  101. return 0;
  102. }
  103. static void SDLCALL
  104. WINRT_SetDisplayOrientationsPreference(void *userdata, const char *name, const char *oldValue, const char *newValue)
  105. {
  106. SDL_assert(SDL_strcmp(name, SDL_HINT_ORIENTATIONS) == 0);
  107. /* HACK: prevent SDL from altering an app's .appxmanifest-set orientation
  108. * from being changed on startup, by detecting when SDL_HINT_ORIENTATIONS
  109. * is getting registered.
  110. *
  111. * TODO, WinRT: consider reading in an app's .appxmanifest file, and apply its orientation when 'newValue == NULL'.
  112. */
  113. if ((oldValue == NULL) && (newValue == NULL)) {
  114. return;
  115. }
  116. // Start with no orientation flags, then add each in as they're parsed
  117. // from newValue.
  118. unsigned int orientationFlags = 0;
  119. if (newValue) {
  120. std::istringstream tokenizer(newValue);
  121. while (!tokenizer.eof()) {
  122. std::string orientationName;
  123. std::getline(tokenizer, orientationName, ' ');
  124. if (orientationName == "LandscapeLeft") {
  125. orientationFlags |= (unsigned int) DisplayOrientations::LandscapeFlipped;
  126. } else if (orientationName == "LandscapeRight") {
  127. orientationFlags |= (unsigned int) DisplayOrientations::Landscape;
  128. } else if (orientationName == "Portrait") {
  129. orientationFlags |= (unsigned int) DisplayOrientations::Portrait;
  130. } else if (orientationName == "PortraitUpsideDown") {
  131. orientationFlags |= (unsigned int) DisplayOrientations::PortraitFlipped;
  132. }
  133. }
  134. }
  135. // If no valid orientation flags were specified, use a reasonable set of defaults:
  136. if (!orientationFlags) {
  137. // TODO, WinRT: consider seeing if an app's default orientation flags can be found out via some API call(s).
  138. orientationFlags = (unsigned int) ( \
  139. DisplayOrientations::Landscape |
  140. DisplayOrientations::LandscapeFlipped |
  141. DisplayOrientations::Portrait |
  142. DisplayOrientations::PortraitFlipped);
  143. }
  144. // Set the orientation/rotation preferences. Please note that this does
  145. // not constitute a 100%-certain lock of a given set of possible
  146. // orientations. According to Microsoft's documentation on WinRT [1]
  147. // when a device is not capable of being rotated, Windows may ignore
  148. // the orientation preferences, and stick to what the device is capable of
  149. // displaying.
  150. //
  151. // [1] Documentation on the 'InitialRotationPreference' setting for a
  152. // Windows app's manifest file describes how some orientation/rotation
  153. // preferences may be ignored. See
  154. // http://msdn.microsoft.com/en-us/library/windows/apps/hh700343.aspx
  155. // for details. Microsoft's "Display orientation sample" also gives an
  156. // outline of how Windows treats device rotation
  157. // (http://code.msdn.microsoft.com/Display-Orientation-Sample-19a58e93).
  158. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences) = (DisplayOrientations) orientationFlags;
  159. }
  160. static void
  161. WINRT_ProcessWindowSizeChange() // TODO: Pass an SDL_Window-identifying thing into WINRT_ProcessWindowSizeChange()
  162. {
  163. CoreWindow ^ coreWindow = CoreWindow::GetForCurrentThread();
  164. if (coreWindow) {
  165. if (WINRT_GlobalSDLWindow) {
  166. SDL_Window * window = WINRT_GlobalSDLWindow;
  167. SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
  168. int x = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Left);
  169. int y = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Top);
  170. int w = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Width);
  171. int h = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Height);
  172. #if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP) && (NTDDI_VERSION == NTDDI_WIN8)
  173. /* WinPhone 8.0 always keeps its native window size in portrait,
  174. regardless of orientation. This changes in WinPhone 8.1,
  175. in which the native window's size changes along with
  176. orientation.
  177. Attempt to emulate WinPhone 8.1's behavior on WinPhone 8.0, with
  178. regards to window size. This fixes a rendering bug that occurs
  179. when a WinPhone 8.0 app is rotated to either 90 or 270 degrees.
  180. */
  181. const DisplayOrientations currentOrientation = WINRT_DISPLAY_PROPERTY(CurrentOrientation);
  182. switch (currentOrientation) {
  183. case DisplayOrientations::Landscape:
  184. case DisplayOrientations::LandscapeFlipped: {
  185. int tmp = w;
  186. w = h;
  187. h = tmp;
  188. } break;
  189. }
  190. #endif
  191. const Uint32 latestFlags = WINRT_DetectWindowFlags(window);
  192. if (latestFlags & SDL_WINDOW_MAXIMIZED) {
  193. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
  194. } else {
  195. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
  196. }
  197. WINRT_UpdateWindowFlags(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
  198. /* The window can move during a resize event, such as when maximizing
  199. or resizing from a corner */
  200. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
  201. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, w, h);
  202. }
  203. }
  204. }
  205. SDL_WinRTApp::SDL_WinRTApp() :
  206. m_windowClosed(false),
  207. m_windowVisible(true)
  208. {
  209. }
  210. void SDL_WinRTApp::Initialize(CoreApplicationView^ applicationView)
  211. {
  212. applicationView->Activated +=
  213. ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &SDL_WinRTApp::OnAppActivated);
  214. CoreApplication::Suspending +=
  215. ref new EventHandler<SuspendingEventArgs^>(this, &SDL_WinRTApp::OnSuspending);
  216. CoreApplication::Resuming +=
  217. ref new EventHandler<Platform::Object^>(this, &SDL_WinRTApp::OnResuming);
  218. CoreApplication::Exiting +=
  219. ref new EventHandler<Platform::Object^>(this, &SDL_WinRTApp::OnExiting);
  220. #if NTDDI_VERSION >= NTDDI_WIN10
  221. /* HACK ALERT! Xbox One doesn't seem to detect gamepads unless something
  222. gets registered to receive Win10's Windows.Gaming.Input.Gamepad.GamepadAdded
  223. events. We'll register an event handler for these events here, to make
  224. sure that gamepad detection works later on, if requested.
  225. */
  226. Windows::Gaming::Input::Gamepad::GamepadAdded +=
  227. ref new Windows::Foundation::EventHandler<Windows::Gaming::Input::Gamepad^>(
  228. this, &SDL_WinRTApp::OnGamepadAdded
  229. );
  230. #endif
  231. }
  232. #if NTDDI_VERSION > NTDDI_WIN8
  233. void SDL_WinRTApp::OnOrientationChanged(DisplayInformation^ sender, Object^ args)
  234. #else
  235. void SDL_WinRTApp::OnOrientationChanged(Object^ sender)
  236. #endif
  237. {
  238. #if LOG_ORIENTATION_EVENTS==1
  239. {
  240. CoreWindow^ window = CoreWindow::GetForCurrentThread();
  241. if (window) {
  242. SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, CoreWindow Bounds={%f,%f,%f,%f}\n",
  243. __FUNCTION__,
  244. WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  245. WINRT_DISPLAY_PROPERTY(NativeOrientation),
  246. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
  247. window->Bounds.X,
  248. window->Bounds.Y,
  249. window->Bounds.Width,
  250. window->Bounds.Height);
  251. } else {
  252. SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d\n",
  253. __FUNCTION__,
  254. WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  255. WINRT_DISPLAY_PROPERTY(NativeOrientation),
  256. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences));
  257. }
  258. }
  259. #endif
  260. WINRT_ProcessWindowSizeChange();
  261. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  262. // HACK: Make sure that orientation changes
  263. // lead to the Direct3D renderer's viewport getting updated:
  264. //
  265. // For some reason, this doesn't seem to need to be done on Windows 8.x,
  266. // even when going from Landscape to LandscapeFlipped. It only seems to
  267. // be needed on Windows Phone, at least when I tested on my devices.
  268. // I'm not currently sure why this is, but it seems to work fine. -- David L.
  269. //
  270. // 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
  271. SDL_Window * window = WINRT_GlobalSDLWindow;
  272. if (window) {
  273. SDL_WindowData * data = (SDL_WindowData *)window->driverdata;
  274. int w = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Width);
  275. int h = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Height);
  276. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_SIZE_CHANGED, w, h);
  277. }
  278. #endif
  279. }
  280. void SDL_WinRTApp::SetWindow(CoreWindow^ window)
  281. {
  282. #if LOG_WINDOW_EVENTS==1
  283. SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, window bounds={%f, %f, %f,%f}\n",
  284. __FUNCTION__,
  285. WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  286. WINRT_DISPLAY_PROPERTY(NativeOrientation),
  287. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
  288. window->Bounds.X,
  289. window->Bounds.Y,
  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->Activated +=
  298. ref new TypedEventHandler<CoreWindow^, WindowActivatedEventArgs^>(this, &SDL_WinRTApp::OnWindowActivated);
  299. window->Closed +=
  300. ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &SDL_WinRTApp::OnWindowClosed);
  301. #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
  302. window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
  303. #endif
  304. window->PointerPressed +=
  305. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerPressed);
  306. window->PointerMoved +=
  307. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerMoved);
  308. window->PointerReleased +=
  309. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerReleased);
  310. window->PointerEntered +=
  311. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerEntered);
  312. window->PointerExited +=
  313. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerExited);
  314. window->PointerWheelChanged +=
  315. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerWheelChanged);
  316. #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
  317. // Retrieves relative-only mouse movements:
  318. Windows::Devices::Input::MouseDevice::GetForCurrentView()->MouseMoved +=
  319. ref new TypedEventHandler<MouseDevice^, MouseEventArgs^>(this, &SDL_WinRTApp::OnMouseMoved);
  320. #endif
  321. window->KeyDown +=
  322. ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyDown);
  323. window->KeyUp +=
  324. ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyUp);
  325. window->CharacterReceived +=
  326. ref new TypedEventHandler<CoreWindow^, CharacterReceivedEventArgs^>(this, &SDL_WinRTApp::OnCharacterReceived);
  327. #if NTDDI_VERSION >= NTDDI_WIN10
  328. Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->BackRequested +=
  329. ref new EventHandler<BackRequestedEventArgs^>(this, &SDL_WinRTApp::OnBackButtonPressed);
  330. #elif 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) && (NTDDI_VERSION < NTDDI_WIN10) // 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) && (NTDDI_VERSION < NTDDI_WIN10)
  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) && (NTDDI_VERSION < NTDDI_WIN10)
  467. void SDL_WinRTApp::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
  468. {
  469. #if LOG_WINDOW_EVENTS==1
  470. SDL_Log("%s, size={%f,%f}, bounds={%f,%f,%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. sender->Bounds.X, sender->Bounds.Y, sender->Bounds.Width, sender->Bounds.Height,
  474. WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  475. WINRT_DISPLAY_PROPERTY(NativeOrientation),
  476. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
  477. (WINRT_GlobalSDLWindow ? "yes" : "no"));
  478. #endif
  479. WINRT_ProcessWindowSizeChange();
  480. }
  481. void SDL_WinRTApp::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
  482. {
  483. #if LOG_WINDOW_EVENTS==1
  484. SDL_Log("%s, visible?=%s, bounds={%f,%f,%f,%f}, WINRT_GlobalSDLWindow?=%s\n",
  485. __FUNCTION__,
  486. (args->Visible ? "yes" : "no"),
  487. sender->Bounds.X, sender->Bounds.Y,
  488. sender->Bounds.Width, sender->Bounds.Height,
  489. (WINRT_GlobalSDLWindow ? "yes" : "no"));
  490. #endif
  491. m_windowVisible = args->Visible;
  492. if (WINRT_GlobalSDLWindow) {
  493. SDL_bool wasSDLWindowSurfaceValid = WINRT_GlobalSDLWindow->surface_valid;
  494. Uint32 latestWindowFlags = WINRT_DetectWindowFlags(WINRT_GlobalSDLWindow);
  495. if (args->Visible) {
  496. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_SHOWN, 0, 0);
  497. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
  498. if (latestWindowFlags & SDL_WINDOW_MAXIMIZED) {
  499. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
  500. } else {
  501. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_RESTORED, 0, 0);
  502. }
  503. } else {
  504. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_HIDDEN, 0, 0);
  505. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
  506. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
  507. }
  508. // HACK: Prevent SDL's window-hide handling code, which currently
  509. // triggers a fake window resize (possibly erronously), from
  510. // marking the SDL window's surface as invalid.
  511. //
  512. // A better solution to this probably involves figuring out if the
  513. // fake window resize can be prevented.
  514. WINRT_GlobalSDLWindow->surface_valid = wasSDLWindowSurfaceValid;
  515. }
  516. }
  517. void SDL_WinRTApp::OnWindowActivated(CoreWindow^ sender, WindowActivatedEventArgs^ args)
  518. {
  519. #if LOG_WINDOW_EVENTS==1
  520. SDL_Log("%s, WINRT_GlobalSDLWindow?=%s\n\n",
  521. __FUNCTION__,
  522. (WINRT_GlobalSDLWindow ? "yes" : "no"));
  523. #endif
  524. /* There's no property in Win 8.x to tell whether a window is active or
  525. not. [De]activation events are, however, sent to the app. We'll just
  526. record those, in case the CoreWindow gets wrapped by an SDL_Window at
  527. some future time.
  528. */
  529. sender->CustomProperties->Insert("SDLHelperWindowActivationState", args->WindowActivationState);
  530. SDL_Window * window = WINRT_GlobalSDLWindow;
  531. if (window) {
  532. if (args->WindowActivationState != CoreWindowActivationState::Deactivated) {
  533. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
  534. if (SDL_GetKeyboardFocus() != window) {
  535. SDL_SetKeyboardFocus(window);
  536. }
  537. /* Send a mouse-motion event as appropriate.
  538. This doesn't work when called from OnPointerEntered, at least
  539. not in WinRT CoreWindow apps (as OnPointerEntered doesn't
  540. appear to be called after window-reactivation, at least not
  541. in Windows 10, Build 10586.3 (November 2015 update, non-beta).
  542. Don't do it on WinPhone 8.0 though, as CoreWindow's 'PointerPosition'
  543. property isn't available.
  544. */
  545. #if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION >= NTDDI_WINBLUE)
  546. Point cursorPos = WINRT_TransformCursorPosition(window, sender->PointerPosition, TransformToSDLWindowSize);
  547. SDL_SendMouseMotion(window, 0, 0, (int)cursorPos.X, (int)cursorPos.Y);
  548. #endif
  549. /* TODO, WinRT: see if the Win32 bugfix from https://hg.libsdl.org/SDL/rev/d278747da408 needs to be applied (on window activation) */
  550. //WIN_CheckAsyncMouseRelease(data);
  551. /* TODO, WinRT: implement clipboard support, if possible */
  552. ///*
  553. // * FIXME: Update keyboard state
  554. // */
  555. //WIN_CheckClipboardUpdate(data->videodata);
  556. // HACK: Resetting the mouse-cursor here seems to fix
  557. // https://bugzilla.libsdl.org/show_bug.cgi?id=3217, whereby a
  558. // WinRT app's mouse cursor may switch to Windows' 'wait' cursor,
  559. // after a user alt-tabs back into a full-screened SDL app.
  560. // This bug does not appear to reproduce 100% of the time.
  561. // It may be a bug in Windows itself (v.10.0.586.36, as tested,
  562. // and the most-recent as of this writing).
  563. SDL_SetCursor(NULL);
  564. } else {
  565. if (SDL_GetKeyboardFocus() == window) {
  566. SDL_SetKeyboardFocus(NULL);
  567. }
  568. }
  569. }
  570. }
  571. void SDL_WinRTApp::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
  572. {
  573. #if LOG_WINDOW_EVENTS==1
  574. SDL_Log("%s\n", __FUNCTION__);
  575. #endif
  576. m_windowClosed = true;
  577. }
  578. void SDL_WinRTApp::OnAppActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
  579. {
  580. CoreWindow::GetForCurrentThread()->Activate();
  581. }
  582. void SDL_WinRTApp::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
  583. {
  584. // Save app state asynchronously after requesting a deferral. Holding a deferral
  585. // indicates that the application is busy performing suspending operations. Be
  586. // aware that a deferral may not be held indefinitely. After about five seconds,
  587. // the app will be forced to exit.
  588. // ... but first, let the app know it's about to go to the background.
  589. // The separation of events may be important, given that the deferral
  590. // runs in a separate thread. This'll make SDL_APP_WILLENTERBACKGROUND
  591. // the only event among the two that runs in the main thread. Given
  592. // that a few WinRT operations can only be done from the main thread
  593. // (things that access the WinRT CoreWindow are one example of this),
  594. // this could be important.
  595. SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
  596. SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
  597. create_task([this, deferral]()
  598. {
  599. // Send an app did-enter-background event immediately to observers.
  600. // CoreDispatcher::ProcessEvents, which is the backbone on which
  601. // SDL_WinRTApp::PumpEvents is built, will not return to its caller
  602. // once it sends out a suspend event. Any events posted to SDL's
  603. // event queue won't get received until the WinRT app is resumed.
  604. // SDL_AddEventWatch() may be used to receive app-suspend events on
  605. // WinRT.
  606. SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
  607. // Let the Direct3D 11 renderer prepare for the app to be backgrounded.
  608. // This is necessary for Windows 8.1, possibly elsewhere in the future.
  609. // More details at: http://msdn.microsoft.com/en-us/library/windows/apps/Hh994929.aspx
  610. #if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
  611. if (WINRT_GlobalSDLWindow) {
  612. SDL_Renderer * renderer = SDL_GetRenderer(WINRT_GlobalSDLWindow);
  613. if (renderer && (SDL_strcmp(renderer->info.name, "direct3d11") == 0)) {
  614. D3D11_Trim(renderer);
  615. }
  616. }
  617. #endif
  618. deferral->Complete();
  619. });
  620. }
  621. void SDL_WinRTApp::OnResuming(Platform::Object^ sender, Platform::Object^ args)
  622. {
  623. // Restore any data or state that was unloaded on suspend. By default, data
  624. // and state are persisted when resuming from suspend. Note that these events
  625. // do not occur if the app was previously terminated.
  626. SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
  627. SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
  628. }
  629. void SDL_WinRTApp::OnExiting(Platform::Object^ sender, Platform::Object^ args)
  630. {
  631. SDL_SendAppEvent(SDL_APP_TERMINATING);
  632. }
  633. static void
  634. WINRT_LogPointerEvent(const char * header, Windows::UI::Core::PointerEventArgs ^ args, Windows::Foundation::Point transformedPoint)
  635. {
  636. Windows::UI::Input::PointerPoint ^ pt = args->CurrentPoint;
  637. SDL_Log("%s: Position={%f,%f}, Transformed Pos={%f, %f}, MouseWheelDelta=%d, FrameId=%d, PointerId=%d, SDL button=%d\n",
  638. header,
  639. pt->Position.X, pt->Position.Y,
  640. transformedPoint.X, transformedPoint.Y,
  641. pt->Properties->MouseWheelDelta,
  642. pt->FrameId,
  643. pt->PointerId,
  644. WINRT_GetSDLButtonForPointerPoint(pt));
  645. }
  646. void SDL_WinRTApp::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
  647. {
  648. #if LOG_POINTER_EVENTS
  649. WINRT_LogPointerEvent("pointer pressed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  650. #endif
  651. WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  652. }
  653. void SDL_WinRTApp::OnPointerMoved(CoreWindow^ sender, PointerEventArgs^ args)
  654. {
  655. #if LOG_POINTER_EVENTS
  656. WINRT_LogPointerEvent("pointer moved", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  657. #endif
  658. WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  659. }
  660. void SDL_WinRTApp::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
  661. {
  662. #if LOG_POINTER_EVENTS
  663. WINRT_LogPointerEvent("pointer released", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  664. #endif
  665. WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  666. }
  667. void SDL_WinRTApp::OnPointerEntered(CoreWindow^ sender, PointerEventArgs^ args)
  668. {
  669. #if LOG_POINTER_EVENTS
  670. WINRT_LogPointerEvent("pointer entered", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  671. #endif
  672. WINRT_ProcessPointerEnteredEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  673. }
  674. void SDL_WinRTApp::OnPointerExited(CoreWindow^ sender, PointerEventArgs^ args)
  675. {
  676. #if LOG_POINTER_EVENTS
  677. WINRT_LogPointerEvent("pointer exited", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  678. #endif
  679. WINRT_ProcessPointerExitedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  680. }
  681. void SDL_WinRTApp::OnPointerWheelChanged(CoreWindow^ sender, PointerEventArgs^ args)
  682. {
  683. #if LOG_POINTER_EVENTS
  684. WINRT_LogPointerEvent("pointer wheel changed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  685. #endif
  686. WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  687. }
  688. void SDL_WinRTApp::OnMouseMoved(MouseDevice^ mouseDevice, MouseEventArgs^ args)
  689. {
  690. WINRT_ProcessMouseMovedEvent(WINRT_GlobalSDLWindow, args);
  691. }
  692. void SDL_WinRTApp::OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
  693. {
  694. WINRT_ProcessKeyDownEvent(args);
  695. }
  696. void SDL_WinRTApp::OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
  697. {
  698. WINRT_ProcessKeyUpEvent(args);
  699. }
  700. void SDL_WinRTApp::OnCharacterReceived(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CharacterReceivedEventArgs^ args)
  701. {
  702. WINRT_ProcessCharacterReceivedEvent(args);
  703. }
  704. template <typename BackButtonEventArgs>
  705. static void WINRT_OnBackButtonPressed(BackButtonEventArgs ^ args)
  706. {
  707. SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_AC_BACK);
  708. SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_AC_BACK);
  709. if (SDL_GetHintBoolean(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, SDL_FALSE)) {
  710. args->Handled = true;
  711. }
  712. }
  713. #if NTDDI_VERSION >= NTDDI_WIN10
  714. void SDL_WinRTApp::OnBackButtonPressed(Platform::Object^ sender, Windows::UI::Core::BackRequestedEventArgs^ args)
  715. {
  716. WINRT_OnBackButtonPressed(args);
  717. }
  718. #elif WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  719. void SDL_WinRTApp::OnBackButtonPressed(Platform::Object^ sender, Windows::Phone::UI::Input::BackPressedEventArgs^ args)
  720. {
  721. WINRT_OnBackButtonPressed(args);
  722. }
  723. #endif
  724. #if NTDDI_VERSION >= NTDDI_WIN10
  725. void SDL_WinRTApp::OnGamepadAdded(Platform::Object ^sender, Windows::Gaming::Input::Gamepad ^gamepad)
  726. {
  727. /* HACK ALERT: Nothing needs to be done here, as this method currently
  728. only exists to allow something to be registered with Win10's
  729. GamepadAdded event, an operation that seems to be necessary to get
  730. Xinput-based detection to work on Xbox One.
  731. */
  732. }
  733. #endif
  734. /* vi: set ts=4 sw=4 expandtab: */