From 5ad8577cfaafce00df10dad5a1093935070158ba Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sat, 22 Oct 2022 03:50:38 -0500 Subject: [PATCH] GCAdapter: Eliminate some global variables. --- Source/Core/InputCommon/GCAdapter.cpp | 37 ++++++++++++--------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index e33023d3e4..70c0bee59c 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -149,15 +149,6 @@ static Common::Event s_hotplug_event; static std::function s_detect_callback; -#if defined(__FreeBSD__) && __FreeBSD__ >= 11 -static bool s_libusb_hotplug_enabled = true; -#else -static bool s_libusb_hotplug_enabled = false; -#endif -#if LIBUSB_API_HAS_HOTPLUG -static libusb_hotplug_callback_handle s_hotplug_handle; -#endif - static std::unique_ptr s_libusb_context; static u8 s_endpoint_in = 0; @@ -353,24 +344,28 @@ static void ScanThreadFunc() #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION #if LIBUSB_API_HAS_HOTPLUG -#ifndef __FreeBSD__ - s_libusb_hotplug_enabled = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) != 0; + +#if defined(__FreeBSD__) + bool hotplug_enabled = (__FreeBSD__ >= 11); +#else + bool hotplug_enabled = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) != 0; #endif - if (s_libusb_hotplug_enabled) + libusb_hotplug_callback_handle hotplug_handle = {}; + if (hotplug_enabled) { const int error = libusb_hotplug_register_callback( *s_libusb_context, (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); + nullptr, &hotplug_handle); if (error == LIBUSB_SUCCESS) { NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Using libUSB hotplug detection"); } else { - s_libusb_hotplug_enabled = false; + hotplug_enabled = false; ERROR_LOG_FMT(CONTROLLERINTERFACE, "Failed to add libUSB hotplug detection callback: {}", LibusbUtils::ErrorWrap(error)); } @@ -385,11 +380,17 @@ static void ScanThreadFunc() Setup(); } - if (s_libusb_hotplug_enabled) + if (hotplug_enabled) s_hotplug_event.Wait(); else Common::SleepCurrentThread(500); } + +#if LIBUSB_API_HAS_HOTPLUG + if (hotplug_enabled) + libusb_hotplug_deregister_callback(*s_libusb_context, hotplug_handle); +#endif + #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION JNIEnv* const env = IDCache::GetEnvForThread(); @@ -688,12 +689,6 @@ static void AddGCAdapter(libusb_device* device) void Shutdown() { StopScanThread(); -#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION -#if LIBUSB_API_HAS_HOTPLUG - if (s_libusb_context && s_libusb_context->IsValid() && s_libusb_hotplug_enabled) - libusb_hotplug_deregister_callback(*s_libusb_context, s_hotplug_handle); -#endif -#endif Reset(CalledFromReadThread::No); #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION