2 * Human-readable config file management for PicoDrive
16 static char *mystrip(char *str);
22 #include <pico/pico.h>
24 // always output DOS endlines
31 static int seek_sect(FILE *f, const char *section)
36 len = strlen(section);
37 // seek to the section needed
40 tmp = fgets(line, sizeof(line), f);
41 if (tmp == NULL) break;
43 if (line[0] != '[') continue; // not section start
44 if (strncmp(line + 1, section, len) == 0 && line[len+1] == ']')
52 static void custom_write(FILE *f, const menu_entry *me, int no_def)
58 /* TODO: this should be rm'd when PSP menu is converted */
60 if (no_def && defaultConfig.scale == currentConfig.scale) return;
61 fprintf(f, "Scale factor = %.2f", currentConfig.scale);
63 case MA_OPT3_HSCALE32:
64 if (no_def && defaultConfig.hscale32 == currentConfig.hscale32) return;
65 fprintf(f, "Hor. scale (for low res. games) = %.2f", currentConfig.hscale32);
67 case MA_OPT3_HSCALE40:
68 if (no_def && defaultConfig.hscale40 == currentConfig.hscale40) return;
69 fprintf(f, "Hor. scale (for hi res. games) = %.2f", currentConfig.hscale40);
71 case MA_OPT3_FILTERING:
72 if (no_def && defaultConfig.scaling == currentConfig.scaling) return;
73 fprintf(f, "Bilinear filtering = %i", currentConfig.scaling);
76 if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
77 fprintf(f, "Gamma adjustment = %i", currentConfig.gamma);
79 case MA_OPT3_BLACKLVL:
80 if (no_def && defaultConfig.gamma2 == currentConfig.gamma2) return;
81 fprintf(f, "Black level = %i", currentConfig.gamma2);
84 if (no_def && (defaultConfig.EmuOpt&0x12000) == (currentConfig.gamma2&0x12000)) return;
85 strcpy(str24, "never");
86 if (currentConfig.EmuOpt & 0x2000)
87 strcpy(str24, (currentConfig.EmuOpt & 0x10000) ? "sometimes" : "always");
88 fprintf(f, "Wait for vsync = %s", str24);
92 lprintf("unhandled custom_write: %i\n", me->id);
99 static void keys_write(FILE *fn, const char *bind_str, int dev_id, const int *binds, int no_defaults)
103 const int *def_binds;
105 key_count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);
106 def_binds = in_get_dev_def_binds(dev_id);
108 for (k = 0; k < key_count; k++)
112 act[0] = act[31] = 0;
114 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
115 if (binds[IN_BIND_OFFS(k, t)] != def_binds[IN_BIND_OFFS(k, t)])
118 if (no_defaults && t == IN_BINDTYPE_COUNT)
119 continue; /* no change from defaults */
121 name = in_get_key_name(dev_id, k);
123 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
124 if (binds[IN_BIND_OFFS(k, t)] != 0 || def_binds[IN_BIND_OFFS(k, t)] == 0)
127 if (t == IN_BINDTYPE_COUNT) {
128 /* key has default bind removed */
129 fprintf(fn, "%s %s =" NL, bind_str, name);
133 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
134 mask = me_ctrl_actions[i].mask;
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 = player1 %s" NL, bind_str, name, mystrip(act));
139 mask = me_ctrl_actions[i].mask << 16;
140 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
141 strncpy(act, me_ctrl_actions[i].name, 31);
142 fprintf(fn, "%s %s = player2 %s" NL, bind_str, name, mystrip(act));
146 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
147 mask = emuctrl_actions[i].mask;
148 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
149 strncpy(act, emuctrl_actions[i].name, 31);
150 fprintf(fn, "%s %s = %s" NL, bind_str, name, mystrip(act));
156 /* XXX: this should go to menu structures instead */
157 static int default_var(const menu_entry *me)
161 case MA_OPT2_ENABLE_YM2612:
162 case MA_OPT2_ENABLE_SN76496:
163 case MA_OPT2_ENABLE_Z80:
164 case MA_OPT_6BUTTON_PAD:
165 case MA_OPT_ACC_SPRITES:
166 case MA_OPT_ARM940_SOUND:
169 case MA_CDOPT_SCALEROT_CHIP:
170 case MA_CDOPT_BETTER_SYNC:
171 case MA_CDOPT_SAVERAM:
172 case MA_OPT2_SVP_DYNAREC:
173 case MA_OPT2_NO_SPRITE_LIM:
174 case MA_OPT2_NO_IDLE_LOOPS:
175 return defaultConfig.s_PicoOpt;
177 case MA_OPT_SRAM_STATES:
178 case MA_OPT_SHOW_FPS:
179 case MA_OPT_ENABLE_SOUND:
180 case MA_OPT2_GZIP_STATES:
181 case MA_OPT2_SQUIDGEHACK:
182 case MA_OPT2_NO_LAST_ROM:
183 case MA_OPT2_RAMTIMINGS:
185 case MA_OPT2_A_SN_GAMMA:
187 case MA_OPT_INTERLACED:
188 case MA_OPT2_DBLBUFF:
189 case MA_OPT2_STATUS_LINE:
190 case MA_OPT2_NO_FRAME_LIMIT:
191 case MA_OPT_TEARING_FIX:
192 return defaultConfig.EmuOpt;
194 case MA_CTRL_TURBO_RATE: return defaultConfig.turbo_rate;
195 case MA_OPT_SCALING: return defaultConfig.scaling;
196 case MA_OPT_ROTATION: return defaultConfig.rotation;
197 case MA_OPT2_GAMMA: return defaultConfig.gamma;
198 case MA_OPT_FRAMESKIP: return defaultConfig.Frameskip;
199 case MA_OPT_CPU_CLOCKS: return defaultConfig.CPUclock;
201 case MA_OPT_SAVE_SLOT:
207 static int is_cust_val_default(const menu_entry *me)
212 return defaultConfig.s_PicoRegion == PicoRegionOverride &&
213 defaultConfig.s_PicoAutoRgnOrder == PicoAutoRgnOrder;
214 case MA_OPT_SOUND_QUALITY:
215 return defaultConfig.s_PsndRate == PsndRate &&
216 ((defaultConfig.s_PicoOpt ^ PicoOpt) & POPT_EN_STEREO) == 0;
217 case MA_OPT_CONFIRM_STATES:
218 return !((defaultConfig.EmuOpt ^ currentConfig.EmuOpt) &
219 (EOPT_CONFIRM_LOAD|EOPT_CONFIRM_SAVE)) == 0;
220 case MA_OPT_RENDERER:
221 return ((defaultConfig.s_PicoOpt ^ PicoOpt) & POPT_ALT_RENDERER) == 0 &&
222 ((defaultConfig.EmuOpt ^ currentConfig.EmuOpt) & EOPT_16BPP) == 0;
223 case MA_CDOPT_READAHEAD:
224 return defaultConfig.s_PicoCDBuffers == PicoCDBuffers;
228 lprintf("is_cust_val_default: unhandled id %i\n", me->id);
232 int config_writesect(const char *fname, const char *section)
234 FILE *fo = NULL, *fn = NULL; // old and new
235 int no_defaults = 0; // avoid saving defaults
238 char line[128], *tmp;
244 fo = fopen(fname, "r");
246 fn = fopen(fname, "w");
250 ret = seek_sect(fo, section);
252 // sect not found, we can simply append
253 fclose(fo); fo = NULL;
254 fn = fopen(fname, "a");
260 rename(fname, "tmp.cfg");
261 fo = fopen("tmp.cfg", "r");
262 fn = fopen(fname, "w");
263 if (fo == NULL || fn == NULL) goto write;
265 // copy everything until sect
266 tlen = strlen(section);
269 tmp = fgets(line, sizeof(line), fo);
270 if (tmp == NULL) break;
272 if (line[0] == '[' && strncmp(line + 1, section, tlen) == 0 && line[tlen+1] == ']')
277 // now skip to next sect
280 tmp = fgets(line, sizeof(line), fo);
281 if (tmp == NULL) break;
282 if (line[0] == '[') {
283 fseek(fo, -strlen(line), SEEK_CUR);
289 fclose(fo); fo = NULL;
295 fn = fopen(fname, "w");
304 fprintf(fn, "[%s]" NL, section);
306 me = me_list_get_first();
310 if (!me->need_to_save)
312 if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
313 if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask))
314 fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
315 } else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
316 if (!no_defaults || (*(int *)me->var ^ default_var(me)))
317 fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
318 } else if (me->name != NULL && me->generate_name != NULL) {
319 if (!no_defaults || !is_cust_val_default(me)) {
320 strncpy(line, me->generate_name(0, &dummy), sizeof(line));
321 line[sizeof(line) - 1] = 0;
323 fprintf(fn, "%s = %s" NL, me->name, line);
326 custom_write(fn, me, no_defaults);
328 me = me_list_get_next();
331 /* input: save device names */
332 for (t = 0; t < IN_MAX_DEVS; t++)
334 const int *binds = in_get_dev_binds(t);
335 const char *name = in_get_dev_name(t, 0, 0);
336 if (binds == NULL || name == NULL)
339 fprintf(fn, "input%d = %s" NL, t, name);
342 /* input: save binds */
343 for (t = 0; t < IN_MAX_DEVS; t++)
345 const int *binds = in_get_dev_binds(t);
346 const char *name = in_get_dev_name(t, 0, 0);
350 if (binds == NULL || name == NULL)
353 sprintf(strbind, "bind%d", t);
354 if (t == 0) strbind[4] = 0;
356 count = in_get_dev_info(t, IN_INFO_BIND_COUNT);
357 keys_write(fn, strbind, t, binds, no_defaults);
362 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
369 // copy whatever is left
372 tmp = fgets(line, sizeof(line), fo);
373 if (tmp == NULL) break;
386 int config_writelrom(const char *fname)
388 char line[128], *tmp, *optr = NULL;
389 char *old_data = NULL;
393 if (strlen(rom_fname_loaded) == 0) return -1;
395 f = fopen(fname, "r");
398 fseek(f, 0, SEEK_END);
400 fseek(f, 0, SEEK_SET);
401 old_data = malloc(size + size/8);
402 if (old_data != NULL)
407 tmp = fgets(line, sizeof(line), f);
408 if (tmp == NULL) break;
410 if (strncasecmp(line, "LastUsedROM", 11) == 0)
412 sprintf(optr, "%s", line);
413 optr += strlen(optr);
419 f = fopen(fname, "w");
420 if (f == NULL) return -1;
422 if (old_data != NULL) {
423 fwrite(old_data, 1, optr - old_data, f);
426 fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
431 /* --------------------------------------------------------------------------*/
433 int config_readlrom(const char *fname)
435 char line[128], *tmp;
436 int i, len, ret = -1;
439 f = fopen(fname, "r");
440 if (f == NULL) return -1;
442 // seek to the section needed
445 tmp = fgets(line, sizeof(line), f);
446 if (tmp == NULL) break;
448 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
450 for (i = 0; i < len; i++)
451 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
452 tmp = strchr(line, '=');
453 if (tmp == NULL) break;
457 len = sizeof(rom_fname_loaded);
458 strncpy(rom_fname_loaded, tmp, len);
459 rom_fname_loaded[len-1] = 0;
468 static int custom_read(menu_entry *me, const char *var, const char *val)
475 case MA_OPT_RENDERER:
476 if (strcasecmp(var, "Renderer") != 0) return 0;
477 if (strcasecmp(val, "8bit fast") == 0 || strcasecmp(val, "fast") == 0) {
478 PicoOpt |= POPT_ALT_RENDERER;
480 else if (strcasecmp(val, "16bit accurate") == 0 || strcasecmp(val, "accurate") == 0) {
481 PicoOpt &= ~POPT_ALT_RENDERER;
482 currentConfig.EmuOpt |= 0x80;
484 else if (strcasecmp(val, "8bit accurate") == 0) {
485 PicoOpt &= ~POPT_ALT_RENDERER;
486 currentConfig.EmuOpt &= ~0x80;
494 if (strcasecmp(var, "Scaling") != 0) return 0;
495 if (strcasecmp(val, "OFF") == 0) {
496 currentConfig.scaling = EOPT_SCALE_NONE;
497 } else if (strcasecmp(val, "hw horizontal") == 0) {
498 currentConfig.scaling = EOPT_SCALE_HW_H;
499 } else if (strcasecmp(val, "hw horiz. + vert.") == 0) {
500 currentConfig.scaling = EOPT_SCALE_HW_HV;
501 } else if (strcasecmp(val, "sw horizontal") == 0) {
502 currentConfig.scaling = EOPT_SCALE_SW_H;
504 currentConfig.scaling = atoi(val);
510 case MA_OPT_FRAMESKIP:
511 if (strcasecmp(var, "Frameskip") != 0) return 0;
512 if (strcasecmp(val, "Auto") == 0)
513 currentConfig.Frameskip = -1;
514 else currentConfig.Frameskip = atoi(val);
517 case MA_OPT_SOUND_QUALITY:
518 if (strcasecmp(var, "Sound Quality") != 0) return 0;
519 PsndRate = strtoul(val, &tmp, 10);
520 if (PsndRate < 8000 || PsndRate > 44100)
522 if (*tmp == 'H' || *tmp == 'h') tmp++;
523 if (*tmp == 'Z' || *tmp == 'z') tmp++;
524 while (*tmp == ' ') tmp++;
525 if (strcasecmp(tmp, "stereo") == 0) {
526 PicoOpt |= POPT_EN_STEREO;
527 } else if (strcasecmp(tmp, "mono") == 0) {
528 PicoOpt &= ~POPT_EN_STEREO;
534 if (strcasecmp(var, "Region") != 0) return 0;
535 if (strncasecmp(val, "Auto: ", 6) == 0)
537 const char *p = val + 5, *end = val + strlen(val);
539 PicoRegionOverride = PicoAutoRgnOrder = 0;
540 for (i = 0; p < end && i < 3; i++)
542 while (*p == ' ') p++;
543 if (p[0] == 'J' && p[1] == 'P') {
544 PicoAutoRgnOrder |= 1 << (i*4);
545 } else if (p[0] == 'U' && p[1] == 'S') {
546 PicoAutoRgnOrder |= 4 << (i*4);
547 } else if (p[0] == 'E' && p[1] == 'U') {
548 PicoAutoRgnOrder |= 8 << (i*4);
550 while (*p != ' ' && *p != 0) p++;
554 else if (strcasecmp(val, "Auto") == 0) {
555 PicoRegionOverride = 0;
556 } else if (strcasecmp(val, "Japan NTSC") == 0) {
557 PicoRegionOverride = 1;
558 } else if (strcasecmp(val, "Japan PAL") == 0) {
559 PicoRegionOverride = 2;
560 } else if (strcasecmp(val, "USA") == 0) {
561 PicoRegionOverride = 4;
562 } else if (strcasecmp(val, "Europe") == 0) {
563 PicoRegionOverride = 8;
568 case MA_OPT_CONFIRM_STATES:
569 if (strcasecmp(var, "Confirm savestate") != 0) return 0;
570 if (strcasecmp(val, "OFF") == 0) {
571 currentConfig.EmuOpt &= ~(5<<9);
572 } else if (strcasecmp(val, "writes") == 0) {
573 currentConfig.EmuOpt &= ~(5<<9);
574 currentConfig.EmuOpt |= 1<<9;
575 } else if (strcasecmp(val, "loads") == 0) {
576 currentConfig.EmuOpt &= ~(5<<9);
577 currentConfig.EmuOpt |= 4<<9;
578 } else if (strcasecmp(val, "both") == 0) {
579 currentConfig.EmuOpt &= ~(5<<9);
580 currentConfig.EmuOpt |= 5<<9;
586 if (strcasecmp(var, "Gamma correction") != 0) return 0;
587 currentConfig.gamma = (int) (atof(val) * 100.0);
590 case MA_OPT2_SQUIDGEHACK:
591 if (strcasecmp(var, "Squidgehack") != 0) return 0;
593 if (tmpi) *(int *)me->var |= me->mask;
594 else *(int *)me->var &= ~me->mask;
597 case MA_CDOPT_READAHEAD:
598 if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
599 PicoCDBuffers = atoi(val) / 2;
604 if (strcasecmp(var, "Scale factor") != 0) return 0;
605 currentConfig.scale = atof(val);
607 case MA_OPT3_HSCALE32:
608 if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
609 currentConfig.hscale32 = atof(val);
611 case MA_OPT3_HSCALE40:
612 if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
613 currentConfig.hscale40 = atof(val);
615 case MA_OPT3_FILTERING:
616 if (strcasecmp(var, "Bilinear filtering") != 0) return 0;
617 currentConfig.scaling = atoi(val);
620 if (strcasecmp(var, "Gamma adjustment") != 0) return 0;
621 currentConfig.gamma = atoi(val);
623 case MA_OPT3_BLACKLVL:
624 if (strcasecmp(var, "Black level") != 0) return 0;
625 currentConfig.gamma2 = atoi(val);
628 if (strcasecmp(var, "Wait for vsync") != 0) return 0;
629 if (strcasecmp(val, "never") == 0) {
630 currentConfig.EmuOpt &= ~0x12000;
631 } else if (strcasecmp(val, "sometimes") == 0) {
632 currentConfig.EmuOpt |= 0x12000;
633 } else if (strcasecmp(val, "always") == 0) {
634 currentConfig.EmuOpt &= ~0x12000;
635 currentConfig.EmuOpt |= 0x02000;
641 lprintf("unhandled custom_read: %i\n", me->id);
647 static unsigned int keys_encountered = 0;
649 static int parse_bind_val(const char *val, int *type)
653 *type = IN_BINDTYPE_NONE;
657 if (strncasecmp(val, "player", 6) == 0)
659 int player, shift = 0;
660 player = atoi(val + 6) - 1;
667 *type = IN_BINDTYPE_PLAYER12;
668 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
669 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
670 return me_ctrl_actions[i].mask << shift;
673 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
674 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
675 *type = IN_BINDTYPE_EMU;
676 return emuctrl_actions[i].mask;
683 static void keys_parse(const char *key, const char *val, int dev_id)
687 acts = parse_bind_val(val, &type);
689 lprintf("config: unhandled action \"%s\"\n", val);
693 in_config_bind_key(dev_id, key, acts, type);
696 static int get_numvar_num(const char *var)
704 num = strtoul(var, &p, 10);
705 if (*p == 0 || *p == ' ')
711 /* map dev number in confing to input dev number */
712 static unsigned char input_dev_map[IN_MAX_DEVS];
714 static void parse(const char *var, const char *val)
719 if (strcasecmp(var, "LastUsedROM") == 0)
720 return; /* handled elsewhere */
722 if (strcasecmp(var, "Sound Volume") == 0) {
723 currentConfig.volume = atoi(val);
727 /* input: device name */
728 if (strncasecmp(var, "input", 5) == 0) {
729 int num = get_numvar_num(var + 5);
730 if (num >= 0 && num < IN_MAX_DEVS)
731 input_dev_map[num] = in_config_parse_dev(val);
733 lprintf("config: failed to parse: %s\n", var);
738 if (strncasecmp(var, "bind", 4) == 0) {
739 const char *p = var + 4;
740 int num = get_numvar_num(p);
741 if (num < 0 || num >= IN_MAX_DEVS) {
742 lprintf("config: failed to parse: %s\n", var);
746 num = input_dev_map[num];
747 if (num < 0 || num >= IN_MAX_DEVS) {
748 lprintf("config: invalid device id: %s\n", var);
752 while (*p && *p != ' ') p++;
753 while (*p && *p == ' ') p++;
754 keys_parse(p, val, num);
758 me = me_list_get_first();
759 while (me != NULL && ret == 0)
761 if (!me->need_to_save)
763 if (me->name != NULL && me->name[0] != 0) {
764 if (strcasecmp(var, me->name) != 0)
765 goto next; /* surely not this one */
766 if (me->beh == MB_OPT_ONOFF) {
768 if (tmp) *(int *)me->var |= me->mask;
769 else *(int *)me->var &= ~me->mask;
771 } else if (me->beh == MB_OPT_RANGE) {
773 if (tmp < me->min) tmp = me->min;
774 if (tmp > me->max) tmp = me->max;
775 *(int *)me->var = tmp;
779 ret = custom_read(me, var, val);
781 me = me_list_get_next();
783 if (!ret) lprintf("config_readsect: unhandled var: \"%s\"\n", var);
787 int config_havesect(const char *fname, const char *section)
792 f = fopen(fname, "r");
793 if (f == NULL) return 0;
795 ret = seek_sect(f, section);
800 int config_readsect(const char *fname, const char *section)
802 char line[128], *var, *val;
806 f = fopen(fname, "r");
807 if (f == NULL) return -1;
811 ret = seek_sect(f, section);
813 lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
819 keys_encountered = 0;
820 memset(input_dev_map, 0xff, sizeof(input_dev_map));
825 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
827 if (ret == -1) continue;
839 static char *mystrip(char *str)
844 for (i = 0; i < len; i++)
845 if (str[i] != ' ') break;
846 if (i > 0) memmove(str, str + i, len - i + 1);
849 for (i = len - 1; i >= 0; i--)
850 if (str[i] != ' ') break;
859 * -1 - failed to parse line
861 int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
863 char *var, *val, *tmp;
867 tmp = fgets(line, lsize, f);
868 if (tmp == NULL) return 0;
870 if (line[0] == '[') return 0; // other section
872 // strip comments, linefeed, spaces..
874 for (i = 0; i < len; i++)
875 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
878 if (len <= 0) return -1;;
881 for (i = 0; i < len; i++)
882 if (line[i] == '=') break;
883 if (i >= len || strchr(&line[i+1], '=') != NULL) {
884 lprintf("config_readsect: can't parse: %s\n", line);
894 if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
895 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);