Merge pull request #4009 from leoetlino/common-id-assign

ControllerInterface: Make the ID assigning code common
This commit is contained in:
Pierre Bourdon 2016-07-14 23:13:30 +02:00 committed by GitHub
commit a9d8a7decd
25 changed files with 41 additions and 112 deletions

View File

@ -27,10 +27,6 @@ std::string Touchscreen::GetSource() const
return "Android"; return "Android";
} }
int Touchscreen::GetId() const
{
return _padID;
}
Touchscreen::Touchscreen(int padID) : _padID(padID) Touchscreen::Touchscreen(int padID) : _padID(padID)
{ {
// GC // GC

View File

@ -46,7 +46,6 @@ public:
Touchscreen(int padID); Touchscreen(int padID);
~Touchscreen() {} ~Touchscreen() {}
std::string GetName() const; std::string GetName() const;
int GetId() const;
std::string GetSource() const; std::string GetSource() const;
private: private:

View File

@ -143,6 +143,20 @@ void ControllerInterface::Shutdown()
void ControllerInterface::AddDevice(std::shared_ptr<ciface::Core::Device> device) void ControllerInterface::AddDevice(std::shared_ptr<ciface::Core::Device> device)
{ {
std::lock_guard<std::mutex> lk(m_devices_mutex); std::lock_guard<std::mutex> lk(m_devices_mutex);
// Try to find an ID for this device
int id = 0;
while (true)
{
const auto it = std::find_if(m_devices.begin(), m_devices.end(), [&device, &id](const auto& d) {
return d->GetSource() == device->GetSource() && d->GetName() == device->GetName() &&
d->GetId() == id;
});
if (it == m_devices.end()) // no device with the same name with this ID, so we can use it
break;
else
id++;
}
device->SetId(id);
m_devices.emplace_back(std::move(device)); m_devices.emplace_back(std::move(device));
} }

View File

@ -23,10 +23,6 @@ void InitJoystick(IDirectInput8* const idi8, HWND hwnd)
idi8->EnumDevices(DI8DEVCLASS_GAMECTRL, DIEnumDevicesCallback, (LPVOID)&joysticks, idi8->EnumDevices(DI8DEVCLASS_GAMECTRL, DIEnumDevicesCallback, (LPVOID)&joysticks,
DIEDFL_ATTACHEDONLY); DIEDFL_ATTACHEDONLY);
// this is used to number the joysticks
// multiple joysticks with the same name shall get unique ids starting at 0
std::map<std::basic_string<TCHAR>, int> name_counts;
std::vector<DWORD> xinput_guids; std::vector<DWORD> xinput_guids;
GetXInputGUIDS(&xinput_guids); GetXInputGUIDS(&xinput_guids);
@ -58,7 +54,7 @@ void InitJoystick(IDirectInput8* const idi8, HWND hwnd)
} }
} }
auto js = std::make_shared<Joystick>(js_device, name_counts[joystick.tszInstanceName]++); auto js = std::make_shared<Joystick>(js_device);
// only add if it has some inputs/outputs // only add if it has some inputs/outputs
if (js->Inputs().size() || js->Outputs().size()) if (js->Inputs().size() || js->Outputs().size())
g_controller_interface.AddDevice(std::move(js)); g_controller_interface.AddDevice(std::move(js));
@ -72,9 +68,8 @@ void InitJoystick(IDirectInput8* const idi8, HWND hwnd)
} }
} }
Joystick::Joystick(/*const LPCDIDEVICEINSTANCE lpddi, */ const LPDIRECTINPUTDEVICE8 device, Joystick::Joystick(/*const LPCDIDEVICEINSTANCE lpddi, */ const LPDIRECTINPUTDEVICE8 device)
const unsigned int index) : m_device(device)
: m_device(device), m_index(index)
//, m_name(TStringToString(lpddi->tszInstanceName)) //, m_name(TStringToString(lpddi->tszInstanceName))
{ {
// seems this needs to be done before GetCapabilities // seems this needs to be done before GetCapabilities
@ -167,11 +162,6 @@ std::string Joystick::GetName() const
return GetDeviceName(m_device); return GetDeviceName(m_device);
} }
int Joystick::GetId() const
{
return m_index;
}
std::string Joystick::GetSource() const std::string Joystick::GetSource() const
{ {
return DINPUT_SOURCE_NAME; return DINPUT_SOURCE_NAME;

View File

@ -62,16 +62,14 @@ private:
public: public:
void UpdateInput() override; void UpdateInput() override;
Joystick(const LPDIRECTINPUTDEVICE8 device, const unsigned int index); Joystick(const LPDIRECTINPUTDEVICE8 device);
~Joystick(); ~Joystick();
std::string GetName() const override; std::string GetName() const override;
int GetId() const override;
std::string GetSource() const override; std::string GetSource() const override;
private: private:
const LPDIRECTINPUTDEVICE8 m_device; const LPDIRECTINPUTDEVICE8 m_device;
const unsigned int m_index;
DIJOYSTATE m_state_in; DIJOYSTATE m_state_in;

View File

@ -185,12 +185,6 @@ std::string KeyboardMouse::GetName() const
return "Keyboard Mouse"; return "Keyboard Mouse";
} }
int KeyboardMouse::GetId() const
{
// should this be -1, idk
return 0;
}
std::string KeyboardMouse::GetSource() const std::string KeyboardMouse::GetSource() const
{ {
return DINPUT_SOURCE_NAME; return DINPUT_SOURCE_NAME;

View File

@ -89,7 +89,6 @@ public:
~KeyboardMouse(); ~KeyboardMouse();
std::string GetName() const override; std::string GetName() const override;
int GetId() const override;
std::string GetSource() const override; std::string GetSource() const override;
private: private:

View File

@ -93,8 +93,9 @@ public:
virtual ~Device(); virtual ~Device();
int GetId() const { return m_id; }
void SetId(int id) { m_id = id; }
virtual std::string GetName() const = 0; virtual std::string GetName() const = 0;
virtual int GetId() const = 0;
virtual std::string GetSource() const = 0; virtual std::string GetSource() const = 0;
virtual void UpdateInput() {} virtual void UpdateInput() {}
const std::vector<Input*>& Inputs() const { return m_inputs; } const std::vector<Input*>& Inputs() const { return m_inputs; }
@ -130,6 +131,7 @@ protected:
} }
private: private:
int m_id;
std::vector<Input*> m_inputs; std::vector<Input*> m_inputs;
std::vector<Output*> m_outputs; std::vector<Output*> m_outputs;
}; };

View File

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

View File

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

View File

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

View File

@ -60,11 +60,10 @@ private:
public: public:
void UpdateInput() override; 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 GetName() const override;
std::string GetSource() const override; std::string GetSource() const override;
int GetId() const override;
private: private:
struct struct
@ -74,7 +73,6 @@ private:
const IOHIDDeviceRef m_device; const IOHIDDeviceRef m_device;
const std::string m_device_name; const std::string m_device_name;
int m_index;
uint32_t m_windowid; uint32_t m_windowid;
unsigned char m_mousebuttons[3]; unsigned char m_mousebuttons[3];
}; };

View File

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

View File

@ -46,7 +46,6 @@ void Init()
// Search the Pipes directory for files that we can open in read-only, // Search the Pipes directory for files that we can open in read-only,
// non-blocking mode. The device name is the virtual name of the file. // non-blocking mode. The device name is the virtual name of the file.
File::FSTEntry fst; File::FSTEntry fst;
int found = 0;
std::string dir_path = File::GetUserPath(D_PIPES_IDX); std::string dir_path = File::GetUserPath(D_PIPES_IDX);
if (!File::Exists(dir_path)) if (!File::Exists(dir_path))
return; return;
@ -61,11 +60,11 @@ void Init()
int fd = open(child.physicalName.c_str(), O_RDONLY | O_NONBLOCK); int fd = open(child.physicalName.c_str(), O_RDONLY | O_NONBLOCK);
if (fd < 0) if (fd < 0)
continue; continue;
g_controller_interface.AddDevice(std::make_shared<PipeDevice>(fd, child.virtualName, found++)); g_controller_interface.AddDevice(std::make_shared<PipeDevice>(fd, child.virtualName));
} }
} }
PipeDevice::PipeDevice(int fd, const std::string& name, int id) : m_fd(fd), m_name(name), m_id(id) PipeDevice::PipeDevice(int fd, const std::string& name) : m_fd(fd), m_name(name)
{ {
for (const auto& tok : s_button_tokens) for (const auto& tok : s_button_tokens)
{ {

View File

@ -27,12 +27,11 @@ void Init();
class PipeDevice : public Core::Device class PipeDevice : public Core::Device
{ {
public: public:
PipeDevice(int fd, const std::string& name, int id); PipeDevice(int fd, const std::string& name);
~PipeDevice(); ~PipeDevice();
void UpdateInput() override; void UpdateInput() override;
std::string GetName() const override { return m_name; } std::string GetName() const override { return m_name; }
int GetId() const override { return m_id; }
std::string GetSource() const override { return "Pipe"; } std::string GetSource() const override { return "Pipe"; }
private: private:
class PipeInput : public Input class PipeInput : public Input
@ -53,7 +52,6 @@ private:
const int m_fd; const int m_fd;
const std::string m_name; const std::string m_name;
const int m_id;
std::string m_buf; std::string m_buf;
std::map<std::string, PipeInput*> m_buttons; std::map<std::string, PipeInput*> m_buttons;
std::map<std::string, PipeInput*> m_axes; std::map<std::string, PipeInput*> m_axes;

View File

@ -35,10 +35,6 @@ static std::string GetJoystickName(int index)
void Init() void Init()
{ {
// this is used to number the joysticks
// multiple joysticks with the same name shall get unique ids starting at 0
std::map<std::string, int> name_counts;
#ifdef USE_SDL_HAPTIC #ifdef USE_SDL_HAPTIC
if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) >= 0) if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) >= 0)
{ {
@ -58,7 +54,7 @@ void Init()
SDL_Joystick* dev = SDL_JoystickOpen(i); SDL_Joystick* dev = SDL_JoystickOpen(i);
if (dev) if (dev)
{ {
auto js = std::make_shared<Joystick>(dev, i, name_counts[GetJoystickName(i)]++); auto js = std::make_shared<Joystick>(dev, i);
// only add if it has some inputs/outputs // only add if it has some inputs/outputs
if (js->Inputs().size() || js->Outputs().size()) if (js->Inputs().size() || js->Outputs().size())
g_controller_interface.AddDevice(std::move(js)); g_controller_interface.AddDevice(std::move(js));
@ -66,8 +62,8 @@ void Init()
} }
} }
Joystick::Joystick(SDL_Joystick* const joystick, const int sdl_index, const unsigned int index) Joystick::Joystick(SDL_Joystick* const joystick, const int sdl_index)
: m_joystick(joystick), m_sdl_index(sdl_index), m_index(index) : m_joystick(joystick), m_sdl_index(sdl_index)
{ {
// really bad HACKS: // really bad HACKS:
// to not use SDL for an XInput device // to not use SDL for an XInput device
@ -290,11 +286,6 @@ std::string Joystick::GetSource() const
return "SDL"; return "SDL";
} }
int Joystick::GetId() const
{
return m_index;
}
std::string Joystick::Button::GetName() const std::string Joystick::Button::GetName() const
{ {
std::ostringstream ss; std::ostringstream ss;

View File

@ -142,17 +142,15 @@ private:
public: public:
void UpdateInput() override; void UpdateInput() override;
Joystick(SDL_Joystick* const joystick, const int sdl_index, const unsigned int index); Joystick(SDL_Joystick* const joystick, const int sdl_index);
~Joystick(); ~Joystick();
std::string GetName() const override; std::string GetName() const override;
int GetId() const override;
std::string GetSource() const override; std::string GetSource() const override;
private: private:
SDL_Joystick* const m_joystick; SDL_Joystick* const m_joystick;
const int m_sdl_index; const int m_sdl_index;
const unsigned int m_index;
#ifdef USE_SDL_HAPTIC #ifdef USE_SDL_HAPTIC
SDL_Haptic* m_haptic; SDL_Haptic* m_haptic;

View File

@ -171,11 +171,6 @@ std::string Device::GetName() const
} }
} }
int Device::GetId() const
{
return m_index;
}
std::string Device::GetSource() const std::string Device::GetSource() const
{ {
return "XInput"; return "XInput";

View File

@ -92,7 +92,6 @@ public:
Device(const XINPUT_CAPABILITIES& capabilities, u8 index); Device(const XINPUT_CAPABILITIES& capabilities, u8 index);
std::string GetName() const override; std::string GetName() const override;
int GetId() const override;
std::string GetSource() const override; std::string GetSource() const override;
void UpdateMotors(); void UpdateMotors();

View File

@ -300,11 +300,6 @@ std::string KeyboardMouse::GetSource() const
return "XInput2"; return "XInput2";
} }
int KeyboardMouse::GetId() const
{
return -1;
}
KeyboardMouse::Key::Key(Display* const display, KeyCode keycode, const char* keyboard) KeyboardMouse::Key::Key(Display* const display, KeyCode keycode, const char* keyboard)
: m_display(display), m_keyboard(keyboard), m_keycode(keycode) : m_display(display), m_keyboard(keyboard), m_keycode(keycode)
{ {

View File

@ -104,7 +104,6 @@ public:
std::string GetName() const override; std::string GetName() const override;
std::string GetSource() const override; std::string GetSource() const override;
int GetId() const override;
private: private:
Window m_window; Window m_window;

View File

@ -80,11 +80,6 @@ std::string KeyboardMouse::GetSource() const
return "Xlib"; return "Xlib";
} }
int KeyboardMouse::GetId() const
{
return 0;
}
KeyboardMouse::Key::Key(Display* const display, KeyCode keycode, const char* keyboard) KeyboardMouse::Key::Key(Display* const display, KeyCode keycode, const char* keyboard)
: m_display(display), m_keyboard(keyboard), m_keycode(keycode) : m_display(display), m_keyboard(keyboard), m_keycode(keycode)
{ {

View File

@ -81,7 +81,6 @@ public:
std::string GetName() const override; std::string GetName() const override;
std::string GetSource() const override; std::string GetSource() const override;
int GetId() const override;
private: private:
Window m_window; Window m_window;

View File

@ -36,12 +36,6 @@ static std::string GetName(const std::string& devnode)
void Init() void Init()
{ {
// this is used to number the joysticks
// multiple joysticks with the same name shall get unique ids starting at 0
std::map<std::string, int> name_counts;
int num_controllers = 0;
// We use Udev to find any devices. In the future this will allow for hotplugging. // We use Udev to find any devices. In the future this will allow for hotplugging.
// But for now it is essentially iterating over /dev/input/event0 to event31. However if the // But for now it is essentially iterating over /dev/input/event0 to event31. However if the
// naming scheme is ever updated in the future, this *should* be forwards compatable. // naming scheme is ever updated in the future, this *should* be forwards compatable.
@ -70,12 +64,11 @@ void Init()
// Unfortunately udev gives us no way to filter out the non event device interfaces. // Unfortunately udev gives us no way to filter out the non event device interfaces.
// So we open it and see if it works with evdev ioctls or not. // So we open it and see if it works with evdev ioctls or not.
std::string name = GetName(devnode); std::string name = GetName(devnode);
auto input = std::make_shared<evdevDevice>(devnode, name_counts[name]++); auto input = std::make_shared<evdevDevice>(devnode);
if (input->IsInteresting()) if (input->IsInteresting())
{ {
g_controller_interface.AddDevice(std::move(input)); g_controller_interface.AddDevice(std::move(input));
num_controllers++;
} }
} }
udev_device_unref(dev); udev_device_unref(dev);
@ -84,7 +77,7 @@ void Init()
udev_unref(udev); udev_unref(udev);
} }
evdevDevice::evdevDevice(const std::string& devnode, int id) : m_devfile(devnode), m_id(id) evdevDevice::evdevDevice(const std::string& devnode) : m_devfile(devnode)
{ {
// The device file will be read on one of the main threads, so we open in non-blocking mode. // The device file will be read on one of the main threads, so we open in non-blocking mode.
m_fd = open(devnode.c_str(), O_RDWR | O_NONBLOCK); m_fd = open(devnode.c_str(), O_RDWR | O_NONBLOCK);

View File

@ -63,11 +63,10 @@ private:
public: public:
void UpdateInput() override; void UpdateInput() override;
evdevDevice(const std::string& devnode, int id); evdevDevice(const std::string& devnode);
~evdevDevice(); ~evdevDevice();
std::string GetName() const override { return m_name; } std::string GetName() const override { return m_name; }
int GetId() const override { return m_id; }
std::string GetSource() const override { return "evdev"; } std::string GetSource() const override { return "evdev"; }
bool IsInteresting() const { return m_initialized && m_interesting; } bool IsInteresting() const { return m_initialized && m_interesting; }
private: private:
@ -75,7 +74,6 @@ private:
int m_fd; int m_fd;
libevdev* m_dev; libevdev* m_dev;
std::string m_name; std::string m_name;
const int m_id;
bool m_initialized; bool m_initialized;
bool m_interesting; bool m_interesting;
}; };