RetroArch/input/input_remapping.c

316 lines
11 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>
#include <streams/file_stream.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
{
2018-04-12 21:39:31 +02:00
unsigned i, j, k;
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;
2017-10-04 06:53:47 +02:00
if (!string_is_empty(global->name.remapfile))
input_remapping_set_defaults(true);
2017-09-29 20:47:54 +02:00
global->name.remapfile = strdup(path);
2015-01-24 23:42:31 +01:00
2015-01-19 18:16:34 +01:00
for (i = 0; i < MAX_USERS; i++)
{
char s1[64], s2[64], s3[64];
2018-04-12 21:39:31 +02:00
char btn_ident[RARCH_FIRST_CUSTOM_BIND][128] = {{0}};
char key_ident[RARCH_FIRST_CUSTOM_BIND][128] = {{0}};
char stk_ident[8][128] = {{0}};
char key_strings[RARCH_FIRST_CUSTOM_BIND + 8][128] = {
"b", "y", "select", "start",
"up", "down", "left", "right",
"a", "x", "l", "r", "l2", "r2",
"l3", "r3", "l_x+", "l_x-", "l_y+", "l_y-", "r_x+", "r_x-", "r_y+", "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';
s3[0] = '\0';
2017-09-09 17:19:04 -05:00
snprintf(s1, sizeof(s1), "input_player%u_btn", i + 1);
2017-09-09 17:19:04 -05:00
snprintf(s2, sizeof(s2), "input_player%u_key", i + 1);
snprintf(s3, sizeof(s3), "input_player%u_stk", i + 1);
2015-01-19 18:16:34 +01:00
2018-04-12 21:39:31 +02:00
for (j = 0; j < RARCH_FIRST_CUSTOM_BIND + 8; j++)
2015-01-19 18:16:34 +01:00
{
2018-04-12 21:39:31 +02:00
if (j < RARCH_FIRST_CUSTOM_BIND)
{
int btn_remap = -1;
int key_remap = -1;
fill_pathname_join_delim(btn_ident[j], s1,
key_strings[j], '_', sizeof(btn_ident[j]));
fill_pathname_join_delim(key_ident[j], s2,
key_strings[j], '_', sizeof(btn_ident[j]));
if (config_get_int(conf, btn_ident[j], &btn_remap)
&& btn_remap != -1)
settings->uints.input_remap_ids[i][j] = btn_remap;
else if (config_get_int(conf, btn_ident[j], &btn_remap)
&& btn_remap == -1)
settings->uints.input_remap_ids[i][j] = RARCH_UNMAPPED;
/* else do nothing, important */
if (config_get_int(conf, key_ident[j], &key_remap))
settings->uints.input_keymapper_ids[i][j] = key_remap;
else
settings->uints.input_keymapper_ids[i][j] = RETROK_UNKNOWN;
}
else
2018-04-12 21:39:31 +02:00
{
int stk_remap = -1;
k = j - RARCH_FIRST_CUSTOM_BIND;
2018-04-12 21:39:31 +02:00
fill_pathname_join_delim(stk_ident[k], s3,
key_strings[j], '$', sizeof(stk_ident[k]));
2015-09-29 17:35:28 +02:00
2018-04-12 21:39:31 +02:00
snprintf(stk_ident[k],
sizeof(stk_ident[k]),
"%s_%s",
s3,
key_strings[j]);
2018-04-12 21:39:31 +02:00
/* RARCH_LOG("pre_ident: %s:%d\n", stk_ident[j], settings->uints.input_remap_ids[i][j]); */
2018-04-12 21:39:31 +02:00
if (config_get_int(conf, stk_ident[k], &stk_remap) && stk_remap != -1)
settings->uints.input_remap_ids[i][j] = stk_remap;
else if (config_get_int(conf, stk_ident[k], &stk_remap) && stk_remap == -1)
settings->uints.input_remap_ids[i][j] = RARCH_UNMAPPED;
/* else do nothing, important */
2018-04-12 21:39:31 +02:00
/*RARCH_LOG("stk_ident: %s:%d\n", stk_ident[j], settings->uints.input_remap_ids[i][j]);*/
}
}
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;
2018-04-12 21:39:31 +02:00
unsigned i, j, k;
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
{
char s1[64], s2[64], s3[64];
2018-04-12 21:39:31 +02:00
char btn_ident[RARCH_FIRST_CUSTOM_BIND][128] = {{0}};
char key_ident[RARCH_FIRST_CUSTOM_BIND][128] = {{0}};
char stk_ident[8][128] = {{0}};
char key_strings[RARCH_FIRST_CUSTOM_BIND + 8][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_x-", "l_y+", "l_y-", "r_x+", "r_x-", "r_y+", "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
snprintf(s1, sizeof(s1), "input_player%u_btn", i + 1);
2017-09-09 17:19:04 -05:00
snprintf(s2, sizeof(s2), "input_player%u_key", i + 1);
snprintf(s3, sizeof(s1), "input_player%u_stk", i + 1);
2015-01-19 18:16:34 +01:00
for (j = 0; j < RARCH_FIRST_CUSTOM_BIND + 8; j++)
2015-01-19 18:16:34 +01:00
{
if(j < RARCH_FIRST_CUSTOM_BIND)
{
2018-04-12 21:39:31 +02:00
fill_pathname_join_delim(btn_ident[j], s1,
key_strings[j], '_', sizeof(btn_ident[j]));
fill_pathname_join_delim(key_ident[j], s2,
key_strings[j], '_', sizeof(btn_ident[j]));
/* only save values that have been modified */
if(settings->uints.input_remap_ids[i][j] != j &&
settings->uints.input_remap_ids[i][j] != RARCH_UNMAPPED)
config_set_int(conf, btn_ident[j], settings->uints.input_remap_ids[i][j]);
else if (settings->uints.input_remap_ids[i][j] != j &&
settings->uints.input_remap_ids[i][j] == RARCH_UNMAPPED)
config_set_int(conf, btn_ident[j], -1);
else
config_unset(conf,btn_ident[j]);
2017-09-09 17:19:04 -05:00
if (settings->uints.input_keymapper_ids[i][j] != RETROK_UNKNOWN)
config_set_int(conf, key_ident[j],
settings->uints.input_keymapper_ids[i][j]);
}
else
{
2018-04-12 21:39:31 +02:00
k = j - RARCH_FIRST_CUSTOM_BIND;
fill_pathname_join_delim(stk_ident[k], s3,
key_strings[j], '_', sizeof(stk_ident[k]));
if(settings->uints.input_remap_ids[i][j] != j &&
settings->uints.input_remap_ids[i][j] != RARCH_UNMAPPED)
2018-04-12 21:39:31 +02:00
config_set_int(conf, stk_ident[k],
settings->uints.input_remap_ids[i][j]);
else if(settings->uints.input_remap_ids[i][j] != j &&
settings->uints.input_remap_ids[i][j] == RARCH_UNMAPPED)
2018-04-12 21:39:31 +02:00
config_set_int(conf, stk_ident[k],
-1);
else
2018-04-19 09:54:16 -05:00
config_unset(conf, stk_ident[k]);
}
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
ret = filestream_delete(remap_file) == 0 ? true : false;
2017-09-09 23:03:28 +02:00
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)
{
2017-10-04 06:53:47 +02:00
if (!string_is_empty(global->name.remapfile))
2017-09-29 20:47:54 +02:00
free(global->name.remapfile);
global->name.remapfile = NULL;
2017-09-12 01:47:43 -05:00
rarch_ctl(RARCH_CTL_UNSET_REMAPS_CORE_ACTIVE, NULL);
rarch_ctl(RARCH_CTL_UNSET_REMAPS_CONTENT_DIR_ACTIVE, NULL);
2017-09-12 01:47:43 -05:00
rarch_ctl(RARCH_CTL_UNSET_REMAPS_GAME_ACTIVE, NULL);
}
for (i = 0; i < MAX_USERS; i++)
{
2018-04-12 21:39:31 +02:00
for (j = 0; j < RARCH_FIRST_CUSTOM_BIND + 8; j++)
{
2018-04-12 21:39:31 +02:00
if (j < RARCH_FIRST_CUSTOM_BIND)
{
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[i][j] = RETROK_UNKNOWN;
}
else
settings->uints.input_remap_ids[i][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];
}
}