2 * Human-readable config file management for PicoDrive
16 static char *mystrip(char *str);
22 #include <pico/pico.h>
26 static int seek_sect(FILE *f, const char *section)
31 len = strlen(section);
32 // seek to the section needed
35 tmp = fgets(line, sizeof(line), f);
36 if (tmp == NULL) break;
38 if (line[0] != '[') continue; // not section start
39 if (strncmp(line + 1, section, len) == 0 && line[len+1] == ']')
47 static void custom_write(FILE *f, const menu_entry *me, int no_def)
53 /* TODO: this should be rm'd when PSP menu is converted */
55 if (no_def && defaultConfig.scale == currentConfig.scale) return;
56 fprintf(f, "Scale factor = %.2f", currentConfig.scale);
58 case MA_OPT3_HSCALE32:
59 if (no_def && defaultConfig.hscale32 == currentConfig.hscale32) return;
60 fprintf(f, "Hor. scale (for low res. games) = %.2f", currentConfig.hscale32);
62 case MA_OPT3_HSCALE40:
63 if (no_def && defaultConfig.hscale40 == currentConfig.hscale40) return;
64 fprintf(f, "Hor. scale (for hi res. games) = %.2f", currentConfig.hscale40);
66 case MA_OPT3_FILTERING:
67 if (no_def && defaultConfig.scaling == currentConfig.scaling) return;
68 fprintf(f, "Bilinear filtering = %i", currentConfig.scaling);
71 if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
72 fprintf(f, "Gamma adjustment = %i", currentConfig.gamma);
74 case MA_OPT3_BLACKLVL:
75 if (no_def && defaultConfig.gamma2 == currentConfig.gamma2) return;
76 fprintf(f, "Black level = %i", currentConfig.gamma2);
79 if (no_def && (defaultConfig.EmuOpt&0x12000) == (currentConfig.gamma2&0x12000)) return;
80 strcpy(str24, "never");
81 if (currentConfig.EmuOpt & 0x2000)
82 strcpy(str24, (currentConfig.EmuOpt & 0x10000) ? "sometimes" : "always");
83 fprintf(f, "Wait for vsync = %s", str24);
87 lprintf("unhandled custom_write: %i\n", me->id);
94 static void keys_write(FILE *fn, const char *bind_str, int dev_id, const int *binds, int no_defaults)
100 key_count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);
101 def_binds = in_get_dev_def_binds(dev_id);
103 for (k = 0; k < key_count; k++)
107 act[0] = act[31] = 0;
109 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
110 if (binds[IN_BIND_OFFS(k, t)] != def_binds[IN_BIND_OFFS(k, t)])
113 if (no_defaults && t == IN_BINDTYPE_COUNT)
114 continue; /* no change from defaults */
116 name = in_get_key_name(dev_id, k);
118 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
119 if (binds[IN_BIND_OFFS(k, t)] != 0 || def_binds[IN_BIND_OFFS(k, t)] == 0)
122 if (t == IN_BINDTYPE_COUNT) {
123 /* key has default bind removed */
124 fprintf(fn, "%s %s =" NL, bind_str, name);
128 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
129 mask = me_ctrl_actions[i].mask;
130 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
131 strncpy(act, me_ctrl_actions[i].name, 31);
132 fprintf(fn, "%s %s = player1 %s" NL, bind_str, name, mystrip(act));
134 mask = me_ctrl_actions[i].mask << 16;
135 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
136 strncpy(act, me_ctrl_actions[i].name, 31);
137 fprintf(fn, "%s %s = player2 %s" NL, bind_str, name, mystrip(act));
141 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
142 mask = emuctrl_actions[i].mask;
143 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
144 strncpy(act, emuctrl_actions[i].name, 31);
145 fprintf(fn, "%s %s = %s" NL, bind_str, name, mystrip(act));
151 /* XXX: this should go to menu structures instead */
152 static int default_var(const menu_entry *me)
156 case MA_OPT2_ENABLE_YM2612:
157 case MA_OPT2_ENABLE_SN76496:
158 case MA_OPT2_ENABLE_Z80:
159 case MA_OPT_6BUTTON_PAD:
160 case MA_OPT_ACC_SPRITES:
161 case MA_OPT_ARM940_SOUND:
164 case MA_CDOPT_SCALEROT_CHIP:
165 case MA_CDOPT_BETTER_SYNC:
166 case MA_CDOPT_SAVERAM:
167 case MA_OPT2_SVP_DYNAREC:
168 case MA_OPT2_NO_SPRITE_LIM:
169 case MA_OPT2_NO_IDLE_LOOPS:
170 return defaultConfig.s_PicoOpt;
172 case MA_OPT_SRAM_STATES:
173 case MA_OPT_SHOW_FPS:
174 case MA_OPT_ENABLE_SOUND:
175 case MA_OPT2_GZIP_STATES:
176 case MA_OPT2_SQUIDGEHACK:
177 case MA_OPT2_NO_LAST_ROM:
178 case MA_OPT2_RAMTIMINGS:
180 case MA_OPT2_A_SN_GAMMA:
182 case MA_OPT_INTERLACED:
183 case MA_OPT2_DBLBUFF:
184 case MA_OPT2_STATUS_LINE:
185 case MA_OPT2_NO_FRAME_LIMIT:
186 case MA_OPT_TEARING_FIX:
187 return defaultConfig.EmuOpt;
189 case MA_CTRL_TURBO_RATE: return defaultConfig.turbo_rate;
190 case MA_OPT_SCALING: return defaultConfig.scaling;
191 case MA_OPT_ROTATION: return defaultConfig.rotation;
192 case MA_OPT2_GAMMA: return defaultConfig.gamma;
193 case MA_OPT_FRAMESKIP: return defaultConfig.Frameskip;
194 case MA_OPT_CPU_CLOCKS: return defaultConfig.CPUclock;
196 case MA_OPT_SAVE_SLOT:
202 static int is_cust_val_default(const menu_entry *me)
207 return defaultConfig.s_PicoRegion == PicoRegionOverride &&
208 defaultConfig.s_PicoAutoRgnOrder == PicoAutoRgnOrder;
209 case MA_OPT_SOUND_QUALITY:
210 return defaultConfig.s_PsndRate == PsndRate &&
211 ((defaultConfig.s_PicoOpt ^ PicoOpt) & POPT_EN_STEREO) == 0;
212 case MA_OPT_CONFIRM_STATES:
213 return !((defaultConfig.EmuOpt ^ currentConfig.EmuOpt) &
214 (EOPT_CONFIRM_LOAD|EOPT_CONFIRM_SAVE)) == 0;
215 case MA_OPT_RENDERER:
216 return ((defaultConfig.s_PicoOpt ^ PicoOpt) & POPT_ALT_RENDERER) == 0 &&
217 ((defaultConfig.EmuOpt ^ currentConfig.EmuOpt) & EOPT_16BPP) == 0;
218 case MA_CDOPT_READAHEAD:
219 return defaultConfig.s_PicoCDBuffers == PicoCDBuffers;
223 lprintf("is_cust_val_default: unhandled id %i\n", me->id);
227 int config_writesect(const char *fname, const char *section)
229 FILE *fo = NULL, *fn = NULL; // old and new
230 int no_defaults = 0; // avoid saving defaults
233 char line[128], *tmp;
239 fo = fopen(fname, "r");
241 fn = fopen(fname, "w");
245 ret = seek_sect(fo, section);
247 // sect not found, we can simply append
248 fclose(fo); fo = NULL;
249 fn = fopen(fname, "a");
255 rename(fname, "tmp.cfg");
256 fo = fopen("tmp.cfg", "r");
257 fn = fopen(fname, "w");
258 if (fo == NULL || fn == NULL) goto write;
260 // copy everything until sect
261 tlen = strlen(section);
264 tmp = fgets(line, sizeof(line), fo);
265 if (tmp == NULL) break;
267 if (line[0] == '[' && strncmp(line + 1, section, tlen) == 0 && line[tlen+1] == ']')
272 // now skip to next sect
275 tmp = fgets(line, sizeof(line), fo);
276 if (tmp == NULL) break;
277 if (line[0] == '[') {
278 fseek(fo, -strlen(line), SEEK_CUR);
284 fclose(fo); fo = NULL;
290 fn = fopen(fname, "w");
299 fprintf(fn, "[%s]" NL, section);
301 me = me_list_get_first();
305 if (!me->need_to_save)
307 if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
308 if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask))
309 fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
310 } else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
311 if (!no_defaults || (*(int *)me->var ^ default_var(me)))
312 fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
313 } else if (me->name != NULL && me->generate_name != NULL) {
314 if (!no_defaults || !is_cust_val_default(me)) {
315 strncpy(line, me->generate_name(0, &dummy), sizeof(line));
316 line[sizeof(line) - 1] = 0;
318 fprintf(fn, "%s = %s" NL, me->name, line);
321 custom_write(fn, me, no_defaults);
323 me = me_list_get_next();
326 /* input: save device names */
327 for (t = 0; t < IN_MAX_DEVS; t++)
329 const int *binds = in_get_dev_binds(t);
330 const char *name = in_get_dev_name(t, 0, 0);
331 if (binds == NULL || name == NULL)
334 fprintf(fn, "input%d = %s" NL, t, name);
337 /* input: save binds */
338 for (t = 0; t < IN_MAX_DEVS; t++)
340 const int *binds = in_get_dev_binds(t);
341 const char *name = in_get_dev_name(t, 0, 0);
345 if (binds == NULL || name == NULL)
348 sprintf(strbind, "bind%d", t);
349 if (t == 0) strbind[4] = 0;
351 count = in_get_dev_info(t, IN_INFO_BIND_COUNT);
352 keys_write(fn, strbind, t, binds, no_defaults);
357 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
364 // copy whatever is left
367 tmp = fgets(line, sizeof(line), fo);
368 if (tmp == NULL) break;
381 int config_writelrom(const char *fname)
383 char line[128], *tmp, *optr = NULL;
384 char *old_data = NULL;
388 if (strlen(rom_fname_loaded) == 0) return -1;
390 f = fopen(fname, "r");
393 fseek(f, 0, SEEK_END);
395 fseek(f, 0, SEEK_SET);
396 old_data = malloc(size + size/8);
397 if (old_data != NULL)
402 tmp = fgets(line, sizeof(line), f);
403 if (tmp == NULL) break;
405 if (strncasecmp(line, "LastUsedROM", 11) == 0)
407 sprintf(optr, "%s", line);
408 optr += strlen(optr);
414 f = fopen(fname, "w");
415 if (f == NULL) return -1;
417 if (old_data != NULL) {
418 fwrite(old_data, 1, optr - old_data, f);
421 fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
426 /* --------------------------------------------------------------------------*/
428 int config_readlrom(const char *fname)
430 char line[128], *tmp;
431 int i, len, ret = -1;
434 f = fopen(fname, "r");
435 if (f == NULL) return -1;
437 // seek to the section needed
440 tmp = fgets(line, sizeof(line), f);
441 if (tmp == NULL) break;
443 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
445 for (i = 0; i < len; i++)
446 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
447 tmp = strchr(line, '=');
448 if (tmp == NULL) break;
452 len = sizeof(rom_fname_loaded);
453 strncpy(rom_fname_loaded, tmp, len);
454 rom_fname_loaded[len-1] = 0;
463 static int custom_read(menu_entry *me, const char *var, const char *val)
470 case MA_OPT_RENDERER:
471 if (strcasecmp(var, "Renderer") != 0) return 0;
472 if (strcasecmp(val, "8bit fast") == 0 || strcasecmp(val, "fast") == 0) {
473 PicoOpt |= POPT_ALT_RENDERER;
475 else if (strcasecmp(val, "16bit accurate") == 0 || strcasecmp(val, "accurate") == 0) {
476 PicoOpt &= ~POPT_ALT_RENDERER;
477 currentConfig.EmuOpt |= 0x80;
479 else if (strcasecmp(val, "8bit accurate") == 0) {
480 PicoOpt &= ~POPT_ALT_RENDERER;
481 currentConfig.EmuOpt &= ~0x80;
489 if (strcasecmp(var, "Scaling") != 0) return 0;
490 if (strcasecmp(val, "OFF") == 0) {
491 currentConfig.scaling = EOPT_SCALE_NONE;
492 } else if (strcasecmp(val, "hw horizontal") == 0) {
493 currentConfig.scaling = EOPT_SCALE_HW_H;
494 } else if (strcasecmp(val, "hw horiz. + vert.") == 0) {
495 currentConfig.scaling = EOPT_SCALE_HW_HV;
496 } else if (strcasecmp(val, "sw horizontal") == 0) {
497 currentConfig.scaling = EOPT_SCALE_SW_H;
499 currentConfig.scaling = atoi(val);
505 case MA_OPT_FRAMESKIP:
506 if (strcasecmp(var, "Frameskip") != 0) return 0;
507 if (strcasecmp(val, "Auto") == 0)
508 currentConfig.Frameskip = -1;
509 else currentConfig.Frameskip = atoi(val);
512 case MA_OPT_SOUND_QUALITY:
513 if (strcasecmp(var, "Sound Quality") != 0) return 0;
514 PsndRate = strtoul(val, &tmp, 10);
515 if (PsndRate < 8000 || PsndRate > 44100)
517 if (*tmp == 'H' || *tmp == 'h') tmp++;
518 if (*tmp == 'Z' || *tmp == 'z') tmp++;
519 while (*tmp == ' ') tmp++;
520 if (strcasecmp(tmp, "stereo") == 0) {
521 PicoOpt |= POPT_EN_STEREO;
522 } else if (strcasecmp(tmp, "mono") == 0) {
523 PicoOpt &= ~POPT_EN_STEREO;
529 if (strcasecmp(var, "Region") != 0) return 0;
530 if (strncasecmp(val, "Auto: ", 6) == 0)
532 const char *p = val + 5, *end = val + strlen(val);
534 PicoRegionOverride = PicoAutoRgnOrder = 0;
535 for (i = 0; p < end && i < 3; i++)
537 while (*p == ' ') p++;
538 if (p[0] == 'J' && p[1] == 'P') {
539 PicoAutoRgnOrder |= 1 << (i*4);
540 } else if (p[0] == 'U' && p[1] == 'S') {
541 PicoAutoRgnOrder |= 4 << (i*4);
542 } else if (p[0] == 'E' && p[1] == 'U') {
543 PicoAutoRgnOrder |= 8 << (i*4);
545 while (*p != ' ' && *p != 0) p++;
549 else if (strcasecmp(val, "Auto") == 0) {
550 PicoRegionOverride = 0;
551 } else if (strcasecmp(val, "Japan NTSC") == 0) {
552 PicoRegionOverride = 1;
553 } else if (strcasecmp(val, "Japan PAL") == 0) {
554 PicoRegionOverride = 2;
555 } else if (strcasecmp(val, "USA") == 0) {
556 PicoRegionOverride = 4;
557 } else if (strcasecmp(val, "Europe") == 0) {
558 PicoRegionOverride = 8;
563 case MA_OPT_CONFIRM_STATES:
564 if (strcasecmp(var, "Confirm savestate") != 0) return 0;
565 if (strcasecmp(val, "OFF") == 0) {
566 currentConfig.EmuOpt &= ~(5<<9);
567 } else if (strcasecmp(val, "writes") == 0) {
568 currentConfig.EmuOpt &= ~(5<<9);
569 currentConfig.EmuOpt |= 1<<9;
570 } else if (strcasecmp(val, "loads") == 0) {
571 currentConfig.EmuOpt &= ~(5<<9);
572 currentConfig.EmuOpt |= 4<<9;
573 } else if (strcasecmp(val, "both") == 0) {
574 currentConfig.EmuOpt &= ~(5<<9);
575 currentConfig.EmuOpt |= 5<<9;
581 if (strcasecmp(var, "Gamma correction") != 0) return 0;
582 currentConfig.gamma = (int) (atof(val) * 100.0);
585 case MA_OPT2_SQUIDGEHACK:
586 if (strcasecmp(var, "Squidgehack") != 0) return 0;
588 if (tmpi) *(int *)me->var |= me->mask;
589 else *(int *)me->var &= ~me->mask;
592 case MA_CDOPT_READAHEAD:
593 if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
594 PicoCDBuffers = atoi(val) / 2;
599 if (strcasecmp(var, "Scale factor") != 0) return 0;
600 currentConfig.scale = atof(val);
602 case MA_OPT3_HSCALE32:
603 if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
604 currentConfig.hscale32 = atof(val);
606 case MA_OPT3_HSCALE40:
607 if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
608 currentConfig.hscale40 = atof(val);
610 case MA_OPT3_FILTERING:
611 if (strcasecmp(var, "Bilinear filtering") != 0) return 0;
612 currentConfig.scaling = atoi(val);
615 if (strcasecmp(var, "Gamma adjustment") != 0) return 0;
616 currentConfig.gamma = atoi(val);
618 case MA_OPT3_BLACKLVL:
619 if (strcasecmp(var, "Black level") != 0) return 0;
620 currentConfig.gamma2 = atoi(val);
623 if (strcasecmp(var, "Wait for vsync") != 0) return 0;
624 if (strcasecmp(val, "never") == 0) {
625 currentConfig.EmuOpt &= ~0x12000;
626 } else if (strcasecmp(val, "sometimes") == 0) {
627 currentConfig.EmuOpt |= 0x12000;
628 } else if (strcasecmp(val, "always") == 0) {
629 currentConfig.EmuOpt &= ~0x12000;
630 currentConfig.EmuOpt |= 0x02000;
636 lprintf("unhandled custom_read: %i\n", me->id);
642 static unsigned int keys_encountered = 0;
644 static int parse_bind_val(const char *val, int *type)
648 *type = IN_BINDTYPE_NONE;
652 if (strncasecmp(val, "player", 6) == 0)
654 int player, shift = 0;
655 player = atoi(val + 6) - 1;
662 *type = IN_BINDTYPE_PLAYER12;
663 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
664 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
665 return me_ctrl_actions[i].mask << shift;
668 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
669 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
670 *type = IN_BINDTYPE_EMU;
671 return emuctrl_actions[i].mask;
678 static void keys_parse(const char *key, const char *val, int dev_id)
682 acts = parse_bind_val(val, &type);
684 lprintf("config: unhandled action \"%s\"\n", val);
688 in_config_bind_key(dev_id, key, acts, type);
691 static int get_numvar_num(const char *var)
699 num = strtoul(var, &p, 10);
700 if (*p == 0 || *p == ' ')
706 /* map dev number in confing to input dev number */
707 static unsigned char input_dev_map[IN_MAX_DEVS];
709 static void parse(const char *var, const char *val)
714 if (strcasecmp(var, "LastUsedROM") == 0)
715 return; /* handled elsewhere */
717 if (strcasecmp(var, "Sound Volume") == 0) {
718 currentConfig.volume = atoi(val);
722 /* input: device name */
723 if (strncasecmp(var, "input", 5) == 0) {
724 int num = get_numvar_num(var + 5);
725 if (num >= 0 && num < IN_MAX_DEVS)
726 input_dev_map[num] = in_config_parse_dev(val);
728 lprintf("config: failed to parse: %s\n", var);
733 if (strncasecmp(var, "bind", 4) == 0) {
734 const char *p = var + 4;
735 int num = get_numvar_num(p);
736 if (num < 0 || num >= IN_MAX_DEVS) {
737 lprintf("config: failed to parse: %s\n", var);
741 num = input_dev_map[num];
742 if (num < 0 || num >= IN_MAX_DEVS) {
743 lprintf("config: invalid device id: %s\n", var);
747 while (*p && *p != ' ') p++;
748 while (*p && *p == ' ') p++;
749 keys_parse(p, val, num);
753 me = me_list_get_first();
754 while (me != NULL && ret == 0)
756 if (!me->need_to_save)
758 if (me->name != NULL && me->name[0] != 0) {
759 if (strcasecmp(var, me->name) != 0)
760 goto next; /* surely not this one */
761 if (me->beh == MB_OPT_ONOFF) {
763 if (tmp) *(int *)me->var |= me->mask;
764 else *(int *)me->var &= ~me->mask;
766 } else if (me->beh == MB_OPT_RANGE) {
768 if (tmp < me->min) tmp = me->min;
769 if (tmp > me->max) tmp = me->max;
770 *(int *)me->var = tmp;
774 ret = custom_read(me, var, val);
776 me = me_list_get_next();
778 if (!ret) lprintf("config_readsect: unhandled var: \"%s\"\n", var);
782 int config_havesect(const char *fname, const char *section)
787 f = fopen(fname, "r");
788 if (f == NULL) return 0;
790 ret = seek_sect(f, section);
795 int config_readsect(const char *fname, const char *section)
797 char line[128], *var, *val;
801 f = fopen(fname, "r");
802 if (f == NULL) return -1;
806 ret = seek_sect(f, section);
808 lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
814 keys_encountered = 0;
815 memset(input_dev_map, 0xff, sizeof(input_dev_map));
820 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
822 if (ret == -1) continue;
834 static char *mystrip(char *str)
839 for (i = 0; i < len; i++)
840 if (str[i] != ' ') break;
841 if (i > 0) memmove(str, str + i, len - i + 1);
844 for (i = len - 1; i >= 0; i--)
845 if (str[i] != ' ') break;
854 * -1 - failed to parse line
856 int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
858 char *var, *val, *tmp;
862 tmp = fgets(line, lsize, f);
863 if (tmp == NULL) return 0;
865 if (line[0] == '[') return 0; // other section
867 // strip comments, linefeed, spaces..
869 for (i = 0; i < len; i++)
870 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
873 if (len <= 0) return -1;;
876 for (i = 0; i < len; i++)
877 if (line[i] == '=') break;
878 if (i >= len || strchr(&line[i+1], '=') != NULL) {
879 lprintf("config_readsect: can't parse: %s\n", line);
889 if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
890 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);