SDL: Even more miscellaneous gamepad buttons!

From d04fea8b8774b1d30613e86afffe89d91eb10e7c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 1 Feb 2024 20:34:05 -0800
Subject: [PATCH] Even more miscellaneous gamepad buttons!

---
 include/SDL3/SDL_gamepad.h                 |  2 ++
 src/joystick/SDL_gamepad.c                 |  6 +++++-
 src/joystick/SDL_joystick_c.h              |  2 ++
 src/joystick/virtual/SDL_virtualjoystick.c | 10 ++++++++++
 test/gamepadutils.c                        |  2 ++
 test/testcontroller.c                      |  2 ++
 6 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/include/SDL3/SDL_gamepad.h b/include/SDL3/SDL_gamepad.h
index b2eb44589c33..31ea9b6dd6db 100644
--- a/include/SDL3/SDL_gamepad.h
+++ b/include/SDL3/SDL_gamepad.h
@@ -122,6 +122,8 @@ typedef enum
     SDL_GAMEPAD_BUTTON_MISC2,           /* Additional button */
     SDL_GAMEPAD_BUTTON_MISC3,           /* Additional button */
     SDL_GAMEPAD_BUTTON_MISC4,           /* Additional button */
+    SDL_GAMEPAD_BUTTON_MISC5,           /* Additional button */
+    SDL_GAMEPAD_BUTTON_MISC6,           /* Additional button */
     SDL_GAMEPAD_BUTTON_MAX
 } SDL_GamepadButton;
 
diff --git a/src/joystick/SDL_gamepad.c b/src/joystick/SDL_gamepad.c
index 02c4221510b4..89882c9a400e 100644
--- a/src/joystick/SDL_gamepad.c
+++ b/src/joystick/SDL_gamepad.c
@@ -1088,7 +1088,9 @@ static const char *map_StringForGamepadButton[] = {
     "touchpad",
     "misc2",
     "misc3",
-    "misc4"
+    "misc4",
+    "misc5",
+    "misc6"
 };
 SDL_COMPILE_TIME_ASSERT(map_StringForGamepadButton, SDL_arraysize(map_StringForGamepadButton) == SDL_GAMEPAD_BUTTON_MAX);
 
@@ -1757,6 +1759,8 @@ static GamepadMapping_t *SDL_PrivateGenerateAutomaticGamepadMapping(const char *
     SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc2", &raw_map->misc2);
     SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc3", &raw_map->misc3);
     SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc4", &raw_map->misc4);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc5", &raw_map->misc5);
+    SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "misc6", &raw_map->misc6);
     /* Keep using paddle1-4 in the generated mapping so that it can be
      * reused with SDL2 */
     SDL_PrivateAppendToMappingString(mapping, sizeof(mapping), "paddle1", &raw_map->right_paddle1);
diff --git a/src/joystick/SDL_joystick_c.h b/src/joystick/SDL_joystick_c.h
index 14fe0ce72dd5..3004bb1485a9 100644
--- a/src/joystick/SDL_joystick_c.h
+++ b/src/joystick/SDL_joystick_c.h
@@ -215,6 +215,8 @@ typedef struct SDL_GamepadMapping
     SDL_InputMapping misc2;
     SDL_InputMapping misc3;
     SDL_InputMapping misc4;
+    SDL_InputMapping misc5;
+    SDL_InputMapping misc6;
     SDL_InputMapping right_paddle1;
     SDL_InputMapping left_paddle1;
     SDL_InputMapping right_paddle2;
diff --git a/src/joystick/virtual/SDL_virtualjoystick.c b/src/joystick/virtual/SDL_virtualjoystick.c
index 80177fbdb094..526ec24cc9bf 100644
--- a/src/joystick/virtual/SDL_virtualjoystick.c
+++ b/src/joystick/virtual/SDL_virtualjoystick.c
@@ -702,6 +702,16 @@ static SDL_bool VIRTUAL_JoystickGetGamepadMapping(int device_index, SDL_GamepadM
         out->misc4.target = current_button++;
     }
 
+    if (current_button < hwdata->desc.nbuttons && (hwdata->desc.button_mask & (1 << SDL_GAMEPAD_BUTTON_MISC5))) {
+        out->misc5.kind = EMappingKind_Button;
+        out->misc5.target = current_button++;
+    }
+
+    if (current_button < hwdata->desc.nbuttons && (hwdata->desc.button_mask & (1 << SDL_GAMEPAD_BUTTON_MISC6))) {
+        out->misc6.kind = EMappingKind_Button;
+        out->misc6.target = current_button++;
+    }
+
     if (current_axis < hwdata->desc.naxes && (hwdata->desc.axis_mask & (1 << SDL_GAMEPAD_AXIS_LEFTX))) {
         out->leftx.kind = EMappingKind_Axis;
         out->leftx.target = current_axis++;
diff --git a/test/gamepadutils.c b/test/gamepadutils.c
index 43b98aeb20f6..cfe414687ac9 100644
--- a/test/gamepadutils.c
+++ b/test/gamepadutils.c
@@ -652,6 +652,8 @@ static const char *gamepad_button_names[] = {
     "Misc2",
     "Misc3",
     "Misc4",
+    "Misc5",
+    "Misc6",
 };
 SDL_COMPILE_TIME_ASSERT(gamepad_button_names, SDL_arraysize(gamepad_button_names) == SDL_GAMEPAD_BUTTON_MAX);
 
diff --git a/test/testcontroller.c b/test/testcontroller.c
index ab695b0ff804..1c093d13eee9 100644
--- a/test/testcontroller.c
+++ b/test/testcontroller.c
@@ -785,6 +785,8 @@ static const char *GetBindingInstruction(void)
     case SDL_GAMEPAD_BUTTON_MISC2:
     case SDL_GAMEPAD_BUTTON_MISC3:
     case SDL_GAMEPAD_BUTTON_MISC4:
+    case SDL_GAMEPAD_BUTTON_MISC5:
+    case SDL_GAMEPAD_BUTTON_MISC6:
         return "Press any additional button not already bound";
     case SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE:
         return "Move the left thumbstick to the left";