diff --git a/source/ControllerLib/ControllerConfig.h b/source/ControllerLib/ControllerConfig.h index 00746d5..65d0db1 100644 --- a/source/ControllerLib/ControllerConfig.h +++ b/source/ControllerLib/ControllerConfig.h @@ -5,10 +5,8 @@ #define MAX_TRIGGERS 2 #define MAX_CONTROLLER_BUTTONS 32 -enum ControllerButton : uint8_t +enum ControllerKey : uint8_t { - DEFAULT = 0, - NONE, FACE_UP, FACE_RIGHT, FACE_DOWN, @@ -29,14 +27,37 @@ enum ControllerButton : uint8_t HOME, SYNC, TOUCHPAD, + INVALID = 31, }; +enum class TurboMode : uint8_t +{ + NONE = 0, + HOLD = 1, + TOGGLE = 2, +}; + +struct ControllerButton +{ + bool mapped : 1; + TurboMode turbo_mode : 2; + ControllerKey key : 5; +}; +static_assert(sizeof(ControllerButton) == 1, "ControllerButton has unexpected size"); + struct NormalizedStick { float axis_x; float axis_y; }; +struct NormalizedButtonData +{ + bool buttons[MAX_CONTROLLER_BUTTONS]; + float triggers[2]; + NormalizedStick sticks[2]; +}; + union RGBAColor { struct { @@ -54,7 +75,7 @@ struct ControllerConfig uint8_t stickDeadzonePercent[MAX_JOYSTICKS]{}; uint16_t stickRotationDegrees[MAX_JOYSTICKS]{}; uint8_t triggerDeadzonePercent[MAX_TRIGGERS]{}; - ControllerButton buttons[MAX_CONTROLLER_BUTTONS]{}; + ControllerButton buttonMap[MAX_CONTROLLER_BUTTONS]{}; float triggers[MAX_TRIGGERS]{}; NormalizedStick sticks[MAX_JOYSTICKS]{}; bool swapDPADandLSTICK{false}; @@ -62,4 +83,14 @@ struct ControllerConfig RGBAColor buttonsColor{0, 0, 0, 255}; RGBAColor leftGripColor{70, 70, 70, 255}; RGBAColor rightGripColor{70, 70, 70, 255}; -}; \ No newline at end of file +}; + +inline void MapButtonsToNormalizedData(NormalizedButtonData &out, const ControllerConfig &config, bool buttons[]) +{ + for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) + { + ControllerButton button = config.buttonMap[i]; + + out.buttons[(button.mapped) ? button.key : i] += buttons[i]; + } +} \ No newline at end of file diff --git a/source/ControllerLib/Controllers/Dualshock3Controller.cpp b/source/ControllerLib/Controllers/Dualshock3Controller.cpp index 8c63b64..1c238ae 100644 --- a/source/ControllerLib/Controllers/Dualshock3Controller.cpp +++ b/source/ControllerLib/Controllers/Dualshock3Controller.cpp @@ -200,14 +200,7 @@ NormalizedButtonData Dualshock3Controller::GetNormalizedButtonData() m_buttonData.guide, }; - for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) - { - ControllerButton button = _dualshock3ControllerConfig.buttons[i]; - if (button == NONE) - continue; - - normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i]; - } + MapButtonsToNormalizedData(normalData, _dualshock3ControllerConfig, buttons); return normalData; } diff --git a/source/ControllerLib/Controllers/Dualshock4Controller.cpp b/source/ControllerLib/Controllers/Dualshock4Controller.cpp index 787bb3a..46b9cc6 100644 --- a/source/ControllerLib/Controllers/Dualshock4Controller.cpp +++ b/source/ControllerLib/Controllers/Dualshock4Controller.cpp @@ -208,14 +208,7 @@ NormalizedButtonData Dualshock4Controller::GetNormalizedButtonData() m_buttonData.touchpad_finger1_unpressed == false, }; - for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) - { - ControllerButton button = _dualshock4ControllerConfig.buttons[i]; - if (button == NONE) - continue; - - normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i]; - } + MapButtonsToNormalizedData(normalData, _dualshock4ControllerConfig, buttons); return normalData; } diff --git a/source/ControllerLib/Controllers/Xbox360Controller.cpp b/source/ControllerLib/Controllers/Xbox360Controller.cpp index 55e30c7..4bacc52 100644 --- a/source/ControllerLib/Controllers/Xbox360Controller.cpp +++ b/source/ControllerLib/Controllers/Xbox360Controller.cpp @@ -200,14 +200,7 @@ NormalizedButtonData Xbox360Controller::GetNormalizedButtonData() m_buttonData.guide, }; - for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) - { - ControllerButton button = _xbox360ControllerConfig.buttons[i]; - if (button == NONE) - continue; - - normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i]; - } + MapButtonsToNormalizedData(normalData, _xbox360ControllerConfig, buttons); return normalData; } diff --git a/source/ControllerLib/Controllers/Xbox360WirelessController.cpp b/source/ControllerLib/Controllers/Xbox360WirelessController.cpp index 99f9eac..0ddc53b 100644 --- a/source/ControllerLib/Controllers/Xbox360WirelessController.cpp +++ b/source/ControllerLib/Controllers/Xbox360WirelessController.cpp @@ -221,14 +221,7 @@ NormalizedButtonData Xbox360WirelessController::GetNormalizedButtonData() m_buttonData.guide, }; - for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) - { - ControllerButton button = _xbox360WControllerConfig.buttons[i]; - if (button == NONE) - continue; - - normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i]; - } + MapButtonsToNormalizedData(normalData, _xbox360WControllerConfig, buttons); return normalData; } diff --git a/source/ControllerLib/Controllers/XboxController.cpp b/source/ControllerLib/Controllers/XboxController.cpp index 3194416..59cdcba 100644 --- a/source/ControllerLib/Controllers/XboxController.cpp +++ b/source/ControllerLib/Controllers/XboxController.cpp @@ -187,14 +187,7 @@ NormalizedButtonData XboxController::GetNormalizedButtonData() m_buttonData.black_buttton != 0, }; - for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) - { - ControllerButton button = _xboxControllerConfig.buttons[i]; - if (button == NONE) - continue; - - normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i]; - } + MapButtonsToNormalizedData(normalData, _xboxControllerConfig, buttons); return normalData; } diff --git a/source/ControllerLib/Controllers/XboxOneController.cpp b/source/ControllerLib/Controllers/XboxOneController.cpp index 74af474..6584c6e 100644 --- a/source/ControllerLib/Controllers/XboxOneController.cpp +++ b/source/ControllerLib/Controllers/XboxOneController.cpp @@ -284,14 +284,7 @@ NormalizedButtonData XboxOneController::GetNormalizedButtonData() m_GuidePressed, }; - for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) - { - ControllerButton button = _xboxoneControllerConfig.buttons[i]; - if (button == NONE) - continue; - - normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i]; - } + MapButtonsToNormalizedData(normalData, _xboxoneControllerConfig, buttons); return normalData; } diff --git a/source/ControllerLib/IController.h b/source/ControllerLib/IController.h index 48e36ed..0919018 100644 --- a/source/ControllerLib/IController.h +++ b/source/ControllerLib/IController.h @@ -4,13 +4,6 @@ #include "ControllerConfig.h" #include -struct NormalizedButtonData -{ - bool buttons[MAX_CONTROLLER_BUTTONS]; - float triggers[2]; - NormalizedStick sticks[2]; -}; - class IController { protected: diff --git a/source/Sysmodule/source/config_handler.cpp b/source/Sysmodule/source/config_handler.cpp index 3b66d6b..d9c49c0 100644 --- a/source/Sysmodule/source/config_handler.cpp +++ b/source/Sysmodule/source/config_handler.cpp @@ -27,8 +27,6 @@ namespace syscon::config bool is_config_changed_check_thread_running = false; constexpr std::array keyNames{ - "DEFAULT", - "NONE", "FACE_UP", "FACE_RIGHT", "FACE_DOWN", @@ -51,16 +49,42 @@ namespace syscon::config "TOUCHPAD", }; - ControllerButton StringToKey(const char *text) + ControllerKey StringToKey(const char *text) { for (int i = 0; i != keyNames.size(); ++i) { if (strcmp(keyNames[i], text) == 0) { - return static_cast(i); + return static_cast(i); } } - return NONE; + return ControllerKey::INVALID; + } + + ControllerButton StringToButton(const char *text) + { + ControllerButton button{ + .mapped = true, + }; + char buffer[128]; + strcpy(buffer, text); + char *token = strtok(buffer, " "); + while (token != NULL) + { + if (strcmp(token, "NONE") == 0) + { + button.key = ControllerKey::INVALID; + break; + } + else if (strcmp(token, "TURBO_HOLD") == 0) + button.turbo_mode = TurboMode::HOLD; + else if (strcmp(token, "TURBO_TOGGLE") == 0) + button.turbo_mode = TurboMode::TOGGLE; + else + button.key = StringToKey(token); + token = strtok(NULL, " "); + } + return button; } RGBAColor DecodeColorValue(const char *value) @@ -86,13 +110,10 @@ namespace syscon::config { if (strncmp(name, "KEY_", 4) == 0) { - ControllerButton button = StringToKey(name + 4); - ControllerButton buttonValue = StringToKey(value); - if (button >= 2) - { - tempConfig.buttons[button - 2] = buttonValue; - return 1; - } + ControllerKey button = StringToKey(name + 4); + ControllerButton buttonValue = StringToButton(value); + tempConfig.buttonMap[button] = buttonValue; + return 1; } else if (strcmp(name, "left_stick_deadzone") == 0) {