1
0
mirror of https://github.com/cathery/sys-con.git synced 2025-03-13 07:14:35 +00:00

Add turbo mode button configuration

Also move the mapping stuff to a common function
This commit is contained in:
cathery 2020-04-23 00:48:34 +03:00
parent a99fa9a420
commit 88bc77d49b
9 changed files with 75 additions and 72 deletions

View File

@ -5,10 +5,8 @@
#define MAX_TRIGGERS 2 #define MAX_TRIGGERS 2
#define MAX_CONTROLLER_BUTTONS 32 #define MAX_CONTROLLER_BUTTONS 32
enum ControllerButton : uint8_t enum ControllerKey : uint8_t
{ {
DEFAULT = 0,
NONE,
FACE_UP, FACE_UP,
FACE_RIGHT, FACE_RIGHT,
FACE_DOWN, FACE_DOWN,
@ -29,14 +27,37 @@ enum ControllerButton : uint8_t
HOME, HOME,
SYNC, SYNC,
TOUCHPAD, 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 struct NormalizedStick
{ {
float axis_x; float axis_x;
float axis_y; float axis_y;
}; };
struct NormalizedButtonData
{
bool buttons[MAX_CONTROLLER_BUTTONS];
float triggers[2];
NormalizedStick sticks[2];
};
union RGBAColor { union RGBAColor {
struct struct
{ {
@ -54,7 +75,7 @@ struct ControllerConfig
uint8_t stickDeadzonePercent[MAX_JOYSTICKS]{}; uint8_t stickDeadzonePercent[MAX_JOYSTICKS]{};
uint16_t stickRotationDegrees[MAX_JOYSTICKS]{}; uint16_t stickRotationDegrees[MAX_JOYSTICKS]{};
uint8_t triggerDeadzonePercent[MAX_TRIGGERS]{}; uint8_t triggerDeadzonePercent[MAX_TRIGGERS]{};
ControllerButton buttons[MAX_CONTROLLER_BUTTONS]{}; ControllerButton buttonMap[MAX_CONTROLLER_BUTTONS]{};
float triggers[MAX_TRIGGERS]{}; float triggers[MAX_TRIGGERS]{};
NormalizedStick sticks[MAX_JOYSTICKS]{}; NormalizedStick sticks[MAX_JOYSTICKS]{};
bool swapDPADandLSTICK{false}; bool swapDPADandLSTICK{false};
@ -63,3 +84,13 @@ struct ControllerConfig
RGBAColor leftGripColor{70, 70, 70, 255}; RGBAColor leftGripColor{70, 70, 70, 255};
RGBAColor rightGripColor{70, 70, 70, 255}; RGBAColor rightGripColor{70, 70, 70, 255};
}; };
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];
}
}

View File

@ -200,14 +200,7 @@ NormalizedButtonData Dualshock3Controller::GetNormalizedButtonData()
m_buttonData.guide, m_buttonData.guide,
}; };
for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) MapButtonsToNormalizedData(normalData, _dualshock3ControllerConfig, buttons);
{
ControllerButton button = _dualshock3ControllerConfig.buttons[i];
if (button == NONE)
continue;
normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i];
}
return normalData; return normalData;
} }

View File

@ -208,14 +208,7 @@ NormalizedButtonData Dualshock4Controller::GetNormalizedButtonData()
m_buttonData.touchpad_finger1_unpressed == false, m_buttonData.touchpad_finger1_unpressed == false,
}; };
for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) MapButtonsToNormalizedData(normalData, _dualshock4ControllerConfig, buttons);
{
ControllerButton button = _dualshock4ControllerConfig.buttons[i];
if (button == NONE)
continue;
normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i];
}
return normalData; return normalData;
} }

View File

@ -200,14 +200,7 @@ NormalizedButtonData Xbox360Controller::GetNormalizedButtonData()
m_buttonData.guide, m_buttonData.guide,
}; };
for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) MapButtonsToNormalizedData(normalData, _xbox360ControllerConfig, buttons);
{
ControllerButton button = _xbox360ControllerConfig.buttons[i];
if (button == NONE)
continue;
normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i];
}
return normalData; return normalData;
} }

View File

@ -221,14 +221,7 @@ NormalizedButtonData Xbox360WirelessController::GetNormalizedButtonData()
m_buttonData.guide, m_buttonData.guide,
}; };
for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) MapButtonsToNormalizedData(normalData, _xbox360WControllerConfig, buttons);
{
ControllerButton button = _xbox360WControllerConfig.buttons[i];
if (button == NONE)
continue;
normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i];
}
return normalData; return normalData;
} }

View File

@ -187,14 +187,7 @@ NormalizedButtonData XboxController::GetNormalizedButtonData()
m_buttonData.black_buttton != 0, m_buttonData.black_buttton != 0,
}; };
for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) MapButtonsToNormalizedData(normalData, _xboxControllerConfig, buttons);
{
ControllerButton button = _xboxControllerConfig.buttons[i];
if (button == NONE)
continue;
normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i];
}
return normalData; return normalData;
} }

View File

@ -284,14 +284,7 @@ NormalizedButtonData XboxOneController::GetNormalizedButtonData()
m_GuidePressed, m_GuidePressed,
}; };
for (int i = 0; i != MAX_CONTROLLER_BUTTONS; ++i) MapButtonsToNormalizedData(normalData, _xboxoneControllerConfig, buttons);
{
ControllerButton button = _xboxoneControllerConfig.buttons[i];
if (button == NONE)
continue;
normalData.buttons[(button != DEFAULT ? button - 2 : i)] += buttons[i];
}
return normalData; return normalData;
} }

View File

@ -4,13 +4,6 @@
#include "ControllerConfig.h" #include "ControllerConfig.h"
#include <memory> #include <memory>
struct NormalizedButtonData
{
bool buttons[MAX_CONTROLLER_BUTTONS];
float triggers[2];
NormalizedStick sticks[2];
};
class IController class IController
{ {
protected: protected:

View File

@ -27,8 +27,6 @@ namespace syscon::config
bool is_config_changed_check_thread_running = false; bool is_config_changed_check_thread_running = false;
constexpr std::array keyNames{ constexpr std::array keyNames{
"DEFAULT",
"NONE",
"FACE_UP", "FACE_UP",
"FACE_RIGHT", "FACE_RIGHT",
"FACE_DOWN", "FACE_DOWN",
@ -51,16 +49,42 @@ namespace syscon::config
"TOUCHPAD", "TOUCHPAD",
}; };
ControllerButton StringToKey(const char *text) ControllerKey StringToKey(const char *text)
{ {
for (int i = 0; i != keyNames.size(); ++i) for (int i = 0; i != keyNames.size(); ++i)
{ {
if (strcmp(keyNames[i], text) == 0) if (strcmp(keyNames[i], text) == 0)
{ {
return static_cast<ControllerButton>(i); return static_cast<ControllerKey>(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) RGBAColor DecodeColorValue(const char *value)
@ -86,14 +110,11 @@ namespace syscon::config
{ {
if (strncmp(name, "KEY_", 4) == 0) if (strncmp(name, "KEY_", 4) == 0)
{ {
ControllerButton button = StringToKey(name + 4); ControllerKey button = StringToKey(name + 4);
ControllerButton buttonValue = StringToKey(value); ControllerButton buttonValue = StringToButton(value);
if (button >= 2) tempConfig.buttonMap[button] = buttonValue;
{
tempConfig.buttons[button - 2] = buttonValue;
return 1; return 1;
} }
}
else if (strcmp(name, "left_stick_deadzone") == 0) else if (strcmp(name, "left_stick_deadzone") == 0)
{ {
tempConfig.stickDeadzonePercent[0] = atoi(value); tempConfig.stickDeadzonePercent[0] = atoi(value);