sdl2-compat: override SDL_Quit and SDL_QuitSubsystem, adjust the gesture code.

From d378cb36d1b647cbf1903ed2a39f3a187005710c Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Thu, 27 Jul 2023 07:55:50 +0300
Subject: [PATCH] override SDL_Quit and SDL_QuitSubsystem, adjust the gesture
 code.

---
 src/sdl2_compat.c | 52 ++++++++++++++++++++++-------------------------
 src/sdl2_protos.h |  3 ---
 src/sdl3_syms.h   |  4 ++--
 3 files changed, 26 insertions(+), 33 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 7868eba..18f11da 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -2559,32 +2559,6 @@ GestureAddTouch(const SDL_TouchID touchId)
     return &GestureTouches[GestureNumTouches++];
 }
 
-static int
-GestureDelTouch(const SDL_TouchID touchId)
-{
-    int i;
-
-    for (i = 0; i < GestureNumTouches; i++) {
-        if (GestureTouches[i].touchId == touchId) {
-            break;
-        }
-    }
-
-    if (i == GestureNumTouches) {
-        /* not found */
-        return -1;
-    }
-
-    SDL3_free(GestureTouches[i].dollarTemplate);
-    SDL3_zero(GestureTouches[i]);
-
-    GestureNumTouches--;
-    if (i != GestureNumTouches) {
-        SDL3_copyp(&GestureTouches[i], &GestureTouches[GestureNumTouches]);
-    }
-    return 0;
-}
-
 static GestureTouch *
 GestureGetTouch(const SDL_TouchID touchId)
 {
@@ -2630,12 +2604,16 @@ SDL_RecordGesture(SDL_TouchID touchId)
     return 1;
 }
 
-/* !!! FIXME: we need to hook this up when we override SDL_Quit */
 static void
 GestureQuit(void)
 {
+    int i;
+    for (i = 0; i < GestureNumTouches; i++) {
+        SDL3_free(GestureTouches[i].dollarTemplate);
+    }
     SDL3_free(GestureTouches);
     GestureTouches = NULL;
+    GestureNumTouches = 0;
 }
 
 static unsigned long
@@ -3351,7 +3329,7 @@ SDL_AudioInit(const char *driver_name)
 DECLSPEC void SDLCALL
 SDL_AudioQuit(void)
 {
-    SDL_QuitSubSystem(SDL_INIT_AUDIO);
+    SDL3_QuitSubSystem(SDL_INIT_AUDIO);
 }
 
 DECLSPEC int SDLCALL
@@ -3412,6 +3390,24 @@ SDL_InitSubSystem(Uint32 flags)
     return ret;
 }
 
+DECLSPEC void SDLCALL
+SDL_Quit(void)
+{
+    if (SDL3_WasInit(SDL_INIT_VIDEO)) {
+        GestureQuit();
+    }
+    SDL3_Quit();
+}
+
+DECLSPEC void SDLCALL
+SDL_QuitSubSystem(Uint32 flags)
+{
+    if (flags & SDL_INIT_VIDEO) {
+        GestureQuit();
+    }
+    SDL3_QuitSubSystem(flags);
+}
+
 DECLSPEC int SDLCALL
 SDL_GL_GetSwapInterval(void)
 {
diff --git a/src/sdl2_protos.h b/src/sdl2_protos.h
index ae437ac..b63d168 100644
--- a/src/sdl2_protos.h
+++ b/src/sdl2_protos.h
@@ -873,9 +873,7 @@ SDL2_PROTO(void*,GetTextureUserData,(SDL_Texture *a))
 SDL2_PROTO(int,RenderGeometry,(SDL_Renderer *a, SDL_Texture *b, const SDL_Vertex *c, int d, const int *e, int f))
 SDL2_PROTO(int,RenderGeometryRaw,(SDL_Renderer *a, SDL_Texture *b, const float *c, int d, const SDL_Color *e, int f, const float *g, int h, int i, const void *j, int k, int l))
 SDL2_PROTO(int,RenderSetVSync,(SDL_Renderer *a, int b))
-#if !SDL2_PROTO_NO_VARARGS
 SDL2_PROTO(int,asprintf,(char **a, SDL_PRINTF_FORMAT_STRING const char *b, ...))
-#endif
 SDL2_PROTO(int,vasprintf,(char **a, const char *b, va_list c))
 SDL2_PROTO(void*,GetWindowICCProfile,(SDL_Window *a, size_t *b))
 SDL2_PROTO(Uint64,GetTicks64,(void))
@@ -940,7 +938,6 @@ SDL2_PROTO(SDL_bool,HasLASX,(void))
 SDL2_PROTO(ID3D12Device*,RenderGetD3D12Device,(SDL_Renderer *a))
 #endif
 SDL2_PROTO(size_t,utf8strnlen,(const char *a, size_t b))
-
 #if defined(__GDK__)
 SDL2_PROTO(int,GDKGetTaskQueue,(XTaskQueueHandle *a))
 SDL2_PROTO(int,GDKRunApp,(SDL_main_func a, void *b))
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index debc6b9..cf4528f 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -80,9 +80,9 @@ SDL3_SYM_PASSTHROUGH(const char*,AndroidGetExternalStoragePath,(void),(),return)
 
 SDL3_SYM(int,Init,(Uint32 a),(a),return)
 SDL3_SYM(int,InitSubSystem,(Uint32 a),(a),return)
-SDL3_SYM_PASSTHROUGH(void,QuitSubSystem,(Uint32 a),(a),)
+SDL3_SYM(void,QuitSubSystem,(Uint32 a),(a),)
 SDL3_SYM_PASSTHROUGH(Uint32,WasInit,(Uint32 a),(a),return)
-SDL3_SYM_PASSTHROUGH(void,Quit,(void),(),)
+SDL3_SYM(void,Quit,(void),(),)
 SDL3_SYM_PASSTHROUGH(SDL_AssertState,ReportAssertion,(SDL_AssertData *a, const char *b, const char *c, int d),(a,b,c,d),return)
 SDL3_SYM_PASSTHROUGH(void,SetAssertionHandler,(SDL_AssertionHandler a, void *b),(a,b),)
 SDL3_SYM_PASSTHROUGH(const SDL_AssertData*,GetAssertionReport,(void),(),return)