RetroArch/input/input_remapping.c

284 lines
9.1 KiB
C
Raw Normal View History

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