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)
54 if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
55 fprintf(f, "Gamma correction = %.3f", (double)currentConfig.gamma / 100.0);
57 case MA_OPT2_SQUIDGEHACK:
58 if (no_def && !((defaultConfig.EmuOpt^currentConfig.EmuOpt)&0x0010)) return;
59 fprintf(f, "Squidgehack = %i", (currentConfig.EmuOpt&0x0010)>>4);
61 case MA_CDOPT_READAHEAD:
62 if (no_def && defaultConfig.s_PicoCDBuffers == PicoCDBuffers) return;
63 sprintf(str24, "%i", PicoCDBuffers * 2);
64 fprintf(f, "ReadAhead buffer = %s", str24);
68 if (no_def && defaultConfig.scale == currentConfig.scale) return;
69 fprintf(f, "Scale factor = %.2f", currentConfig.scale);
71 case MA_OPT3_HSCALE32:
72 if (no_def && defaultConfig.hscale32 == currentConfig.hscale32) return;
73 fprintf(f, "Hor. scale (for low res. games) = %.2f", currentConfig.hscale32);
75 case MA_OPT3_HSCALE40:
76 if (no_def && defaultConfig.hscale40 == currentConfig.hscale40) return;
77 fprintf(f, "Hor. scale (for hi res. games) = %.2f", currentConfig.hscale40);
79 case MA_OPT3_FILTERING:
80 if (no_def && defaultConfig.scaling == currentConfig.scaling) return;
81 fprintf(f, "Bilinear filtering = %i", currentConfig.scaling);
84 if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
85 fprintf(f, "Gamma adjustment = %i", currentConfig.gamma);
87 case MA_OPT3_BLACKLVL:
88 if (no_def && defaultConfig.gamma2 == currentConfig.gamma2) return;
89 fprintf(f, "Black level = %i", currentConfig.gamma2);
92 if (no_def && (defaultConfig.EmuOpt&0x12000) == (currentConfig.gamma2&0x12000)) return;
93 strcpy(str24, "never");
94 if (currentConfig.EmuOpt & 0x2000)
95 strcpy(str24, (currentConfig.EmuOpt & 0x10000) ? "sometimes" : "always");
96 fprintf(f, "Wait for vsync = %s", str24);
100 lprintf("unhandled custom_write: %i\n", me->id);
107 static void keys_write(FILE *fn, const char *bind_str, int dev_id, const int *binds, int no_defaults)
111 const int *def_binds;
113 key_count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);
114 def_binds = in_get_dev_def_binds(dev_id);
116 for (t = 0; t < key_count; t++)
119 act[0] = act[31] = 0;
121 if (no_defaults && binds[t] == def_binds[t])
124 name = in_get_key_name(dev_id, t);
126 if (strcmp(name, "SELECT") == 0) continue;
129 if (binds[t] == 0 && def_binds[t] != 0) {
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 if (me_ctrl_actions[i].mask & binds[t]) {
136 strncpy(act, me_ctrl_actions[i].name, 31);
137 fprintf(fn, "%s %s = player%i %s" NL, bind_str, name,
138 ((binds[t]>>16)&1)+1, mystrip(act));
142 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
143 if (emuctrl_actions[i].mask & binds[t]) {
144 strncpy(act, emuctrl_actions[i].name, 31);
145 fprintf(fn, "%s %s = %s" NL, bind_str, name, mystrip(act));
152 static int default_var(const menu_entry *me)
156 case MA_OPT_ACC_TIMING:
157 case MA_OPT_ACC_SPRITES:
158 case MA_OPT_ARM940_SOUND:
159 case MA_OPT_6BUTTON_PAD:
160 case MA_OPT2_ENABLE_Z80:
161 case MA_OPT2_ENABLE_YM2612:
162 case MA_OPT2_ENABLE_SN76496:
163 case MA_OPT2_SVP_DYNAREC:
166 case MA_CDOPT_SAVERAM:
167 case MA_CDOPT_SCALEROT_CHIP:
168 case MA_CDOPT_BETTER_SYNC:
169 return defaultConfig.s_PicoOpt;
171 case MA_OPT_SHOW_FPS:
172 case MA_OPT_ENABLE_SOUND:
173 case MA_OPT_SRAM_STATES:
174 case MA_OPT2_A_SN_GAMMA:
176 case MA_OPT2_GZIP_STATES:
177 case MA_OPT2_NO_LAST_ROM:
178 case MA_OPT2_RAMTIMINGS:
180 return defaultConfig.EmuOpt;
182 case MA_CTRL_TURBO_RATE: return defaultConfig.turbo_rate;
183 case MA_OPT_SCALING: return defaultConfig.scaling;
184 case MA_OPT_ROTATION: return defaultConfig.rotation;
186 case MA_OPT_SAVE_SLOT:
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 me = me_list_get_first();
270 if (!me->need_to_save)
272 if (me->beh == MB_OPT_ONOFF) {
273 if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask))
274 fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
275 } else if (me->beh == MB_OPT_RANGE) {
276 if (!no_defaults || (*(int *)me->var ^ default_var(me)))
277 fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
278 } else if (me->name != NULL && me->generate_name != NULL) {
279 strncpy(line, me->generate_name(0, &dummy), sizeof(line));
280 line[sizeof(line) - 1] = 0;
282 fprintf(fn, "%s = %s" NL, me->name, line);
284 custom_write(fn, me, no_defaults);
286 me = me_list_get_next();
289 /* input: save device names */
290 for (t = 0; t < IN_MAX_DEVS; t++)
292 const int *binds = in_get_dev_binds(t);
293 const char *name = in_get_dev_name(t, 0, 0);
294 if (binds == NULL || name == NULL)
297 fprintf(fn, "input%d = %s" NL, t, name);
300 /* input: save binds */
301 for (t = 0; t < IN_MAX_DEVS; t++)
303 const int *binds = in_get_dev_binds(t);
304 const char *name = in_get_dev_name(t, 0, 0);
308 if (binds == NULL || name == NULL)
311 sprintf(strbind, "bind%d", t);
312 if (t == 0) strbind[4] = 0;
314 count = in_get_dev_info(t, IN_INFO_BIND_COUNT);
315 keys_write(fn, strbind, t, binds, no_defaults);
320 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
327 // copy whatever is left
330 tmp = fgets(line, sizeof(line), fo);
331 if (tmp == NULL) break;
344 int config_writelrom(const char *fname)
346 char line[128], *tmp, *optr = NULL;
347 char *old_data = NULL;
351 if (strlen(rom_fname_loaded) == 0) return -1;
353 f = fopen(fname, "r");
356 fseek(f, 0, SEEK_END);
358 fseek(f, 0, SEEK_SET);
359 old_data = malloc(size + size/8);
360 if (old_data != NULL)
365 tmp = fgets(line, sizeof(line), f);
366 if (tmp == NULL) break;
368 if (strncasecmp(line, "LastUsedROM", 11) == 0)
370 sprintf(optr, "%s", line);
371 optr += strlen(optr);
377 f = fopen(fname, "w");
378 if (f == NULL) return -1;
380 if (old_data != NULL) {
381 fwrite(old_data, 1, optr - old_data, f);
384 fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
389 /* --------------------------------------------------------------------------*/
391 int config_readlrom(const char *fname)
393 char line[128], *tmp;
394 int i, len, ret = -1;
397 f = fopen(fname, "r");
398 if (f == NULL) return -1;
400 // seek to the section needed
403 tmp = fgets(line, sizeof(line), f);
404 if (tmp == NULL) break;
406 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
408 for (i = 0; i < len; i++)
409 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
410 tmp = strchr(line, '=');
411 if (tmp == NULL) break;
415 len = sizeof(rom_fname_loaded);
416 strncpy(rom_fname_loaded, tmp, len);
417 rom_fname_loaded[len-1] = 0;
426 static int custom_read(menu_entry *me, const char *var, const char *val)
433 case MA_OPT_RENDERER:
434 if (strcasecmp(var, "Renderer") != 0) return 0;
435 if (strcasecmp(val, "8bit fast") == 0 || strcasecmp(val, "fast") == 0) {
436 PicoOpt |= POPT_ALT_RENDERER;
438 else if (strcasecmp(val, "16bit accurate") == 0 || strcasecmp(val, "accurate") == 0) {
439 PicoOpt &= ~POPT_ALT_RENDERER;
440 currentConfig.EmuOpt |= 0x80;
442 else if (strcasecmp(val, "8bit accurate") == 0) {
443 PicoOpt &= ~POPT_ALT_RENDERER;
444 currentConfig.EmuOpt &= ~0x80;
452 if (strcasecmp(var, "Scaling") != 0) return 0;
453 if (strcasecmp(val, "OFF") == 0) {
454 currentConfig.scaling = EOPT_SCALE_NONE;
455 } else if (strcasecmp(val, "hw horizontal") == 0) {
456 currentConfig.scaling = EOPT_SCALE_HW_H;
457 } else if (strcasecmp(val, "hw horiz. + vert.") == 0) {
458 currentConfig.scaling = EOPT_SCALE_HW_HV;
459 } else if (strcasecmp(val, "sw horizontal") == 0) {
460 currentConfig.scaling = EOPT_SCALE_SW_H;
468 case MA_OPT_FRAMESKIP:
469 if (strcasecmp(var, "Frameskip") != 0) return 0;
470 if (strcasecmp(val, "Auto") == 0)
471 currentConfig.Frameskip = -1;
472 else currentConfig.Frameskip = atoi(val);
475 case MA_OPT_SOUND_QUALITY:
476 if (strcasecmp(var, "Sound Quality") != 0) return 0;
477 PsndRate = strtoul(val, &tmp, 10);
478 if (PsndRate < 8000 || PsndRate > 44100)
480 while (*tmp == ' ') tmp++;
481 if (strcasecmp(tmp, "stereo") == 0) {
482 PicoOpt |= POPT_EN_STEREO;
483 } else if (strcasecmp(tmp, "mono") == 0) {
484 PicoOpt &= ~POPT_EN_STEREO;
490 if (strcasecmp(var, "Region") != 0) return 0;
491 if (strncasecmp(val, "Auto: ", 6) == 0)
493 const char *p = val + 5, *end = val + strlen(val);
495 PicoRegionOverride = PicoAutoRgnOrder = 0;
496 for (i = 0; p < end && i < 3; i++)
498 while (*p == ' ') p++;
499 if (p[0] == 'J' && p[1] == 'P') {
500 PicoAutoRgnOrder |= 1 << (i*4);
501 } else if (p[0] == 'U' && p[1] == 'S') {
502 PicoAutoRgnOrder |= 4 << (i*4);
503 } else if (p[0] == 'E' && p[1] == 'U') {
504 PicoAutoRgnOrder |= 8 << (i*4);
506 while (*p != ' ' && *p != 0) p++;
510 else if (strcasecmp(val, "Auto") == 0) {
511 PicoRegionOverride = 0;
512 } else if (strcasecmp(val, "Japan NTSC") == 0) {
513 PicoRegionOverride = 1;
514 } else if (strcasecmp(val, "Japan PAL") == 0) {
515 PicoRegionOverride = 2;
516 } else if (strcasecmp(val, "USA") == 0) {
517 PicoRegionOverride = 4;
518 } else if (strcasecmp(val, "Europe") == 0) {
519 PicoRegionOverride = 8;
524 case MA_OPT_CONFIRM_STATES:
525 if (strcasecmp(var, "Confirm savestate") != 0) return 0;
526 if (strcasecmp(val, "OFF") == 0) {
527 currentConfig.EmuOpt &= ~(5<<9);
528 } else if (strcasecmp(val, "writes") == 0) {
529 currentConfig.EmuOpt &= ~(5<<9);
530 currentConfig.EmuOpt |= 1<<9;
531 } else if (strcasecmp(val, "loads") == 0) {
532 currentConfig.EmuOpt &= ~(5<<9);
533 currentConfig.EmuOpt |= 4<<9;
534 } else if (strcasecmp(val, "both") == 0) {
535 currentConfig.EmuOpt &= ~(5<<9);
536 currentConfig.EmuOpt |= 5<<9;
542 case MA_OPT_CPU_CLOCKS:
544 if (strcasecmp(var, "GP2X CPU clocks") != 0) return 0;
546 if (strcasecmp(var, "PSP CPU clock") != 0) return 0;
548 currentConfig.CPUclock = atoi(val);
553 if (strcasecmp(var, "Gamma correction") != 0) return 0;
554 currentConfig.gamma = (int) (atof(val) * 100.0);
557 case MA_OPT2_SQUIDGEHACK:
558 if (strcasecmp(var, "Squidgehack") != 0) return 0;
560 if (tmpi) *(int *)me->var |= me->mask;
561 else *(int *)me->var &= ~me->mask;
564 case MA_CDOPT_READAHEAD:
565 if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
566 PicoCDBuffers = atoi(val) / 2;
571 if (strcasecmp(var, "Scale factor") != 0) return 0;
572 currentConfig.scale = atof(val);
574 case MA_OPT3_HSCALE32:
575 if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
576 currentConfig.hscale32 = atof(val);
578 case MA_OPT3_HSCALE40:
579 if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
580 currentConfig.hscale40 = atof(val);
582 case MA_OPT3_FILTERING:
583 if (strcasecmp(var, "Bilinear filtering") != 0) return 0;
584 currentConfig.scaling = atoi(val);
587 if (strcasecmp(var, "Gamma adjustment") != 0) return 0;
588 currentConfig.gamma = atoi(val);
590 case MA_OPT3_BLACKLVL:
591 if (strcasecmp(var, "Black level") != 0) return 0;
592 currentConfig.gamma2 = atoi(val);
595 if (strcasecmp(var, "Wait for vsync") != 0) return 0;
596 if (strcasecmp(val, "never") == 0) {
597 currentConfig.EmuOpt &= ~0x12000;
598 } else if (strcasecmp(val, "sometimes") == 0) {
599 currentConfig.EmuOpt |= 0x12000;
600 } else if (strcasecmp(val, "always") == 0) {
601 currentConfig.EmuOpt &= ~0x12000;
602 currentConfig.EmuOpt |= 0x02000;
608 lprintf("unhandled custom_read: %i\n", me->id);
614 static unsigned int keys_encountered = 0;
616 static int parse_bind_val(const char *val)
623 if (strncasecmp(val, "player", 6) == 0)
626 player = atoi(val + 6) - 1;
630 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
631 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
632 return me_ctrl_actions[i].mask | (player<<16);
635 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
636 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0)
637 return emuctrl_actions[i].mask;
643 static void keys_parse(const char *key, const char *val, int dev_id)
647 binds = parse_bind_val(val);
649 lprintf("config: unhandled action \"%s\"\n", val);
653 in_config_bind_key(dev_id, key, binds);
656 static int get_numvar_num(const char *var)
664 num = strtoul(var, &p, 10);
665 if (*p == 0 || *p == ' ')
671 /* map dev number in confing to input dev number */
672 static unsigned char input_dev_map[IN_MAX_DEVS];
674 static void parse(const char *var, const char *val)
679 if (strcasecmp(var, "LastUsedROM") == 0)
680 return; /* handled elsewhere */
682 if (strcasecmp(var, "Sound Volume") == 0) {
683 currentConfig.volume = atoi(val);
687 /* input: device name */
688 if (strncasecmp(var, "input", 5) == 0) {
689 int num = get_numvar_num(var + 5);
690 if (num >= 0 && num < IN_MAX_DEVS)
691 input_dev_map[num] = in_config_parse_dev(val);
693 lprintf("config: failed to parse: %s\n", var);
698 if (strncasecmp(var, "bind", 4) == 0) {
699 const char *p = var + 4;
700 int num = get_numvar_num(p);
701 if (num < 0 || num >= IN_MAX_DEVS) {
702 lprintf("config: failed to parse: %s\n", var);
706 num = input_dev_map[num];
707 if (num < 0 || num >= IN_MAX_DEVS) {
708 lprintf("config: invalid device id: %s\n", var);
712 while (*p && *p != ' ') p++;
713 while (*p && *p == ' ') p++;
714 keys_parse(p, val, num);
718 me = me_list_get_first();
719 while (me != NULL && ret == 0)
721 if (!me->need_to_save)
723 if (me->name != NULL && me->name[0] != 0) {
724 if (strcasecmp(var, me->name) != 0)
725 goto next; /* surely not this one */
726 if (me->beh == MB_OPT_ONOFF) {
728 if (tmp) *(int *)me->var |= me->mask;
729 else *(int *)me->var &= ~me->mask;
731 } else if (me->beh == MB_OPT_RANGE) {
733 if (tmp < me->min) tmp = me->min;
734 if (tmp > me->max) tmp = me->max;
735 *(int *)me->var = tmp;
739 ret = custom_read(me, var, val);
741 me = me_list_get_next();
743 if (!ret) lprintf("config_readsect: unhandled var: \"%s\"\n", var);
747 int config_havesect(const char *fname, const char *section)
752 f = fopen(fname, "r");
753 if (f == NULL) return 0;
755 ret = seek_sect(f, section);
760 int config_readsect(const char *fname, const char *section)
762 char line[128], *var, *val;
766 f = fopen(fname, "r");
767 if (f == NULL) return -1;
771 ret = seek_sect(f, section);
773 lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
779 keys_encountered = 0;
780 memset(input_dev_map, 0xff, sizeof(input_dev_map));
785 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
787 if (ret == -1) continue;
799 static char *mystrip(char *str)
804 for (i = 0; i < len; i++)
805 if (str[i] != ' ') break;
806 if (i > 0) memmove(str, str + i, len - i + 1);
809 for (i = len - 1; i >= 0; i--)
810 if (str[i] != ' ') break;
819 * -1 - failed to parse line
821 int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
823 char *var, *val, *tmp;
827 tmp = fgets(line, lsize, f);
828 if (tmp == NULL) return 0;
830 if (line[0] == '[') return 0; // other section
832 // strip comments, linefeed, spaces..
834 for (i = 0; i < len; i++)
835 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
838 if (len <= 0) return -1;;
841 for (i = 0; i < len; i++)
842 if (line[i] == '=') break;
843 if (i >= len || strchr(&line[i+1], '=') != NULL) {
844 lprintf("config_readsect: can't parse: %s\n", line);
854 if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
855 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);