SDL: Lock joysticks when removing a controller on the WGI thread

From a3900a751e18630f05ce6164c351ade6331548b3 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 8 Sep 2022 13:59:25 -0700
Subject: [PATCH] Lock joysticks when removing a controller on the WGI thread

---
 src/joystick/windows/SDL_windows_gaming_input.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/joystick/windows/SDL_windows_gaming_input.c b/src/joystick/windows/SDL_windows_gaming_input.c
index 657650cc65a..863b3f50486 100644
--- a/src/joystick/windows/SDL_windows_gaming_input.c
+++ b/src/joystick/windows/SDL_windows_gaming_input.c
@@ -401,6 +401,14 @@ static HRESULT STDMETHODCALLTYPE IEventHandler_CRawGameControllerVtbl_InvokeRemo
     HRESULT hr;
     __x_ABI_CWindows_CGaming_CInput_CIRawGameController *controller = NULL;
 
+    SDL_LockJoysticks();
+
+    /* Can we get delayed calls to InvokeRemoved() after WGI_JoystickQuit()? */
+    if (SDL_JoysticksQuitting() || !SDL_JoysticksInitialized()) {
+        SDL_UnlockJoysticks();
+        return S_OK;
+    }
+
     hr = __x_ABI_CWindows_CGaming_CInput_CIRawGameController_QueryInterface(e, &IID_IRawGameController, (void **)&controller);
     if (SUCCEEDED(hr)) {
         int i;
@@ -426,6 +434,9 @@ static HRESULT STDMETHODCALLTYPE IEventHandler_CRawGameControllerVtbl_InvokeRemo
 
         __x_ABI_CWindows_CGaming_CInput_CIRawGameController_Release(controller);
     }
+
+    SDL_UnlockJoysticks();
+
     return S_OK;
 }