From b6d526ba2e94e1bcb155c7d6b2eee7833e3ae81b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Mon, 6 Feb 2017 23:42:29 +0100 Subject: [PATCH] IOS: Do not init libusb unless passthrough is enabled --- Source/Core/Core/IOS/USB/Host.cpp | 29 ++++++++++++++++++++++------- Source/Core/Core/IOS/USB/Host.h | 1 - 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/Source/Core/Core/IOS/USB/Host.cpp b/Source/Core/Core/IOS/USB/Host.cpp index 80c19e34fa..226c107eb0 100644 --- a/Source/Core/Core/IOS/USB/Host.cpp +++ b/Source/Core/Core/IOS/USB/Host.cpp @@ -30,9 +30,6 @@ namespace Device { USBHost::USBHost(u32 device_id, const std::string& device_name) : Device(device_id, device_name) { -#ifdef __LIBUSB__ - m_libusb_context = LibusbContext::Get(); -#endif } ReturnCode USBHost::Open(const OpenRequest& request) @@ -116,10 +113,14 @@ bool USBHost::AddNewDevices(std::set& new_devices, DeviceChangeHooks& hooks const bool always_add_hooks) { #ifdef __LIBUSB__ - if (m_libusb_context) + if (SConfig::GetInstance().m_usb_passthrough_devices.empty()) + return true; + + auto libusb_context = LibusbContext::Get(); + if (libusb_context) { libusb_device** list; - const ssize_t count = libusb_get_device_list(m_libusb_context.get(), &list); + const ssize_t count = libusb_get_device_list(libusb_context.get(), &list); if (count < 0) { WARN_LOG(IOS_USB, "Failed to get device list: %s", @@ -199,15 +200,29 @@ void USBHost::StartThreads() } #ifdef __LIBUSB__ - if (m_libusb_context && !m_event_thread_running.IsSet()) + if (!m_event_thread_running.IsSet()) { m_event_thread_running.Set(); m_event_thread = std::thread([this] { Common::SetCurrentThreadName("USB Passthrough Thread"); + std::shared_ptr context; while (m_event_thread_running.IsSet()) { + if (SConfig::GetInstance().m_usb_passthrough_devices.empty()) + { + Common::SleepCurrentThread(50); + continue; + } + + if (!context) + context = LibusbContext::Get(); + + // If we failed to get a libusb context, stop the thread. + if (!context) + return; + static timeval tv = {0, 50000}; - libusb_handle_events_timeout_completed(m_libusb_context.get(), &tv, nullptr); + libusb_handle_events_timeout_completed(context.get(), &tv, nullptr); } }); } diff --git a/Source/Core/Core/IOS/USB/Host.h b/Source/Core/Core/IOS/USB/Host.h index 40c09ca709..27d94d805f 100644 --- a/Source/Core/Core/IOS/USB/Host.h +++ b/Source/Core/Core/IOS/USB/Host.h @@ -71,7 +71,6 @@ private: void DispatchHooks(const DeviceChangeHooks& hooks); #ifdef __LIBUSB__ - std::shared_ptr m_libusb_context; // Event thread for libusb Common::Flag m_event_thread_running; std::thread m_event_thread;