GCAdapter: Eliminate some global variables.

This commit is contained in:
Jordan Woyak 2022-10-22 03:50:38 -05:00
parent cbc6b15db3
commit 7418fd03bf

View File

@ -142,15 +142,6 @@ static Common::Event s_hotplug_event;
static std::function<void(void)> 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<LibusbUtils::Context> s_libusb_context;
static u8 s_endpoint_in = 0;
@ -346,24 +337,32 @@ 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__)
#if __FreeBSD__ >= 11
bool hotplug_enabled = true;
#else
bool hotplug_enabled = false;
#endif
if (s_libusb_hotplug_enabled)
#else
bool hotplug_enabled = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) != 0;
#endif
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));
}
@ -378,11 +377,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();
@ -676,12 +681,6 @@ static void AddGCAdapter(libusb_device* device)
void Shutdown()
{
StopScanThread();
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
#if LIBUSB_API_HAS_HOTPLUG
if (s_libusb_context->IsValid() && s_libusb_hotplug_enabled)
libusb_hotplug_deregister_callback(*s_libusb_context, s_hotplug_handle);
#endif
#endif
Reset();
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION