1
0
mirror of https://github.com/cathery/sys-con.git synced 2024-09-01 20:39:25 +00:00
sys-con/source/configFile.cpp

283 lines
8.2 KiB
C++
Raw Normal View History

2019-11-15 11:03:06 +00:00
#include "mainLoop.h"
2019-11-06 22:20:58 +00:00
#include "configFile.h"
#include "Controllers.h"
#include "ControllerConfig.h"
#include <cstring>
2019-11-07 17:46:47 +00:00
#include <sys/stat.h>
2019-11-06 22:20:58 +00:00
#include <array>
#include "ini.h"
#include "log.h"
#define GLOBALCONFIG "config_global.ini"
2019-11-08 14:15:02 +00:00
#define XBOXCONFIG "config_xboxorig.ini"
2019-11-06 22:20:58 +00:00
#define XBOX360CONFIG "config_xbox360.ini"
#define XBOXONECONFIG "config_xboxone.ini"
2019-11-12 17:55:50 +00:00
#define XBOXONEADAPTERCONFIG "config_xboxoneadapter.ini"
2019-11-06 22:20:58 +00:00
#define DUALSHOCK3CONFIG "config_dualshock3.ini"
#define DUALSHOCK4CONFIG "config_dualshock4.ini"
2019-11-21 21:40:42 +00:00
constexpr std::array<const char *, NUM_CONTROLLERBUTTONS> keyNames{
2019-11-06 22:20:58 +00:00
"FACE_UP",
"FACE_RIGHT",
"FACE_DOWN",
"FACE_LEFT",
"LSTICK_CLICK",
"RSTICK_CLICK",
"LEFT_BUMPER",
"RIGHT_BUMPER",
"LEFT_TRIGGER",
"RIGHT_TRIGGER",
"BACK",
"START",
"DPAD_UP",
"DPAD_RIGHT",
"DPAD_DOWN",
"DPAD_LEFT",
"SYNC",
"GUIDE",
2019-11-15 11:03:06 +00:00
"TOUCHPAD",
2019-11-06 22:20:58 +00:00
};
static ControllerButton _StringToKey(const char *text)
{
2019-11-07 17:46:47 +00:00
for (int i = 0; i != NUM_CONTROLLERBUTTONS; ++i)
2019-11-06 22:20:58 +00:00
{
2019-11-07 17:46:47 +00:00
if (strcmp(keyNames[i], text) == 0)
2019-11-06 22:20:58 +00:00
{
return static_cast<ControllerButton>(i);
}
}
return NOT_SET;
}
2019-11-13 20:49:15 +00:00
static RGBAColor _DecodeColorValue(const char *value)
{
RGBAColor color{255};
uint8_t counter = 0;
int charIndex = 0;
while (value[charIndex] != '\0')
{
if (charIndex == 0)
color.values[counter++] = atoi(value + charIndex++);
if (value[charIndex++] == ',')
{
color.values[counter++] = atoi(value + charIndex);
if (counter == 4)
break;
}
}
return color;
}
2019-11-06 22:20:58 +00:00
static ControllerConfig temp_config;
2019-11-15 11:03:06 +00:00
static GlobalConfig global_config;
2019-11-21 22:03:31 +00:00
static RGBAColor temp_color;
2019-11-12 17:55:50 +00:00
static char firmwarePath[100];
2019-11-06 22:20:58 +00:00
static int _ParseConfigLine(void *dummy, const char *section, const char *name, const char *value)
{
2019-11-15 11:03:06 +00:00
if (strcmp(section, "global") == 0)
2019-11-06 22:20:58 +00:00
{
2019-11-15 11:03:06 +00:00
if (strcmp(name, "use_dualshock_2nd_generation") == 0)
{
global_config.dualshock4_productID = (strcmp(value, "true") ? PRODUCT_DUALSHOCK4_1X : PRODUCT_DUALSHOCK4_2X);
return 1;
}
2019-11-12 17:55:50 +00:00
}
2019-11-15 11:03:06 +00:00
else
2019-11-13 20:49:15 +00:00
{
2019-11-15 11:03:06 +00:00
if (strncmp(name, "key_", 4) == 0)
2019-11-13 20:49:15 +00:00
{
2019-11-15 11:03:06 +00:00
ControllerButton button = _StringToKey(name + 4);
ControllerButton buttonValue = _StringToKey(value);
temp_config.buttons[button] = buttonValue;
temp_config.buttons[buttonValue] = button;
2019-11-13 20:49:15 +00:00
return 1;
}
2019-11-15 11:03:06 +00:00
else if (strcmp(name, "left_stick_deadzone") == 0)
2019-11-13 20:49:15 +00:00
{
2019-11-15 11:03:06 +00:00
temp_config.leftStickDeadzonePercent = atoi(value);
2019-11-13 20:49:15 +00:00
return 1;
}
2019-11-15 11:03:06 +00:00
else if (strcmp(name, "right_stick_deadzone") == 0)
2019-11-13 20:49:15 +00:00
{
2019-11-15 11:03:06 +00:00
temp_config.rightStickDeadzonePercent = atoi(value);
2019-11-13 20:49:15 +00:00
return 1;
}
2019-11-15 11:03:06 +00:00
else if (strcmp(name, "left_stick_rotation") == 0)
2019-11-13 20:49:15 +00:00
{
2019-11-15 11:03:06 +00:00
temp_config.leftStickRotationDegrees = atoi(value);
2019-11-13 20:49:15 +00:00
return 1;
}
2019-11-15 11:03:06 +00:00
else if (strcmp(name, "right_stick_rotation") == 0)
{
temp_config.rightStickRotationDegrees = atoi(value);
return 1;
}
else if (strcmp(name, "trigger_deadzone") == 0)
{
temp_config.triggerDeadzonePercent = atoi(value);
return 1;
}
else if (strcmp(name, "swap_dpad_and_lstick") == 0)
{
temp_config.swapDPADandLSTICK = (strcmp(value, "true") ? false : true);
return 1;
}
else if (strcmp(name, "firmware_path") == 0)
{
strcpy(firmwarePath, value);
return 1;
}
else if (strncmp(name, "color_", 6) == 0)
{
if (strcmp(name + 6, "body") == 0)
{
temp_config.bodyColor = _DecodeColorValue(value);
return 1;
}
else if (strcmp(name + 6, "buttons") == 0)
{
temp_config.buttonsColor = _DecodeColorValue(value);
return 1;
}
else if (strcmp(name + 6, "leftGrip") == 0)
{
temp_config.leftGripColor = _DecodeColorValue(value);
return 1;
}
else if (strcmp(name + 6, "rightGrip") == 0)
{
temp_config.rightGripColor = _DecodeColorValue(value);
return 1;
}
2019-11-21 22:03:31 +00:00
else if (strcmp(name + 6, "led") == 0)
{
temp_color = _DecodeColorValue(value);
return 1;
}
2019-11-15 11:03:06 +00:00
}
2019-11-13 20:49:15 +00:00
}
2019-11-06 22:20:58 +00:00
return 0;
}
static Result _ReadFromConfig(const char *path)
{
2019-11-07 17:46:47 +00:00
temp_config = ControllerConfig{};
for (int i = 0; i != NUM_CONTROLLERBUTTONS; ++i)
{
temp_config.buttons[i] = NOT_SET;
}
2019-11-06 22:20:58 +00:00
return ini_parse(path, _ParseConfigLine, NULL);
}
void LoadAllConfigs()
{
2019-11-15 11:03:06 +00:00
if (R_SUCCEEDED(_ReadFromConfig(CONFIG_PATH GLOBALCONFIG)))
{
LoadGlobalConfig(&global_config);
}
else
WriteToLog("Failed to read from global config!");
2019-11-08 14:15:02 +00:00
if (R_SUCCEEDED(_ReadFromConfig(CONFIG_PATH XBOXCONFIG)))
{
XboxController::LoadConfig(&temp_config);
}
else
WriteToLog("Failed to read from xbox orig config!");
2019-11-06 22:20:58 +00:00
if (R_SUCCEEDED(_ReadFromConfig(CONFIG_PATH XBOXONECONFIG)))
XboxOneController::LoadConfig(&temp_config);
else
WriteToLog("Failed to read from xbox one config!");
2019-11-12 17:55:50 +00:00
if (R_SUCCEEDED(_ReadFromConfig(CONFIG_PATH XBOXONEADAPTERCONFIG)))
XboxOneAdapter::LoadConfig(&temp_config, firmwarePath);
else
WriteToLog("Failed to read from xbox one adapter config!");
2019-11-06 22:20:58 +00:00
if (R_SUCCEEDED(_ReadFromConfig(CONFIG_PATH XBOX360CONFIG)))
2019-11-08 12:56:20 +00:00
{
2019-11-06 22:20:58 +00:00
Xbox360Controller::LoadConfig(&temp_config);
2019-11-08 12:56:20 +00:00
Xbox360WirelessController::LoadConfig(&temp_config);
}
2019-11-06 22:20:58 +00:00
else
WriteToLog("Failed to read from xbox 360 config!");
if (R_SUCCEEDED(_ReadFromConfig(CONFIG_PATH DUALSHOCK3CONFIG)))
Dualshock3Controller::LoadConfig(&temp_config);
else
WriteToLog("Failed to read from dualshock 3 config!");
if (R_SUCCEEDED(_ReadFromConfig(CONFIG_PATH DUALSHOCK4CONFIG)))
2019-11-21 22:03:31 +00:00
Dualshock4Controller::LoadConfig(&temp_config, temp_color);
2019-11-06 22:20:58 +00:00
else
WriteToLog("Failed to read from dualshock 4 config!");
}
2019-11-07 17:46:47 +00:00
bool CheckForFileChanges()
{
bool filesChanged = false;
static time_t globalConfigLastModified;
2019-11-08 14:15:02 +00:00
static time_t xboxConfigLastModified;
2019-11-07 17:46:47 +00:00
static time_t xbox360ConfigLastModified;
static time_t xboxOneConfigLastModified;
2019-11-12 17:55:50 +00:00
static time_t xboxOneAdapterConfigLastModified;
2019-11-07 17:46:47 +00:00
static time_t dualshock3ConfigLastModified;
static time_t dualshock4ConfigLastModified;
struct stat result;
if (stat(CONFIG_PATH GLOBALCONFIG, &result) == 0)
if (globalConfigLastModified != result.st_mtime)
{
globalConfigLastModified = result.st_mtime;
filesChanged = true;
}
2019-11-08 14:15:02 +00:00
if (stat(CONFIG_PATH XBOXCONFIG, &result) == 0)
if (xboxConfigLastModified != result.st_mtime)
{
xboxConfigLastModified = result.st_mtime;
filesChanged = true;
}
2019-11-07 17:46:47 +00:00
if (stat(CONFIG_PATH XBOX360CONFIG, &result) == 0)
if (xbox360ConfigLastModified != result.st_mtime)
{
xbox360ConfigLastModified = result.st_mtime;
filesChanged = true;
}
if (stat(CONFIG_PATH XBOXONECONFIG, &result) == 0)
if (xboxOneConfigLastModified != result.st_mtime)
{
xboxOneConfigLastModified = result.st_mtime;
filesChanged = true;
}
if (stat(CONFIG_PATH DUALSHOCK3CONFIG, &result) == 0)
if (dualshock3ConfigLastModified != result.st_mtime)
{
dualshock3ConfigLastModified = result.st_mtime;
filesChanged = true;
}
if (stat(CONFIG_PATH DUALSHOCK4CONFIG, &result) == 0)
if (dualshock4ConfigLastModified != result.st_mtime)
{
dualshock4ConfigLastModified = result.st_mtime;
filesChanged = true;
}
2019-11-12 17:55:50 +00:00
if (stat(CONFIG_PATH XBOXONEADAPTERCONFIG, &result) == 0)
if (xboxOneAdapterConfigLastModified != result.st_mtime)
{
xboxOneAdapterConfigLastModified = result.st_mtime;
filesChanged = true;
}
2019-11-07 17:46:47 +00:00
return filesChanged;
}