RetroArch/input/input_remapping.c

129 lines
3.8 KiB
C
Raw Normal View History

2015-01-19 17:16:34 +00:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2015 - Daniel De Matteis
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "input_remapping.h"
#include <file/config_file.h>
#include <file/file_path.h>
#include "../general.h"
/**
* input_remapping_load_file:
* @path : Path to remapping file (absolute path).
*
* Loads a remap file from disk to memory.
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool input_remapping_load_file(const char *path)
{
unsigned i, j;
2015-03-20 20:22:38 +00:00
config_file_t *conf = config_file_new(path);
settings_t *settings = config_get_ptr();
2015-01-19 17:16:34 +00:00
if (!conf)
return false;
2015-03-20 20:22:38 +00:00
strlcpy(settings->input.remapping_path, path,
sizeof(settings->input.remapping_path));
2015-01-24 22:42:31 +00:00
2015-01-19 17:16:34 +00:00
for (i = 0; i < MAX_USERS; i++)
{
char buf[64];
char key_ident[RARCH_FIRST_META_KEY][128];
char key_strings[RARCH_FIRST_META_KEY][128] = { "b", "y", "select", "start",
2015-04-09 22:49:40 +00:00
"up", "down", "left", "right", "a", "x", "l", "r", "l2", "r2", "l3", "r3", "l_x_plus", "l_x_minus", "l_y_plus", "l_y_minus", "r_x_plus", "r_x_minus", "r_y_plus", "r_y_minus"};
2015-01-19 17:16:34 +00:00
snprintf(buf, sizeof(buf), "input_player%u", i + 1);
for (j = 0; j < RARCH_FIRST_META_KEY; j++)
{
int key_remap = -1;
snprintf(key_ident[j], sizeof(key_ident[j]), "%s_%s", buf, key_strings[j]);
if (config_get_int(conf, key_ident[j], &key_remap))
2015-03-20 20:22:38 +00:00
settings->input.remap_ids[i][j] = key_remap;
2015-01-19 17:16:34 +00:00
}
}
config_file_free(conf);
return true;
}
/**
* input_remapping_save_file:
* @path : Path to remapping file (relative path).
*
* Saves remapping values to file.
2015-04-08 03:51:31 +00:00
*
* Returns: true (1) if successful, otherwise false (0).
2015-01-19 17:16:34 +00:00
**/
2015-04-08 03:40:42 +00:00
bool input_remapping_save_file(const char *path)
2015-01-19 17:16:34 +00:00
{
2015-04-08 03:40:42 +00:00
bool ret;
2015-01-19 17:16:34 +00:00
unsigned i, j;
char buf[PATH_MAX_LENGTH];
char remap_file[PATH_MAX_LENGTH];
config_file_t *conf = NULL;
2015-03-20 20:22:38 +00:00
settings_t *settings = config_get_ptr();
2015-01-19 17:16:34 +00:00
2015-03-20 20:22:38 +00:00
fill_pathname_join(buf, settings->input_remapping_directory,
2015-01-19 17:16:34 +00:00
path, sizeof(buf));
fill_pathname_noext(remap_file, buf, ".rmp", sizeof(remap_file));
conf = config_file_new(remap_file);
if (!conf)
2015-04-08 03:40:42 +00:00
{
2015-01-19 17:16:34 +00:00
conf = config_file_new(NULL);
2015-04-08 03:40:42 +00:00
return false;
}
2015-01-19 17:16:34 +00:00
2015-03-20 20:22:38 +00:00
for (i = 0; i < settings->input.max_users; i++)
2015-01-19 17:16:34 +00:00
{
char key_ident[RARCH_FIRST_META_KEY][128];
char key_strings[RARCH_FIRST_META_KEY][128] = { "b", "y", "select", "start",
2015-04-09 22:49:40 +00:00
"up", "down", "left", "right", "a", "x", "l", "r", "l2", "r2", "l3", "r3", "l_x_plus" , "l_x_minus", "l_y_plus", "l_y_minus", "r_x_plus", "r_x_minus", "r_y_plus", "r_y_minus" };
2015-01-19 17:16:34 +00:00
snprintf(buf, sizeof(buf), "input_player%u", i + 1);
for (j = 0; j < RARCH_FIRST_META_KEY; j++)
{
snprintf(key_ident[j], sizeof(key_ident[j]), "%s_%s", buf, key_strings[j]);
2015-03-20 20:22:38 +00:00
config_set_int(conf, key_ident[j], settings->input.remap_ids[i][j]);
2015-01-19 17:16:34 +00:00
}
}
2015-04-08 03:40:42 +00:00
ret = config_file_write(conf, remap_file);
2015-01-19 17:16:34 +00:00
config_file_free(conf);
2015-04-08 03:51:31 +00:00
2015-04-08 03:40:42 +00:00
return ret;
2015-01-19 17:16:34 +00:00
}
void input_remapping_set_defaults(void)
{
unsigned i, j;
2015-03-20 20:22:38 +00:00
settings_t *settings = config_get_ptr();
for (i = 0; i < MAX_USERS; i++)
{
for (j = 0; j < RARCH_BIND_LIST_END; j++)
2015-03-20 20:22:38 +00:00
settings->input.remap_ids[i][j] = settings->input.binds[i][j].id;
}
}