Skip to content

Commit

Permalink
Add support for SDL_GAMECONTROLLERCONFIG environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
irtimmer committed Jun 19, 2017
1 parent c23d95f commit 3e2c5b1
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 111 deletions.
221 changes: 112 additions & 109 deletions src/input/mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/input/mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ struct mapping {
struct mapping* next;
};

struct mapping* mapping_parse(char* mapping);
struct mapping* mapping_load(char* fileName, bool verbose);
14 changes: 12 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3e2c5b1

Please sign in to comment.