Configuration refactor

This commit is contained in:
Denis 2023-12-14 23:16:36 +01:00
parent 917ec565f8
commit a3b2eb0e41
No known key found for this signature in database
GPG key ID: DD9B63F805CF5C03
16 changed files with 226 additions and 148 deletions

View file

@ -2,68 +2,66 @@
#include <libconfig.h>
void init_config(controller_settings_t *const conf) {
conf->ff_gain = 100;
conf->enable_qam = 0;
conf->nintendo_layout = 0;
conf->gamepad_output_device = 1;
conf->rumble_dedicated_thread = 0;
}
int fill_config(controller_settings_t *const conf, const char* file) {
int res = 0;
void load_in_config(dev_in_settings_t *const out_conf, const char* const filepath) {
config_t cfg;
config_init(&cfg);
const int config_read_res = config_read_file(&cfg, file);
const int config_read_res = config_read_file(&cfg, filepath);
if (config_read_res != CONFIG_TRUE) {
fprintf(stderr, "Error in reading config file: %s\n", config_error_text(&cfg));
goto fill_config_err;
goto load_in_config_err;
}
int enable_qam;
if (config_lookup_bool(&cfg, "enable_qam", &enable_qam) != CONFIG_FALSE) {
conf->enable_qam = enable_qam;
out_conf->enable_qam = enable_qam;
} else {
fprintf(stderr, "enable_qam (bool) configuration not found. Default value will be used.\n");
}
int ff_gain;
if (config_lookup_int(&cfg, "ff_gain", &ff_gain) != CONFIG_FALSE) {
if (ff_gain <= 100) {
conf->ff_gain = (ff_gain == 100) ? 0xFFFF : ((int)0xFFFF / 100) * ff_gain;
if (ff_gain <= 0xFF) {
out_conf->ff_gain = (ff_gain == 0) ? 0x0000 : ((uint16_t)ff_gain << (uint16_t)8) | (uint16_t)0x00FF;
} else {
fprintf(stderr, "ff_gain (int) must be a number between 0 and 100");
fprintf(stderr, "ff_gain (int) must be a number between 0 and 255");
}
} else {
fprintf(stderr, "ff_gain (int) configuration not found. Default value will be used.\n");
}
config_destroy(&cfg);
load_in_config_err:
return;
}
void load_out_config(dev_out_settings_t *const out_conf, const char* const filepath) {
config_t cfg;
config_init(&cfg);
const int config_read_res = config_read_file(&cfg, filepath);
if (config_read_res != CONFIG_TRUE) {
fprintf(stderr, "Error in reading config file: %s\n", config_error_text(&cfg));
goto load_out_config_err;
}
int nintendo_layout;
if (config_lookup_bool(&cfg, "nintendo_layout", &nintendo_layout) != CONFIG_FALSE) {
conf->nintendo_layout = nintendo_layout;
out_conf->nintendo_layout = nintendo_layout;
} else {
fprintf(stderr, "nintendo_layout (bool) configuration not found. Default value will be used.\n");
}
int gamepad_output_device;
if (config_lookup_int(&cfg, "gamepad_output_device", &gamepad_output_device) != CONFIG_FALSE) {
conf->gamepad_output_device = gamepad_output_device;
int default_gamepad;
if (config_lookup_bool(&cfg, "default_gamepad", &default_gamepad) != CONFIG_FALSE) {
out_conf->default_gamepad = default_gamepad % 3;
} else {
fprintf(stderr, "gamepad_output_device (int) configuration not found. Default value will be used.\n");
}
int rumble_dedicated_thread;
if (config_lookup_bool(&cfg, "rumble_dedicated_thread", &rumble_dedicated_thread) != CONFIG_FALSE) {
conf->rumble_dedicated_thread = rumble_dedicated_thread;
} else {
fprintf(stderr, "rumble_dedicated_thread (bool) configuration not found. Default value will be used.\n");
fprintf(stderr, "default_gamepad (int) configuration not found. Default value will be used.\n");
}
config_destroy(&cfg);
fill_config_err:
return res;
load_out_config_err:
return;
}