SDL_gdk.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. extern "C" {
  20. #include "../windows/SDL_windows.h"
  21. #include "../../events/SDL_events_c.h"
  22. }
  23. #include <XGameRuntime.h>
  24. #include <xsapi-c/services_c.h>
  25. #include <appnotify.h>
  26. static XTaskQueueHandle GDK_GlobalTaskQueue;
  27. PAPPSTATE_REGISTRATION hPLM = {};
  28. PAPPCONSTRAIN_REGISTRATION hCPLM = {};
  29. HANDLE plmSuspendComplete = nullptr;
  30. extern "C"
  31. bool SDL_GetGDKTaskQueue(XTaskQueueHandle *outTaskQueue)
  32. {
  33. // If this is the first call, first create the global task queue.
  34. if (!GDK_GlobalTaskQueue) {
  35. HRESULT hr;
  36. hr = XTaskQueueCreate(XTaskQueueDispatchMode::ThreadPool,
  37. XTaskQueueDispatchMode::Manual,
  38. &GDK_GlobalTaskQueue);
  39. if (FAILED(hr)) {
  40. return SDL_SetError("[GDK] Could not create global task queue");
  41. }
  42. // The initial call gets the non-duplicated handle so they can clean it up
  43. *outTaskQueue = GDK_GlobalTaskQueue;
  44. } else {
  45. // Duplicate the global task queue handle into outTaskQueue
  46. if (FAILED(XTaskQueueDuplicateHandle(GDK_GlobalTaskQueue, outTaskQueue))) {
  47. return SDL_SetError("[GDK] Unable to acquire global task queue");
  48. }
  49. }
  50. return true;
  51. }
  52. extern "C"
  53. void GDK_DispatchTaskQueue(void)
  54. {
  55. /* If there is no global task queue, don't do anything.
  56. * This gives the option to opt-out for those who want to handle everything themselves.
  57. */
  58. if (GDK_GlobalTaskQueue) {
  59. // Dispatch any callbacks which are ready.
  60. while (XTaskQueueDispatch(GDK_GlobalTaskQueue, XTaskQueuePort::Completion, 0))
  61. ;
  62. }
  63. }
  64. extern "C"
  65. bool GDK_RegisterChangeNotifications(void)
  66. {
  67. // Register suspend/resume handling
  68. plmSuspendComplete = CreateEventEx(nullptr, nullptr, 0, EVENT_MODIFY_STATE | SYNCHRONIZE);
  69. if (!plmSuspendComplete) {
  70. return SDL_SetError("[GDK] Unable to create plmSuspendComplete event");
  71. }
  72. auto rascn = [](BOOLEAN quiesced, PVOID context) {
  73. SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "[GDK] in RegisterAppStateChangeNotification handler");
  74. if (quiesced) {
  75. ResetEvent(plmSuspendComplete);
  76. SDL_SendAppEvent(SDL_EVENT_DID_ENTER_BACKGROUND);
  77. // To defer suspension, we must wait to exit this callback.
  78. // IMPORTANT: The app must call SDL_GDKSuspendComplete() to release this lock.
  79. (void)WaitForSingleObject(plmSuspendComplete, INFINITE);
  80. SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "[GDK] in RegisterAppStateChangeNotification handler: plmSuspendComplete event signaled.");
  81. } else {
  82. SDL_SendAppEvent(SDL_EVENT_WILL_ENTER_FOREGROUND);
  83. }
  84. };
  85. if (RegisterAppStateChangeNotification(rascn, NULL, &hPLM)) {
  86. return SDL_SetError("[GDK] Unable to call RegisterAppStateChangeNotification");
  87. }
  88. // Register constrain/unconstrain handling
  89. auto raccn = [](BOOLEAN constrained, PVOID context) {
  90. SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "[GDK] in RegisterAppConstrainedChangeNotification handler");
  91. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  92. if (_this) {
  93. if (constrained && !((_this->windows) && _this->windows->text_input_active)) {
  94. SDL_SetKeyboardFocus(NULL);
  95. } else {
  96. SDL_SetKeyboardFocus(_this->windows);
  97. }
  98. }
  99. };
  100. if (RegisterAppConstrainedChangeNotification(raccn, NULL, &hCPLM)) {
  101. return SDL_SetError("[GDK] Unable to call RegisterAppConstrainedChangeNotification");
  102. }
  103. return true;
  104. }
  105. extern "C"
  106. void GDK_UnregisterChangeNotifications(void)
  107. {
  108. // Unregister suspend/resume handling
  109. UnregisterAppStateChangeNotification(hPLM);
  110. CloseHandle(plmSuspendComplete);
  111. // Unregister constrain/unconstrain handling
  112. UnregisterAppConstrainedChangeNotification(hCPLM);
  113. }
  114. extern "C"
  115. void SDL_GDKSuspendComplete()
  116. {
  117. if (plmSuspendComplete) {
  118. SetEvent(plmSuspendComplete);
  119. }
  120. }
  121. extern "C"
  122. bool SDL_GetGDKDefaultUser(XUserHandle *outUserHandle)
  123. {
  124. XAsyncBlock block = { 0 };
  125. HRESULT result;
  126. if (FAILED(result = XUserAddAsync(XUserAddOptions::AddDefaultUserAllowingUI, &block))) {
  127. return WIN_SetErrorFromHRESULT("XUserAddAsync", result);
  128. }
  129. do {
  130. result = XUserAddResult(&block, outUserHandle);
  131. } while (result == E_PENDING);
  132. if (FAILED(result)) {
  133. return WIN_SetErrorFromHRESULT("XUserAddResult", result);
  134. }
  135. return true;
  136. }