b2f55ccf3f167ed5bc424c49fbaff2d0f1bcb4c3
[libpicofe.git] / common / config.c
1 /*
2  * Human-readable config file management for PicoDrive
3  * (c) notaz, 2008
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #ifdef __EPOC32__
10 #include <unistd.h>
11 #endif
12 #include "config.h"
13 #include "input.h"
14 #include "lprintf.h"
15
16 static char *mystrip(char *str);
17
18 #ifndef _MSC_VER
19
20 #include "menu.h"
21 #include "emu.h"
22 #include <pico/pico.h>
23
24 #define NL "\r\n"
25
26 static int seek_sect(FILE *f, const char *section)
27 {
28         char line[128], *tmp;
29         int len;
30
31         len = strlen(section);
32         // seek to the section needed
33         while (!feof(f))
34         {
35                 tmp = fgets(line, sizeof(line), f);
36                 if (tmp == NULL) break;
37
38                 if (line[0] != '[') continue; // not section start
39                 if (strncmp(line + 1, section, len) == 0 && line[len+1] == ']')
40                         return 1; // found it
41         }
42
43         return 0;
44 }
45
46
47 static void custom_write(FILE *f, const menu_entry *me, int no_def)
48 {
49         char str24[24];
50
51         switch (me->id)
52         {
53                 /* TODO: this should be rm'd when PSP menu is converted */
54                 case MA_OPT3_SCALE:
55                         if (no_def && defaultConfig.scale == currentConfig.scale) return;
56                         fprintf(f, "Scale factor = %.2f", currentConfig.scale);
57                         break;
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);
61                         break;
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);
65                         break;
66                 case MA_OPT3_FILTERING:
67                         if (no_def && defaultConfig.scaling == currentConfig.scaling) return;
68                         fprintf(f, "Bilinear filtering = %i", currentConfig.scaling);
69                         break;
70                 case MA_OPT3_GAMMAA:
71                         if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
72                         fprintf(f, "Gamma adjustment = %i", currentConfig.gamma);
73                         break;
74                 case MA_OPT3_BLACKLVL:
75                         if (no_def && defaultConfig.gamma2 == currentConfig.gamma2) return;
76                         fprintf(f, "Black level = %i", currentConfig.gamma2);
77                         break;
78                 case MA_OPT3_VSYNC:
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);
84                         break;
85
86                 default:
87                         lprintf("unhandled custom_write: %i\n", me->id);
88                         return;
89         }
90         fprintf(f, NL);
91 }
92
93
94 static void keys_write(FILE *fn, const char *bind_str, int dev_id, const int *binds, int no_defaults)
95 {
96         char act[48];
97         int key_count, k, i;
98         const int *def_binds;
99
100         key_count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);
101         def_binds = in_get_dev_def_binds(dev_id);
102
103         for (k = 0; k < key_count; k++)
104         {
105                 const char *name;
106                 int t, mask;
107                 act[0] = act[31] = 0;
108
109                 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
110                         if (binds[IN_BIND_OFFS(k, t)] != def_binds[IN_BIND_OFFS(k, t)])
111                                 break;
112
113                 if (no_defaults && t == IN_BINDTYPE_COUNT)
114                         continue;       /* no change from defaults */
115
116                 name = in_get_key_name(dev_id, k);
117
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)
120                                 break;
121
122                 if (t == IN_BINDTYPE_COUNT) {
123                         /* key has default bind removed */
124                         fprintf(fn, "%s %s =" NL, bind_str, name);
125                         continue;
126                 }
127
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));
133                         }
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));
138                         }
139                 }
140
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));
146                         }
147                 }
148         }
149 }
150
151 /* XXX: this should go to menu structures instead */
152 static int default_var(const menu_entry *me)
153 {
154         switch (me->id)
155         {
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:
162                 case MA_CDOPT_PCM:
163                 case MA_CDOPT_CDDA:
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;
171
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:
179                 case MA_CDOPT_LEDS:
180                 case MA_OPT2_A_SN_GAMMA:
181                 case MA_OPT2_VSYNC:
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;
188
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;
195
196                 case MA_OPT_SAVE_SLOT:
197                 default:
198                         return 0;
199         }
200 }
201
202 static int is_cust_val_default(const menu_entry *me)
203 {
204         switch (me->id)
205         {
206                 case MA_OPT_REGION:
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;
220                 default:break;
221         }
222
223         lprintf("is_cust_val_default: unhandled id %i\n", me->id);
224         return 0;
225 }
226
227 int config_writesect(const char *fname, const char *section)
228 {
229         FILE *fo = NULL, *fn = NULL; // old and new
230         int no_defaults = 0; // avoid saving defaults
231         menu_entry *me;
232         int t, tlen, ret;
233         char line[128], *tmp;
234
235         if (section != NULL)
236         {
237                 no_defaults = 1;
238
239                 fo = fopen(fname, "r");
240                 if (fo == NULL) {
241                         fn = fopen(fname, "w");
242                         goto write;
243                 }
244
245                 ret = seek_sect(fo, section);
246                 if (!ret) {
247                         // sect not found, we can simply append
248                         fclose(fo); fo = NULL;
249                         fn = fopen(fname, "a");
250                         goto write;
251                 }
252
253                 // use 2 files..
254                 fclose(fo);
255                 rename(fname, "tmp.cfg");
256                 fo = fopen("tmp.cfg", "r");
257                 fn = fopen(fname, "w");
258                 if (fo == NULL || fn == NULL) goto write;
259
260                 // copy everything until sect
261                 tlen = strlen(section);
262                 while (!feof(fo))
263                 {
264                         tmp = fgets(line, sizeof(line), fo);
265                         if (tmp == NULL) break;
266
267                         if (line[0] == '[' && strncmp(line + 1, section, tlen) == 0 && line[tlen+1] == ']')
268                                 break;
269                         fputs(line, fn);
270                 }
271
272                 // now skip to next sect
273                 while (!feof(fo))
274                 {
275                         tmp = fgets(line, sizeof(line), fo);
276                         if (tmp == NULL) break;
277                         if (line[0] == '[') {
278                                 fseek(fo, -strlen(line), SEEK_CUR);
279                                 break;
280                         }
281                 }
282                 if (feof(fo))
283                 {
284                         fclose(fo); fo = NULL;
285                         remove("tmp.cfg");
286                 }
287         }
288         else
289         {
290                 fn = fopen(fname, "w");
291         }
292
293 write:
294         if (fn == NULL) {
295                 if (fo) fclose(fo);
296                 return -1;
297         }
298         if (section != NULL)
299                 fprintf(fn, "[%s]" NL, section);
300
301         me = me_list_get_first();
302         while (me != NULL)
303         {
304                 int dummy;
305                 if (!me->need_to_save)
306                         goto next;
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;
317                                 mystrip(line);
318                                 fprintf(fn, "%s = %s" NL, me->name, line);
319                         }
320                 } else
321                         custom_write(fn, me, no_defaults);
322 next:
323                 me = me_list_get_next();
324         }
325
326         /* input: save device names */
327         for (t = 0; t < IN_MAX_DEVS; t++)
328         {
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)
332                         continue;
333
334                 fprintf(fn, "input%d = %s" NL, t, name);
335         }
336
337         /* input: save binds */
338         for (t = 0; t < IN_MAX_DEVS; t++)
339         {
340                 const int *binds = in_get_dev_binds(t);
341                 const char *name = in_get_dev_name(t, 0, 0);
342                 char strbind[16];
343                 int count;
344
345                 if (binds == NULL || name == NULL)
346                         continue;
347
348                 sprintf(strbind, "bind%d", t);
349                 if (t == 0) strbind[4] = 0;
350
351                 count = in_get_dev_info(t, IN_INFO_BIND_COUNT);
352                 keys_write(fn, strbind, t, binds, no_defaults);
353         }
354
355 #ifndef PSP
356         if (section == NULL)
357                 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
358 #endif
359
360         fprintf(fn, NL);
361
362         if (fo != NULL)
363         {
364                 // copy whatever is left
365                 while (!feof(fo))
366                 {
367                         tmp = fgets(line, sizeof(line), fo);
368                         if (tmp == NULL) break;
369
370                         fputs(line, fn);
371                 }
372                 fclose(fo);
373                 remove("tmp.cfg");
374         }
375
376         fclose(fn);
377         return 0;
378 }
379
380
381 int config_writelrom(const char *fname)
382 {
383         char line[128], *tmp, *optr = NULL;
384         char *old_data = NULL;
385         int size;
386         FILE *f;
387
388         if (strlen(rom_fname_loaded) == 0) return -1;
389
390         f = fopen(fname, "r");
391         if (f != NULL)
392         {
393                 fseek(f, 0, SEEK_END);
394                 size = ftell(f);
395                 fseek(f, 0, SEEK_SET);
396                 old_data = malloc(size + size/8);
397                 if (old_data != NULL)
398                 {
399                         optr = old_data;
400                         while (!feof(f))
401                         {
402                                 tmp = fgets(line, sizeof(line), f);
403                                 if (tmp == NULL) break;
404                                 mystrip(line);
405                                 if (strncasecmp(line, "LastUsedROM", 11) == 0)
406                                         continue;
407                                 sprintf(optr, "%s", line);
408                                 optr += strlen(optr);
409                         }
410                 }
411                 fclose(f);
412         }
413
414         f = fopen(fname, "w");
415         if (f == NULL) return -1;
416
417         if (old_data != NULL) {
418                 fwrite(old_data, 1, optr - old_data, f);
419                 free(old_data);
420         }
421         fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
422         fclose(f);
423         return 0;
424 }
425
426 /* --------------------------------------------------------------------------*/
427
428 int config_readlrom(const char *fname)
429 {
430         char line[128], *tmp;
431         int i, len, ret = -1;
432         FILE *f;
433
434         f = fopen(fname, "r");
435         if (f == NULL) return -1;
436
437         // seek to the section needed
438         while (!feof(f))
439         {
440                 tmp = fgets(line, sizeof(line), f);
441                 if (tmp == NULL) break;
442
443                 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
444                 len = strlen(line);
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;
449                 tmp++;
450                 mystrip(tmp);
451
452                 len = sizeof(rom_fname_loaded);
453                 strncpy(rom_fname_loaded, tmp, len);
454                 rom_fname_loaded[len-1] = 0;
455                 ret = 0;
456                 break;
457         }
458         fclose(f);
459         return ret;
460 }
461
462
463 static int custom_read(menu_entry *me, const char *var, const char *val)
464 {
465         char *tmp;
466         int tmpi;
467
468         switch (me->id)
469         {
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;
474                         }
475                         else if (strcasecmp(val, "16bit accurate") == 0 || strcasecmp(val, "accurate") == 0) {
476                                 PicoOpt &= ~POPT_ALT_RENDERER;
477                                 currentConfig.EmuOpt |=  0x80;
478                         }
479                         else if (strcasecmp(val, "8bit accurate") == 0) {
480                                 PicoOpt &= ~POPT_ALT_RENDERER;
481                                 currentConfig.EmuOpt &= ~0x80;
482                         }
483                         else
484                                 return 0;
485                         return 1;
486
487                 case MA_OPT_SCALING:
488 #ifdef __GP2X__
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;
498                         } else
499                                 currentConfig.scaling = atoi(val);
500                         return 1;
501 #else
502                         return 0;
503 #endif
504
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);
510                         return 1;
511
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)
516                                 PsndRate = 22050;
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;
524                         } else
525                                 return 0;
526                         return 1;
527
528                 case MA_OPT_REGION:
529                         if (strcasecmp(var, "Region") != 0) return 0;
530                         if       (strncasecmp(val, "Auto: ", 6) == 0)
531                         {
532                                 const char *p = val + 5, *end = val + strlen(val);
533                                 int i;
534                                 PicoRegionOverride = PicoAutoRgnOrder = 0;
535                                 for (i = 0; p < end && i < 3; i++)
536                                 {
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);
544                                         }
545                                         while (*p != ' ' && *p != 0) p++;
546                                         if (*p == 0) break;
547                                 }
548                         }
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;
559                         } else
560                                 return 0;
561                         return 1;
562
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;
576                         } else
577                                 return 0;
578                         return 1;
579
580                 case MA_OPT2_GAMMA:
581                         if (strcasecmp(var, "Gamma correction") != 0) return 0;
582                         currentConfig.gamma = (int) (atof(val) * 100.0);
583                         return 1;
584
585                 case MA_OPT2_SQUIDGEHACK:
586                         if (strcasecmp(var, "Squidgehack") != 0) return 0;
587                         tmpi = atoi(val);
588                         if (tmpi) *(int *)me->var |=  me->mask;
589                         else      *(int *)me->var &= ~me->mask;
590                         return 1;
591
592                 case MA_CDOPT_READAHEAD:
593                         if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
594                         PicoCDBuffers = atoi(val) / 2;
595                         return 1;
596
597                 /* PSP */
598                 case MA_OPT3_SCALE:
599                         if (strcasecmp(var, "Scale factor") != 0) return 0;
600                         currentConfig.scale = atof(val);
601                         return 1;
602                 case MA_OPT3_HSCALE32:
603                         if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
604                         currentConfig.hscale32 = atof(val);
605                         return 1;
606                 case MA_OPT3_HSCALE40:
607                         if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
608                         currentConfig.hscale40 = atof(val);
609                         return 1;
610                 case MA_OPT3_FILTERING:
611                         if (strcasecmp(var, "Bilinear filtering") != 0) return 0;
612                         currentConfig.scaling = atoi(val);
613                         return 1;
614                 case MA_OPT3_GAMMAA:
615                         if (strcasecmp(var, "Gamma adjustment") != 0) return 0;
616                         currentConfig.gamma = atoi(val);
617                         return 1;
618                 case MA_OPT3_BLACKLVL:
619                         if (strcasecmp(var, "Black level") != 0) return 0;
620                         currentConfig.gamma2 = atoi(val);
621                         return 1;
622                 case MA_OPT3_VSYNC:
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;
631                         } else
632                                 return 0;
633                         return 1;
634
635                 default:
636                         lprintf("unhandled custom_read: %i\n", me->id);
637                         return 0;
638         }
639 }
640
641
642 static unsigned int keys_encountered = 0;
643
644 static int parse_bind_val(const char *val, int *type)
645 {
646         int i;
647
648         *type = IN_BINDTYPE_NONE;
649         if (val[0] == 0)
650                 return 0;
651         
652         if (strncasecmp(val, "player", 6) == 0)
653         {
654                 int player, shift = 0;
655                 player = atoi(val + 6) - 1;
656
657                 if (player > 1)
658                         return -1;
659                 if (player == 1)
660                         shift = 16;
661
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;
666                 }
667         }
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;
672                 }
673         }
674
675         return -1;
676 }
677
678 static void keys_parse(const char *key, const char *val, int dev_id)
679 {
680         int acts, type;
681
682         acts = parse_bind_val(val, &type);
683         if (acts == -1) {
684                 lprintf("config: unhandled action \"%s\"\n", val);
685                 return;
686         }
687
688         in_config_bind_key(dev_id, key, acts, type);
689 }
690
691 static int get_numvar_num(const char *var)
692 {
693         char *p = NULL;
694         int num;
695         
696         if (var[0] == ' ')
697                 return 0;
698
699         num = strtoul(var, &p, 10);
700         if (*p == 0 || *p == ' ')
701                 return num;
702
703         return -1;
704 }
705
706 /* map dev number in confing to input dev number */
707 static unsigned char input_dev_map[IN_MAX_DEVS];
708
709 static void parse(const char *var, const char *val)
710 {
711         menu_entry *me;
712         int tmp, ret = 0;
713
714         if (strcasecmp(var, "LastUsedROM") == 0)
715                 return; /* handled elsewhere */
716
717         if (strcasecmp(var, "Sound Volume") == 0) {
718                 currentConfig.volume = atoi(val);
719                 return;
720         }
721
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);
727                 else
728                         lprintf("config: failed to parse: %s\n", var);
729                 return;
730         }
731
732         // key binds
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);
738                         return;
739                 }
740
741                 num = input_dev_map[num];
742                 if (num < 0 || num >= IN_MAX_DEVS) {
743                         lprintf("config: invalid device id: %s\n", var);
744                         return;
745                 }
746
747                 while (*p && *p != ' ') p++;
748                 while (*p && *p == ' ') p++;
749                 keys_parse(p, val, num);
750                 return;
751         }
752
753         me = me_list_get_first();
754         while (me != NULL && ret == 0)
755         {
756                 if (!me->need_to_save)
757                         goto next;
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) {
762                                 tmp = atoi(val);
763                                 if (tmp) *(int *)me->var |=  me->mask;
764                                 else     *(int *)me->var &= ~me->mask;
765                                 return;
766                         } else if (me->beh == MB_OPT_RANGE) {
767                                 tmp = atoi(val);
768                                 if (tmp < me->min) tmp = me->min;
769                                 if (tmp > me->max) tmp = me->max;
770                                 *(int *)me->var = tmp;
771                                 return;
772                         }
773                 }
774                 ret = custom_read(me, var, val);
775 next:
776                 me = me_list_get_next();
777         }
778         if (!ret) lprintf("config_readsect: unhandled var: \"%s\"\n", var);
779 }
780
781
782 int config_havesect(const char *fname, const char *section)
783 {
784         FILE *f;
785         int ret;
786
787         f = fopen(fname, "r");
788         if (f == NULL) return 0;
789
790         ret = seek_sect(f, section);
791         fclose(f);
792         return ret;
793 }
794
795 int config_readsect(const char *fname, const char *section)
796 {
797         char line[128], *var, *val;
798         FILE *f;
799         int ret;
800
801         f = fopen(fname, "r");
802         if (f == NULL) return -1;
803
804         if (section != NULL)
805         {
806                 ret = seek_sect(f, section);
807                 if (!ret) {
808                         lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
809                         fclose(f);
810                         return -1;
811                 }
812         }
813
814         keys_encountered = 0;
815         memset(input_dev_map, 0xff, sizeof(input_dev_map));
816
817         in_config_start();
818         while (!feof(f))
819         {
820                 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
821                 if (ret ==  0) break;
822                 if (ret == -1) continue;
823
824                 parse(var, val);
825         }
826         in_config_end();
827
828         fclose(f);
829         return 0;
830 }
831
832 #endif // _MSC_VER
833
834 static char *mystrip(char *str)
835 {
836         int i, len;
837
838         len = strlen(str);
839         for (i = 0; i < len; i++)
840                 if (str[i] != ' ') break;
841         if (i > 0) memmove(str, str + i, len - i + 1);
842
843         len = strlen(str);
844         for (i = len - 1; i >= 0; i--)
845                 if (str[i] != ' ') break;
846         str[i+1] = 0;
847
848         return str;
849 }
850
851 /* returns:
852  *  0 - EOF, end
853  *  1 - parsed ok
854  * -1 - failed to parse line
855  */
856 int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
857 {
858         char *var, *val, *tmp;
859         FILE *f = file;
860         int len, i;
861
862         tmp = fgets(line, lsize, f);
863         if (tmp == NULL) return 0;
864
865         if (line[0] == '[') return 0; // other section
866
867         // strip comments, linefeed, spaces..
868         len = strlen(line);
869         for (i = 0; i < len; i++)
870                 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
871         mystrip(line);
872         len = strlen(line);
873         if (len <= 0) return -1;;
874
875         // get var and val
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);
880                 return -1;
881         }
882         line[i] = 0;
883         var = line;
884         val = &line[i+1];
885         mystrip(var);
886         mystrip(val);
887
888 #ifndef _MSC_VER
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);
891                 return -1;;
892         }
893 #endif
894
895         *rvar = var;
896         *rval = val;
897         return 1;
898 }
899