Commit 0672dc8d8f6e22789351136a8addc730080797f4

Sam Lantinga 2022-10-02T08:48:38

Fixed combined Joy-Cons after https://github.com/libsdl-org/SDL/commit/aa2e2f4843567cfe4e6f2f4ffa3f0b74dd98f1ba (thanks @happyharryh!)

diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 3fe935e..25db216 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -358,14 +358,16 @@ HIDAPI_SetupDeviceDriver(SDL_HIDAPI_Device *device)
     }
 
     /* Make sure we can open the device and leave it open for the driver */
-    device->dev = SDL_hid_open_path(device->path, 0);
-    if (!device->dev) {
-        SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
-                     "HIDAPI_SetupDeviceDriver() couldn't open %s: %s\n",
-                     device->path, SDL_GetError());
-        return;
+    if (device->num_children == 0) {
+        device->dev = SDL_hid_open_path(device->path, 0);
+        if (!device->dev) {
+            SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
+                         "HIDAPI_SetupDeviceDriver() couldn't open %s: %s\n",
+                         device->path, SDL_GetError());
+            return;
+        }
+        SDL_hid_set_nonblocking(device->dev, 1);
     }
-    SDL_hid_set_nonblocking(device->dev, 1);
 
     device->driver = HIDAPI_GetDeviceDriver(device);
 
@@ -774,6 +776,7 @@ static void
 HIDAPI_DelDevice(SDL_HIDAPI_Device *device)
 {
     SDL_HIDAPI_Device *curr, *last;
+    int i;
 
 #ifdef DEBUG_HIDAPI
     SDL_Log("Removing HIDAPI device '%s' VID 0x%.4x, PID 0x%.4x, version %d, serial %s, interface %d, interface_class %d, interface_subclass %d, interface_protocol %d, usage page 0x%.4x, usage 0x%.4x, path = %s, driver = %s (%s)\n", device->name, device->vendor_id, device->product_id, device->version, device->serial ? device->serial : "NONE", device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol, device->usage_page, device->usage, device->path, device->driver ? device->driver->name : "NONE", device->driver && device->driver->enabled ? "ENABLED" : "DISABLED");
@@ -794,6 +797,10 @@ HIDAPI_DelDevice(SDL_HIDAPI_Device *device)
                 SDL_Delay(10);
             }
 
+            for (i = 0; i < device->num_children; ++i) {
+                device->children[i]->parent = NULL;
+            }
+
             SDL_DestroyMutex(device->dev_lock);
             SDL_free(device->serial);
             SDL_free(device->name);
@@ -864,7 +871,9 @@ HIDAPI_CreateCombinedJoyCons()
             if (combined && combined->driver) {
                 return SDL_TRUE;
             } else {
-                if (!combined) {
+                if (combined) {
+                    HIDAPI_DelDevice(combined);
+                } else {
                     SDL_free(children);
                 }
                 return SDL_FALSE;