SDL: Use predefined names for constant keyboard and mouse IDs (a979d)

From a979d8d060d378d93e5b08e8a4f9fe84ccd9e819 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 12 May 2026 20:12:56 -0700
Subject: [PATCH] Use predefined names for constant keyboard and mouse IDs

Fixes https://github.com/libsdl-org/SDL/issues/15563

(cherry picked from commit fcaf5bbf8cf339eab4614e246f33ea67e9a34a28)
---
 src/events/SDL_keyboard.c | 24 ++++++++++++++++--------
 src/events/SDL_mouse.c    | 30 ++++++++++++++++++++++--------
 2 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c
index 0945339e4479d..f6969e37ed26b 100644
--- a/src/events/SDL_keyboard.c
+++ b/src/events/SDL_keyboard.c
@@ -201,14 +201,22 @@ SDL_KeyboardID *SDL_GetKeyboards(int *count)
 const char *SDL_GetKeyboardNameForID(SDL_KeyboardID instance_id)
 {
     const char *name = NULL;
-    if (!SDL_FindInHashTable(SDL_keyboard_names, (const void *)(uintptr_t)instance_id, (const void **)&name)) {
-        SDL_SetError("Keyboard %" SDL_PRIu32 " not found", instance_id);
-        return NULL;
-    }
-    if (!name) {
-        // SDL_strdup() failed during insert
-        SDL_OutOfMemory();
-        return NULL;
+
+    switch (instance_id) {
+    case SDL_GLOBAL_KEYBOARD_ID:
+        name = "Keyboard";
+        break;
+    default:
+        if (!SDL_FindInHashTable(SDL_keyboard_names, (const void *)(uintptr_t)instance_id, (const void **)&name)) {
+            SDL_SetError("Keyboard %" SDL_PRIu32 " not found", instance_id);
+            return NULL;
+        }
+        if (!name) {
+            // SDL_strdup() failed during insert
+            SDL_OutOfMemory();
+            return NULL;
+        }
+        break;
     }
     return name;
 }
diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c
index 5dd25d713aef3..96641495ae59b 100644
--- a/src/events/SDL_mouse.c
+++ b/src/events/SDL_mouse.c
@@ -422,14 +422,28 @@ SDL_MouseID *SDL_GetMice(int *count)
 const char *SDL_GetMouseNameForID(SDL_MouseID instance_id)
 {
     const char *name = NULL;
-    if (!SDL_FindInHashTable(SDL_mouse_names, (const void *)(uintptr_t)instance_id, (const void **)&name)) {
-        SDL_SetError("Mouse %" SDL_PRIu32 " not found", instance_id);
-        return NULL;
-    }
-    if (!name) {
-        // SDL_strdup() failed during insert
-        SDL_OutOfMemory();
-        return NULL;
+
+    switch (instance_id) {
+    case SDL_GLOBAL_MOUSE_ID:
+        name = "Mouse";
+        break;
+    case SDL_TOUCH_MOUSEID:
+        name = "Touch";
+        break;
+    case SDL_PEN_MOUSEID:
+        name = "Pen";
+        break;
+    default:
+        if (!SDL_FindInHashTable(SDL_mouse_names, (const void *)(uintptr_t)instance_id, (const void **)&name)) {
+            SDL_SetError("Mouse %" SDL_PRIu32 " not found", instance_id);
+            return NULL;
+        }
+        if (!name) {
+            // SDL_strdup() failed during insert
+            SDL_OutOfMemory();
+            return NULL;
+        }
+        break;
     }
     return name;
 }