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

View File

@ -66,6 +66,10 @@ struct SettingInfo
float FloatMinValue() const; float FloatMinValue() const;
float FloatMaxValue() const; float FloatMaxValue() const;
float FloatStepValue() 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; enum class GenericInputBinding : u8;

View File

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

View File

@ -287,26 +287,7 @@ void PAD::SetDefaultControllerConfig(SettingsInterface& si)
for (u32 i = 0; i < ci->num_settings; i++) for (u32 i = 0; i < ci->num_settings; i++)
{ {
const SettingInfo& csi = ci->settings[i]; const SettingInfo& csi = ci->settings[i];
switch (csi.type) csi.SetDefaultValue(&si, section.c_str(), csi.name);
{
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;
}
} }
} }
} }
@ -583,26 +564,7 @@ void PAD::CopyConfiguration(SettingsInterface* dest_si, const SettingsInterface&
for (u32 i = 0; i < info->num_settings; i++) for (u32 i = 0; i < info->num_settings; i++)
{ {
const SettingInfo& csi = info->settings[i]; const SettingInfo& csi = info->settings[i];
switch (csi.type) csi.CopyValue(dest_si, src_si, section.c_str(), csi.name);
{
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;
}
} }
} }
} }

View File

@ -88,6 +88,55 @@ float SettingInfo::FloatStepValue() const
return step_value ? StringUtil::FromChars<float>(step_value).value_or(fallback_value) : fallback_value; 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 namespace EmuFolders
{ {
std::string AppRoot; 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) void USB::ClearPortBindings(SettingsInterface& si, u32 port)
{ {
const std::string section(GetConfigSection(port)); const std::string section = GetConfigSection(port);
const std::string type(GetConfigDevice(si, port)); const std::string type = GetConfigDevice(si, port);
const u32 subtype = GetConfigSubType(si, port, type); const u32 subtype = GetConfigSubType(si, port, type);
const DeviceProxy* dev = RegisterDevice::instance().Device(type); const DeviceProxy* dev = RegisterDevice::instance().Device(type);
if (!dev) if (!dev)
@ -760,6 +760,47 @@ void USB::ClearPortBindings(SettingsInterface& si, u32 port)
si.DeleteValue(section.c_str(), GetConfigSubKey(type, bi.name).c_str()); 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) void USB::CheckForConfigChanges(const Pcsx2Config& old_config)
{ {
static_assert(Pcsx2Config::USBOptions::NUM_PORTS == NUM_PORTS); static_assert(Pcsx2Config::USBOptions::NUM_PORTS == NUM_PORTS);

View File

@ -70,6 +70,13 @@ namespace USB
/// Clears all bindings for a given port. /// Clears all bindings for a given port.
void ClearPortBindings(SettingsInterface& si, u32 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. /// Identifies any device/subtype changes and recreates devices.
void CheckForConfigChanges(const Pcsx2Config& old_config); void CheckForConfigChanges(const Pcsx2Config& old_config);

View File

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