Merge pull request #5732 from arthurc/fix-multiple-axis-inputs

Prevent multiple HID elements of same usage type on OSX
This commit is contained in:
Leo Lam 2017-09-15 19:28:04 +02:00 committed by GitHub
commit 7caf44c8b5
1 changed files with 17 additions and 0 deletions

View File

@ -2,6 +2,7 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include <sstream> #include <sstream>
#include <Foundation/Foundation.h> #include <Foundation/Foundation.h>
@ -49,11 +50,26 @@ Joystick::Joystick(IOHIDDeviceRef device, std::string name)
if (axes) if (axes)
{ {
std::vector<IOHIDElementRef> elems;
for (int i = 0; i < CFArrayGetCount(axes); i++) for (int i = 0; i < CFArrayGetCount(axes); i++)
{ {
IOHIDElementRef e = (IOHIDElementRef)CFArrayGetValueAtIndex(axes, i); IOHIDElementRef e = (IOHIDElementRef)CFArrayGetValueAtIndex(axes, i);
// DeviceElementDebugPrint(e, nullptr); // DeviceElementDebugPrint(e, nullptr);
uint32_t usage = IOHIDElementGetUsage(e);
// Check for any existing elements with the same usage
auto it = std::find_if(elems.begin(), elems.end(), [usage](const auto& ref) {
return usage == IOHIDElementGetUsage(ref);
});
if (it == elems.end())
elems.push_back(e);
else
*it = e;
}
for (auto e : elems)
{
if (IOHIDElementGetUsage(e) == kHIDUsage_GD_Hatswitch) if (IOHIDElementGetUsage(e) == kHIDUsage_GD_Hatswitch)
{ {
AddInput(new Hat(e, m_device, Hat::up)); AddInput(new Hat(e, m_device, Hat::up));
@ -67,6 +83,7 @@ Joystick::Joystick(IOHIDDeviceRef device, std::string name)
new Axis(e, m_device, Axis::positive)); new Axis(e, m_device, Axis::positive));
} }
} }
CFRelease(axes); CFRelease(axes);
} }