From 724d24d990868e617ea897352064909cc26e1965 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sun, 31 Aug 2014 11:21:10 -0400 Subject: [PATCH] Mac: Don't add the same joystick twice if IOKit reports a duplicate device. Fixes Bugzilla #2704. --- src/joystick/darwin/SDL_sysjoystick.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/joystick/darwin/SDL_sysjoystick.c b/src/joystick/darwin/SDL_sysjoystick.c index f0bd5b8d1..0518cc75b 100644 --- a/src/joystick/darwin/SDL_sysjoystick.c +++ b/src/joystick/darwin/SDL_sysjoystick.c @@ -364,15 +364,33 @@ GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice) 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();