2 * Human-readable config file management for PicoDrive
17 static char *mystrip(char *str);
23 #include <pico/pico.h>
25 // always output DOS endlines
32 static int seek_sect(FILE *f, const char *section)
37 len = strlen(section);
38 // seek to the section needed
41 tmp = fgets(line, sizeof(line), f);
42 if (tmp == NULL) break;
44 if (line[0] != '[') continue; // not section start
45 if (strncmp(line + 1, section, len) == 0 && line[len+1] == ']')
53 static void keys_write(FILE *fn, const char *bind_str, int dev_id, const int *binds, int no_defaults)
56 int key_count = 0, k, i;
59 in_get_config(dev_id, IN_CFG_BIND_COUNT, &key_count);
60 def_binds = in_get_dev_def_binds(dev_id);
62 for (k = 0; k < key_count; k++)
68 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
69 if (binds[IN_BIND_OFFS(k, t)] != def_binds[IN_BIND_OFFS(k, t)])
72 if (no_defaults && t == IN_BINDTYPE_COUNT)
73 continue; /* no change from defaults */
75 name = in_get_key_name(dev_id, k);
77 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
78 if (binds[IN_BIND_OFFS(k, t)] != 0 || def_binds[IN_BIND_OFFS(k, t)] == 0)
81 if (t == IN_BINDTYPE_COUNT) {
82 /* key has default bind removed */
83 fprintf(fn, "%s %s =" NL, bind_str, name);
87 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
88 mask = me_ctrl_actions[i].mask;
89 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
90 strncpy(act, me_ctrl_actions[i].name, 31);
91 fprintf(fn, "%s %s = player1 %s" NL, bind_str, name, mystrip(act));
93 mask = me_ctrl_actions[i].mask << 16;
94 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
95 strncpy(act, me_ctrl_actions[i].name, 31);
96 fprintf(fn, "%s %s = player2 %s" NL, bind_str, name, mystrip(act));
100 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
101 mask = emuctrl_actions[i].mask;
102 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
103 strncpy(act, emuctrl_actions[i].name, 31);
104 fprintf(fn, "%s %s = %s" NL, bind_str, name, mystrip(act));
110 /* XXX: this should go to menu structures instead */
111 static int default_var(const menu_entry *me)
115 case MA_OPT2_ENABLE_YM2612:
116 case MA_OPT2_ENABLE_SN76496:
117 case MA_OPT2_ENABLE_Z80:
118 case MA_OPT_6BUTTON_PAD:
119 case MA_OPT_ACC_SPRITES:
120 case MA_OPT_ARM940_SOUND:
123 case MA_CDOPT_SCALEROT_CHIP:
124 case MA_CDOPT_BETTER_SYNC:
125 case MA_CDOPT_SAVERAM:
126 case MA_32XOPT_ENABLE_32X:
128 case MA_OPT2_SVP_DYNAREC:
129 case MA_OPT2_NO_SPRITE_LIM:
130 case MA_OPT2_NO_IDLE_LOOPS:
131 return defaultConfig.s_PicoOpt;
133 case MA_OPT_SRAM_STATES:
134 case MA_OPT_SHOW_FPS:
135 case MA_OPT_ENABLE_SOUND:
136 case MA_OPT2_GZIP_STATES:
137 case MA_OPT2_SQUIDGEHACK:
138 case MA_OPT2_NO_LAST_ROM:
139 case MA_OPT2_RAMTIMINGS:
141 case MA_OPT2_A_SN_GAMMA:
143 case MA_OPT_INTERLACED:
144 case MA_OPT2_DBLBUFF:
145 case MA_OPT2_STATUS_LINE:
146 case MA_OPT2_NO_FRAME_LIMIT:
147 case MA_OPT_TEARING_FIX:
148 return defaultConfig.EmuOpt;
150 case MA_CTRL_TURBO_RATE: return defaultConfig.turbo_rate;
151 case MA_OPT_SCALING: return defaultConfig.scaling;
152 case MA_OPT_ROTATION: return defaultConfig.rotation;
153 case MA_OPT2_GAMMA: return defaultConfig.gamma;
154 case MA_OPT_FRAMESKIP: return defaultConfig.Frameskip;
155 case MA_OPT_CONFIRM_STATES: return defaultConfig.confirm_save;
156 case MA_OPT_CPU_CLOCKS: return defaultConfig.CPUclock;
157 case MA_OPT_RENDERER: return defaultConfig.renderer;
158 case MA_32XOPT_RENDERER: return defaultConfig.renderer32x;
160 case MA_OPT_SAVE_SLOT:
164 lprintf("missing default for %d\n", me->id);
169 static int is_cust_val_default(const menu_entry *me)
174 return defaultConfig.s_PicoRegion == PicoRegionOverride &&
175 defaultConfig.s_PicoAutoRgnOrder == PicoAutoRgnOrder;
176 case MA_OPT_SOUND_QUALITY:
177 return defaultConfig.s_PsndRate == PsndRate &&
178 ((defaultConfig.s_PicoOpt ^ PicoOpt) & POPT_EN_STEREO) == 0;
179 case MA_CDOPT_READAHEAD:
180 return defaultConfig.s_PicoCDBuffers == PicoCDBuffers;
181 case MA_32XOPT_MSH2_CYCLES:
182 return p32x_msh2_multiplier == MSH2_MULTI_DEFAULT;
183 case MA_32XOPT_SSH2_CYCLES:
184 return p32x_ssh2_multiplier == SSH2_MULTI_DEFAULT;
188 lprintf("is_cust_val_default: unhandled id %i\n", me->id);
192 int config_writesect(const char *fname, const char *section)
194 FILE *fo = NULL, *fn = NULL; // old and new
195 int no_defaults = 0; // avoid saving defaults
198 char line[128], *tmp;
204 fo = fopen(fname, "r");
206 fn = fopen(fname, "w");
210 ret = seek_sect(fo, section);
212 // sect not found, we can simply append
213 fclose(fo); fo = NULL;
214 fn = fopen(fname, "a");
220 rename(fname, "tmp.cfg");
221 fo = fopen("tmp.cfg", "r");
222 fn = fopen(fname, "w");
223 if (fo == NULL || fn == NULL) goto write;
225 // copy everything until sect
226 tlen = strlen(section);
229 tmp = fgets(line, sizeof(line), fo);
230 if (tmp == NULL) break;
232 if (line[0] == '[' && strncmp(line + 1, section, tlen) == 0 && line[tlen+1] == ']')
237 // now skip to next sect
240 tmp = fgets(line, sizeof(line), fo);
241 if (tmp == NULL) break;
242 if (line[0] == '[') {
243 fseek(fo, -strlen(line), SEEK_CUR);
249 fclose(fo); fo = NULL;
255 fn = fopen(fname, "w");
264 fprintf(fn, "[%s]" NL, section);
266 for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
269 if (!me->need_to_save)
271 if (me->name == NULL || me->name[0] == 0)
274 if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
275 if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask))
276 fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
278 else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
279 if (!no_defaults || (*(int *)me->var ^ default_var(me)))
280 fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
282 else if (me->beh == MB_OPT_ENUM && me->data != NULL) {
283 const char **names = (const char **)me->data;
284 for (t = 0; names[t] != NULL; t++)
285 if (*(int *)me->var == t && (!no_defaults || (*(int *)me->var ^ default_var(me)))) {
286 strncpy(line, names[t], sizeof(line));
290 else if (me->generate_name != NULL) {
291 if (!no_defaults || !is_cust_val_default(me)) {
292 strncpy(line, me->generate_name(0, &dummy), sizeof(line));
297 lprintf("config: unhandled write: %i\n", me->id);
301 line[sizeof(line) - 1] = 0;
303 fprintf(fn, "%s = %s" NL, me->name, line);
306 /* input: save device names */
307 for (t = 0; t < IN_MAX_DEVS; t++)
309 const int *binds = in_get_dev_binds(t);
310 const char *name = in_get_dev_name(t, 0, 0);
311 if (binds == NULL || name == NULL)
314 fprintf(fn, "input%d = %s" NL, t, name);
317 /* input: save binds */
318 for (t = 0; t < IN_MAX_DEVS; t++)
320 const int *binds = in_get_dev_binds(t);
321 const char *name = in_get_dev_name(t, 0, 0);
325 if (binds == NULL || name == NULL)
328 sprintf(strbind, "bind%d", t);
329 if (t == 0) strbind[4] = 0;
331 in_get_config(t, IN_CFG_BIND_COUNT, &count);
332 keys_write(fn, strbind, t, binds, no_defaults);
337 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
344 // copy whatever is left
347 tmp = fgets(line, sizeof(line), fo);
348 if (tmp == NULL) break;
361 int config_writelrom(const char *fname)
363 char line[128], *tmp, *optr = NULL;
364 char *old_data = NULL;
368 if (strlen(rom_fname_loaded) == 0) return -1;
370 f = fopen(fname, "r");
373 fseek(f, 0, SEEK_END);
375 fseek(f, 0, SEEK_SET);
376 old_data = malloc(size + size/8);
377 if (old_data != NULL)
382 tmp = fgets(line, sizeof(line), f);
383 if (tmp == NULL) break;
385 if (strncasecmp(line, "LastUsedROM", 11) == 0)
387 sprintf(optr, "%s", line);
388 optr += strlen(optr);
394 f = fopen(fname, "w");
395 if (f == NULL) return -1;
397 if (old_data != NULL) {
398 fwrite(old_data, 1, optr - old_data, f);
401 fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
406 /* --------------------------------------------------------------------------*/
408 int config_readlrom(const char *fname)
410 char line[128], *tmp;
411 int i, len, ret = -1;
414 f = fopen(fname, "r");
415 if (f == NULL) return -1;
417 // seek to the section needed
420 tmp = fgets(line, sizeof(line), f);
421 if (tmp == NULL) break;
423 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
425 for (i = 0; i < len; i++)
426 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
427 tmp = strchr(line, '=');
428 if (tmp == NULL) break;
432 len = sizeof(rom_fname_loaded);
433 strncpy(rom_fname_loaded, tmp, len);
434 rom_fname_loaded[len-1] = 0;
443 static int custom_read(menu_entry *me, const char *var, const char *val)
449 case MA_OPT_FRAMESKIP:
450 if (strcasecmp(var, "Frameskip") != 0) return 0;
451 if (strcasecmp(val, "Auto") == 0)
452 currentConfig.Frameskip = -1;
453 else currentConfig.Frameskip = atoi(val);
456 case MA_OPT_SOUND_QUALITY:
457 if (strcasecmp(var, "Sound Quality") != 0) return 0;
458 PsndRate = strtoul(val, &tmp, 10);
459 if (PsndRate < 8000 || PsndRate > 44100)
461 if (*tmp == 'H' || *tmp == 'h') tmp++;
462 if (*tmp == 'Z' || *tmp == 'z') tmp++;
463 while (*tmp == ' ') tmp++;
464 if (strcasecmp(tmp, "stereo") == 0) {
465 PicoOpt |= POPT_EN_STEREO;
466 } else if (strcasecmp(tmp, "mono") == 0) {
467 PicoOpt &= ~POPT_EN_STEREO;
473 if (strcasecmp(var, "Region") != 0) return 0;
474 if (strncasecmp(val, "Auto: ", 6) == 0)
476 const char *p = val + 5, *end = val + strlen(val);
478 PicoRegionOverride = PicoAutoRgnOrder = 0;
479 for (i = 0; p < end && i < 3; i++)
481 while (*p == ' ') p++;
482 if (p[0] == 'J' && p[1] == 'P') {
483 PicoAutoRgnOrder |= 1 << (i*4);
484 } else if (p[0] == 'U' && p[1] == 'S') {
485 PicoAutoRgnOrder |= 4 << (i*4);
486 } else if (p[0] == 'E' && p[1] == 'U') {
487 PicoAutoRgnOrder |= 8 << (i*4);
489 while (*p != ' ' && *p != 0) p++;
493 else if (strcasecmp(val, "Auto") == 0) {
494 PicoRegionOverride = 0;
495 } else if (strcasecmp(val, "Japan NTSC") == 0) {
496 PicoRegionOverride = 1;
497 } else if (strcasecmp(val, "Japan PAL") == 0) {
498 PicoRegionOverride = 2;
499 } else if (strcasecmp(val, "USA") == 0) {
500 PicoRegionOverride = 4;
501 } else if (strcasecmp(val, "Europe") == 0) {
502 PicoRegionOverride = 8;
508 if (strcasecmp(var, "Gamma correction") != 0) return 0;
509 currentConfig.gamma = (int) (atof(val) * 100.0);
512 case MA_CDOPT_READAHEAD:
513 if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
514 PicoCDBuffers = atoi(val) / 2;
517 case MA_32XOPT_MSH2_CYCLES:
518 case MA_32XOPT_SSH2_CYCLES: {
519 int *mul = (me->id == MA_32XOPT_MSH2_CYCLES) ? &p32x_msh2_multiplier : &p32x_ssh2_multiplier;
520 *mul = ((unsigned int)atoi(val) << SH2_MULTI_SHIFT) / 7670;
526 if (strcasecmp(var, "Scale factor") != 0) return 0;
527 currentConfig.scale = atof(val);
529 case MA_OPT3_HSCALE32:
530 if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
531 currentConfig.hscale32 = atof(val);
533 case MA_OPT3_HSCALE40:
534 if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
535 currentConfig.hscale40 = atof(val);
539 if (strcasecmp(var, "Wait for vsync") != 0) return 0;
540 if (strcasecmp(val, "never") == 0) {
541 currentConfig.EmuOpt &= ~0x12000;
542 } else if (strcasecmp(val, "sometimes") == 0) {
543 currentConfig.EmuOpt |= 0x12000;
544 } else if (strcasecmp(val, "always") == 0) {
545 currentConfig.EmuOpt &= ~0x12000;
546 currentConfig.EmuOpt |= 0x02000;
552 lprintf("unhandled custom_read %i: %s\n", me->id, var);
558 static unsigned int keys_encountered = 0;
560 static int parse_bind_val(const char *val, int *type)
564 *type = IN_BINDTYPE_NONE;
568 if (strncasecmp(val, "player", 6) == 0)
570 int player, shift = 0;
571 player = atoi(val + 6) - 1;
578 *type = IN_BINDTYPE_PLAYER12;
579 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
580 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
581 return me_ctrl_actions[i].mask << shift;
584 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
585 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
586 *type = IN_BINDTYPE_EMU;
587 return emuctrl_actions[i].mask;
594 static void keys_parse(const char *key, const char *val, int dev_id)
598 acts = parse_bind_val(val, &type);
600 lprintf("config: unhandled action \"%s\"\n", val);
604 in_config_bind_key(dev_id, key, acts, type);
607 static int get_numvar_num(const char *var)
615 num = strtoul(var, &p, 10);
616 if (*p == 0 || *p == ' ')
622 /* map dev number in confing to input dev number */
623 static unsigned char input_dev_map[IN_MAX_DEVS];
625 static void parse(const char *var, const char *val)
630 if (strcasecmp(var, "LastUsedROM") == 0)
631 return; /* handled elsewhere */
633 if (strcasecmp(var, "Sound Volume") == 0) {
634 currentConfig.volume = atoi(val);
638 /* input: device name */
639 if (strncasecmp(var, "input", 5) == 0) {
640 int num = get_numvar_num(var + 5);
641 if (num >= 0 && num < IN_MAX_DEVS)
642 input_dev_map[num] = in_config_parse_dev(val);
644 lprintf("config: failed to parse: %s\n", var);
649 if (strncasecmp(var, "bind", 4) == 0) {
650 const char *p = var + 4;
651 int num = get_numvar_num(p);
652 if (num < 0 || num >= IN_MAX_DEVS) {
653 lprintf("config: failed to parse: %s\n", var);
657 num = input_dev_map[num];
658 if (num < 0 || num >= IN_MAX_DEVS) {
659 lprintf("config: invalid device id: %s\n", var);
663 while (*p && *p != ' ') p++;
664 while (*p && *p == ' ') p++;
665 keys_parse(p, val, num);
669 for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
673 if (!me->need_to_save)
675 if (me->name == NULL || strcasecmp(var, me->name) != 0)
678 if (me->beh == MB_OPT_ONOFF) {
679 tmp = strtol(val, &p, 0);
682 if (tmp) *(int *)me->var |= me->mask;
683 else *(int *)me->var &= ~me->mask;
686 else if (me->beh == MB_OPT_RANGE) {
687 tmp = strtol(val, &p, 0);
690 if (tmp < me->min) tmp = me->min;
691 if (tmp > me->max) tmp = me->max;
692 *(int *)me->var = tmp;
695 else if (me->beh == MB_OPT_ENUM) {
696 const char **names, *p1;
699 names = (const char **)me->data;
702 for (i = 0; names[i] != NULL; i++) {
703 for (p1 = names[i]; *p1 == ' '; p1++)
705 if (strcasecmp(p1, val) == 0) {
712 else if (custom_read(me, var, val))
716 lprintf("config_readsect: unhandled var: \"%s\"\n", var);
720 lprintf("config_readsect: unhandled val for \"%s\": %s\n", var, val);
724 int config_havesect(const char *fname, const char *section)
729 f = fopen(fname, "r");
730 if (f == NULL) return 0;
732 ret = seek_sect(f, section);
737 int config_readsect(const char *fname, const char *section)
739 char line[128], *var, *val;
743 f = fopen(fname, "r");
744 if (f == NULL) return -1;
748 ret = seek_sect(f, section);
750 lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
756 keys_encountered = 0;
757 memset(input_dev_map, 0xff, sizeof(input_dev_map));
762 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
764 if (ret == -1) continue;
776 static char *mystrip(char *str)
781 for (i = 0; i < len; i++)
782 if (str[i] != ' ') break;
783 if (i > 0) memmove(str, str + i, len - i + 1);
786 for (i = len - 1; i >= 0; i--)
787 if (str[i] != ' ') break;
796 * -1 - failed to parse line
798 int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
800 char *var, *val, *tmp;
804 tmp = fgets(line, lsize, f);
805 if (tmp == NULL) return 0;
807 if (line[0] == '[') return 0; // other section
809 // strip comments, linefeed, spaces..
811 for (i = 0; i < len; i++)
812 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
815 if (len <= 0) return -1;;
818 for (i = 0; i < len; i++)
819 if (line[i] == '=') break;
820 if (i >= len || strchr(&line[i+1], '=') != NULL) {
821 lprintf("config_readsect: can't parse: %s\n", line);
831 if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
832 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);