From 247f5d823fb68be1f6ce33453585f1a5d4046482 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Fri, 31 Dec 2021 01:15:38 +0100 Subject: [PATCH] Config: Port USBPassthrough setting to new config system. --- Source/Core/Core/Config/MainSettings.cpp | 45 +++++++++++++++++++ Source/Core/Core/Config/MainSettings.h | 8 ++++ .../Core/ConfigLoaders/IsSettingSaveable.cpp | 2 +- Source/Core/Core/ConfigManager.cpp | 40 ----------------- Source/Core/Core/ConfigManager.h | 6 --- Source/Core/Core/IOS/USB/Host.cpp | 8 ++-- .../USBDeviceAddToWhitelistDialog.cpp | 12 +++-- Source/Core/DolphinQt/Settings/WiiPane.cpp | 7 ++- 8 files changed, 71 insertions(+), 57 deletions(-) diff --git a/Source/Core/Core/Config/MainSettings.cpp b/Source/Core/Core/Config/MainSettings.cpp index fdbe76d924..5bf163b834 100644 --- a/Source/Core/Core/Config/MainSettings.cpp +++ b/Source/Core/Core/Config/MainSettings.cpp @@ -3,10 +3,13 @@ #include "Core/Config/MainSettings.h" +#include + #include #include "AudioCommon/AudioCommon.h" #include "Common/Config/Config.h" +#include "Common/StringUtil.h" #include "Common/Version.h" #include "Core/Config/DefaultLocale.h" #include "Core/HW/EXI/EXI_Device.h" @@ -307,4 +310,46 @@ const Info MAIN_BLUETOOTH_PASSTHROUGH_PID{{System::Main, "BluetoothPassthro const Info MAIN_BLUETOOTH_PASSTHROUGH_LINK_KEYS{ {System::Main, "BluetoothPassthrough", "LinkKeys"}, ""}; +// Main.USBPassthrough + +const Info MAIN_USB_PASSTHROUGH_DEVICES{{System::Main, "USBPassthrough", "Devices"}, + ""}; + +static std::set> LoadUSBWhitelistFromString(const std::string& devices_string) +{ + std::set> devices; + for (const auto& pair : SplitString(devices_string, ',')) + { + const auto index = pair.find(':'); + if (index == std::string::npos) + continue; + + const u16 vid = static_cast(strtol(pair.substr(0, index).c_str(), nullptr, 16)); + const u16 pid = static_cast(strtol(pair.substr(index + 1).c_str(), nullptr, 16)); + if (vid && pid) + devices.emplace(vid, pid); + } + return devices; +} + +static std::string SaveUSBWhitelistToString(const std::set>& devices) +{ + std::ostringstream oss; + for (const auto& device : devices) + oss << fmt::format("{:04x}:{:04x}", device.first, device.second) << ','; + std::string devices_string = oss.str(); + if (!devices_string.empty()) + devices_string.pop_back(); + return devices_string; +} + +std::set> GetUSBDeviceWhitelist() +{ + return LoadUSBWhitelistFromString(Config::Get(Config::MAIN_USB_PASSTHROUGH_DEVICES)); +} + +void SetUSBDeviceWhitelist(const std::set>& devices) +{ + Config::SetBase(Config::MAIN_USB_PASSTHROUGH_DEVICES, SaveUSBWhitelistToString(devices)); +} } // namespace Config diff --git a/Source/Core/Core/Config/MainSettings.h b/Source/Core/Core/Config/MainSettings.h index 87025dfc77..8b88399cc6 100644 --- a/Source/Core/Core/Config/MainSettings.h +++ b/Source/Core/Core/Config/MainSettings.h @@ -4,7 +4,9 @@ #pragma once #include +#include #include +#include #include "Common/Common.h" #include "Common/Config/Config.h" @@ -257,4 +259,10 @@ extern const Info MAIN_BLUETOOTH_PASSTHROUGH_VID; extern const Info MAIN_BLUETOOTH_PASSTHROUGH_PID; extern const Info MAIN_BLUETOOTH_PASSTHROUGH_LINK_KEYS; +// Main.USBPassthrough + +extern const Info MAIN_USB_PASSTHROUGH_DEVICES; +std::set> GetUSBDeviceWhitelist(); +void SetUSBDeviceWhitelist(const std::set>& devices); + } // namespace Config diff --git a/Source/Core/Core/ConfigLoaders/IsSettingSaveable.cpp b/Source/Core/Core/ConfigLoaders/IsSettingSaveable.cpp index 1ea385fddc..0dc22b9b91 100644 --- a/Source/Core/Core/ConfigLoaders/IsSettingSaveable.cpp +++ b/Source/Core/Core/ConfigLoaders/IsSettingSaveable.cpp @@ -28,7 +28,7 @@ bool IsSettingSaveable(const Config::Location& config_location) for (const std::string_view section : {"NetPlay", "General", "GBA", "Display", "Network", "Analytics", "AndroidOverlayButtons", "DSP", "GameList", "FifoPlayer", "AutoUpdate", "Movie", "Input", "Debug", - "BluetoothPassthrough"}) + "BluetoothPassthrough", "USBPassthrough"}) { if (config_location.section == section) return true; diff --git a/Source/Core/Core/ConfigManager.cpp b/Source/Core/Core/ConfigManager.cpp index 2d44656ae6..0e2ce2ad3c 100644 --- a/Source/Core/Core/ConfigManager.cpp +++ b/Source/Core/Core/ConfigManager.cpp @@ -91,7 +91,6 @@ void SConfig::SaveSettings() SaveGeneralSettings(ini); SaveInterfaceSettings(ini); SaveCoreSettings(ini); - SaveUSBPassthroughSettings(ini); ini.Save(File::GetUserPath(F_DOLPHINCONFIG_IDX)); @@ -195,20 +194,6 @@ void SConfig::SaveCoreSettings(IniFile& ini) core->Set("CustomRTCValue", m_customRTCValue); } -void SConfig::SaveUSBPassthroughSettings(IniFile& ini) -{ - IniFile::Section* section = ini.GetOrCreateSection("USBPassthrough"); - - std::ostringstream oss; - for (const auto& device : m_usb_passthrough_devices) - oss << fmt::format("{:04x}:{:04x}", device.first, device.second) << ','; - std::string devices_string = oss.str(); - if (!devices_string.empty()) - devices_string.pop_back(); - - section->Set("Devices", devices_string); -} - void SConfig::LoadSettings() { Config::Load(); @@ -220,7 +205,6 @@ void SConfig::LoadSettings() LoadGeneralSettings(ini); LoadInterfaceSettings(ini); LoadCoreSettings(ini); - LoadUSBPassthroughSettings(ini); } void SConfig::LoadGeneralSettings(IniFile& ini) @@ -329,25 +313,6 @@ void SConfig::LoadCoreSettings(IniFile& ini) core->Get("CustomRTCValue", &m_customRTCValue, 946684800); } -void SConfig::LoadUSBPassthroughSettings(IniFile& ini) -{ - IniFile::Section* section = ini.GetOrCreateSection("USBPassthrough"); - m_usb_passthrough_devices.clear(); - std::string devices_string; - section->Get("Devices", &devices_string, ""); - for (const auto& pair : SplitString(devices_string, ',')) - { - const auto index = pair.find(':'); - if (index == std::string::npos) - continue; - - const u16 vid = static_cast(strtol(pair.substr(0, index).c_str(), nullptr, 16)); - const u16 pid = static_cast(strtol(pair.substr(index + 1).c_str(), nullptr, 16)); - if (vid && pid) - m_usb_passthrough_devices.emplace(vid, pid); - } -} - void SConfig::ResetRunningGameMetadata() { SetRunningGameMetadata("00000000", "", 0, 0, DiscIO::Region::Unknown); @@ -490,11 +455,6 @@ void SConfig::LoadDefaults() ResetRunningGameMetadata(); } -bool SConfig::IsUSBDeviceWhitelisted(const std::pair vid_pid) const -{ - return m_usb_passthrough_devices.find(vid_pid) != m_usb_passthrough_devices.end(); -} - // Static method to make a simple game ID for elf/dol files std::string SConfig::MakeGameID(std::string_view file_name) { diff --git a/Source/Core/Core/ConfigManager.h b/Source/Core/Core/ConfigManager.h index e206dba83e..843aa5e103 100644 --- a/Source/Core/Core/ConfigManager.h +++ b/Source/Core/Core/ConfigManager.h @@ -128,10 +128,6 @@ struct SConfig bool bLockCursor = false; std::string theme_name; - // USB passthrough settings - std::set> m_usb_passthrough_devices; - bool IsUSBDeviceWhitelisted(std::pair vid_pid) const; - // Custom RTC bool bEnableCustomRTC; u32 m_customRTCValue; @@ -237,12 +233,10 @@ private: void SaveGeneralSettings(IniFile& ini); void SaveInterfaceSettings(IniFile& ini); void SaveCoreSettings(IniFile& ini); - void SaveUSBPassthroughSettings(IniFile& ini); void LoadGeneralSettings(IniFile& ini); void LoadInterfaceSettings(IniFile& ini); void LoadCoreSettings(IniFile& ini); - void LoadUSBPassthroughSettings(IniFile& ini); void SetRunningGameMetadata(const std::string& game_id, const std::string& gametdb_id, u64 title_id, u16 revision, DiscIO::Region region); diff --git a/Source/Core/Core/IOS/USB/Host.cpp b/Source/Core/Core/IOS/USB/Host.cpp index 7968d27dfe..42442ddea5 100644 --- a/Source/Core/Core/IOS/USB/Host.cpp +++ b/Source/Core/Core/IOS/USB/Host.cpp @@ -19,7 +19,7 @@ #include "Common/CommonTypes.h" #include "Common/Logging/Log.h" #include "Common/Thread.h" -#include "Core/ConfigManager.h" +#include "Core/Config/MainSettings.h" #include "Core/Core.h" #include "Core/IOS/USB/Common.h" #include "Core/IOS/USB/LibusbDevice.h" @@ -115,7 +115,8 @@ bool USBHost::AddNewDevices(std::set& new_devices, DeviceChangeHooks& hooks const bool always_add_hooks) { #ifdef __LIBUSB__ - if (SConfig::GetInstance().m_usb_passthrough_devices.empty()) + auto whitelist = Config::GetUSBDeviceWhitelist(); + if (whitelist.empty()) return true; if (m_context.IsValid()) @@ -123,8 +124,7 @@ bool USBHost::AddNewDevices(std::set& new_devices, DeviceChangeHooks& hooks m_context.GetDeviceList([&](libusb_device* device) { libusb_device_descriptor descriptor; libusb_get_device_descriptor(device, &descriptor); - const std::pair vid_pid = {descriptor.idVendor, descriptor.idProduct}; - if (!SConfig::GetInstance().IsUSBDeviceWhitelisted(vid_pid)) + if (whitelist.count({descriptor.idVendor, descriptor.idProduct}) == 0) return true; auto usb_device = std::make_unique(m_ios, device, descriptor); diff --git a/Source/Core/DolphinQt/Settings/USBDeviceAddToWhitelistDialog.cpp b/Source/Core/DolphinQt/Settings/USBDeviceAddToWhitelistDialog.cpp index 3fab4f4834..d2272d03b0 100644 --- a/Source/Core/DolphinQt/Settings/USBDeviceAddToWhitelistDialog.cpp +++ b/Source/Core/DolphinQt/Settings/USBDeviceAddToWhitelistDialog.cpp @@ -19,6 +19,7 @@ #include "Common/StringUtil.h" +#include "Core/Config/MainSettings.h" #include "Core/ConfigManager.h" #include "DolphinQt/QtUtils/ModalMessageBox.h" @@ -109,9 +110,10 @@ void USBDeviceAddToWhitelistDialog::RefreshDeviceList() return; const auto selection_string = usb_inserted_devices_list->currentItem(); usb_inserted_devices_list->clear(); + auto whitelist = Config::GetUSBDeviceWhitelist(); for (const auto& device : current_devices) { - if (SConfig::GetInstance().IsUSBDeviceWhitelisted(device.first)) + if (whitelist.count({device.first.first, device.first.second}) != 0) continue; usb_inserted_devices_list->addItem(QString::fromStdString(device.second)); } @@ -141,14 +143,16 @@ void USBDeviceAddToWhitelistDialog::AddUSBDeviceToWhitelist() const u16 vid = static_cast(std::stoul(vid_string, nullptr, 16)); const u16 pid = static_cast(std::stoul(pid_string, nullptr, 16)); - if (SConfig::GetInstance().IsUSBDeviceWhitelisted({vid, pid})) + auto whitelist = Config::GetUSBDeviceWhitelist(); + auto it = whitelist.emplace(vid, pid); + if (!it.second) { ModalMessageBox::critical(this, tr("USB Whitelist Error"), tr("This USB device is already whitelisted.")); return; } - SConfig::GetInstance().m_usb_passthrough_devices.emplace(vid, pid); - SConfig::GetInstance().SaveSettings(); + Config::SetUSBDeviceWhitelist(whitelist); + Config::Save(); accept(); } diff --git a/Source/Core/DolphinQt/Settings/WiiPane.cpp b/Source/Core/DolphinQt/Settings/WiiPane.cpp index cdb094d01b..168091abf6 100644 --- a/Source/Core/DolphinQt/Settings/WiiPane.cpp +++ b/Source/Core/DolphinQt/Settings/WiiPane.cpp @@ -283,14 +283,17 @@ void WiiPane::OnUSBWhitelistRemoveButton() QString pid = QString(split[1]); const u16 vid_u16 = static_cast(std::stoul(vid.toStdString(), nullptr, 16)); const u16 pid_u16 = static_cast(std::stoul(pid.toStdString(), nullptr, 16)); - SConfig::GetInstance().m_usb_passthrough_devices.erase({vid_u16, pid_u16}); + auto whitelist = Config::GetUSBDeviceWhitelist(); + whitelist.erase({vid_u16, pid_u16}); + Config::SetUSBDeviceWhitelist(whitelist); PopulateUSBPassthroughListWidget(); } void WiiPane::PopulateUSBPassthroughListWidget() { m_whitelist_usb_list->clear(); - for (const auto& device : SConfig::GetInstance().m_usb_passthrough_devices) + auto whitelist = Config::GetUSBDeviceWhitelist(); + for (const auto& device : whitelist) { QListWidgetItem* usb_lwi = new QListWidgetItem(QString::fromStdString(USBUtils::GetDeviceName(device)));