only call hid_exit when all hid_handlers are done

This commit is contained in:
Megamouse 2021-02-12 22:42:00 +01:00
parent 1479468730
commit 1b2260132b
1 changed files with 28 additions and 21 deletions

View File

@ -9,10 +9,15 @@
LOG_CHANNEL(hid_log, "HID"); LOG_CHANNEL(hid_log, "HID");
static std::mutex s_hid_mutex; // hid_pad_handler is created by pad_thread and pad_settings_dialog
static u8 s_hid_instances{0};
template <class Device> template <class Device>
hid_pad_handler<Device>::hid_pad_handler(pad_handler type, u16 vid, std::vector<u16> pids) hid_pad_handler<Device>::hid_pad_handler(pad_handler type, u16 vid, std::vector<u16> pids)
: PadHandlerBase(type), m_vid(vid), m_pids(std::move(pids)) : PadHandlerBase(type), m_vid(vid), m_pids(std::move(pids))
{ {
std::scoped_lock lock(s_hid_mutex);
ensure(s_hid_instances++ < 255);
}; };
template <class Device> template <class Device>
@ -26,9 +31,16 @@ hid_pad_handler<Device>::~hid_pad_handler()
} }
} }
std::scoped_lock lock(s_hid_mutex);
ensure(s_hid_instances-- > 0);
if (s_hid_instances == 0)
{
// Call hid_exit after all hid_pad_handlers are finished
if (hid_exit() != 0) if (hid_exit() != 0)
{ {
hid_log.error("%s hid_exit failed!", m_type); hid_log.error("hid_exit failed!");
}
hid_log.error("hid_exit !");
} }
} }
@ -126,18 +138,14 @@ void hid_pad_handler<Device>::enumerate_devices()
// Find and add new devices // Find and add new devices
for (const auto& path : device_paths) for (const auto& path : device_paths)
{
// Check if we already have this controller
const auto it_found = std::find_if(m_controllers.cbegin(), m_controllers.cend(), [path](const auto& c) { return c.second && c.second->path == path; });
if (it_found == m_controllers.cend())
{ {
// Check if we have at least one virtual controller left // Check if we have at least one virtual controller left
const auto it_free = std::find_if(m_controllers.cbegin(), m_controllers.cend(), [](const auto& c) { return !c.second || !c.second->hidDevice; }); if (std::none_of(m_controllers.cbegin(), m_controllers.cend(), [](const auto& c) { return !c.second || !c.second->hidDevice; }))
if (it_free == m_controllers.cend())
{
break; break;
}
// Check if we already have this controller
if (std::any_of(m_controllers.cbegin(), m_controllers.cend(), [&path](const auto& c) { return c.second && c.second->path == path; }))
continue;
hid_device* dev = hid_open_path(path.c_str()); hid_device* dev = hid_open_path(path.c_str());
if (dev) if (dev)
@ -150,7 +158,6 @@ void hid_pad_handler<Device>::enumerate_devices()
warn_about_drivers = true; warn_about_drivers = true;
} }
} }
}
if (warn_about_drivers) if (warn_about_drivers)
{ {