Config: Port USBPassthrough setting to new config system.

This commit is contained in:
Admiral H. Curtiss 2021-12-31 01:15:38 +01:00
parent 1af0c5f89f
commit 247f5d823f
No known key found for this signature in database
GPG Key ID: F051B4C4044F33FB
8 changed files with 71 additions and 57 deletions

View File

@ -3,10 +3,13 @@
#include "Core/Config/MainSettings.h" #include "Core/Config/MainSettings.h"
#include <sstream>
#include <fmt/format.h> #include <fmt/format.h>
#include "AudioCommon/AudioCommon.h" #include "AudioCommon/AudioCommon.h"
#include "Common/Config/Config.h" #include "Common/Config/Config.h"
#include "Common/StringUtil.h"
#include "Common/Version.h" #include "Common/Version.h"
#include "Core/Config/DefaultLocale.h" #include "Core/Config/DefaultLocale.h"
#include "Core/HW/EXI/EXI_Device.h" #include "Core/HW/EXI/EXI_Device.h"
@ -307,4 +310,46 @@ const Info<int> MAIN_BLUETOOTH_PASSTHROUGH_PID{{System::Main, "BluetoothPassthro
const Info<std::string> MAIN_BLUETOOTH_PASSTHROUGH_LINK_KEYS{ const Info<std::string> MAIN_BLUETOOTH_PASSTHROUGH_LINK_KEYS{
{System::Main, "BluetoothPassthrough", "LinkKeys"}, ""}; {System::Main, "BluetoothPassthrough", "LinkKeys"}, ""};
// Main.USBPassthrough
const Info<std::string> MAIN_USB_PASSTHROUGH_DEVICES{{System::Main, "USBPassthrough", "Devices"},
""};
static std::set<std::pair<u16, u16>> LoadUSBWhitelistFromString(const std::string& devices_string)
{
std::set<std::pair<u16, u16>> devices;
for (const auto& pair : SplitString(devices_string, ','))
{
const auto index = pair.find(':');
if (index == std::string::npos)
continue;
const u16 vid = static_cast<u16>(strtol(pair.substr(0, index).c_str(), nullptr, 16));
const u16 pid = static_cast<u16>(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<std::pair<u16, u16>>& 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<std::pair<u16, u16>> GetUSBDeviceWhitelist()
{
return LoadUSBWhitelistFromString(Config::Get(Config::MAIN_USB_PASSTHROUGH_DEVICES));
}
void SetUSBDeviceWhitelist(const std::set<std::pair<u16, u16>>& devices)
{
Config::SetBase(Config::MAIN_USB_PASSTHROUGH_DEVICES, SaveUSBWhitelistToString(devices));
}
} // namespace Config } // namespace Config

View File

@ -4,7 +4,9 @@
#pragma once #pragma once
#include <array> #include <array>
#include <set>
#include <string> #include <string>
#include <utility>
#include "Common/Common.h" #include "Common/Common.h"
#include "Common/Config/Config.h" #include "Common/Config/Config.h"
@ -257,4 +259,10 @@ extern const Info<int> MAIN_BLUETOOTH_PASSTHROUGH_VID;
extern const Info<int> MAIN_BLUETOOTH_PASSTHROUGH_PID; extern const Info<int> MAIN_BLUETOOTH_PASSTHROUGH_PID;
extern const Info<std::string> MAIN_BLUETOOTH_PASSTHROUGH_LINK_KEYS; extern const Info<std::string> MAIN_BLUETOOTH_PASSTHROUGH_LINK_KEYS;
// Main.USBPassthrough
extern const Info<std::string> MAIN_USB_PASSTHROUGH_DEVICES;
std::set<std::pair<u16, u16>> GetUSBDeviceWhitelist();
void SetUSBDeviceWhitelist(const std::set<std::pair<u16, u16>>& devices);
} // namespace Config } // namespace Config

View File

@ -28,7 +28,7 @@ bool IsSettingSaveable(const Config::Location& config_location)
for (const std::string_view section : for (const std::string_view section :
{"NetPlay", "General", "GBA", "Display", "Network", "Analytics", "AndroidOverlayButtons", {"NetPlay", "General", "GBA", "Display", "Network", "Analytics", "AndroidOverlayButtons",
"DSP", "GameList", "FifoPlayer", "AutoUpdate", "Movie", "Input", "Debug", "DSP", "GameList", "FifoPlayer", "AutoUpdate", "Movie", "Input", "Debug",
"BluetoothPassthrough"}) "BluetoothPassthrough", "USBPassthrough"})
{ {
if (config_location.section == section) if (config_location.section == section)
return true; return true;

View File

@ -91,7 +91,6 @@ void SConfig::SaveSettings()
SaveGeneralSettings(ini); SaveGeneralSettings(ini);
SaveInterfaceSettings(ini); SaveInterfaceSettings(ini);
SaveCoreSettings(ini); SaveCoreSettings(ini);
SaveUSBPassthroughSettings(ini);
ini.Save(File::GetUserPath(F_DOLPHINCONFIG_IDX)); ini.Save(File::GetUserPath(F_DOLPHINCONFIG_IDX));
@ -195,20 +194,6 @@ void SConfig::SaveCoreSettings(IniFile& ini)
core->Set("CustomRTCValue", m_customRTCValue); 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() void SConfig::LoadSettings()
{ {
Config::Load(); Config::Load();
@ -220,7 +205,6 @@ void SConfig::LoadSettings()
LoadGeneralSettings(ini); LoadGeneralSettings(ini);
LoadInterfaceSettings(ini); LoadInterfaceSettings(ini);
LoadCoreSettings(ini); LoadCoreSettings(ini);
LoadUSBPassthroughSettings(ini);
} }
void SConfig::LoadGeneralSettings(IniFile& ini) void SConfig::LoadGeneralSettings(IniFile& ini)
@ -329,25 +313,6 @@ void SConfig::LoadCoreSettings(IniFile& ini)
core->Get("CustomRTCValue", &m_customRTCValue, 946684800); 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<u16>(strtol(pair.substr(0, index).c_str(), nullptr, 16));
const u16 pid = static_cast<u16>(strtol(pair.substr(index + 1).c_str(), nullptr, 16));
if (vid && pid)
m_usb_passthrough_devices.emplace(vid, pid);
}
}
void SConfig::ResetRunningGameMetadata() void SConfig::ResetRunningGameMetadata()
{ {
SetRunningGameMetadata("00000000", "", 0, 0, DiscIO::Region::Unknown); SetRunningGameMetadata("00000000", "", 0, 0, DiscIO::Region::Unknown);
@ -490,11 +455,6 @@ void SConfig::LoadDefaults()
ResetRunningGameMetadata(); ResetRunningGameMetadata();
} }
bool SConfig::IsUSBDeviceWhitelisted(const std::pair<u16, u16> 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 // Static method to make a simple game ID for elf/dol files
std::string SConfig::MakeGameID(std::string_view file_name) std::string SConfig::MakeGameID(std::string_view file_name)
{ {

View File

@ -128,10 +128,6 @@ struct SConfig
bool bLockCursor = false; bool bLockCursor = false;
std::string theme_name; std::string theme_name;
// USB passthrough settings
std::set<std::pair<u16, u16>> m_usb_passthrough_devices;
bool IsUSBDeviceWhitelisted(std::pair<u16, u16> vid_pid) const;
// Custom RTC // Custom RTC
bool bEnableCustomRTC; bool bEnableCustomRTC;
u32 m_customRTCValue; u32 m_customRTCValue;
@ -237,12 +233,10 @@ private:
void SaveGeneralSettings(IniFile& ini); void SaveGeneralSettings(IniFile& ini);
void SaveInterfaceSettings(IniFile& ini); void SaveInterfaceSettings(IniFile& ini);
void SaveCoreSettings(IniFile& ini); void SaveCoreSettings(IniFile& ini);
void SaveUSBPassthroughSettings(IniFile& ini);
void LoadGeneralSettings(IniFile& ini); void LoadGeneralSettings(IniFile& ini);
void LoadInterfaceSettings(IniFile& ini); void LoadInterfaceSettings(IniFile& ini);
void LoadCoreSettings(IniFile& ini); void LoadCoreSettings(IniFile& ini);
void LoadUSBPassthroughSettings(IniFile& ini);
void SetRunningGameMetadata(const std::string& game_id, const std::string& gametdb_id, void SetRunningGameMetadata(const std::string& game_id, const std::string& gametdb_id,
u64 title_id, u16 revision, DiscIO::Region region); u64 title_id, u16 revision, DiscIO::Region region);

View File

@ -19,7 +19,7 @@
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/Thread.h" #include "Common/Thread.h"
#include "Core/ConfigManager.h" #include "Core/Config/MainSettings.h"
#include "Core/Core.h" #include "Core/Core.h"
#include "Core/IOS/USB/Common.h" #include "Core/IOS/USB/Common.h"
#include "Core/IOS/USB/LibusbDevice.h" #include "Core/IOS/USB/LibusbDevice.h"
@ -115,7 +115,8 @@ 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()) auto whitelist = Config::GetUSBDeviceWhitelist();
if (whitelist.empty())
return true; return true;
if (m_context.IsValid()) if (m_context.IsValid())
@ -123,8 +124,7 @@ bool USBHost::AddNewDevices(std::set<u64>& new_devices, DeviceChangeHooks& hooks
m_context.GetDeviceList([&](libusb_device* device) { m_context.GetDeviceList([&](libusb_device* device) {
libusb_device_descriptor descriptor; libusb_device_descriptor descriptor;
libusb_get_device_descriptor(device, &descriptor); libusb_get_device_descriptor(device, &descriptor);
const std::pair<u16, u16> vid_pid = {descriptor.idVendor, descriptor.idProduct}; if (whitelist.count({descriptor.idVendor, descriptor.idProduct}) == 0)
if (!SConfig::GetInstance().IsUSBDeviceWhitelisted(vid_pid))
return true; return true;
auto usb_device = std::make_unique<USB::LibusbDevice>(m_ios, device, descriptor); auto usb_device = std::make_unique<USB::LibusbDevice>(m_ios, device, descriptor);

View File

@ -19,6 +19,7 @@
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
#include "Core/Config/MainSettings.h"
#include "Core/ConfigManager.h" #include "Core/ConfigManager.h"
#include "DolphinQt/QtUtils/ModalMessageBox.h" #include "DolphinQt/QtUtils/ModalMessageBox.h"
@ -109,9 +110,10 @@ void USBDeviceAddToWhitelistDialog::RefreshDeviceList()
return; return;
const auto selection_string = usb_inserted_devices_list->currentItem(); const auto selection_string = usb_inserted_devices_list->currentItem();
usb_inserted_devices_list->clear(); usb_inserted_devices_list->clear();
auto whitelist = Config::GetUSBDeviceWhitelist();
for (const auto& device : current_devices) for (const auto& device : current_devices)
{ {
if (SConfig::GetInstance().IsUSBDeviceWhitelisted(device.first)) if (whitelist.count({device.first.first, device.first.second}) != 0)
continue; continue;
usb_inserted_devices_list->addItem(QString::fromStdString(device.second)); usb_inserted_devices_list->addItem(QString::fromStdString(device.second));
} }
@ -141,14 +143,16 @@ void USBDeviceAddToWhitelistDialog::AddUSBDeviceToWhitelist()
const u16 vid = static_cast<u16>(std::stoul(vid_string, nullptr, 16)); const u16 vid = static_cast<u16>(std::stoul(vid_string, nullptr, 16));
const u16 pid = static_cast<u16>(std::stoul(pid_string, nullptr, 16)); const u16 pid = static_cast<u16>(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"), ModalMessageBox::critical(this, tr("USB Whitelist Error"),
tr("This USB device is already whitelisted.")); tr("This USB device is already whitelisted."));
return; return;
} }
SConfig::GetInstance().m_usb_passthrough_devices.emplace(vid, pid); Config::SetUSBDeviceWhitelist(whitelist);
SConfig::GetInstance().SaveSettings(); Config::Save();
accept(); accept();
} }

View File

@ -283,14 +283,17 @@ void WiiPane::OnUSBWhitelistRemoveButton()
QString pid = QString(split[1]); QString pid = QString(split[1]);
const u16 vid_u16 = static_cast<u16>(std::stoul(vid.toStdString(), nullptr, 16)); const u16 vid_u16 = static_cast<u16>(std::stoul(vid.toStdString(), nullptr, 16));
const u16 pid_u16 = static_cast<u16>(std::stoul(pid.toStdString(), nullptr, 16)); const u16 pid_u16 = static_cast<u16>(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(); PopulateUSBPassthroughListWidget();
} }
void WiiPane::PopulateUSBPassthroughListWidget() void WiiPane::PopulateUSBPassthroughListWidget()
{ {
m_whitelist_usb_list->clear(); 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 = QListWidgetItem* usb_lwi =
new QListWidgetItem(QString::fromStdString(USBUtils::GetDeviceName(device))); new QListWidgetItem(QString::fromStdString(USBUtils::GetDeviceName(device)));