key combos for new input code
[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                 case MA_OPT2_GAMMA:
54                         if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
55                         fprintf(f, "Gamma correction = %.3f", (double)currentConfig.gamma / 100.0);
56                         break;
57                 case MA_OPT2_SQUIDGEHACK:
58                         if (no_def && !((defaultConfig.EmuOpt^currentConfig.EmuOpt)&0x0010)) return;
59                         fprintf(f, "Squidgehack = %i", (currentConfig.EmuOpt&0x0010)>>4);
60                         break;
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);
65                         break;
66                 /* PSP */
67                 case MA_OPT3_SCALE:
68                         if (no_def && defaultConfig.scale == currentConfig.scale) return;
69                         fprintf(f, "Scale factor = %.2f", currentConfig.scale);
70                         break;
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);
74                         break;
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);
78                         break;
79                 case MA_OPT3_FILTERING:
80                         if (no_def && defaultConfig.scaling == currentConfig.scaling) return;
81                         fprintf(f, "Bilinear filtering = %i", currentConfig.scaling);
82                         break;
83                 case MA_OPT3_GAMMAA:
84                         if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
85                         fprintf(f, "Gamma adjustment = %i", currentConfig.gamma);
86                         break;
87                 case MA_OPT3_BLACKLVL:
88                         if (no_def && defaultConfig.gamma2 == currentConfig.gamma2) return;
89                         fprintf(f, "Black level = %i", currentConfig.gamma2);
90                         break;
91                 case MA_OPT3_VSYNC:
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);
97                         break;
98
99                 default:
100                         lprintf("unhandled custom_write: %i\n", me->id);
101                         return;
102         }
103         fprintf(f, NL);
104 }
105
106
107 static void keys_write(FILE *fn, const char *bind_str, int dev_id, const int *binds, int no_defaults)
108 {
109         char act[48];
110         int key_count, t, i;
111         const int *def_binds;
112
113         key_count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);
114         def_binds = in_get_dev_def_binds(dev_id);
115
116         for (t = 0; t < key_count; t++)
117         {
118                 const char *name;
119                 act[0] = act[31] = 0;
120
121                 if (no_defaults && binds[t] == def_binds[t])
122                         continue;
123
124                 name = in_get_key_name(dev_id, t);
125 #ifdef __GP2X__
126                 if (strcmp(name, "SELECT") == 0) continue;
127 #endif
128
129                 if (binds[t] == 0 && def_binds[t] != 0) {
130                         fprintf(fn, "%s %s =" NL, bind_str, name);
131                         continue;
132                 }
133
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));
139                         }
140                 }
141
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));
146                         }
147                 }
148         }
149 }
150
151
152 static int default_var(const menu_entry *me)
153 {
154         switch (me->id)
155         {
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:
164                 case MA_CDOPT_CDDA:
165                 case MA_CDOPT_PCM:
166                 case MA_CDOPT_SAVERAM:
167                 case MA_CDOPT_SCALEROT_CHIP:
168                 case MA_CDOPT_BETTER_SYNC:
169                         return defaultConfig.s_PicoOpt;
170
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:
175                 case MA_OPT2_VSYNC:
176                 case MA_OPT2_GZIP_STATES:
177                 case MA_OPT2_NO_LAST_ROM:
178                 case MA_OPT2_RAMTIMINGS:
179                 case MA_CDOPT_LEDS:
180                         return defaultConfig.EmuOpt;
181
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;
185
186                 case MA_OPT_SAVE_SLOT:
187                 default:
188                         return 0;
189         }
190 }
191
192 int config_writesect(const char *fname, const char *section)
193 {
194         FILE *fo = NULL, *fn = NULL; // old and new
195         int no_defaults = 0; // avoid saving defaults
196         menu_entry *me;
197         int t, tlen, ret;
198         char line[128], *tmp;
199
200         if (section != NULL)
201         {
202                 no_defaults = 1;
203
204                 fo = fopen(fname, "r");
205                 if (fo == NULL) {
206                         fn = fopen(fname, "w");
207                         goto write;
208                 }
209
210                 ret = seek_sect(fo, section);
211                 if (!ret) {
212                         // sect not found, we can simply append
213                         fclose(fo); fo = NULL;
214                         fn = fopen(fname, "a");
215                         goto write;
216                 }
217
218                 // use 2 files..
219                 fclose(fo);
220                 rename(fname, "tmp.cfg");
221                 fo = fopen("tmp.cfg", "r");
222                 fn = fopen(fname, "w");
223                 if (fo == NULL || fn == NULL) goto write;
224
225                 // copy everything until sect
226                 tlen = strlen(section);
227                 while (!feof(fo))
228                 {
229                         tmp = fgets(line, sizeof(line), fo);
230                         if (tmp == NULL) break;
231
232                         if (line[0] == '[' && strncmp(line + 1, section, tlen) == 0 && line[tlen+1] == ']')
233                                 break;
234                         fputs(line, fn);
235                 }
236
237                 // now skip to next sect
238                 while (!feof(fo))
239                 {
240                         tmp = fgets(line, sizeof(line), fo);
241                         if (tmp == NULL) break;
242                         if (line[0] == '[') {
243                                 fseek(fo, -strlen(line), SEEK_CUR);
244                                 break;
245                         }
246                 }
247                 if (feof(fo))
248                 {
249                         fclose(fo); fo = NULL;
250                         remove("tmp.cfg");
251                 }
252         }
253         else
254         {
255                 fn = fopen(fname, "w");
256         }
257
258 write:
259         if (fn == NULL) {
260                 if (fo) fclose(fo);
261                 return -1;
262         }
263         if (section != NULL)
264                 fprintf(fn, "[%s]" NL, section);
265
266         me = me_list_get_first();
267         while (me != NULL)
268         {
269                 int dummy;
270                 if (!me->need_to_save)
271                         goto next;
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;
281                         mystrip(line);
282                         fprintf(fn, "%s = %s" NL, me->name, line);
283                 } else
284                         custom_write(fn, me, no_defaults);
285 next:
286                 me = me_list_get_next();
287         }
288
289         /* input: save device names */
290         for (t = 0; t < IN_MAX_DEVS; t++)
291         {
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)
295                         continue;
296
297                 fprintf(fn, "input%d = %s" NL, t, name);
298         }
299
300         /* input: save binds */
301         for (t = 0; t < IN_MAX_DEVS; t++)
302         {
303                 const int *binds = in_get_dev_binds(t);
304                 const char *name = in_get_dev_name(t, 0, 0);
305                 char strbind[16];
306                 int count;
307
308                 if (binds == NULL || name == NULL)
309                         continue;
310
311                 sprintf(strbind, "bind%d", t);
312                 if (t == 0) strbind[4] = 0;
313
314                 count = in_get_dev_info(t, IN_INFO_BIND_COUNT);
315                 keys_write(fn, strbind, t, binds, no_defaults);
316         }
317
318 #ifndef PSP
319         if (section == NULL)
320                 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
321 #endif
322
323         fprintf(fn, NL);
324
325         if (fo != NULL)
326         {
327                 // copy whatever is left
328                 while (!feof(fo))
329                 {
330                         tmp = fgets(line, sizeof(line), fo);
331                         if (tmp == NULL) break;
332
333                         fputs(line, fn);
334                 }
335                 fclose(fo);
336                 remove("tmp.cfg");
337         }
338
339         fclose(fn);
340         return 0;
341 }
342
343
344 int config_writelrom(const char *fname)
345 {
346         char line[128], *tmp, *optr = NULL;
347         char *old_data = NULL;
348         int size;
349         FILE *f;
350
351         if (strlen(rom_fname_loaded) == 0) return -1;
352
353         f = fopen(fname, "r");
354         if (f != NULL)
355         {
356                 fseek(f, 0, SEEK_END);
357                 size = ftell(f);
358                 fseek(f, 0, SEEK_SET);
359                 old_data = malloc(size + size/8);
360                 if (old_data != NULL)
361                 {
362                         optr = old_data;
363                         while (!feof(f))
364                         {
365                                 tmp = fgets(line, sizeof(line), f);
366                                 if (tmp == NULL) break;
367                                 mystrip(line);
368                                 if (strncasecmp(line, "LastUsedROM", 11) == 0)
369                                         continue;
370                                 sprintf(optr, "%s", line);
371                                 optr += strlen(optr);
372                         }
373                 }
374                 fclose(f);
375         }
376
377         f = fopen(fname, "w");
378         if (f == NULL) return -1;
379
380         if (old_data != NULL) {
381                 fwrite(old_data, 1, optr - old_data, f);
382                 free(old_data);
383         }
384         fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
385         fclose(f);
386         return 0;
387 }
388
389 /* --------------------------------------------------------------------------*/
390
391 int config_readlrom(const char *fname)
392 {
393         char line[128], *tmp;
394         int i, len, ret = -1;
395         FILE *f;
396
397         f = fopen(fname, "r");
398         if (f == NULL) return -1;
399
400         // seek to the section needed
401         while (!feof(f))
402         {
403                 tmp = fgets(line, sizeof(line), f);
404                 if (tmp == NULL) break;
405
406                 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
407                 len = strlen(line);
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;
412                 tmp++;
413                 mystrip(tmp);
414
415                 len = sizeof(rom_fname_loaded);
416                 strncpy(rom_fname_loaded, tmp, len);
417                 rom_fname_loaded[len-1] = 0;
418                 ret = 0;
419                 break;
420         }
421         fclose(f);
422         return ret;
423 }
424
425
426 static int custom_read(menu_entry *me, const char *var, const char *val)
427 {
428         char *tmp;
429         int tmpi;
430
431         switch (me->id)
432         {
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;
437                         }
438                         else if (strcasecmp(val, "16bit accurate") == 0 || strcasecmp(val, "accurate") == 0) {
439                                 PicoOpt &= ~POPT_ALT_RENDERER;
440                                 currentConfig.EmuOpt |=  0x80;
441                         }
442                         else if (strcasecmp(val, "8bit accurate") == 0) {
443                                 PicoOpt &= ~POPT_ALT_RENDERER;
444                                 currentConfig.EmuOpt &= ~0x80;
445                         }
446                         else
447                                 return 0;
448                         return 1;
449
450                 case MA_OPT_SCALING:
451 #ifdef __GP2X__
452                         if (strcasecmp(var, "Scaling") != 0) return 0;
453                         if        (strcasecmp(val, "OFF") == 0) {
454                                 currentConfig.scaling = 0;
455                         } else if (strcasecmp(val, "hw horizontal") == 0) {
456                                 currentConfig.scaling = 1;
457                         } else if (strcasecmp(val, "hw horiz. + vert.") == 0) {
458                                 currentConfig.scaling = 2;
459                         } else if (strcasecmp(val, "sw horizontal") == 0) {
460                                 currentConfig.scaling = 3;
461                         } else
462                                 return 0;
463                         return 1;
464 #else
465                         return 0;
466 #endif
467
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);
473                         return 1;
474
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)
479                                 PsndRate = 22050;
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;
485                         } else
486                                 return 0;
487                         return 1;
488
489                 case MA_OPT_REGION:
490                         if (strcasecmp(var, "Region") != 0) return 0;
491                         if       (strncasecmp(val, "Auto: ", 6) == 0)
492                         {
493                                 const char *p = val + 5, *end = val + strlen(val);
494                                 int i;
495                                 PicoRegionOverride = PicoAutoRgnOrder = 0;
496                                 for (i = 0; p < end && i < 3; i++)
497                                 {
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);
505                                         }
506                                         while (*p != ' ' && *p != 0) p++;
507                                         if (*p == 0) break;
508                                 }
509                         }
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;
520                         } else
521                                 return 0;
522                         return 1;
523
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;
537                         } else
538                                 return 0;
539                         return 1;
540
541                 case MA_OPT_CPU_CLOCKS:
542 #ifdef __GP2X__
543                         if (strcasecmp(var, "GP2X CPU clocks") != 0) return 0;
544 #elif defined(PSP)
545                         if (strcasecmp(var, "PSP CPU clock") != 0) return 0;
546 #endif
547                         currentConfig.CPUclock = atoi(val);
548                         return 1;
549
550                 case MA_OPT2_GAMMA:
551                         if (strcasecmp(var, "Gamma correction") != 0) return 0;
552                         currentConfig.gamma = (int) (atof(val) * 100.0);
553                         return 1;
554
555                 case MA_OPT2_SQUIDGEHACK:
556                         if (strcasecmp(var, "Squidgehack") != 0) return 0;
557                         tmpi = atoi(val);
558                         if (tmpi) *(int *)me->var |=  me->mask;
559                         else      *(int *)me->var &= ~me->mask;
560                         return 1;
561
562                 case MA_CDOPT_READAHEAD:
563                         if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
564                         PicoCDBuffers = atoi(val) / 2;
565                         return 1;
566
567                 /* PSP */
568                 case MA_OPT3_SCALE:
569                         if (strcasecmp(var, "Scale factor") != 0) return 0;
570                         currentConfig.scale = atof(val);
571                         return 1;
572                 case MA_OPT3_HSCALE32:
573                         if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
574                         currentConfig.hscale32 = atof(val);
575                         return 1;
576                 case MA_OPT3_HSCALE40:
577                         if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
578                         currentConfig.hscale40 = atof(val);
579                         return 1;
580                 case MA_OPT3_FILTERING:
581                         if (strcasecmp(var, "Bilinear filtering") != 0) return 0;
582                         currentConfig.scaling = atoi(val);
583                         return 1;
584                 case MA_OPT3_GAMMAA:
585                         if (strcasecmp(var, "Gamma adjustment") != 0) return 0;
586                         currentConfig.gamma = atoi(val);
587                         return 1;
588                 case MA_OPT3_BLACKLVL:
589                         if (strcasecmp(var, "Black level") != 0) return 0;
590                         currentConfig.gamma2 = atoi(val);
591                         return 1;
592                 case MA_OPT3_VSYNC:
593                         if (strcasecmp(var, "Wait for vsync") != 0) return 0;
594                         if        (strcasecmp(val, "never") == 0) {
595                                 currentConfig.EmuOpt &= ~0x12000;
596                         } else if (strcasecmp(val, "sometimes") == 0) {
597                                 currentConfig.EmuOpt |=  0x12000;
598                         } else if (strcasecmp(val, "always") == 0) {
599                                 currentConfig.EmuOpt &= ~0x12000;
600                                 currentConfig.EmuOpt |=  0x02000;
601                         } else
602                                 return 0;
603                         return 1;
604
605                 default:
606                         lprintf("unhandled custom_read: %i\n", me->id);
607                         return 0;
608         }
609 }
610
611
612 static unsigned int keys_encountered = 0;
613
614 static int parse_bind_val(const char *val)
615 {
616         int i;
617
618         if (val[0] == 0)
619                 return 0;
620         
621         if (strncasecmp(val, "player", 6) == 0)
622         {
623                 unsigned int player;
624                 player = atoi(val + 6) - 1;
625                 if (player > 1)
626                         return -1;
627
628                 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
629                         if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
630                                 return me_ctrl_actions[i].mask | (player<<16);
631                 }
632         }
633         for (i = 0; emuctrl_actions[i].name != NULL; i++) {
634                 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0)
635                         return emuctrl_actions[i].mask;
636         }
637
638         return -1;
639 }
640
641 static void keys_parse(const char *key, const char *val, int dev_id)
642 {
643         int binds;
644
645         binds = parse_bind_val(val);
646         if (binds == -1) {
647                 lprintf("config: unhandled action \"%s\"\n", val);
648                 return;
649         }
650
651         in_config_bind_key(dev_id, key, binds);
652 }
653
654 static int get_numvar_num(const char *var)
655 {
656         char *p = NULL;
657         int num;
658         
659         if (var[0] == ' ')
660                 return 0;
661
662         num = strtoul(var, &p, 10);
663         if (*p == 0 || *p == ' ')
664                 return num;
665
666         return -1;
667 }
668
669 /* map dev number in confing to input dev number */
670 static unsigned char input_dev_map[IN_MAX_DEVS];
671
672 static void parse(const char *var, const char *val)
673 {
674         menu_entry *me;
675         int tmp, ret = 0;
676
677         if (strcasecmp(var, "LastUsedROM") == 0)
678                 return; /* handled elsewhere */
679
680         if (strcasecmp(var, "Sound Volume") == 0) {
681                 currentConfig.volume = atoi(val);
682                 return;
683         }
684
685         /* input: device name */
686         if (strncasecmp(var, "input", 5) == 0) {
687                 int num = get_numvar_num(var + 5);
688                 if (num >= 0 && num < IN_MAX_DEVS)
689                         input_dev_map[num] = in_config_parse_dev(val);
690                 else
691                         lprintf("config: failed to parse: %s\n", var);
692                 return;
693         }
694
695         // key binds
696         if (strncasecmp(var, "bind", 4) == 0) {
697                 const char *p = var + 4;
698                 int num = get_numvar_num(p);
699                 if (num < 0 || num >= IN_MAX_DEVS) {
700                         lprintf("config: failed to parse: %s\n", var);
701                         return;
702                 }
703
704                 num = input_dev_map[num];
705                 if (num < 0 || num >= IN_MAX_DEVS) {
706                         lprintf("config: invalid device id: %s\n", var);
707                         return;
708                 }
709
710                 while (*p && *p != ' ') p++;
711                 while (*p && *p == ' ') p++;
712                 keys_parse(p, val, num);
713                 return;
714         }
715
716         me = me_list_get_first();
717         while (me != NULL && ret == 0)
718         {
719                 if (!me->need_to_save)
720                         goto next;
721                 if (me->name != NULL && me->name[0] != 0) {
722                         if (strcasecmp(var, me->name) != 0)
723                                 goto next; /* surely not this one */
724                         if (me->beh == MB_OPT_ONOFF) {
725                                 tmp = atoi(val);
726                                 if (tmp) *(int *)me->var |=  me->mask;
727                                 else     *(int *)me->var &= ~me->mask;
728                                 return;
729                         } else if (me->beh == MB_OPT_RANGE) {
730                                 tmp = atoi(val);
731                                 if (tmp < me->min) tmp = me->min;
732                                 if (tmp > me->max) tmp = me->max;
733                                 *(int *)me->var = tmp;
734                                 return;
735                         }
736                 }
737                 ret = custom_read(me, var, val);
738 next:
739                 me = me_list_get_next();
740         }
741         if (!ret) lprintf("config_readsect: unhandled var: \"%s\"\n", var);
742 }
743
744
745 int config_havesect(const char *fname, const char *section)
746 {
747         FILE *f;
748         int ret;
749
750         f = fopen(fname, "r");
751         if (f == NULL) return 0;
752
753         ret = seek_sect(f, section);
754         fclose(f);
755         return ret;
756 }
757
758 int config_readsect(const char *fname, const char *section)
759 {
760         char line[128], *var, *val;
761         FILE *f;
762         int ret;
763
764         f = fopen(fname, "r");
765         if (f == NULL) return -1;
766
767         if (section != NULL)
768         {
769                 ret = seek_sect(f, section);
770                 if (!ret) {
771                         lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
772                         fclose(f);
773                         return -1;
774                 }
775         }
776
777         keys_encountered = 0;
778         memset(input_dev_map, 0xff, sizeof(input_dev_map));
779
780         in_config_start();
781         while (!feof(f))
782         {
783                 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
784                 if (ret ==  0) break;
785                 if (ret == -1) continue;
786
787                 parse(var, val);
788         }
789         in_config_end();
790
791         fclose(f);
792         return 0;
793 }
794
795 #endif // _MSC_VER
796
797 static char *mystrip(char *str)
798 {
799         int i, len;
800
801         len = strlen(str);
802         for (i = 0; i < len; i++)
803                 if (str[i] != ' ') break;
804         if (i > 0) memmove(str, str + i, len - i + 1);
805
806         len = strlen(str);
807         for (i = len - 1; i >= 0; i--)
808                 if (str[i] != ' ') break;
809         str[i+1] = 0;
810
811         return str;
812 }
813
814 /* returns:
815  *  0 - EOF, end
816  *  1 - parsed ok
817  * -1 - failed to parse line
818  */
819 int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
820 {
821         char *var, *val, *tmp;
822         FILE *f = file;
823         int len, i;
824
825         tmp = fgets(line, lsize, f);
826         if (tmp == NULL) return 0;
827
828         if (line[0] == '[') return 0; // other section
829
830         // strip comments, linefeed, spaces..
831         len = strlen(line);
832         for (i = 0; i < len; i++)
833                 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
834         mystrip(line);
835         len = strlen(line);
836         if (len <= 0) return -1;;
837
838         // get var and val
839         for (i = 0; i < len; i++)
840                 if (line[i] == '=') break;
841         if (i >= len || strchr(&line[i+1], '=') != NULL) {
842                 lprintf("config_readsect: can't parse: %s\n", line);
843                 return -1;
844         }
845         line[i] = 0;
846         var = line;
847         val = &line[i+1];
848         mystrip(var);
849         mystrip(val);
850
851 #ifndef _MSC_VER
852         if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
853                 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
854                 return -1;;
855         }
856 #endif
857
858         *rvar = var;
859         *rval = val;
860         return 1;
861 }
862