1
0
mirror of https://github.com/cathery/sys-con.git synced 2025-03-12 22:14:30 +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_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};
};
};
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,
};
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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

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

View File

@ -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<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)
@ -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)
{