rpcsx/rpcs3/Emu/Io/pad_config.cpp

106 lines
2.6 KiB
C++
Raw Normal View History

2020-12-05 13:08:24 +01:00
#include "stdafx.h"
#include "pad_config.h"
#include "Emu/system_utils.hpp"
2021-08-10 21:45:26 +02:00
LOG_CHANNEL(input_log, "Input");
2020-10-30 21:26:22 +01:00
2021-08-10 21:45:26 +02:00
bool cfg_input::load(const std::string& title_id, const std::string& profile, bool strict)
{
2021-09-05 03:12:23 +02:00
input_log.notice("Loading pad config (title_id='%s', profile='%s', strict=%d)", title_id, profile, strict);
2021-08-10 21:45:26 +02:00
std::string cfg_name;
2021-09-05 03:12:23 +02:00
// Check custom config first
if (!title_id.empty())
{
2021-09-05 03:12:23 +02:00
cfg_name = rpcs3::utils::get_custom_input_config_path(title_id);
2021-08-10 21:45:26 +02:00
}
2021-09-05 03:12:23 +02:00
// Check active global profile next
if ((title_id.empty() || !strict) && !fs::is_file(cfg_name))
2021-08-10 21:45:26 +02:00
{
2021-09-05 03:12:23 +02:00
cfg_name = rpcs3::utils::get_input_config_dir() + profile + ".yml";
}
2021-09-05 03:12:23 +02:00
// Fallback to default profile
2021-08-10 21:45:26 +02:00
if (!strict && !fs::is_file(cfg_name))
{
2021-08-10 21:45:26 +02:00
cfg_name = rpcs3::utils::get_input_config_dir() + g_cfg_profile.default_profile + ".yml";
}
2021-08-10 21:45:26 +02:00
from_default();
if (fs::file cfg_file{ cfg_name, fs::read })
{
2021-08-10 21:45:26 +02:00
input_log.notice("Loading pad profile: '%s'", cfg_name);
if (std::string content = cfg_file.to_string(); !content.empty())
{
return from_string(content);
}
}
2021-08-10 21:45:26 +02:00
// Add keyboard by default
input_log.notice("Pad profile empty. Adding default keyboard pad handler");
player[0]->handler.from_string(fmt::format("%s", pad_handler::keyboard));
player[0]->device.from_string(pad::keyboard_device_name.data());
return false;
}
2021-08-10 21:45:26 +02:00
void cfg_input::save(const std::string& title_id, const std::string& profile) const
{
2021-08-10 21:45:26 +02:00
std::string cfg_name;
if (title_id.empty())
{
2021-08-10 21:45:26 +02:00
cfg_name = rpcs3::utils::get_input_config_dir() + profile + ".yml";
input_log.notice("Saving pad config profile '%s' to '%s'", profile, cfg_name);
}
else
{
cfg_name = rpcs3::utils::get_custom_input_config_path(title_id);
2021-08-10 21:45:26 +02:00
input_log.notice("Saving custom pad config for '%s' to '%s'", title_id, cfg_name);
}
2021-08-10 21:45:26 +02:00
if (!fs::create_path(fs::get_parent_dir(cfg_name)))
{
input_log.fatal("Failed to create path: %s (%s)", cfg_name, fs::g_tls_error);
}
fs::pending_file cfg_file(cfg_name);
if (!cfg_file.file || (cfg_file.file.write(to_string()), !cfg_file.commit()))
2021-08-10 21:45:26 +02:00
{
input_log.error("Failed to save pad config to '%s'", cfg_name);
}
}
2021-08-10 21:45:26 +02:00
cfg_profile::cfg_profile()
: path(rpcs3::utils::get_input_config_root() + "/active_profiles.yml")
{
}
2021-08-10 21:45:26 +02:00
bool cfg_profile::load()
{
2021-08-10 21:45:26 +02:00
if (fs::file cfg_file{ path, fs::read })
{
return from_string(cfg_file.to_string());
}
2021-08-10 21:45:26 +02:00
from_default();
return false;
}
2021-08-10 21:45:26 +02:00
void cfg_profile::save() const
{
2021-08-10 21:45:26 +02:00
input_log.notice("Saving pad profile config to '%s'", path);
2021-10-22 16:58:55 +02:00
fs::pending_file cfg_file(path);
if (!cfg_file.file || (cfg_file.file.write(to_string()), !cfg_file.commit()))
2021-08-10 21:45:26 +02:00
{
input_log.error("Failed to save pad profile config to '%s'", path);
}
}