PAD: Make controller info public

This commit is contained in:
Connor McLaughlin 2022-05-29 23:56:03 +10:00 committed by refractionpcsx2
parent 9f09aa725b
commit 277706505e
3 changed files with 80 additions and 54 deletions

View File

@ -52,9 +52,8 @@ ControllerBindingWidget::~ControllerBindingWidget() = default;
void ControllerBindingWidget::populateControllerTypes() void ControllerBindingWidget::populateControllerTypes()
{ {
m_ui.controllerType->addItem(tr("None (Not Connected)"), QStringLiteral("None")); for (const auto& [name, display_name] : PAD::GetControllerTypeNames())
for (const std::string& type : PAD::GetControllerTypeNames()) m_ui.controllerType->addItem(QString::fromStdString(display_name), QString::fromStdString(name));
m_ui.controllerType->addItem(QString::fromStdString(type), QString::fromStdString(type));
} }
void ControllerBindingWidget::onTypeChanged() void ControllerBindingWidget::onTypeChanged()

View File

@ -43,7 +43,6 @@ namespace PAD
bool trigger_state; ///< Whether the macro button is active. bool trigger_state; ///< Whether the macro button is active.
}; };
static const char* GetDefaultPadType(u32 pad);
static void LoadMacroButtonConfig(const SettingsInterface& si, u32 pad, const std::string_view& type, const std::string& section); static void LoadMacroButtonConfig(const SettingsInterface& si, u32 pad, const std::string_view& type, const std::string& section);
static void ApplyMacroButton(u32 pad, const MacroButton& mb); static void ApplyMacroButton(u32 pad, const MacroButton& mb);
static void UpdateMacroButtons(); static void UpdateMacroButtons();
@ -273,52 +272,42 @@ void PAD::Update()
UpdateMacroButtons(); UpdateMacroButtons();
} }
struct ControllerBindingInfo static const PAD::ControllerBindingInfo s_dualshock2_binds[] = {
{ {"Up", "D-Pad Up", PAD::ControllerBindingType::Button, GenericInputBinding::DPadUp},
const char* name; {"Right", "D-Pad Right", PAD::ControllerBindingType::Button, GenericInputBinding::DPadRight},
GenericInputBinding generic_mapping; {"Down", "D-Pad Down", PAD::ControllerBindingType::Button, GenericInputBinding::DPadDown},
}; {"Left", "D-Pad Left", PAD::ControllerBindingType::Button, GenericInputBinding::DPadLeft},
struct ControllerInfo {"Triangle", "Triangle", PAD::ControllerBindingType::Button, GenericInputBinding::Triangle},
{ {"Circle", "Circle", PAD::ControllerBindingType::Button, GenericInputBinding::Circle},
const char* name; {"Cross", "Cross", PAD::ControllerBindingType::Button, GenericInputBinding::Cross},
const ControllerBindingInfo* bindings; {"Square", "Square", PAD::ControllerBindingType::Button, GenericInputBinding::Square},
u32 num_bindings; {"Select", "Select", PAD::ControllerBindingType::Button, GenericInputBinding::Select},
PAD::VibrationCapabilities vibration_caps; {"Start", "Start", PAD::ControllerBindingType::Button, GenericInputBinding::Start},
{"L1", "L1 (Left Bumper)", PAD::ControllerBindingType::Button, GenericInputBinding::L1},
{"L2", "L2 (Left Trigger)", PAD::ControllerBindingType::HalfAxis, GenericInputBinding::L2},
{"R1", "R1 (Right Bumper)", PAD::ControllerBindingType::Button, GenericInputBinding::R1},
{"R2", "R2 (Right Trigger)", PAD::ControllerBindingType::HalfAxis, GenericInputBinding::R2},
{"L3", "L3 (Left Stick Button)", PAD::ControllerBindingType::Button, GenericInputBinding::L3},
{"R3", "R3 (Right Stick Button)", PAD::ControllerBindingType::Button, GenericInputBinding::R3},
{"Analog", "Analog Toggle", PAD::ControllerBindingType::Button, GenericInputBinding::System},
{"LUp", "Left Stick Up", PAD::ControllerBindingType::HalfAxis, GenericInputBinding::LeftStickUp},
{"LRight", "Left Stick Right", PAD::ControllerBindingType::HalfAxis, GenericInputBinding::LeftStickRight},
{"LDown", "Left Stick Down", PAD::ControllerBindingType::HalfAxis, GenericInputBinding::LeftStickDown},
{"LLeft", "Left Stick Left", PAD::ControllerBindingType::HalfAxis, GenericInputBinding::LeftStickLeft},
{"RUp", "Right Stick Up", PAD::ControllerBindingType::HalfAxis, GenericInputBinding::RightStickUp},
{"RRight", "Right Stick Right", PAD::ControllerBindingType::HalfAxis, GenericInputBinding::RightStickRight},
{"RDown", "Right Stick Down", PAD::ControllerBindingType::HalfAxis, GenericInputBinding::RightStickDown},
{"RLeft", "Right Stick Left", PAD::ControllerBindingType::HalfAxis, GenericInputBinding::RightStickLeft},
{"LargeMotor", "Large (Low Frequency) Motor", PAD::ControllerBindingType::Motor, GenericInputBinding::LargeMotor},
{"SmallMotor", "Small (High Frequency) Motor", PAD::ControllerBindingType::Motor, GenericInputBinding::SmallMotor},
}; };
static const ControllerBindingInfo s_dualshock2_binds[] = { static const PAD::ControllerInfo s_controller_info[] = {
{"Up", GenericInputBinding::DPadUp }, {"None", "Not Connected", nullptr, 0, PAD::VibrationCapabilities::NoVibration},
{"Right",GenericInputBinding::DPadRight }, {"DualShock2", "DuckShock 2", s_dualshock2_binds, std::size(s_dualshock2_binds), PAD::VibrationCapabilities::LargeSmallMotors},
{"Down", GenericInputBinding::DPadDown },
{"Left", GenericInputBinding::DPadLeft },
{"Triangle", GenericInputBinding::Triangle },
{"Circle", GenericInputBinding::Circle },
{"Cross", GenericInputBinding::Cross },
{"Square", GenericInputBinding::Square },
{"Select", GenericInputBinding::Select },
{"Start", GenericInputBinding::Start },
{"L1", GenericInputBinding::L1 },
{"L2", GenericInputBinding::L2 },
{"R1", GenericInputBinding::R1 },
{"R2", GenericInputBinding::R2 },
{"L3", GenericInputBinding::L3 },
{"R3", GenericInputBinding::R3 },
{"Analog", GenericInputBinding::System},
{"LUp", GenericInputBinding::LeftStickUp },
{"LRight", GenericInputBinding::LeftStickRight },
{"LDown", GenericInputBinding::LeftStickDown },
{"LLeft", GenericInputBinding::LeftStickLeft },
{"RUp", GenericInputBinding::RightStickUp },
{"RRight", GenericInputBinding::RightStickRight },
{"RDown", GenericInputBinding::RightStickDown },
{"RLeft", GenericInputBinding::RightStickLeft },
}; };
static const ControllerInfo s_controller_info[] = { const PAD::ControllerInfo* PAD::GetControllerInfo(const std::string_view& name)
{ "DualShock2", s_dualshock2_binds, std::size(s_dualshock2_binds), PAD::VibrationCapabilities::LargeSmallMotors },
};
static const ControllerInfo* GetControllerInfo(const std::string_view& name)
{ {
for (const ControllerInfo& info : s_controller_info) for (const ControllerInfo& info : s_controller_info)
{ {
@ -329,12 +318,11 @@ static const ControllerInfo* GetControllerInfo(const std::string_view& name)
return nullptr; return nullptr;
} }
std::vector<std::string> PAD::GetControllerTypeNames() std::vector<std::pair<std::string, std::string>> PAD::GetControllerTypeNames()
{ {
std::vector<std::string> ret; std::vector<std::pair<std::string, std::string>> ret;
for (const ControllerInfo& info : s_controller_info) for (const ControllerInfo& info : s_controller_info)
ret.emplace_back(info.name); ret.emplace_back(info.name, info.display_name);
return ret; return ret;
} }
@ -347,7 +335,13 @@ std::vector<std::string> PAD::GetControllerBinds(const std::string_view& type)
if (info) if (info)
{ {
for (u32 i = 0; i < info->num_bindings; i++) for (u32 i = 0; i < info->num_bindings; i++)
{
const ControllerBindingInfo& bi = info->bindings[i];
if (bi.type == ControllerBindingType::Unknown || bi.type == ControllerBindingType::Motor)
continue;
ret.emplace_back(info->bindings[i].name); ret.emplace_back(info->bindings[i].name);
}
} }
return ret; return ret;

View File

@ -37,7 +37,17 @@ u8 PADpoll(u8 value);
namespace PAD namespace PAD
{ {
enum class VibrationCapabilities enum class ControllerBindingType : u8
{
Unknown,
Button,
Axis,
HalfAxis,
Motor,
Macro
};
enum class VibrationCapabilities : u8
{ {
NoVibration, NoVibration,
LargeSmallMotors, LargeSmallMotors,
@ -45,9 +55,29 @@ namespace PAD
Count Count
}; };
struct ControllerBindingInfo
{
const char* name;
const char* display_name;
ControllerBindingType type;
GenericInputBinding generic_mapping;
};
struct ControllerInfo
{
const char* name;
const char* display_name;
const ControllerBindingInfo* bindings;
u32 num_bindings;
PAD::VibrationCapabilities vibration_caps;
};
/// Number of macro buttons per controller. /// Number of macro buttons per controller.
static constexpr u32 NUM_MACRO_BUTTONS_PER_CONTROLLER = 4; static constexpr u32 NUM_MACRO_BUTTONS_PER_CONTROLLER = 4;
/// Returns the default type for the specified port.
const char* GetDefaultPadType(u32 pad);
/// Reloads configuration. /// Reloads configuration.
void LoadConfig(const SettingsInterface& si); void LoadConfig(const SettingsInterface& si);
@ -60,8 +90,8 @@ namespace PAD
/// Updates vibration and other internal state. Called at the *end* of a frame. /// Updates vibration and other internal state. Called at the *end* of a frame.
void Update(); void Update();
/// Returns a list of controller type names. /// Returns a list of controller type names. Pair of [name, display name].
std::vector<std::string> GetControllerTypeNames(); std::vector<std::pair<std::string, std::string>> GetControllerTypeNames();
/// Returns the list of binds for the specified controller type. /// Returns the list of binds for the specified controller type.
std::vector<std::string> GetControllerBinds(const std::string_view& type); std::vector<std::string> GetControllerBinds(const std::string_view& type);
@ -69,6 +99,9 @@ namespace PAD
/// Returns the vibration configuration for the specified controller type. /// Returns the vibration configuration for the specified controller type.
VibrationCapabilities GetControllerVibrationCapabilities(const std::string_view& type); VibrationCapabilities GetControllerVibrationCapabilities(const std::string_view& type);
/// Returns general information for the specified controller type.
const ControllerInfo* GetControllerInfo(const std::string_view& name);
/// Performs automatic controller mapping with the provided list of generic mappings. /// Performs automatic controller mapping with the provided list of generic mappings.
bool MapController(SettingsInterface& si, u32 controller, bool MapController(SettingsInterface& si, u32 controller,
const std::vector<std::pair<GenericInputBinding, std::string>>& mapping); const std::vector<std::pair<GenericInputBinding, std::string>>& mapping);