mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-04-02 22:16:24 +00:00
Add support for SDL_GAMECONTROLLERCONFIG environment variable
This commit is contained in:
@@ -24,9 +24,116 @@
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
struct mapping* mapping_parse(char* mapping) {
|
||||
char* strpoint;
|
||||
char* guid = strtok_r(mapping, ",", &strpoint);
|
||||
char* name = strtok_r(NULL, ",", &strpoint);
|
||||
if (guid == NULL || name == NULL)
|
||||
return NULL;
|
||||
|
||||
struct mapping* map = malloc(sizeof(struct mapping));
|
||||
if (map == NULL) {
|
||||
fprintf(stderr, "Not enough memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
strncpy(map->guid, guid, sizeof(map->guid));
|
||||
strncpy(map->name, name, sizeof(map->name));
|
||||
|
||||
char* option;
|
||||
while ((option = strtok_r(NULL, ",", &strpoint)) != NULL) {
|
||||
char *key = NULL, *value = NULL;
|
||||
int ret;
|
||||
if ((ret = sscanf(option, "%m[^:]:%ms", &key, &value)) == 2) {
|
||||
int int_value, direction_value;
|
||||
char flag = 0;
|
||||
if (strcmp("platform", key) == 0)
|
||||
strncpy(map->platform, value, sizeof(map->platform));
|
||||
else if (sscanf(value, "b%d", &int_value) == 1) {
|
||||
if (strcmp("a", key) == 0)
|
||||
map->btn_a = int_value;
|
||||
else if (strcmp("y", key) == 0)
|
||||
map->btn_y = int_value;
|
||||
else if (strcmp("x", key) == 0)
|
||||
map->btn_x = int_value;
|
||||
else if (strcmp("b", key) == 0)
|
||||
map->btn_b = int_value;
|
||||
else if (strcmp("back", key) == 0)
|
||||
map->btn_back = int_value;
|
||||
else if (strcmp("start", key) == 0)
|
||||
map->btn_start = int_value;
|
||||
else if (strcmp("guide", key) == 0)
|
||||
map->btn_guide = int_value;
|
||||
else if (strcmp("dpup", key) == 0)
|
||||
map->btn_dpup = int_value;
|
||||
else if (strcmp("dpdown", key) == 0)
|
||||
map->btn_dpdown = int_value;
|
||||
else if (strcmp("dpleft", key) == 0)
|
||||
map->btn_dpleft = int_value;
|
||||
else if (strcmp("dpright", key) == 0)
|
||||
map->btn_dpright = int_value;
|
||||
else if (strcmp("leftstick", key) == 0)
|
||||
map->btn_leftstick = int_value;
|
||||
else if (strcmp("rightstick", key) == 0)
|
||||
map->btn_rightstick = int_value;
|
||||
else if (strcmp("leftshoulder", key) == 0)
|
||||
map->btn_leftshoulder = int_value;
|
||||
else if (strcmp("rightshoulder", key) == 0)
|
||||
map->btn_rightshoulder = int_value;
|
||||
else if (strcmp("lefttrigger", key) == 0)
|
||||
map->btn_lefttrigger = int_value;
|
||||
else if (strcmp("righttrigger", key) == 0)
|
||||
map->btn_righttrigger = int_value;
|
||||
} else if (sscanf(value, "a%d%c", &int_value, &flag) >= 1) {
|
||||
if (strcmp("leftx", key) == 0) {
|
||||
map->abs_leftx = int_value;
|
||||
map->reverse_leftx = flag == '~';
|
||||
} else if (strcmp("lefty", key) == 0) {
|
||||
map->abs_lefty = int_value;
|
||||
map->reverse_lefty = flag == '~';
|
||||
} else if (strcmp("rightx", key) == 0) {
|
||||
map->abs_rightx = int_value;
|
||||
map->reverse_rightx = flag == '~';
|
||||
} else if (strcmp("righty", key) == 0) {
|
||||
map->abs_righty = int_value;
|
||||
map->reverse_righty = flag == '~';
|
||||
} else if (strcmp("lefttrigger", key) == 0)
|
||||
map->abs_lefttrigger = int_value;
|
||||
else if (strcmp("righttrigger", key) == 0)
|
||||
map->abs_righttrigger = int_value;
|
||||
} else if (sscanf(value, "h%d.%d", &int_value, &direction_value) == 2) {
|
||||
if (strcmp("dpright", key) == 0) {
|
||||
map->hat_dpright = int_value;
|
||||
map->hat_dir_dpright = direction_value;
|
||||
} else if (strcmp("dpleft", key) == 0) {
|
||||
map->hat_dpleft = int_value;
|
||||
map->hat_dir_dpleft = direction_value;
|
||||
} else if (strcmp("dpup", key) == 0) {
|
||||
map->hat_dpup = int_value;
|
||||
map->hat_dir_dpup = direction_value;
|
||||
} else if (strcmp("dpdown", key) == 0) {
|
||||
map->hat_dpdown = int_value;
|
||||
map->hat_dir_dpdown = direction_value;
|
||||
}
|
||||
} else
|
||||
fprintf(stderr, "Can't map (%s)\n", option);
|
||||
} else if (ret == 0 && option[0] != '\n')
|
||||
fprintf(stderr, "Can't map (%s)\n", option);
|
||||
|
||||
if (key != NULL)
|
||||
free(key);
|
||||
|
||||
if (value != NULL)
|
||||
free(value);
|
||||
}
|
||||
map->guid[32] = '\0';
|
||||
map->name[256] = '\0';
|
||||
map->platform[32] = '\0';
|
||||
return map;
|
||||
}
|
||||
|
||||
struct mapping* mapping_load(char* fileName, bool verbose) {
|
||||
struct mapping* mappings = NULL;
|
||||
struct mapping* map = NULL;
|
||||
FILE* fd = fopen(fileName, "r");
|
||||
if (fd == NULL) {
|
||||
fprintf(stderr, "Can't open mapping file: %s\n", fileName);
|
||||
@@ -37,115 +144,11 @@ struct mapping* mapping_load(char* fileName, bool verbose) {
|
||||
char *line = NULL;
|
||||
size_t len = 0;
|
||||
while (getline(&line, &len, fd) != -1) {
|
||||
char* strpoint;
|
||||
char* guid = strtok_r(line, ",", &strpoint);
|
||||
char* name = strtok_r(NULL, ",", &strpoint);
|
||||
if (guid == NULL || name == NULL)
|
||||
continue;
|
||||
|
||||
struct mapping* newmap = malloc(sizeof(struct mapping));
|
||||
if (newmap == NULL) {
|
||||
fprintf(stderr, "Not enough memory");
|
||||
exit(EXIT_FAILURE);
|
||||
} else if (mappings == NULL)
|
||||
mappings = newmap;
|
||||
else
|
||||
map->next = newmap;
|
||||
|
||||
map = newmap;
|
||||
|
||||
strncpy(map->guid, guid, sizeof(map->guid));
|
||||
strncpy(map->name, name, sizeof(map->name));
|
||||
|
||||
char* option;
|
||||
while ((option = strtok_r(NULL, ",", &strpoint)) != NULL) {
|
||||
char *key = NULL, *value = NULL;
|
||||
int ret;
|
||||
if ((ret = sscanf(option, "%m[^:]:%ms", &key, &value)) == 2) {
|
||||
int int_value, direction_value;
|
||||
char flag = 0;
|
||||
if (strcmp("platform", key) == 0)
|
||||
strncpy(map->platform, value, sizeof(map->platform));
|
||||
else if (sscanf(value, "b%d", &int_value) == 1) {
|
||||
if (strcmp("a", key) == 0)
|
||||
map->btn_a = int_value;
|
||||
else if (strcmp("y", key) == 0)
|
||||
map->btn_y = int_value;
|
||||
else if (strcmp("x", key) == 0)
|
||||
map->btn_x = int_value;
|
||||
else if (strcmp("b", key) == 0)
|
||||
map->btn_b = int_value;
|
||||
else if (strcmp("back", key) == 0)
|
||||
map->btn_back = int_value;
|
||||
else if (strcmp("start", key) == 0)
|
||||
map->btn_start = int_value;
|
||||
else if (strcmp("guide", key) == 0)
|
||||
map->btn_guide = int_value;
|
||||
else if (strcmp("dpup", key) == 0)
|
||||
map->btn_dpup = int_value;
|
||||
else if (strcmp("dpdown", key) == 0)
|
||||
map->btn_dpdown = int_value;
|
||||
else if (strcmp("dpleft", key) == 0)
|
||||
map->btn_dpleft = int_value;
|
||||
else if (strcmp("dpright", key) == 0)
|
||||
map->btn_dpright = int_value;
|
||||
else if (strcmp("leftstick", key) == 0)
|
||||
map->btn_leftstick = int_value;
|
||||
else if (strcmp("rightstick", key) == 0)
|
||||
map->btn_rightstick = int_value;
|
||||
else if (strcmp("leftshoulder", key) == 0)
|
||||
map->btn_leftshoulder = int_value;
|
||||
else if (strcmp("rightshoulder", key) == 0)
|
||||
map->btn_rightshoulder = int_value;
|
||||
else if (strcmp("lefttrigger", key) == 0)
|
||||
map->btn_lefttrigger = int_value;
|
||||
else if (strcmp("righttrigger", key) == 0)
|
||||
map->btn_righttrigger = int_value;
|
||||
} else if (sscanf(value, "a%d%c", &int_value, &flag) >= 1) {
|
||||
if (strcmp("leftx", key) == 0) {
|
||||
map->abs_leftx = int_value;
|
||||
map->reverse_leftx = flag == '~';
|
||||
} else if (strcmp("lefty", key) == 0) {
|
||||
map->abs_lefty = int_value;
|
||||
map->reverse_lefty = flag == '~';
|
||||
} else if (strcmp("rightx", key) == 0) {
|
||||
map->abs_rightx = int_value;
|
||||
map->reverse_rightx = flag == '~';
|
||||
} else if (strcmp("righty", key) == 0) {
|
||||
map->abs_righty = int_value;
|
||||
map->reverse_righty = flag == '~';
|
||||
} else if (strcmp("lefttrigger", key) == 0)
|
||||
map->abs_lefttrigger = int_value;
|
||||
else if (strcmp("righttrigger", key) == 0)
|
||||
map->abs_righttrigger = int_value;
|
||||
} else if (sscanf(value, "h%d.%d", &int_value, &direction_value) == 2) {
|
||||
if (strcmp("dpright", key) == 0) {
|
||||
map->hat_dpright = int_value;
|
||||
map->hat_dir_dpright = direction_value;
|
||||
} else if (strcmp("dpleft", key) == 0) {
|
||||
map->hat_dpleft = int_value;
|
||||
map->hat_dir_dpleft = direction_value;
|
||||
} else if (strcmp("dpup", key) == 0) {
|
||||
map->hat_dpup = int_value;
|
||||
map->hat_dir_dpup = direction_value;
|
||||
} else if (strcmp("dpdown", key) == 0) {
|
||||
map->hat_dpdown = int_value;
|
||||
map->hat_dir_dpdown = direction_value;
|
||||
}
|
||||
} else
|
||||
fprintf(stderr, "Can't map (%s)\n", option);
|
||||
} else if (ret == 0 && option[0] != '\n')
|
||||
fprintf(stderr, "Can't map (%s)\n", option);
|
||||
|
||||
if (key != NULL)
|
||||
free(key);
|
||||
|
||||
if (value != NULL)
|
||||
free(value);
|
||||
struct mapping* map = mapping_parse(line);
|
||||
if (map) {
|
||||
map->next = mappings;
|
||||
mappings = map;
|
||||
}
|
||||
map->guid[32] = '\0';
|
||||
map->name[256] = '\0';
|
||||
map->platform[32] = '\0';
|
||||
}
|
||||
free(line);
|
||||
|
||||
|
||||
@@ -47,4 +47,5 @@ struct mapping {
|
||||
struct mapping* next;
|
||||
};
|
||||
|
||||
struct mapping* mapping_parse(char* mapping);
|
||||
struct mapping* mapping_load(char* fileName, bool verbose);
|
||||
|
||||
14
src/main.c
14
src/main.c
@@ -252,11 +252,21 @@ int main(int argc, char* argv[]) {
|
||||
config.stream.supportsHevc = config.codec != CODEC_H264 && (config.codec == CODEC_HEVC || platform_supports_hevc(system));
|
||||
|
||||
if (IS_EMBEDDED(system)) {
|
||||
if (config.mapping == NULL) {
|
||||
char* mapping_env = getenv("SDL_GAMECONTROLLERCONFIG");
|
||||
if (config.mapping == NULL && mapping_env == NULL) {
|
||||
fprintf(stderr, "Please specify mapping file as default mapping could not be found.\n");
|
||||
exit(-1);
|
||||
}
|
||||
struct mapping* mappings = mapping_load(config.mapping, config.debug_level > 0);
|
||||
|
||||
struct mapping* mappings;
|
||||
if (config.mapping != NULL)
|
||||
mappings = mapping_load(config.mapping, config.debug_level > 0);
|
||||
|
||||
if (mapping_env != NULL) {
|
||||
struct mapping* map = mapping_parse(mapping_env);
|
||||
map->next = mappings;
|
||||
mappings = map;
|
||||
}
|
||||
|
||||
for (int i=0;i<config.inputsCount;i++) {
|
||||
if (config.debug_level > 0)
|
||||
|
||||
Reference in New Issue
Block a user