SDL: Added GDK app constrained/unconstrained events (#9608) (3d180)

From 3d18043005852afd6ec549a64016d9802f33bd7d Mon Sep 17 00:00:00 2001
From: Austin Sojka <[EMAIL REDACTED]>
Date: Tue, 30 Apr 2024 12:19:29 -0500
Subject: [PATCH] Added GDK app constrained/unconstrained events (#9608)

(cherry picked from commit 7554e82ec36bd1a2a78875d8def9f94d9854ab12)
---
 src/core/gdk/SDL_gdk.cpp | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/core/gdk/SDL_gdk.cpp b/src/core/gdk/SDL_gdk.cpp
index c5bac8aaead4e..06995686740f2 100644
--- a/src/core/gdk/SDL_gdk.cpp
+++ b/src/core/gdk/SDL_gdk.cpp
@@ -36,6 +36,7 @@ extern "C" {
 static XTaskQueueHandle GDK_GlobalTaskQueue;
 
 PAPPSTATE_REGISTRATION hPLM = {};
+PAPPCONSTRAIN_REGISTRATION hCPLM = {};
 HANDLE plmSuspendComplete = nullptr;
 
 extern "C" DECLSPEC int
@@ -177,6 +178,23 @@ SDL_GDKRunApp(SDL_main_func mainFunction, void *reserved)
             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;
+        }
+
         /* Run the application main() code */
         result = mainFunction(argc, argv);
 
@@ -184,6 +202,9 @@ SDL_GDKRunApp(SDL_main_func mainFunction, void *reserved)
         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? */
         /* Terminate the task queue and dispatch any pending tasks */
         XTaskQueueTerminate(taskQueue, false, nullptr, nullptr);