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)
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; me_ctrl_actions[i].name != NULL; i++) {
85 mask = me_ctrl_actions[i].mask;
86 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER34)]) {
87 strncpy(act, me_ctrl_actions[i].name, 31);
88 fprintf(fn, "bind %s = player3 %s" NL, name, mystrip(act));
90 mask = me_ctrl_actions[i].mask << 16;
91 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER34)]) {
92 strncpy(act, me_ctrl_actions[i].name, 31);
93 fprintf(fn, "bind %s = player4 %s" NL, name, mystrip(act));
97 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
98 mask = emuctrl_actions[i].mask;
99 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
100 strncpy(act, emuctrl_actions[i].name, 31);
101 fprintf(fn, "bind %s = %s" NL, name, mystrip(act));
107 int config_write(const char *fname)
114 fn = fopen(fname, "w");
118 for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
121 if (!me->need_to_save)
123 if (me->name == NULL || me->name[0] == 0)
126 if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
127 fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
129 else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
130 fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
132 else if (me->beh == MB_OPT_ENUM) {
133 const char **names = (const char **)me->data;
136 for (t = 0; names[t] != NULL; t++) {
137 if (*(int *)me->var == t) {
138 strncpy(line, names[t], sizeof(line)-1);
139 line[sizeof(line)-1] = '\0';
144 else if (me->generate_name != NULL) {
145 strncpy(line, me->generate_name(me->id, &dummy), sizeof(line)-1);
146 line[sizeof(line)-1] = '\0';
150 lprintf("config: unhandled write: '%s' id %d behavior %d\n",
151 me->name, me->id, me->beh);
155 line[sizeof(line) - 1] = 0;
157 fprintf(fn, "%s = %s" NL, me->name, line);
160 /* input: save binds */
161 for (t = 0; t < IN_MAX_DEVS; t++)
163 const int *binds = in_get_dev_binds(t);
164 const char *name = in_get_dev_name(t, 0, 0);
167 if (binds == NULL || name == NULL)
170 fprintf(fn, "binddev = %s" NL, name);
172 in_get_config(t, IN_CFG_BIND_COUNT, &count);
173 keys_write(fn, t, binds);
176 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
184 int config_writelrom(const char *fname)
186 char line[640], *tmp, *optr = NULL;
187 char *old_data = NULL;
191 if (strlen(rom_fname_loaded) == 0) return -1;
193 f = fopen(fname, "r");
196 fseek(f, 0, SEEK_END);
198 fseek(f, 0, SEEK_SET);
199 old_data = malloc(size + size/8);
200 if (old_data != NULL)
205 tmp = fgets(line, sizeof(line), f);
206 if (tmp == NULL) break;
208 if (strncasecmp(line, "LastUsedROM", 11) == 0)
210 sprintf(optr, "%s", line);
211 optr += strlen(optr);
217 f = fopen(fname, "w");
218 if (f == NULL) return -1;
220 if (old_data != NULL) {
221 fwrite(old_data, 1, optr - old_data, f);
224 fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
229 /* --------------------------------------------------------------------------*/
231 int config_readlrom(const char *fname)
233 char line[640], *tmp;
234 int i, len, ret = -1;
237 f = fopen(fname, "r");
238 if (f == NULL) return -1;
240 // seek to the section needed
243 tmp = fgets(line, sizeof(line), f);
244 if (tmp == NULL) break;
246 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
248 for (i = 0; i < len; i++)
249 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
250 tmp = strchr(line, '=');
251 if (tmp == NULL) break;
255 len = sizeof(rom_fname_loaded)-1;
256 strncpy(rom_fname_loaded, tmp, len);
257 rom_fname_loaded[len] = 0;
265 static int custom_read(menu_entry *me, const char *var, const char *val)
271 case MA_OPT_FRAMESKIP:
272 if (strcasecmp(var, "Frameskip") != 0) return 0;
273 if (strcasecmp(val, "Auto") == 0)
274 currentConfig.Frameskip = -1;
275 else currentConfig.Frameskip = atoi(val);
278 case MA_OPT_SOUND_QUALITY:
279 if (strcasecmp(var, "Sound Quality") != 0) return 0;
280 PicoIn.sndRate = strtoul(val, &tmp, 10);
281 if (PicoIn.sndRate < 8000 || PicoIn.sndRate > 54000) {
282 if (strncasecmp(tmp, "native", 6) == 0) {
284 PicoIn.sndRate = 53000;
286 PicoIn.sndRate = 22050;
288 if (*tmp == 'H' || *tmp == 'h') tmp++;
289 if (*tmp == 'Z' || *tmp == 'z') tmp++;
290 while (*tmp == ' ') tmp++;
291 if (strcasecmp(tmp, "stereo") == 0) {
292 PicoIn.opt |= POPT_EN_STEREO;
293 } else if (strcasecmp(tmp, "mono") == 0) {
294 PicoIn.opt &= ~POPT_EN_STEREO;
299 case MA_OPT_SOUND_ALPHA:
300 if (strcasecmp(var, "Filter strength") != 0) return 0;
301 PicoIn.sndFilterAlpha = 0x10000 * atof(val);
305 if (strcasecmp(var, "Region") != 0) return 0;
306 if (strncasecmp(val, "Auto: ", 6) == 0)
308 const char *p = val + 5, *end = val + strlen(val);
310 PicoIn.regionOverride = PicoIn.autoRgnOrder = 0;
311 for (i = 0; p < end && i < 3; i++)
313 while (*p == ' ') p++;
314 if (p[0] == 'J' && p[1] == 'P') {
315 PicoIn.autoRgnOrder |= 1 << (i*4);
316 } else if (p[0] == 'U' && p[1] == 'S') {
317 PicoIn.autoRgnOrder |= 4 << (i*4);
318 } else if (p[0] == 'E' && p[1] == 'U') {
319 PicoIn.autoRgnOrder |= 8 << (i*4);
321 while (*p != ' ' && *p != 0) p++;
325 else if (strcasecmp(val, "Auto") == 0) {
326 PicoIn.regionOverride = 0;
327 } else if (strcasecmp(val, "Japan NTSC") == 0) {
328 PicoIn.regionOverride = 1;
329 } else if (strcasecmp(val, "Japan PAL") == 0) {
330 PicoIn.regionOverride = 2;
331 } else if (strcasecmp(val, "USA") == 0) {
332 PicoIn.regionOverride = 4;
333 } else if (strcasecmp(val, "Europe") == 0) {
334 PicoIn.regionOverride = 8;
339 case MA_32XOPT_MSH2_CYCLES:
340 currentConfig.msh2_khz = atoi(val);
341 Pico32xSetClocks(currentConfig.msh2_khz * 1000, 0);
344 case MA_32XOPT_SSH2_CYCLES:
345 currentConfig.ssh2_khz = atoi(val);
346 Pico32xSetClocks(0, currentConfig.ssh2_khz * 1000);
350 currentConfig.gamma = atoi(val);
353 case MA_OPT2_MAX_FRAMESKIP:
354 currentConfig.max_skip = atoi(val);
360 if (strcasecmp(var, "Wait for vsync") != 0) return 0;
361 if (strcasecmp(val, "never") == 0) {
362 currentConfig.EmuOpt &= ~(EOPT_VSYNC|EOPT_VSYNC_MODE);
363 } else if (strcasecmp(val, "sometimes") == 0) {
364 currentConfig.EmuOpt |= (EOPT_VSYNC|EOPT_VSYNC_MODE);
365 } else if (strcasecmp(val, "always") == 0) {
366 currentConfig.EmuOpt &= ~EOPT_VSYNC_MODE;
367 currentConfig.EmuOpt |= EOPT_VSYNC;
373 lprintf("unhandled custom_read %i: %s\n", me->id, var);
378 static int parse_bind_val(const char *val, int *type)
382 *type = IN_BINDTYPE_NONE;
386 if (strncasecmp(val, "player", 6) == 0)
388 int player, shift = 0;
389 player = atoi(val + 6) - 1;
396 *type = IN_BINDTYPE_PLAYER12 + (player >> 1);
397 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
398 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
399 return me_ctrl_actions[i].mask << shift;
402 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
403 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
404 *type = IN_BINDTYPE_EMU;
405 return emuctrl_actions[i].mask;
412 static void keys_parse_all(FILE *f)
414 char line[640], *var, *val;
421 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
423 if (ret == -1) continue;
425 if (strcasecmp(var, "binddev") == 0) {
426 dev_id = in_config_parse_dev(val);
428 printf("input: can't handle dev: %s\n", val);
431 in_unbind_all(dev_id, -1, -1);
434 if (dev_id < 0 || strncasecmp(var, "bind ", 5) != 0)
437 acts = parse_bind_val(val, &type);
439 lprintf("config: unhandled action \"%s\"\n", val);
444 in_config_bind_key(dev_id, var + 5, acts, type);
449 static void parse(const char *var, const char *val, int *keys_encountered)
454 if (strcasecmp(var, "LastUsedROM") == 0)
455 return; /* handled elsewhere */
457 if (strncasecmp(var, "bind", 4) == 0) {
458 *keys_encountered = 1;
459 return; /* handled elsewhere */
462 if (strcasecmp(var, "Sound Volume") == 0) {
463 currentConfig.volume = atoi(val);
467 for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
471 if (!me->need_to_save)
473 if (me->name == NULL || strcasecmp(var, me->name) != 0)
476 if (me->beh == MB_OPT_ONOFF) {
477 tmp = strtol(val, &p, 0);
480 if (tmp) *(int *)me->var |= me->mask;
481 else *(int *)me->var &= ~me->mask;
484 else if (me->beh == MB_OPT_RANGE) {
485 tmp = strtol(val, &p, 0);
488 if (tmp < me->min) tmp = me->min;
489 if (tmp > me->max) tmp = me->max;
490 *(int *)me->var = tmp;
493 else if (me->beh == MB_OPT_ENUM) {
494 const char **names, *p1;
497 names = (const char **)me->data;
500 for (i = 0; names[i] != NULL; i++) {
501 for (p1 = names[i]; *p1 == ' '; p1++)
503 if (strcasecmp(p1, val) == 0) {
510 else if (custom_read(me, var, val))
514 lprintf("config_readsect: unhandled var: \"%s\"\n", var);
518 lprintf("config_readsect: unhandled val for \"%s\": \"%s\"\n", var, val);
521 int config_readsect(const char *fname, const char *section)
523 char line[640], *var, *val;
524 int keys_encountered = 0;
528 f = fopen(fname, "r");
529 if (f == NULL) return -1;
533 /* for game_def.cfg only */
534 ret = seek_sect(f, section);
536 lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
546 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
548 if (ret == -1) continue;
550 parse(var, val, &keys_encountered);
553 if (keys_encountered) {
560 lprintf("config_readsect: loaded from %s", fname);
562 lprintf(" [%s]", section);
570 static char *mystrip(char *str)
575 for (i = 0; i < len; i++)
576 if (str[i] != ' ') break;
577 if (i > 0) memmove(str, str + i, len - i + 1);
580 for (i = len - 1; i >= 0; i--)
581 if (str[i] != ' ') break;
590 * -1 - failed to parse line
592 int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
594 char *var, *val, *tmp;
598 tmp = fgets(line, lsize, f);
599 if (tmp == NULL) return 0;
601 if (line[0] == '[') return 0; // other section
603 // strip comments, linefeed, spaces..
605 for (i = 0; i < len; i++)
606 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
609 if (len <= 0) return -1;;
612 for (i = 0; i < len; i++)
613 if (line[i] == '=') break;
614 if (i >= len || strchr(&line[i+1], '=') != NULL) {
615 lprintf("config_readsect: can't parse: %s\n", line);
625 if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
626 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);