IOS: Do not init libusb unless passthrough is enabled

This commit is contained in:
Léo Lam 2017-02-06 23:42:29 +01:00
parent 6a0bf24e0b
commit b6d526ba2e
2 changed files with 22 additions and 8 deletions

View File

@ -30,9 +30,6 @@ namespace Device
{ {
USBHost::USBHost(u32 device_id, const std::string& device_name) : Device(device_id, device_name) 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) ReturnCode USBHost::Open(const OpenRequest& request)
@ -116,10 +113,14 @@ bool USBHost::AddNewDevices(std::set<u64>& new_devices, DeviceChangeHooks& hooks
const bool always_add_hooks) const bool always_add_hooks)
{ {
#ifdef __LIBUSB__ #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; 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) if (count < 0)
{ {
WARN_LOG(IOS_USB, "Failed to get device list: %s", WARN_LOG(IOS_USB, "Failed to get device list: %s",
@ -199,15 +200,29 @@ void USBHost::StartThreads()
} }
#ifdef __LIBUSB__ #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_running.Set();
m_event_thread = std::thread([this] { m_event_thread = std::thread([this] {
Common::SetCurrentThreadName("USB Passthrough Thread"); Common::SetCurrentThreadName("USB Passthrough Thread");
std::shared_ptr<libusb_context> context;
while (m_event_thread_running.IsSet()) 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}; 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);
} }
}); });
} }

View File

@ -71,7 +71,6 @@ private:
void DispatchHooks(const DeviceChangeHooks& hooks); void DispatchHooks(const DeviceChangeHooks& hooks);
#ifdef __LIBUSB__ #ifdef __LIBUSB__
std::shared_ptr<libusb_context> m_libusb_context;
// Event thread for libusb // Event thread for libusb
Common::Flag m_event_thread_running; Common::Flag m_event_thread_running;
std::thread m_event_thread; std::thread m_event_thread;