diff options
| -rw-r--r-- | src/input_common/gcadapter/gc_adapter.cpp | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index b39d2a3fb..6d9f4d9eb 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp | |||
| @@ -24,9 +24,13 @@ Adapter::Adapter() { | |||
| 24 | LOG_INFO(Input, "GC Adapter Initialization started"); | 24 | LOG_INFO(Input, "GC Adapter Initialization started"); |
| 25 | 25 | ||
| 26 | current_status = NO_ADAPTER_DETECTED; | 26 | current_status = NO_ADAPTER_DETECTED; |
| 27 | libusb_init(&libusb_ctx); | ||
| 28 | 27 | ||
| 29 | StartScanThread(); | 28 | const int init_res = libusb_init(&libusb_ctx); |
| 29 | if (init_res == LIBUSB_SUCCESS) { | ||
| 30 | StartScanThread(); | ||
| 31 | } else { | ||
| 32 | LOG_ERROR(Input, "libusb could not be initialized. failed with error = {}", init_res); | ||
| 33 | } | ||
| 30 | } | 34 | } |
| 31 | 35 | ||
| 32 | GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) { | 36 | GCPadStatus Adapter::GetPadStatus(int port, const std::array<u8, 37>& adapter_payload) { |
| @@ -211,17 +215,26 @@ void Adapter::Setup() { | |||
| 211 | adapter_controllers_status.fill(ControllerTypes::None); | 215 | adapter_controllers_status.fill(ControllerTypes::None); |
| 212 | 216 | ||
| 213 | // pointer to list of connected usb devices | 217 | // pointer to list of connected usb devices |
| 214 | libusb_device** devices; | 218 | libusb_device** devices{}; |
| 215 | 219 | ||
| 216 | // populate the list of devices, get the count | 220 | // populate the list of devices, get the count |
| 217 | const std::size_t device_count = libusb_get_device_list(libusb_ctx, &devices); | 221 | const ssize_t device_count = libusb_get_device_list(libusb_ctx, &devices); |
| 222 | if (device_count < 0) { | ||
| 223 | LOG_ERROR(Input, "libusb_get_device_list failed with error: {}", device_count); | ||
| 224 | detect_thread_running = false; // Stop the loop constantly checking for gc adapter | ||
| 225 | // TODO: For hotplug+gc adapter checkbox implementation, revert this. | ||
| 226 | return; | ||
| 227 | } | ||
| 218 | 228 | ||
| 219 | for (std::size_t index = 0; index < device_count; ++index) { | 229 | if (devices != nullptr) { |
| 220 | if (CheckDeviceAccess(devices[index])) { | 230 | for (std::size_t index = 0; index < device_count; ++index) { |
| 221 | // GC Adapter found and accessible, registering it | 231 | if (CheckDeviceAccess(devices[index])) { |
| 222 | GetGCEndpoint(devices[index]); | 232 | // GC Adapter found and accessible, registering it |
| 223 | break; | 233 | GetGCEndpoint(devices[index]); |
| 234 | break; | ||
| 235 | } | ||
| 224 | } | 236 | } |
| 237 | libusb_free_device_list(devices, 1); | ||
| 225 | } | 238 | } |
| 226 | } | 239 | } |
| 227 | 240 | ||
| @@ -279,7 +292,13 @@ bool Adapter::CheckDeviceAccess(libusb_device* device) { | |||
| 279 | 292 | ||
| 280 | void Adapter::GetGCEndpoint(libusb_device* device) { | 293 | void Adapter::GetGCEndpoint(libusb_device* device) { |
| 281 | libusb_config_descriptor* config = nullptr; | 294 | libusb_config_descriptor* config = nullptr; |
| 282 | libusb_get_config_descriptor(device, 0, &config); | 295 | const int config_descriptor_return = libusb_get_config_descriptor(device, 0, &config); |
| 296 | if (config_descriptor_return != LIBUSB_SUCCESS) { | ||
| 297 | LOG_ERROR(Input, "libusb_get_config_descriptor failed with error = {}", | ||
| 298 | config_descriptor_return); | ||
| 299 | return; | ||
| 300 | } | ||
| 301 | |||
| 283 | for (u8 ic = 0; ic < config->bNumInterfaces; ic++) { | 302 | for (u8 ic = 0; ic < config->bNumInterfaces; ic++) { |
| 284 | const libusb_interface* interfaceContainer = &config->interface[ic]; | 303 | const libusb_interface* interfaceContainer = &config->interface[ic]; |
| 285 | for (int i = 0; i < interfaceContainer->num_altsetting; i++) { | 304 | for (int i = 0; i < interfaceContainer->num_altsetting; i++) { |