From 323bdfa18382986763450e55a6117e78873e6cfd Mon Sep 17 00:00:00 2001 From: capitalistspz Date: Tue, 19 Sep 2023 16:54:38 +0100 Subject: [PATCH] More changes to finding wiimotes (#961) --- .../settings/WiimoteControllerSettings.cpp | 1 - .../api/Wiimote/WiimoteControllerProvider.cpp | 46 +++++++++++++------ .../api/Wiimote/hidapi/HidapiWiimote.cpp | 13 ++---- src/input/api/Wiimote/hidapi/HidapiWiimote.h | 3 +- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/gui/input/settings/WiimoteControllerSettings.cpp b/src/gui/input/settings/WiimoteControllerSettings.cpp index 5bc20269..9830b666 100644 --- a/src/gui/input/settings/WiimoteControllerSettings.cpp +++ b/src/gui/input/settings/WiimoteControllerSettings.cpp @@ -56,7 +56,6 @@ WiimoteControllerSettings::WiimoteControllerSettings(wxWindow* parent, const wxP // Motion m_use_motion = new wxCheckBox(box, wxID_ANY, _("Use motion")); m_use_motion->SetValue(m_settings.motion); - m_use_motion->SetValue(m_settings.motion); m_use_motion->Enable(m_controller->has_motion()); row_sizer->Add(m_use_motion, 0, wxALL, 5); diff --git a/src/input/api/Wiimote/WiimoteControllerProvider.cpp b/src/input/api/Wiimote/WiimoteControllerProvider.cpp index 0ebf88aa..0ca00a1a 100644 --- a/src/input/api/Wiimote/WiimoteControllerProvider.cpp +++ b/src/input/api/Wiimote/WiimoteControllerProvider.cpp @@ -9,6 +9,7 @@ #endif #include +#include WiimoteControllerProvider::WiimoteControllerProvider() : m_running(true) @@ -30,20 +31,39 @@ WiimoteControllerProvider::~WiimoteControllerProvider() std::vector> WiimoteControllerProvider::get_controllers() { std::scoped_lock lock(m_device_mutex); - for (const auto& device : WiimoteDevice_t::get_devices()) + + std::queue disconnected_wiimote_indices; + for (auto i{0u}; i < m_wiimotes.size(); ++i){ + if (!(m_wiimotes[i].connected = m_wiimotes[i].device->write_data({kStatusRequest, 0x00}))){ + disconnected_wiimote_indices.push(i); + } + } + + const auto valid_new_device = [&](std::shared_ptr & device) { + const auto writeable = device->write_data({kStatusRequest, 0x00}); + const auto not_already_connected = + std::none_of(m_wiimotes.cbegin(), m_wiimotes.cend(), + [device](const auto& it) { + return (*it.device == *device) && it.connected; + }); + return writeable && not_already_connected; + }; + + for (auto& device : WiimoteDevice_t::get_devices()) { - // test connection of all devices as they might have been changed - const bool is_connected = device->write_data({kStatusRequest, 0x00}); - if (is_connected) - { - // only add unknown, connected devices to our list - const bool is_new_device = std::none_of(m_wiimotes.cbegin(), m_wiimotes.cend(), - [device](const auto& it) { return *it.device == *device; }); - if (is_new_device) - { - m_wiimotes.push_back(std::make_unique(device)); - } - } + if (!valid_new_device(device)) + continue; + // Replace disconnected wiimotes + if (!disconnected_wiimote_indices.empty()){ + const auto idx = disconnected_wiimote_indices.front(); + disconnected_wiimote_indices.pop(); + + m_wiimotes.replace(idx, std::make_unique(device)); + } + // Otherwise add them + else { + m_wiimotes.push_back(std::make_unique(device)); + } } std::vector> result; diff --git a/src/input/api/Wiimote/hidapi/HidapiWiimote.cpp b/src/input/api/Wiimote/hidapi/HidapiWiimote.cpp index 898e6cf4..a5701f56 100644 --- a/src/input/api/Wiimote/hidapi/HidapiWiimote.cpp +++ b/src/input/api/Wiimote/hidapi/HidapiWiimote.cpp @@ -5,8 +5,8 @@ static constexpr uint16 WIIMOTE_PRODUCT_ID = 0x0306; static constexpr uint16 WIIMOTE_MP_PRODUCT_ID = 0x0330; static constexpr uint16 WIIMOTE_MAX_INPUT_REPORT_LENGTH = 22; -HidapiWiimote::HidapiWiimote(hid_device* dev, uint64_t identifier, std::string_view path) - : m_handle(dev), m_identifier(identifier), m_path(path) { +HidapiWiimote::HidapiWiimote(hid_device* dev, std::string_view path) + : m_handle(dev), m_path(path) { } @@ -36,11 +36,7 @@ std::vector HidapiWiimote::get_devices() { } else { hid_set_nonblocking(dev, true); - // Enough to have a unique id for each device within a session - uint64_t id = (static_cast(it->interface_number) << 32) | - (static_cast(it->usage_page) << 16) | - (it->usage); - wiimote_devices.push_back(std::make_shared(dev, id, it->path)); + wiimote_devices.push_back(std::make_shared(dev, it->path)); } } hid_free_enumeration(device_enumeration); @@ -48,8 +44,7 @@ std::vector HidapiWiimote::get_devices() { } bool HidapiWiimote::operator==(WiimoteDevice& o) const { - auto const& other_mote = static_cast(o); - return m_identifier == other_mote.m_identifier && other_mote.m_path == m_path; + return static_cast(o).m_path == m_path; } HidapiWiimote::~HidapiWiimote() { diff --git a/src/input/api/Wiimote/hidapi/HidapiWiimote.h b/src/input/api/Wiimote/hidapi/HidapiWiimote.h index 7b91dbbe..858cb1f3 100644 --- a/src/input/api/Wiimote/hidapi/HidapiWiimote.h +++ b/src/input/api/Wiimote/hidapi/HidapiWiimote.h @@ -5,7 +5,7 @@ class HidapiWiimote : public WiimoteDevice { public: - HidapiWiimote(hid_device* dev, uint64_t identifier, std::string_view path); + HidapiWiimote(hid_device* dev, std::string_view path); ~HidapiWiimote() override; bool write_data(const std::vector &data) override; @@ -16,7 +16,6 @@ public: private: hid_device* m_handle; - const uint64_t m_identifier; const std::string m_path; };