USB: Copy configuration when creating input profile

Also reset configuration when requested.
This commit is contained in:
Stenzek 2023-06-19 17:51:14 +10:00 committed by Connor McLaughlin
parent 02352ab231
commit 5b5016bfd4
8 changed files with 115 additions and 42 deletions

View File

@ -128,6 +128,7 @@ void ControllerSettingsDialog::onNewProfileClicked()
// from global
auto lock = Host::GetSettingsLock();
PAD::CopyConfiguration(&temp_si, *Host::Internal::GetBaseSettingsLayer(), true, true, false);
USB::CopyConfiguration(&temp_si, *Host::Internal::GetBaseSettingsLayer(), true, true);
}
else
{
@ -135,6 +136,7 @@ void ControllerSettingsDialog::onNewProfileClicked()
const bool copy_hotkey_bindings = m_profile_interface->GetBoolValue("Pad", "UseProfileHotkeyBindings", false);
temp_si.SetBoolValue("Pad", "UseProfileHotkeyBindings", copy_hotkey_bindings);
PAD::CopyConfiguration(&temp_si, *m_profile_interface, true, true, copy_hotkey_bindings);
USB::CopyConfiguration(&temp_si, *m_profile_interface, true, true);
}
}
@ -163,6 +165,7 @@ void ControllerSettingsDialog::onLoadProfileClicked()
{
auto lock = Host::GetSettingsLock();
PAD::CopyConfiguration(Host::Internal::GetBaseSettingsLayer(), *m_profile_interface, true, true, false);
USB::CopyConfiguration(Host::Internal::GetBaseSettingsLayer(), *m_profile_interface, true, true);
}
Host::CommitBaseSettingChanges();

View File

@ -66,6 +66,10 @@ struct SettingInfo
float FloatMinValue() const;
float FloatMaxValue() const;
float FloatStepValue() const;
void SetDefaultValue(SettingsInterface* si, const char* section, const char* key) const;
void CopyValue(SettingsInterface* dest_si, const SettingsInterface& src_si,
const char* section, const char* key) const;
};
enum class GenericInputBinding : u8;

View File

@ -3590,6 +3590,7 @@ void FullscreenUI::CopyGlobalControllerSettingsToGame()
SettingsInterface* ssi = GetEditingSettingsInterface(false);
PAD::CopyConfiguration(dsi, *ssi, true, true, false);
USB::CopyConfiguration(dsi, *ssi, true, true);
SetSettingsChanged(dsi);
ShowToast(std::string(), "Per-game controller configuration initialized with global settings.");
@ -3601,6 +3602,7 @@ void FullscreenUI::ResetControllerSettings()
PAD::SetDefaultControllerConfig(*dsi);
PAD::SetDefaultHotkeyConfig(*dsi);
USB::SetDefaultConfiguration(dsi);
ShowToast(std::string(), "Controller settings reset to default.");
}
@ -3633,6 +3635,7 @@ void FullscreenUI::DoLoadInputProfile()
auto lock = Host::GetSettingsLock();
SettingsInterface* dsi = GetEditingSettingsInterface();
PAD::CopyConfiguration(dsi, ssi, true, true, IsEditingGameSettings(dsi));
USB::CopyConfiguration(dsi, ssi, true, true);
SetSettingsChanged(dsi);
ShowToast(std::string(), fmt::format("Input profile '{}' loaded.", title));
CloseChoiceDialog();
@ -3646,6 +3649,7 @@ void FullscreenUI::DoSaveInputProfile(const std::string& name)
auto lock = Host::GetSettingsLock();
SettingsInterface* ssi = GetEditingSettingsInterface();
PAD::CopyConfiguration(&dsi, *ssi, true, true, IsEditingGameSettings(ssi));
USB::CopyConfiguration(&dsi, *ssi, true, true);
if (dsi.Save())
ShowToast(std::string(), fmt::format("Input profile '{}' saved.", name));
else

View File

@ -287,26 +287,7 @@ void PAD::SetDefaultControllerConfig(SettingsInterface& si)
for (u32 i = 0; i < ci->num_settings; i++)
{
const SettingInfo& csi = ci->settings[i];
switch (csi.type)
{
case SettingInfo::Type::Boolean:
si.SetBoolValue(section.c_str(), csi.name, csi.BooleanDefaultValue());
break;
case SettingInfo::Type::Integer:
case SettingInfo::Type::IntegerList:
si.SetIntValue(section.c_str(), csi.name, csi.IntegerDefaultValue());
break;
case SettingInfo::Type::Float:
si.SetFloatValue(section.c_str(), csi.name, csi.FloatDefaultValue());
break;
case SettingInfo::Type::String:
case SettingInfo::Type::StringList:
case SettingInfo::Type::Path:
si.SetStringValue(section.c_str(), csi.name, csi.StringDefaultValue());
break;
default:
break;
}
csi.SetDefaultValue(&si, section.c_str(), csi.name);
}
}
}
@ -583,26 +564,7 @@ void PAD::CopyConfiguration(SettingsInterface* dest_si, const SettingsInterface&
for (u32 i = 0; i < info->num_settings; i++)
{
const SettingInfo& csi = info->settings[i];
switch (csi.type)
{
case SettingInfo::Type::Boolean:
dest_si->CopyBoolValue(src_si, section.c_str(), csi.name);
break;
case SettingInfo::Type::Integer:
case SettingInfo::Type::IntegerList:
dest_si->CopyIntValue(src_si, section.c_str(), csi.name);
break;
case SettingInfo::Type::Float:
dest_si->CopyFloatValue(src_si, section.c_str(), csi.name);
break;
case SettingInfo::Type::String:
case SettingInfo::Type::StringList:
case SettingInfo::Type::Path:
dest_si->CopyStringValue(src_si, section.c_str(), csi.name);
break;
default:
break;
}
csi.CopyValue(dest_si, src_si, section.c_str(), csi.name);
}
}
}

View File

@ -88,6 +88,55 @@ float SettingInfo::FloatStepValue() const
return step_value ? StringUtil::FromChars<float>(step_value).value_or(fallback_value) : fallback_value;
}
void SettingInfo::SetDefaultValue(SettingsInterface* si, const char* section, const char* key) const
{
switch (type)
{
case SettingInfo::Type::Boolean:
si->SetBoolValue(section, key, BooleanDefaultValue());
break;
case SettingInfo::Type::Integer:
case SettingInfo::Type::IntegerList:
si->SetIntValue(section, key, IntegerDefaultValue());
break;
case SettingInfo::Type::Float:
si->SetFloatValue(section, key, FloatDefaultValue());
break;
case SettingInfo::Type::String:
case SettingInfo::Type::StringList:
case SettingInfo::Type::Path:
si->SetStringValue(section, key, StringDefaultValue());
break;
default:
break;
}
}
void SettingInfo::CopyValue(SettingsInterface* dest_si, const SettingsInterface& src_si,
const char* section, const char* key) const
{
switch (type)
{
case SettingInfo::Type::Boolean:
dest_si->CopyBoolValue(src_si, section, key);
break;
case SettingInfo::Type::Integer:
case SettingInfo::Type::IntegerList:
dest_si->CopyIntValue(src_si, section, key);
break;
case SettingInfo::Type::Float:
dest_si->CopyFloatValue(src_si, section, key);
break;
case SettingInfo::Type::String:
case SettingInfo::Type::StringList:
case SettingInfo::Type::Path:
dest_si->CopyStringValue(src_si, section, key);
break;
default:
break;
}
}
namespace EmuFolders
{
std::string AppRoot;

View File

@ -749,8 +749,8 @@ bool USB::MapDevice(SettingsInterface& si, u32 port, const std::vector<std::pair
void USB::ClearPortBindings(SettingsInterface& si, u32 port)
{
const std::string section(GetConfigSection(port));
const std::string type(GetConfigDevice(si, port));
const std::string section = GetConfigSection(port);
const std::string type = GetConfigDevice(si, port);
const u32 subtype = GetConfigSubType(si, port, type);
const DeviceProxy* dev = RegisterDevice::instance().Device(type);
if (!dev)
@ -760,6 +760,47 @@ void USB::ClearPortBindings(SettingsInterface& si, u32 port)
si.DeleteValue(section.c_str(), GetConfigSubKey(type, bi.name).c_str());
}
void USB::CopyConfiguration(SettingsInterface* dest_si, const SettingsInterface& src_si,
bool copy_devices, bool copy_bindings)
{
for (u32 port = 0; port < NUM_PORTS; port++)
{
const std::string section = GetConfigSection(port);
const std::string type = GetConfigDevice(src_si, port);
const u32 subtype = GetConfigSubType(src_si, port, type);
const DeviceProxy* dev = RegisterDevice::instance().Device(type);
if (copy_devices)
{
dest_si->CopyStringValue(src_si, section.c_str(), "Type");
if (dev)
{
dest_si->CopyUIntValue(src_si, section.c_str(), fmt::format("{}_subtype", type).c_str());
for (const SettingInfo& si : dev->Settings(subtype))
si.CopyValue(dest_si, src_si, section.c_str(), GetConfigSubKey(type, si.name).c_str());
}
}
if (copy_bindings && dev)
{
for (const InputBindingInfo& bi : dev->Bindings(subtype))
dest_si->CopyStringValue(src_si, section.c_str(), GetConfigSubKey(type, bi.name).c_str());
}
}
}
void USB::SetDefaultConfiguration(SettingsInterface* si)
{
for (u32 port = 0; port < NUM_PORTS; port++)
{
const std::string section = GetConfigSection(port);
si->ClearSection(section.c_str());
si->SetStringValue(section.c_str(), "Type", "None");
}
}
void USB::CheckForConfigChanges(const Pcsx2Config& old_config)
{
static_assert(Pcsx2Config::USBOptions::NUM_PORTS == NUM_PORTS);

View File

@ -70,6 +70,13 @@ namespace USB
/// Clears all bindings for a given port.
void ClearPortBindings(SettingsInterface& si, u32 port);
/// Copies configuration between two profiles.
void CopyConfiguration(SettingsInterface* dest_si, const SettingsInterface& src_si, bool copy_devices = true,
bool copy_bindings = true);
/// Resets configuration for all ports.
void SetDefaultConfiguration(SettingsInterface* si);
/// Identifies any device/subtype changes and recreates devices.
void CheckForConfigChanges(const Pcsx2Config& old_config);

View File

@ -443,7 +443,10 @@ void VMManager::SetDefaultSettings(
LogSink::SetDefaultLoggingSettings(si);
}
if (controllers)
{
PAD::SetDefaultControllerConfig(si);
USB::SetDefaultConfiguration(&si);
}
if (hotkeys)
PAD::SetDefaultHotkeyConfig(si);
if (ui)