Mac joystick: ignore duplicate HID elements.

The DualShock 4 has all elements listed twice: once in the top-level list of
 elements, and once in an "Application Collection" element at the top-level.

Each element has a proper cookie with a unique value, so now we descend into
 each element collections, but before we add an element to the device's list,
 we make sure we don't already have one with that cookie, probably from
 another collection or a buggy device.
This commit is contained in:
Ryan C. Gordon 2014-02-22 21:15:34 -05:00
parent 9cd5f5ceca
commit b67b970db1
2 changed files with 38 additions and 18 deletions

View File

@ -149,6 +149,17 @@ AddHIDElements(CFArrayRef array, recDevice *pDevice)
CFArrayApplyFunction(array, range, AddHIDElement, pDevice); CFArrayApplyFunction(array, range, AddHIDElement, pDevice);
} }
static SDL_bool
ElementAlreadyAdded(const IOHIDElementCookie cookie, const recElement *listitem) {
while (listitem) {
if (listitem->cookie == cookie) {
return SDL_TRUE;
}
listitem = listitem->pNext;
}
return SDL_FALSE;
}
/* See if we care about this HID element, and if so, note it in our recDevice. */ /* See if we care about this HID element, and if so, note it in our recDevice. */
static void static void
AddHIDElement(const void *value, void *parameter) AddHIDElement(const void *value, void *parameter)
@ -158,6 +169,7 @@ AddHIDElement(const void *value, void *parameter)
const CFTypeID elementTypeID = refElement ? CFGetTypeID(refElement) : 0; const CFTypeID elementTypeID = refElement ? CFGetTypeID(refElement) : 0;
if (refElement && (elementTypeID == IOHIDElementGetTypeID())) { if (refElement && (elementTypeID == IOHIDElementGetTypeID())) {
const IOHIDElementCookie cookie = IOHIDElementGetCookie(refElement);
const uint32_t usagePage = IOHIDElementGetUsagePage(refElement); const uint32_t usagePage = IOHIDElementGetUsagePage(refElement);
const uint32_t usage = IOHIDElementGetUsage(refElement); const uint32_t usage = IOHIDElementGetUsage(refElement);
recElement *element = NULL; recElement *element = NULL;
@ -180,19 +192,23 @@ AddHIDElement(const void *value, void *parameter)
case kHIDUsage_GD_Slider: case kHIDUsage_GD_Slider:
case kHIDUsage_GD_Dial: case kHIDUsage_GD_Dial:
case kHIDUsage_GD_Wheel: case kHIDUsage_GD_Wheel:
if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement)); element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) { if (element) {
pDevice->axes++; pDevice->axes++;
headElement = &(pDevice->firstAxis); headElement = &(pDevice->firstAxis);
} }
}
break; break;
case kHIDUsage_GD_Hatswitch: case kHIDUsage_GD_Hatswitch:
if (!ElementAlreadyAdded(cookie, pDevice->firstHat)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement)); element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) { if (element) {
pDevice->hats++; pDevice->hats++;
headElement = &(pDevice->firstHat); headElement = &(pDevice->firstHat);
} }
}
break; break;
} }
break; break;
@ -201,11 +217,13 @@ AddHIDElement(const void *value, void *parameter)
switch (usage) { switch (usage) {
case kHIDUsage_Sim_Rudder: case kHIDUsage_Sim_Rudder:
case kHIDUsage_Sim_Throttle: case kHIDUsage_Sim_Throttle:
if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement)); element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) { if (element) {
pDevice->axes++; pDevice->axes++;
headElement = &(pDevice->firstAxis); headElement = &(pDevice->firstAxis);
} }
}
break; break;
default: default:
@ -214,11 +232,13 @@ AddHIDElement(const void *value, void *parameter)
break; break;
case kHIDPage_Button: case kHIDPage_Button:
if (!ElementAlreadyAdded(cookie, pDevice->firstButton)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement)); element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) { if (element) {
pDevice->buttons++; pDevice->buttons++;
headElement = &(pDevice->firstButton); headElement = &(pDevice->firstButton);
} }
}
break; break;
default: default:
@ -227,7 +247,6 @@ AddHIDElement(const void *value, void *parameter)
} }
break; break;
#if 0 /* !!! FIXME: this causes everything to get added twice on a DualShock 4. */
case kIOHIDElementTypeCollection: { case kIOHIDElementTypeCollection: {
CFArrayRef array = IOHIDElementGetChildren(refElement); CFArrayRef array = IOHIDElementGetChildren(refElement);
if (array) { if (array) {
@ -235,7 +254,6 @@ AddHIDElement(const void *value, void *parameter)
} }
} }
break; break;
#endif
default: default:
break; break;
@ -261,6 +279,7 @@ AddHIDElement(const void *value, void *parameter)
element->minReport = element->min = (SInt32) IOHIDElementGetLogicalMin(refElement); element->minReport = element->min = (SInt32) IOHIDElementGetLogicalMin(refElement);
element->maxReport = element->max = (SInt32) IOHIDElementGetLogicalMax(refElement); element->maxReport = element->max = (SInt32) IOHIDElementGetLogicalMax(refElement);
element->cookie = IOHIDElementGetCookie(refElement);
pDevice->elements++; pDevice->elements++;
} }

View File

@ -27,6 +27,7 @@
struct recElement struct recElement
{ {
IOHIDElementRef elementRef; IOHIDElementRef elementRef;
IOHIDElementCookie cookie;
uint32_t usagePage, usage; /* HID usage */ uint32_t usagePage, usage; /* HID usage */
SInt32 min; /* reported min value possible */ SInt32 min; /* reported min value possible */
SInt32 max; /* reported max value possible */ SInt32 max; /* reported max value possible */