Add support for SDL_GAMECONTROLLERCONFIG environment variable

This commit is contained in:
Iwan Timmer
2017-06-19 22:44:33 +02:00
parent c23d95f83c
commit 3e2c5b1789
3 changed files with 125 additions and 111 deletions
+26 -23
View File
@@ -24,35 +24,18 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
struct mapping* mapping_load(char* fileName, bool verbose) { struct mapping* mapping_parse(char* mapping) {
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);
exit(EXIT_FAILURE);
} else if (verbose)
printf("Loading mappingfile %s\n", fileName);
char *line = NULL;
size_t len = 0;
while (getline(&line, &len, fd) != -1) {
char* strpoint; char* strpoint;
char* guid = strtok_r(line, ",", &strpoint); char* guid = strtok_r(mapping, ",", &strpoint);
char* name = strtok_r(NULL, ",", &strpoint); char* name = strtok_r(NULL, ",", &strpoint);
if (guid == NULL || name == NULL) if (guid == NULL || name == NULL)
continue; return NULL;
struct mapping* newmap = malloc(sizeof(struct mapping)); struct mapping* map = malloc(sizeof(struct mapping));
if (newmap == NULL) { if (map == NULL) {
fprintf(stderr, "Not enough memory"); fprintf(stderr, "Not enough memory");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else if (mappings == NULL) }
mappings = newmap;
else
map->next = newmap;
map = newmap;
strncpy(map->guid, guid, sizeof(map->guid)); strncpy(map->guid, guid, sizeof(map->guid));
strncpy(map->name, name, sizeof(map->name)); strncpy(map->name, name, sizeof(map->name));
@@ -146,6 +129,26 @@ struct mapping* mapping_load(char* fileName, bool verbose) {
map->guid[32] = '\0'; map->guid[32] = '\0';
map->name[256] = '\0'; map->name[256] = '\0';
map->platform[32] = '\0'; map->platform[32] = '\0';
return map;
}
struct mapping* mapping_load(char* fileName, bool verbose) {
struct mapping* mappings = NULL;
FILE* fd = fopen(fileName, "r");
if (fd == NULL) {
fprintf(stderr, "Can't open mapping file: %s\n", fileName);
exit(EXIT_FAILURE);
} else if (verbose)
printf("Loading mappingfile %s\n", fileName);
char *line = NULL;
size_t len = 0;
while (getline(&line, &len, fd) != -1) {
struct mapping* map = mapping_parse(line);
if (map) {
map->next = mappings;
mappings = map;
}
} }
free(line); free(line);
+1
View File
@@ -47,4 +47,5 @@ struct mapping {
struct mapping* next; struct mapping* next;
}; };
struct mapping* mapping_parse(char* mapping);
struct mapping* mapping_load(char* fileName, bool verbose); struct mapping* mapping_load(char* fileName, bool verbose);
+12 -2
View File
@@ -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)); config.stream.supportsHevc = config.codec != CODEC_H264 && (config.codec == CODEC_HEVC || platform_supports_hevc(system));
if (IS_EMBEDDED(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"); fprintf(stderr, "Please specify mapping file as default mapping could not be found.\n");
exit(-1); 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++) { for (int i=0;i<config.inputsCount;i++) {
if (config.debug_level > 0) if (config.debug_level > 0)