bugfix
[picodrive.git] / platform / common / config.c
CommitLineData
835e7900 1/*
2 * Human-readable config file management for PicoDrive
ca482e5d 3 * (c) notaz, 2008
835e7900 4 */
5
4609d0cd 6#include <stdio.h>
835e7900 7#include <string.h>
8#include <stdlib.h>
f8af9634 9#ifdef __EPOC32__
ca482e5d 10#include <unistd.h>
11#endif
835e7900 12#include "config.h"
7e4c661a 13#include "input.h"
4609d0cd 14#include "lprintf.h"
15
16static char *mystrip(char *str);
17
18#ifndef _MSC_VER
19
835e7900 20#include "menu.h"
21#include "emu.h"
efcba75f 22#include <pico/pico.h>
835e7900 23
823b9004 24// always output DOS endlines
25#ifdef _WIN32
26#define NL "\n"
27#else
7b802576 28#define NL "\r\n"
823b9004 29#endif
835e7900 30
835e7900 31static int seek_sect(FILE *f, const char *section)
32{
33 char line[128], *tmp;
34 int len;
35
36 len = strlen(section);
37 // seek to the section needed
38 while (!feof(f))
39 {
40 tmp = fgets(line, sizeof(line), f);
41 if (tmp == NULL) break;
42
43 if (line[0] != '[') continue; // not section start
44 if (strncmp(line + 1, section, len) == 0 && line[len+1] == ']')
45 return 1; // found it
46 }
47
48 return 0;
49}
50
51
c46ffd31 52static void custom_write(FILE *f, const menu_entry *me, int no_def)
835e7900 53{
713c9224 54 char str24[24];
835e7900 55
56 switch (me->id)
57 {
dabe23b6 58 /* TODO: this should be rm'd when PSP menu is converted */
6fc57144 59 case MA_OPT3_SCALE:
60 if (no_def && defaultConfig.scale == currentConfig.scale) return;
61 fprintf(f, "Scale factor = %.2f", currentConfig.scale);
62 break;
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);
66 break;
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);
70 break;
71 case MA_OPT3_FILTERING:
72 if (no_def && defaultConfig.scaling == currentConfig.scaling) return;
73 fprintf(f, "Bilinear filtering = %i", currentConfig.scaling);
74 break;
75 case MA_OPT3_GAMMAA:
76 if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
77 fprintf(f, "Gamma adjustment = %i", currentConfig.gamma);
78 break;
79 case MA_OPT3_BLACKLVL:
80 if (no_def && defaultConfig.gamma2 == currentConfig.gamma2) return;
81 fprintf(f, "Black level = %i", currentConfig.gamma2);
82 break;
83 case MA_OPT3_VSYNC:
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);
89 break;
835e7900 90
91 default:
c46ffd31 92 lprintf("unhandled custom_write: %i\n", me->id);
835e7900 93 return;
94 }
95 fprintf(f, NL);
96}
97
1ca2ea4f 98
7e4c661a 99static void keys_write(FILE *fn, const char *bind_str, int dev_id, const int *binds, int no_defaults)
1ca2ea4f 100{
7e4c661a 101 char act[48];
093b8a42 102 int key_count, k, i;
7e4c661a 103 const int *def_binds;
104
9025b931 105 key_count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);
7e4c661a 106 def_binds = in_get_dev_def_binds(dev_id);
1ca2ea4f 107
093b8a42 108 for (k = 0; k < key_count; k++)
1ca2ea4f 109 {
7e4c661a 110 const char *name;
093b8a42 111 int t, mask;
1ca2ea4f 112 act[0] = act[31] = 0;
7e4c661a 113
093b8a42 114 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
115 if (binds[IN_BIND_OFFS(k, t)] != def_binds[IN_BIND_OFFS(k, t)])
116 break;
7e4c661a 117
093b8a42 118 if (no_defaults && t == IN_BINDTYPE_COUNT)
119 continue; /* no change from defaults */
120
121 name = in_get_key_name(dev_id, k);
ca482e5d 122
093b8a42 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)
125 break;
126
127 if (t == IN_BINDTYPE_COUNT) {
128 /* key has default bind removed */
7e4c661a 129 fprintf(fn, "%s %s =" NL, bind_str, name);
6fc57144 130 continue;
131 }
132
1ca2ea4f 133 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
093b8a42 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));
138 }
139 mask = me_ctrl_actions[i].mask << 16;
140 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
bdec53c9 141 strncpy(act, me_ctrl_actions[i].name, 31);
093b8a42 142 fprintf(fn, "%s %s = player2 %s" NL, bind_str, name, mystrip(act));
1ca2ea4f 143 }
144 }
1ca2ea4f 145
bdec53c9 146 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
093b8a42 147 mask = emuctrl_actions[i].mask;
148 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
bdec53c9 149 strncpy(act, emuctrl_actions[i].name, 31);
ca482e5d 150 fprintf(fn, "%s %s = %s" NL, bind_str, name, mystrip(act));
bdec53c9 151 }
152 }
1ca2ea4f 153 }
154}
155
dabe23b6 156/* XXX: this should go to menu structures instead */
c46ffd31 157static int default_var(const menu_entry *me)
158{
159 switch (me->id)
160 {
c46ffd31 161 case MA_OPT2_ENABLE_YM2612:
162 case MA_OPT2_ENABLE_SN76496:
dabe23b6 163 case MA_OPT2_ENABLE_Z80:
164 case MA_OPT_6BUTTON_PAD:
165 case MA_OPT_ACC_SPRITES:
166 case MA_OPT_ARM940_SOUND:
c46ffd31 167 case MA_CDOPT_PCM:
dabe23b6 168 case MA_CDOPT_CDDA:
c46ffd31 169 case MA_CDOPT_SCALEROT_CHIP:
170 case MA_CDOPT_BETTER_SYNC:
dabe23b6 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:
c46ffd31 175 return defaultConfig.s_PicoOpt;
176
dabe23b6 177 case MA_OPT_SRAM_STATES:
c46ffd31 178 case MA_OPT_SHOW_FPS:
179 case MA_OPT_ENABLE_SOUND:
c46ffd31 180 case MA_OPT2_GZIP_STATES:
dabe23b6 181 case MA_OPT2_SQUIDGEHACK:
c46ffd31 182 case MA_OPT2_NO_LAST_ROM:
183 case MA_OPT2_RAMTIMINGS:
184 case MA_CDOPT_LEDS:
dabe23b6 185 case MA_OPT2_A_SN_GAMMA:
186 case MA_OPT2_VSYNC:
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:
c46ffd31 192 return defaultConfig.EmuOpt;
193
ca482e5d 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;
dabe23b6 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;
f0f0d2df 200
c46ffd31 201 case MA_OPT_SAVE_SLOT:
202 default:
203 return 0;
204 }
205}
835e7900 206
dabe23b6 207static int is_cust_val_default(const menu_entry *me)
208{
209 switch (me->id)
210 {
211 case MA_OPT_REGION:
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;
225 default:break;
226 }
227
228 lprintf("is_cust_val_default: unhandled id %i\n", me->id);
229 return 0;
230}
231
835e7900 232int config_writesect(const char *fname, const char *section)
233{
234 FILE *fo = NULL, *fn = NULL; // old and new
c46ffd31 235 int no_defaults = 0; // avoid saving defaults
835e7900 236 menu_entry *me;
713c9224 237 int t, tlen, ret;
835e7900 238 char line[128], *tmp;
239
240 if (section != NULL)
241 {
c46ffd31 242 no_defaults = 1;
243
835e7900 244 fo = fopen(fname, "r");
245 if (fo == NULL) {
246 fn = fopen(fname, "w");
247 goto write;
248 }
249
250 ret = seek_sect(fo, section);
251 if (!ret) {
252 // sect not found, we can simply append
253 fclose(fo); fo = NULL;
254 fn = fopen(fname, "a");
255 goto write;
256 }
257
258 // use 2 files..
259 fclose(fo);
260 rename(fname, "tmp.cfg");
261 fo = fopen("tmp.cfg", "r");
262 fn = fopen(fname, "w");
263 if (fo == NULL || fn == NULL) goto write;
264
265 // copy everything until sect
266 tlen = strlen(section);
267 while (!feof(fo))
268 {
269 tmp = fgets(line, sizeof(line), fo);
270 if (tmp == NULL) break;
271
272 if (line[0] == '[' && strncmp(line + 1, section, tlen) == 0 && line[tlen+1] == ']')
273 break;
274 fputs(line, fn);
275 }
276
277 // now skip to next sect
278 while (!feof(fo))
279 {
280 tmp = fgets(line, sizeof(line), fo);
281 if (tmp == NULL) break;
282 if (line[0] == '[') {
283 fseek(fo, -strlen(line), SEEK_CUR);
284 break;
285 }
286 }
287 if (feof(fo))
288 {
289 fclose(fo); fo = NULL;
290 remove("tmp.cfg");
291 }
292 }
293 else
294 {
295 fn = fopen(fname, "w");
296 }
297
298write:
299 if (fn == NULL) {
300 if (fo) fclose(fo);
301 return -1;
302 }
303 if (section != NULL)
304 fprintf(fn, "[%s]" NL, section);
305
713c9224 306 me = me_list_get_first();
307 while (me != NULL)
835e7900 308 {
713c9224 309 int dummy;
310 if (!me->need_to_save)
311 goto next;
dabe23b6 312 if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
713c9224 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);
dabe23b6 315 } else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
713c9224 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) {
dabe23b6 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;
322 mystrip(line);
323 fprintf(fn, "%s = %s" NL, me->name, line);
324 }
713c9224 325 } else
326 custom_write(fn, me, no_defaults);
327next:
328 me = me_list_get_next();
835e7900 329 }
1ca2ea4f 330
7e4c661a 331 /* input: save device names */
332 for (t = 0; t < IN_MAX_DEVS; t++)
333 {
334 const int *binds = in_get_dev_binds(t);
713c9224 335 const char *name = in_get_dev_name(t, 0, 0);
7e4c661a 336 if (binds == NULL || name == NULL)
337 continue;
338
339 fprintf(fn, "input%d = %s" NL, t, name);
340 }
341
342 /* input: save binds */
343 for (t = 0; t < IN_MAX_DEVS; t++)
344 {
345 const int *binds = in_get_dev_binds(t);
713c9224 346 const char *name = in_get_dev_name(t, 0, 0);
7e4c661a 347 char strbind[16];
348 int count;
349
350 if (binds == NULL || name == NULL)
351 continue;
352
353 sprintf(strbind, "bind%d", t);
354 if (t == 0) strbind[4] = 0;
355
9025b931 356 count = in_get_dev_info(t, IN_INFO_BIND_COUNT);
7e4c661a 357 keys_write(fn, strbind, t, binds, no_defaults);
358 }
359
6fc57144 360#ifndef PSP
7b802576 361 if (section == NULL)
362 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
6fc57144 363#endif
7b802576 364
835e7900 365 fprintf(fn, NL);
366
367 if (fo != NULL)
368 {
369 // copy whatever is left
370 while (!feof(fo))
371 {
372 tmp = fgets(line, sizeof(line), fo);
373 if (tmp == NULL) break;
374
375 fputs(line, fn);
376 }
377 fclose(fo);
378 remove("tmp.cfg");
379 }
380
381 fclose(fn);
382 return 0;
383}
384
385
835e7900 386int config_writelrom(const char *fname)
387{
388 char line[128], *tmp, *optr = NULL;
389 char *old_data = NULL;
390 int size;
391 FILE *f;
392
713c9224 393 if (strlen(rom_fname_loaded) == 0) return -1;
835e7900 394
395 f = fopen(fname, "r");
396 if (f != NULL)
397 {
398 fseek(f, 0, SEEK_END);
399 size = ftell(f);
400 fseek(f, 0, SEEK_SET);
401 old_data = malloc(size + size/8);
402 if (old_data != NULL)
403 {
404 optr = old_data;
405 while (!feof(f))
406 {
407 tmp = fgets(line, sizeof(line), f);
408 if (tmp == NULL) break;
409 mystrip(line);
410 if (strncasecmp(line, "LastUsedROM", 11) == 0)
411 continue;
412 sprintf(optr, "%s", line);
413 optr += strlen(optr);
414 }
415 }
416 fclose(f);
417 }
418
419 f = fopen(fname, "w");
420 if (f == NULL) return -1;
421
422 if (old_data != NULL) {
423 fwrite(old_data, 1, optr - old_data, f);
424 free(old_data);
425 }
713c9224 426 fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
835e7900 427 fclose(f);
428 return 0;
429}
430
1ca2ea4f 431/* --------------------------------------------------------------------------*/
432
433int config_readlrom(const char *fname)
434{
435 char line[128], *tmp;
436 int i, len, ret = -1;
437 FILE *f;
438
439 f = fopen(fname, "r");
440 if (f == NULL) return -1;
441
442 // seek to the section needed
443 while (!feof(f))
444 {
445 tmp = fgets(line, sizeof(line), f);
446 if (tmp == NULL) break;
447
448 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
449 len = strlen(line);
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;
454 tmp++;
455 mystrip(tmp);
456
713c9224 457 len = sizeof(rom_fname_loaded);
458 strncpy(rom_fname_loaded, tmp, len);
459 rom_fname_loaded[len-1] = 0;
1ca2ea4f 460 ret = 0;
461 break;
462 }
463 fclose(f);
464 return ret;
465}
466
835e7900 467
468static int custom_read(menu_entry *me, const char *var, const char *val)
469{
470 char *tmp;
471 int tmpi;
472
473 switch (me->id)
474 {
475 case MA_OPT_RENDERER:
476 if (strcasecmp(var, "Renderer") != 0) return 0;
6fc57144 477 if (strcasecmp(val, "8bit fast") == 0 || strcasecmp(val, "fast") == 0) {
602133e1 478 PicoOpt |= POPT_ALT_RENDERER;
835e7900 479 }
6fc57144 480 else if (strcasecmp(val, "16bit accurate") == 0 || strcasecmp(val, "accurate") == 0) {
602133e1 481 PicoOpt &= ~POPT_ALT_RENDERER;
835e7900 482 currentConfig.EmuOpt |= 0x80;
483 }
484 else if (strcasecmp(val, "8bit accurate") == 0) {
602133e1 485 PicoOpt &= ~POPT_ALT_RENDERER;
835e7900 486 currentConfig.EmuOpt &= ~0x80;
487 }
488 else
489 return 0;
490 return 1;
491
492 case MA_OPT_SCALING:
6fc57144 493#ifdef __GP2X__
835e7900 494 if (strcasecmp(var, "Scaling") != 0) return 0;
495 if (strcasecmp(val, "OFF") == 0) {
ee2a3bdf 496 currentConfig.scaling = EOPT_SCALE_NONE;
835e7900 497 } else if (strcasecmp(val, "hw horizontal") == 0) {
ee2a3bdf 498 currentConfig.scaling = EOPT_SCALE_HW_H;
835e7900 499 } else if (strcasecmp(val, "hw horiz. + vert.") == 0) {
ee2a3bdf 500 currentConfig.scaling = EOPT_SCALE_HW_HV;
835e7900 501 } else if (strcasecmp(val, "sw horizontal") == 0) {
ee2a3bdf 502 currentConfig.scaling = EOPT_SCALE_SW_H;
835e7900 503 } else
dabe23b6 504 currentConfig.scaling = atoi(val);
835e7900 505 return 1;
6fc57144 506#else
507 return 0;
508#endif
835e7900 509
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);
515 return 1;
516
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)
521 PsndRate = 22050;
21ecaf23 522 if (*tmp == 'H' || *tmp == 'h') tmp++;
523 if (*tmp == 'Z' || *tmp == 'z') tmp++;
835e7900 524 while (*tmp == ' ') tmp++;
525 if (strcasecmp(tmp, "stereo") == 0) {
602133e1 526 PicoOpt |= POPT_EN_STEREO;
835e7900 527 } else if (strcasecmp(tmp, "mono") == 0) {
602133e1 528 PicoOpt &= ~POPT_EN_STEREO;
835e7900 529 } else
530 return 0;
531 return 1;
532
533 case MA_OPT_REGION:
534 if (strcasecmp(var, "Region") != 0) return 0;
535 if (strncasecmp(val, "Auto: ", 6) == 0)
536 {
537 const char *p = val + 5, *end = val + strlen(val);
538 int i;
539 PicoRegionOverride = PicoAutoRgnOrder = 0;
c46ffd31 540 for (i = 0; p < end && i < 3; i++)
541 {
542 while (*p == ' ') p++;
835e7900 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);
549 }
c46ffd31 550 while (*p != ' ' && *p != 0) p++;
551 if (*p == 0) break;
835e7900 552 }
553 }
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;
564 } else
565 return 0;
566 return 1;
567
568 case MA_OPT_CONFIRM_STATES:
569 if (strcasecmp(var, "Confirm savestate") != 0) return 0;
570 if (strcasecmp(val, "OFF") == 0) {
1ca2ea4f 571 currentConfig.EmuOpt &= ~(5<<9);
835e7900 572 } else if (strcasecmp(val, "writes") == 0) {
1ca2ea4f 573 currentConfig.EmuOpt &= ~(5<<9);
574 currentConfig.EmuOpt |= 1<<9;
835e7900 575 } else if (strcasecmp(val, "loads") == 0) {
1ca2ea4f 576 currentConfig.EmuOpt &= ~(5<<9);
577 currentConfig.EmuOpt |= 4<<9;
835e7900 578 } else if (strcasecmp(val, "both") == 0) {
1ca2ea4f 579 currentConfig.EmuOpt &= ~(5<<9);
580 currentConfig.EmuOpt |= 5<<9;
835e7900 581 } else
582 return 0;
583 return 1;
584
835e7900 585 case MA_OPT2_GAMMA:
586 if (strcasecmp(var, "Gamma correction") != 0) return 0;
587 currentConfig.gamma = (int) (atof(val) * 100.0);
588 return 1;
589
590 case MA_OPT2_SQUIDGEHACK:
591 if (strcasecmp(var, "Squidgehack") != 0) return 0;
592 tmpi = atoi(val);
593 if (tmpi) *(int *)me->var |= me->mask;
594 else *(int *)me->var &= ~me->mask;
595 return 1;
596
597 case MA_CDOPT_READAHEAD:
598 if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
599 PicoCDBuffers = atoi(val) / 2;
600 return 1;
601
6fc57144 602 /* PSP */
603 case MA_OPT3_SCALE:
604 if (strcasecmp(var, "Scale factor") != 0) return 0;
605 currentConfig.scale = atof(val);
606 return 1;
607 case MA_OPT3_HSCALE32:
608 if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
609 currentConfig.hscale32 = atof(val);
610 return 1;
611 case MA_OPT3_HSCALE40:
612 if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
613 currentConfig.hscale40 = atof(val);
614 return 1;
615 case MA_OPT3_FILTERING:
616 if (strcasecmp(var, "Bilinear filtering") != 0) return 0;
617 currentConfig.scaling = atoi(val);
618 return 1;
619 case MA_OPT3_GAMMAA:
620 if (strcasecmp(var, "Gamma adjustment") != 0) return 0;
621 currentConfig.gamma = atoi(val);
622 return 1;
623 case MA_OPT3_BLACKLVL:
624 if (strcasecmp(var, "Black level") != 0) return 0;
625 currentConfig.gamma2 = atoi(val);
626 return 1;
627 case MA_OPT3_VSYNC:
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;
636 } else
637 return 0;
638 return 1;
639
835e7900 640 default:
c46ffd31 641 lprintf("unhandled custom_read: %i\n", me->id);
835e7900 642 return 0;
643 }
644}
645
646
bdec53c9 647static unsigned int keys_encountered = 0;
648
093b8a42 649static int parse_bind_val(const char *val, int *type)
7e4c661a 650{
651 int i;
652
093b8a42 653 *type = IN_BINDTYPE_NONE;
7e4c661a 654 if (val[0] == 0)
655 return 0;
656
657 if (strncasecmp(val, "player", 6) == 0)
658 {
093b8a42 659 int player, shift = 0;
7e4c661a 660 player = atoi(val + 6) - 1;
093b8a42 661
7e4c661a 662 if (player > 1)
663 return -1;
093b8a42 664 if (player == 1)
665 shift = 16;
7e4c661a 666
093b8a42 667 *type = IN_BINDTYPE_PLAYER12;
7e4c661a 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)
093b8a42 670 return me_ctrl_actions[i].mask << shift;
7e4c661a 671 }
672 }
673 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
093b8a42 674 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
675 *type = IN_BINDTYPE_EMU;
7e4c661a 676 return emuctrl_actions[i].mask;
093b8a42 677 }
7e4c661a 678 }
679
680 return -1;
681}
682
683static void keys_parse(const char *key, const char *val, int dev_id)
1ca2ea4f 684{
093b8a42 685 int acts, type;
1ca2ea4f 686
093b8a42 687 acts = parse_bind_val(val, &type);
688 if (acts == -1) {
7e4c661a 689 lprintf("config: unhandled action \"%s\"\n", val);
690 return;
691 }
692
093b8a42 693 in_config_bind_key(dev_id, key, acts, type);
1ca2ea4f 694}
695
7e4c661a 696static int get_numvar_num(const char *var)
697{
698 char *p = NULL;
699 int num;
700
701 if (var[0] == ' ')
702 return 0;
703
704 num = strtoul(var, &p, 10);
705 if (*p == 0 || *p == ' ')
706 return num;
707
708 return -1;
709}
710
711/* map dev number in confing to input dev number */
712static unsigned char input_dev_map[IN_MAX_DEVS];
713
835e7900 714static void parse(const char *var, const char *val)
715{
716 menu_entry *me;
713c9224 717 int tmp, ret = 0;
835e7900 718
1ca2ea4f 719 if (strcasecmp(var, "LastUsedROM") == 0)
720 return; /* handled elsewhere */
721
7b802576 722 if (strcasecmp(var, "Sound Volume") == 0) {
723 currentConfig.volume = atoi(val);
724 return;
725 }
726
7e4c661a 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);
732 else
b6820926 733 lprintf("config: failed to parse: %s\n", var);
7e4c661a 734 return;
735 }
736
1ca2ea4f 737 // key binds
7e4c661a 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) {
b6820926 742 lprintf("config: failed to parse: %s\n", var);
7e4c661a 743 return;
744 }
745
746 num = input_dev_map[num];
747 if (num < 0 || num >= IN_MAX_DEVS) {
b6820926 748 lprintf("config: invalid device id: %s\n", var);
7e4c661a 749 return;
750 }
751
752 while (*p && *p != ' ') p++;
753 while (*p && *p == ' ') p++;
754 keys_parse(p, val, num);
1ca2ea4f 755 return;
756 }
7e4c661a 757
713c9224 758 me = me_list_get_first();
759 while (me != NULL && ret == 0)
835e7900 760 {
713c9224 761 if (!me->need_to_save)
762 goto next;
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) {
767 tmp = atoi(val);
768 if (tmp) *(int *)me->var |= me->mask;
769 else *(int *)me->var &= ~me->mask;
770 return;
771 } else if (me->beh == MB_OPT_RANGE) {
772 tmp = atoi(val);
773 if (tmp < me->min) tmp = me->min;
774 if (tmp > me->max) tmp = me->max;
775 *(int *)me->var = tmp;
776 return;
835e7900 777 }
835e7900 778 }
713c9224 779 ret = custom_read(me, var, val);
780next:
781 me = me_list_get_next();
835e7900 782 }
84100c0f 783 if (!ret) lprintf("config_readsect: unhandled var: \"%s\"\n", var);
835e7900 784}
785
786
1ca2ea4f 787int config_havesect(const char *fname, const char *section)
788{
789 FILE *f;
790 int ret;
791
792 f = fopen(fname, "r");
793 if (f == NULL) return 0;
794
795 ret = seek_sect(f, section);
796 fclose(f);
797 return ret;
798}
799
835e7900 800int config_readsect(const char *fname, const char *section)
801{
4609d0cd 802 char line[128], *var, *val;
1ca2ea4f 803 FILE *f;
4609d0cd 804 int ret;
835e7900 805
1ca2ea4f 806 f = fopen(fname, "r");
d6114368 807 if (f == NULL) return -1;
835e7900 808
809 if (section != NULL)
810 {
811 ret = seek_sect(f, section);
812 if (!ret) {
c46ffd31 813 lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
835e7900 814 fclose(f);
815 return -1;
816 }
817 }
818
bdec53c9 819 keys_encountered = 0;
7e4c661a 820 memset(input_dev_map, 0xff, sizeof(input_dev_map));
bdec53c9 821
7e4c661a 822 in_config_start();
835e7900 823 while (!feof(f))
824 {
4609d0cd 825 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
826 if (ret == 0) break;
827 if (ret == -1) continue;
835e7900 828
829 parse(var, val);
830 }
7e4c661a 831 in_config_end();
835e7900 832
833 fclose(f);
834 return 0;
835}
836
4609d0cd 837#endif // _MSC_VER
838
839static char *mystrip(char *str)
840{
841 int i, len;
842
843 len = strlen(str);
844 for (i = 0; i < len; i++)
845 if (str[i] != ' ') break;
846 if (i > 0) memmove(str, str + i, len - i + 1);
847
848 len = strlen(str);
849 for (i = len - 1; i >= 0; i--)
850 if (str[i] != ' ') break;
851 str[i+1] = 0;
852
853 return str;
854}
855
856/* returns:
857 * 0 - EOF, end
858 * 1 - parsed ok
859 * -1 - failed to parse line
860 */
861int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
862{
863 char *var, *val, *tmp;
864 FILE *f = file;
865 int len, i;
866
867 tmp = fgets(line, lsize, f);
868 if (tmp == NULL) return 0;
869
870 if (line[0] == '[') return 0; // other section
871
872 // strip comments, linefeed, spaces..
873 len = strlen(line);
874 for (i = 0; i < len; i++)
875 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
876 mystrip(line);
877 len = strlen(line);
878 if (len <= 0) return -1;;
879
880 // get var and val
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);
885 return -1;
886 }
887 line[i] = 0;
888 var = line;
889 val = &line[i+1];
890 mystrip(var);
891 mystrip(val);
892
893#ifndef _MSC_VER
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);
896 return -1;;
897 }
898#endif
899
900 *rvar = var;
901 *rval = val;
902 return 1;
903}
904