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 custom_write(FILE *f, const menu_entry *me, int no_def)
59 /* TODO: this should be rm'd when PSP menu is converted */
61 if (no_def && defaultConfig.scale == currentConfig.scale) return;
62 fprintf(f, "Scale factor = %.2f", currentConfig.scale);
64 case MA_OPT3_HSCALE32:
65 if (no_def && defaultConfig.hscale32 == currentConfig.hscale32) return;
66 fprintf(f, "Hor. scale (for low res. games) = %.2f", currentConfig.hscale32);
68 case MA_OPT3_HSCALE40:
69 if (no_def && defaultConfig.hscale40 == currentConfig.hscale40) return;
70 fprintf(f, "Hor. scale (for hi res. games) = %.2f", currentConfig.hscale40);
72 case MA_OPT3_FILTERING:
73 if (no_def && defaultConfig.scaling == currentConfig.scaling) return;
74 fprintf(f, "Bilinear filtering = %i", currentConfig.scaling);
77 if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
78 fprintf(f, "Gamma adjustment = %i", currentConfig.gamma);
80 case MA_OPT3_BLACKLVL:
81 if (no_def && defaultConfig.gamma2 == currentConfig.gamma2) return;
82 fprintf(f, "Black level = %i", currentConfig.gamma2);
85 if (no_def && (defaultConfig.EmuOpt&0x12000) == (currentConfig.gamma2&0x12000)) return;
86 strcpy(str24, "never");
87 if (currentConfig.EmuOpt & 0x2000)
88 strcpy(str24, (currentConfig.EmuOpt & 0x10000) ? "sometimes" : "always");
89 fprintf(f, "Wait for vsync = %s", str24);
93 lprintf("unhandled custom_write: %i\n", me->id);
100 static void keys_write(FILE *fn, const char *bind_str, int dev_id, const int *binds, int no_defaults)
104 const int *def_binds;
106 key_count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);
107 def_binds = in_get_dev_def_binds(dev_id);
109 for (k = 0; k < key_count; k++)
113 act[0] = act[31] = 0;
115 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
116 if (binds[IN_BIND_OFFS(k, t)] != def_binds[IN_BIND_OFFS(k, t)])
119 if (no_defaults && t == IN_BINDTYPE_COUNT)
120 continue; /* no change from defaults */
122 name = in_get_key_name(dev_id, k);
124 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
125 if (binds[IN_BIND_OFFS(k, t)] != 0 || def_binds[IN_BIND_OFFS(k, t)] == 0)
128 if (t == IN_BINDTYPE_COUNT) {
129 /* key has default bind removed */
130 fprintf(fn, "%s %s =" NL, bind_str, name);
134 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
135 mask = me_ctrl_actions[i].mask;
136 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
137 strncpy(act, me_ctrl_actions[i].name, 31);
138 fprintf(fn, "%s %s = player1 %s" NL, bind_str, name, mystrip(act));
140 mask = me_ctrl_actions[i].mask << 16;
141 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
142 strncpy(act, me_ctrl_actions[i].name, 31);
143 fprintf(fn, "%s %s = player2 %s" NL, bind_str, name, mystrip(act));
147 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
148 mask = emuctrl_actions[i].mask;
149 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
150 strncpy(act, emuctrl_actions[i].name, 31);
151 fprintf(fn, "%s %s = %s" NL, bind_str, name, mystrip(act));
157 /* XXX: this should go to menu structures instead */
158 static int default_var(const menu_entry *me)
162 case MA_OPT2_ENABLE_YM2612:
163 case MA_OPT2_ENABLE_SN76496:
164 case MA_OPT2_ENABLE_Z80:
165 case MA_OPT_6BUTTON_PAD:
166 case MA_OPT_ACC_SPRITES:
167 case MA_OPT_ARM940_SOUND:
170 case MA_CDOPT_SCALEROT_CHIP:
171 case MA_CDOPT_BETTER_SYNC:
172 case MA_CDOPT_SAVERAM:
173 case MA_OPT2_SVP_DYNAREC:
174 case MA_OPT2_NO_SPRITE_LIM:
175 case MA_OPT2_NO_IDLE_LOOPS:
176 return defaultConfig.s_PicoOpt;
178 case MA_OPT_SRAM_STATES:
179 case MA_OPT_SHOW_FPS:
180 case MA_OPT_ENABLE_SOUND:
181 case MA_OPT2_GZIP_STATES:
182 case MA_OPT2_SQUIDGEHACK:
183 case MA_OPT2_NO_LAST_ROM:
184 case MA_OPT2_RAMTIMINGS:
186 case MA_OPT2_A_SN_GAMMA:
188 case MA_OPT_INTERLACED:
189 case MA_OPT2_DBLBUFF:
190 case MA_OPT2_STATUS_LINE:
191 case MA_OPT2_NO_FRAME_LIMIT:
192 case MA_OPT_TEARING_FIX:
193 return defaultConfig.EmuOpt;
195 case MA_CTRL_TURBO_RATE: return defaultConfig.turbo_rate;
196 case MA_OPT_SCALING: return defaultConfig.scaling;
197 case MA_OPT_ROTATION: return defaultConfig.rotation;
198 case MA_OPT2_GAMMA: return defaultConfig.gamma;
199 case MA_OPT_FRAMESKIP: return defaultConfig.Frameskip;
200 case MA_OPT_CPU_CLOCKS: return defaultConfig.CPUclock;
201 case MA_OPT_RENDERER: return defaultConfig.renderer;
203 case MA_OPT_SAVE_SLOT:
209 static int is_cust_val_default(const menu_entry *me)
214 return defaultConfig.s_PicoRegion == PicoRegionOverride &&
215 defaultConfig.s_PicoAutoRgnOrder == PicoAutoRgnOrder;
216 case MA_OPT_SOUND_QUALITY:
217 return defaultConfig.s_PsndRate == PsndRate &&
218 ((defaultConfig.s_PicoOpt ^ PicoOpt) & POPT_EN_STEREO) == 0;
219 case MA_OPT_CONFIRM_STATES:
220 return !((defaultConfig.EmuOpt ^ currentConfig.EmuOpt) &
221 (EOPT_CONFIRM_LOAD|EOPT_CONFIRM_SAVE)) == 0;
222 case MA_CDOPT_READAHEAD:
223 return defaultConfig.s_PicoCDBuffers == PicoCDBuffers;
227 lprintf("is_cust_val_default: unhandled id %i\n", me->id);
231 int config_writesect(const char *fname, const char *section)
233 FILE *fo = NULL, *fn = NULL; // old and new
234 int no_defaults = 0; // avoid saving defaults
237 char line[128], *tmp;
243 fo = fopen(fname, "r");
245 fn = fopen(fname, "w");
249 ret = seek_sect(fo, section);
251 // sect not found, we can simply append
252 fclose(fo); fo = NULL;
253 fn = fopen(fname, "a");
259 rename(fname, "tmp.cfg");
260 fo = fopen("tmp.cfg", "r");
261 fn = fopen(fname, "w");
262 if (fo == NULL || fn == NULL) goto write;
264 // copy everything until sect
265 tlen = strlen(section);
268 tmp = fgets(line, sizeof(line), fo);
269 if (tmp == NULL) break;
271 if (line[0] == '[' && strncmp(line + 1, section, tlen) == 0 && line[tlen+1] == ']')
276 // now skip to next sect
279 tmp = fgets(line, sizeof(line), fo);
280 if (tmp == NULL) break;
281 if (line[0] == '[') {
282 fseek(fo, -strlen(line), SEEK_CUR);
288 fclose(fo); fo = NULL;
294 fn = fopen(fname, "w");
303 fprintf(fn, "[%s]" NL, section);
305 me = me_list_get_first();
309 if (!me->need_to_save)
311 if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
312 if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask))
313 fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
314 } else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
315 if (!no_defaults || (*(int *)me->var ^ default_var(me)))
316 fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
317 } else if (me->name != NULL && me->generate_name != NULL) {
318 if (!no_defaults || !is_cust_val_default(me)) {
319 strncpy(line, me->generate_name(0, &dummy), sizeof(line));
320 line[sizeof(line) - 1] = 0;
322 fprintf(fn, "%s = %s" NL, me->name, line);
325 custom_write(fn, me, no_defaults);
327 me = me_list_get_next();
330 /* input: save device names */
331 for (t = 0; t < IN_MAX_DEVS; t++)
333 const int *binds = in_get_dev_binds(t);
334 const char *name = in_get_dev_name(t, 0, 0);
335 if (binds == NULL || name == NULL)
338 fprintf(fn, "input%d = %s" NL, t, name);
341 /* input: save binds */
342 for (t = 0; t < IN_MAX_DEVS; t++)
344 const int *binds = in_get_dev_binds(t);
345 const char *name = in_get_dev_name(t, 0, 0);
349 if (binds == NULL || name == NULL)
352 sprintf(strbind, "bind%d", t);
353 if (t == 0) strbind[4] = 0;
355 count = in_get_dev_info(t, IN_INFO_BIND_COUNT);
356 keys_write(fn, strbind, t, binds, no_defaults);
361 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
368 // copy whatever is left
371 tmp = fgets(line, sizeof(line), fo);
372 if (tmp == NULL) break;
385 int config_writelrom(const char *fname)
387 char line[128], *tmp, *optr = NULL;
388 char *old_data = NULL;
392 if (strlen(rom_fname_loaded) == 0) return -1;
394 f = fopen(fname, "r");
397 fseek(f, 0, SEEK_END);
399 fseek(f, 0, SEEK_SET);
400 old_data = malloc(size + size/8);
401 if (old_data != NULL)
406 tmp = fgets(line, sizeof(line), f);
407 if (tmp == NULL) break;
409 if (strncasecmp(line, "LastUsedROM", 11) == 0)
411 sprintf(optr, "%s", line);
412 optr += strlen(optr);
418 f = fopen(fname, "w");
419 if (f == NULL) return -1;
421 if (old_data != NULL) {
422 fwrite(old_data, 1, optr - old_data, f);
425 fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
430 /* --------------------------------------------------------------------------*/
432 int config_readlrom(const char *fname)
434 char line[128], *tmp;
435 int i, len, ret = -1;
438 f = fopen(fname, "r");
439 if (f == NULL) return -1;
441 // seek to the section needed
444 tmp = fgets(line, sizeof(line), f);
445 if (tmp == NULL) break;
447 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
449 for (i = 0; i < len; i++)
450 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
451 tmp = strchr(line, '=');
452 if (tmp == NULL) break;
456 len = sizeof(rom_fname_loaded);
457 strncpy(rom_fname_loaded, tmp, len);
458 rom_fname_loaded[len-1] = 0;
467 static int custom_read(menu_entry *me, const char *var, const char *val)
474 case MA_OPT_RENDERER:
475 if (strcasecmp(var, "Renderer") != 0 || renderer_names == NULL)
478 for (i = 0; renderer_names[i] != NULL; i++) {
479 if (strcasecmp(val, renderer_names[i]) == 0) {
480 currentConfig.renderer = i;
488 if (strcasecmp(var, "Scaling") != 0) return 0;
489 if (strcasecmp(val, "OFF") == 0) {
490 currentConfig.scaling = EOPT_SCALE_NONE;
491 } else if (strcasecmp(val, "hw horizontal") == 0) {
492 currentConfig.scaling = EOPT_SCALE_HW_H;
493 } else if (strcasecmp(val, "hw horiz. + vert.") == 0) {
494 currentConfig.scaling = EOPT_SCALE_HW_HV;
495 } else if (strcasecmp(val, "sw horizontal") == 0) {
496 currentConfig.scaling = EOPT_SCALE_SW_H;
498 currentConfig.scaling = atoi(val);
504 case MA_OPT_FRAMESKIP:
505 if (strcasecmp(var, "Frameskip") != 0) return 0;
506 if (strcasecmp(val, "Auto") == 0)
507 currentConfig.Frameskip = -1;
508 else currentConfig.Frameskip = atoi(val);
511 case MA_OPT_SOUND_QUALITY:
512 if (strcasecmp(var, "Sound Quality") != 0) return 0;
513 PsndRate = strtoul(val, &tmp, 10);
514 if (PsndRate < 8000 || PsndRate > 44100)
516 if (*tmp == 'H' || *tmp == 'h') tmp++;
517 if (*tmp == 'Z' || *tmp == 'z') tmp++;
518 while (*tmp == ' ') tmp++;
519 if (strcasecmp(tmp, "stereo") == 0) {
520 PicoOpt |= POPT_EN_STEREO;
521 } else if (strcasecmp(tmp, "mono") == 0) {
522 PicoOpt &= ~POPT_EN_STEREO;
528 if (strcasecmp(var, "Region") != 0) return 0;
529 if (strncasecmp(val, "Auto: ", 6) == 0)
531 const char *p = val + 5, *end = val + strlen(val);
533 PicoRegionOverride = PicoAutoRgnOrder = 0;
534 for (i = 0; p < end && i < 3; i++)
536 while (*p == ' ') p++;
537 if (p[0] == 'J' && p[1] == 'P') {
538 PicoAutoRgnOrder |= 1 << (i*4);
539 } else if (p[0] == 'U' && p[1] == 'S') {
540 PicoAutoRgnOrder |= 4 << (i*4);
541 } else if (p[0] == 'E' && p[1] == 'U') {
542 PicoAutoRgnOrder |= 8 << (i*4);
544 while (*p != ' ' && *p != 0) p++;
548 else if (strcasecmp(val, "Auto") == 0) {
549 PicoRegionOverride = 0;
550 } else if (strcasecmp(val, "Japan NTSC") == 0) {
551 PicoRegionOverride = 1;
552 } else if (strcasecmp(val, "Japan PAL") == 0) {
553 PicoRegionOverride = 2;
554 } else if (strcasecmp(val, "USA") == 0) {
555 PicoRegionOverride = 4;
556 } else if (strcasecmp(val, "Europe") == 0) {
557 PicoRegionOverride = 8;
562 case MA_OPT_CONFIRM_STATES:
563 if (strcasecmp(var, "Confirm savestate") != 0) return 0;
564 if (strcasecmp(val, "OFF") == 0) {
565 currentConfig.EmuOpt &= ~(5<<9);
566 } else if (strcasecmp(val, "writes") == 0) {
567 currentConfig.EmuOpt &= ~(5<<9);
568 currentConfig.EmuOpt |= 1<<9;
569 } else if (strcasecmp(val, "loads") == 0) {
570 currentConfig.EmuOpt &= ~(5<<9);
571 currentConfig.EmuOpt |= 4<<9;
572 } else if (strcasecmp(val, "both") == 0) {
573 currentConfig.EmuOpt &= ~(5<<9);
574 currentConfig.EmuOpt |= 5<<9;
580 if (strcasecmp(var, "Gamma correction") != 0) return 0;
581 currentConfig.gamma = (int) (atof(val) * 100.0);
584 case MA_OPT2_SQUIDGEHACK:
585 if (strcasecmp(var, "Squidgehack") != 0) return 0;
587 if (i) *(int *)me->var |= me->mask;
588 else *(int *)me->var &= ~me->mask;
591 case MA_CDOPT_READAHEAD:
592 if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
593 PicoCDBuffers = atoi(val) / 2;
598 if (strcasecmp(var, "Scale factor") != 0) return 0;
599 currentConfig.scale = atof(val);
601 case MA_OPT3_HSCALE32:
602 if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
603 currentConfig.hscale32 = atof(val);
605 case MA_OPT3_HSCALE40:
606 if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
607 currentConfig.hscale40 = atof(val);
609 case MA_OPT3_FILTERING:
610 if (strcasecmp(var, "Bilinear filtering") != 0) return 0;
611 currentConfig.scaling = atoi(val);
614 if (strcasecmp(var, "Gamma adjustment") != 0) return 0;
615 currentConfig.gamma = atoi(val);
617 case MA_OPT3_BLACKLVL:
618 if (strcasecmp(var, "Black level") != 0) return 0;
619 currentConfig.gamma2 = atoi(val);
622 if (strcasecmp(var, "Wait for vsync") != 0) return 0;
623 if (strcasecmp(val, "never") == 0) {
624 currentConfig.EmuOpt &= ~0x12000;
625 } else if (strcasecmp(val, "sometimes") == 0) {
626 currentConfig.EmuOpt |= 0x12000;
627 } else if (strcasecmp(val, "always") == 0) {
628 currentConfig.EmuOpt &= ~0x12000;
629 currentConfig.EmuOpt |= 0x02000;
635 lprintf("unhandled custom_read: %i\n", me->id);
641 static unsigned int keys_encountered = 0;
643 static int parse_bind_val(const char *val, int *type)
647 *type = IN_BINDTYPE_NONE;
651 if (strncasecmp(val, "player", 6) == 0)
653 int player, shift = 0;
654 player = atoi(val + 6) - 1;
661 *type = IN_BINDTYPE_PLAYER12;
662 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
663 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
664 return me_ctrl_actions[i].mask << shift;
667 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
668 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
669 *type = IN_BINDTYPE_EMU;
670 return emuctrl_actions[i].mask;
677 static void keys_parse(const char *key, const char *val, int dev_id)
681 acts = parse_bind_val(val, &type);
683 lprintf("config: unhandled action \"%s\"\n", val);
687 in_config_bind_key(dev_id, key, acts, type);
690 static int get_numvar_num(const char *var)
698 num = strtoul(var, &p, 10);
699 if (*p == 0 || *p == ' ')
705 /* map dev number in confing to input dev number */
706 static unsigned char input_dev_map[IN_MAX_DEVS];
708 static void parse(const char *var, const char *val)
713 if (strcasecmp(var, "LastUsedROM") == 0)
714 return; /* handled elsewhere */
716 if (strcasecmp(var, "Sound Volume") == 0) {
717 currentConfig.volume = atoi(val);
721 /* input: device name */
722 if (strncasecmp(var, "input", 5) == 0) {
723 int num = get_numvar_num(var + 5);
724 if (num >= 0 && num < IN_MAX_DEVS)
725 input_dev_map[num] = in_config_parse_dev(val);
727 lprintf("config: failed to parse: %s\n", var);
732 if (strncasecmp(var, "bind", 4) == 0) {
733 const char *p = var + 4;
734 int num = get_numvar_num(p);
735 if (num < 0 || num >= IN_MAX_DEVS) {
736 lprintf("config: failed to parse: %s\n", var);
740 num = input_dev_map[num];
741 if (num < 0 || num >= IN_MAX_DEVS) {
742 lprintf("config: invalid device id: %s\n", var);
746 while (*p && *p != ' ') p++;
747 while (*p && *p == ' ') p++;
748 keys_parse(p, val, num);
752 me = me_list_get_first();
753 while (me != NULL && ret == 0)
755 if (!me->need_to_save)
757 if (me->name != NULL && me->name[0] != 0) {
758 if (strcasecmp(var, me->name) != 0)
759 goto next; /* surely not this one */
760 if (me->beh == MB_OPT_ONOFF) {
762 if (tmp) *(int *)me->var |= me->mask;
763 else *(int *)me->var &= ~me->mask;
765 } else if (me->beh == MB_OPT_RANGE) {
767 if (tmp < me->min) tmp = me->min;
768 if (tmp > me->max) tmp = me->max;
769 *(int *)me->var = tmp;
773 ret = custom_read(me, var, val);
775 me = me_list_get_next();
777 if (!ret) lprintf("config_readsect: unhandled var: \"%s\"\n", var);
781 int config_havesect(const char *fname, const char *section)
786 f = fopen(fname, "r");
787 if (f == NULL) return 0;
789 ret = seek_sect(f, section);
794 int config_readsect(const char *fname, const char *section)
796 char line[128], *var, *val;
800 f = fopen(fname, "r");
801 if (f == NULL) return -1;
805 ret = seek_sect(f, section);
807 lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
813 keys_encountered = 0;
814 memset(input_dev_map, 0xff, sizeof(input_dev_map));
819 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
821 if (ret == -1) continue;
833 static char *mystrip(char *str)
838 for (i = 0; i < len; i++)
839 if (str[i] != ' ') break;
840 if (i > 0) memmove(str, str + i, len - i + 1);
843 for (i = len - 1; i >= 0; i--)
844 if (str[i] != ' ') break;
853 * -1 - failed to parse line
855 int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
857 char *var, *val, *tmp;
861 tmp = fgets(line, lsize, f);
862 if (tmp == NULL) return 0;
864 if (line[0] == '[') return 0; // other section
866 // strip comments, linefeed, spaces..
868 for (i = 0; i < len; i++)
869 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
872 if (len <= 0) return -1;;
875 for (i = 0; i < len; i++)
876 if (line[i] == '=') break;
877 if (i >= len || strchr(&line[i+1], '=') != NULL) {
878 lprintf("config_readsect: can't parse: %s\n", line);
888 if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
889 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);