Merge pull request #4839 from leoetlino/better-warnings

Be less annoying when usbdk is not installed
This commit is contained in:
JosJuice 2017-02-08 14:00:12 +01:00 committed by GitHub
commit 99492c22a6
8 changed files with 64 additions and 64 deletions

View File

@ -8,6 +8,7 @@
#include "Common/LibusbContext.h" #include "Common/LibusbContext.h"
#include "Common/MsgHandler.h" #include "Common/MsgHandler.h"
#include "Common/StringUtil.h"
namespace LibusbContext namespace LibusbContext
{ {
@ -24,10 +25,12 @@ static libusb_context* Create()
#ifdef _WIN32 #ifdef _WIN32
is_windows = true; is_windows = true;
#endif #endif
if (is_windows && ret == LIBUSB_ERROR_NOT_FOUND) const std::string reason =
PanicAlertT("Failed to initialize libusb because usbdk is not installed."); is_windows && ret == LIBUSB_ERROR_NOT_FOUND ?
else GetStringT("Failed to initialize libusb because usbdk is not installed.") :
PanicAlertT("Failed to initialize libusb: %s", libusb_error_name(ret)); StringFromFormat(GetStringT("Failed to initialize libusb (%s).").c_str(),
libusb_error_name(ret));
PanicAlertT("%s\nSome USB features will not work.", reason.c_str());
return nullptr; return nullptr;
} }
return context; return context;

View File

@ -61,8 +61,6 @@ BluetoothReal::BluetoothReal(u32 device_id, const std::string& device_name)
: BluetoothBase(device_id, device_name) : BluetoothBase(device_id, device_name)
{ {
m_libusb_context = LibusbContext::Get(); m_libusb_context = LibusbContext::Get();
_assert_msg_(IOS_WIIMOTE, m_libusb_context, "Failed to init libusb.");
LoadLinkKeys(); LoadLinkKeys();
} }
@ -84,6 +82,9 @@ BluetoothReal::~BluetoothReal()
ReturnCode BluetoothReal::Open(const OpenRequest& request) ReturnCode BluetoothReal::Open(const OpenRequest& request)
{ {
if (!m_libusb_context)
return IPC_EACCES;
libusb_device** list; libusb_device** list;
const ssize_t cnt = libusb_get_device_list(m_libusb_context.get(), &list); const ssize_t cnt = libusb_get_device_list(m_libusb_context.get(), &list);
_dbg_assert_msg_(IOS, cnt > 0, "Couldn't get device list"); _dbg_assert_msg_(IOS, cnt > 0, "Couldn't get device list");

View File

@ -30,10 +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();
_assert_msg_(IOS_USB, m_libusb_context, "Failed to init libusb.");
#endif
} }
ReturnCode USBHost::Open(const OpenRequest& request) ReturnCode USBHost::Open(const OpenRequest& request)
@ -117,11 +113,18 @@ 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 (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", libusb_error_name(static_cast<int>(count))); WARN_LOG(IOS_USB, "Failed to get device list: %s",
libusb_error_name(static_cast<int>(count)));
return false; return false;
} }
@ -130,7 +133,8 @@ bool USBHost::AddNewDevices(std::set<u64>& new_devices, DeviceChangeHooks& hooks
libusb_device* device = list[i]; libusb_device* device = list[i];
libusb_device_descriptor descriptor; libusb_device_descriptor descriptor;
libusb_get_device_descriptor(device, &descriptor); libusb_get_device_descriptor(device, &descriptor);
if (!SConfig::GetInstance().IsUSBDeviceWhitelisted({descriptor.idVendor, descriptor.idProduct})) if (!SConfig::GetInstance().IsUSBDeviceWhitelisted(
{descriptor.idVendor, descriptor.idProduct}))
continue; continue;
auto usb_device = std::make_unique<USB::LibusbDevice>(device, descriptor); auto usb_device = std::make_unique<USB::LibusbDevice>(device, descriptor);
@ -142,6 +146,7 @@ bool USBHost::AddNewDevices(std::set<u64>& new_devices, DeviceChangeHooks& hooks
hooks.emplace(GetDeviceById(id), ChangeEvent::Inserted); hooks.emplace(GetDeviceById(id), ChangeEvent::Inserted);
} }
libusb_free_device_list(list, 1); libusb_free_device_list(list, 1);
}
#endif #endif
return true; return true;
} }
@ -200,10 +205,24 @@ void USBHost::StartThreads()
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;

View File

@ -170,24 +170,18 @@ void Init()
s_libusb_driver_not_supported = false; s_libusb_driver_not_supported = false;
s_libusb_context = LibusbContext::Get();
if (!s_libusb_context)
{
s_libusb_driver_not_supported = true;
Shutdown();
}
else
{
if (UseAdapter()) if (UseAdapter())
StartScanThread(); StartScanThread();
} }
}
void StartScanThread() 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