RetroArch/input/input_remapping.c

224 lines
7.0 KiB
C
Raw Normal View History

2015-01-19 17:16:34 +00:00
/* RetroArch - A frontend for libretro.
2017-01-22 12:40:32 +00:00
* Copyright (C) 2011-2017 - Daniel De Matteis
2017-08-05 05:37:26 +00:00
* Copyright (C) 2015-2017 - Andrés Suárez
2015-01-19 17:16:34 +00:00
*
* 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/>.
*/
2016-09-01 16:07:44 +00:00
#include <compat/strl.h>
2015-01-19 17:16:34 +00:00
#include <file/config_file.h>
#include <file/file_path.h>
2015-12-26 06:54:17 +00:00
#include <string/stdstring.h>
2017-04-25 16:48:06 +00:00
#include "input_config.h"
2015-12-26 06:54:17 +00:00
#include "input_remapping.h"
2016-09-05 16:31:32 +00:00
#include "../configuration.h"
2016-10-01 06:24:02 +00:00
#include "../retroarch.h"
2015-01-19 17:16:34 +00:00
static unsigned old_analog_dpad_mode[MAX_USERS];
static unsigned old_libretro_device[MAX_USERS];
2015-01-19 17:16:34 +00:00
/**
* input_remapping_load_file:
* @data : Path to config file.
2015-01-19 17:16:34 +00:00
*
* Loads a remap file from disk to memory.
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool input_remapping_load_file(void *data, const char *path)
2015-01-19 17:16:34 +00:00
{
unsigned i, j;
config_file_t *conf = (config_file_t*)data;
2015-03-20 20:22:38 +00:00
settings_t *settings = config_get_ptr();
2016-10-01 06:24:02 +00:00
global_t *global = global_get_ptr();
2015-01-19 17:16:34 +00:00
2015-12-26 06:54:17 +00:00
if (!conf || string_is_empty(path))
2015-01-19 17:16:34 +00:00
return false;
strlcpy(global->name.remapfile, path,
sizeof(global->name.remapfile));
2015-01-24 22:42:31 +00:00
2015-01-19 17:16:34 +00:00
for (i = 0; i < MAX_USERS; i++)
{
2016-02-05 13:11:38 +00:00
char buf[64];
2015-06-12 21:52:52 +00:00
char key_ident[RARCH_FIRST_CUSTOM_BIND + 4][128] = {{0}};
char key_strings[RARCH_FIRST_CUSTOM_BIND + 4][128] =
2016-02-05 13:06:43 +00:00
{ "b", "y", "select", "start",
"up", "down", "left", "right",
"a", "x", "l", "r", "l2", "r2",
2016-02-05 13:06:43 +00:00
"l3", "r3", "l_x", "l_y", "r_x", "r_y" };
2015-01-19 17:16:34 +00:00
2017-08-06 15:09:30 +00:00
old_analog_dpad_mode[i] = settings->uints.input_analog_dpad_mode[i];
old_libretro_device[i] = settings->uints.input_libretro_device[i];
2015-01-19 17:16:34 +00:00
snprintf(buf, sizeof(buf), "input_player%u", i + 1);
for (j = 0; j < RARCH_FIRST_CUSTOM_BIND + 4; j++)
2015-01-19 17:16:34 +00:00
{
int key_remap = -1;
2016-02-05 13:06:43 +00:00
fill_pathname_join_delim(key_ident[j], buf,
key_strings[j], '_', sizeof(key_ident[j]));
if (config_get_int(conf, key_ident[j], &key_remap)
&& key_remap < RARCH_FIRST_CUSTOM_BIND)
2017-04-28 19:03:04 +00:00
settings->uints.input_remap_ids[i][j] = key_remap;
2015-01-19 17:16:34 +00:00
}
for (j = 0; j < 4; j++)
{
int key_remap = -1;
2015-09-29 15:35:28 +00:00
snprintf(key_ident[RARCH_FIRST_CUSTOM_BIND + j],
sizeof(key_ident[RARCH_FIRST_CUSTOM_BIND + j]),
"%s_%s",
buf,
key_strings[RARCH_FIRST_CUSTOM_BIND + j]);
2016-02-05 13:06:43 +00:00
if (config_get_int(conf, key_ident[RARCH_FIRST_CUSTOM_BIND + j],
&key_remap) && (key_remap < 4))
2017-04-28 19:03:04 +00:00
settings->uints.input_remap_ids[i][RARCH_FIRST_CUSTOM_BIND + j] =
2016-02-05 13:06:43 +00:00
key_remap;
}
snprintf(buf, sizeof(buf), "input_player%u_analog_dpad_mode", i + 1);
CONFIG_GET_INT_BASE(conf, settings, uints.input_analog_dpad_mode[i], buf);
snprintf(buf, sizeof(buf), "input_libretro_device_p%u", i + 1);
CONFIG_GET_INT_BASE(conf, settings, uints.input_libretro_device[i], buf);
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;
2016-12-17 13:40:06 +00:00
char buf[PATH_MAX_LENGTH];
char remap_file[PATH_MAX_LENGTH];
2015-06-12 21:52:52 +00:00
config_file_t *conf = NULL;
2017-05-11 05:36:21 +00:00
unsigned max_users = *(input_driver_get_uint(INPUT_ACTION_MAX_USERS));
2015-06-12 21:52:52 +00:00
settings_t *settings = config_get_ptr();
2015-01-19 17:16:34 +00:00
2016-12-17 13:40:06 +00:00
buf[0] = remap_file[0] = '\0';
2017-04-28 22:39:29 +00:00
fill_pathname_join(buf, settings->paths.directory_input_remapping,
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
{
conf = config_file_new(NULL);
2015-11-17 05:23:14 +00:00
if (!conf)
return false;
2015-04-08 03:40:42 +00:00
}
2015-01-19 17:16:34 +00:00
2017-05-11 05:36:21 +00:00
for (i = 0; i < max_users; i++)
2015-01-19 17:16:34 +00:00
{
2016-12-17 13:40:06 +00:00
char buf[64];
2015-06-12 21:52:52 +00:00
char key_ident[RARCH_FIRST_CUSTOM_BIND + 4][128] = {{0}};
char key_strings[RARCH_FIRST_CUSTOM_BIND + 4][128] = {
2016-02-05 13:06:43 +00:00
"b", "y", "select", "start",
"up", "down", "left", "right",
"a", "x", "l", "r", "l2", "r2",
"l3", "r3", "l_x", "l_y", "r_x", "r_y" };
2015-01-19 17:16:34 +00:00
2016-12-17 13:40:06 +00:00
buf[0] = '\0';
2015-01-19 17:16:34 +00:00
snprintf(buf, sizeof(buf), "input_player%u", i + 1);
for (j = 0; j < RARCH_FIRST_CUSTOM_BIND + 4; j++)
2015-01-19 17:16:34 +00:00
{
2016-02-05 13:06:43 +00:00
fill_pathname_join_delim(key_ident[j], buf,
key_strings[j], '_', sizeof(key_ident[j]));
/* only save values that have been modified */
if(j < RARCH_FIRST_CUSTOM_BIND)
{
2017-04-28 19:03:04 +00:00
if(settings->uints.input_remap_ids[i][j] != j)
config_set_int(conf, key_ident[j], settings->uints.input_remap_ids[i][j]);
else
config_unset(conf,key_ident[j]);
}
else
{
2017-04-28 19:03:04 +00:00
if(settings->uints.input_remap_ids[i][j] != j - RARCH_FIRST_CUSTOM_BIND)
config_set_int(conf, key_ident[j], settings->uints.input_remap_ids[i][j]);
else
config_unset(conf,key_ident[j]);
}
2015-01-19 17:16:34 +00:00
}
snprintf(buf, sizeof(buf), "input_libretro_device_p%u", i + 1);
config_set_int(conf, buf, input_config_get_device(i));
snprintf(buf, sizeof(buf), "input_player%u_analog_dpad_mode", i + 1);
config_set_int(conf, buf, settings->uints.input_analog_dpad_mode[i]);
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
}
2017-08-05 05:37:26 +00:00
bool input_remapping_remove_file(const char *path)
{
char buf[PATH_MAX_LENGTH];
char remap_file[PATH_MAX_LENGTH];
settings_t *settings = config_get_ptr();
buf[0] = remap_file[0] = '\0';
fill_pathname_join(buf, settings->paths.directory_input_remapping,
path, sizeof(buf));
fill_pathname_noext(remap_file, buf, ".rmp", sizeof(remap_file));
return remove(remap_file) == 0 ? true : false;
}
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_FIRST_CUSTOM_BIND; j++)
2017-04-25 16:48:06 +00:00
{
const struct retro_keybind *keybind = &input_config_binds[i][j];
if (keybind)
settings->uints.input_remap_ids[i][j] = keybind->id;
2017-04-25 16:48:06 +00:00
}
for (j = 0; j < 4; j++)
2017-04-28 19:03:04 +00:00
settings->uints.input_remap_ids[i][RARCH_FIRST_CUSTOM_BIND + j] = j;
2017-08-07 00:55:44 +00:00
if (old_analog_dpad_mode[i])
settings->uints.input_analog_dpad_mode[i] = old_analog_dpad_mode[i];
if (old_libretro_device[i])
settings->uints.input_libretro_device[i] = old_libretro_device[i];
}
}