rework cfg

This commit is contained in:
Katharine Chui 2025-05-01 12:14:07 +02:00
parent 7d19cac8f9
commit 2ef8c9e4a2
5 changed files with 110 additions and 186 deletions

View file

@ -14,7 +14,6 @@
#include <thread>
#include "LogitechG27.h"
#include "LogitechG27Config.h"
#include "Emu/Cell/lv2/sys_usbd.h"
#include "Emu/system_config.h"
#include "Input/pad_thread.h"
@ -199,14 +198,14 @@ static inline logitech_g27_sdl_mapping get_runtime_mapping()
{
logitech_g27_sdl_mapping mapping;
#define CONVERT_MAPPING(name) \
{ \
mapping.name.device_type_id = g_cfg_logitech_g27.name##_device_type_id.get(); \
mapping.name.type = static_cast<sdl_mapping_type>(g_cfg_logitech_g27.name##_type.get()); \
mapping.name.id = static_cast<uint8_t>(g_cfg_logitech_g27.name##_id.get()); \
mapping.name.hat = static_cast<hat_component>(g_cfg_logitech_g27.name##_hat.get()); \
mapping.name.reverse = g_cfg_logitech_g27.name##_reverse.get(); \
mapping.name.positive_axis = false; \
#define CONVERT_MAPPING(name) \
{ \
mapping.name.device_type_id = g_cfg_logitech_g27.name.device_type_id.get(); \
mapping.name.type = static_cast<sdl_mapping_type>(g_cfg_logitech_g27.name.type.get()); \
mapping.name.id = static_cast<uint8_t>(g_cfg_logitech_g27.name.id.get()); \
mapping.name.hat = static_cast<hat_component>(g_cfg_logitech_g27.name.hat.get()); \
mapping.name.reverse = g_cfg_logitech_g27.name.reverse.get(); \
mapping.name.positive_axis = false; \
}
CONVERT_MAPPING(steering);

View file

@ -1,6 +1,7 @@
#pragma once
#include "Emu/Io/usb_device.h"
#include "LogitechG27Config.h"
#ifndef _MSC_VER
#pragma GCC diagnostic push
@ -11,7 +12,6 @@
#pragma GCC diagnostic pop
#endif
#include <mutex>
#include <map>
#include <vector>
@ -30,23 +30,6 @@ struct logitech_g27_ffb_slot
int effect_id;
};
// TODO maybe push these into cfg
enum sdl_mapping_type
{
MAPPING_BUTTON = 0,
MAPPING_HAT,
MAPPING_AXIS,
};
enum hat_component
{
HAT_NONE = 0,
HAT_UP,
HAT_DOWN,
HAT_LEFT,
HAT_RIGHT
};
struct sdl_mapping
{
uint32_t device_type_id; // (vendor_id << 16) | product_id

View file

@ -3,104 +3,21 @@
#ifdef HAVE_SDL3
#include "Utilities/File.h"
#include "LogitechG27.h"
#include "LogitechG27Config.h"
emulated_logitech_g27_config g_cfg_logitech_g27;
LOG_CHANNEL(cfg_log, "CFG");
void emulated_logitech_g27_config::fill_defaults()
void emulated_logitech_g27_config::from_default()
{
// a shifter-less g29 with a xbox 360 controller shifter place holder...
m_mutex.lock();
#define INIT_AXIS_MAPPING(name, device_type_id, id, reverse) \
{ \
name##_device_type_id.set(device_type_id); \
name##_type.set(MAPPING_AXIS); \
name##_id.set(id); \
name##_hat.set(HAT_NONE); \
name##_reverse.set(reverse); \
}
INIT_AXIS_MAPPING(steering, 0x046dc24f, 0, false);
INIT_AXIS_MAPPING(throttle, 0x046dc24f, 2, false);
INIT_AXIS_MAPPING(brake, 0x046dc24f, 3, false);
INIT_AXIS_MAPPING(clutch, 0x046dc24f, 1, false);
#undef INIT_AXIS_MAPPING
#define INIT_BUTTON_MAPPING(name, device_type_id, id, reverse) \
{ \
name##_device_type_id.set(device_type_id); \
name##_type.set(MAPPING_BUTTON); \
name##_id.set(id); \
name##_hat.set(HAT_NONE); \
name##_reverse.set(reverse); \
}
INIT_BUTTON_MAPPING(shift_up, 0x046dc24f, 4, false);
INIT_BUTTON_MAPPING(shift_down, 0x046dc24f, 5, false);
INIT_BUTTON_MAPPING(triangle, 0x046dc24f, 3, false);
INIT_BUTTON_MAPPING(cross, 0x046dc24f, 0, false);
INIT_BUTTON_MAPPING(square, 0x046dc24f, 1, false);
INIT_BUTTON_MAPPING(circle, 0x046dc24f, 2, false);
INIT_BUTTON_MAPPING(l2, 0x046dc24f, 7, false);
INIT_BUTTON_MAPPING(l3, 0x046dc24f, 11, false);
INIT_BUTTON_MAPPING(r2, 0x046dc24f, 6, false);
INIT_BUTTON_MAPPING(r3, 0x046dc24f, 10, false);
INIT_BUTTON_MAPPING(plus, 0x046dc24f, 19, false);
INIT_BUTTON_MAPPING(minus, 0x046dc24f, 20, false);
INIT_BUTTON_MAPPING(dial_clockwise, 0x046dc24f, 21, false);
INIT_BUTTON_MAPPING(dial_anticlockwise, 0x046dc24f, 22, false);
INIT_BUTTON_MAPPING(select, 0x046dc24f, 8, false);
INIT_BUTTON_MAPPING(pause, 0x046dc24f, 9, false);
INIT_BUTTON_MAPPING(shifter_1, 0x045e028e, 3, false);
INIT_BUTTON_MAPPING(shifter_2, 0x045e028e, 0, false);
INIT_BUTTON_MAPPING(shifter_3, 0x045e028e, 2, false);
INIT_BUTTON_MAPPING(shifter_4, 0x045e028e, 1, false);
#undef INIT_BUTTON_MAPPING
#define INIT_HAT_MAPPING(name, device_type_id, id, hat, reverse) \
{ \
name##_device_type_id.set(device_type_id); \
name##_type.set(MAPPING_HAT); \
name##_id.set(id); \
name##_hat.set(hat); \
name##_reverse.set(reverse); \
}
INIT_HAT_MAPPING(up, 0x046dc24f, 0, HAT_UP, false);
INIT_HAT_MAPPING(down, 0x046dc24f, 0, HAT_DOWN, false);
INIT_HAT_MAPPING(left, 0x046dc24f, 0, HAT_LEFT, false);
INIT_HAT_MAPPING(right, 0x046dc24f, 0, HAT_RIGHT, false);
INIT_HAT_MAPPING(shifter_5, 0x045e028e, 0, HAT_UP, false);
INIT_HAT_MAPPING(shifter_6, 0x045e028e, 0, HAT_DOWN, false);
INIT_HAT_MAPPING(shifter_r, 0x045e028e, 0, HAT_LEFT, false);
#undef INIT_HAT_MAPPING
reverse_effects.set(true);
ffb_device_type_id.set(0x046dc24f);
led_device_type_id.set(0x046dc24f);
enabled.set(false);
m_mutex.unlock();
std::lock_guard<std::mutex> lock(m_mutex);
cfg::node::from_default();
}
void emulated_logitech_g27_config::save()
{
m_mutex.lock();
std::lock_guard<std::mutex> lock(m_mutex);
const std::string cfg_name = fmt::format("%s%s.yml", fs::get_config_dir(true), "LogitechG27");
cfg_log.notice("Saving LogitechG27 config: %s", cfg_name);
@ -113,7 +30,6 @@ void emulated_logitech_g27_config::save()
{
cfg_log.error("Failed to save LogitechG27 config to '%s' (error=%s)", cfg_name, fs::g_tls_error);
}
m_mutex.unlock();
}
bool emulated_logitech_g27_config::load()
@ -122,7 +38,7 @@ bool emulated_logitech_g27_config::load()
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);
fill_defaults();
from_default();
m_mutex.lock();

View file

@ -4,68 +4,94 @@
#include <mutex>
enum sdl_mapping_type
{
MAPPING_BUTTON = 0,
MAPPING_HAT,
MAPPING_AXIS,
};
enum hat_component
{
HAT_NONE = 0,
HAT_UP,
HAT_DOWN,
HAT_LEFT,
HAT_RIGHT
};
struct emulated_logitech_g27_mapping : cfg::node
{
cfg::uint<0, 0xFFFFFFFF> device_type_id;
cfg::uint<0, 0xFFFFFFFF> type;
cfg::uint<0, 0xFFFFFFFFFFFFFFFF> id;
cfg::uint<0, 0xFFFFFFFF> hat;
cfg::_bool reverse;
emulated_logitech_g27_mapping(cfg::node* owner, std::string name, uint32_t device_type_id_def, sdl_mapping_type type_def, uint64_t id_def, hat_component hat_def, bool reverse_def)
: cfg::node(owner, name),
device_type_id(this, "device_type_id", device_type_id_def),
type(this, "type", type_def),
id(this, "id", id_def),
hat(this, "hat", hat_def),
reverse(this, "reverse", reverse_def)
{
}
};
struct emulated_logitech_g27_config : cfg::node
{
std::mutex m_mutex;
bool load();
void save();
void fill_defaults();
void from_default() override;
#define STR(s) #s
#define MAPPING_ENTRY(name) \
cfg::uint<0, 0xFFFFFFFF> name##_device_type_id{this, STR(name##_device_type_id)}; \
cfg::uint<0, 0xFFFFFFFF> name##_type{this, STR(name##_type)}; \
cfg::uint<0, 0xFFFFFFFFFFFFFFFF> name##_id{this, STR(name##_id)}; \
cfg::uint<0, 0xFFFFFFFF> name##_hat{this, STR(name##_hat)}; \
cfg::_bool name##_reverse{this, STR(name##_reverse)};
// TODO these defaults are for a shifter-less G29 + a xbox controller for shifter testing, perhaps find a new default
MAPPING_ENTRY(steering);
MAPPING_ENTRY(throttle);
MAPPING_ENTRY(brake);
MAPPING_ENTRY(clutch);
MAPPING_ENTRY(shift_up);
MAPPING_ENTRY(shift_down);
emulated_logitech_g27_mapping steering{this, "steering", 0x046dc24f, MAPPING_AXIS, 0, HAT_NONE, false};
emulated_logitech_g27_mapping throttle{this, "throttle", 0x046dc24f, MAPPING_AXIS, 2, HAT_NONE, false};
emulated_logitech_g27_mapping brake{this, "brake", 0x046dc24f, MAPPING_AXIS, 3, HAT_NONE, false};
emulated_logitech_g27_mapping clutch{this, "clutch", 0x046dc24f, MAPPING_AXIS, 1, HAT_NONE, false};
emulated_logitech_g27_mapping shift_up{this, "shift_up", 0x046dc24f, MAPPING_BUTTON, 4, HAT_NONE, false};
emulated_logitech_g27_mapping shift_down{this, "shift_down", 0x046dc24f, MAPPING_BUTTON, 5, HAT_NONE, false};
MAPPING_ENTRY(up);
MAPPING_ENTRY(down);
MAPPING_ENTRY(left);
MAPPING_ENTRY(right);
emulated_logitech_g27_mapping up{this, "up", 0x046dc24f, MAPPING_HAT, 0, HAT_UP, false};
emulated_logitech_g27_mapping down{this, "down", 0x046dc24f, MAPPING_HAT, 0, HAT_DOWN, false};
emulated_logitech_g27_mapping left{this, "left", 0x046dc24f, MAPPING_HAT, 0, HAT_LEFT, false};
emulated_logitech_g27_mapping right{this, "right", 0x046dc24f, MAPPING_HAT, 0, HAT_RIGHT, false};
MAPPING_ENTRY(triangle);
MAPPING_ENTRY(cross);
MAPPING_ENTRY(square);
MAPPING_ENTRY(circle);
emulated_logitech_g27_mapping triangle{this, "triangle", 0x046dc24f, MAPPING_BUTTON, 3, HAT_NONE, false};
emulated_logitech_g27_mapping cross{this, "cross", 0x046dc24f, MAPPING_BUTTON, 0, HAT_NONE, false};
emulated_logitech_g27_mapping square{this, "square", 0x046dc24f, MAPPING_BUTTON, 1, HAT_NONE, false};
emulated_logitech_g27_mapping circle{this, "circle", 0x046dc24f, MAPPING_BUTTON, 2, HAT_NONE, false};
MAPPING_ENTRY(l2);
MAPPING_ENTRY(l3);
MAPPING_ENTRY(r2);
MAPPING_ENTRY(r3);
emulated_logitech_g27_mapping l2{this, "l2", 0x046dc24f, MAPPING_BUTTON, 7, HAT_NONE, false};
emulated_logitech_g27_mapping l3{this, "l3", 0x046dc24f, MAPPING_BUTTON, 11, HAT_NONE, false};
emulated_logitech_g27_mapping r2{this, "r2", 0x046dc24f, MAPPING_BUTTON, 6, HAT_NONE, false};
emulated_logitech_g27_mapping r3{this, "r3", 0x046dc24f, MAPPING_BUTTON, 10, HAT_NONE, false};
MAPPING_ENTRY(plus);
MAPPING_ENTRY(minus);
emulated_logitech_g27_mapping plus{this, "plus", 0x046dc24f, MAPPING_BUTTON, 19, HAT_NONE, false};
emulated_logitech_g27_mapping minus{this, "minus", 0x046dc24f, MAPPING_BUTTON, 20, HAT_NONE, false};
MAPPING_ENTRY(dial_clockwise);
MAPPING_ENTRY(dial_anticlockwise);
emulated_logitech_g27_mapping dial_clockwise{this, "dial_clockwise", 0x046dc24f, MAPPING_BUTTON, 21, HAT_NONE, false};
emulated_logitech_g27_mapping dial_anticlockwise{this, "dial_anticlockwise", 0x046dc24f, MAPPING_BUTTON, 22, HAT_NONE, false};
MAPPING_ENTRY(select);
MAPPING_ENTRY(pause);
emulated_logitech_g27_mapping select{this, "select", 0x046dc24f, MAPPING_BUTTON, 8, HAT_NONE, false};
emulated_logitech_g27_mapping pause{this, "pause", 0x046dc24f, MAPPING_BUTTON, 9, HAT_NONE, false};
MAPPING_ENTRY(shifter_1);
MAPPING_ENTRY(shifter_2);
MAPPING_ENTRY(shifter_3);
MAPPING_ENTRY(shifter_4);
MAPPING_ENTRY(shifter_5);
MAPPING_ENTRY(shifter_6);
MAPPING_ENTRY(shifter_r);
emulated_logitech_g27_mapping shifter_1{this, "shifter_1", 0x045e028e, MAPPING_BUTTON, 3, HAT_NONE, false};
emulated_logitech_g27_mapping shifter_2{this, "shifter_2", 0x045e028e, MAPPING_BUTTON, 0, HAT_NONE, false};
emulated_logitech_g27_mapping shifter_3{this, "shifter_3", 0x045e028e, MAPPING_BUTTON, 2, HAT_NONE, false};
emulated_logitech_g27_mapping shifter_4{this, "shifter_4", 0x045e028e, MAPPING_BUTTON, 1, HAT_NONE, false};
emulated_logitech_g27_mapping shifter_5{this, "shifter_5", 0x045e028e, MAPPING_HAT, 0, HAT_UP, false};
emulated_logitech_g27_mapping shifter_6{this, "shifter_6", 0x045e028e, MAPPING_HAT, 0, HAT_DOWN, false};
emulated_logitech_g27_mapping shifter_r{this, "shifter_r", 0x045e028e, MAPPING_HAT, 0, HAT_LEFT, false};
#undef MAPPING_ENTRY
#undef STR
cfg::_bool reverse_effects{this, "reverse_effects", true};
cfg::uint<0, 0xFFFFFFFF> ffb_device_type_id{this, "ffb_device_type_id", 0x046dc24f};
cfg::uint<0, 0xFFFFFFFF> led_device_type_id{this, "led_device_type_id", 0x046dc24f};
cfg::_bool reverse_effects{this, "reverse_effects"};
cfg::uint<0, 0xFFFFFFFF> ffb_device_type_id{this, "ffb_device_type_id"};
cfg::uint<0, 0xFFFFFFFF> led_device_type_id{this, "led_device_type_id"};
cfg::_bool enabled{this, "enabled"};
cfg::_bool enabled{this, "enabled", false};
};
extern emulated_logitech_g27_config g_cfg_logitech_g27;

View file

@ -438,14 +438,14 @@ private:
void emulated_logitech_g27_settings_dialog::save_ui_state_to_config()
{
#define SAVE_MAPPING(name) \
{ \
const sdl_mapping& m = name->get_mapping(); \
g_cfg_logitech_g27.name##_device_type_id.set(m.device_type_id); \
g_cfg_logitech_g27.name##_type.set(m.type); \
g_cfg_logitech_g27.name##_id.set(m.id); \
g_cfg_logitech_g27.name##_hat.set(m.hat); \
g_cfg_logitech_g27.name##_reverse.set(m.reverse); \
#define SAVE_MAPPING(name) \
{ \
const sdl_mapping& m = name->get_mapping(); \
g_cfg_logitech_g27.name.device_type_id.set(m.device_type_id); \
g_cfg_logitech_g27.name.type.set(m.type); \
g_cfg_logitech_g27.name.id.set(m.id); \
g_cfg_logitech_g27.name.hat.set(m.hat); \
g_cfg_logitech_g27.name.reverse.set(m.reverse); \
}
SAVE_MAPPING(steering);
@ -498,16 +498,16 @@ void emulated_logitech_g27_settings_dialog::save_ui_state_to_config()
void emulated_logitech_g27_settings_dialog::load_ui_state_from_config()
{
#define LOAD_MAPPING(name) \
{ \
sdl_mapping m = { \
.device_type_id = static_cast<uint32_t>(g_cfg_logitech_g27.name##_device_type_id.get()), \
.type = static_cast<sdl_mapping_type>(g_cfg_logitech_g27.name##_type.get()), \
.id = static_cast<uint8_t>(g_cfg_logitech_g27.name##_id.get()), \
.hat = static_cast<hat_component>(g_cfg_logitech_g27.name##_hat.get()), \
.reverse = g_cfg_logitech_g27.name##_reverse.get(), \
.positive_axis = false}; \
name->set_mapping(m); \
#define LOAD_MAPPING(name) \
{ \
sdl_mapping m = { \
.device_type_id = static_cast<uint32_t>(g_cfg_logitech_g27.name.device_type_id.get()), \
.type = static_cast<sdl_mapping_type>(g_cfg_logitech_g27.name.type.get()), \
.id = static_cast<uint8_t>(g_cfg_logitech_g27.name.id.get()), \
.hat = static_cast<hat_component>(g_cfg_logitech_g27.name.hat.get()), \
.reverse = g_cfg_logitech_g27.name.reverse.get(), \
.positive_axis = false}; \
name->set_mapping(m); \
}
LOAD_MAPPING(steering);
@ -592,7 +592,7 @@ emulated_logitech_g27_settings_dialog::emulated_logitech_g27_settings_dialog(QWi
{
if (QMessageBox::question(this, tr("Confirm Reset"), tr("Reset all?")) != QMessageBox::Yes)
return;
g_cfg_logitech_g27.fill_defaults();
g_cfg_logitech_g27.from_default();
load_ui_state_from_config();
g_cfg_logitech_g27.save();
}
@ -635,11 +635,11 @@ emulated_logitech_g27_settings_dialog::emulated_logitech_g27_settings_dialog(QWi
#define ADD_MAPPING_SETTING(name, is_axis, display_name, flip_axis_display) \
{ \
sdl_mapping m = { \
.device_type_id = static_cast<uint32_t>(g_cfg_logitech_g27.name##_device_type_id.get()), \
.type = static_cast<sdl_mapping_type>(g_cfg_logitech_g27.name##_type.get()), \
.id = static_cast<uint8_t>(g_cfg_logitech_g27.name##_id.get()), \
.hat = static_cast<hat_component>(g_cfg_logitech_g27.name##_hat.get()), \
.reverse = g_cfg_logitech_g27.name##_reverse.get(), \
.device_type_id = static_cast<uint32_t>(g_cfg_logitech_g27.name.device_type_id.get()), \
.type = static_cast<sdl_mapping_type>(g_cfg_logitech_g27.name.type.get()), \
.id = static_cast<uint8_t>(g_cfg_logitech_g27.name.id.get()), \
.hat = static_cast<hat_component>(g_cfg_logitech_g27.name.hat.get()), \
.reverse = g_cfg_logitech_g27.name.reverse.get(), \
.positive_axis = false}; \
name = new Mapping(mapping_widget, this, ffb_device, led_device, m, is_axis, display_name, flip_axis_display); \
mapping_layout->addWidget(name); \