From 0d783f0869eaef163a318ab4d79744f592ed1cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Mon, 13 Jun 2016 11:11:47 +0200 Subject: ControllerInterface: Add a way to register callbacks This adds RegisterHotplugCallback() to register a callback which will be invoked by the input backends' hotplug threads when there is a new device, so that Core (GCKeyboard, GCPad, Wiimote, Hotkey) can reload the configuration without adding a dependency to Core from InputCommon. --- .../ControllerInterface/ControllerInterface.cpp | 22 ++++++++++++++++++++++ .../ControllerInterface/ControllerInterface.h | 4 ++++ 2 files changed, 26 insertions(+) (limited to 'Source/Core/InputCommon/ControllerInterface') diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index 700d390b92..cc80b7b234 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -172,6 +172,28 @@ void ControllerInterface::UpdateInput() d->UpdateInput(); } +// +// RegisterHotplugCallback +// +// Register a callback to be called from the input backends' hotplug thread +// when there is a new device +// +void ControllerInterface::RegisterHotplugCallback(std::function callback) +{ + m_hotplug_callbacks.emplace_back(std::move(callback)); +} + +// +// InvokeHotplugCallbacks +// +// Invoke all callbacks that were registered +// +void ControllerInterface::InvokeHotplugCallbacks() const +{ + for (const auto& callback : m_hotplug_callbacks) + callback(); +} + // // InputReference :: State // diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h index 2f4212ba85..24f92fd3c1 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h @@ -127,7 +127,11 @@ public: const ciface::Core::DeviceQualifier& default_device) const; void UpdateInput(); + void RegisterHotplugCallback(std::function callback); + void InvokeHotplugCallbacks() const; + private: + std::vector> m_hotplug_callbacks; bool m_is_init; void* m_hwnd; }; -- cgit v1.2.3 From 93f5df419521171c11e16d6a8ff84b9d0d8fd6c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Thu, 14 Jul 2016 17:45:59 +0200 Subject: ControllerInterface: Add RemoveDevice() This adds RemoveDevice() to ControllerInterface, fixes ExpressionParser and some other code to support device removals without crashing, and adds an IsValid() method to Device, to prepare for hotplugging. --- .../InputCommon/ControllerInterface/ControllerInterface.cpp | 8 ++++++++ .../InputCommon/ControllerInterface/ControllerInterface.h | 1 + Source/Core/InputCommon/ControllerInterface/Device.h | 1 + .../InputCommon/ControllerInterface/ExpressionParser.cpp | 13 +++++++++---- .../Core/InputCommon/ControllerInterface/ExpressionParser.h | 2 +- 5 files changed, 20 insertions(+), 5 deletions(-) (limited to 'Source/Core/InputCommon/ControllerInterface') diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index cc80b7b234..cf726698f1 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -160,6 +160,14 @@ void ControllerInterface::AddDevice(std::shared_ptr device m_devices.emplace_back(std::move(device)); } +void ControllerInterface::RemoveDevice(std::function callback) +{ + std::lock_guard lk(m_devices_mutex); + m_devices.erase(std::remove_if(m_devices.begin(), m_devices.end(), + [&callback](const auto& dev) { return callback(dev.get()); }), + m_devices.end()); +} + // // UpdateInput // diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h index 24f92fd3c1..198a2c180b 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h @@ -122,6 +122,7 @@ public: void Reinitialize(); void Shutdown(); void AddDevice(std::shared_ptr device); + void RemoveDevice(std::function callback); bool IsInit() const { return m_is_init; } void UpdateReference(ControlReference* control, const ciface::Core::DeviceQualifier& default_device) const; diff --git a/Source/Core/InputCommon/ControllerInterface/Device.h b/Source/Core/InputCommon/ControllerInterface/Device.h index e3fe4c11cd..657ece5016 100644 --- a/Source/Core/InputCommon/ControllerInterface/Device.h +++ b/Source/Core/InputCommon/ControllerInterface/Device.h @@ -98,6 +98,7 @@ public: virtual std::string GetName() const = 0; virtual std::string GetSource() const = 0; virtual void UpdateInput() {} + virtual bool IsValid() const { return true; } const std::vector& Inputs() const { return m_inputs; } const std::vector& Outputs() const { return m_outputs; } Input* FindInput(const std::string& name) const; diff --git a/Source/Core/InputCommon/ControllerInterface/ExpressionParser.cpp b/Source/Core/InputCommon/ControllerInterface/ExpressionParser.cpp index 6e9b8b77ab..2960888148 100644 --- a/Source/Core/InputCommon/ControllerInterface/ExpressionParser.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ExpressionParser.cpp @@ -235,8 +235,9 @@ public: ControlQualifier qualifier; Device::Control* control; - ControlExpression(ControlQualifier qualifier_, Device::Control* control_) - : qualifier(qualifier_), control(control_) + ControlExpression(ControlQualifier qualifier_, std::shared_ptr device, + Device::Control* control_) + : qualifier(qualifier_), control(control_), m_device(device) { } @@ -244,6 +245,8 @@ public: void SetValue(ControlState value) override { control->ToOutput()->SetGatedState(value); } int CountNumControls() override { return 1; } operator std::string() override { return "`" + (std::string)qualifier + "`"; } +private: + std::shared_ptr m_device; }; class BinaryExpression : public ExpressionNode @@ -393,6 +396,7 @@ private: { case TOK_CONTROL: { + std::shared_ptr device = finder.FindDevice(tok.qualifier); Device::Control* control = finder.FindControl(tok.qualifier); if (control == nullptr) { @@ -400,7 +404,7 @@ private: return EXPRESSION_PARSE_SUCCESS; } - *expr_out = new ControlExpression(tok.qualifier, control); + *expr_out = new ControlExpression(tok.qualifier, device, control); return EXPRESSION_PARSE_SUCCESS; } case TOK_LPAREN: @@ -550,10 +554,11 @@ ExpressionParseStatus ParseExpression(const std::string& str, ControlFinder& fin qualifier.control_name = str; qualifier.has_device = false; + std::shared_ptr device = finder.FindDevice(qualifier); Device::Control* control = finder.FindControl(qualifier); if (control) { - *expr_out = new Expression(new ControlExpression(qualifier, control)); + *expr_out = new Expression(new ControlExpression(qualifier, device, control)); return EXPRESSION_PARSE_SUCCESS; } diff --git a/Source/Core/InputCommon/ControllerInterface/ExpressionParser.h b/Source/Core/InputCommon/ControllerInterface/ExpressionParser.h index 992ca65f03..2c8f5a793f 100644 --- a/Source/Core/InputCommon/ControllerInterface/ExpressionParser.h +++ b/Source/Core/InputCommon/ControllerInterface/ExpressionParser.h @@ -37,10 +37,10 @@ public: : container(container_), default_device(default_), is_input(is_input_) { } + std::shared_ptr FindDevice(ControlQualifier qualifier); Core::Device::Control* FindControl(ControlQualifier qualifier); private: - std::shared_ptr FindDevice(ControlQualifier qualifier); const Core::DeviceContainer& container; const Core::DeviceQualifier& default_device; bool is_input; -- cgit v1.2.3 From 3926db624d5ab2f959e67eb2369f1fe5f0f9543a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Fri, 15 Jul 2016 11:34:18 +0200 Subject: ControllerInterface: Don't block on UpdateInput() Changes UpdateInput() to skip if we can't lock the mutex, instead of potentially blocking the CPU thread and causing a short but noticeable frame drop. --- .../InputCommon/ControllerInterface/ControllerInterface.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'Source/Core/InputCommon/ControllerInterface') diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index cf726698f1..9c7a08428e 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -175,9 +175,13 @@ void ControllerInterface::RemoveDevice(std::function lk(m_devices_mutex); - for (const auto& d : m_devices) - d->UpdateInput(); + // Don't block the UI or CPU thread (to avoid a short but noticeable frame drop) + if (m_devices_mutex.try_lock()) + { + std::lock_guard lk(m_devices_mutex, std::adopt_lock); + for (const auto& d : m_devices) + d->UpdateInput(); + } } // -- cgit v1.2.3 From 135641404af9c52e433ef28dba4f86fa208f4365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Thu, 14 Jul 2016 17:50:35 +0200 Subject: evdev: Add hotplugging support This adds hotplugging support to the evdev input backend. We use libudev to monitor changes to input devices in a separate thread. Removed devices are removed from the devices list, and new devices are added to the list. The effect is that controllers are usable immediately after plugging them without having to manually refresh devices (if they were configured to be used, of course). --- .../ControllerInterface/ControllerInterface.cpp | 25 ++-- .../ControllerInterface/evdev/evdev.cpp | 140 ++++++++++++++++++++- .../InputCommon/ControllerInterface/evdev/evdev.h | 4 + 3 files changed, 152 insertions(+), 17 deletions(-) (limited to 'Source/Core/InputCommon/ControllerInterface') diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index 9c7a08428e..f1e62e73cf 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -106,17 +106,6 @@ void ControllerInterface::Shutdown() if (!m_is_init) return; - std::lock_guard lk(m_devices_mutex); - - for (const auto& d : m_devices) - { - // Set outputs to ZERO before destroying device - for (ciface::Core::Device::Output* o : d->Outputs()) - o->SetState(0); - } - - m_devices.clear(); - #ifdef CIFACE_USE_XINPUT ciface::XInput::DeInit(); #endif @@ -136,6 +125,20 @@ void ControllerInterface::Shutdown() #ifdef CIFACE_USE_ANDROID // nothing needed #endif +#ifdef CIFACE_USE_EVDEV + ciface::evdev::Shutdown(); +#endif + + std::lock_guard lk(m_devices_mutex); + + for (const auto& d : m_devices) + { + // Set outputs to ZERO before destroying device + for (ciface::Core::Device::Output* o : d->Outputs()) + o->SetState(0); + } + + m_devices.clear(); m_is_init = false; } diff --git a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp index 16d0d108b2..08687332fb 100644 --- a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp +++ b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp @@ -5,12 +5,17 @@ #include #include #include +#include #include +#include + #include "Common/Assert.h" +#include "Common/Flag.h" #include "Common/Logging/Log.h" #include "Common/MathUtil.h" #include "Common/StringUtil.h" +#include "Common/Thread.h" #include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/evdev/evdev.h" @@ -18,6 +23,15 @@ namespace ciface { namespace evdev { +static std::thread s_hotplug_thread; +static Common::Flag s_hotplug_thread_running; +static int s_wakeup_eventfd; + +// There is no easy way to get the device name from only a dev node +// during a device removed event, since libevdev can't work on removed devices; +// sysfs is not stable, so this is probably the easiest way to get a name for a node. +static std::map s_devnode_name_map; + static std::string GetName(const std::string& devnode) { int fd = open(devnode.c_str(), O_RDWR | O_NONBLOCK); @@ -28,20 +42,110 @@ static std::string GetName(const std::string& devnode) close(fd); return std::string(); } - std::string res = libevdev_get_name(dev); + std::string res = StripSpaces(libevdev_get_name(dev)); libevdev_free(dev); close(fd); return res; } +static void HotplugThreadFunc() +{ + Common::SetCurrentThreadName("evdev Hotplug Thread"); + NOTICE_LOG(SERIALINTERFACE, "evdev hotplug thread started"); + + udev* udev = udev_new(); + _assert_msg_(PAD, udev != nullptr, "Couldn't initialize libudev."); + + // Set up monitoring + udev_monitor* monitor = udev_monitor_new_from_netlink(udev, "udev"); + udev_monitor_filter_add_match_subsystem_devtype(monitor, "input", nullptr); + udev_monitor_enable_receiving(monitor); + const int monitor_fd = udev_monitor_get_fd(monitor); + + while (s_hotplug_thread_running.IsSet()) + { + fd_set fds; + + FD_ZERO(&fds); + FD_SET(monitor_fd, &fds); + FD_SET(s_wakeup_eventfd, &fds); + + int ret = select(monitor_fd + 1, &fds, nullptr, nullptr, nullptr); + if (ret < 1 || !FD_ISSET(monitor_fd, &fds)) + continue; + + udev_device* dev = udev_monitor_receive_device(monitor); + + const char* action = udev_device_get_action(dev); + const char* devnode = udev_device_get_devnode(dev); + if (!devnode) + continue; + + if (strcmp(action, "remove") == 0) + { + const auto it = s_devnode_name_map.find(devnode); + if (it == s_devnode_name_map.end()) + continue; // we don't know the name for this device, so it is probably not an evdev device + const std::string& name = it->second; + g_controller_interface.RemoveDevice([&name](const auto& device) { + return device->GetSource() == "evdev" && device->GetName() == name && !device->IsValid(); + }); + NOTICE_LOG(SERIALINTERFACE, "Removed device: %s", name.c_str()); + s_devnode_name_map.erase(devnode); + g_controller_interface.InvokeHotplugCallbacks(); + } + // Only react to "device added" events for evdev devices that we can access. + else if (strcmp(action, "add") == 0 && access(devnode, W_OK) == 0) + { + const std::string name = GetName(devnode); + if (name.empty()) + continue; // probably not an evdev device + auto device = std::make_shared(devnode); + if (device->IsInteresting()) + { + g_controller_interface.AddDevice(std::move(device)); + s_devnode_name_map.insert(std::pair(devnode, name)); + NOTICE_LOG(SERIALINTERFACE, "Added new device: %s", name.c_str()); + g_controller_interface.InvokeHotplugCallbacks(); + } + } + udev_device_unref(dev); + } + NOTICE_LOG(SERIALINTERFACE, "evdev hotplug thread stopped"); +} + +static void StartHotplugThread() +{ + if (s_hotplug_thread_running.IsSet()) + return; + + s_wakeup_eventfd = eventfd(0, 0); + _assert_msg_(PAD, s_wakeup_eventfd != -1, "Couldn't create eventfd."); + s_hotplug_thread_running.Set(true); + s_hotplug_thread = std::thread(HotplugThreadFunc); +} + +static void StopHotplugThread() +{ + if (s_hotplug_thread_running.TestAndClear()) + { + // Write something to efd so that select() stops blocking. + uint64_t value = 1; + write(s_wakeup_eventfd, &value, sizeof(uint64_t)); + s_hotplug_thread.join(); + } +} + void Init() { - // 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 - // naming scheme is ever updated in the future, this *should* be forwards compatable. + s_devnode_name_map.clear(); - struct udev* udev = udev_new(); - _assert_msg_(PAD, udev != 0, "Couldn't initilize libudev."); + // During initialization we use udev to iterate over all /dev/input/event* devices. + // Note: the Linux kernel is currently limited to just 32 event devices. If this ever + // changes, hopefully udev will take care of this. + + udev* udev = udev_new(); + _assert_msg_(PAD, udev != nullptr, "Couldn't initialize libudev."); // List all input devices udev_enumerate* enumerate = udev_enumerate_new(udev); @@ -69,12 +173,20 @@ void Init() if (input->IsInteresting()) { g_controller_interface.AddDevice(std::move(input)); + s_devnode_name_map.insert(std::pair(devnode, name)); } } udev_device_unref(dev); } udev_enumerate_unref(enumerate); udev_unref(udev); + + StartHotplugThread(); +} + +void Shutdown() +{ + StopHotplugThread(); } evdevDevice::evdevDevice(const std::string& devnode) : m_devfile(devnode) @@ -152,6 +264,22 @@ void evdevDevice::UpdateInput() } while (rc >= 0); } +bool evdevDevice::IsValid() const +{ + int current_fd = libevdev_get_fd(m_dev); + if (current_fd == -1) + return false; + + libevdev* device; + if (libevdev_new_from_fd(current_fd, &device) != 0) + { + close(current_fd); + return false; + } + libevdev_free(device); + return true; +} + std::string evdevDevice::Button::GetName() const { // Buttons below 0x100 are mostly keyboard keys, and the names make sense diff --git a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.h b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.h index 081913fa8e..dc8e917e23 100644 --- a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.h +++ b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.h @@ -8,11 +8,14 @@ #include #include +#include "InputCommon/ControllerInterface/ControllerInterface.h" + namespace ciface { namespace evdev { void Init(); +void Shutdown(); class evdevDevice : public Core::Device { @@ -62,6 +65,7 @@ private: public: void UpdateInput() override; + bool IsValid() const override; evdevDevice(const std::string& devnode); ~evdevDevice(); -- cgit v1.2.3