From c8a6dc6c23a5efe99b4bf937003ba3f2f7f8f200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sun, 15 Jan 2017 20:50:26 +0100 Subject: Use a single libusb context libusb on Windows is limited to only a single context. Trying to open more than one can cause device enumerations to fail randomly. libusb is thread-safe and we don't use the manual polling support (with `poll()`) so this should be safe. --- Source/Core/InputCommon/GCAdapter.cpp | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'Source/Core/InputCommon/GCAdapter.cpp') diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index e3dcb44739..fae7a81055 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -4,9 +4,11 @@ #include #include +#include #include #include "Common/Flag.h" +#include "Common/LibusbContext.h" #include "Common/Logging/Log.h" #include "Common/Thread.h" #include "Core/ConfigManager.h" @@ -50,7 +52,7 @@ static Common::Flag s_adapter_detect_thread_running; static std::function s_detect_callback; static bool s_libusb_driver_not_supported = false; -static libusb_context* s_libusb_context = nullptr; +static std::shared_ptr s_libusb_context; #if defined(__FreeBSD__) && __FreeBSD__ >= 11 static bool s_libusb_hotplug_enabled = true; #else @@ -116,8 +118,8 @@ static void ScanThreadFunc() if (s_libusb_hotplug_enabled) { if (libusb_hotplug_register_callback( - s_libusb_context, (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | - LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT), + s_libusb_context.get(), (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | + LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT), LIBUSB_HOTPLUG_ENUMERATE, 0x057e, 0x0337, LIBUSB_HOTPLUG_MATCH_ANY, HotplugCallback, nullptr, &s_hotplug_handle) != LIBUSB_SUCCESS) s_libusb_hotplug_enabled = false; @@ -131,7 +133,7 @@ static void ScanThreadFunc() if (s_libusb_hotplug_enabled) { static timeval tv = {0, 500000}; - libusb_handle_events_timeout(s_libusb_context, &tv); + libusb_handle_events_timeout(s_libusb_context.get(), &tv); } else { @@ -168,11 +170,9 @@ void Init() s_libusb_driver_not_supported = false; - int ret = libusb_init(&s_libusb_context); - - if (ret) + s_libusb_context = LibusbContext::Get(); + if (!s_libusb_context) { - ERROR_LOG(SERIALINTERFACE, "libusb_init failed with error: %d", ret); s_libusb_driver_not_supported = true; Shutdown(); } @@ -203,7 +203,7 @@ void StopScanThread() static void Setup() { libusb_device** list; - ssize_t cnt = libusb_get_device_list(s_libusb_context, &list); + ssize_t cnt = libusb_get_device_list(s_libusb_context.get(), &list); for (int i = 0; i < MAX_SI_CHANNELS; i++) { @@ -335,16 +335,11 @@ void Shutdown() StopScanThread(); #if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102 if (s_libusb_hotplug_enabled) - libusb_hotplug_deregister_callback(s_libusb_context, s_hotplug_handle); + libusb_hotplug_deregister_callback(s_libusb_context.get(), s_hotplug_handle); #endif Reset(); - if (s_libusb_context) - { - libusb_exit(s_libusb_context); - s_libusb_context = nullptr; - } - + s_libusb_context.reset(); s_libusb_driver_not_supported = false; } -- cgit v1.2.3