2 * Human-readable config file management for PicoDrive
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
16 #include "../libpicofe/input.h"
17 #include "../libpicofe/plat.h"
18 #include "../libpicofe/lprintf.h"
19 #include "config_file.h"
21 static char *mystrip(char *str);
25 #include "menu_pico.h"
27 #include <pico/pico.h>
29 // always output DOS endlines
36 static int seek_sect(FILE *f, const char *section)
41 len = strlen(section);
42 // seek to the section needed
45 tmp = fgets(line, sizeof(line), f);
46 if (tmp == NULL) break;
48 if (line[0] != '[') continue; // not section start
49 if (strncmp(line + 1, section, len) == 0 && line[len+1] == ']')
56 static void keys_write(FILE *fn, int dev_id, const int *binds)
59 int key_count = 0, k, i;
61 in_get_config(dev_id, IN_CFG_BIND_COUNT, &key_count);
63 for (k = 0; k < key_count; k++)
69 name = in_get_key_name(dev_id, k);
71 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
72 mask = me_ctrl_actions[i].mask;
73 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
74 strncpy(act, me_ctrl_actions[i].name, 31);
75 fprintf(fn, "bind %s = player1 %s" NL, name, mystrip(act));
77 mask = me_ctrl_actions[i].mask << 16;
78 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
79 strncpy(act, me_ctrl_actions[i].name, 31);
80 fprintf(fn, "bind %s = player2 %s" NL, name, mystrip(act));
84 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
85 mask = emuctrl_actions[i].mask;
86 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
87 strncpy(act, emuctrl_actions[i].name, 31);
88 fprintf(fn, "bind %s = %s" NL, name, mystrip(act));
94 int config_write(const char *fname)
101 fn = fopen(fname, "w");
105 for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
108 if (!me->need_to_save || !me->enabled)
110 if (me->name == NULL || me->name[0] == 0)
113 if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
114 fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
116 else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
117 fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
119 else if (me->beh == MB_OPT_ENUM && me->data != NULL) {
120 const char **names = (const char **)me->data;
121 for (t = 0; names[t] != NULL; t++) {
122 if (*(int *)me->var == t) {
123 strncpy(line, names[t], sizeof(line));
128 else if (me->generate_name != NULL) {
129 strncpy(line, me->generate_name(0, &dummy), sizeof(line));
133 lprintf("config: unhandled write: %i\n", me->id);
137 line[sizeof(line) - 1] = 0;
139 fprintf(fn, "%s = %s" NL, me->name, line);
142 /* input: save binds */
143 for (t = 0; t < IN_MAX_DEVS; t++)
145 const int *binds = in_get_dev_binds(t);
146 const char *name = in_get_dev_name(t, 0, 0);
149 if (binds == NULL || name == NULL)
152 fprintf(fn, "binddev = %s" NL, name);
154 in_get_config(t, IN_CFG_BIND_COUNT, &count);
155 keys_write(fn, t, binds);
158 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
166 int config_writelrom(const char *fname)
168 char line[128], *tmp, *optr = NULL;
169 char *old_data = NULL;
173 if (strlen(rom_fname_loaded) == 0) return -1;
175 f = fopen(fname, "r");
178 fseek(f, 0, SEEK_END);
180 fseek(f, 0, SEEK_SET);
181 old_data = malloc(size + size/8);
182 if (old_data != NULL)
187 tmp = fgets(line, sizeof(line), f);
188 if (tmp == NULL) break;
190 if (strncasecmp(line, "LastUsedROM", 11) == 0)
192 sprintf(optr, "%s", line);
193 optr += strlen(optr);
199 f = fopen(fname, "w");
200 if (f == NULL) return -1;
202 if (old_data != NULL) {
203 fwrite(old_data, 1, optr - old_data, f);
206 fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
211 /* --------------------------------------------------------------------------*/
213 int config_readlrom(const char *fname)
215 char line[128], *tmp;
216 int i, len, ret = -1;
219 f = fopen(fname, "r");
220 if (f == NULL) return -1;
222 // seek to the section needed
225 tmp = fgets(line, sizeof(line), f);
226 if (tmp == NULL) break;
228 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
230 for (i = 0; i < len; i++)
231 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
232 tmp = strchr(line, '=');
233 if (tmp == NULL) break;
237 len = sizeof(rom_fname_loaded);
238 strncpy(rom_fname_loaded, tmp, len);
239 rom_fname_loaded[len-1] = 0;
247 static int custom_read(menu_entry *me, const char *var, const char *val)
253 case MA_OPT_FRAMESKIP:
254 if (strcasecmp(var, "Frameskip") != 0) return 0;
255 if (strcasecmp(val, "Auto") == 0)
256 currentConfig.Frameskip = -1;
257 else currentConfig.Frameskip = atoi(val);
260 case MA_OPT_SOUND_QUALITY:
261 if (strcasecmp(var, "Sound Quality") != 0) return 0;
262 PsndRate = strtoul(val, &tmp, 10);
263 if (PsndRate < 8000 || PsndRate > 44100)
265 if (*tmp == 'H' || *tmp == 'h') tmp++;
266 if (*tmp == 'Z' || *tmp == 'z') tmp++;
267 while (*tmp == ' ') tmp++;
268 if (strcasecmp(tmp, "stereo") == 0) {
269 PicoOpt |= POPT_EN_STEREO;
270 } else if (strcasecmp(tmp, "mono") == 0) {
271 PicoOpt &= ~POPT_EN_STEREO;
277 if (strcasecmp(var, "Region") != 0) return 0;
278 if (strncasecmp(val, "Auto: ", 6) == 0)
280 const char *p = val + 5, *end = val + strlen(val);
282 PicoRegionOverride = PicoAutoRgnOrder = 0;
283 for (i = 0; p < end && i < 3; i++)
285 while (*p == ' ') p++;
286 if (p[0] == 'J' && p[1] == 'P') {
287 PicoAutoRgnOrder |= 1 << (i*4);
288 } else if (p[0] == 'U' && p[1] == 'S') {
289 PicoAutoRgnOrder |= 4 << (i*4);
290 } else if (p[0] == 'E' && p[1] == 'U') {
291 PicoAutoRgnOrder |= 8 << (i*4);
293 while (*p != ' ' && *p != 0) p++;
297 else if (strcasecmp(val, "Auto") == 0) {
298 PicoRegionOverride = 0;
299 } else if (strcasecmp(val, "Japan NTSC") == 0) {
300 PicoRegionOverride = 1;
301 } else if (strcasecmp(val, "Japan PAL") == 0) {
302 PicoRegionOverride = 2;
303 } else if (strcasecmp(val, "USA") == 0) {
304 PicoRegionOverride = 4;
305 } else if (strcasecmp(val, "Europe") == 0) {
306 PicoRegionOverride = 8;
312 if (strcasecmp(var, "Gamma correction") != 0) return 0;
313 currentConfig.gamma = (int) (atof(val) * 100.0);
316 case MA_CDOPT_READAHEAD:
317 if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
318 PicoCDBuffers = atoi(val) / 2;
321 case MA_32XOPT_MSH2_CYCLES:
322 currentConfig.msh2_khz = atoi(val);
323 Pico32xSetClocks(currentConfig.msh2_khz * 1000, 0);
326 case MA_32XOPT_SSH2_CYCLES:
327 currentConfig.ssh2_khz = atoi(val);
328 Pico32xSetClocks(0, currentConfig.ssh2_khz * 1000);
333 if (strcasecmp(var, "Scale factor") != 0) return 0;
334 currentConfig.scale = atof(val);
336 case MA_OPT3_HSCALE32:
337 if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
338 currentConfig.hscale32 = atof(val);
340 case MA_OPT3_HSCALE40:
341 if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
342 currentConfig.hscale40 = atof(val);
346 if (strcasecmp(var, "Wait for vsync") != 0) return 0;
347 if (strcasecmp(val, "never") == 0) {
348 currentConfig.EmuOpt &= ~0x12000;
349 } else if (strcasecmp(val, "sometimes") == 0) {
350 currentConfig.EmuOpt |= 0x12000;
351 } else if (strcasecmp(val, "always") == 0) {
352 currentConfig.EmuOpt &= ~0x12000;
353 currentConfig.EmuOpt |= 0x02000;
359 lprintf("unhandled custom_read %i: %s\n", me->id, var);
364 static int parse_bind_val(const char *val, int *type)
368 *type = IN_BINDTYPE_NONE;
372 if (strncasecmp(val, "player", 6) == 0)
374 int player, shift = 0;
375 player = atoi(val + 6) - 1;
382 *type = IN_BINDTYPE_PLAYER12;
383 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
384 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
385 return me_ctrl_actions[i].mask << shift;
388 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
389 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
390 *type = IN_BINDTYPE_EMU;
391 return emuctrl_actions[i].mask;
398 static void keys_parse_all(FILE *f)
400 char line[256], *var, *val;
407 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
409 if (ret == -1) continue;
411 if (strcasecmp(var, "binddev") == 0) {
412 dev_id = in_config_parse_dev(val);
414 printf("input: can't handle dev: %s\n", val);
417 in_unbind_all(dev_id, -1, -1);
420 if (dev_id < 0 || strncasecmp(var, "bind ", 5) != 0)
423 acts = parse_bind_val(val, &type);
425 lprintf("config: unhandled action \"%s\"\n", val);
430 in_config_bind_key(dev_id, var + 5, acts, type);
434 static void parse(const char *var, const char *val, int *keys_encountered)
439 if (strcasecmp(var, "LastUsedROM") == 0)
440 return; /* handled elsewhere */
442 if (strncasecmp(var, "bind", 4) == 0) {
443 *keys_encountered = 1;
444 return; /* handled elsewhere */
447 if (strcasecmp(var, "Sound Volume") == 0) {
448 currentConfig.volume = atoi(val);
452 for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
456 if (!me->need_to_save)
458 if (me->name == NULL || strcasecmp(var, me->name) != 0)
461 if (me->beh == MB_OPT_ONOFF) {
462 tmp = strtol(val, &p, 0);
465 if (tmp) *(int *)me->var |= me->mask;
466 else *(int *)me->var &= ~me->mask;
469 else if (me->beh == MB_OPT_RANGE) {
470 tmp = strtol(val, &p, 0);
473 if (tmp < me->min) tmp = me->min;
474 if (tmp > me->max) tmp = me->max;
475 *(int *)me->var = tmp;
478 else if (me->beh == MB_OPT_ENUM) {
479 const char **names, *p1;
482 names = (const char **)me->data;
485 for (i = 0; names[i] != NULL; i++) {
486 for (p1 = names[i]; *p1 == ' '; p1++)
488 if (strcasecmp(p1, val) == 0) {
495 else if (custom_read(me, var, val))
499 lprintf("config_readsect: unhandled var: \"%s\"\n", var);
503 lprintf("config_readsect: unhandled val for \"%s\": \"%s\"\n", var, val);
506 int config_readsect(const char *fname, const char *section)
508 char line[128], *var, *val;
509 int keys_encountered = 0;
513 f = fopen(fname, "r");
514 if (f == NULL) return -1;
518 /* for game_def.cfg only */
519 ret = seek_sect(f, section);
521 lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
531 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
533 if (ret == -1) continue;
535 parse(var, val, &keys_encountered);
538 if (keys_encountered) {
545 lprintf("config_readsect: loaded from %s", fname);
547 lprintf(" [%s]", section);
555 static char *mystrip(char *str)
560 for (i = 0; i < len; i++)
561 if (str[i] != ' ') break;
562 if (i > 0) memmove(str, str + i, len - i + 1);
565 for (i = len - 1; i >= 0; i--)
566 if (str[i] != ' ') break;
575 * -1 - failed to parse line
577 int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
579 char *var, *val, *tmp;
583 tmp = fgets(line, lsize, f);
584 if (tmp == NULL) return 0;
586 if (line[0] == '[') return 0; // other section
588 // strip comments, linefeed, spaces..
590 for (i = 0; i < len; i++)
591 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
594 if (len <= 0) return -1;;
597 for (i = 0; i < len; i++)
598 if (line[i] == '=') break;
599 if (i >= len || strchr(&line[i+1], '=') != NULL) {
600 lprintf("config_readsect: can't parse: %s\n", line);
610 if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
611 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);