From 73e7f2a839dd63d5b5fdf6b7289a307cda7acb36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sun, 15 Mar 2020 13:58:12 +0100 Subject: [PATCH] BTReal: Improve error handling for device opening This commit attempts to improve error handling for device opening by reducing panic alert spam when opening one or several devices fails. Currently, Dolphin shows a panic alert for every device that we fail to open, and another panic alert at the end if no usable device was found. That is certainly a bit excessive -- we should only keep the very last panic alert (the one that is shown if everything fails) and we can just put the error for the last device open operation there. This also changes the PanicAlert to a CriticalAlert to ensure the message is visible even if the user has disabled regular panic alerts. The message has also been reworded and should hopefully be clearer. --- Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp | 27 ++++++++++++++----- Source/Core/Core/IOS/USB/Bluetooth/BTReal.h | 2 ++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp b/Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp index 2247ba000e51..244b2d459151 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp +++ b/Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include "Common/ChunkFile.h" @@ -81,6 +82,7 @@ IPCCommandResult BluetoothReal::Open(const OpenRequest& request) if (!m_context.IsValid()) return GetDefaultReply(IPC_EACCES); + m_last_open_error.clear(); m_context.GetDeviceList([this](libusb_device* device) { libusb_device_descriptor device_descriptor; libusb_get_device_descriptor(device, &device_descriptor); @@ -115,8 +117,19 @@ IPCCommandResult BluetoothReal::Open(const OpenRequest& request) if (m_handle == nullptr) { - PanicAlertT("Bluetooth passthrough mode is enabled, " - "but no usable Bluetooth USB device was found. Aborting."); + if (m_last_open_error.empty()) + { + CriticalAlertT( + "Could not find any usable Bluetooth USB adapter for Bluetooth Passthrough.\n\n" + "The emulated console will now stop."); + } + else + { + CriticalAlertT("Could not find any usable Bluetooth USB adapter for Bluetooth Passthrough.\n" + "The following error occurred when Dolphin tried to use an adapter:\n%s\n\n" + "The emulated console will now stop.", + m_last_open_error.c_str()); + } Core::QueueHostJob(Core::Stop); return GetDefaultReply(IPC_ENOENT); } @@ -547,7 +560,8 @@ bool BluetoothReal::OpenDevice(libusb_device* device) const int ret = libusb_open(m_device, &m_handle); if (ret != 0) { - PanicAlertT("Failed to open Bluetooth device: %s", libusb_error_name(ret)); + m_last_open_error = fmt::format(Common::GetStringT("Failed to open Bluetooth device: {}"), + libusb_error_name(ret)); return false; } @@ -560,15 +574,16 @@ bool BluetoothReal::OpenDevice(libusb_device* device) result = libusb_detach_kernel_driver(m_handle, INTERFACE); if (result < 0 && result != LIBUSB_ERROR_NOT_FOUND && result != LIBUSB_ERROR_NOT_SUPPORTED) { - PanicAlertT("Failed to detach kernel driver for BT passthrough: %s", - libusb_error_name(result)); + m_last_open_error = + fmt::format(Common::GetStringT("Failed to detach kernel driver for BT passthrough: {}"), + libusb_error_name(result)); return false; } } #endif if (libusb_claim_interface(m_handle, INTERFACE) < 0) { - PanicAlertT("Failed to claim interface for BT passthrough"); + m_last_open_error = Common::GetStringT("Failed to claim interface for BT passthrough"); return false; } diff --git a/Source/Core/Core/IOS/USB/Bluetooth/BTReal.h b/Source/Core/Core/IOS/USB/Bluetooth/BTReal.h index a1986638ea0b..e3435c32d279 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/BTReal.h +++ b/Source/Core/Core/IOS/USB/Bluetooth/BTReal.h @@ -71,6 +71,8 @@ class BluetoothReal final : public BluetoothBase std::atomic m_sync_button_state{SyncButtonState::Unpressed}; Common::Timer m_sync_button_held_timer; + std::string m_last_open_error; + LibusbUtils::Context m_context; libusb_device* m_device = nullptr; libusb_device_handle* m_handle = nullptr;