SDL_winrtgamebar.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. #if SDL_VIDEO_DRIVER_WINRT
  20. /* Windows includes */
  21. #include <roapi.h>
  22. #include <windows.foundation.h>
  23. #include <windows.system.h>
  24. /* SDL includes */
  25. extern "C" {
  26. #include "SDL_mouse.h"
  27. #include "../SDL_sysvideo.h"
  28. }
  29. #include "SDL_winrtvideo_cpp.h"
  30. /* Game Bar events can come in off the main thread. Use the following
  31. WinRT CoreDispatcher to deal with them on SDL's thread.
  32. */
  33. static Platform::WeakReference WINRT_MainThreadDispatcher;
  34. /* Win10's initial SDK (the 10.0.10240.0 release) does not include references
  35. to Game Bar APIs, as the Game Bar was released via Win10 10.0.10586.0.
  36. Declare its WinRT/COM interface here, to allow compilation with earlier
  37. Windows SDKs.
  38. */
  39. MIDL_INTERFACE("1DB9A292-CC78-4173-BE45-B61E67283EA7")
  40. IGameBarStatics_ : public IInspectable
  41. {
  42. public:
  43. virtual HRESULT STDMETHODCALLTYPE add_VisibilityChanged(
  44. __FIEventHandler_1_IInspectable *handler,
  45. Windows::Foundation::EventRegistrationToken *token) = 0;
  46. virtual HRESULT STDMETHODCALLTYPE remove_VisibilityChanged(
  47. Windows::Foundation::EventRegistrationToken token) = 0;
  48. virtual HRESULT STDMETHODCALLTYPE add_IsInputRedirectedChanged(
  49. __FIEventHandler_1_IInspectable *handler,
  50. Windows::Foundation::EventRegistrationToken *token) = 0;
  51. virtual HRESULT STDMETHODCALLTYPE remove_IsInputRedirectedChanged(
  52. Windows::Foundation::EventRegistrationToken token) = 0;
  53. virtual HRESULT STDMETHODCALLTYPE get_Visible(
  54. boolean *value) = 0;
  55. virtual HRESULT STDMETHODCALLTYPE get_IsInputRedirected(
  56. boolean *value) = 0;
  57. };
  58. /* Declare the game bar's COM GUID */
  59. static GUID IID_IGameBarStatics_ = { MAKELONG(0xA292, 0x1DB9), 0xCC78, 0x4173, { 0xBE, 0x45, 0xB6, 0x1E, 0x67, 0x28, 0x3E, 0xA7 } };
  60. /* Retrieves a pointer to the game bar, or NULL if it is not available.
  61. If a pointer is returned, it's ->Release() method must be called
  62. after the caller has finished using it.
  63. */
  64. static IGameBarStatics_ *
  65. WINRT_GetGameBar()
  66. {
  67. wchar_t *wClassName = L"Windows.Gaming.UI.GameBar";
  68. HSTRING hClassName;
  69. IActivationFactory *pActivationFactory = NULL;
  70. IGameBarStatics_ *pGameBar = NULL;
  71. HRESULT hr;
  72. hr = ::WindowsCreateString(wClassName, (UINT32)SDL_wcslen(wClassName), &hClassName);
  73. if (FAILED(hr)) {
  74. goto done;
  75. }
  76. hr = Windows::Foundation::GetActivationFactory(hClassName, &pActivationFactory);
  77. if (FAILED(hr)) {
  78. goto done;
  79. }
  80. pActivationFactory->QueryInterface(IID_IGameBarStatics_, (void **) &pGameBar);
  81. done:
  82. if (pActivationFactory) {
  83. pActivationFactory->Release();
  84. }
  85. if (hClassName) {
  86. ::WindowsDeleteString(hClassName);
  87. }
  88. return pGameBar;
  89. }
  90. static void
  91. WINRT_HandleGameBarIsInputRedirected_MainThread()
  92. {
  93. IGameBarStatics_ *gameBar;
  94. boolean isInputRedirected = 0;
  95. if (!WINRT_MainThreadDispatcher) {
  96. /* The game bar event handler has been deregistered! */
  97. return;
  98. }
  99. gameBar = WINRT_GetGameBar();
  100. if (!gameBar) {
  101. /* Shouldn't happen, but just in case... */
  102. return;
  103. }
  104. if (SUCCEEDED(gameBar->get_IsInputRedirected(&isInputRedirected))) {
  105. if ( ! isInputRedirected) {
  106. /* Input-control is now back to the SDL app. Restore the cursor,
  107. in case Windows does not (it does not in either Win10
  108. 10.0.10240.0 or 10.0.10586.0, maybe later version(s) too.
  109. */
  110. SDL_Cursor *cursor = SDL_GetCursor();
  111. SDL_SetCursor(cursor);
  112. }
  113. }
  114. gameBar->Release();
  115. }
  116. static void
  117. WINRT_HandleGameBarIsInputRedirected_NonMainThread(Platform::Object ^ o1, Platform::Object ^o2)
  118. {
  119. Windows::UI::Core::CoreDispatcher ^dispatcher = WINRT_MainThreadDispatcher.Resolve<Windows::UI::Core::CoreDispatcher>();
  120. if (dispatcher) {
  121. dispatcher->RunAsync(
  122. Windows::UI::Core::CoreDispatcherPriority::Normal,
  123. ref new Windows::UI::Core::DispatchedHandler(&WINRT_HandleGameBarIsInputRedirected_MainThread));
  124. }
  125. }
  126. void
  127. WINRT_InitGameBar(_THIS)
  128. {
  129. SDL_VideoData *driverdata = (SDL_VideoData *)_this->driverdata;
  130. IGameBarStatics_ *gameBar = WINRT_GetGameBar();
  131. if (gameBar) {
  132. /* GameBar.IsInputRedirected events can come in via something other than
  133. the main/SDL thread.
  134. Get a WinRT 'CoreDispatcher' that can be used to call back into the
  135. SDL thread.
  136. */
  137. WINRT_MainThreadDispatcher = Windows::UI::Core::CoreWindow::GetForCurrentThread()->Dispatcher;
  138. Windows::Foundation::EventHandler<Platform::Object ^> ^handler = \
  139. ref new Windows::Foundation::EventHandler<Platform::Object ^>(&WINRT_HandleGameBarIsInputRedirected_NonMainThread);
  140. __FIEventHandler_1_IInspectable * pHandler = reinterpret_cast<__FIEventHandler_1_IInspectable *>(handler);
  141. gameBar->add_IsInputRedirectedChanged(pHandler, &driverdata->gameBarIsInputRedirectedToken);
  142. gameBar->Release();
  143. }
  144. }
  145. void
  146. WINRT_QuitGameBar(_THIS)
  147. {
  148. SDL_VideoData *driverdata;
  149. IGameBarStatics_ *gameBar;
  150. if (!_this || !_this->driverdata) {
  151. return;
  152. }
  153. gameBar = WINRT_GetGameBar();
  154. if (!gameBar) {
  155. return;
  156. }
  157. driverdata = (SDL_VideoData *)_this->driverdata;
  158. if (driverdata->gameBarIsInputRedirectedToken.Value) {
  159. gameBar->remove_IsInputRedirectedChanged(driverdata->gameBarIsInputRedirectedToken);
  160. driverdata->gameBarIsInputRedirectedToken.Value = 0;
  161. }
  162. WINRT_MainThreadDispatcher = nullptr;
  163. gameBar->Release();
  164. }
  165. #endif /* SDL_VIDEO_DRIVER_WINRT */
  166. /* vi: set ts=4 sw=4 expandtab: */