diff --git a/src/mapping.c b/src/mapping.c new file mode 100644 index 0000000..335e537 --- /dev/null +++ b/src/mapping.c @@ -0,0 +1,143 @@ +/* + * This file is part of Moonlight Embedded. + * + * Copyright (C) 2015 Iwan Timmer + * + * Moonlight is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Moonlight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Moonlight; if not, see . + */ + +#include "mapping.h" + +#include +#include +#include + +#define write_config(fd, key, value) fprintf(fd, "%s = %d\n", key, value) + +void mapping_load(char* fileName, struct mapping* map) { + FILE* fd = fopen(fileName, "r"); + if (fd == NULL) { + fprintf(stderr, "Can't open mapping file: %s\n", fileName); + exit(EXIT_FAILURE); + } + + char *line = NULL; + size_t len = 0; + while (getline(&line, &len, fd) != -1) { + short value; + char* key = NULL; + if (sscanf(line, "%ms = %hd", &key, &value) == 2) { + if (strcmp("abs_x", key) == 0) + map->abs_x = value; + else if (strcmp("abs_y", key) == 0) + map->abs_y = value; + else if (strcmp("abs_z", key) == 0) + map->abs_z = value; + else if (strcmp("abs_rx", key) == 0) + map->abs_rx = value; + else if (strcmp("abs_ry", key) == 0) + map->abs_ry = value; + else if (strcmp("abs_rz", key) == 0) + map->abs_rz = value; + else if (strcmp("abs_deadzone", key) == 0) + map->abs_deadzone = value; + else if (strcmp("abs_dpad_x", key) == 0) + map->abs_dpad_x = value; + else if (strcmp("abs_dpad_y", key) == 0) + map->abs_dpad_y = value; + else if (strcmp("btn_south", key) == 0) + map->btn_south = value; + else if (strcmp("btn_north", key) == 0) + map->btn_north = value; + else if (strcmp("btn_east", key) == 0) + map->btn_east = value; + else if (strcmp("btn_west", key) == 0) + map->btn_west = value; + else if (strcmp("btn_select", key) == 0) + map->btn_select = value; + else if (strcmp("btn_start", key) == 0) + map->btn_start = value; + else if (strcmp("btn_mode", key) == 0) + map->btn_mode = value; + else if (strcmp("btn_thumbl", key) == 0) + map->btn_thumbl = value; + else if (strcmp("btn_thumbr", key) == 0) + map->btn_thumbr = value; + else if (strcmp("btn_tl", key) == 0) + map->btn_tl = value; + else if (strcmp("btn_tr", key) == 0) + map->btn_tr = value; + else if (strcmp("btn_tl2", key) == 0) + map->btn_tl2 = value; + else if (strcmp("btn_tr2", key) == 0) + map->btn_tr2 = value; + else if (strcmp("btn_dpad_up", key) == 0) + map->btn_dpad_up = value; + else if (strcmp("btn_dpad_down", key) == 0) + map->btn_dpad_down = value; + else if (strcmp("btn_dpad_left", key) == 0) + map->btn_dpad_left = value; + else if (strcmp("btn_dpad_right", key) == 0) + map->btn_dpad_right = value; + else + printf("Can't map %s\n", key); + } + if (key != NULL) + free(key); + } + free(line); +} + +void mapping_save(char* fileName, struct mapping* map) { + FILE* fd = fopen(fileName, "w"); + if (fd == NULL) { + fprintf(stderr, "Can't open mapping file: %s\n", fileName); + exit(EXIT_FAILURE); + } + + write_config(fd, "abx_x", map->abs_x); + write_config(fd, "abx_y", map->abs_y); + write_config(fd, "abx_z", map->abs_z); + + write_config(fd, "abx_rx", map->abs_rx); + write_config(fd, "abx_ry", map->abs_ry); + write_config(fd, "abx_rz", map->abs_rz); + + write_config(fd, "abx_deadzone", map->abs_deadzone); + + write_config(fd, "abx_dpad_x", map->abs_dpad_x); + write_config(fd, "abx_dpad_y", map->abs_dpad_y); + + write_config(fd, "btn_north", map->btn_north); + write_config(fd, "btn_east", map->btn_east); + write_config(fd, "btn_south", map->btn_south); + write_config(fd, "btn_west", map->btn_west); + + write_config(fd, "btn_select", map->btn_select); + write_config(fd, "btn_start", map->btn_start); + write_config(fd, "btn_mode", map->btn_mode); + + write_config(fd, "btn_thumbl", map->btn_thumbl); + write_config(fd, "btn_thumbr", map->btn_thumbr); + + write_config(fd, "btn_tl", map->btn_tl); + write_config(fd, "btn_tr", map->btn_tr); + write_config(fd, "btn_tl2", map->btn_tl2); + write_config(fd, "btn_tr2", map->btn_tr2); + + write_config(fd, "btn_dpad_up", map->btn_dpad_up); + write_config(fd, "btn_dpad_down", map->btn_dpad_down); + write_config(fd, "btn_dpad_left", map->btn_dpad_left); + write_config(fd, "btn_dpad_right", map->btn_dpad_right); +} diff --git a/src/mapping.h b/src/mapping.h new file mode 100644 index 0000000..5c4f51b --- /dev/null +++ b/src/mapping.h @@ -0,0 +1,37 @@ +/* + * This file is part of Moonlight Embedded. + * + * Copyright (C) 2015 Iwan Timmer + * + * Moonlight is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Moonlight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Moonlight; if not, see . + */ + +struct mapping { + short abs_x, abs_y, abs_z; + short abs_rx, abs_ry, abs_rz; + + short abs_deadzone; + + short abs_dpad_x, abs_dpad_y; + + short btn_south, btn_east, btn_north, btn_west; + short btn_select, btn_start, btn_mode; + short btn_thumbl, btn_thumbr; + short btn_tl, btn_tr, btn_tl2, btn_tr2; + + short btn_dpad_up, btn_dpad_down, btn_dpad_left, btn_dpad_right; +}; + +void mapping_load(char* fileName, struct mapping* map); +void mapping_save(char* fileName, struct mapping* map);