SDL: WGI: Add workaround for delayed callbacks after WGI_JoystickQuit()

From 6312aaea0fe7fa9514ee2b114fcbb29e2bee2765 Mon Sep 17 00:00:00 2001
From: Cameron Gutman <[EMAIL REDACTED]>
Date: Sat, 6 Nov 2021 21:52:22 -0500
Subject: [PATCH] WGI: Add workaround for delayed callbacks after
 WGI_JoystickQuit()

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

diff --git a/src/joystick/windows/SDL_windows_gaming_input.c b/src/joystick/windows/SDL_windows_gaming_input.c
index ad68ab3e32..abb0ff2965 100644
--- a/src/joystick/windows/SDL_windows_gaming_input.c
+++ b/src/joystick/windows/SDL_windows_gaming_input.c
@@ -166,6 +166,12 @@ static HRESULT STDMETHODCALLTYPE IEventHandler_CRawGameControllerVtbl_InvokeAdde
     HRESULT hr;
     __x_ABI_CWindows_CGaming_CInput_CIRawGameController *controller = NULL;
 
+    /* We can get delayed calls to InvokeAdded() after WGI_JoystickQuit(). Do nothing if WGI is deinitialized.
+     * FIXME: Can we tell if WGI has been quit and reinitialized prior to a delayed callback? */
+    if (wgi.statics == NULL) {
+        return S_OK;
+    }
+
     hr = IUnknown_QueryInterface((IUnknown *)e, &IID_IRawGameController, (void **)&controller);
     if (SUCCEEDED(hr)) {
         char *name = NULL;