From 3e2c5b1789d5baad2bb054c7e84da5fe5168f92c Mon Sep 17 00:00:00 2001 From: Iwan Timmer Date: Mon, 19 Jun 2017 22:44:33 +0200 Subject: [PATCH] Add support for SDL_GAMECONTROLLERCONFIG environment variable --- src/input/mapping.c | 221 ++++++++++++++++++++++---------------------- src/input/mapping.h | 1 + src/main.c | 14 ++- 3 files changed, 125 insertions(+), 111 deletions(-) diff --git a/src/input/mapping.c b/src/input/mapping.c index 51bbe01d..ac8a935f 100644 --- a/src/input/mapping.c +++ b/src/input/mapping.c @@ -24,9 +24,116 @@ #include #include +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); diff --git a/src/input/mapping.h b/src/input/mapping.h index 84d73ee1..a82b6c99 100644 --- a/src/input/mapping.h +++ b/src/input/mapping.h @@ -47,4 +47,5 @@ struct mapping { struct mapping* next; }; +struct mapping* mapping_parse(char* mapping); struct mapping* mapping_load(char* fileName, bool verbose); diff --git a/src/main.c b/src/main.c index 695d1298..54f4c8cb 100644 --- a/src/main.c +++ b/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 0)