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_int.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, const int *kbd_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);
70 if (strcmp(name, "#") == 0) name = "\\x23"; // replace comment sign
71 if (strcmp(name, "=") == 0) name = "\\x3d"; // replace assignment sign
73 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
74 mask = me_ctrl_actions[i].mask;
75 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
76 strncpy(act, me_ctrl_actions[i].name, 31);
77 fprintf(fn, "bind %s = player1 %s" NL, name, mystrip(act));
79 mask = me_ctrl_actions[i].mask << 16;
80 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
81 strncpy(act, me_ctrl_actions[i].name, 31);
82 fprintf(fn, "bind %s = player2 %s" NL, name, mystrip(act));
86 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
87 mask = me_ctrl_actions[i].mask;
88 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER34)]) {
89 strncpy(act, me_ctrl_actions[i].name, 31);
90 fprintf(fn, "bind %s = player3 %s" NL, name, mystrip(act));
92 mask = me_ctrl_actions[i].mask << 16;
93 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER34)]) {
94 strncpy(act, me_ctrl_actions[i].name, 31);
95 fprintf(fn, "bind %s = player4 %s" NL, name, mystrip(act));
99 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
100 mask = emuctrl_actions[i].mask;
101 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
102 strncpy(act, emuctrl_actions[i].name, 31);
103 fprintf(fn, "bind %s = %s" NL, name, mystrip(act));
108 for (k = 0; k < key_count; k++) {
109 const char *name = in_get_key_name(dev_id, k);
110 if (strcmp(name, "#") == 0) name = "\\x23"; // replace comment sign
111 if (strcmp(name, "=") == 0) name = "\\x3d"; // replace assignment sign
113 fprintf(fn, "bind %s = key%02x" NL, name, kbd_binds[k]);
117 int config_write(const char *fname)
124 fn = fopen(fname, "w");
128 for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
131 if (!me->need_to_save)
133 if (me->name == NULL || me->name[0] == 0)
136 if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
137 fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
139 else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
140 fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
142 else if (me->beh == MB_OPT_ENUM) {
143 const char **names = (const char **)me->data;
146 for (t = 0; names[t] != NULL; t++) {
147 if (*(int *)me->var == t) {
148 strncpy(line, names[t], sizeof(line)-1);
149 line[sizeof(line)-1] = '\0';
154 else if (me->generate_name != NULL) {
155 strncpy(line, me->generate_name(me->id, &dummy), sizeof(line)-1);
156 line[sizeof(line)-1] = '\0';
160 lprintf("config: unhandled write: '%s' id %d behavior %d\n",
161 me->name, me->id, me->beh);
165 line[sizeof(line) - 1] = 0;
167 fprintf(fn, "%s = %s" NL, me->name, line);
170 /* input: save binds */
171 for (t = 0; t < IN_MAX_DEVS; t++)
173 const int *binds = in_get_dev_binds(t);
174 const char *name = in_get_dev_name(t, 0, 0);
177 if (binds == NULL || name == NULL)
180 fprintf(fn, "binddev = %s" NL, name);
182 in_get_config(t, IN_CFG_BIND_COUNT, &count);
183 keys_write(fn, t, binds, in_get_dev_kbd_binds(t));
186 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
194 int config_writelrom(const char *fname)
196 char line[640], *tmp, *optr = NULL;
197 char *old_data = NULL;
201 if (strlen(rom_fname_loaded) == 0) return -1;
203 f = fopen(fname, "r");
206 fseek(f, 0, SEEK_END);
208 fseek(f, 0, SEEK_SET);
209 old_data = malloc(size + size/8);
210 if (old_data != NULL)
215 tmp = fgets(line, sizeof(line), f);
216 if (tmp == NULL) break;
218 if (strncasecmp(line, "LastUsedROM", 11) == 0)
220 sprintf(optr, "%s", line);
221 optr += strlen(optr);
227 f = fopen(fname, "w");
228 if (f == NULL) return -1;
230 if (old_data != NULL) {
231 fwrite(old_data, 1, optr - old_data, f);
234 fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
239 /* --------------------------------------------------------------------------*/
241 int config_readlrom(const char *fname)
243 char line[640], *tmp;
244 int i, len, ret = -1;
247 f = fopen(fname, "r");
248 if (f == NULL) return -1;
250 // seek to the section needed
253 tmp = fgets(line, sizeof(line), f);
254 if (tmp == NULL) break;
256 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
258 for (i = 0; i < len; i++)
259 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
260 tmp = strchr(line, '=');
261 if (tmp == NULL) break;
265 len = sizeof(rom_fname_loaded)-1;
266 strncpy(rom_fname_loaded, tmp, len);
267 rom_fname_loaded[len] = 0;
275 static int custom_read(menu_entry *me, const char *var, const char *val)
281 case MA_OPT_FRAMESKIP:
282 if (strcasecmp(var, "Frameskip") != 0) return 0;
283 if (strcasecmp(val, "Auto") == 0)
284 currentConfig.Frameskip = -1;
285 else currentConfig.Frameskip = atoi(val);
288 case MA_OPT_SOUND_QUALITY:
289 if (strcasecmp(var, "Sound Quality") != 0) return 0;
290 PicoIn.sndRate = strtoul(val, &tmp, 10);
291 if (PicoIn.sndRate < 8000 || PicoIn.sndRate > 54000) {
292 if (strncasecmp(tmp, "native", 6) == 0) {
294 PicoIn.sndRate = 53000;
296 PicoIn.sndRate = 22050;
298 if (*tmp == 'H' || *tmp == 'h') tmp++;
299 if (*tmp == 'Z' || *tmp == 'z') tmp++;
300 while (*tmp == ' ') tmp++;
301 if (strcasecmp(tmp, "stereo") == 0) {
302 PicoIn.opt |= POPT_EN_STEREO;
303 } else if (strcasecmp(tmp, "mono") == 0) {
304 PicoIn.opt &= ~POPT_EN_STEREO;
309 case MA_OPT_SOUND_ALPHA:
310 if (strcasecmp(var, "Filter strength") != 0) return 0;
311 PicoIn.sndFilterAlpha = 0x10000 * atof(val);
315 if (strcasecmp(var, "Region") != 0) return 0;
316 if (strncasecmp(val, "Auto: ", 6) == 0)
318 const char *p = val + 5, *end = val + strlen(val);
320 PicoIn.regionOverride = PicoIn.autoRgnOrder = 0;
321 for (i = 0; p < end && i < 3; i++)
323 while (*p == ' ') p++;
324 if (p[0] == 'J' && p[1] == 'P') {
325 PicoIn.autoRgnOrder |= 1 << (i*4);
326 } else if (p[0] == 'U' && p[1] == 'S') {
327 PicoIn.autoRgnOrder |= 4 << (i*4);
328 } else if (p[0] == 'E' && p[1] == 'U') {
329 PicoIn.autoRgnOrder |= 8 << (i*4);
331 while (*p != ' ' && *p != 0) p++;
335 else if (strcasecmp(val, "Auto") == 0) {
336 PicoIn.regionOverride = 0;
337 } else if (strcasecmp(val, "Japan NTSC") == 0) {
338 PicoIn.regionOverride = 1;
339 } else if (strcasecmp(val, "Japan PAL") == 0) {
340 PicoIn.regionOverride = 2;
341 } else if (strcasecmp(val, "USA") == 0) {
342 PicoIn.regionOverride = 4;
343 } else if (strcasecmp(val, "Europe") == 0) {
344 PicoIn.regionOverride = 8;
349 case MA_32XOPT_MSH2_CYCLES:
350 currentConfig.msh2_khz = atoi(val);
351 Pico32xSetClocks(currentConfig.msh2_khz * 1000, 0);
354 case MA_32XOPT_SSH2_CYCLES:
355 currentConfig.ssh2_khz = atoi(val);
356 Pico32xSetClocks(0, currentConfig.ssh2_khz * 1000);
360 currentConfig.gamma = atoi(val);
363 case MA_OPT2_MAX_FRAMESKIP:
364 currentConfig.max_skip = atoi(val);
367 case MA_CTRL_KEYBOARD:
368 currentConfig.keyboard = 0;
369 if (strcasecmp(val, "physical") == 0)
370 currentConfig.keyboard = 2;
371 else if (strcasecmp(val, "virtual") == 0)
372 currentConfig.keyboard = 1;
378 if (strcasecmp(var, "Wait for vsync") != 0) return 0;
379 if (strcasecmp(val, "never") == 0) {
380 currentConfig.EmuOpt &= ~(EOPT_VSYNC|EOPT_VSYNC_MODE);
381 } else if (strcasecmp(val, "sometimes") == 0) {
382 currentConfig.EmuOpt |= (EOPT_VSYNC|EOPT_VSYNC_MODE);
383 } else if (strcasecmp(val, "always") == 0) {
384 currentConfig.EmuOpt &= ~EOPT_VSYNC_MODE;
385 currentConfig.EmuOpt |= EOPT_VSYNC;
391 lprintf("unhandled custom_read %i: %s\n", me->id, var);
396 static int parse_bind_val(const char *val, int *type)
400 *type = IN_BINDTYPE_NONE;
404 if (strncasecmp(val, "key", 3) == 0)
406 *type = IN_BINDTYPE_KEYBOARD;
407 return strtol(val + 3, NULL, 16);
410 if (strncasecmp(val, "player", 6) == 0)
412 int player, shift = 0;
413 player = atoi(val + 6) - 1;
420 *type = IN_BINDTYPE_PLAYER12 + (player >> 1);
421 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
422 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
423 return me_ctrl_actions[i].mask << shift;
426 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
427 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
428 *type = IN_BINDTYPE_EMU;
429 return emuctrl_actions[i].mask;
436 static void keys_parse_all(FILE *f)
438 char line[640], *var, *val;
445 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
447 if (ret == -1) continue;
449 if (strcasecmp(var, "binddev") == 0) {
450 dev_id = in_config_parse_dev(val);
452 printf("input: can't handle dev: %s\n", val);
455 in_unbind_all(dev_id, -1, -1);
458 if (dev_id < 0 || strncasecmp(var, "bind ", 5) != 0)
461 acts = parse_bind_val(val, &type);
463 lprintf("config: unhandled action \"%s\"\n", val);
468 if (type == IN_BINDTYPE_KEYBOARD)
469 in_config_bind_kbd_key(dev_id, var + 5, acts);
471 in_config_bind_key(dev_id, var + 5, acts, type);
476 static void parse(const char *var, const char *val, int *keys_encountered)
481 if (strcasecmp(var, "LastUsedROM") == 0)
482 return; /* handled elsewhere */
484 if (strncasecmp(var, "bind", 4) == 0) {
485 *keys_encountered = 1;
486 return; /* handled elsewhere */
489 if (strcasecmp(var, "Sound Volume") == 0) {
490 currentConfig.volume = atoi(val);
494 for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
498 if (!me->need_to_save)
500 if (me->name == NULL || strcasecmp(var, me->name) != 0)
503 if (me->beh == MB_OPT_ONOFF) {
504 tmp = strtol(val, &p, 0);
507 if (tmp) *(int *)me->var |= me->mask;
508 else *(int *)me->var &= ~me->mask;
511 else if (me->beh == MB_OPT_RANGE) {
512 tmp = strtol(val, &p, 0);
515 if (tmp < me->min) tmp = me->min;
516 if (tmp > me->max) tmp = me->max;
517 *(int *)me->var = tmp;
520 else if (me->beh == MB_OPT_ENUM) {
521 const char **names, *p1;
524 names = (const char **)me->data;
527 for (i = 0; names[i] != NULL; i++) {
528 for (p1 = names[i]; *p1 == ' '; p1++)
530 if (strcasecmp(p1, val) == 0) {
537 else if (custom_read(me, var, val))
541 lprintf("config_readsect: unhandled var: \"%s\"\n", var);
545 lprintf("config_readsect: unhandled val for \"%s\": \"%s\"\n", var, val);
548 int config_readsect(const char *fname, const char *section)
550 char line[640], *var, *val;
551 int keys_encountered = 0;
555 f = fopen(fname, "r");
556 if (f == NULL) return -1;
560 /* for game_def.cfg only */
561 ret = seek_sect(f, section);
563 lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
573 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
575 if (ret == -1) continue;
577 parse(var, val, &keys_encountered);
580 if (keys_encountered) {
587 lprintf("config_readsect: loaded from %s", fname);
589 lprintf(" [%s]", section);
597 static char *mystrip(char *str)
602 for (i = 0; i < len; i++)
603 if (str[i] != ' ') break;
604 if (i > 0) memmove(str, str + i, len - i + 1);
607 for (i = len - 1; i >= 0; i--)
608 if (str[i] != ' ') break;
617 * -1 - failed to parse line
619 int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
621 char *var, *val, *tmp;
625 tmp = fgets(line, lsize, f);
626 if (tmp == NULL) return 0;
628 if (line[0] == '[') return 0; // other section
630 // strip comments, linefeed, spaces..
632 for (i = 0; i < len; i++)
633 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
636 if (len <= 0) return -1;;
639 for (i = 0; i < len; i++)
640 if (line[i] == '=') break;
641 if (i >= len || strchr(&line[i+1], '=') != NULL) {
642 lprintf("config_readsect: can't parse: %s\n", line);
652 if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
653 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);