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 9120b186c0df3c18
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
     1.1 --- a/src/joystick/darwin/SDL_sysjoystick.c	Wed Aug 27 23:27:42 2014 +0200
     1.2 +++ b/src/joystick/darwin/SDL_sysjoystick.c	Sun Aug 31 11:21:10 2014 -0400
     1.3 @@ -364,15 +364,33 @@
     1.4      return SDL_TRUE;
     1.5  }
     1.6  
     1.7 +static SDL_bool
     1.8 +JoystickAlreadyKnown(IOHIDDeviceRef ioHIDDeviceObject)
     1.9 +{
    1.10 +    recDevice *i;
    1.11 +    for (i = gpDeviceList; i != NULL; i = i->pNext) {
    1.12 +        if (i->deviceRef == ioHIDDeviceObject) {
    1.13 +            return SDL_TRUE;
    1.14 +        }
    1.15 +    }
    1.16 +    return SDL_FALSE;
    1.17 +}
    1.18 +
    1.19  
    1.20  static void
    1.21  JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject)
    1.22  {
    1.23 +    recDevice *device;
    1.24 +
    1.25      if (res != kIOReturnSuccess) {
    1.26          return;
    1.27      }
    1.28  
    1.29 -    recDevice *device = (recDevice *) SDL_calloc(1, sizeof(recDevice));
    1.30 +    if (JoystickAlreadyKnown(ioHIDDeviceObject)) {
    1.31 +        return;  /* IOKit sent us a duplicate. */
    1.32 +    }
    1.33 +
    1.34 +    device = (recDevice *) SDL_calloc(1, sizeof(recDevice));
    1.35  
    1.36      if (!device) {
    1.37          SDL_OutOfMemory();