More changes to finding wiimotes (#961)

This commit is contained in:
capitalistspz 2023-09-19 16:54:38 +01:00 committed by GitHub
parent 98b5a8758a
commit 323bdfa183
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 25 deletions

View File

@ -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);

View File

@ -9,6 +9,7 @@
#endif
#include <numbers>
#include <queue>
WiimoteControllerProvider::WiimoteControllerProvider()
: m_running(true)
@ -30,20 +31,39 @@ WiimoteControllerProvider::~WiimoteControllerProvider()
std::vector<std::shared_ptr<ControllerBase>> WiimoteControllerProvider::get_controllers()
{
std::scoped_lock lock(m_device_mutex);
for (const 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<Wiimote>(device));
std::queue<uint32> 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<WiimoteDevice> & 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())
{
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<Wiimote>(device));
}
// Otherwise add them
else {
m_wiimotes.push_back(std::make_unique<Wiimote>(device));
}
}
std::vector<std::shared_ptr<ControllerBase>> result;

View File

@ -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<WiimoteDevicePtr> 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<uint64>(it->interface_number) << 32) |
(static_cast<uint64>(it->usage_page) << 16) |
(it->usage);
wiimote_devices.push_back(std::make_shared<HidapiWiimote>(dev, id, it->path));
wiimote_devices.push_back(std::make_shared<HidapiWiimote>(dev, it->path));
}
}
hid_free_enumeration(device_enumeration);
@ -48,8 +44,7 @@ std::vector<WiimoteDevicePtr> HidapiWiimote::get_devices() {
}
bool HidapiWiimote::operator==(WiimoteDevice& o) const {
auto const& other_mote = static_cast<HidapiWiimote const&>(o);
return m_identifier == other_mote.m_identifier && other_mote.m_path == m_path;
return static_cast<HidapiWiimote const&>(o).m_path == m_path;
}
HidapiWiimote::~HidapiWiimote() {

View File

@ -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<uint8> &data) override;
@ -16,7 +16,6 @@ public:
private:
hid_device* m_handle;
const uint64_t m_identifier;
const std::string m_path;
};