From d307ae547c7b3cef3f8b2ca4a8c464eb3a31254c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 2 Nov 2026 08:38:08 -0800
Subject: [PATCH] Simplification assuming no separate joystick thread
---
include/SDL3/SDL_hints.h | 16 ---
src/joystick/SDL_joystick.c | 123 ++++-----------------
src/joystick/SDL_joystick_c.h | 2 +-
src/joystick/SDL_sysjoystick.h | 5 -
src/joystick/dos/SDL_sysjoystick.c | 2 +-
src/joystick/haiku/SDL_haikujoystick.cc | 2 +-
src/joystick/n3ds/SDL_sysjoystick.c | 2 +-
src/joystick/ps2/SDL_sysjoystick.c | 2 +-
src/joystick/psp/SDL_sysjoystick.c | 2 +-
src/joystick/virtual/SDL_virtualjoystick.c | 2 +-
src/joystick/vita/SDL_sysjoystick.c | 2 +-
11 files changed, 31 insertions(+), 129 deletions(-)
diff --git a/include/SDL3/SDL_hints.h b/include/SDL3/SDL_hints.h
index bf543319586d6..8c5a97447f7a0 100644
--- a/include/SDL3/SDL_hints.h
+++ b/include/SDL3/SDL_hints.h
@@ -2436,22 +2436,6 @@ extern "C" {
*/
#define SDL_HINT_JOYSTICK_ROG_CHAKRAM "SDL_JOYSTICK_ROG_CHAKRAM"
-/**
- * A variable controlling whether a separate thread should be used for
- * handling joystick detection on Windows.
- *
- * The variable can be set to the following values:
- *
- * - "0": A separate thread is not used.
- * - "1": A separate thread is used for handling device change messages.
- * (default)
- *
- * This hint should be set before SDL is initialized.
- *
- * \since This hint is available since SDL 3.2.0.
- */
-#define SDL_HINT_JOYSTICK_THREAD "SDL_JOYSTICK_THREAD"
-
/**
* A variable containing a list of throttle style controllers.
*
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index f498443763724..78e8036d2ac36 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -114,11 +114,9 @@ static int SDL_joysticks_locked;
static bool SDL_joysticks_initialized;
static bool SDL_joysticks_quitting;
static bool SDL_joystick_being_added;
-static void *SDL_pending_joystick_devices;
static bool SDL_joystick_hid_notifications SDL_GUARDED_BY(SDL_event_lock);
static Uint32 SDL_joystick_hid_change_count SDL_GUARDED_BY(SDL_event_lock);
static Uint64 SDL_last_device_update SDL_GUARDED_BY(SDL_event_lock);
-static SDL_JoystickDevice *SDL_pending_manual_devices SDL_GUARDED_BY(SDL_event_lock);
static SDL_JoystickDevice *SDL_joystick_devices SDL_GUARDED_BY(SDL_event_lock);
static SDL_Joystick *SDL_joysticks SDL_GUARDED_BY(SDL_event_lock);
static int SDL_joystick_player_count SDL_GUARDED_BY(SDL_event_lock);
@@ -936,12 +934,9 @@ void SDL_RequestJoystickDeviceUpdate(bool immediate)
}
}
-static void SDL_UpdatePendingJoystickDevices(void)
+static SDL_JoystickDevice *SDL_UpdatePendingJoystickDevices(void)
{
- SDL_JoystickDeviceList *pending = (SDL_JoystickDeviceList *)SDL_calloc(1, sizeof(*pending));
- if (!pending) {
- return;
- }
+ SDL_JoystickDevice *pending_devices = NULL;
SDL_JoystickDevice *tail = NULL;
for (int i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
@@ -956,10 +951,10 @@ static void SDL_UpdatePendingJoystickDevices(void)
SDL_JoystickDevice *devices = driver->QueryDevices();
if (devices) {
- if (pending->devices) {
+ if (pending_devices) {
tail->next = devices;
} else {
- pending->devices = devices;
+ pending_devices = devices;
tail = devices;
}
while (tail->next) {
@@ -968,15 +963,10 @@ static void SDL_UpdatePendingJoystickDevices(void)
}
}
- if (pending->devices) {
- pending->devices = SDL_RemoveHiddenDevices(pending->devices);
- }
-
- SDL_JoystickDeviceList *stale = SDL_SetAtomicPointer(&SDL_pending_joystick_devices, pending);
- if (stale) {
- SDL_DestroyJoystickDevices(stale->devices);
- SDL_free(stale);
+ if (pending_devices) {
+ pending_devices = SDL_RemoveHiddenDevices(pending_devices);
}
+ return pending_devices;
}
// Remove and return the device in the list with the same driver and unique ID, if any
@@ -1000,32 +990,11 @@ static SDL_JoystickDevice *SDL_FindAndRemoveJoystickDevice(SDL_JoystickDevice *d
return NULL;
}
-static void SDL_ProcessPendingJoystickDevices(void)
+static void SDL_ProcessPendingJoystickDevices(SDL_JoystickDevice *pending_devices)
{
SDL_AssertJoysticksLocked();
- SDL_JoystickDeviceList *pending = SDL_SetAtomicPointer(&SDL_pending_joystick_devices, NULL);
- if (!pending && !SDL_pending_manual_devices) {
- return;
- }
-
- SDL_JoystickDevice *pending_devices = NULL;
- if (pending) {
- pending_devices = pending->devices;
- }
- if (SDL_pending_manual_devices) {
- SDL_JoystickDevice *tail = SDL_pending_manual_devices;
- while (tail->next) {
- tail = tail->next;
- }
- tail->next = pending_devices;
-
- pending_devices = SDL_pending_manual_devices;
- SDL_pending_manual_devices = NULL;
- }
-
// Reconcile existing devices with the new list
- // Scanned devices are only removed if there is a new scan
SDL_JoystickDevice *devices = NULL;
SDL_JoystickDevice *tail = NULL;
SDL_JoystickDevice *removed = NULL;
@@ -1033,7 +1002,7 @@ static void SDL_ProcessPendingJoystickDevices(void)
while (old) {
SDL_JoystickDevice *next = old->next;
bool keep = false;
- if (old->manually_added || !pending) {
+ if (old->manually_added) {
keep = true;
} else {
// Pull the new device matching the old one out of the list
@@ -1080,10 +1049,7 @@ static void SDL_ProcessPendingJoystickDevices(void)
}
// Send events for devices that are ready to be published
- SDL_JoystickDevice *prev = NULL;
- SDL_JoystickDevice *next = NULL;
- for (SDL_JoystickDevice *device = devices; device; device = next) {
- next = device->next;
+ for (SDL_JoystickDevice *device = devices; device; device = device->next) {
if (!device->published && !device->initializing) {
bool prepared = true;
if (device->driver->PrepareDevice) {
@@ -1095,53 +1061,37 @@ static void SDL_ProcessPendingJoystickDevices(void)
}
device->published = true;
SDL_PrivateJoystickAdded(device->instance_id);
- } else if (device->manually_added) {
- if (prev) {
- prev->next = next;
- } else {
- SDL_joystick_devices = next;
- }
- SDL_DestroyJoystickDevice(device);
- continue;
}
}
- prev = device;
}
-
- SDL_free(pending);
}
-SDL_JoystickID SDL_AddJoystickDevice(SDL_JoystickDevice *device, bool synchronous)
+SDL_JoystickID SDL_AddJoystickDevice(SDL_JoystickDevice *device)
{
SDL_AssertJoysticksLocked();
- SDL_JoystickID instance_id = device->instance_id;
- if (!instance_id) {
- instance_id = device->instance_id = SDL_GetNextObjectID();
+ SDL_assert(!device->driver->PrepareDevice);
+
+ if (!device->instance_id) {
+ device->instance_id = SDL_GetNextObjectID();
}
device->manually_added = true;
+ device->published = true;
- // Add this to the end of the pending device list
- SDL_JoystickDevice *tail = SDL_pending_manual_devices;
- if (tail) {
+ // Add this to the end of the device list
+ if (SDL_joystick_devices) {
+ SDL_JoystickDevice *tail = SDL_joystick_devices;
while (tail->next) {
tail = tail->next;
}
tail->next = device;
} else {
- SDL_pending_manual_devices = device;
+ SDL_joystick_devices = device;
}
- if (synchronous) {
- SDL_ProcessPendingJoystickDevices();
-
- // Make sure the device was published
- if (!SDL_GetJoystickDevice(instance_id)) {
- instance_id = 0;
- }
- }
+ SDL_PrivateJoystickAdded(device->instance_id);
- return instance_id;
+ return device->instance_id;
}
void SDL_RemoveJoystickDevice(SDL_JoystickID instance_id)
@@ -1153,20 +1103,6 @@ void SDL_RemoveJoystickDevice(SDL_JoystickID instance_id)
}
SDL_JoystickDevice *prev = NULL;
- for (SDL_JoystickDevice *device = SDL_pending_manual_devices; device; device = device->next) {
- if (device->instance_id == instance_id) {
- if (prev) {
- prev->next = device->next;
- } else {
- SDL_pending_manual_devices = device->next;
- }
- SDL_DestroyJoystickDevice(device);
- return;
- }
- prev = device;
- }
-
- prev = NULL;
for (SDL_JoystickDevice *device = SDL_joystick_devices; device; device = device->next) {
if (device->instance_id == instance_id) {
SDL_assert(device->manually_added);
@@ -1240,10 +1176,8 @@ static void SDL_UpdateJoystickDevices(bool update)
if (update) {
SDL_last_device_update = SDL_GetTicks();
- SDL_UpdatePendingJoystickDevices();
+ SDL_ProcessPendingJoystickDevices(SDL_UpdatePendingJoystickDevices());
}
-
- SDL_ProcessPendingJoystickDevices();
}
static int SDL_FindFreePlayerIndex(void)
@@ -2956,22 +2890,11 @@ void SDL_QuitJoysticks(void)
SDL_CloseJoystick(SDL_joysticks);
}
- if (SDL_pending_manual_devices) {
- SDL_DestroyJoystickDevices(SDL_pending_manual_devices);
- SDL_pending_manual_devices = NULL;
- }
-
if (SDL_joystick_devices) {
SDL_DestroyJoystickDevices(SDL_joystick_devices);
SDL_joystick_devices = NULL;
}
- SDL_JoystickDeviceList *pending = SDL_SetAtomicPointer(&SDL_pending_joystick_devices, NULL);
- if (pending) {
- SDL_DestroyJoystickDevices(pending->devices);
- SDL_free(pending);
- }
-
// Quit drivers in reverse order to avoid breaking dependencies between drivers
for (i = SDL_arraysize(SDL_joystick_drivers) - 1; i >= 0; --i) {
SDL_JoystickDriver *driver = SDL_GetJoystickDriver(i);
diff --git a/src/joystick/SDL_joystick_c.h b/src/joystick/SDL_joystick_c.h
index cdeb45eb18e33..c6a74b8932bf6 100644
--- a/src/joystick/SDL_joystick_c.h
+++ b/src/joystick/SDL_joystick_c.h
@@ -70,7 +70,7 @@ extern void SDL_AddJoystickDeviceMatchXInput(SDL_JoystickDevice *device, int xin
extern SDL_JoystickDevice *SDL_GetJoystickDevices(void);
// Manually publish a device
-extern SDL_JoystickID SDL_AddJoystickDevice(SDL_JoystickDevice *device, bool synchronous);
+extern SDL_JoystickID SDL_AddJoystickDevice(SDL_JoystickDevice *device);
// Remove a device published with SDL_AddJoystickDevice()
extern void SDL_RemoveJoystickDevice(SDL_JoystickID instance_id);
diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h
index 5d1aebc56706b..bbe3be3fe2539 100644
--- a/src/joystick/SDL_sysjoystick.h
+++ b/src/joystick/SDL_sysjoystick.h
@@ -84,11 +84,6 @@ typedef struct SDL_JoystickDevice
} SDL_JoystickDevice;
-typedef struct SDL_JoystickDeviceList
-{
- SDL_JoystickDevice *devices;
-} SDL_JoystickDeviceList;
-
typedef struct SDL_JoystickAxisInfo
{
Sint16 initial_value; // Initial axis state
diff --git a/src/joystick/dos/SDL_sysjoystick.c b/src/joystick/dos/SDL_sysjoystick.c
index 709fa3cd76045..16ffe87c7278c 100644
--- a/src/joystick/dos/SDL_sysjoystick.c
+++ b/src/joystick/dos/SDL_sysjoystick.c
@@ -155,7 +155,7 @@ static bool DOS_JoystickInit(void)
0 /* driver_data */
);
- SDL_AddJoystickDevice(device, false);
+ SDL_AddJoystickDevice(device);
return true;
}
diff --git a/src/joystick/haiku/SDL_haikujoystick.cc b/src/joystick/haiku/SDL_haikujoystick.cc
index 40c8bab1c5be7..c8247f35efa01 100644
--- a/src/joystick/haiku/SDL_haikujoystick.cc
+++ b/src/joystick/haiku/SDL_haikujoystick.cc
@@ -57,7 +57,7 @@ extern "C"
// the GUID is just the name for now
device->guid = SDL_CreateJoystickGUIDForName(device->name);
- SDL_AddJoystickDevice(device, false);
+ SDL_AddJoystickDevice(device);
return true;
}
diff --git a/src/joystick/n3ds/SDL_sysjoystick.c b/src/joystick/n3ds/SDL_sysjoystick.c
index 1e2e20e80531f..04b95e1a56014 100644
--- a/src/joystick/n3ds/SDL_sysjoystick.c
+++ b/src/joystick/n3ds/SDL_sysjoystick.c
@@ -82,7 +82,7 @@ static bool N3DS_JoystickInit(void)
device->name = SDL_strdup(name);
device->guid = SDL_CreateJoystickGUIDForName(name);
- SDL_AddJoystickDevice(device, false);
+ SDL_AddJoystickDevice(device);
return true;
}
diff --git a/src/joystick/ps2/SDL_sysjoystick.c b/src/joystick/ps2/SDL_sysjoystick.c
index 12982168d8819..412d0f6153cdb 100644
--- a/src/joystick/ps2/SDL_sysjoystick.c
+++ b/src/joystick/ps2/SDL_sysjoystick.c
@@ -120,7 +120,7 @@ static void PS2_AddJoystick(struct JoyInfo *info)
device->name = SDL_strdup(name);
device->guid = SDL_CreateJoystickGUIDForName(name);
- info->instance_id = SDL_AddJoystickDevice(device, false);
+ info->instance_id = SDL_AddJoystickDevice(device);
}
static void PS2_RemoveJoystick(struct JoyInfo *info)
diff --git a/src/joystick/psp/SDL_sysjoystick.c b/src/joystick/psp/SDL_sysjoystick.c
index 4d7423fe3ace5..2c0c775c7e693 100644
--- a/src/joystick/psp/SDL_sysjoystick.c
+++ b/src/joystick/psp/SDL_sysjoystick.c
@@ -100,7 +100,7 @@ static bool PSP_JoystickInit(void)
device->name = SDL_strdup(name);
device->guid = SDL_CreateJoystickGUIDForName(name);
- SDL_AddJoystickDevice(device, false);
+ SDL_AddJoystickDevice(device);
return true;
}
diff --git a/src/joystick/virtual/SDL_virtualjoystick.c b/src/joystick/virtual/SDL_virtualjoystick.c
index 2f4447ead22e9..7d50ca1e37b0c 100644
--- a/src/joystick/virtual/SDL_virtualjoystick.c
+++ b/src/joystick/virtual/SDL_virtualjoystick.c
@@ -313,7 +313,7 @@ SDL_JoystickID SDL_JoystickAttachVirtualInner(const SDL_VirtualJoystickDesc *des
device->name = SDL_strdup(name);
device->guid = SDL_CreateJoystickGUID(SDL_HARDWARE_BUS_VIRTUAL, hwdata->desc.vendor_id, hwdata->desc.product_id, 0, NULL, name, 'v', (Uint8)hwdata->desc.type);
- return SDL_AddJoystickDevice(device, true);
+ return SDL_AddJoystickDevice(device);
}
bool SDL_JoystickDetachVirtualInner(SDL_JoystickID instance_id)
diff --git a/src/joystick/vita/SDL_sysjoystick.c b/src/joystick/vita/SDL_sysjoystick.c
index bd82cb13b4037..4797828dd9953 100644
--- a/src/joystick/vita/SDL_sysjoystick.c
+++ b/src/joystick/vita/SDL_sysjoystick.c
@@ -117,7 +117,7 @@ static bool VITA_AddJoystick(int index)
device->name = SDL_strdup(name);
device->guid = SDL_CreateJoystickGUIDForName(name);
- SDL_AddJoystickDevice(device, false);
+ SDL_AddJoystickDevice(device);
return true;
}