Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 31 Aug 2014 11:21:10 -0400
changeset 9120 b186c0df3c18
parent 9118 8eeb0d759bc3
child 9121 00d99eff96a6
Mac: Don't add the same joystick twice if IOKit reports a duplicate device. Fixes Bugzilla #2704.
src/joystick/darwin/SDL_sysjoystick.c
--- a/src/joystick/darwin/SDL_sysjoystick.c	Wed Aug 27 23:27:42 2014 +0200
+++ b/src/joystick/darwin/SDL_sysjoystick.c	Sun Aug 31 11:21:10 2014 -0400
@@ -364,15 +364,33 @@
     return SDL_TRUE;
 }
 
+static SDL_bool
+JoystickAlreadyKnown(IOHIDDeviceRef ioHIDDeviceObject)
+{
+    recDevice *i;
+    for (i = gpDeviceList; i != NULL; i = i->pNext) {
+        if (i->deviceRef == ioHIDDeviceObject) {
+            return SDL_TRUE;
+        }
+    }
+    return SDL_FALSE;
+}
+
 
 static void
 JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject)
 {
+    recDevice *device;
+
     if (res != kIOReturnSuccess) {
         return;
     }
 
-    recDevice *device = (recDevice *) SDL_calloc(1, sizeof(recDevice));
+    if (JoystickAlreadyKnown(ioHIDDeviceObject)) {
+        return;  /* IOKit sent us a duplicate. */
+    }
+
+    device = (recDevice *) SDL_calloc(1, sizeof(recDevice));
 
     if (!device) {
         SDL_OutOfMemory();