SDL: Fixed bug #2032: add SDL_GetTouchName to expose the driver name of the device (Thanks @mgerhardy!)

From 34d4f5b14e5b009e94ff752f3c4e2c37033b78c4 Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Tue, 8 Feb 2022 11:32:20 +0100
Subject: [PATCH] Fixed bug #2032: add SDL_GetTouchName to expose the driver
 name of the device (Thanks @mgerhardy!)

---
 include/SDL_touch.h      |  5 +++++
 src/events/SDL_touch.c   | 12 ++++++++++++
 src/events/SDL_touch_c.h |  1 +
 3 files changed, 18 insertions(+)

diff --git a/include/SDL_touch.h b/include/SDL_touch.h
index 9b00716b2a9..d7f6a83b469 100644
--- a/include/SDL_touch.h
+++ b/include/SDL_touch.h
@@ -95,6 +95,11 @@ extern DECLSPEC int SDLCALL SDL_GetNumTouchDevices(void);
  */
 extern DECLSPEC SDL_TouchID SDLCALL SDL_GetTouchDevice(int index);
 
+/**
+ *  \brief Get the touch device name as reported from the driver or NULL if the index is invalid.
+ */
+extern DECLSPEC const char* SDLCALL SDL_GetTouchName(int index);
+
 /**
  * Get the type of the given touch device.
  *
diff --git a/src/events/SDL_touch.c b/src/events/SDL_touch.c
index fda5305203d..c3534f00e43 100644
--- a/src/events/SDL_touch.c
+++ b/src/events/SDL_touch.c
@@ -63,6 +63,16 @@ SDL_GetTouchDevice(int index)
     return SDL_touchDevices[index]->id;
 }
 
+const char*
+SDL_GetTouchName(int index)
+{
+    if (index < 0 || index >= SDL_num_touch) {
+        SDL_SetError("Unknown touch device");
+        return NULL;
+    }
+    return SDL_touchDevices[index]->name;
+}
+
 static int
 SDL_GetTouchIndex(SDL_TouchID id)
 {
@@ -185,6 +195,7 @@ SDL_AddTouch(SDL_TouchID touchID, SDL_TouchDeviceType type, const char *name)
     SDL_touchDevices[index]->num_fingers = 0;
     SDL_touchDevices[index]->max_fingers = 0;
     SDL_touchDevices[index]->fingers = NULL;
+    SDL_touchDevices[index]->name = SDL_strdup(name ? name : "");
 
     /* Record this touch device for gestures */
     /* We could do this on the fly in the gesture code if we wanted */
@@ -452,6 +463,7 @@ SDL_DelTouch(SDL_TouchID id)
         SDL_free(touch->fingers[i]);
     }
     SDL_free(touch->fingers);
+    SDL_free(touch->name);
     SDL_free(touch);
 
     SDL_num_touch--;
diff --git a/src/events/SDL_touch_c.h b/src/events/SDL_touch_c.h
index c9d9a326542..4d5caf134b5 100644
--- a/src/events/SDL_touch_c.h
+++ b/src/events/SDL_touch_c.h
@@ -31,6 +31,7 @@ typedef struct SDL_Touch
     int num_fingers;
     int max_fingers;
     SDL_Finger** fingers;
+    char *name;
 } SDL_Touch;