Merge pull request #4839 from leoetlino/better-warnings
Be less annoying when usbdk is not installed
This commit is contained in:
commit
99492c22a6
|
@ -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;
|
||||||
|
|
|
@ -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");
|
||||||
|
|
|
@ -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,31 +113,40 @@ bool USBHost::AddNewDevices(std::set<u64>& new_devices, DeviceChangeHooks& hooks
|
||||||
const bool always_add_hooks)
|
const bool always_add_hooks)
|
||||||
{
|
{
|
||||||
#ifdef __LIBUSB__
|
#ifdef __LIBUSB__
|
||||||
libusb_device** list;
|
if (SConfig::GetInstance().m_usb_passthrough_devices.empty())
|
||||||
const ssize_t count = libusb_get_device_list(m_libusb_context.get(), &list);
|
return true;
|
||||||
if (count < 0)
|
|
||||||
{
|
|
||||||
WARN_LOG(IOS_USB, "Failed to get device list: %s", libusb_error_name(static_cast<int>(count)));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (ssize_t i = 0; i < count; ++i)
|
auto libusb_context = LibusbContext::Get();
|
||||||
|
if (libusb_context)
|
||||||
{
|
{
|
||||||
libusb_device* device = list[i];
|
libusb_device** list;
|
||||||
libusb_device_descriptor descriptor;
|
const ssize_t count = libusb_get_device_list(libusb_context.get(), &list);
|
||||||
libusb_get_device_descriptor(device, &descriptor);
|
if (count < 0)
|
||||||
if (!SConfig::GetInstance().IsUSBDeviceWhitelisted({descriptor.idVendor, descriptor.idProduct}))
|
{
|
||||||
continue;
|
WARN_LOG(IOS_USB, "Failed to get device list: %s",
|
||||||
|
libusb_error_name(static_cast<int>(count)));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
auto usb_device = std::make_unique<USB::LibusbDevice>(device, descriptor);
|
for (ssize_t i = 0; i < count; ++i)
|
||||||
if (!ShouldAddDevice(*usb_device))
|
{
|
||||||
continue;
|
libusb_device* device = list[i];
|
||||||
const u64 id = usb_device->GetId();
|
libusb_device_descriptor descriptor;
|
||||||
new_devices.insert(id);
|
libusb_get_device_descriptor(device, &descriptor);
|
||||||
if (AddDevice(std::move(usb_device)) || always_add_hooks)
|
if (!SConfig::GetInstance().IsUSBDeviceWhitelisted(
|
||||||
hooks.emplace(GetDeviceById(id), ChangeEvent::Inserted);
|
{descriptor.idVendor, descriptor.idProduct}))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
auto usb_device = std::make_unique<USB::LibusbDevice>(device, descriptor);
|
||||||
|
if (!ShouldAddDevice(*usb_device))
|
||||||
|
continue;
|
||||||
|
const u64 id = usb_device->GetId();
|
||||||
|
new_devices.insert(id);
|
||||||
|
if (AddDevice(std::move(usb_device)) || always_add_hooks)
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue