get rid of some CamelCase names
[picodrive.git] / platform / common / config.c
CommitLineData
835e7900 1/*
2 * Human-readable config file management for PicoDrive
ca482e5d 3 * (c) notaz, 2008
835e7900 4 */
5
4609d0cd 6#include <stdio.h>
835e7900 7#include <string.h>
8#include <stdlib.h>
f8af9634 9#ifdef __EPOC32__
ca482e5d 10#include <unistd.h>
11#endif
835e7900 12#include "config.h"
7e4c661a 13#include "input.h"
4609d0cd 14#include "lprintf.h"
15
16static char *mystrip(char *str);
17
18#ifndef _MSC_VER
19
835e7900 20#include "menu.h"
21#include "emu.h"
efcba75f 22#include <pico/pico.h>
835e7900 23
7b802576 24#define NL "\r\n"
835e7900 25
835e7900 26static 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
c46ffd31 47static void custom_write(FILE *f, const menu_entry *me, int no_def)
835e7900 48{
713c9224 49 char str24[24];
835e7900 50
51 switch (me->id)
52 {
835e7900 53 case MA_OPT2_GAMMA:
c46ffd31 54 if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
835e7900 55 fprintf(f, "Gamma correction = %.3f", (double)currentConfig.gamma / 100.0);
56 break;
57 case MA_OPT2_SQUIDGEHACK:
c46ffd31 58 if (no_def && !((defaultConfig.EmuOpt^currentConfig.EmuOpt)&0x0010)) return;
835e7900 59 fprintf(f, "Squidgehack = %i", (currentConfig.EmuOpt&0x0010)>>4);
60 break;
61 case MA_CDOPT_READAHEAD:
c46ffd31 62 if (no_def && defaultConfig.s_PicoCDBuffers == PicoCDBuffers) return;
835e7900 63 sprintf(str24, "%i", PicoCDBuffers * 2);
64 fprintf(f, "ReadAhead buffer = %s", str24);
65 break;
6fc57144 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;
835e7900 98
99 default:
c46ffd31 100 lprintf("unhandled custom_write: %i\n", me->id);
835e7900 101 return;
102 }
103 fprintf(f, NL);
104}
105
1ca2ea4f 106
7e4c661a 107static void keys_write(FILE *fn, const char *bind_str, int dev_id, const int *binds, int no_defaults)
1ca2ea4f 108{
7e4c661a 109 char act[48];
110 int key_count, t, i;
111 const int *def_binds;
112
9025b931 113 key_count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);
7e4c661a 114 def_binds = in_get_dev_def_binds(dev_id);
1ca2ea4f 115
ca482e5d 116 for (t = 0; t < key_count; t++)
1ca2ea4f 117 {
7e4c661a 118 const char *name;
1ca2ea4f 119 act[0] = act[31] = 0;
7e4c661a 120
1ca2ea4f 121 if (no_defaults && binds[t] == def_binds[t])
122 continue;
7e4c661a 123
124 name = in_get_key_name(dev_id, t);
1ca2ea4f 125#ifdef __GP2X__
7e4c661a 126 if (strcmp(name, "SELECT") == 0) continue;
1ca2ea4f 127#endif
ca482e5d 128
6fc57144 129 if (binds[t] == 0 && def_binds[t] != 0) {
7e4c661a 130 fprintf(fn, "%s %s =" NL, bind_str, name);
6fc57144 131 continue;
132 }
133
1ca2ea4f 134 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
135 if (me_ctrl_actions[i].mask & binds[t]) {
bdec53c9 136 strncpy(act, me_ctrl_actions[i].name, 31);
ca482e5d 137 fprintf(fn, "%s %s = player%i %s" NL, bind_str, name,
bdec53c9 138 ((binds[t]>>16)&1)+1, mystrip(act));
1ca2ea4f 139 }
140 }
1ca2ea4f 141
bdec53c9 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);
ca482e5d 145 fprintf(fn, "%s %s = %s" NL, bind_str, name, mystrip(act));
bdec53c9 146 }
147 }
1ca2ea4f 148 }
149}
150
151
c46ffd31 152static 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:
1ca2ea4f 163 case MA_OPT2_SVP_DYNAREC:
c46ffd31 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
ca482e5d 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;
f0f0d2df 185
c46ffd31 186 case MA_OPT_SAVE_SLOT:
187 default:
188 return 0;
189 }
190}
835e7900 191
192int config_writesect(const char *fname, const char *section)
193{
194 FILE *fo = NULL, *fn = NULL; // old and new
c46ffd31 195 int no_defaults = 0; // avoid saving defaults
835e7900 196 menu_entry *me;
713c9224 197 int t, tlen, ret;
835e7900 198 char line[128], *tmp;
199
200 if (section != NULL)
201 {
c46ffd31 202 no_defaults = 1;
203
835e7900 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
258write:
259 if (fn == NULL) {
260 if (fo) fclose(fo);
261 return -1;
262 }
263 if (section != NULL)
264 fprintf(fn, "[%s]" NL, section);
265
713c9224 266 me = me_list_get_first();
267 while (me != NULL)
835e7900 268 {
713c9224 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);
285next:
286 me = me_list_get_next();
835e7900 287 }
1ca2ea4f 288
7e4c661a 289 /* input: save device names */
290 for (t = 0; t < IN_MAX_DEVS; t++)
291 {
292 const int *binds = in_get_dev_binds(t);
713c9224 293 const char *name = in_get_dev_name(t, 0, 0);
7e4c661a 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);
713c9224 304 const char *name = in_get_dev_name(t, 0, 0);
7e4c661a 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
9025b931 314 count = in_get_dev_info(t, IN_INFO_BIND_COUNT);
7e4c661a 315 keys_write(fn, strbind, t, binds, no_defaults);
316 }
317
6fc57144 318#ifndef PSP
7b802576 319 if (section == NULL)
320 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
6fc57144 321#endif
7b802576 322
835e7900 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
835e7900 344int 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
713c9224 351 if (strlen(rom_fname_loaded) == 0) return -1;
835e7900 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 }
713c9224 384 fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
835e7900 385 fclose(f);
386 return 0;
387}
388
1ca2ea4f 389/* --------------------------------------------------------------------------*/
390
391int 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
713c9224 415 len = sizeof(rom_fname_loaded);
416 strncpy(rom_fname_loaded, tmp, len);
417 rom_fname_loaded[len-1] = 0;
1ca2ea4f 418 ret = 0;
419 break;
420 }
421 fclose(f);
422 return ret;
423}
424
835e7900 425
426static 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;
6fc57144 435 if (strcasecmp(val, "8bit fast") == 0 || strcasecmp(val, "fast") == 0) {
602133e1 436 PicoOpt |= POPT_ALT_RENDERER;
835e7900 437 }
6fc57144 438 else if (strcasecmp(val, "16bit accurate") == 0 || strcasecmp(val, "accurate") == 0) {
602133e1 439 PicoOpt &= ~POPT_ALT_RENDERER;
835e7900 440 currentConfig.EmuOpt |= 0x80;
441 }
442 else if (strcasecmp(val, "8bit accurate") == 0) {
602133e1 443 PicoOpt &= ~POPT_ALT_RENDERER;
835e7900 444 currentConfig.EmuOpt &= ~0x80;
445 }
446 else
447 return 0;
448 return 1;
449
450 case MA_OPT_SCALING:
6fc57144 451#ifdef __GP2X__
835e7900 452 if (strcasecmp(var, "Scaling") != 0) return 0;
453 if (strcasecmp(val, "OFF") == 0) {
ee2a3bdf 454 currentConfig.scaling = EOPT_SCALE_NONE;
835e7900 455 } else if (strcasecmp(val, "hw horizontal") == 0) {
ee2a3bdf 456 currentConfig.scaling = EOPT_SCALE_HW_H;
835e7900 457 } else if (strcasecmp(val, "hw horiz. + vert.") == 0) {
ee2a3bdf 458 currentConfig.scaling = EOPT_SCALE_HW_HV;
835e7900 459 } else if (strcasecmp(val, "sw horizontal") == 0) {
ee2a3bdf 460 currentConfig.scaling = EOPT_SCALE_SW_H;
835e7900 461 } else
462 return 0;
463 return 1;
6fc57144 464#else
465 return 0;
466#endif
835e7900 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) {
602133e1 482 PicoOpt |= POPT_EN_STEREO;
835e7900 483 } else if (strcasecmp(tmp, "mono") == 0) {
602133e1 484 PicoOpt &= ~POPT_EN_STEREO;
835e7900 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;
c46ffd31 496 for (i = 0; p < end && i < 3; i++)
497 {
498 while (*p == ' ') p++;
835e7900 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 }
c46ffd31 506 while (*p != ' ' && *p != 0) p++;
507 if (*p == 0) break;
835e7900 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) {
1ca2ea4f 527 currentConfig.EmuOpt &= ~(5<<9);
835e7900 528 } else if (strcasecmp(val, "writes") == 0) {
1ca2ea4f 529 currentConfig.EmuOpt &= ~(5<<9);
530 currentConfig.EmuOpt |= 1<<9;
835e7900 531 } else if (strcasecmp(val, "loads") == 0) {
1ca2ea4f 532 currentConfig.EmuOpt &= ~(5<<9);
533 currentConfig.EmuOpt |= 4<<9;
835e7900 534 } else if (strcasecmp(val, "both") == 0) {
1ca2ea4f 535 currentConfig.EmuOpt &= ~(5<<9);
536 currentConfig.EmuOpt |= 5<<9;
835e7900 537 } else
538 return 0;
539 return 1;
540
42171343 541#if 0 // TODO rm?
835e7900 542 case MA_OPT_CPU_CLOCKS:
6fc57144 543#ifdef __GP2X__
835e7900 544 if (strcasecmp(var, "GP2X CPU clocks") != 0) return 0;
6fc57144 545#elif defined(PSP)
546 if (strcasecmp(var, "PSP CPU clock") != 0) return 0;
547#endif
835e7900 548 currentConfig.CPUclock = atoi(val);
549 return 1;
42171343 550#endif
835e7900 551
552 case MA_OPT2_GAMMA:
553 if (strcasecmp(var, "Gamma correction") != 0) return 0;
554 currentConfig.gamma = (int) (atof(val) * 100.0);
555 return 1;
556
557 case MA_OPT2_SQUIDGEHACK:
558 if (strcasecmp(var, "Squidgehack") != 0) return 0;
559 tmpi = atoi(val);
560 if (tmpi) *(int *)me->var |= me->mask;
561 else *(int *)me->var &= ~me->mask;
562 return 1;
563
564 case MA_CDOPT_READAHEAD:
565 if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
566 PicoCDBuffers = atoi(val) / 2;
567 return 1;
568
6fc57144 569 /* PSP */
570 case MA_OPT3_SCALE:
571 if (strcasecmp(var, "Scale factor") != 0) return 0;
572 currentConfig.scale = atof(val);
573 return 1;
574 case MA_OPT3_HSCALE32:
575 if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
576 currentConfig.hscale32 = atof(val);
577 return 1;
578 case MA_OPT3_HSCALE40:
579 if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
580 currentConfig.hscale40 = atof(val);
581 return 1;
582 case MA_OPT3_FILTERING:
583 if (strcasecmp(var, "Bilinear filtering") != 0) return 0;
584 currentConfig.scaling = atoi(val);
585 return 1;
586 case MA_OPT3_GAMMAA:
587 if (strcasecmp(var, "Gamma adjustment") != 0) return 0;
588 currentConfig.gamma = atoi(val);
589 return 1;
590 case MA_OPT3_BLACKLVL:
591 if (strcasecmp(var, "Black level") != 0) return 0;
592 currentConfig.gamma2 = atoi(val);
593 return 1;
594 case MA_OPT3_VSYNC:
595 if (strcasecmp(var, "Wait for vsync") != 0) return 0;
596 if (strcasecmp(val, "never") == 0) {
597 currentConfig.EmuOpt &= ~0x12000;
598 } else if (strcasecmp(val, "sometimes") == 0) {
599 currentConfig.EmuOpt |= 0x12000;
600 } else if (strcasecmp(val, "always") == 0) {
601 currentConfig.EmuOpt &= ~0x12000;
602 currentConfig.EmuOpt |= 0x02000;
603 } else
604 return 0;
605 return 1;
606
835e7900 607 default:
c46ffd31 608 lprintf("unhandled custom_read: %i\n", me->id);
835e7900 609 return 0;
610 }
611}
612
613
bdec53c9 614static unsigned int keys_encountered = 0;
615
7e4c661a 616static int parse_bind_val(const char *val)
617{
618 int i;
619
620 if (val[0] == 0)
621 return 0;
622
623 if (strncasecmp(val, "player", 6) == 0)
624 {
625 unsigned int player;
626 player = atoi(val + 6) - 1;
627 if (player > 1)
628 return -1;
629
630 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
631 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
632 return me_ctrl_actions[i].mask | (player<<16);
633 }
634 }
635 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
636 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0)
637 return emuctrl_actions[i].mask;
638 }
639
640 return -1;
641}
642
643static void keys_parse(const char *key, const char *val, int dev_id)
1ca2ea4f 644{
7e4c661a 645 int binds;
1ca2ea4f 646
7e4c661a 647 binds = parse_bind_val(val);
648 if (binds == -1) {
649 lprintf("config: unhandled action \"%s\"\n", val);
650 return;
651 }
652
653 in_config_bind_key(dev_id, key, binds);
1ca2ea4f 654}
655
7e4c661a 656static int get_numvar_num(const char *var)
657{
658 char *p = NULL;
659 int num;
660
661 if (var[0] == ' ')
662 return 0;
663
664 num = strtoul(var, &p, 10);
665 if (*p == 0 || *p == ' ')
666 return num;
667
668 return -1;
669}
670
671/* map dev number in confing to input dev number */
672static unsigned char input_dev_map[IN_MAX_DEVS];
673
835e7900 674static void parse(const char *var, const char *val)
675{
676 menu_entry *me;
713c9224 677 int tmp, ret = 0;
835e7900 678
1ca2ea4f 679 if (strcasecmp(var, "LastUsedROM") == 0)
680 return; /* handled elsewhere */
681
7b802576 682 if (strcasecmp(var, "Sound Volume") == 0) {
683 currentConfig.volume = atoi(val);
684 return;
685 }
686
7e4c661a 687 /* input: device name */
688 if (strncasecmp(var, "input", 5) == 0) {
689 int num = get_numvar_num(var + 5);
690 if (num >= 0 && num < IN_MAX_DEVS)
691 input_dev_map[num] = in_config_parse_dev(val);
692 else
b6820926 693 lprintf("config: failed to parse: %s\n", var);
7e4c661a 694 return;
695 }
696
1ca2ea4f 697 // key binds
7e4c661a 698 if (strncasecmp(var, "bind", 4) == 0) {
699 const char *p = var + 4;
700 int num = get_numvar_num(p);
701 if (num < 0 || num >= IN_MAX_DEVS) {
b6820926 702 lprintf("config: failed to parse: %s\n", var);
7e4c661a 703 return;
704 }
705
706 num = input_dev_map[num];
707 if (num < 0 || num >= IN_MAX_DEVS) {
b6820926 708 lprintf("config: invalid device id: %s\n", var);
7e4c661a 709 return;
710 }
711
712 while (*p && *p != ' ') p++;
713 while (*p && *p == ' ') p++;
714 keys_parse(p, val, num);
1ca2ea4f 715 return;
716 }
7e4c661a 717
713c9224 718 me = me_list_get_first();
719 while (me != NULL && ret == 0)
835e7900 720 {
713c9224 721 if (!me->need_to_save)
722 goto next;
723 if (me->name != NULL && me->name[0] != 0) {
724 if (strcasecmp(var, me->name) != 0)
725 goto next; /* surely not this one */
726 if (me->beh == MB_OPT_ONOFF) {
727 tmp = atoi(val);
728 if (tmp) *(int *)me->var |= me->mask;
729 else *(int *)me->var &= ~me->mask;
730 return;
731 } else if (me->beh == MB_OPT_RANGE) {
732 tmp = atoi(val);
733 if (tmp < me->min) tmp = me->min;
734 if (tmp > me->max) tmp = me->max;
735 *(int *)me->var = tmp;
736 return;
835e7900 737 }
835e7900 738 }
713c9224 739 ret = custom_read(me, var, val);
740next:
741 me = me_list_get_next();
835e7900 742 }
84100c0f 743 if (!ret) lprintf("config_readsect: unhandled var: \"%s\"\n", var);
835e7900 744}
745
746
1ca2ea4f 747int config_havesect(const char *fname, const char *section)
748{
749 FILE *f;
750 int ret;
751
752 f = fopen(fname, "r");
753 if (f == NULL) return 0;
754
755 ret = seek_sect(f, section);
756 fclose(f);
757 return ret;
758}
759
835e7900 760int config_readsect(const char *fname, const char *section)
761{
4609d0cd 762 char line[128], *var, *val;
1ca2ea4f 763 FILE *f;
4609d0cd 764 int ret;
835e7900 765
1ca2ea4f 766 f = fopen(fname, "r");
d6114368 767 if (f == NULL) return -1;
835e7900 768
769 if (section != NULL)
770 {
771 ret = seek_sect(f, section);
772 if (!ret) {
c46ffd31 773 lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
835e7900 774 fclose(f);
775 return -1;
776 }
777 }
778
bdec53c9 779 keys_encountered = 0;
7e4c661a 780 memset(input_dev_map, 0xff, sizeof(input_dev_map));
bdec53c9 781
7e4c661a 782 in_config_start();
835e7900 783 while (!feof(f))
784 {
4609d0cd 785 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
786 if (ret == 0) break;
787 if (ret == -1) continue;
835e7900 788
789 parse(var, val);
790 }
7e4c661a 791 in_config_end();
835e7900 792
793 fclose(f);
794 return 0;
795}
796
4609d0cd 797#endif // _MSC_VER
798
799static char *mystrip(char *str)
800{
801 int i, len;
802
803 len = strlen(str);
804 for (i = 0; i < len; i++)
805 if (str[i] != ' ') break;
806 if (i > 0) memmove(str, str + i, len - i + 1);
807
808 len = strlen(str);
809 for (i = len - 1; i >= 0; i--)
810 if (str[i] != ' ') break;
811 str[i+1] = 0;
812
813 return str;
814}
815
816/* returns:
817 * 0 - EOF, end
818 * 1 - parsed ok
819 * -1 - failed to parse line
820 */
821int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
822{
823 char *var, *val, *tmp;
824 FILE *f = file;
825 int len, i;
826
827 tmp = fgets(line, lsize, f);
828 if (tmp == NULL) return 0;
829
830 if (line[0] == '[') return 0; // other section
831
832 // strip comments, linefeed, spaces..
833 len = strlen(line);
834 for (i = 0; i < len; i++)
835 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
836 mystrip(line);
837 len = strlen(line);
838 if (len <= 0) return -1;;
839
840 // get var and val
841 for (i = 0; i < len; i++)
842 if (line[i] == '=') break;
843 if (i >= len || strchr(&line[i+1], '=') != NULL) {
844 lprintf("config_readsect: can't parse: %s\n", line);
845 return -1;
846 }
847 line[i] = 0;
848 var = line;
849 val = &line[i+1];
850 mystrip(var);
851 mystrip(val);
852
853#ifndef _MSC_VER
854 if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
855 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
856 return -1;;
857 }
858#endif
859
860 *rvar = var;
861 *rval = val;
862 return 1;
863}
864