Move libusb context initialization to on first use

This prevents libusb warnings from showing up even when the user is
not using Bluetooth or USB passthrough, or the Wii U GC adapter.
This commit is contained in:
Léo Lam 2017-02-06 23:04:14 +01:00
parent db7ee668ff
commit 6a0bf24e0b
4 changed files with 9 additions and 31 deletions

View File

@ -170,17 +170,8 @@ void Init()
s_libusb_driver_not_supported = false; s_libusb_driver_not_supported = false;
s_libusb_context = LibusbContext::Get(); if (UseAdapter())
if (!s_libusb_context) StartScanThread();
{
s_libusb_driver_not_supported = true;
Shutdown();
}
else
{
if (UseAdapter())
StartScanThread();
}
} }
void StartScanThread() void StartScanThread()
@ -188,6 +179,9 @@ void StartScanThread()
if (s_adapter_detect_thread_running.IsSet()) if (s_adapter_detect_thread_running.IsSet())
return; return;
s_libusb_context = LibusbContext::Get();
if (!s_libusb_context)
return;
s_adapter_detect_thread_running.Set(true); s_adapter_detect_thread_running.Set(true);
s_adapter_detect_thread = std::thread(ScanThreadFunc); s_adapter_detect_thread = std::thread(ScanThreadFunc);
} }
@ -334,7 +328,7 @@ void Shutdown()
{ {
StopScanThread(); StopScanThread();
#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102 #if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102
if (s_libusb_hotplug_enabled) if (s_libusb_context && s_libusb_hotplug_enabled)
libusb_hotplug_deregister_callback(s_libusb_context.get(), s_hotplug_handle); libusb_hotplug_deregister_callback(s_libusb_context.get(), s_hotplug_handle);
#endif #endif
Reset(); Reset();

View File

@ -30,7 +30,6 @@ void Init()
VideoBackendBase::PopulateList(); VideoBackendBase::PopulateList();
WiimoteReal::LoadSettings(); WiimoteReal::LoadSettings();
GCAdapter::Init(); GCAdapter::Init();
USBUtils::Init();
VideoBackendBase::ActivateBackend(SConfig::GetInstance().m_strVideoBackend); VideoBackendBase::ActivateBackend(SConfig::GetInstance().m_strVideoBackend);
SetEnableAlert(SConfig::GetInstance().bUsePanicHandlers); SetEnableAlert(SConfig::GetInstance().bUsePanicHandlers);
@ -42,7 +41,6 @@ void Shutdown()
WiimoteReal::Shutdown(); WiimoteReal::Shutdown();
VideoBackendBase::ClearList(); VideoBackendBase::ClearList();
SConfig::Shutdown(); SConfig::Shutdown();
USBUtils::Shutdown();
LogManager::Shutdown(); LogManager::Shutdown();
} }

View File

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <memory> #include <memory>
#include <mutex>
#ifdef __LIBUSB__ #ifdef __LIBUSB__
#include <libusb.h> #include <libusb.h>
@ -14,6 +15,7 @@
#include "UICommon/USBUtils.h" #include "UICommon/USBUtils.h"
#ifdef __LIBUSB__ #ifdef __LIBUSB__
static std::once_flag s_tried_libusb_init;
static std::shared_ptr<libusb_context> s_libusb_context; static std::shared_ptr<libusb_context> s_libusb_context;
#endif #endif
@ -36,25 +38,12 @@ static const std::map<std::pair<u16, u16>, std::string> s_wii_peripherals = {{
namespace USBUtils namespace USBUtils
{ {
void Init()
{
#ifdef __LIBUSB__
s_libusb_context = LibusbContext::Get();
#endif
}
void Shutdown()
{
#ifdef __LIBUSB__
s_libusb_context = nullptr;
#endif
}
std::map<std::pair<u16, u16>, std::string> GetInsertedDevices() std::map<std::pair<u16, u16>, std::string> GetInsertedDevices()
{ {
std::map<std::pair<u16, u16>, std::string> devices; std::map<std::pair<u16, u16>, std::string> devices;
#ifdef __LIBUSB__ #ifdef __LIBUSB__
std::call_once(s_tried_libusb_init, []() { s_libusb_context = LibusbContext::Get(); });
if (!s_libusb_context) if (!s_libusb_context)
return devices; return devices;

View File

@ -12,9 +12,6 @@
namespace USBUtils namespace USBUtils
{ {
void Init();
void Shutdown();
std::map<std::pair<u16, u16>, std::string> GetInsertedDevices(); std::map<std::pair<u16, u16>, std::string> GetInsertedDevices();
std::string GetDeviceName(std::pair<u16, u16> vid_pid); std::string GetDeviceName(std::pair<u16, u16> vid_pid);
} // namespace USBUtils } // namespace USBUtils