forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput_remapping.c
222 lines (184 loc) · 6.85 KB
/
input_remapping.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2016 - 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 <file/config_file.h>
#include <file/file_path.h>
#include <string/stdstring.h>
#include "input_remapping.h"
#include "../general.h"
/**
* input_remapping_load_file:
* @data : Path to config file.
*
* 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)
{
unsigned i, j;
config_file_t *conf = (config_file_t*)data;
settings_t *settings = config_get_ptr();
global_t *global = global_get_ptr();
if (!conf || string_is_empty(path))
return false;
strlcpy(global->name.remapfile, path,
sizeof(global->name.remapfile));
for (i = 0; i < MAX_USERS; i++)
{
char buf[64];
char key_ident[RARCH_FIRST_CUSTOM_BIND + 4][128] = {{0}};
char key_strings[RARCH_FIRST_CUSTOM_BIND + 4][128] =
{ "b", "y", "select", "start",
"up", "down", "left", "right",
"a", "x", "l", "r", "l2", "r2",
"l3", "r3", "l_x", "l_y", "r_x", "r_y" };
snprintf(buf, sizeof(buf), "input_player%u", i + 1);
for (j = 0; j < RARCH_FIRST_CUSTOM_BIND + 4; j++)
{
int key_remap = -1;
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)
settings->input.remap_ids[i][j] = key_remap;
}
for (j = 0; j < 4; j++)
{
int key_remap = -1;
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]);
if (config_get_int(conf, key_ident[RARCH_FIRST_CUSTOM_BIND + j],
&key_remap) && (key_remap < 4))
settings->input.remap_ids[i][RARCH_FIRST_CUSTOM_BIND + j] =
key_remap;
}
snprintf(buf, sizeof(buf), "input_player%u_joypad_index", i + 1);
CONFIG_GET_INT_BASE(conf, settings, input.joypad_map[i], buf);
snprintf(buf, sizeof(buf), "input_player%u_analog_dpad_mode", i + 1);
CONFIG_GET_INT_BASE(conf, settings, input.analog_dpad_mode[i], buf);
snprintf(buf, sizeof(buf), "input_player%u_joypad_index", i + 1);
CONFIG_GET_INT_BASE(conf, settings, input.joypad_map[i], buf);
snprintf(buf, sizeof(buf), "input_player%u_analog_dpad_mode", i + 1);
CONFIG_GET_INT_BASE(conf, settings, input.analog_dpad_mode[i], buf);
if (!global->has_set.libretro_device[i])
{
snprintf(buf, sizeof(buf), "input_libretro_device_p%u", i + 1);
CONFIG_GET_INT_BASE(conf, settings, input.libretro_device[i], buf);
}
}
config_file_free(conf);
return true;
}
/**
* input_remapping_save_file:
* @path : Path to remapping file (relative path).
*
* Saves remapping values to file.
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool input_remapping_save_file(const char *path)
{
bool ret;
unsigned i, j;
char buf[PATH_MAX_LENGTH] = {0};
char remap_file[PATH_MAX_LENGTH] = {0};
config_file_t *conf = NULL;
settings_t *settings = config_get_ptr();
fill_pathname_join(buf, settings->directory.input_remapping,
path, sizeof(buf));
fill_pathname_noext(remap_file, buf, ".rmp", sizeof(remap_file));
conf = config_file_new(remap_file);
if (!conf)
{
conf = config_file_new(NULL);
if (!conf)
return false;
}
for (i = 0; i < settings->input.max_users; i++)
{
char buf[64] = {0};
char key_ident[RARCH_FIRST_CUSTOM_BIND + 4][128] = {{0}};
char key_strings[RARCH_FIRST_CUSTOM_BIND + 4][128] = {
"b", "y", "select", "start",
"up", "down", "left", "right",
"a", "x", "l", "r", "l2", "r2",
"l3", "r3", "l_x", "l_y", "r_x", "r_y" };
snprintf(buf, sizeof(buf), "input_player%u", i + 1);
for (j = 0; j < RARCH_FIRST_CUSTOM_BIND + 4; j++)
{
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)
{
if(settings->input.remap_ids[i][j] != j)
config_set_int(conf, key_ident[j], settings->input.remap_ids[i][j]);
else
config_unset(conf,key_ident[j]);
}
else
{
if(settings->input.remap_ids[i][j] != j - RARCH_FIRST_CUSTOM_BIND)
config_set_int(conf, key_ident[j], settings->input.remap_ids[i][j]);
else
config_unset(conf,key_ident[j]);
}
}
snprintf(buf, sizeof(buf), "input_libretro_device_p%u", i + 1);
config_set_int(conf, buf, settings->input.libretro_device[i]);
snprintf(buf, sizeof(buf), "input_player%u_analog_dpad_mode", i + 1);
config_set_int(conf, buf, settings->input.analog_dpad_mode[i]);
}
ret = config_file_write(conf, remap_file);
config_file_free(conf);
return ret;
}
void input_remapping_set_defaults(void)
{
unsigned i, j;
settings_t *settings = config_get_ptr();
for (i = 0; i < MAX_USERS; i++)
{
for (j = 0; j < RARCH_FIRST_CUSTOM_BIND; j++)
settings->input.remap_ids[i][j] = settings->input.binds[i][j].id;
for (j = 0; j < 4; j++)
settings->input.remap_ids[i][RARCH_FIRST_CUSTOM_BIND + j] = j;
}
}
void input_remapping_state(unsigned port,
unsigned *device, unsigned *idx, unsigned *id)
{
settings_t *settings = config_get_ptr();
switch (*device)
{
case RETRO_DEVICE_JOYPAD:
if (*id < RARCH_FIRST_CUSTOM_BIND)
*id = settings->input.remap_ids[port][*id];
break;
case RETRO_DEVICE_ANALOG:
if (*idx < 2 && *id < 2)
{
unsigned new_id = RARCH_FIRST_CUSTOM_BIND + (*idx * 2 + *id);
new_id = settings->input.remap_ids[port][new_id];
*idx = (new_id & 2) >> 1;
*id = new_id & 1;
}
break;
}
}