SDL_winrtapp_direct3d.cpp 31 KB

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