SDL: Added a cleanup function for virtual joysticks

From 47926d7bd0220b1fa9501de71454e96d4ef29228 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 26 Aug 2024 20:11:09 -0700
Subject: [PATCH] Added a cleanup function for virtual joysticks

---
 include/SDL3/SDL_joystick.h                | 1 +
 src/joystick/virtual/SDL_virtualjoystick.c | 4 ++++
 2 files changed, 5 insertions(+)

diff --git a/include/SDL3/SDL_joystick.h b/include/SDL3/SDL_joystick.h
index 62de3f22c626d..2e39b509c9f3b 100644
--- a/include/SDL3/SDL_joystick.h
+++ b/include/SDL3/SDL_joystick.h
@@ -451,6 +451,7 @@ typedef struct SDL_VirtualJoystickDesc
     SDL_bool (SDLCALL *SetLED)(void *userdata, Uint8 red, Uint8 green, Uint8 blue); /**< Implements SDL_SetJoystickLED() */
     SDL_bool (SDLCALL *SendEffect)(void *userdata, const void *data, int size); /**< Implements SDL_SendJoystickEffect() */
     SDL_bool (SDLCALL *SetSensorsEnabled)(void *userdata, SDL_bool enabled); /**< Implements SDL_SetGamepadSensorEnabled() */
+    void (SDLCALL *Cleanup)(void *userdata); /**< Cleans up the userdata when the joystick is detached */
 } SDL_VirtualJoystickDesc;
 
 /**
diff --git a/src/joystick/virtual/SDL_virtualjoystick.c b/src/joystick/virtual/SDL_virtualjoystick.c
index 294773e9d26e7..0bbb0b51f62c2 100644
--- a/src/joystick/virtual/SDL_virtualjoystick.c
+++ b/src/joystick/virtual/SDL_virtualjoystick.c
@@ -70,6 +70,10 @@ static void VIRTUAL_FreeHWData(joystick_hwdata *hwdata)
         return;
     }
 
+    if (hwdata->desc.Cleanup) {
+        hwdata->desc.Cleanup(hwdata->desc.userdata);
+    }
+
     // Remove hwdata from SDL-global list
     for (cur = g_VJoys; cur; prev = cur, cur = cur->next) {
         if (hwdata == cur) {