Skip to content

Commit

Permalink
check if device is opened prior to constructing HidapiWiimote
Browse files Browse the repository at this point in the history
  • Loading branch information
capitalistspz committed Aug 10, 2023
1 parent 7e330fb commit 024c7ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/input/api/Wiimote/hidapi/HidapiWiimote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ static constexpr uint16 WIIMOTE_VENDOR_ID = 0x057e;
static constexpr uint16 WIIMOTE_PRODUCT_ID = 0x0306;
static constexpr uint16 WIIMOTE_MAX_INPUT_REPROT_LENGTH = 21;

HidapiWiimote::HidapiWiimote(fs::path const& device_path, uint64_t identifier)
: m_handle(hid_open_path(_pathToUtf8(device_path).c_str())), m_identifier(identifier) {
HidapiWiimote::HidapiWiimote(hid_device* dev, uint64_t identifier)
: m_handle(dev), m_identifier(identifier) {

}

Expand All @@ -27,11 +27,17 @@ std::vector<WiimoteDevicePtr> HidapiWiimote::get_devices() {
const auto device_enumeration = hid_enumerate(WIIMOTE_VENDOR_ID, WIIMOTE_PRODUCT_ID);
auto it = device_enumeration;
while (it){
// 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>(it->path, id));
auto dev = hid_open_path(it->path);
if (!dev){
cemuLog_logDebug(LogType::Force, "Unable to open Wiimote device at {}: {}", it->path, boost::nowide::narrow(hid_error(nullptr)));
}
else {
// 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 = it->next;
}
hid_free_enumeration(device_enumeration);
Expand Down
2 changes: 1 addition & 1 deletion src/input/api/Wiimote/hidapi/HidapiWiimote.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class HidapiWiimote : public WiimoteDevice {
public:
HidapiWiimote(fs::path const& device_path, uint64_t identifier);
HidapiWiimote(hid_device* dev, uint64_t identifier);
~HidapiWiimote() override;

bool write_data(const std::vector<uint8> &data) override;
Expand Down

0 comments on commit 024c7ff

Please sign in to comment.