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