SDL_winrtapp_direct3d.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  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. /* Windows includes */
  20. #include "ppltasks.h"
  21. using namespace concurrency;
  22. using namespace Windows::ApplicationModel;
  23. using namespace Windows::ApplicationModel::Core;
  24. using namespace Windows::ApplicationModel::Activation;
  25. using namespace Windows::Devices::Input;
  26. using namespace Windows::Graphics::Display;
  27. using namespace Windows::Foundation;
  28. using namespace Windows::System;
  29. using namespace Windows::UI::Core;
  30. using namespace Windows::UI::Input;
  31. #if SDL_WINAPI_FAMILY_PHONE
  32. using namespace Windows::Phone::UI::Input;
  33. #endif
  34. /* SDL includes */
  35. extern "C" {
  36. #include "../../video/SDL_sysvideo.h"
  37. #include "../../events/SDL_events_c.h"
  38. #include "../../events/SDL_keyboard_c.h"
  39. #include "../../events/SDL_mouse_c.h"
  40. #include "../../events/SDL_windowevents_c.h"
  41. #include "../../render/SDL_sysrender.h"
  42. #include "../windows/SDL_windows.h"
  43. }
  44. #include "../../video/winrt/SDL_winrtevents_c.h"
  45. #include "../../video/winrt/SDL_winrtvideo_cpp.h"
  46. #include "SDL_winrtapp_common.h"
  47. #include "SDL_winrtapp_direct3d.h"
  48. #if SDL_VIDEO_RENDER_D3D11
  49. /* Calling IDXGIDevice3::Trim on the active Direct3D 11.x device is necessary
  50. * when Windows 8.1 apps are about to get suspended.
  51. */
  52. extern "C" void D3D11_Trim(SDL_Renderer *);
  53. #endif
  54. // Compile-time debugging options:
  55. // To enable, uncomment; to disable, comment them out.
  56. // #define LOG_POINTER_EVENTS 1
  57. // #define LOG_WINDOW_EVENTS 1
  58. // #define LOG_ORIENTATION_EVENTS 1
  59. // HACK, DLudwig: record a reference to the global, WinRT 'app'/view.
  60. // SDL/WinRT will use this throughout its code.
  61. //
  62. // TODO, WinRT: consider replacing SDL_WinRTGlobalApp with something
  63. // non-global, such as something created inside
  64. // SDL_InitSubSystem(SDL_INIT_VIDEO), or something inside
  65. // SDL_CreateWindow().
  66. SDL_WinRTApp ^ SDL_WinRTGlobalApp = nullptr;
  67. ref class SDLApplicationSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource
  68. {
  69. public:
  70. virtual Windows::ApplicationModel::Core::IFrameworkView ^ CreateView();
  71. };
  72. IFrameworkView ^ SDLApplicationSource::CreateView()
  73. {
  74. // TODO, WinRT: see if this function (CreateView) can ever get called
  75. // more than once. For now, just prevent it from ever assigning
  76. // SDL_WinRTGlobalApp more than once.
  77. SDL_assert(!SDL_WinRTGlobalApp);
  78. SDL_WinRTApp ^ app = ref new SDL_WinRTApp();
  79. if (!SDL_WinRTGlobalApp) {
  80. SDL_WinRTGlobalApp = app;
  81. }
  82. return app;
  83. }
  84. int SDL_WinRTInitNonXAMLApp(int (*mainFunction)(int, char **))
  85. {
  86. WINRT_SDLAppEntryPoint = mainFunction;
  87. auto direct3DApplicationSource = ref new SDLApplicationSource();
  88. CoreApplication::Run(direct3DApplicationSource);
  89. return 0;
  90. }
  91. static void WINRT_ProcessWindowSizeChange() // TODO: Pass an SDL_Window-identifying thing into WINRT_ProcessWindowSizeChange()
  92. {
  93. CoreWindow ^ coreWindow = CoreWindow::GetForCurrentThread();
  94. if (coreWindow) {
  95. if (WINRT_GlobalSDLWindow) {
  96. SDL_Window *window = WINRT_GlobalSDLWindow;
  97. SDL_WindowData *data = window->driverdata;
  98. int x = (int)SDL_lroundf(data->coreWindow->Bounds.Left);
  99. int y = (int)SDL_lroundf(data->coreWindow->Bounds.Top);
  100. int w = (int)SDL_floorf(data->coreWindow->Bounds.Width);
  101. int h = (int)SDL_floorf(data->coreWindow->Bounds.Height);
  102. #if SDL_WINAPI_FAMILY_PHONE && NTDDI_VERSION == NTDDI_WIN8
  103. /* WinPhone 8.0 always keeps its native window size in portrait,
  104. regardless of orientation. This changes in WinPhone 8.1,
  105. in which the native window's size changes along with
  106. orientation.
  107. Attempt to emulate WinPhone 8.1's behavior on WinPhone 8.0, with
  108. regards to window size. This fixes a rendering bug that occurs
  109. when a WinPhone 8.0 app is rotated to either 90 or 270 degrees.
  110. */
  111. const DisplayOrientations currentOrientation = WINRT_DISPLAY_PROPERTY(CurrentOrientation);
  112. switch (currentOrientation) {
  113. case DisplayOrientations::Landscape:
  114. case DisplayOrientations::LandscapeFlipped:
  115. {
  116. int tmp = w;
  117. w = h;
  118. h = tmp;
  119. } break;
  120. }
  121. #endif
  122. const Uint32 latestFlags = WINRT_DetectWindowFlags(window);
  123. if (latestFlags & SDL_WINDOW_MAXIMIZED) {
  124. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MAXIMIZED, 0, 0);
  125. } else {
  126. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESTORED, 0, 0);
  127. }
  128. WINRT_UpdateWindowFlags(window, SDL_WINDOW_FULLSCREEN);
  129. /* The window can move during a resize event, such as when maximizing
  130. or resizing from a corner */
  131. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MOVED, x, y);
  132. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, w, h);
  133. }
  134. }
  135. }
  136. SDL_WinRTApp::SDL_WinRTApp() : m_windowClosed(false),
  137. m_windowVisible(true)
  138. {
  139. }
  140. void SDL_WinRTApp::Initialize(CoreApplicationView ^ applicationView)
  141. {
  142. applicationView->Activated +=
  143. ref new TypedEventHandler<CoreApplicationView ^, IActivatedEventArgs ^>(this, &SDL_WinRTApp::OnAppActivated);
  144. CoreApplication::Suspending +=
  145. ref new EventHandler<SuspendingEventArgs ^>(this, &SDL_WinRTApp::OnSuspending);
  146. CoreApplication::Resuming +=
  147. ref new EventHandler<Platform::Object ^>(this, &SDL_WinRTApp::OnResuming);
  148. CoreApplication::Exiting +=
  149. ref new EventHandler<Platform::Object ^>(this, &SDL_WinRTApp::OnExiting);
  150. #if NTDDI_VERSION >= NTDDI_WIN10
  151. /* HACK ALERT! Xbox One doesn't seem to detect gamepads unless something
  152. gets registered to receive Win10's Windows.Gaming.Input.Gamepad.GamepadAdded
  153. events. We'll register an event handler for these events here, to make
  154. sure that gamepad detection works later on, if requested.
  155. */
  156. Windows::Gaming::Input::Gamepad::GamepadAdded +=
  157. ref new Windows::Foundation::EventHandler<Windows::Gaming::Input::Gamepad ^>(
  158. this, &SDL_WinRTApp::OnGamepadAdded);
  159. #endif
  160. }
  161. #if NTDDI_VERSION > NTDDI_WIN8
  162. void SDL_WinRTApp::OnOrientationChanged(DisplayInformation ^ sender, Object ^ args)
  163. #else
  164. void SDL_WinRTApp::OnOrientationChanged(Object ^ sender)
  165. #endif
  166. {
  167. #if LOG_ORIENTATION_EVENTS == 1
  168. {
  169. CoreWindow ^ window = CoreWindow::GetForCurrentThread();
  170. if (window) {
  171. SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, CoreWindow Bounds={%f,%f,%f,%f}\n",
  172. __FUNCTION__,
  173. WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  174. WINRT_DISPLAY_PROPERTY(NativeOrientation),
  175. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
  176. window->Bounds.X,
  177. window->Bounds.Y,
  178. window->Bounds.Width,
  179. window->Bounds.Height);
  180. } else {
  181. SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d\n",
  182. __FUNCTION__,
  183. WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  184. WINRT_DISPLAY_PROPERTY(NativeOrientation),
  185. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences));
  186. }
  187. }
  188. #endif
  189. WINRT_ProcessWindowSizeChange();
  190. #if SDL_WINAPI_FAMILY_PHONE
  191. // HACK: Make sure that orientation changes
  192. // lead to the Direct3D renderer's viewport getting updated:
  193. //
  194. // For some reason, this doesn't seem to need to be done on Windows 8.x,
  195. // even when going from Landscape to LandscapeFlipped. It only seems to
  196. // be needed on Windows Phone, at least when I tested on my devices.
  197. // I'm not currently sure why this is, but it seems to work fine. -- David L.
  198. //
  199. // 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
  200. SDL_Window *window = WINRT_GlobalSDLWindow;
  201. if (window) {
  202. SDL_WindowData *data = window->driverdata;
  203. int w = (int)SDL_floorf(data->coreWindow->Bounds.Width);
  204. int h = (int)SDL_floorf(data->coreWindow->Bounds.Height);
  205. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_EVENT_WINDOW_RESIZED, w, h);
  206. }
  207. #endif
  208. }
  209. void SDL_WinRTApp::SetWindow(CoreWindow ^ window)
  210. {
  211. #if LOG_WINDOW_EVENTS == 1
  212. SDL_Log("%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, window bounds={%f, %f, %f,%f}\n",
  213. __FUNCTION__,
  214. WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  215. WINRT_DISPLAY_PROPERTY(NativeOrientation),
  216. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
  217. window->Bounds.X,
  218. window->Bounds.Y,
  219. window->Bounds.Width,
  220. window->Bounds.Height);
  221. #endif
  222. window->SizeChanged +=
  223. ref new TypedEventHandler<CoreWindow ^, WindowSizeChangedEventArgs ^>(this, &SDL_WinRTApp::OnWindowSizeChanged);
  224. window->VisibilityChanged +=
  225. ref new TypedEventHandler<CoreWindow ^, VisibilityChangedEventArgs ^>(this, &SDL_WinRTApp::OnVisibilityChanged);
  226. window->Activated +=
  227. ref new TypedEventHandler<CoreWindow ^, WindowActivatedEventArgs ^>(this, &SDL_WinRTApp::OnWindowActivated);
  228. window->Closed +=
  229. ref new TypedEventHandler<CoreWindow ^, CoreWindowEventArgs ^>(this, &SDL_WinRTApp::OnWindowClosed);
  230. #if !SDL_WINAPI_FAMILY_PHONE
  231. window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);
  232. #endif
  233. window->PointerPressed +=
  234. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &SDL_WinRTApp::OnPointerPressed);
  235. window->PointerMoved +=
  236. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &SDL_WinRTApp::OnPointerMoved);
  237. window->PointerReleased +=
  238. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &SDL_WinRTApp::OnPointerReleased);
  239. window->PointerEntered +=
  240. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &SDL_WinRTApp::OnPointerEntered);
  241. window->PointerExited +=
  242. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &SDL_WinRTApp::OnPointerExited);
  243. window->PointerWheelChanged +=
  244. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &SDL_WinRTApp::OnPointerWheelChanged);
  245. #if !SDL_WINAPI_FAMILY_PHONE
  246. // Retrieves relative-only mouse movements:
  247. Windows::Devices::Input::MouseDevice::GetForCurrentView()->MouseMoved +=
  248. ref new TypedEventHandler<MouseDevice ^, MouseEventArgs ^>(this, &SDL_WinRTApp::OnMouseMoved);
  249. #endif
  250. window->Dispatcher->AcceleratorKeyActivated +=
  251. ref new TypedEventHandler<CoreDispatcher ^, AcceleratorKeyEventArgs ^>(this, &SDL_WinRTApp::OnAcceleratorKeyActivated);
  252. window->CharacterReceived +=
  253. ref new TypedEventHandler<CoreWindow ^, CharacterReceivedEventArgs ^>(this, &SDL_WinRTApp::OnCharacterReceived);
  254. #if NTDDI_VERSION >= NTDDI_WIN10
  255. Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->BackRequested +=
  256. ref new EventHandler<BackRequestedEventArgs ^>(this, &SDL_WinRTApp::OnBackButtonPressed);
  257. #elif SDL_WINAPI_FAMILY_PHONE
  258. HardwareButtons::BackPressed +=
  259. ref new EventHandler<BackPressedEventArgs ^>(this, &SDL_WinRTApp::OnBackButtonPressed);
  260. #endif
  261. #if NTDDI_VERSION > NTDDI_WIN8
  262. DisplayInformation::GetForCurrentView()->OrientationChanged +=
  263. ref new TypedEventHandler<Windows::Graphics::Display::DisplayInformation ^, Object ^>(this, &SDL_WinRTApp::OnOrientationChanged);
  264. #else
  265. DisplayProperties::OrientationChanged +=
  266. ref new DisplayPropertiesEventHandler(this, &SDL_WinRTApp::OnOrientationChanged);
  267. #endif
  268. #if (WINAPI_FAMILY == WINAPI_FAMILY_APP) && (NTDDI_VERSION < NTDDI_WIN10) // for Windows 8/8.1/RT apps... (and not Phone apps)
  269. // Make sure we know when a user has opened the app's settings pane.
  270. // This is needed in order to display a privacy policy, which needs
  271. // to be done for network-enabled apps, as per Windows Store requirements.
  272. using namespace Windows::UI::ApplicationSettings;
  273. SettingsPane::GetForCurrentView()->CommandsRequested +=
  274. ref new TypedEventHandler<SettingsPane ^, SettingsPaneCommandsRequestedEventArgs ^>(this, &SDL_WinRTApp::OnSettingsPaneCommandsRequested);
  275. #endif
  276. }
  277. void SDL_WinRTApp::Load(Platform::String ^ entryPoint)
  278. {
  279. }
  280. void SDL_WinRTApp::Run()
  281. {
  282. SDL_SetMainReady();
  283. if (WINRT_SDLAppEntryPoint) {
  284. // TODO, WinRT: pass the C-style main() a reasonably realistic
  285. // representation of command line arguments.
  286. int argc = 1;
  287. char **argv = (char **)SDL_malloc(2 * sizeof(*argv));
  288. if (!argv) {
  289. return;
  290. }
  291. argv[0] = SDL_strdup("WinRTApp");
  292. argv[1] = NULL;
  293. WINRT_SDLAppEntryPoint(argc, argv);
  294. SDL_free(argv[0]);
  295. SDL_free(argv);
  296. }
  297. }
  298. static bool IsSDLWindowEventPending(SDL_EventType windowEventID)
  299. {
  300. SDL_Event events[128];
  301. const int count = SDL_PeepEvents(events, sizeof(events) / sizeof(SDL_Event), SDL_PEEKEVENT, windowEventID, windowEventID);
  302. return (count > 0);
  303. }
  304. bool SDL_WinRTApp::ShouldWaitForAppResumeEvents()
  305. {
  306. /* Don't wait if the app is visible: */
  307. if (m_windowVisible) {
  308. return false;
  309. }
  310. /* Don't wait until the window-hide events finish processing.
  311. * Do note that if an app-suspend event is sent (as indicated
  312. * by SDL_EVENT_WILL_ENTER_BACKGROUND and SDL_EVENT_DID_ENTER_BACKGROUND
  313. * events), then this code may be a moot point, as WinRT's
  314. * own event pump (aka ProcessEvents()) will pause regardless
  315. * of what we do here. This happens on Windows Phone 8, to note.
  316. * Windows 8.x apps, on the other hand, may get a chance to run
  317. * these.
  318. */
  319. if (IsSDLWindowEventPending(SDL_EVENT_WINDOW_HIDDEN)) {
  320. return false;
  321. } else if (IsSDLWindowEventPending(SDL_EVENT_WINDOW_FOCUS_LOST)) {
  322. return false;
  323. } else if (IsSDLWindowEventPending(SDL_EVENT_WINDOW_MINIMIZED)) {
  324. return false;
  325. }
  326. return true;
  327. }
  328. void SDL_WinRTApp::PumpEvents()
  329. {
  330. if (!m_windowClosed) {
  331. if (!ShouldWaitForAppResumeEvents()) {
  332. /* This is the normal way in which events should be pumped.
  333. * 'ProcessAllIfPresent' will make ProcessEvents() process anywhere
  334. * from zero to N events, and will then return.
  335. */
  336. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
  337. } else {
  338. /* This style of event-pumping, with 'ProcessOneAndAllPending',
  339. * will cause anywhere from one to N events to be processed. If
  340. * at least one event is processed, the call will return. If
  341. * no events are pending, then the call will wait until one is
  342. * available, and will not return (to the caller) until this
  343. * happens! This should only occur when the app is hidden.
  344. */
  345. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
  346. }
  347. }
  348. }
  349. void SDL_WinRTApp::Uninitialize()
  350. {
  351. }
  352. #if (WINAPI_FAMILY == WINAPI_FAMILY_APP) && (NTDDI_VERSION < NTDDI_WIN10)
  353. void SDL_WinRTApp::OnSettingsPaneCommandsRequested(
  354. Windows::UI::ApplicationSettings::SettingsPane ^ p,
  355. Windows::UI::ApplicationSettings::SettingsPaneCommandsRequestedEventArgs ^ args)
  356. {
  357. using namespace Platform;
  358. using namespace Windows::UI::ApplicationSettings;
  359. using namespace Windows::UI::Popups;
  360. String ^ privacyPolicyURL = nullptr; // a URL to an app's Privacy Policy
  361. String ^ privacyPolicyLabel = nullptr; // label/link text
  362. const char *tmpHintValue = NULL; // SDL_GetHint-retrieved value, used immediately
  363. wchar_t *tmpStr = NULL; // used for UTF8 to UCS2 conversion
  364. // Setup a 'Privacy Policy' link, if one is available (via SDL_GetHint):
  365. tmpHintValue = SDL_GetHint(SDL_HINT_WINRT_PRIVACY_POLICY_URL);
  366. if (tmpHintValue && tmpHintValue[0] != '\0') {
  367. // Convert the privacy policy's URL to UCS2:
  368. tmpStr = WIN_UTF8ToString(tmpHintValue);
  369. privacyPolicyURL = ref new String(tmpStr);
  370. SDL_free(tmpStr);
  371. // Optionally retrieve custom label-text for the link. If this isn't
  372. // available, a default value will be used instead.
  373. tmpHintValue = SDL_GetHint(SDL_HINT_WINRT_PRIVACY_POLICY_LABEL);
  374. if (tmpHintValue && tmpHintValue[0] != '\0') {
  375. tmpStr = WIN_UTF8ToString(tmpHintValue);
  376. privacyPolicyLabel = ref new String(tmpStr);
  377. SDL_free(tmpStr);
  378. } else {
  379. privacyPolicyLabel = ref new String(L"Privacy Policy");
  380. }
  381. // Register the link, along with a handler to be called if and when it is
  382. // clicked:
  383. auto cmd = ref new SettingsCommand(L"privacyPolicy", privacyPolicyLabel,
  384. ref new UICommandInvokedHandler([=](IUICommand ^) {
  385. Windows::System::Launcher::LaunchUriAsync(ref new Uri(privacyPolicyURL));
  386. }));
  387. args->Request->ApplicationCommands->Append(cmd);
  388. }
  389. }
  390. #endif // if (WINAPI_FAMILY == WINAPI_FAMILY_APP) && (NTDDI_VERSION < NTDDI_WIN10)
  391. void SDL_WinRTApp::OnWindowSizeChanged(CoreWindow ^ sender, WindowSizeChangedEventArgs ^ args)
  392. {
  393. #if LOG_WINDOW_EVENTS == 1
  394. 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",
  395. __FUNCTION__,
  396. args->Size.Width, args->Size.Height,
  397. sender->Bounds.X, sender->Bounds.Y, sender->Bounds.Width, sender->Bounds.Height,
  398. WINRT_DISPLAY_PROPERTY(CurrentOrientation),
  399. WINRT_DISPLAY_PROPERTY(NativeOrientation),
  400. WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
  401. (WINRT_GlobalSDLWindow ? "yes" : "no"));
  402. #endif
  403. WINRT_ProcessWindowSizeChange();
  404. }
  405. void SDL_WinRTApp::OnVisibilityChanged(CoreWindow ^ sender, VisibilityChangedEventArgs ^ args)
  406. {
  407. #if LOG_WINDOW_EVENTS == 1
  408. SDL_Log("%s, visible?=%s, bounds={%f,%f,%f,%f}, WINRT_GlobalSDLWindow?=%s\n",
  409. __FUNCTION__,
  410. (args->Visible ? "yes" : "no"),
  411. sender->Bounds.X, sender->Bounds.Y,
  412. sender->Bounds.Width, sender->Bounds.Height,
  413. (WINRT_GlobalSDLWindow ? "yes" : "no"));
  414. #endif
  415. m_windowVisible = args->Visible;
  416. if (WINRT_GlobalSDLWindow) {
  417. SDL_bool wasSDLWindowSurfaceValid = WINRT_GlobalSDLWindow->surface_valid;
  418. Uint32 latestWindowFlags = WINRT_DetectWindowFlags(WINRT_GlobalSDLWindow);
  419. if (args->Visible) {
  420. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_EVENT_WINDOW_SHOWN, 0, 0);
  421. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_EVENT_WINDOW_FOCUS_GAINED, 0, 0);
  422. if (latestWindowFlags & SDL_WINDOW_MAXIMIZED) {
  423. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_EVENT_WINDOW_MAXIMIZED, 0, 0);
  424. } else {
  425. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_EVENT_WINDOW_RESTORED, 0, 0);
  426. }
  427. } else {
  428. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_EVENT_WINDOW_HIDDEN, 0, 0);
  429. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_EVENT_WINDOW_FOCUS_LOST, 0, 0);
  430. SDL_SendWindowEvent(WINRT_GlobalSDLWindow, SDL_EVENT_WINDOW_MINIMIZED, 0, 0);
  431. }
  432. // HACK: Prevent SDL's window-hide handling code, which currently
  433. // triggers a fake window resize (possibly erroneously), from
  434. // marking the SDL window's surface as invalid.
  435. //
  436. // A better solution to this probably involves figuring out if the
  437. // fake window resize can be prevented.
  438. WINRT_GlobalSDLWindow->surface_valid = wasSDLWindowSurfaceValid;
  439. }
  440. }
  441. void SDL_WinRTApp::OnWindowActivated(CoreWindow ^ sender, WindowActivatedEventArgs ^ args)
  442. {
  443. #if LOG_WINDOW_EVENTS == 1
  444. SDL_Log("%s, WINRT_GlobalSDLWindow?=%s\n\n",
  445. __FUNCTION__,
  446. (WINRT_GlobalSDLWindow ? "yes" : "no"));
  447. #endif
  448. /* There's no property in Win 8.x to tell whether a window is active or
  449. not. [De]activation events are, however, sent to the app. We'll just
  450. record those, in case the CoreWindow gets wrapped by an SDL_Window at
  451. some future time.
  452. */
  453. sender->CustomProperties->Insert("SDLHelperWindowActivationState", args->WindowActivationState);
  454. SDL_Window *window = WINRT_GlobalSDLWindow;
  455. if (window) {
  456. if (args->WindowActivationState != CoreWindowActivationState::Deactivated) {
  457. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_SHOWN, 0, 0);
  458. if (SDL_GetKeyboardFocus() != window) {
  459. SDL_SetKeyboardFocus(window);
  460. }
  461. /* Send a mouse-motion event as appropriate.
  462. This doesn't work when called from OnPointerEntered, at least
  463. not in WinRT CoreWindow apps (as OnPointerEntered doesn't
  464. appear to be called after window-reactivation, at least not
  465. in Windows 10, Build 10586.3 (November 2015 update, non-beta).
  466. Don't do it on WinPhone 8.0 though, as CoreWindow's 'PointerPosition'
  467. property isn't available.
  468. */
  469. #if !SDL_WINAPI_FAMILY_PHONE || NTDDI_VERSION >= NTDDI_WINBLUE
  470. Point cursorPos = WINRT_TransformCursorPosition(window, sender->PointerPosition, TransformToSDLWindowSize);
  471. SDL_SendMouseMotion(0, window, 0, SDL_FALSE, cursorPos.X, cursorPos.Y);
  472. #endif
  473. /* TODO, WinRT: see if the Win32 bugfix from https://hg.libsdl.org/SDL/rev/d278747da408 needs to be applied (on window activation) */
  474. // WIN_CheckAsyncMouseRelease(data);
  475. /* TODO, WinRT: implement clipboard support, if possible */
  476. ///*
  477. // * FIXME: Update keyboard state
  478. // */
  479. // WIN_CheckClipboardUpdate(data->videodata);
  480. // HACK: Resetting the mouse-cursor here seems to fix
  481. // https://bugzilla.libsdl.org/show_bug.cgi?id=3217, whereby a
  482. // WinRT app's mouse cursor may switch to Windows' 'wait' cursor,
  483. // after a user alt-tabs back into a full-screened SDL app.
  484. // This bug does not appear to reproduce 100% of the time.
  485. // It may be a bug in Windows itself (v.10.0.586.36, as tested,
  486. // and the most-recent as of this writing).
  487. SDL_SetCursor(NULL);
  488. } else {
  489. if (SDL_GetKeyboardFocus() == window) {
  490. SDL_SetKeyboardFocus(NULL);
  491. }
  492. }
  493. }
  494. }
  495. void SDL_WinRTApp::OnWindowClosed(CoreWindow ^ sender, CoreWindowEventArgs ^ args)
  496. {
  497. #if LOG_WINDOW_EVENTS == 1
  498. SDL_Log("%s\n", __FUNCTION__);
  499. #endif
  500. m_windowClosed = true;
  501. }
  502. void SDL_WinRTApp::OnAppActivated(CoreApplicationView ^ applicationView, IActivatedEventArgs ^ args)
  503. {
  504. CoreWindow::GetForCurrentThread()->Activate();
  505. }
  506. void SDL_WinRTApp::OnSuspending(Platform::Object ^ sender, SuspendingEventArgs ^ args)
  507. {
  508. // Save app state asynchronously after requesting a deferral. Holding a deferral
  509. // indicates that the application is busy performing suspending operations. Be
  510. // aware that a deferral may not be held indefinitely. After about five seconds,
  511. // the app will be forced to exit.
  512. // ... but first, let the app know it's about to go to the background.
  513. // The separation of events may be important, given that the deferral
  514. // runs in a separate thread. This'll make SDL_EVENT_WILL_ENTER_BACKGROUND
  515. // the only event among the two that runs in the main thread. Given
  516. // that a few WinRT operations can only be done from the main thread
  517. // (things that access the WinRT CoreWindow are one example of this),
  518. // this could be important.
  519. SDL_SendAppEvent(SDL_EVENT_WILL_ENTER_BACKGROUND);
  520. SuspendingDeferral ^ deferral = args->SuspendingOperation->GetDeferral();
  521. create_task([this, deferral]() {
  522. // Send an app did-enter-background event immediately to observers.
  523. // CoreDispatcher::ProcessEvents, which is the backbone on which
  524. // SDL_WinRTApp::PumpEvents is built, will not return to its caller
  525. // once it sends out a suspend event. Any events posted to SDL's
  526. // event queue won't get received until the WinRT app is resumed.
  527. // SDL_AddEventWatch() may be used to receive app-suspend events on
  528. // WinRT.
  529. SDL_SendAppEvent(SDL_EVENT_DID_ENTER_BACKGROUND);
  530. // Let the Direct3D 11 renderer prepare for the app to be backgrounded.
  531. // This is necessary for Windows 8.1, possibly elsewhere in the future.
  532. // More details at: http://msdn.microsoft.com/en-us/library/windows/apps/Hh994929.aspx
  533. #if SDL_VIDEO_RENDER_D3D11
  534. if (WINRT_GlobalSDLWindow) {
  535. SDL_Renderer *renderer = SDL_GetRenderer(WINRT_GlobalSDLWindow);
  536. if (renderer && (SDL_strcmp(renderer->info.name, "direct3d11") == 0)) {
  537. D3D11_Trim(renderer);
  538. }
  539. }
  540. #endif
  541. deferral->Complete();
  542. });
  543. }
  544. void SDL_WinRTApp::OnResuming(Platform::Object ^ sender, Platform::Object ^ args)
  545. {
  546. // Restore any data or state that was unloaded on suspend. By default, data
  547. // and state are persisted when resuming from suspend. Note that these events
  548. // do not occur if the app was previously terminated.
  549. SDL_SendAppEvent(SDL_EVENT_WILL_ENTER_FOREGROUND);
  550. SDL_SendAppEvent(SDL_EVENT_DID_ENTER_FOREGROUND);
  551. }
  552. void SDL_WinRTApp::OnExiting(Platform::Object ^ sender, Platform::Object ^ args)
  553. {
  554. SDL_SendAppEvent(SDL_EVENT_TERMINATING);
  555. }
  556. static void WINRT_LogPointerEvent(const char *header, Windows::UI::Core::PointerEventArgs ^ args, Windows::Foundation::Point transformedPoint)
  557. {
  558. Uint8 button, pressed;
  559. Windows::UI::Input::PointerPoint ^ pt = args->CurrentPoint;
  560. WINRT_GetSDLButtonForPointerPoint(pt, &button, &pressed);
  561. SDL_Log("%s: Position={%f,%f}, Transformed Pos={%f, %f}, MouseWheelDelta=%d, FrameId=%d, PointerId=%d, SDL button=%d pressed=%d\n",
  562. header,
  563. pt->Position.X, pt->Position.Y,
  564. transformedPoint.X, transformedPoint.Y,
  565. pt->Properties->MouseWheelDelta,
  566. pt->FrameId,
  567. pt->PointerId,
  568. button,
  569. pressed);
  570. }
  571. void SDL_WinRTApp::OnPointerPressed(CoreWindow ^ sender, PointerEventArgs ^ args)
  572. {
  573. #if LOG_POINTER_EVENTS
  574. WINRT_LogPointerEvent("pointer pressed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  575. #endif
  576. WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  577. }
  578. void SDL_WinRTApp::OnPointerMoved(CoreWindow ^ sender, PointerEventArgs ^ args)
  579. {
  580. #if LOG_POINTER_EVENTS
  581. WINRT_LogPointerEvent("pointer moved", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  582. #endif
  583. WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  584. }
  585. void SDL_WinRTApp::OnPointerReleased(CoreWindow ^ sender, PointerEventArgs ^ args)
  586. {
  587. #if LOG_POINTER_EVENTS
  588. WINRT_LogPointerEvent("pointer released", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  589. #endif
  590. WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  591. }
  592. void SDL_WinRTApp::OnPointerEntered(CoreWindow ^ sender, PointerEventArgs ^ args)
  593. {
  594. #if LOG_POINTER_EVENTS
  595. WINRT_LogPointerEvent("pointer entered", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  596. #endif
  597. WINRT_ProcessPointerEnteredEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  598. }
  599. void SDL_WinRTApp::OnPointerExited(CoreWindow ^ sender, PointerEventArgs ^ args)
  600. {
  601. #if LOG_POINTER_EVENTS
  602. WINRT_LogPointerEvent("pointer exited", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  603. #endif
  604. WINRT_ProcessPointerExitedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  605. }
  606. void SDL_WinRTApp::OnPointerWheelChanged(CoreWindow ^ sender, PointerEventArgs ^ args)
  607. {
  608. #if LOG_POINTER_EVENTS
  609. WINRT_LogPointerEvent("pointer wheel changed", args, WINRT_TransformCursorPosition(WINRT_GlobalSDLWindow, args->CurrentPoint->Position, TransformToSDLWindowSize));
  610. #endif
  611. WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
  612. }
  613. void SDL_WinRTApp::OnMouseMoved(MouseDevice ^ mouseDevice, MouseEventArgs ^ args)
  614. {
  615. WINRT_ProcessMouseMovedEvent(WINRT_GlobalSDLWindow, args);
  616. }
  617. void SDL_WinRTApp::OnAcceleratorKeyActivated(Windows::UI::Core::CoreDispatcher ^ sender, Windows::UI::Core::AcceleratorKeyEventArgs ^ args)
  618. {
  619. WINRT_ProcessAcceleratorKeyActivated(args);
  620. }
  621. void SDL_WinRTApp::OnCharacterReceived(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::CharacterReceivedEventArgs ^ args)
  622. {
  623. WINRT_ProcessCharacterReceivedEvent(WINRT_GlobalSDLWindow, args);
  624. }
  625. template <typename BackButtonEventArgs>
  626. static void WINRT_OnBackButtonPressed(BackButtonEventArgs ^ args)
  627. {
  628. SDL_SendKeyboardKey(0, 0, SDL_PRESSED, SDL_SCANCODE_AC_BACK);
  629. SDL_SendKeyboardKey(0, 0, SDL_RELEASED, SDL_SCANCODE_AC_BACK);
  630. if (SDL_GetHintBoolean(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, SDL_FALSE)) {
  631. args->Handled = true;
  632. }
  633. }
  634. #if NTDDI_VERSION >= NTDDI_WIN10
  635. void SDL_WinRTApp::OnBackButtonPressed(Platform::Object ^ sender, Windows::UI::Core::BackRequestedEventArgs ^ args)
  636. {
  637. WINRT_OnBackButtonPressed(args);
  638. }
  639. #elif SDL_WINAPI_FAMILY_PHONE
  640. void SDL_WinRTApp::OnBackButtonPressed(Platform::Object ^ sender, Windows::Phone::UI::Input::BackPressedEventArgs ^ args)
  641. {
  642. WINRT_OnBackButtonPressed(args);
  643. }
  644. #endif
  645. #if NTDDI_VERSION >= NTDDI_WIN10
  646. void SDL_WinRTApp::OnGamepadAdded(Platform::Object ^ sender, Windows::Gaming::Input::Gamepad ^ gamepad)
  647. {
  648. /* HACK ALERT: Nothing needs to be done here, as this method currently
  649. only exists to allow something to be registered with Win10's
  650. GamepadAdded event, an operation that seems to be necessary to get
  651. Xinput-based detection to work on Xbox One.
  652. */
  653. }
  654. #endif