SDL: Send joystick added events on portable handheld platforms

From 977285ef6a44e3f6519b93c3e92432536f3c0936 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 25 Mar 2024 07:14:00 -0700
Subject: [PATCH] Send joystick added events on portable handheld platforms

The IDs on these drivers are hard-coded as device_index + 1, so that's the ID we will send during initialization.
---
 src/joystick/haiku/SDL_haikujoystick.cc | 14 ++++++++------
 src/joystick/n3ds/SDL_sysjoystick.c     |  1 +
 src/joystick/ps2/SDL_sysjoystick.c      |  1 +
 src/joystick/psp/SDL_sysjoystick.c      |  2 ++
 src/joystick/vita/SDL_sysjoystick.c     |  5 +++--
 5 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/src/joystick/haiku/SDL_haikujoystick.cc b/src/joystick/haiku/SDL_haikujoystick.cc
index 2de21f8a1bece..4191acff5deb7 100644
--- a/src/joystick/haiku/SDL_haikujoystick.cc
+++ b/src/joystick/haiku/SDL_haikujoystick.cc
@@ -70,12 +70,14 @@ extern "C"
         for (i = 0; (numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i) {
             if (joystick.GetDeviceName(i, name) == B_OK) {
                 if (joystick.Open(name) != B_ERROR) {
-                      BString stick_name;
-                      joystick.GetControllerName(&stick_name);
-                      SDL_joyport[numjoysticks] = SDL_strdup(name);
-                      SDL_joyname[numjoysticks] = SDL_CreateJoystickName(0, 0, NULL, stick_name.String());
-                      numjoysticks++;
-                      joystick.Close();
+                    BString stick_name;
+                    joystick.GetControllerName(&stick_name);
+                    SDL_joyport[numjoysticks] = SDL_strdup(name);
+                    SDL_joyname[numjoysticks] = SDL_CreateJoystickName(0, 0, NULL, stick_name.String());
+                    numjoysticks++;
+                    joystick.Close();
+
+                    SDL_PrivateJoystickAdded(numjoysticks);
                 }
             }
         }
diff --git a/src/joystick/n3ds/SDL_sysjoystick.c b/src/joystick/n3ds/SDL_sysjoystick.c
index f79521de93b65..5837d9166ceea 100644
--- a/src/joystick/n3ds/SDL_sysjoystick.c
+++ b/src/joystick/n3ds/SDL_sysjoystick.c
@@ -62,6 +62,7 @@ static void UpdateN3DSCStick(Uint64 timestamp, SDL_Joystick *joystick);
 static int N3DS_JoystickInit(void)
 {
     hidInit();
+    SDL_PrivateJoystickAdded(1);
     return 0;
 }
 
diff --git a/src/joystick/ps2/SDL_sysjoystick.c b/src/joystick/ps2/SDL_sysjoystick.c
index 922a48bd63005..5042dcda898bb 100644
--- a/src/joystick/ps2/SDL_sysjoystick.c
+++ b/src/joystick/ps2/SDL_sysjoystick.c
@@ -122,6 +122,7 @@ static int PS2_JoystickInit(void)
                 info->slot = (uint8_t)slot;
                 info->opened = 1;
                 enabled_pads++;
+                SDL_PrivateJoystickAdded(enabled_pads);
             }
         }
     }
diff --git a/src/joystick/psp/SDL_sysjoystick.c b/src/joystick/psp/SDL_sysjoystick.c
index 1c57a486065e4..544cc758d06d5 100644
--- a/src/joystick/psp/SDL_sysjoystick.c
+++ b/src/joystick/psp/SDL_sysjoystick.c
@@ -93,6 +93,8 @@ static int PSP_JoystickInit(void)
         analog_map[127 - i] = -1 * analog_map[i + 128];
     }
 
+    SDL_PrivateJoystickAdded(1);
+
     return 1;
 }
 
diff --git a/src/joystick/vita/SDL_sysjoystick.c b/src/joystick/vita/SDL_sysjoystick.c
index 9cd79f50b7b57..09b7ab82f9c2c 100644
--- a/src/joystick/vita/SDL_sysjoystick.c
+++ b/src/joystick/vita/SDL_sysjoystick.c
@@ -124,7 +124,8 @@ static int VITA_JoystickInit(void)
     // after the app has already started.
 
     SDL_numjoysticks = 1;
-    SDL_PrivateJoystickAdded(0);
+    SDL_PrivateJoystickAdded(SDL_numjoysticks);
+
     // How many additional paired controllers are there?
     sceCtrlGetControllerPortInfo(&myPortInfo);
 
@@ -132,8 +133,8 @@ static int VITA_JoystickInit(void)
     // and that is the first one, so start at port 2
     for (i = 2; i <= 4; i++) {
         if (myPortInfo.port[i] != SCE_CTRL_TYPE_UNPAIRED) {
+            ++SDL_numjoysticks;
             SDL_PrivateJoystickAdded(SDL_numjoysticks);
-            SDL_numjoysticks++;
         }
     }
     return SDL_numjoysticks;