2528651632b1292002bc61327d49110dc717d0fd
[picodrive.git] / platform / common / config_file.c
1 /*
2  * Human-readable config file management for PicoDrive
3  * (C) notaz, 2008-2010
4  *
5  * This work is licensed under the terms of MAME license.
6  * See COPYING file in the top-level directory.
7  */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #ifdef __EPOC32__
13 #include <unistd.h>
14 #endif
15
16 #include "../libpicofe/input.h"
17 #include "../libpicofe/plat.h"
18 #include "../libpicofe/lprintf.h"
19 #include "config_file.h"
20
21 static char *mystrip(char *str);
22
23 #ifndef _MSC_VER
24
25 #include "menu_pico.h"
26 #include "emu.h"
27 #include <pico/pico_int.h>
28
29 // always output DOS endlines
30 #ifdef _WIN32
31 #define NL "\n"
32 #else
33 #define NL "\r\n"
34 #endif
35
36 static int seek_sect(FILE *f, const char *section)
37 {
38         char line[640], *tmp;
39         int len;
40
41         len = strlen(section);
42         // seek to the section needed
43         while (!feof(f))
44         {
45                 tmp = fgets(line, sizeof(line), f);
46                 if (tmp == NULL) break;
47
48                 if (line[0] != '[') continue; // not section start
49                 if (strncmp(line + 1, section, len) == 0 && line[len+1] == ']')
50                         return 1; // found it
51         }
52
53         return 0;
54 }
55
56 static void keys_write(FILE *fn, int dev_id, const int *binds, const int *kbd_binds)
57 {
58         char act[48];
59         int key_count = 0, k, i;
60
61         in_get_config(dev_id, IN_CFG_BIND_COUNT, &key_count);
62
63         for (k = 0; k < key_count; k++)
64         {
65                 const char *name;
66                 int mask;
67                 act[0] = act[31] = 0;
68
69                 name = in_get_key_name(dev_id, k);
70                 if (strcmp(name, "#") == 0) name = "\\x23"; // replace comment sign
71                 if (strcmp(name, "=") == 0) name = "\\x3d"; // replace assignment sign
72
73                 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
74                         mask = me_ctrl_actions[i].mask;
75                         if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
76                                 strncpy(act, me_ctrl_actions[i].name, 31);
77                                 fprintf(fn, "bind %s = player1 %s" NL, name, mystrip(act));
78                         }
79                         mask = me_ctrl_actions[i].mask << 16;
80                         if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
81                                 strncpy(act, me_ctrl_actions[i].name, 31);
82                                 fprintf(fn, "bind %s = player2 %s" NL, name, mystrip(act));
83                         }
84                 }
85
86                 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
87                         mask = me_ctrl_actions[i].mask;
88                         if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER34)]) {
89                                 strncpy(act, me_ctrl_actions[i].name, 31);
90                                 fprintf(fn, "bind %s = player3 %s" NL, name, mystrip(act));
91                         }
92                         mask = me_ctrl_actions[i].mask << 16;
93                         if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER34)]) {
94                                 strncpy(act, me_ctrl_actions[i].name, 31);
95                                 fprintf(fn, "bind %s = player4 %s" NL, name, mystrip(act));
96                         }
97                 }
98
99                 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
100                         mask = emuctrl_actions[i].mask;
101                         if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
102                                 strncpy(act, emuctrl_actions[i].name, 31);
103                                 fprintf(fn, "bind %s = %s" NL, name, mystrip(act));
104                         }
105                 }
106         }
107
108         for (k = 0; k < key_count; k++) {
109                 const char *name = in_get_key_name(dev_id, k);
110                 if (strcmp(name, "#") == 0) name = "\\x23"; // replace comment sign
111                 if (strcmp(name, "=") == 0) name = "\\x3d"; // replace assignment sign
112                 if (kbd_binds[k])
113                         fprintf(fn, "bind %s = key%02x" NL, name, kbd_binds[k]);
114         }
115 }
116
117 int config_write(const char *fname)
118 {
119         FILE *fn = NULL;
120         menu_entry *me;
121         int t;
122         char line[640];
123
124         fn = fopen(fname, "w");
125         if (fn == NULL)
126                 return -1;
127
128         for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
129         {
130                 int dummy;
131                 if (!me->need_to_save)
132                         continue;
133                 if (me->name == NULL || me->name[0] == 0)
134                         continue;
135
136                 if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
137                         fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
138                 }
139                 else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
140                         fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
141                 }
142                 else if (me->beh == MB_OPT_ENUM) {
143                         const char **names = (const char **)me->data;
144                         if (names == NULL)
145                                 continue;
146                         for (t = 0; names[t] != NULL; t++) {
147                                 if (*(int *)me->var == t) {
148                                         strncpy(line, names[t], sizeof(line)-1);
149                                         line[sizeof(line)-1] = '\0';
150                                         goto write_line;
151                                 }
152                         }
153                 }
154                 else if (me->generate_name != NULL) {
155                         strncpy(line, me->generate_name(me->id, &dummy), sizeof(line)-1);
156                         line[sizeof(line)-1] = '\0';
157                         goto write_line;
158                 }
159                 else
160                         lprintf("config: unhandled write: '%s' id %d behavior %d\n",
161                                 me->name, me->id, me->beh);
162                 continue;
163
164 write_line:
165                 line[sizeof(line) - 1] = 0;
166                 mystrip(line);
167                 fprintf(fn, "%s = %s" NL, me->name, line);
168         }
169
170         /* input: save binds */
171         for (t = 0; t < IN_MAX_DEVS; t++)
172         {
173                 const int *binds = in_get_dev_binds(t);
174                 const char *name = in_get_dev_name(t, 0, 0);
175                 int count = 0;
176
177                 if (binds == NULL || name == NULL)
178                         continue;
179
180                 fprintf(fn, "binddev = %s" NL, name);
181
182                 in_get_config(t, IN_CFG_BIND_COUNT, &count);
183                 keys_write(fn, t, binds, in_get_dev_kbd_binds(t));
184         }
185
186         fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
187
188         fprintf(fn, NL);
189
190         fclose(fn);
191         return 0;
192 }
193
194 int config_writelrom(const char *fname)
195 {
196         char line[640], *tmp, *optr = NULL;
197         char *old_data = NULL;
198         int size;
199         FILE *f;
200
201         if (strlen(rom_fname_loaded) == 0) return -1;
202
203         f = fopen(fname, "r");
204         if (f != NULL)
205         {
206                 fseek(f, 0, SEEK_END);
207                 size = ftell(f);
208                 fseek(f, 0, SEEK_SET);
209                 old_data = malloc(size + size/8);
210                 if (old_data != NULL)
211                 {
212                         optr = old_data;
213                         while (!feof(f))
214                         {
215                                 tmp = fgets(line, sizeof(line), f);
216                                 if (tmp == NULL) break;
217                                 mystrip(line);
218                                 if (strncasecmp(line, "LastUsedROM", 11) == 0)
219                                         continue;
220                                 sprintf(optr, "%s", line);
221                                 optr += strlen(optr);
222                         }
223                 }
224                 fclose(f);
225         }
226
227         f = fopen(fname, "w");
228         if (f == NULL) return -1;
229
230         if (old_data != NULL) {
231                 fwrite(old_data, 1, optr - old_data, f);
232                 free(old_data);
233         }
234         fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
235         fclose(f);
236         return 0;
237 }
238
239 /* --------------------------------------------------------------------------*/
240
241 int config_readlrom(const char *fname)
242 {
243         char line[640], *tmp;
244         int i, len, ret = -1;
245         FILE *f;
246
247         f = fopen(fname, "r");
248         if (f == NULL) return -1;
249
250         // seek to the section needed
251         while (!feof(f))
252         {
253                 tmp = fgets(line, sizeof(line), f);
254                 if (tmp == NULL) break;
255
256                 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
257                 len = strlen(line);
258                 for (i = 0; i < len; i++)
259                         if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
260                 tmp = strchr(line, '=');
261                 if (tmp == NULL) break;
262                 tmp++;
263                 mystrip(tmp);
264
265                 len = sizeof(rom_fname_loaded)-1;
266                 strncpy(rom_fname_loaded, tmp, len);
267                 rom_fname_loaded[len] = 0;
268                 ret = 0;
269                 break;
270         }
271         fclose(f);
272         return ret;
273 }
274
275 static int custom_read(menu_entry *me, const char *var, const char *val)
276 {
277         char *tmp;
278
279         switch (me->id)
280         {
281                 case MA_OPT_FRAMESKIP:
282                         if (strcasecmp(var, "Frameskip") != 0) return 0;
283                         if (strcasecmp(val, "Auto") == 0)
284                              currentConfig.Frameskip = -1;
285                         else currentConfig.Frameskip = atoi(val);
286                         return 1;
287
288                 case MA_OPT_SOUND_QUALITY:
289                         if (strcasecmp(var, "Sound Quality") != 0) return 0;
290                         PicoIn.sndRate = strtoul(val, &tmp, 10);
291                         if (PicoIn.sndRate < 8000 || PicoIn.sndRate > 54000) {
292                                 if  (strncasecmp(tmp, "native", 6) == 0) {
293                                         tmp += 6;
294                                         PicoIn.sndRate = 53000;
295                                 } else
296                                         PicoIn.sndRate = 22050;
297                         }
298                         if (*tmp == 'H' || *tmp == 'h') tmp++;
299                         if (*tmp == 'Z' || *tmp == 'z') tmp++;
300                         while (*tmp == ' ') tmp++;
301                         if        (strcasecmp(tmp, "stereo") == 0) {
302                                 PicoIn.opt |=  POPT_EN_STEREO;
303                         } else if (strcasecmp(tmp, "mono") == 0) {
304                                 PicoIn.opt &= ~POPT_EN_STEREO;
305                         } else
306                                 return 0;
307                         return 1;
308
309                 case MA_OPT_SOUND_ALPHA:
310                         if (strcasecmp(var, "Filter strength") != 0) return 0;
311                         PicoIn.sndFilterAlpha = 0x10000 * atof(val);
312                         return 1;
313
314                 case MA_OPT_REGION:
315                         if (strcasecmp(var, "Region") != 0) return 0;
316                         if       (strncasecmp(val, "Auto: ", 6) == 0)
317                         {
318                                 const char *p = val + 5, *end = val + strlen(val);
319                                 int i;
320                                 PicoIn.regionOverride = PicoIn.autoRgnOrder = 0;
321                                 for (i = 0; p < end && i < 3; i++)
322                                 {
323                                         while (*p == ' ') p++;
324                                         if        (p[0] == 'J' && p[1] == 'P') {
325                                                 PicoIn.autoRgnOrder |= 1 << (i*4);
326                                         } else if (p[0] == 'U' && p[1] == 'S') {
327                                                 PicoIn.autoRgnOrder |= 4 << (i*4);
328                                         } else if (p[0] == 'E' && p[1] == 'U') {
329                                                 PicoIn.autoRgnOrder |= 8 << (i*4);
330                                         }
331                                         while (*p != ' ' && *p != 0) p++;
332                                         if (*p == 0) break;
333                                 }
334                         }
335                         else   if (strcasecmp(val, "Auto") == 0) {
336                                 PicoIn.regionOverride = 0;
337                         } else if (strcasecmp(val, "Japan NTSC") == 0) {
338                                 PicoIn.regionOverride = 1;
339                         } else if (strcasecmp(val, "Japan PAL") == 0) {
340                                 PicoIn.regionOverride = 2;
341                         } else if (strcasecmp(val, "USA") == 0) {
342                                 PicoIn.regionOverride = 4;
343                         } else if (strcasecmp(val, "Europe") == 0) {
344                                 PicoIn.regionOverride = 8;
345                         } else
346                                 return 0;
347                         return 1;
348
349                 case MA_32XOPT_MSH2_CYCLES:
350                         currentConfig.msh2_khz = atoi(val);
351                         Pico32xSetClocks(currentConfig.msh2_khz * 1000, 0);
352                         return 1;
353
354                 case MA_32XOPT_SSH2_CYCLES:
355                         currentConfig.ssh2_khz = atoi(val);
356                         Pico32xSetClocks(0, currentConfig.ssh2_khz * 1000);
357                         return 1;
358
359                 case MA_OPT2_GAMMA:
360                         currentConfig.gamma = atoi(val);
361                         return 1;
362
363                 case MA_OPT2_MAX_FRAMESKIP:
364                         currentConfig.max_skip = atoi(val);
365                         return 1;
366
367                 case MA_CTRL_KEYBOARD:
368                         currentConfig.keyboard = 0;
369                         if (strcasecmp(val, "physical") == 0)
370                                 currentConfig.keyboard = 2;
371                         else if (strcasecmp(val, "virtual") == 0)
372                                 currentConfig.keyboard = 1;
373                         return 1;
374
375                 /* PSP */
376                 case MA_OPT3_VSYNC:
377                         // XXX: use enum
378                         if (strcasecmp(var, "Wait for vsync") != 0) return 0;
379                         if        (strcasecmp(val, "never") == 0) {
380                                 currentConfig.EmuOpt &= ~(EOPT_VSYNC|EOPT_VSYNC_MODE);
381                         } else if (strcasecmp(val, "sometimes") == 0) {
382                                 currentConfig.EmuOpt |=  (EOPT_VSYNC|EOPT_VSYNC_MODE);
383                         } else if (strcasecmp(val, "always") == 0) {
384                                 currentConfig.EmuOpt &= ~EOPT_VSYNC_MODE;
385                                 currentConfig.EmuOpt |=  EOPT_VSYNC;
386                         } else
387                                 return 0;
388                         return 1;
389
390                 default:
391                         lprintf("unhandled custom_read %i: %s\n", me->id, var);
392                         return 0;
393         }
394 }
395
396 static int parse_bind_val(const char *val, int *type)
397 {
398         int i;
399
400         *type = IN_BINDTYPE_NONE;
401         if (val[0] == 0)
402                 return 0;
403         
404         if (strncasecmp(val, "key", 3) == 0)
405         {
406                 *type = IN_BINDTYPE_KEYBOARD;
407                 return strtol(val + 3, NULL, 16);
408         }
409
410         if (strncasecmp(val, "player", 6) == 0)
411         {
412                 int player, shift = 0;
413                 player = atoi(val + 6) - 1;
414
415                 if (player > 3)
416                         return -1;
417                 if (player & 1)
418                         shift = 16;
419
420                 *type = IN_BINDTYPE_PLAYER12 + (player >> 1);
421                 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
422                         if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
423                                 return me_ctrl_actions[i].mask << shift;
424                 }
425         }
426         for (i = 0; emuctrl_actions[i].name != NULL; i++) {
427                 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
428                         *type = IN_BINDTYPE_EMU;
429                         return emuctrl_actions[i].mask;
430                 }
431         }
432
433         return -1;
434 }
435
436 static void keys_parse_all(FILE *f)
437 {
438         char line[640], *var, *val;
439         int dev_id = -1;
440         int acts, type;
441         int ret;
442
443         while (!feof(f))
444         {
445                 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
446                 if (ret ==  0) break;
447                 if (ret == -1) continue;
448
449                 if (strcasecmp(var, "binddev") == 0) {
450                         dev_id = in_config_parse_dev(val);
451                         if (dev_id < 0) {
452                                 printf("input: can't handle dev: %s\n", val);
453                                 continue;
454                         }
455                         in_unbind_all(dev_id, -1, -1);
456                         continue;
457                 }
458                 if (dev_id < 0 || strncasecmp(var, "bind ", 5) != 0)
459                         continue;
460
461                 acts = parse_bind_val(val, &type);
462                 if (acts == -1) {
463                         lprintf("config: unhandled action \"%s\"\n", val);
464                         continue;
465                 }
466
467                 mystrip(var + 5);
468                 if (type == IN_BINDTYPE_KEYBOARD)
469                         in_config_bind_kbd_key(dev_id, var + 5, acts);
470                 else
471                         in_config_bind_key(dev_id, var + 5, acts, type);
472         }
473         in_clean_binds();
474 }
475
476 static void parse(const char *var, const char *val, int *keys_encountered)
477 {
478         menu_entry *me;
479         int tmp;
480
481         if (strcasecmp(var, "LastUsedROM") == 0)
482                 return; /* handled elsewhere */
483
484         if (strncasecmp(var, "bind", 4) == 0) {
485                 *keys_encountered = 1;
486                 return; /* handled elsewhere */
487         }
488
489         if (strcasecmp(var, "Sound Volume") == 0) {
490                 currentConfig.volume = atoi(val);
491                 return;
492         }
493
494         for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
495         {
496                 char *p;
497
498                 if (!me->need_to_save)
499                         continue;
500                 if (me->name == NULL || strcasecmp(var, me->name) != 0)
501                         continue;
502
503                 if (me->beh == MB_OPT_ONOFF) {
504                         tmp = strtol(val, &p, 0);
505                         if (*p != 0)
506                                 goto bad_val;
507                         if (tmp) *(int *)me->var |=  me->mask;
508                         else     *(int *)me->var &= ~me->mask;
509                         return;
510                 }
511                 else if (me->beh == MB_OPT_RANGE) {
512                         tmp = strtol(val, &p, 0);
513                         if (*p != 0)
514                                 goto bad_val;
515                         if (tmp < me->min) tmp = me->min;
516                         if (tmp > me->max) tmp = me->max;
517                         *(int *)me->var = tmp;
518                         return;
519                 }
520                 else if (me->beh == MB_OPT_ENUM) {
521                         const char **names, *p1;
522                         int i;
523
524                         names = (const char **)me->data;
525                         if (names == NULL)
526                                 goto bad_val;
527                         for (i = 0; names[i] != NULL; i++) {
528                                 for (p1 = names[i]; *p1 == ' '; p1++)
529                                         ;
530                                 if (strcasecmp(p1, val) == 0) {
531                                         *(int *)me->var = i;
532                                         return;
533                                 }
534                         }
535                         goto bad_val;
536                 }
537                 else if (custom_read(me, var, val))
538                         return;
539         }
540
541         lprintf("config_readsect: unhandled var: \"%s\"\n", var);
542         return;
543
544 bad_val:
545         lprintf("config_readsect: unhandled val for \"%s\": \"%s\"\n", var, val);
546 }
547
548 int config_readsect(const char *fname, const char *section)
549 {
550         char line[640], *var, *val;
551         int keys_encountered = 0;
552         FILE *f;
553         int ret;
554
555         f = fopen(fname, "r");
556         if (f == NULL) return -1;
557
558         if (section != NULL)
559         {
560                 /* for game_def.cfg only */
561                 ret = seek_sect(f, section);
562                 if (!ret) {
563                         lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
564                         fclose(f);
565                         return -1;
566                 }
567         }
568
569         emu_set_defconfig();
570
571         while (!feof(f))
572         {
573                 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
574                 if (ret ==  0) break;
575                 if (ret == -1) continue;
576
577                 parse(var, val, &keys_encountered);
578         }
579
580         if (keys_encountered) {
581                 rewind(f);
582                 keys_parse_all(f);
583         }
584
585         fclose(f);
586
587         lprintf("config_readsect: loaded from %s", fname);
588         if (section != NULL)
589                 lprintf(" [%s]", section);
590         printf("\n");
591
592         return 0;
593 }
594
595 #endif // _MSC_VER
596
597 static char *mystrip(char *str)
598 {
599         int i, len;
600
601         len = strlen(str);
602         for (i = 0; i < len; i++)
603                 if (str[i] != ' ') break;
604         if (i > 0) memmove(str, str + i, len - i + 1);
605
606         len = strlen(str);
607         for (i = len - 1; i >= 0; i--)
608                 if (str[i] != ' ') break;
609         str[i+1] = 0;
610
611         return str;
612 }
613
614 /* returns:
615  *  0 - EOF, end
616  *  1 - parsed ok
617  * -1 - failed to parse line
618  */
619 int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
620 {
621         char *var, *val, *tmp;
622         FILE *f = file;
623         int len, i;
624
625         tmp = fgets(line, lsize, f);
626         if (tmp == NULL) return 0;
627
628         if (line[0] == '[') return 0; // other section
629
630         // strip comments, linefeed, spaces..
631         len = strlen(line);
632         for (i = 0; i < len; i++)
633                 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
634         mystrip(line);
635         len = strlen(line);
636         if (len <= 0) return -1;;
637
638         // get var and val
639         for (i = 0; i < len; i++)
640                 if (line[i] == '=') break;
641         if (i >= len || strchr(&line[i+1], '=') != NULL) {
642                 lprintf("config_readsect: can't parse: %s\n", line);
643                 return -1;
644         }
645         line[i] = 0;
646         var = line;
647         val = &line[i+1];
648         mystrip(var);
649         mystrip(val);
650
651 #ifndef _MSC_VER
652         if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
653                 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
654                 return -1;;
655         }
656 #endif
657
658         *rvar = var;
659         *rval = val;
660         return 1;
661 }
662