forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_options.c
413 lines (343 loc) · 9.19 KB
/
core_options.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* 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 <string.h>
#include <file/config_file.h>
#include <lists/dir_list.h>
#include <lists/string_list.h>
#include <compat/posix_string.h>
#include <compat/strl.h>
#include <retro_miscellaneous.h>
#include <string/stdstring.h>
#include "libretro.h"
#include "core_options.h"
struct core_option
{
char *desc;
char *key;
struct string_list *vals;
size_t index;
};
struct core_option_manager
{
config_file_t *conf;
char conf_path[PATH_MAX_LENGTH];
struct core_option *opts;
size_t size;
bool updated;
};
/**
* core_option_free:
* @opt : options manager handle
*
* Frees core option manager handle.
**/
void core_option_free(core_option_manager_t *opt)
{
size_t i;
if (!opt)
return;
for (i = 0; i < opt->size; i++)
{
if (opt->opts[i].desc)
free(opt->opts[i].desc);
if (opt->opts[i].key)
free(opt->opts[i].key);
if (opt->opts[i].vals)
string_list_free(opt->opts[i].vals);
opt->opts[i].desc = NULL;
opt->opts[i].key = NULL;
opt->opts[i].vals = NULL;
}
if (opt->conf)
config_file_free(opt->conf);
free(opt->opts);
free(opt);
}
void core_option_get(core_option_manager_t *opt, void *data)
{
size_t i;
struct retro_variable *var = (struct retro_variable*)data;
if (!opt)
return;
opt->updated = false;
for (i = 0; i < opt->size; i++)
{
if (string_is_empty(opt->opts[i].key))
continue;
if (string_is_equal(opt->opts[i].key, var->key))
{
var->value = core_option_get_val(opt, i);
return;
}
}
var->value = NULL;
}
static bool parse_variable(core_option_manager_t *opt, size_t idx,
const struct retro_variable *var)
{
const char *val_start = NULL;
char *value = NULL;
char *desc_end = NULL;
char *config_val = NULL;
struct core_option *option = (struct core_option*)&opt->opts[idx];
if (!option)
return false;
option->key = strdup(var->key);
value = strdup(var->value);
desc_end = strstr(value, "; ");
if (!desc_end)
{
free(value);
return false;
}
*desc_end = '\0';
option->desc = strdup(value);
val_start = desc_end + 2;
option->vals = string_split(val_start, "|");
if (!option->vals)
{
free(value);
return false;
}
if (config_get_string(opt->conf, option->key, &config_val))
{
size_t i;
for (i = 0; i < option->vals->size; i++)
{
if (string_is_equal(option->vals->elems[i].data, config_val))
{
option->index = i;
break;
}
}
free(config_val);
}
free(value);
return true;
}
/**
* core_option_new:
* @conf_path : Filesystem path to write core option config file to.
* @vars : Pointer to variable array handle.
*
* Creates and initializes a core manager handle.
*
* Returns: handle to new core manager handle, otherwise NULL.
**/
core_option_manager_t *core_option_new(const char *conf_path,
const void *data)
{
const struct retro_variable *var;
const struct retro_variable *vars = (const struct retro_variable*)data;
size_t size = 0;
core_option_manager_t *opt = (core_option_manager_t*)
calloc(1, sizeof(*opt));
if (!opt)
return NULL;
if (*conf_path)
opt->conf = config_file_new(conf_path);
if (!opt->conf)
opt->conf = config_file_new(NULL);
strlcpy(opt->conf_path, conf_path, sizeof(opt->conf_path));
if (!opt->conf)
goto error;
for (var = vars; var->key && var->value; var++)
size++;
opt->opts = (struct core_option*)calloc(size, sizeof(*opt->opts));
if (!opt->opts)
goto error;
opt->size = size;
size = 0;
for (var = vars; var->key && var->value; size++, var++)
{
if (!parse_variable(opt, size, var))
goto error;
}
return opt;
error:
core_option_free(opt);
return NULL;
}
/**
* core_option_updated:
* @opt : options manager handle
*
* Has a core option been updated?
*
* Returns: true (1) if a core option has been updated,
* otherwise false (0).
**/
bool core_option_updated(core_option_manager_t *opt)
{
if (!opt)
return false;
return opt->updated;
}
/**
* core_option_flush:
* @opt : options manager handle
*
* Writes core option key-pair values to file.
*
* Returns: true (1) if core option values could be
* successfully saved to disk, otherwise false (0).
**/
bool core_option_flush(core_option_manager_t *opt)
{
size_t i;
for (i = 0; i < opt->size; i++)
{
struct core_option *option = (struct core_option*)&opt->opts[i];
if (option)
config_set_string(opt->conf, option->key, core_option_get_val(opt, i));
}
return config_file_write(opt->conf, opt->conf_path);
}
/**
* core_option_flush_game_specific:
* @opt : options manager handle
* @path : path for the core options file
*
* Writes core option key-pair values to a custom file.
*
* Returns: true (1) if core option values could be
* successfully saved to disk, otherwise false (0).
**/
bool core_option_flush_game_specific(core_option_manager_t *opt, char* path)
{
size_t i;
for (i = 0; i < opt->size; i++)
{
struct core_option *option = (struct core_option*)&opt->opts[i];
if (option)
config_set_string(opt->conf, option->key, core_option_get_val(opt, i));
}
return config_file_write(opt->conf, path);
}
/**
* core_option_size:
* @opt : options manager handle
*
* Gets total number of options.
*
* Returns: Total number of options.
**/
size_t core_option_size(core_option_manager_t *opt)
{
if (!opt)
return 0;
return opt->size;
}
/**
* core_option_get_desc:
* @opt : options manager handle
* @index : index identifier of the option
*
* Gets description for an option.
*
* Returns: Description for an option.
**/
const char *core_option_get_desc(core_option_manager_t *opt, size_t idx)
{
if (!opt)
return NULL;
return opt->opts[idx].desc;
}
/**
* core_option_get_val:
* @opt : options manager handle
* @index : index identifier of the option
*
* Gets value for an option.
*
* Returns: Value for an option.
**/
const char *core_option_get_val(core_option_manager_t *opt, size_t idx)
{
struct core_option *option = NULL;
if (!opt)
return NULL;
option = (struct core_option*)&opt->opts[idx];
if (!option)
return NULL;
return option->vals->elems[option->index].data;
}
void core_option_set_val(core_option_manager_t *opt,
size_t idx, size_t val_idx)
{
struct core_option *option= NULL;
if (!opt)
return;
option = (struct core_option*)&opt->opts[idx];
if (!option)
return;
option->index = val_idx % option->vals->size;
opt->updated = true;
}
/**
* core_option_next:
* @opt : pointer to core option manager object.
* @idx : index of core option to be reset to defaults.
*
* Get next value for core option specified by @idx.
* Options wrap around.
**/
void core_option_next(core_option_manager_t *opt, size_t idx)
{
struct core_option *option = NULL;
if (!opt)
return;
option = (struct core_option*)&opt->opts[idx];
if (!option)
return;
option->index = (option->index + 1) % option->vals->size;
opt->updated = true;
}
/**
* core_option_prev:
* @opt : pointer to core option manager object.
* @idx : index of core option to be reset to defaults.
* Options wrap around.
*
* Get previous value for core option specified by @idx.
* Options wrap around.
**/
void core_option_prev(core_option_manager_t *opt, size_t idx)
{
struct core_option *option = NULL;
if (!opt)
return;
option = (struct core_option*)&opt->opts[idx];
if (!option)
return;
option->index = (option->index + option->vals->size - 1) %
option->vals->size;
opt->updated = true;
}
/**
* core_option_set_default:
* @opt : pointer to core option manager object.
* @idx : index of core option to be reset to defaults.
*
* Reset core option specified by @idx and sets default value for option.
**/
void core_option_set_default(core_option_manager_t *opt, size_t idx)
{
if (!opt)
return;
opt->opts[idx].index = 0;
opt->updated = true;
}