ControllerInterface: Make the ID assigning code common

This makes the device ID assigning code common to all backends, by
moving it to AddDevice() instead of copy-pasting or replicating
the logic in the backends.

Also, to prepare for hotplugging, instead of relying on a name usage
count, the new ID assigning system always starts from ID 0 and tries
to assign the first ID that is not used.
This commit is contained in:
Léo Lam
2016-07-14 10:25:52 +02:00
parent 89a03174cc
commit 788e19f54d
25 changed files with 41 additions and 112 deletions

View File

@ -21,7 +21,6 @@ namespace OSX
{
static IOHIDManagerRef HIDManager = nullptr;
static CFStringRef OurRunLoop = CFSTR("DolphinOSXInput");
static std::map<std::string, int> kbd_name_counts, joy_name_counts;
void DeviceElementDebugPrint(const void* value, void* context)
{
@ -145,17 +144,13 @@ static void DeviceMatching_callback(void* inContext, IOReturn inResult, void* in
// Add a device if it's of a type we want
if (IOHIDDeviceConformsTo(inIOHIDDeviceRef, kHIDPage_GenericDesktop, kHIDUsage_GD_Keyboard))
g_controller_interface.AddDevice(
std::make_shared<Keyboard>(inIOHIDDeviceRef, name, kbd_name_counts[name]++, g_window));
g_controller_interface.AddDevice(std::make_shared<Keyboard>(inIOHIDDeviceRef, name, g_window));
#if 0
else if (IOHIDDeviceConformsTo(inIOHIDDeviceRef,
kHIDPage_GenericDesktop, kHIDUsage_GD_Mouse))
g_controller_interface.AddDevice(new Mouse(inIOHIDDeviceRef,
name, mouse_name_counts[name]++));
else if (IOHIDDeviceConformsTo(inIOHIDDeviceRef, kHIDPage_GenericDesktop, kHIDUsage_GD_Mouse))
g_controller_interface.AddDevice(new Mouse(inIOHIDDeviceRef, name));
#endif
else
g_controller_interface.AddDevice(
std::make_shared<Joystick>(inIOHIDDeviceRef, name, joy_name_counts[name]++));
g_controller_interface.AddDevice(std::make_shared<Joystick>(inIOHIDDeviceRef, name));
}
void Init(void* window)
@ -176,9 +171,6 @@ void Init(void* window)
if (IOHIDManagerOpen(HIDManager, kIOHIDOptionsTypeNone) != kIOReturnSuccess)
NSLog(@"Failed to open HID Manager");
kbd_name_counts.clear();
joy_name_counts.clear();
// Wait while current devices are initialized
while (CFRunLoopRunInMode(OurRunLoop, 0, TRUE) == kCFRunLoopRunHandledSource)
{

View File

@ -73,17 +73,15 @@ private:
};
public:
Joystick(IOHIDDeviceRef device, std::string name, int index);
Joystick(IOHIDDeviceRef device, std::string name);
~Joystick();
std::string GetName() const override;
std::string GetSource() const override;
int GetId() const override;
private:
const IOHIDDeviceRef m_device;
const std::string m_device_name;
const int m_index;
ForceFeedback::FFDeviceAdapterReference m_ff_device;
};

View File

@ -14,8 +14,8 @@ namespace ciface
{
namespace OSX
{
Joystick::Joystick(IOHIDDeviceRef device, std::string name, int index)
: m_device(device), m_device_name(name), m_index(index), m_ff_device(nullptr)
Joystick::Joystick(IOHIDDeviceRef device, std::string name)
: m_device(device), m_device_name(name), m_ff_device(nullptr)
{
// Buttons
NSDictionary* buttonDict = @{
@ -93,11 +93,6 @@ std::string Joystick::GetSource() const
return "Input";
}
int Joystick::GetId() const
{
return m_index;
}
ControlState Joystick::Button::GetState() const
{
IOHIDValueRef value;

View File

@ -60,11 +60,10 @@ private:
public:
void UpdateInput() override;
Keyboard(IOHIDDeviceRef device, std::string name, int index, void* window);
Keyboard(IOHIDDeviceRef device, std::string name, void* window);
std::string GetName() const override;
std::string GetSource() const override;
int GetId() const override;
private:
struct
@ -74,7 +73,6 @@ private:
const IOHIDDeviceRef m_device;
const std::string m_device_name;
int m_index;
uint32_t m_windowid;
unsigned char m_mousebuttons[3];
};

View File

@ -14,8 +14,8 @@ namespace ciface
{
namespace OSX
{
Keyboard::Keyboard(IOHIDDeviceRef device, std::string name, int index, void* window)
: m_device(device), m_device_name(name), m_index(index)
Keyboard::Keyboard(IOHIDDeviceRef device, std::string name, void* window)
: m_device(device), m_device_name(name)
{
// This class should only recieve Keyboard or Keypad devices
// Now, filter on just the buttons we can handle sanely
@ -98,11 +98,6 @@ std::string Keyboard::GetSource() const
return "Keyboard";
}
int Keyboard::GetId() const
{
return m_index;
}
Keyboard::Key::Key(IOHIDElementRef element, IOHIDDeviceRef device)
: m_element(element), m_device(device)
{