From ddfe1bedd18d4f00affb590de2d47946158c6cb7 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 31 Oct 2026 13:55:18 -0700
Subject: [PATCH] Created the concept of a central joystick device
This allows us to hold common data between all drivers and handle driver overriding in a centralized way, guaranteeing that all drivers have updated device state before sending device change updates.
The dummy joystick driver goes to one line. :)
---
Xcode/SDL/SDL.xcodeproj/project.pbxproj | 4 -
src/haptic/windows/SDL_windowshaptic.c | 10 +-
src/hidapi/SDL_hidapi.c | 17 +
src/hidapi/SDL_hidapi_c.h | 7 +-
src/joystick/SDL_joystick.c | 809 +++++++++++++++----
src/joystick/SDL_joystick_c.h | 30 +-
src/joystick/SDL_sysjoystick.h | 126 +--
src/joystick/android/SDL_sysjoystick.c | 285 +++----
src/joystick/android/SDL_sysjoystick_c.h | 22 -
src/joystick/apple/SDL_mfijoystick.m | 707 ++++++++--------
src/joystick/apple/SDL_mfijoystick_c.h | 74 --
src/joystick/bsd/SDL_bsdjoystick.c | 121 ++-
src/joystick/darwin/SDL_iokitjoystick.c | 171 ++--
src/joystick/darwin/SDL_iokitjoystick_c.h | 3 +-
src/joystick/dos/SDL_sysjoystick.c | 85 +-
src/joystick/dummy/SDL_sysjoystick.c | 79 +-
src/joystick/emscripten/SDL_sysjoystick.c | 259 +++---
src/joystick/emscripten/SDL_sysjoystick_c.h | 62 --
src/joystick/gdk/SDL_gameinputjoystick.cpp | 536 ++++++------
src/joystick/haiku/SDL_haikujoystick.cc | 124 +--
src/joystick/hidapi/SDL_hidapi_flydigi.c | 18 +-
src/joystick/hidapi/SDL_hidapijoystick.c | 442 ++++------
src/joystick/hidapi/SDL_hidapijoystick_c.h | 17 +-
src/joystick/linux/SDL_sysjoystick.c | 219 +++--
src/joystick/linux/SDL_sysjoystick_c.h | 2 +
src/joystick/n3ds/SDL_sysjoystick.c | 160 ++--
src/joystick/ps2/SDL_sysjoystick.c | 200 ++---
src/joystick/psp/SDL_sysjoystick.c | 139 +---
src/joystick/virtual/SDL_virtualjoystick.c | 245 ++----
src/joystick/virtual/SDL_virtualjoystick_c.h | 42 -
src/joystick/vita/SDL_sysjoystick.c | 212 ++---
src/joystick/windows/SDL_dinputjoystick.c | 601 +++-----------
src/joystick/windows/SDL_dinputjoystick_c.h | 15 +-
src/joystick/windows/SDL_xinputjoystick.c | 328 +++-----
34 files changed, 2613 insertions(+), 3558 deletions(-)
delete mode 100644 src/joystick/apple/SDL_mfijoystick_c.h
delete mode 100644 src/joystick/emscripten/SDL_sysjoystick_c.h
diff --git a/Xcode/SDL/SDL.xcodeproj/project.pbxproj b/Xcode/SDL/SDL.xcodeproj/project.pbxproj
index 530c8f5d5cb00..6663fb0deb83a 100644
--- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj
+++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj
@@ -430,7 +430,6 @@
F395C1932569C68F00942BFF /* SDL_iokitjoystick_c.h in Headers */ = {isa = PBXBuildFile; fileRef = F395C1912569C68E00942BFF /* SDL_iokitjoystick_c.h */; };
F395C19C2569C68F00942BFF /* SDL_iokitjoystick.c in Sources */ = {isa = PBXBuildFile; fileRef = F395C1922569C68E00942BFF /* SDL_iokitjoystick.c */; };
F395C1B12569C6A000942BFF /* SDL_mfijoystick.m in Sources */ = {isa = PBXBuildFile; fileRef = F395C1AF2569C6A000942BFF /* SDL_mfijoystick.m */; };
- F395C1BA2569C6A000942BFF /* SDL_mfijoystick_c.h in Headers */ = {isa = PBXBuildFile; fileRef = F395C1B02569C6A000942BFF /* SDL_mfijoystick_c.h */; };
F3973FA228A59BDD00B84553 /* SDL_vacopy.h in Headers */ = {isa = PBXBuildFile; fileRef = F3973FA028A59BDD00B84553 /* SDL_vacopy.h */; };
F3973FAB28A59BDD00B84553 /* SDL_crc16.c in Sources */ = {isa = PBXBuildFile; fileRef = F3973FA128A59BDD00B84553 /* SDL_crc16.c */; };
F3984CD025BCC92900374F43 /* SDL_hidapi_stadia.c in Sources */ = {isa = PBXBuildFile; fileRef = F3984CCF25BCC92800374F43 /* SDL_hidapi_stadia.c */; };
@@ -1040,7 +1039,6 @@
F395C1912569C68E00942BFF /* SDL_iokitjoystick_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_iokitjoystick_c.h; sourceTree = "<group>"; };
F395C1922569C68E00942BFF /* SDL_iokitjoystick.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_iokitjoystick.c; sourceTree = "<group>"; };
F395C1AF2569C6A000942BFF /* SDL_mfijoystick.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_mfijoystick.m; sourceTree = "<group>"; };
- F395C1B02569C6A000942BFF /* SDL_mfijoystick_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_mfijoystick_c.h; sourceTree = "<group>"; };
F3973FA028A59BDD00B84553 /* SDL_vacopy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_vacopy.h; sourceTree = "<group>"; };
F3973FA128A59BDD00B84553 /* SDL_crc16.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_crc16.c; sourceTree = "<group>"; };
F3984CCF25BCC92800374F43 /* SDL_hidapi_stadia.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_stadia.c; sourceTree = "<group>"; };
@@ -2017,7 +2015,6 @@
A7D8A7AA23E2513E00DCD162 /* apple */ = {
isa = PBXGroup;
children = (
- F395C1B02569C6A000942BFF /* SDL_mfijoystick_c.h */,
F395C1AF2569C6A000942BFF /* SDL_mfijoystick.m */,
);
path = apple;
@@ -2758,7 +2755,6 @@
A7D8BB8723E2514500DCD162 /* SDL_keyboard_c.h in Headers */,
A1BB8B6C27F6CF330057CFA8 /* SDL_list.h in Headers */,
F386F6E72884663E001840AA /* SDL_log_c.h in Headers */,
- F395C1BA2569C6A000942BFF /* SDL_mfijoystick_c.h in Headers */,
A7D8BB1B23E2514500DCD162 /* SDL_mouse_c.h in Headers */,
A7D8ABFD23E2514100DCD162 /* SDL_nullevents_c.h in Headers */,
A7D8ABE523E2514100DCD162 /* SDL_nullframebuffer_c.h in Headers */,
diff --git a/src/haptic/windows/SDL_windowshaptic.c b/src/haptic/windows/SDL_windowshaptic.c
index 15689b9657bc9..833165070773c 100644
--- a/src/haptic/windows/SDL_windowshaptic.c
+++ b/src/haptic/windows/SDL_windowshaptic.c
@@ -47,7 +47,7 @@ static int numhaptics = 0;
*/
bool SDL_SYS_HapticInit(void)
{
- JoyStick_DeviceData *device;
+ SDL_JoystickDevice *device;
if (!SDL_DINPUT_HapticInit()) {
return false;
@@ -58,9 +58,13 @@ bool SDL_SYS_HapticInit(void)
* subsystem will arrive too early to create haptic devices. We will
* invoke those callbacks again here to pick up any joysticks that
* were added prior to haptics initialization. */
- for (device = SYS_Joystick; device; device = device->pNext) {
- SDL_DINPUT_HapticMaybeAddDevice(&device->dxdevice);
+ SDL_LockJoysticks();
+ for (device = SDL_GetJoystickDevices(); device; device = device->next) {
+ if (device->driver == &SDL_DINPUT_JoystickDriver && device->published) {
+ SDL_DINPUT_HapticMaybeAddDevice(&device->internal->dxdevice);
+ }
}
+ SDL_UnlockJoysticks();
return true;
}
diff --git a/src/hidapi/SDL_hidapi.c b/src/hidapi/SDL_hidapi.c
index a379a9022f4d1..5d4291f901e62 100644
--- a/src/hidapi/SDL_hidapi.c
+++ b/src/hidapi/SDL_hidapi.c
@@ -1241,6 +1241,23 @@ int SDL_hid_exit(void)
return result;
}
+bool SDL_HIDAPI_HasDeviceNotifications(void)
+{
+#ifndef SDL_HIDAPI_DISABLED
+ if (SDL_hidapi_refcount == 0) {
+ return false;
+ }
+
+ if (!SDL_HIDAPI_discovery.m_bInitialized) {
+ HIDAPI_InitializeDiscovery();
+ }
+
+ return SDL_HIDAPI_discovery.m_bCanGetNotifications;
+#else
+ return false;
+#endif
+}
+
Uint32 SDL_hid_device_change_count(void)
{
Uint32 counter = 0;
diff --git a/src/hidapi/SDL_hidapi_c.h b/src/hidapi/SDL_hidapi_c.h
index 2ad9e72b7207e..1422956dc3d5e 100644
--- a/src/hidapi/SDL_hidapi_c.h
+++ b/src/hidapi/SDL_hidapi_c.h
@@ -21,9 +21,12 @@
#include "SDL_internal.h"
-/* Return true if the HIDAPI should ignore a device during enumeration */
+// Return true if this platform has device notifications available
+extern bool SDL_HIDAPI_HasDeviceNotifications(void);
+
+// Return true if the HIDAPI should ignore a device during enumeration
extern bool SDL_HIDAPI_ShouldIgnoreDevice(int bus_type, Uint16 vendor_id, Uint16 product_id, Uint16 usage_page, Uint16 usage, bool libusb, bool is_xbox);
-/* Called by platform backends when devices may have changed, this is safe to call from any thread */
+// Called by platform backends when devices may have changed, this is safe to call from any thread
extern void SDL_HIDAPI_DeviceChanged(void);
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index ba1073ae5086e..f498443763724 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -32,6 +32,7 @@
#include "../video/SDL_sysvideo.h"
#include "../sensor/SDL_sensor_c.h"
#include "hidapi/SDL_hidapijoystick_c.h"
+#include "../hidapi/SDL_hidapi_c.h"
// This is included in only one place because it has a large static list of controllers
#include "controller_type.h"
@@ -107,16 +108,27 @@ static SDL_JoystickDriver *SDL_joystick_drivers[] = {
&SDL_DUMMY_JoystickDriver
#endif
};
+static bool SDL_joystick_drivers_initialized[SDL_arraysize(SDL_joystick_drivers)];
static int SDL_joysticks_locked;
static bool SDL_joysticks_initialized;
static bool SDL_joysticks_quitting;
static bool SDL_joystick_being_added;
-static SDL_Joystick *SDL_joysticks SDL_GUARDED_BY(SDL_event_lock) = NULL;
-static int SDL_joystick_player_count SDL_GUARDED_BY(SDL_event_lock) = 0;
-static SDL_JoystickID *SDL_joystick_players SDL_GUARDED_BY(SDL_event_lock) = NULL;
-static SDL_HashTable *SDL_joystick_names SDL_GUARDED_BY(SDL_event_lock) = NULL;
-static bool SDL_joystick_allows_background_events = false;
+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);
+static SDL_JoystickID *SDL_joystick_players SDL_GUARDED_BY(SDL_event_lock);
+static SDL_HashTable *SDL_joystick_names SDL_GUARDED_BY(SDL_event_lock);
+static Uint64 SDL_joystick_device_change_time SDL_GUARDED_BY(SDL_event_lock);
+static int SDL_joystick_device_update_step SDL_GUARDED_BY(SDL_event_lock);
+static SDL_AtomicInt SDL_joystick_devices_changed;
+static SDL_AtomicInt SDL_joystick_devices_update;
+static bool SDL_joystick_allows_background_events;
static Uint32 initial_old_xboxone_controllers[] = {
MAKE_VIDPID(0x0000, 0x6686),
@@ -729,34 +741,511 @@ void SDL_AssertJoysticksLocked(void)
SDL_assert(SDL_JoysticksLocked());
}
-/*
- * Get the driver and device index for a joystick instance ID
- * This should be called while the joystick lock is held, to prevent another thread from updating the list
- */
-static bool SDL_GetDriverAndJoystickIndex(SDL_JoystickID instance_id, SDL_JoystickDriver **driver, int *driver_index)
+static SDL_JoystickDriver *SDL_GetJoystickDriver(int index)
+{
+ if (SDL_joystick_drivers_initialized[index]) {
+ return SDL_joystick_drivers[index];
+ }
+ return NULL;
+}
+
+SDL_JoystickDevice *SDL_CreateJoystickDevice(SDL_JoystickDriver *driver, size_t extra_length)
+{
+ SDL_JoystickDevice *device = (SDL_JoystickDevice *)SDL_calloc(1, sizeof(*device) + extra_length);
+ if (!device) {
+ return NULL;
+ }
+
+ device->driver = driver;
+ device->player_index = -1;
+ device->steam_virtual_gamepad_slot = -1;
+ return device;
+}
+
+void SDL_DestroyJoystickDevice(SDL_JoystickDevice *device)
+{
+ if (device->free) {
+ device->free(device);
+ }
+
+ SDL_free(device->matches);
+ SDL_free(device->name);
+ SDL_free(device->path);
+ SDL_free(device);
+}
+
+static void SDL_DestroyJoystickDevices(SDL_JoystickDevice *device)
+{
+ while (device) {
+ SDL_JoystickDevice *next = device->next;
+ SDL_DestroyJoystickDevice(device);
+ device = next;
+ }
+}
+
+SDL_JoystickDevice *SDL_GetJoystickDevices(void)
{
- int i, num_joysticks, device_index;
+ SDL_AssertJoysticksLocked();
+ return SDL_joystick_devices;
+}
+
+static SDL_JoystickDevice *SDL_GetJoystickDevice(SDL_JoystickID instance_id)
+{
SDL_AssertJoysticksLocked();
- if (instance_id > 0) {
- for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
- num_joysticks = SDL_joystick_drivers[i]->GetCount();
- for (device_index = 0; device_index < num_joysticks; ++device_index) {
- SDL_JoystickID joystick_id = SDL_joystick_drivers[i]->GetDeviceInstanceID(device_index);
- if (joystick_id == instance_id) {
- *driver = SDL_joystick_drivers[i];
- *driver_index = device_index;
- return true;
- }
+ if (instance_id) {
+ for (SDL_JoystickDevice *device = SDL_joystick_devices; device; device = device->next) {
+ if (device->published && instance_id == device->instance_id) {
+ return device;
}
}
}
+ return NULL;
+}
+
+static SDL_JoystickDeviceMatch *SDL_AddJoystickDeviceMatch(SDL_JoystickDevice *device)
+{
+ SDL_JoystickDeviceMatch *matches = (SDL_JoystickDeviceMatch *)SDL_realloc(device->matches, (device->num_matches + 1) * sizeof(*matches));
+ if (!matches) {
+ return NULL;
+ }
+
+ ++device->num_matches;
+ device->matches = matches;
+
+ return &matches[device->num_matches - 1];
+}
+
+void SDL_AddJoystickDeviceMatchType(SDL_JoystickDevice *device, SDL_GamepadType type)
+{
+ SDL_JoystickDeviceMatch *match = SDL_AddJoystickDeviceMatch(device);
+ if (!match) {
+ return;
+ }
+
+ match->type = JOYSTICK_DEVICE_MATCH_TYPE;
+ match->data.type = type;
+}
- SDL_SetError("Joystick %" SDL_PRIu32 " not found", instance_id);
+void SDL_AddJoystickDeviceMatchVIDPID(SDL_JoystickDevice *device, Uint16 vendor_id, Uint16 product_id)
+{
+ SDL_JoystickDeviceMatch *match = SDL_AddJoystickDeviceMatch(device);
+ if (!match) {
+ return;
+ }
+
+ match->type = JOYSTICK_DEVICE_MATCH_VIDPID;
+ match->data.vidpid = MAKE_VIDPID(vendor_id, product_id);
+}
+
+void SDL_AddJoystickDeviceMatchXInput(SDL_JoystickDevice *device, int xinput_index)
+{
+ SDL_JoystickDeviceMatch *match = SDL_AddJoystickDeviceMatch(device);
+ if (!match) {
+ return;
+ }
+
+ match->type = JOYSTICK_DEVICE_MATCH_XINPUT;
+ match->data.xinput_index = xinput_index;
+}
+
+static bool SDL_HasJoystickDeviceMatch(SDL_JoystickDevice *device, const SDL_JoystickDeviceMatch *match)
+{
+ for (int i = 0; i < device->num_matches; ++i) {
+ const SDL_JoystickDeviceMatch *current = &device->matches[i];
+ if (current->type != match->type) {
+ continue;
+ }
+
+ switch (match->type) {
+ case JOYSTICK_DEVICE_MATCH_TYPE:
+ if (current->data.type == match->data.type) {
+ return true;
+ }
+ break;
+ case JOYSTICK_DEVICE_MATCH_VIDPID:
+ if (current->data.vidpid == match->data.vidpid) {
+ return true;
+ }
+ break;
+ case JOYSTICK_DEVICE_MATCH_XINPUT:
+ if (current->data.xinput_index < 0 ||
+ match->data.xinput_index < 0 ||
+ current->data.xinput_index == match->data.xinput_index) {
+ return true;
+ }
+ break;
+ default:
+ break;
+ }
+ }
return false;
}
+static bool SDL_DevicesMatch(SDL_JoystickDevice *a, SDL_JoystickDevice *b)
+{
+ for (int i = 0; i < a->num_matches; ++i) {
+ if (SDL_HasJoystickDeviceMatch(b, &a->matches[i])) {
+ return true;
+ }
+ }
+ return false;
+}
+
+static bool SDL_DeviceHidden(SDL_JoystickDevice *device, SDL_JoystickDevice *devices)
+{
+ // Check to see whether this device is hidden by a higher priority driver
+ while (devices->driver != device->driver) {
+ if (SDL_DevicesMatch(device, devices)) {
+ return true;
+ }
+ devices = devices->next;
+ }
+ return false;
+}
+
+static SDL_JoystickDevice *SDL_RemoveHiddenDevices(SDL_JoystickDevice *devices)
+{
+ SDL_JoystickDevice *device = devices;
+ SDL_JoystickDevice *prev = NULL;
+ while (device) {
+ SDL_JoystickDevice *next = device->next;
+
+ if (SDL_DeviceHidden(device, devices)) {
+ SDL_DestroyJoystickDevice(device);
+ if (prev) {
+ prev->next = next;
+ } else {
+ devices = next;
+ }
+ } else {
+ prev = device;
+ }
+ device = next;
+ }
+ return devices;
+}
+
+void SDL_RequestJoystickDeviceUpdate(bool immediate)
+{
+ if (immediate) {
+ SDL_SetAtomicInt(&SDL_joystick_devices_update, 1);
+ } else {
+ SDL_SetAtomicInt(&SDL_joystick_devices_changed, 1);
+ }
+}
+
+static void SDL_UpdatePendingJoystickDevices(void)
+{
+ SDL_JoystickDeviceList *pending = (SDL_JoystickDeviceList *)SDL_calloc(1, sizeof(*pending));
+ if (!pending) {
+ return;
+ }
+
+ SDL_JoystickDevice *tail = NULL;
+ for (int i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
+ SDL_JoystickDriver *driver = SDL_GetJoystickDriver(i);
+ if (!driver) {
+ continue;
+ }
+
+ if (!driver->QueryDevices) {
+ continue;
+ }
+
+ SDL_JoystickDevice *devices = driver->QueryDevices();
+ if (devices) {
+ if (pending->devices) {
+ tail->next = devices;
+ } else {
+ pending->devices = devices;
+ tail = devices;
+ }
+ while (tail->next) {
+ tail = tail->next;
+ }
+ }
+ }
+
+ 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);
+ }
+}
+
+// Remove and return the device in the list with the same driver and unique ID, if any
+static SDL_JoystickDevice *SDL_FindAndRemoveJoystickDevice(SDL_JoystickDevice *device, SDL_JoystickDevice **devices)
+{
+ SDL_JoystickDevice *prev = NULL;
+ for (SDL_JoystickDevice *candidate = *devices; candidate; candidate = candidate->next) {
+ if (candidate->driver == device->driver &&
+ candidate->unique_id_len > 0 && candidate->unique_id_len == device->unique_id_len &&
+ SDL_memcmp(candidate->unique_id, device->unique_id, device->unique_id_len) == 0) {
+ if (prev) {
+ prev->next = candidate->next;
+ } else {
+ *devices = candidate->next;
+ }
+ candidate->next = NULL;
+ return candidate;
+ }
+ prev = candidate;
+ }
+ return NULL;
+}
+
+static void SDL_ProcessPendingJoystickDevices(void)
+{
+ 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;
+ SDL_JoystickDevice *old = SDL_joystick_devices;
+ while (old) {
+ SDL_JoystickDevice *next = old->next;
+ bool keep = false;
+ if (old->manually_added || !pending) {
+ keep = true;
+ } else {
+ // Pull the new device matching the old one out of the list
+ SDL_JoystickDevice *device = SDL_FindAndRemoveJoystickDevice(old, &pending_devices);
+ if (device) {
+ // Copy any updated state from the new device
+ old->initializing = device->initializing;
+ SDL_DestroyJoystickDevice(device);
+ keep = true;
+ }
+ }
+
+ if (keep) {
+ old->next = NULL;
+ if (tail) {
+ tail->next = old;
+ } else {
+ devices = old;
+ }
+ tail = old;
+ } else {
+ old->next = removed;
+ removed = old;
+ }
+ old = next;
+ }
+
+ // Anything left in the pending list is a new device
+ if (tail) {
+ tail->next = pending_devices;
+ } else {
+ devices = pending_devices;
+ }
+ SDL_joystick_devices = devices;
+
+ // Send events for removed devices
+ while (removed) {
+ SDL_JoystickDevice *next = removed->next;
+ if (removed->published) {
+ SDL_PrivateJoystickRemoved(removed->instance_id);
+ }
+ SDL_DestroyJoystickDevice(removed);
+ removed = next;
+ }
+
+ // 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;
+ if (!device->published && !device->initializing) {
+ bool prepared = true;
+ if (device->driver->PrepareDevice) {
+ prepared = device->driver->PrepareDevice(device);
+ }
+ if (prepared) {
+ if (!device->instance_id) {
+ device->instance_id = SDL_GetNextObjectID();
+ }
+ 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_AssertJoysticksLocked();
+
+ SDL_JoystickID instance_id = device->instance_id;
+ if (!instance_id) {
+ instance_id = device->instance_id = SDL_GetNextObjectID();
+ }
+ device->manually_added = true;
+
+ // Add this to the end of the pending device list
+ SDL_JoystickDevice *tail = SDL_pending_manual_devices;
+ if (tail) {
+ while (tail->next) {
+ tail = tail->next;
+ }
+ tail->next = device;
+ } else {
+ SDL_pending_manual_devices = device;
+ }
+
+ if (synchronous) {
+ SDL_ProcessPendingJoystickDevices();
+
+ // Make sure the device was published
+ if (!SDL_GetJoystickDevice(instance_id)) {
+ instance_id = 0;
+ }
+ }
+
+ return instance_id;
+}
+
+void SDL_RemoveJoystickDevice(SDL_JoystickID instance_id)
+{
+ SDL_AssertJoysticksLocked();
+
+ if (!instance_id) {
+ return;
+ }
+
+ 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);
+ if (prev) {
+ prev->next = device->next;
+ } else {
+ SDL_joystick_devices = device->next;
+ }
+ if (device->published) {
+ SDL_PrivateJoystickRemoved(instance_id);
+ }
+ SDL_DestroyJoystickDevice(device);
+ return;
+ }
+ prev = device;
+ }
+}
+
+static void SDL_UpdateJoystickDevices(bool update)
+{
+ SDL_AssertJoysticksLocked();
+
+ for (int i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
+ SDL_JoystickDriver *driver = SDL_GetJoystickDriver(i);
+ if (driver && driver->DetectDevices) {
+ driver->DetectDevices();
+ }
+ }
+
+ if (SDL_joystick_hid_notifications) {
+ Uint32 change_count = SDL_hid_device_change_count();
+ if (change_count != SDL_joystick_hid_change_count) {
+ SDL_RequestJoystickDeviceUpdate(false);
+ SDL_joystick_hid_change_count = change_count;
+ }
+ } else {
+ // Poll for updates every 3 seconds
+ const int DEVICE_UPDATE_INTERVAL_MS = 3000;
+ if (SDL_GetTicks() >= (SDL_last_device_update + DEVICE_UPDATE_INTERVAL_MS)) {
+ update = true;
+ }
+ }
+
+ if (SDL_CompareAndSwapAtomicInt(&SDL_joystick_devices_changed, 1, 0)) {
+ SDL_joystick_device_change_time = SDL_GetTicks();
+ SDL_joystick_device_update_step = 0;
+ }
+ if (SDL_CompareAndSwapAtomicInt(&SDL_joystick_devices_update, 1, 0)) {
+ update = true;
+ }
+
+ if (SDL_joystick_device_change_time) {
+ static Uint32 update_step_times[] = {
+ 50,
+ 300,
+ 2000
+ };
+
+ Uint64 update_time = SDL_joystick_device_change_time +
+ update_step_times[SDL_joystick_device_update_step];
+ if (SDL_GetTicks() >= update_time) {
+ update = true;
+ ++SDL_joystick_device_update_step;
+ if (SDL_joystick_device_update_step == SDL_arraysize(update_step_times)) {
+ SDL_joystick_device_update_step = 0;
+ SDL_joystick_device_change_time = 0;
+ }
+ }
+ }
+
+ if (update) {
+ SDL_last_device_update = SDL_GetTicks();
+
+ SDL_UpdatePendingJoystickDevices();
+ }
+
+ SDL_ProcessPendingJoystickDevices();
+}
+
static int SDL_FindFreePlayerIndex(void)
{
int player_index;
@@ -801,8 +1290,6 @@ static SDL_JoystickID SDL_GetJoystickIDForPlayerIndex(int player_index)
static bool SDL_SetJoystickIDForPlayerIndex(int player_index, SDL_JoystickID instance_id)
{
SDL_JoystickID existing_instance = SDL_GetJoystickIDForPlayerIndex(player_index);
- SDL_JoystickDriver *driver;
- int device_index;
int existing_player_index;
SDL_AssertJoysticksLocked();
@@ -832,10 +1319,9 @@ static bool SDL_SetJoystickIDForPlayerIndex(int player_index, SDL_JoystickID ins
}
// Update the driver with the new index
- if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
- if (driver->SetDevicePlayerIndex) {
- driver->SetDevicePlayerIndex(device_index, player_index);
- }
+ SDL_Joystick *joystick = SDL_GetJoystickFromID(instance_id);
+ if (joystick && joystick->driver->SetPlayerIndex) {
+ joystick->driver->SetPlayerIndex(joystick, player_index);
}
// Move any existing joystick to another slot
@@ -890,10 +1376,28 @@ bool SDL_InitJoysticks(void)
SDL_InitSteamVirtualGamepadInfo();
for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
- if (SDL_joystick_drivers[i]->Init()) {
+ SDL_JoystickDriver *driver = SDL_joystick_drivers[i];
+ bool initialized = true;
+ if (driver->Init) {
+ initialized = driver->Init();
+ }
+ if (initialized) {
result = true;
}
+ SDL_joystick_drivers_initialized[i] = initialized;
+ }
+
+ if (SDL_hid_init() == 0) {
+ if (SDL_HIDAPI_HasDeviceNotifications()) {
+ SDL_joystick_hid_notifications = true;
+ SDL_joystick_hid_change_count = SDL_hid_device_change_count();
+ } else {
+ SDL_hid_exit();
+ }
}
+
+ SDL_UpdateJoystickDevices(true);
+
SDL_UnlockJoysticks();
if (!result) {
@@ -920,22 +1424,15 @@ bool SDL_JoysticksOpened(void)
return opened;
}
-bool SDL_JoystickHandledByAnotherDriver(struct SDL_JoystickDriver *driver, Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
+bool SDL_HasJoystick(void)
{
- int i;
bool result = false;
SDL_LockJoysticks();
- {
- for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
- if (driver == SDL_joystick_drivers[i]) {
- // Higher priority drivers do not have this device
- break;
- }
- if (SDL_joystick_drivers[i]->IsDevicePresent(vendor_id, product_id, version, name)) {
- result = true;
- break;
- }
+ for (SDL_JoystickDevice *device = SDL_joystick_devices; device; device = device->next) {
+ if (device->published) {
+ result = true;
+ break;
}
}
SDL_UnlockJoysticks();
@@ -943,60 +1440,37 @@ bool SDL_JoystickHandledByAnotherDriver(struct SDL_JoystickDriver *driver, Uint1
return result;
}
-bool SDL_HasJoystick(void)
-{
- int i;
- int total_joysticks = 0;
-
- SDL_LockJoysticks();
- {
- for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
- total_joysticks += SDL_joystick_drivers[i]->GetCount();
- }
- }
- SDL_UnlockJoysticks();
-
- if (total_joysticks > 0) {
- return true;
- }
- return false;
-}
-
SDL_JoystickID *SDL_GetJoysticks(int *count)
{
- int i, num_joysticks, device_index;
int joystick_index = 0, total_joysticks = 0;
SDL_JoystickID *joysticks;
SDL_LockJoysticks();
- {
- for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
- total_joysticks += SDL_joystick_drivers[i]->GetCount();
+
+ for (SDL_JoystickDevice *device = SDL_joystick_devices; device; device = device->next) {
+ if (device->published) {
+ ++total_joysticks;
}
+ }
- joysticks = (SDL_JoystickID *)SDL_malloc((total_joysticks + 1) * sizeof(*joysticks));
- if (joysticks) {
- if (count) {
- *count = total_joysticks;
+ joysticks = (SDL_JoystickID *)SDL_malloc((total_joysticks + 1) * sizeof(*joysticks));
+ if (joysticks) {
+ for (SDL_JoystickDevice *device = SDL_joystick_devices; device; device = device->next) {
+ if (device->published) {
+ joysticks[joystick_index++] = device->instance_id;
}
+ }
+ joysticks[joystick_index] = 0;
- for (i = 0; i < SDL_arr
(Patch may be truncated, please check the link at the top of this post.)