GDK build: move suspend/constrain setup back to previous compilation unit

This commit is contained in:
Daniel Ludwig
2024-07-24 15:11:03 +02:00
committed by Ozkan Sezer
parent 0a678a654e
commit 7e48d4522b
3 changed files with 67 additions and 46 deletions

View File

@@ -73,6 +73,66 @@ void GDK_DispatchTaskQueue(void)
} }
} }
extern "C"
int GDK_RegisterChangeNotifications(void)
{
/* Register suspend/resume handling */
plmSuspendComplete = CreateEventEx(nullptr, nullptr, 0, EVENT_MODIFY_STATE | SYNCHRONIZE);
if (!plmSuspendComplete) {
SDL_SetError("[GDK] Unable to create plmSuspendComplete event");
return -1;
}
auto rascn = [](BOOLEAN quiesced, PVOID context) {
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "[GDK] in RegisterAppStateChangeNotification handler");
if (quiesced) {
ResetEvent(plmSuspendComplete);
SDL_SendAppEvent(SDL_EVENT_DID_ENTER_BACKGROUND);
// To defer suspension, we must wait to exit this callback.
// IMPORTANT: The app must call SDL_GDKSuspendComplete() to release this lock.
(void)WaitForSingleObject(plmSuspendComplete, INFINITE);
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "[GDK] in RegisterAppStateChangeNotification handler: plmSuspendComplete event signaled.");
} else {
SDL_SendAppEvent(SDL_EVENT_WILL_ENTER_FOREGROUND);
}
};
if (RegisterAppStateChangeNotification(rascn, NULL, &hPLM)) {
SDL_SetError("[GDK] Unable to call RegisterAppStateChangeNotification");
return -1;
}
/* Register constrain/unconstrain handling */
auto raccn = [](BOOLEAN constrained, PVOID context) {
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "[GDK] in RegisterAppConstrainedChangeNotification handler");
SDL_VideoDevice *_this = SDL_GetVideoDevice();
if (_this) {
if (constrained) {
SDL_SetKeyboardFocus(NULL);
} else {
SDL_SetKeyboardFocus(_this->windows);
}
}
};
if (RegisterAppConstrainedChangeNotification(raccn, NULL, &hCPLM)) {
SDL_SetError("[GDK] Unable to call RegisterAppConstrainedChangeNotification");
return -1;
}
return 0;
}
extern "C"
void GDK_UnregisterChangeNotifications(void)
{
/* Unregister suspend/resume handling */
UnregisterAppStateChangeNotification(hPLM);
CloseHandle(plmSuspendComplete);
/* Unregister constrain/unconstrain handling */
UnregisterAppConstrainedChangeNotification(hCPLM);
}
extern "C" extern "C"
void SDL_GDKSuspendComplete() void SDL_GDKSuspendComplete()
{ {

View File

@@ -22,3 +22,7 @@
/* This is called from WIN_PumpEvents on GDK */ /* This is called from WIN_PumpEvents on GDK */
extern void GDK_DispatchTaskQueue(void); extern void GDK_DispatchTaskQueue(void);
extern int GDK_RegisterChangeNotifications(void);
extern void GDK_UnregisterChangeNotifications(void);

View File

@@ -21,6 +21,7 @@
#include "SDL_internal.h" #include "SDL_internal.h"
extern "C" { extern "C" {
#include "../../core/gdk/SDL_gdk.h"
#include "../../core/windows/SDL_windows.h" #include "../../core/windows/SDL_windows.h"
#include "../../events/SDL_events_c.h" #include "../../events/SDL_events_c.h"
} }
@@ -105,58 +106,14 @@ int SDL_RunApp(int, char**, SDL_main_func mainFunction, void *reserved)
SDL_SetMainReady(); SDL_SetMainReady();
/* Register suspend/resume handling */ if (GDK_RegisterChangeNotifications() != 0) {
plmSuspendComplete = CreateEventEx(nullptr, nullptr, 0, EVENT_MODIFY_STATE | SYNCHRONIZE);
if (!plmSuspendComplete) {
SDL_SetError("[GDK] Unable to create plmSuspendComplete event");
return -1;
}
auto rascn = [](BOOLEAN quiesced, PVOID context) {
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "[GDK] in RegisterAppStateChangeNotification handler");
if (quiesced) {
ResetEvent(plmSuspendComplete);
SDL_SendAppEvent(SDL_EVENT_DID_ENTER_BACKGROUND);
// To defer suspension, we must wait to exit this callback.
// IMPORTANT: The app must call SDL_GDKSuspendComplete() to release this lock.
(void)WaitForSingleObject(plmSuspendComplete, INFINITE);
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "[GDK] in RegisterAppStateChangeNotification handler: plmSuspendComplete event signaled.");
} else {
SDL_SendAppEvent(SDL_EVENT_WILL_ENTER_FOREGROUND);
}
};
if (RegisterAppStateChangeNotification(rascn, NULL, &hPLM)) {
SDL_SetError("[GDK] Unable to call RegisterAppStateChangeNotification");
return -1;
}
/* Register constrain/unconstrain handling */
auto raccn = [](BOOLEAN constrained, PVOID context) {
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "[GDK] in RegisterAppConstrainedChangeNotification handler");
SDL_VideoDevice *_this = SDL_GetVideoDevice();
if (_this) {
if (constrained) {
SDL_SetKeyboardFocus(NULL);
} else {
SDL_SetKeyboardFocus(_this->windows);
}
}
};
if (RegisterAppConstrainedChangeNotification(raccn, NULL, &hCPLM)) {
SDL_SetError("[GDK] Unable to call RegisterAppConstrainedChangeNotification");
return -1; return -1;
} }
/* Run the application main() code */ /* Run the application main() code */
result = mainFunction(argc, argv); result = mainFunction(argc, argv);
/* Unregister suspend/resume handling */ GDK_UnregisterChangeNotifications();
UnregisterAppStateChangeNotification(hPLM);
CloseHandle(plmSuspendComplete);
/* Unregister constrain/unconstrain handling */
UnregisterAppConstrainedChangeNotification(hCPLM);
/* !!! FIXME: This follows the docs exactly, but for some reason still leaks handles on exit? */ /* !!! FIXME: This follows the docs exactly, but for some reason still leaks handles on exit? */
/* Terminate the task queue and dispatch any pending tasks */ /* Terminate the task queue and dispatch any pending tasks */