From cff0773e00d756af6669b127ef6c09562ace62c3 Mon Sep 17 00:00:00 2001 From: Gliniak <153369+Gliniak@users.noreply.github.com> Date: Sun, 26 Apr 2026 13:26:30 +0200 Subject: [PATCH] [HID] Portal: Simplified device searching procedure --- src/xenia/hid/portal/hardware_portal.cc | 42 ++++++------------------- src/xenia/hid/portal/hardware_portal.h | 2 ++ src/xenia/hid/portal/portal.cc | 9 ++---- src/xenia/hid/portal/portal.h | 5 +-- 4 files changed, 15 insertions(+), 43 deletions(-) diff --git a/src/xenia/hid/portal/hardware_portal.cc b/src/xenia/hid/portal/hardware_portal.cc index 98b8b274f..8c449dbc6 100644 --- a/src/xenia/hid/portal/hardware_portal.cc +++ b/src/xenia/hid/portal/hardware_portal.cc @@ -26,53 +26,31 @@ HardwarePortal::~HardwarePortal() { libusb_exit(context_); } +bool HardwarePortal::IsConnected() { return handle_ != nullptr; } + void HardwarePortal::OpenDevice() { - if (!context_ || connected_) { + if (!context_ || handle_) { return; } - libusb_device** devs; - const ssize_t cnt = libusb_get_device_list(context_, &devs); - if (cnt < 0) { - // No device available... It might appear later. - return; - } - - for (ssize_t i = 0; i < cnt; ++i) { - libusb_device* dev = devs[i]; - - libusb_device_descriptor desc; - if (libusb_get_device_descriptor(dev, &desc) == 0) { - // Check if this device matches the target Vendor ID and Product ID. - // Small limitation. We're only supporting one device being connected at - // the time. - for (const auto& entry : kPortalVendorProductIdList) { - if (desc.idVendor == entry.first && desc.idProduct == entry.second) { - if (libusb_open(dev, &handle_) == 0) { - libusb_claim_interface(handle_, 0); - connected_ = true; - // We found device. No need to go thorugh remaining IDs. - break; - } - } - } - } - // We found device. No need to go thorugh remaining devices. - if (connected_) { + // Allow only one portal device at the time. + for (const auto& entry : kPortalVendorProductIdList) { + handle_ = + libusb_open_device_with_vid_pid(context_, entry.first, entry.second); + if (handle_) { + libusb_claim_interface(handle_, 0); break; } } - libusb_free_device_list(devs, 0); } void HardwarePortal::CloseDevice() { - if (!connected_) { + if (!handle_) { return; } libusb_release_interface(handle_, 0); libusb_close(handle_); - connected_ = false; handle_ = nullptr; } diff --git a/src/xenia/hid/portal/hardware_portal.h b/src/xenia/hid/portal/hardware_portal.h index 4f0c6db0c..9c8849706 100644 --- a/src/xenia/hid/portal/hardware_portal.h +++ b/src/xenia/hid/portal/hardware_portal.h @@ -28,6 +28,8 @@ class HardwarePortal final : public Portal { HardwarePortal(); ~HardwarePortal() override; + virtual bool IsConnected() override; + virtual void OnDeviceArrival() override; virtual void OnDeviceRemoval() override; diff --git a/src/xenia/hid/portal/portal.cc b/src/xenia/hid/portal/portal.cc index 429c4cf41..dd79ef686 100644 --- a/src/xenia/hid/portal/portal.cc +++ b/src/xenia/hid/portal/portal.cc @@ -15,16 +15,11 @@ namespace hid { Portal::Portal() {} Portal::~Portal() {} -bool Portal::IsConnected() { - std::lock_guard guard(lock_); - return connected_; -} - X_STATUS Portal::Read(std::span data, uint32_t& bytes_read, uint16_t& state) { std::lock_guard guard(lock_); - if (!connected_) { + if (!IsConnected()) { return X_ERROR_DEVICE_NOT_CONNECTED; } @@ -55,7 +50,7 @@ X_STATUS Portal::Read(std::span data, uint32_t& bytes_read, X_STATUS Portal::Write(std::span data) { std::lock_guard guard(lock_); - if (!connected_) { + if (!IsConnected()) { return X_ERROR_DEVICE_NOT_CONNECTED; } diff --git a/src/xenia/hid/portal/portal.h b/src/xenia/hid/portal/portal.h index baeace9d2..d2c8f2461 100644 --- a/src/xenia/hid/portal/portal.h +++ b/src/xenia/hid/portal/portal.h @@ -25,7 +25,7 @@ class Portal { Portal(); virtual ~Portal(); - bool IsConnected(); + virtual bool IsConnected() = 0; virtual X_STATUS Read(std::span data, uint32_t& bytes_read, uint16_t& state); @@ -34,9 +34,6 @@ class Portal { virtual void OnDeviceArrival() = 0; virtual void OnDeviceRemoval() = 0; - protected: - bool connected_ = false; - private: virtual void OpenDevice() = 0; virtual void CloseDevice() = 0;