release 1.85
[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.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[128], *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)
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
71                 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
72                         mask = me_ctrl_actions[i].mask;
73                         if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
74                                 strncpy(act, me_ctrl_actions[i].name, 31);
75                                 fprintf(fn, "bind %s = player1 %s" NL, name, mystrip(act));
76                         }
77                         mask = me_ctrl_actions[i].mask << 16;
78                         if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
79                                 strncpy(act, me_ctrl_actions[i].name, 31);
80                                 fprintf(fn, "bind %s = player2 %s" NL, name, mystrip(act));
81                         }
82                 }
83
84                 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
85                         mask = emuctrl_actions[i].mask;
86                         if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
87                                 strncpy(act, emuctrl_actions[i].name, 31);
88                                 fprintf(fn, "bind %s = %s" NL, name, mystrip(act));
89                         }
90                 }
91         }
92 }
93
94 int config_write(const char *fname)
95 {
96         FILE *fn = NULL;
97         menu_entry *me;
98         int t;
99         char line[128];
100
101         fn = fopen(fname, "w");
102         if (fn == NULL)
103                 return -1;
104
105         for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
106         {
107                 int dummy;
108                 if (!me->need_to_save || !me->enabled)
109                         continue;
110                 if (me->name == NULL || me->name[0] == 0)
111                         continue;
112
113                 if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
114                         fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
115                 }
116                 else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
117                         fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
118                 }
119                 else if (me->beh == MB_OPT_ENUM && me->data != NULL) {
120                         const char **names = (const char **)me->data;
121                         for (t = 0; names[t] != NULL; t++) {
122                                 if (*(int *)me->var == t) {
123                                         strncpy(line, names[t], sizeof(line));
124                                         goto write_line;
125                                 }
126                         }
127                 }
128                 else if (me->generate_name != NULL) {
129                         strncpy(line, me->generate_name(0, &dummy), sizeof(line));
130                         goto write_line;
131                 }
132                 else
133                         lprintf("config: unhandled write: %i\n", me->id);
134                 continue;
135
136 write_line:
137                 line[sizeof(line) - 1] = 0;
138                 mystrip(line);
139                 fprintf(fn, "%s = %s" NL, me->name, line);
140         }
141
142         /* input: save binds */
143         for (t = 0; t < IN_MAX_DEVS; t++)
144         {
145                 const int *binds = in_get_dev_binds(t);
146                 const char *name = in_get_dev_name(t, 0, 0);
147                 int count = 0;
148
149                 if (binds == NULL || name == NULL)
150                         continue;
151
152                 fprintf(fn, "binddev = %s" NL, name);
153
154                 in_get_config(t, IN_CFG_BIND_COUNT, &count);
155                 keys_write(fn, t, binds);
156         }
157
158         fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
159
160         fprintf(fn, NL);
161
162         fclose(fn);
163         return 0;
164 }
165
166 int config_writelrom(const char *fname)
167 {
168         char line[128], *tmp, *optr = NULL;
169         char *old_data = NULL;
170         int size;
171         FILE *f;
172
173         if (strlen(rom_fname_loaded) == 0) return -1;
174
175         f = fopen(fname, "r");
176         if (f != NULL)
177         {
178                 fseek(f, 0, SEEK_END);
179                 size = ftell(f);
180                 fseek(f, 0, SEEK_SET);
181                 old_data = malloc(size + size/8);
182                 if (old_data != NULL)
183                 {
184                         optr = old_data;
185                         while (!feof(f))
186                         {
187                                 tmp = fgets(line, sizeof(line), f);
188                                 if (tmp == NULL) break;
189                                 mystrip(line);
190                                 if (strncasecmp(line, "LastUsedROM", 11) == 0)
191                                         continue;
192                                 sprintf(optr, "%s", line);
193                                 optr += strlen(optr);
194                         }
195                 }
196                 fclose(f);
197         }
198
199         f = fopen(fname, "w");
200         if (f == NULL) return -1;
201
202         if (old_data != NULL) {
203                 fwrite(old_data, 1, optr - old_data, f);
204                 free(old_data);
205         }
206         fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
207         fclose(f);
208         return 0;
209 }
210
211 /* --------------------------------------------------------------------------*/
212
213 int config_readlrom(const char *fname)
214 {
215         char line[128], *tmp;
216         int i, len, ret = -1;
217         FILE *f;
218
219         f = fopen(fname, "r");
220         if (f == NULL) return -1;
221
222         // seek to the section needed
223         while (!feof(f))
224         {
225                 tmp = fgets(line, sizeof(line), f);
226                 if (tmp == NULL) break;
227
228                 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
229                 len = strlen(line);
230                 for (i = 0; i < len; i++)
231                         if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
232                 tmp = strchr(line, '=');
233                 if (tmp == NULL) break;
234                 tmp++;
235                 mystrip(tmp);
236
237                 len = sizeof(rom_fname_loaded);
238                 strncpy(rom_fname_loaded, tmp, len);
239                 rom_fname_loaded[len-1] = 0;
240                 ret = 0;
241                 break;
242         }
243         fclose(f);
244         return ret;
245 }
246
247 static int custom_read(menu_entry *me, const char *var, const char *val)
248 {
249         char *tmp;
250
251         switch (me->id)
252         {
253                 case MA_OPT_FRAMESKIP:
254                         if (strcasecmp(var, "Frameskip") != 0) return 0;
255                         if (strcasecmp(val, "Auto") == 0)
256                              currentConfig.Frameskip = -1;
257                         else currentConfig.Frameskip = atoi(val);
258                         return 1;
259
260                 case MA_OPT_SOUND_QUALITY:
261                         if (strcasecmp(var, "Sound Quality") != 0) return 0;
262                         PsndRate = strtoul(val, &tmp, 10);
263                         if (PsndRate < 8000 || PsndRate > 44100)
264                                 PsndRate = 22050;
265                         if (*tmp == 'H' || *tmp == 'h') tmp++;
266                         if (*tmp == 'Z' || *tmp == 'z') tmp++;
267                         while (*tmp == ' ') tmp++;
268                         if        (strcasecmp(tmp, "stereo") == 0) {
269                                 PicoOpt |=  POPT_EN_STEREO;
270                         } else if (strcasecmp(tmp, "mono") == 0) {
271                                 PicoOpt &= ~POPT_EN_STEREO;
272                         } else
273                                 return 0;
274                         return 1;
275
276                 case MA_OPT_REGION:
277                         if (strcasecmp(var, "Region") != 0) return 0;
278                         if       (strncasecmp(val, "Auto: ", 6) == 0)
279                         {
280                                 const char *p = val + 5, *end = val + strlen(val);
281                                 int i;
282                                 PicoRegionOverride = PicoAutoRgnOrder = 0;
283                                 for (i = 0; p < end && i < 3; i++)
284                                 {
285                                         while (*p == ' ') p++;
286                                         if        (p[0] == 'J' && p[1] == 'P') {
287                                                 PicoAutoRgnOrder |= 1 << (i*4);
288                                         } else if (p[0] == 'U' && p[1] == 'S') {
289                                                 PicoAutoRgnOrder |= 4 << (i*4);
290                                         } else if (p[0] == 'E' && p[1] == 'U') {
291                                                 PicoAutoRgnOrder |= 8 << (i*4);
292                                         }
293                                         while (*p != ' ' && *p != 0) p++;
294                                         if (*p == 0) break;
295                                 }
296                         }
297                         else   if (strcasecmp(val, "Auto") == 0) {
298                                 PicoRegionOverride = 0;
299                         } else if (strcasecmp(val, "Japan NTSC") == 0) {
300                                 PicoRegionOverride = 1;
301                         } else if (strcasecmp(val, "Japan PAL") == 0) {
302                                 PicoRegionOverride = 2;
303                         } else if (strcasecmp(val, "USA") == 0) {
304                                 PicoRegionOverride = 4;
305                         } else if (strcasecmp(val, "Europe") == 0) {
306                                 PicoRegionOverride = 8;
307                         } else
308                                 return 0;
309                         return 1;
310
311                 case MA_CDOPT_READAHEAD:
312                         if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
313                         PicoCDBuffers = atoi(val) / 2;
314                         return 1;
315
316                 case MA_32XOPT_MSH2_CYCLES:
317                         currentConfig.msh2_khz = atoi(val);
318                         Pico32xSetClocks(currentConfig.msh2_khz * 1000, 0);
319                         return 1;
320
321                 case MA_32XOPT_SSH2_CYCLES:
322                         currentConfig.ssh2_khz = atoi(val);
323                         Pico32xSetClocks(0, currentConfig.ssh2_khz * 1000);
324                         return 1;
325
326                 case MA_OPT2_GAMMA:
327                         currentConfig.gamma = atoi(val);
328                         return 1;
329
330                 /* PSP */
331                 case MA_OPT3_SCALE:
332                         if (strcasecmp(var, "Scale factor") != 0) return 0;
333                         currentConfig.scale = atof(val);
334                         return 1;
335                 case MA_OPT3_HSCALE32:
336                         if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
337                         currentConfig.hscale32 = atof(val);
338                         return 1;
339                 case MA_OPT3_HSCALE40:
340                         if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
341                         currentConfig.hscale40 = atof(val);
342                         return 1;
343                 case MA_OPT3_VSYNC:
344                         // XXX: use enum
345                         if (strcasecmp(var, "Wait for vsync") != 0) return 0;
346                         if        (strcasecmp(val, "never") == 0) {
347                                 currentConfig.EmuOpt &= ~0x12000;
348                         } else if (strcasecmp(val, "sometimes") == 0) {
349                                 currentConfig.EmuOpt |=  0x12000;
350                         } else if (strcasecmp(val, "always") == 0) {
351                                 currentConfig.EmuOpt &= ~0x12000;
352                                 currentConfig.EmuOpt |=  0x02000;
353                         } else
354                                 return 0;
355                         return 1;
356
357                 default:
358                         lprintf("unhandled custom_read %i: %s\n", me->id, var);
359                         return 0;
360         }
361 }
362
363 static int parse_bind_val(const char *val, int *type)
364 {
365         int i;
366
367         *type = IN_BINDTYPE_NONE;
368         if (val[0] == 0)
369                 return 0;
370         
371         if (strncasecmp(val, "player", 6) == 0)
372         {
373                 int player, shift = 0;
374                 player = atoi(val + 6) - 1;
375
376                 if (player > 1)
377                         return -1;
378                 if (player == 1)
379                         shift = 16;
380
381                 *type = IN_BINDTYPE_PLAYER12;
382                 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
383                         if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
384                                 return me_ctrl_actions[i].mask << shift;
385                 }
386         }
387         for (i = 0; emuctrl_actions[i].name != NULL; i++) {
388                 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
389                         *type = IN_BINDTYPE_EMU;
390                         return emuctrl_actions[i].mask;
391                 }
392         }
393
394         return -1;
395 }
396
397 static void keys_parse_all(FILE *f)
398 {
399         char line[256], *var, *val;
400         int dev_id = -1;
401         int acts, type;
402         int ret;
403
404         while (!feof(f))
405         {
406                 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
407                 if (ret ==  0) break;
408                 if (ret == -1) continue;
409
410                 if (strcasecmp(var, "binddev") == 0) {
411                         dev_id = in_config_parse_dev(val);
412                         if (dev_id < 0) {
413                                 printf("input: can't handle dev: %s\n", val);
414                                 continue;
415                         }
416                         in_unbind_all(dev_id, -1, -1);
417                         continue;
418                 }
419                 if (dev_id < 0 || strncasecmp(var, "bind ", 5) != 0)
420                         continue;
421
422                 acts = parse_bind_val(val, &type);
423                 if (acts == -1) {
424                         lprintf("config: unhandled action \"%s\"\n", val);
425                         return;
426                 }
427
428                 mystrip(var + 5);
429                 in_config_bind_key(dev_id, var + 5, acts, type);
430         }
431 }
432
433 static void parse(const char *var, const char *val, int *keys_encountered)
434 {
435         menu_entry *me;
436         int tmp;
437
438         if (strcasecmp(var, "LastUsedROM") == 0)
439                 return; /* handled elsewhere */
440
441         if (strncasecmp(var, "bind", 4) == 0) {
442                 *keys_encountered = 1;
443                 return; /* handled elsewhere */
444         }
445
446         if (strcasecmp(var, "Sound Volume") == 0) {
447                 currentConfig.volume = atoi(val);
448                 return;
449         }
450
451         for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
452         {
453                 char *p;
454
455                 if (!me->need_to_save)
456                         continue;
457                 if (me->name == NULL || strcasecmp(var, me->name) != 0)
458                         continue;
459
460                 if (me->beh == MB_OPT_ONOFF) {
461                         tmp = strtol(val, &p, 0);
462                         if (*p != 0)
463                                 goto bad_val;
464                         if (tmp) *(int *)me->var |=  me->mask;
465                         else     *(int *)me->var &= ~me->mask;
466                         return;
467                 }
468                 else if (me->beh == MB_OPT_RANGE) {
469                         tmp = strtol(val, &p, 0);
470                         if (*p != 0)
471                                 goto bad_val;
472                         if (tmp < me->min) tmp = me->min;
473                         if (tmp > me->max) tmp = me->max;
474                         *(int *)me->var = tmp;
475                         return;
476                 }
477                 else if (me->beh == MB_OPT_ENUM) {
478                         const char **names, *p1;
479                         int i;
480
481                         names = (const char **)me->data;
482                         if (names == NULL)
483                                 goto bad_val;
484                         for (i = 0; names[i] != NULL; i++) {
485                                 for (p1 = names[i]; *p1 == ' '; p1++)
486                                         ;
487                                 if (strcasecmp(p1, val) == 0) {
488                                         *(int *)me->var = i;
489                                         return;
490                                 }
491                         }
492                         goto bad_val;
493                 }
494                 else if (custom_read(me, var, val))
495                         return;
496         }
497
498         lprintf("config_readsect: unhandled var: \"%s\"\n", var);
499         return;
500
501 bad_val:
502         lprintf("config_readsect: unhandled val for \"%s\": \"%s\"\n", var, val);
503 }
504
505 int config_readsect(const char *fname, const char *section)
506 {
507         char line[128], *var, *val;
508         int keys_encountered = 0;
509         FILE *f;
510         int ret;
511
512         f = fopen(fname, "r");
513         if (f == NULL) return -1;
514
515         if (section != NULL)
516         {
517                 /* for game_def.cfg only */
518                 ret = seek_sect(f, section);
519                 if (!ret) {
520                         lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
521                         fclose(f);
522                         return -1;
523                 }
524         }
525
526         emu_set_defconfig();
527
528         while (!feof(f))
529         {
530                 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
531                 if (ret ==  0) break;
532                 if (ret == -1) continue;
533
534                 parse(var, val, &keys_encountered);
535         }
536
537         if (keys_encountered) {
538                 rewind(f);
539                 keys_parse_all(f);
540         }
541
542         fclose(f);
543
544         lprintf("config_readsect: loaded from %s", fname);
545         if (section != NULL)
546                 lprintf(" [%s]", section);
547         printf("\n");
548
549         return 0;
550 }
551
552 #endif // _MSC_VER
553
554 static char *mystrip(char *str)
555 {
556         int i, len;
557
558         len = strlen(str);
559         for (i = 0; i < len; i++)
560                 if (str[i] != ' ') break;
561         if (i > 0) memmove(str, str + i, len - i + 1);
562
563         len = strlen(str);
564         for (i = len - 1; i >= 0; i--)
565                 if (str[i] != ' ') break;
566         str[i+1] = 0;
567
568         return str;
569 }
570
571 /* returns:
572  *  0 - EOF, end
573  *  1 - parsed ok
574  * -1 - failed to parse line
575  */
576 int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
577 {
578         char *var, *val, *tmp;
579         FILE *f = file;
580         int len, i;
581
582         tmp = fgets(line, lsize, f);
583         if (tmp == NULL) return 0;
584
585         if (line[0] == '[') return 0; // other section
586
587         // strip comments, linefeed, spaces..
588         len = strlen(line);
589         for (i = 0; i < len; i++)
590                 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
591         mystrip(line);
592         len = strlen(line);
593         if (len <= 0) return -1;;
594
595         // get var and val
596         for (i = 0; i < len; i++)
597                 if (line[i] == '=') break;
598         if (i >= len || strchr(&line[i+1], '=') != NULL) {
599                 lprintf("config_readsect: can't parse: %s\n", line);
600                 return -1;
601         }
602         line[i] = 0;
603         var = line;
604         val = &line[i+1];
605         mystrip(var);
606         mystrip(val);
607
608 #ifndef _MSC_VER
609         if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
610                 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
611                 return -1;;
612         }
613 #endif
614
615         *rvar = var;
616         *rval = val;
617         return 1;
618 }
619