cfg touchup

This commit is contained in:
Katharine Chui 2025-05-02 15:28:39 +02:00
parent 4d89be9ce7
commit 36a1bb6808
3 changed files with 37 additions and 26 deletions

View file

@ -9,54 +9,58 @@ emulated_logitech_g27_config g_cfg_logitech_g27;
LOG_CHANNEL(cfg_log, "CFG"); LOG_CHANNEL(cfg_log, "CFG");
void emulated_logitech_g27_config::from_default() emulated_logitech_g27_config::emulated_logitech_g27_config()
: m_path(fmt::format("%s%s.yml", fs::get_config_dir(true), "LogitechG27"))
{ {
std::lock_guard<std::mutex> lock(m_mutex); }
void emulated_logitech_g27_config::reset()
{
const std::lock_guard<std::mutex> lock(m_mutex);
cfg::node::from_default(); cfg::node::from_default();
} }
void emulated_logitech_g27_config::save() void emulated_logitech_g27_config::save(bool lock_mutex)
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::unique_lock lock(m_mutex, std::defer_lock);
const std::string cfg_name = fmt::format("%s%s.yml", fs::get_config_dir(true), "LogitechG27"); if (lock_mutex)
cfg_log.notice("Saving LogitechG27 config: %s", cfg_name);
if (!fs::create_path(fs::get_parent_dir(cfg_name)))
{ {
cfg_log.fatal("Failed to create path: %s (%s)", cfg_name, fs::g_tls_error); lock.lock();
}
cfg_log.notice("Saving LogitechG27 config: '%s'", m_path);
if (!fs::create_path(fs::get_parent_dir(m_path)))
{
cfg_log.fatal("Failed to create path: '%s' (%s)", m_path, fs::g_tls_error);
} }
if (!cfg::node::save(cfg_name)) if (!cfg::node::save(m_path))
{ {
cfg_log.error("Failed to save LogitechG27 config to '%s' (error=%s)", cfg_name, fs::g_tls_error); cfg_log.error("Failed to save LogitechG27 config to '%s' (error=%s)", m_path, fs::g_tls_error);
} }
} }
bool emulated_logitech_g27_config::load() bool emulated_logitech_g27_config::load()
{ {
bool result = false; const std::lock_guard lock(m_mutex);
const std::string cfg_name = fmt::format("%s%s.yml", fs::get_config_dir(true), "LogitechG27");
cfg_log.notice("Loading LogitechG27 config: %s", cfg_name); cfg_log.notice("Loading LogitechG27 config: %s", m_path);
from_default(); from_default();
m_mutex.lock(); if (fs::file cfg_file{m_path, fs::read})
if (fs::file cfg_file{cfg_name, fs::read})
{ {
if (const std::string content = cfg_file.to_string(); !content.empty()) if (const std::string content = cfg_file.to_string(); !content.empty())
{ {
result = from_string(content); return from_string(content);
} }
m_mutex.unlock();
} }
else else
{ {
m_mutex.unlock(); save(false);
save();
} }
return result; return true;
} }
#endif #endif

View file

@ -41,10 +41,8 @@ struct emulated_logitech_g27_mapping : cfg::node
struct emulated_logitech_g27_config : cfg::node struct emulated_logitech_g27_config : cfg::node
{ {
public:
std::mutex m_mutex; std::mutex m_mutex;
bool load();
void save();
void from_default() override;
// TODO these defaults are for a shifter-less G29 + a xbox controller for shifter testing, perhaps find a new default // TODO these defaults are for a shifter-less G29 + a xbox controller for shifter testing, perhaps find a new default
@ -92,6 +90,15 @@ struct emulated_logitech_g27_config : cfg::node
cfg::uint<0, 0xFFFFFFFF> led_device_type_id{this, "led_device_type_id", 0x046dc24f}; cfg::uint<0, 0xFFFFFFFF> led_device_type_id{this, "led_device_type_id", 0x046dc24f};
cfg::_bool enabled{this, "enabled", false}; cfg::_bool enabled{this, "enabled", false};
emulated_logitech_g27_config();
bool load();
void save(bool lock_mutex = true);
void reset();
private:
const std::string m_path;
}; };
extern emulated_logitech_g27_config g_cfg_logitech_g27; extern emulated_logitech_g27_config g_cfg_logitech_g27;

View file

@ -668,7 +668,7 @@ emulated_logitech_g27_settings_dialog::emulated_logitech_g27_settings_dialog(QWi
{ {
if (QMessageBox::question(this, tr("Confirm Reset"), tr("Reset all?")) != QMessageBox::Yes) if (QMessageBox::question(this, tr("Confirm Reset"), tr("Reset all?")) != QMessageBox::Yes)
return; return;
g_cfg_logitech_g27.from_default(); g_cfg_logitech_g27.reset();
load_ui_state_from_config(); load_ui_state_from_config();
g_cfg_logitech_g27.save(); g_cfg_logitech_g27.save();
} }