32x: drc: finish MAC, gen drc entry/exit (for statically alloced regs)
[libpicofe.git] / common / config.c
... / ...
CommitLineData
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
16static 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// always output DOS endlines
25#ifdef _WIN32
26#define NL "\n"
27#else
28#define NL "\r\n"
29#endif
30
31static int seek_sect(FILE *f, const char *section)
32{
33 char line[128], *tmp;
34 int len;
35
36 len = strlen(section);
37 // seek to the section needed
38 while (!feof(f))
39 {
40 tmp = fgets(line, sizeof(line), f);
41 if (tmp == NULL) break;
42
43 if (line[0] != '[') continue; // not section start
44 if (strncmp(line + 1, section, len) == 0 && line[len+1] == ']')
45 return 1; // found it
46 }
47
48 return 0;
49}
50
51
52static void custom_write(FILE *f, const menu_entry *me, int no_def)
53{
54 char str24[24];
55
56 switch (me->id)
57 {
58 /* TODO: this should be rm'd when PSP menu is converted */
59 case MA_OPT3_SCALE:
60 if (no_def && defaultConfig.scale == currentConfig.scale) return;
61 fprintf(f, "Scale factor = %.2f", currentConfig.scale);
62 break;
63 case MA_OPT3_HSCALE32:
64 if (no_def && defaultConfig.hscale32 == currentConfig.hscale32) return;
65 fprintf(f, "Hor. scale (for low res. games) = %.2f", currentConfig.hscale32);
66 break;
67 case MA_OPT3_HSCALE40:
68 if (no_def && defaultConfig.hscale40 == currentConfig.hscale40) return;
69 fprintf(f, "Hor. scale (for hi res. games) = %.2f", currentConfig.hscale40);
70 break;
71 case MA_OPT3_FILTERING:
72 if (no_def && defaultConfig.scaling == currentConfig.scaling) return;
73 fprintf(f, "Bilinear filtering = %i", currentConfig.scaling);
74 break;
75 case MA_OPT3_GAMMAA:
76 if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
77 fprintf(f, "Gamma adjustment = %i", currentConfig.gamma);
78 break;
79 case MA_OPT3_BLACKLVL:
80 if (no_def && defaultConfig.gamma2 == currentConfig.gamma2) return;
81 fprintf(f, "Black level = %i", currentConfig.gamma2);
82 break;
83 case MA_OPT3_VSYNC:
84 if (no_def && (defaultConfig.EmuOpt&0x12000) == (currentConfig.gamma2&0x12000)) return;
85 strcpy(str24, "never");
86 if (currentConfig.EmuOpt & 0x2000)
87 strcpy(str24, (currentConfig.EmuOpt & 0x10000) ? "sometimes" : "always");
88 fprintf(f, "Wait for vsync = %s", str24);
89 break;
90
91 default:
92 lprintf("unhandled custom_write: %i\n", me->id);
93 return;
94 }
95 fprintf(f, NL);
96}
97
98
99static void keys_write(FILE *fn, const char *bind_str, int dev_id, const int *binds, int no_defaults)
100{
101 char act[48];
102 int key_count, k, i;
103 const int *def_binds;
104
105 key_count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);
106 def_binds = in_get_dev_def_binds(dev_id);
107
108 for (k = 0; k < key_count; k++)
109 {
110 const char *name;
111 int t, mask;
112 act[0] = act[31] = 0;
113
114 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
115 if (binds[IN_BIND_OFFS(k, t)] != def_binds[IN_BIND_OFFS(k, t)])
116 break;
117
118 if (no_defaults && t == IN_BINDTYPE_COUNT)
119 continue; /* no change from defaults */
120
121 name = in_get_key_name(dev_id, k);
122
123 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
124 if (binds[IN_BIND_OFFS(k, t)] != 0 || def_binds[IN_BIND_OFFS(k, t)] == 0)
125 break;
126
127 if (t == IN_BINDTYPE_COUNT) {
128 /* key has default bind removed */
129 fprintf(fn, "%s %s =" NL, bind_str, name);
130 continue;
131 }
132
133 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
134 mask = me_ctrl_actions[i].mask;
135 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
136 strncpy(act, me_ctrl_actions[i].name, 31);
137 fprintf(fn, "%s %s = player1 %s" NL, bind_str, name, mystrip(act));
138 }
139 mask = me_ctrl_actions[i].mask << 16;
140 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
141 strncpy(act, me_ctrl_actions[i].name, 31);
142 fprintf(fn, "%s %s = player2 %s" NL, bind_str, name, mystrip(act));
143 }
144 }
145
146 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
147 mask = emuctrl_actions[i].mask;
148 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
149 strncpy(act, emuctrl_actions[i].name, 31);
150 fprintf(fn, "%s %s = %s" NL, bind_str, name, mystrip(act));
151 }
152 }
153 }
154}
155
156/* XXX: this should go to menu structures instead */
157static int default_var(const menu_entry *me)
158{
159 switch (me->id)
160 {
161 case MA_OPT2_ENABLE_YM2612:
162 case MA_OPT2_ENABLE_SN76496:
163 case MA_OPT2_ENABLE_Z80:
164 case MA_OPT_6BUTTON_PAD:
165 case MA_OPT_ACC_SPRITES:
166 case MA_OPT_ARM940_SOUND:
167 case MA_CDOPT_PCM:
168 case MA_CDOPT_CDDA:
169 case MA_CDOPT_SCALEROT_CHIP:
170 case MA_CDOPT_BETTER_SYNC:
171 case MA_CDOPT_SAVERAM:
172 case MA_OPT2_SVP_DYNAREC:
173 case MA_OPT2_NO_SPRITE_LIM:
174 case MA_OPT2_NO_IDLE_LOOPS:
175 return defaultConfig.s_PicoOpt;
176
177 case MA_OPT_SRAM_STATES:
178 case MA_OPT_SHOW_FPS:
179 case MA_OPT_ENABLE_SOUND:
180 case MA_OPT2_GZIP_STATES:
181 case MA_OPT2_SQUIDGEHACK:
182 case MA_OPT2_NO_LAST_ROM:
183 case MA_OPT2_RAMTIMINGS:
184 case MA_CDOPT_LEDS:
185 case MA_OPT2_A_SN_GAMMA:
186 case MA_OPT2_VSYNC:
187 case MA_OPT_INTERLACED:
188 case MA_OPT2_DBLBUFF:
189 case MA_OPT2_STATUS_LINE:
190 case MA_OPT2_NO_FRAME_LIMIT:
191 case MA_OPT_TEARING_FIX:
192 return defaultConfig.EmuOpt;
193
194 case MA_CTRL_TURBO_RATE: return defaultConfig.turbo_rate;
195 case MA_OPT_SCALING: return defaultConfig.scaling;
196 case MA_OPT_ROTATION: return defaultConfig.rotation;
197 case MA_OPT2_GAMMA: return defaultConfig.gamma;
198 case MA_OPT_FRAMESKIP: return defaultConfig.Frameskip;
199 case MA_OPT_CPU_CLOCKS: return defaultConfig.CPUclock;
200
201 case MA_OPT_SAVE_SLOT:
202 default:
203 return 0;
204 }
205}
206
207static int is_cust_val_default(const menu_entry *me)
208{
209 switch (me->id)
210 {
211 case MA_OPT_REGION:
212 return defaultConfig.s_PicoRegion == PicoRegionOverride &&
213 defaultConfig.s_PicoAutoRgnOrder == PicoAutoRgnOrder;
214 case MA_OPT_SOUND_QUALITY:
215 return defaultConfig.s_PsndRate == PsndRate &&
216 ((defaultConfig.s_PicoOpt ^ PicoOpt) & POPT_EN_STEREO) == 0;
217 case MA_OPT_CONFIRM_STATES:
218 return !((defaultConfig.EmuOpt ^ currentConfig.EmuOpt) &
219 (EOPT_CONFIRM_LOAD|EOPT_CONFIRM_SAVE)) == 0;
220 case MA_OPT_RENDERER:
221 return ((defaultConfig.s_PicoOpt ^ PicoOpt) & POPT_ALT_RENDERER) == 0 &&
222 ((defaultConfig.EmuOpt ^ currentConfig.EmuOpt) & EOPT_16BPP) == 0;
223 case MA_CDOPT_READAHEAD:
224 return defaultConfig.s_PicoCDBuffers == PicoCDBuffers;
225 default:break;
226 }
227
228 lprintf("is_cust_val_default: unhandled id %i\n", me->id);
229 return 0;
230}
231
232int config_writesect(const char *fname, const char *section)
233{
234 FILE *fo = NULL, *fn = NULL; // old and new
235 int no_defaults = 0; // avoid saving defaults
236 menu_entry *me;
237 int t, tlen, ret;
238 char line[128], *tmp;
239
240 if (section != NULL)
241 {
242 no_defaults = 1;
243
244 fo = fopen(fname, "r");
245 if (fo == NULL) {
246 fn = fopen(fname, "w");
247 goto write;
248 }
249
250 ret = seek_sect(fo, section);
251 if (!ret) {
252 // sect not found, we can simply append
253 fclose(fo); fo = NULL;
254 fn = fopen(fname, "a");
255 goto write;
256 }
257
258 // use 2 files..
259 fclose(fo);
260 rename(fname, "tmp.cfg");
261 fo = fopen("tmp.cfg", "r");
262 fn = fopen(fname, "w");
263 if (fo == NULL || fn == NULL) goto write;
264
265 // copy everything until sect
266 tlen = strlen(section);
267 while (!feof(fo))
268 {
269 tmp = fgets(line, sizeof(line), fo);
270 if (tmp == NULL) break;
271
272 if (line[0] == '[' && strncmp(line + 1, section, tlen) == 0 && line[tlen+1] == ']')
273 break;
274 fputs(line, fn);
275 }
276
277 // now skip to next sect
278 while (!feof(fo))
279 {
280 tmp = fgets(line, sizeof(line), fo);
281 if (tmp == NULL) break;
282 if (line[0] == '[') {
283 fseek(fo, -strlen(line), SEEK_CUR);
284 break;
285 }
286 }
287 if (feof(fo))
288 {
289 fclose(fo); fo = NULL;
290 remove("tmp.cfg");
291 }
292 }
293 else
294 {
295 fn = fopen(fname, "w");
296 }
297
298write:
299 if (fn == NULL) {
300 if (fo) fclose(fo);
301 return -1;
302 }
303 if (section != NULL)
304 fprintf(fn, "[%s]" NL, section);
305
306 me = me_list_get_first();
307 while (me != NULL)
308 {
309 int dummy;
310 if (!me->need_to_save)
311 goto next;
312 if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
313 if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask))
314 fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
315 } else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
316 if (!no_defaults || (*(int *)me->var ^ default_var(me)))
317 fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
318 } else if (me->name != NULL && me->generate_name != NULL) {
319 if (!no_defaults || !is_cust_val_default(me)) {
320 strncpy(line, me->generate_name(0, &dummy), sizeof(line));
321 line[sizeof(line) - 1] = 0;
322 mystrip(line);
323 fprintf(fn, "%s = %s" NL, me->name, line);
324 }
325 } else
326 custom_write(fn, me, no_defaults);
327next:
328 me = me_list_get_next();
329 }
330
331 /* input: save device names */
332 for (t = 0; t < IN_MAX_DEVS; t++)
333 {
334 const int *binds = in_get_dev_binds(t);
335 const char *name = in_get_dev_name(t, 0, 0);
336 if (binds == NULL || name == NULL)
337 continue;
338
339 fprintf(fn, "input%d = %s" NL, t, name);
340 }
341
342 /* input: save binds */
343 for (t = 0; t < IN_MAX_DEVS; t++)
344 {
345 const int *binds = in_get_dev_binds(t);
346 const char *name = in_get_dev_name(t, 0, 0);
347 char strbind[16];
348 int count;
349
350 if (binds == NULL || name == NULL)
351 continue;
352
353 sprintf(strbind, "bind%d", t);
354 if (t == 0) strbind[4] = 0;
355
356 count = in_get_dev_info(t, IN_INFO_BIND_COUNT);
357 keys_write(fn, strbind, t, binds, no_defaults);
358 }
359
360#ifndef PSP
361 if (section == NULL)
362 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
363#endif
364
365 fprintf(fn, NL);
366
367 if (fo != NULL)
368 {
369 // copy whatever is left
370 while (!feof(fo))
371 {
372 tmp = fgets(line, sizeof(line), fo);
373 if (tmp == NULL) break;
374
375 fputs(line, fn);
376 }
377 fclose(fo);
378 remove("tmp.cfg");
379 }
380
381 fclose(fn);
382 return 0;
383}
384
385
386int config_writelrom(const char *fname)
387{
388 char line[128], *tmp, *optr = NULL;
389 char *old_data = NULL;
390 int size;
391 FILE *f;
392
393 if (strlen(rom_fname_loaded) == 0) return -1;
394
395 f = fopen(fname, "r");
396 if (f != NULL)
397 {
398 fseek(f, 0, SEEK_END);
399 size = ftell(f);
400 fseek(f, 0, SEEK_SET);
401 old_data = malloc(size + size/8);
402 if (old_data != NULL)
403 {
404 optr = old_data;
405 while (!feof(f))
406 {
407 tmp = fgets(line, sizeof(line), f);
408 if (tmp == NULL) break;
409 mystrip(line);
410 if (strncasecmp(line, "LastUsedROM", 11) == 0)
411 continue;
412 sprintf(optr, "%s", line);
413 optr += strlen(optr);
414 }
415 }
416 fclose(f);
417 }
418
419 f = fopen(fname, "w");
420 if (f == NULL) return -1;
421
422 if (old_data != NULL) {
423 fwrite(old_data, 1, optr - old_data, f);
424 free(old_data);
425 }
426 fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
427 fclose(f);
428 return 0;
429}
430
431/* --------------------------------------------------------------------------*/
432
433int config_readlrom(const char *fname)
434{
435 char line[128], *tmp;
436 int i, len, ret = -1;
437 FILE *f;
438
439 f = fopen(fname, "r");
440 if (f == NULL) return -1;
441
442 // seek to the section needed
443 while (!feof(f))
444 {
445 tmp = fgets(line, sizeof(line), f);
446 if (tmp == NULL) break;
447
448 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
449 len = strlen(line);
450 for (i = 0; i < len; i++)
451 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
452 tmp = strchr(line, '=');
453 if (tmp == NULL) break;
454 tmp++;
455 mystrip(tmp);
456
457 len = sizeof(rom_fname_loaded);
458 strncpy(rom_fname_loaded, tmp, len);
459 rom_fname_loaded[len-1] = 0;
460 ret = 0;
461 break;
462 }
463 fclose(f);
464 return ret;
465}
466
467
468static int custom_read(menu_entry *me, const char *var, const char *val)
469{
470 char *tmp;
471 int tmpi;
472
473 switch (me->id)
474 {
475 case MA_OPT_RENDERER:
476 if (strcasecmp(var, "Renderer") != 0) return 0;
477 if (strcasecmp(val, "8bit fast") == 0 || strcasecmp(val, "fast") == 0) {
478 PicoOpt |= POPT_ALT_RENDERER;
479 }
480 else if (strcasecmp(val, "16bit accurate") == 0 || strcasecmp(val, "accurate") == 0) {
481 PicoOpt &= ~POPT_ALT_RENDERER;
482 currentConfig.EmuOpt |= 0x80;
483 }
484 else if (strcasecmp(val, "8bit accurate") == 0) {
485 PicoOpt &= ~POPT_ALT_RENDERER;
486 currentConfig.EmuOpt &= ~0x80;
487 }
488 else
489 return 0;
490 return 1;
491
492 case MA_OPT_SCALING:
493#ifdef __GP2X__
494 if (strcasecmp(var, "Scaling") != 0) return 0;
495 if (strcasecmp(val, "OFF") == 0) {
496 currentConfig.scaling = EOPT_SCALE_NONE;
497 } else if (strcasecmp(val, "hw horizontal") == 0) {
498 currentConfig.scaling = EOPT_SCALE_HW_H;
499 } else if (strcasecmp(val, "hw horiz. + vert.") == 0) {
500 currentConfig.scaling = EOPT_SCALE_HW_HV;
501 } else if (strcasecmp(val, "sw horizontal") == 0) {
502 currentConfig.scaling = EOPT_SCALE_SW_H;
503 } else
504 currentConfig.scaling = atoi(val);
505 return 1;
506#else
507 return 0;
508#endif
509
510 case MA_OPT_FRAMESKIP:
511 if (strcasecmp(var, "Frameskip") != 0) return 0;
512 if (strcasecmp(val, "Auto") == 0)
513 currentConfig.Frameskip = -1;
514 else currentConfig.Frameskip = atoi(val);
515 return 1;
516
517 case MA_OPT_SOUND_QUALITY:
518 if (strcasecmp(var, "Sound Quality") != 0) return 0;
519 PsndRate = strtoul(val, &tmp, 10);
520 if (PsndRate < 8000 || PsndRate > 44100)
521 PsndRate = 22050;
522 if (*tmp == 'H' || *tmp == 'h') tmp++;
523 if (*tmp == 'Z' || *tmp == 'z') tmp++;
524 while (*tmp == ' ') tmp++;
525 if (strcasecmp(tmp, "stereo") == 0) {
526 PicoOpt |= POPT_EN_STEREO;
527 } else if (strcasecmp(tmp, "mono") == 0) {
528 PicoOpt &= ~POPT_EN_STEREO;
529 } else
530 return 0;
531 return 1;
532
533 case MA_OPT_REGION:
534 if (strcasecmp(var, "Region") != 0) return 0;
535 if (strncasecmp(val, "Auto: ", 6) == 0)
536 {
537 const char *p = val + 5, *end = val + strlen(val);
538 int i;
539 PicoRegionOverride = PicoAutoRgnOrder = 0;
540 for (i = 0; p < end && i < 3; i++)
541 {
542 while (*p == ' ') p++;
543 if (p[0] == 'J' && p[1] == 'P') {
544 PicoAutoRgnOrder |= 1 << (i*4);
545 } else if (p[0] == 'U' && p[1] == 'S') {
546 PicoAutoRgnOrder |= 4 << (i*4);
547 } else if (p[0] == 'E' && p[1] == 'U') {
548 PicoAutoRgnOrder |= 8 << (i*4);
549 }
550 while (*p != ' ' && *p != 0) p++;
551 if (*p == 0) break;
552 }
553 }
554 else if (strcasecmp(val, "Auto") == 0) {
555 PicoRegionOverride = 0;
556 } else if (strcasecmp(val, "Japan NTSC") == 0) {
557 PicoRegionOverride = 1;
558 } else if (strcasecmp(val, "Japan PAL") == 0) {
559 PicoRegionOverride = 2;
560 } else if (strcasecmp(val, "USA") == 0) {
561 PicoRegionOverride = 4;
562 } else if (strcasecmp(val, "Europe") == 0) {
563 PicoRegionOverride = 8;
564 } else
565 return 0;
566 return 1;
567
568 case MA_OPT_CONFIRM_STATES:
569 if (strcasecmp(var, "Confirm savestate") != 0) return 0;
570 if (strcasecmp(val, "OFF") == 0) {
571 currentConfig.EmuOpt &= ~(5<<9);
572 } else if (strcasecmp(val, "writes") == 0) {
573 currentConfig.EmuOpt &= ~(5<<9);
574 currentConfig.EmuOpt |= 1<<9;
575 } else if (strcasecmp(val, "loads") == 0) {
576 currentConfig.EmuOpt &= ~(5<<9);
577 currentConfig.EmuOpt |= 4<<9;
578 } else if (strcasecmp(val, "both") == 0) {
579 currentConfig.EmuOpt &= ~(5<<9);
580 currentConfig.EmuOpt |= 5<<9;
581 } else
582 return 0;
583 return 1;
584
585 case MA_OPT2_GAMMA:
586 if (strcasecmp(var, "Gamma correction") != 0) return 0;
587 currentConfig.gamma = (int) (atof(val) * 100.0);
588 return 1;
589
590 case MA_OPT2_SQUIDGEHACK:
591 if (strcasecmp(var, "Squidgehack") != 0) return 0;
592 tmpi = atoi(val);
593 if (tmpi) *(int *)me->var |= me->mask;
594 else *(int *)me->var &= ~me->mask;
595 return 1;
596
597 case MA_CDOPT_READAHEAD:
598 if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
599 PicoCDBuffers = atoi(val) / 2;
600 return 1;
601
602 /* PSP */
603 case MA_OPT3_SCALE:
604 if (strcasecmp(var, "Scale factor") != 0) return 0;
605 currentConfig.scale = atof(val);
606 return 1;
607 case MA_OPT3_HSCALE32:
608 if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
609 currentConfig.hscale32 = atof(val);
610 return 1;
611 case MA_OPT3_HSCALE40:
612 if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
613 currentConfig.hscale40 = atof(val);
614 return 1;
615 case MA_OPT3_FILTERING:
616 if (strcasecmp(var, "Bilinear filtering") != 0) return 0;
617 currentConfig.scaling = atoi(val);
618 return 1;
619 case MA_OPT3_GAMMAA:
620 if (strcasecmp(var, "Gamma adjustment") != 0) return 0;
621 currentConfig.gamma = atoi(val);
622 return 1;
623 case MA_OPT3_BLACKLVL:
624 if (strcasecmp(var, "Black level") != 0) return 0;
625 currentConfig.gamma2 = atoi(val);
626 return 1;
627 case MA_OPT3_VSYNC:
628 if (strcasecmp(var, "Wait for vsync") != 0) return 0;
629 if (strcasecmp(val, "never") == 0) {
630 currentConfig.EmuOpt &= ~0x12000;
631 } else if (strcasecmp(val, "sometimes") == 0) {
632 currentConfig.EmuOpt |= 0x12000;
633 } else if (strcasecmp(val, "always") == 0) {
634 currentConfig.EmuOpt &= ~0x12000;
635 currentConfig.EmuOpt |= 0x02000;
636 } else
637 return 0;
638 return 1;
639
640 default:
641 lprintf("unhandled custom_read: %i\n", me->id);
642 return 0;
643 }
644}
645
646
647static unsigned int keys_encountered = 0;
648
649static int parse_bind_val(const char *val, int *type)
650{
651 int i;
652
653 *type = IN_BINDTYPE_NONE;
654 if (val[0] == 0)
655 return 0;
656
657 if (strncasecmp(val, "player", 6) == 0)
658 {
659 int player, shift = 0;
660 player = atoi(val + 6) - 1;
661
662 if (player > 1)
663 return -1;
664 if (player == 1)
665 shift = 16;
666
667 *type = IN_BINDTYPE_PLAYER12;
668 for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
669 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
670 return me_ctrl_actions[i].mask << shift;
671 }
672 }
673 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
674 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
675 *type = IN_BINDTYPE_EMU;
676 return emuctrl_actions[i].mask;
677 }
678 }
679
680 return -1;
681}
682
683static void keys_parse(const char *key, const char *val, int dev_id)
684{
685 int acts, type;
686
687 acts = parse_bind_val(val, &type);
688 if (acts == -1) {
689 lprintf("config: unhandled action \"%s\"\n", val);
690 return;
691 }
692
693 in_config_bind_key(dev_id, key, acts, type);
694}
695
696static int get_numvar_num(const char *var)
697{
698 char *p = NULL;
699 int num;
700
701 if (var[0] == ' ')
702 return 0;
703
704 num = strtoul(var, &p, 10);
705 if (*p == 0 || *p == ' ')
706 return num;
707
708 return -1;
709}
710
711/* map dev number in confing to input dev number */
712static unsigned char input_dev_map[IN_MAX_DEVS];
713
714static void parse(const char *var, const char *val)
715{
716 menu_entry *me;
717 int tmp, ret = 0;
718
719 if (strcasecmp(var, "LastUsedROM") == 0)
720 return; /* handled elsewhere */
721
722 if (strcasecmp(var, "Sound Volume") == 0) {
723 currentConfig.volume = atoi(val);
724 return;
725 }
726
727 /* input: device name */
728 if (strncasecmp(var, "input", 5) == 0) {
729 int num = get_numvar_num(var + 5);
730 if (num >= 0 && num < IN_MAX_DEVS)
731 input_dev_map[num] = in_config_parse_dev(val);
732 else
733 lprintf("config: failed to parse: %s\n", var);
734 return;
735 }
736
737 // key binds
738 if (strncasecmp(var, "bind", 4) == 0) {
739 const char *p = var + 4;
740 int num = get_numvar_num(p);
741 if (num < 0 || num >= IN_MAX_DEVS) {
742 lprintf("config: failed to parse: %s\n", var);
743 return;
744 }
745
746 num = input_dev_map[num];
747 if (num < 0 || num >= IN_MAX_DEVS) {
748 lprintf("config: invalid device id: %s\n", var);
749 return;
750 }
751
752 while (*p && *p != ' ') p++;
753 while (*p && *p == ' ') p++;
754 keys_parse(p, val, num);
755 return;
756 }
757
758 me = me_list_get_first();
759 while (me != NULL && ret == 0)
760 {
761 if (!me->need_to_save)
762 goto next;
763 if (me->name != NULL && me->name[0] != 0) {
764 if (strcasecmp(var, me->name) != 0)
765 goto next; /* surely not this one */
766 if (me->beh == MB_OPT_ONOFF) {
767 tmp = atoi(val);
768 if (tmp) *(int *)me->var |= me->mask;
769 else *(int *)me->var &= ~me->mask;
770 return;
771 } else if (me->beh == MB_OPT_RANGE) {
772 tmp = atoi(val);
773 if (tmp < me->min) tmp = me->min;
774 if (tmp > me->max) tmp = me->max;
775 *(int *)me->var = tmp;
776 return;
777 }
778 }
779 ret = custom_read(me, var, val);
780next:
781 me = me_list_get_next();
782 }
783 if (!ret) lprintf("config_readsect: unhandled var: \"%s\"\n", var);
784}
785
786
787int config_havesect(const char *fname, const char *section)
788{
789 FILE *f;
790 int ret;
791
792 f = fopen(fname, "r");
793 if (f == NULL) return 0;
794
795 ret = seek_sect(f, section);
796 fclose(f);
797 return ret;
798}
799
800int config_readsect(const char *fname, const char *section)
801{
802 char line[128], *var, *val;
803 FILE *f;
804 int ret;
805
806 f = fopen(fname, "r");
807 if (f == NULL) return -1;
808
809 if (section != NULL)
810 {
811 ret = seek_sect(f, section);
812 if (!ret) {
813 lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
814 fclose(f);
815 return -1;
816 }
817 }
818
819 keys_encountered = 0;
820 memset(input_dev_map, 0xff, sizeof(input_dev_map));
821
822 in_config_start();
823 while (!feof(f))
824 {
825 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
826 if (ret == 0) break;
827 if (ret == -1) continue;
828
829 parse(var, val);
830 }
831 in_config_end();
832
833 fclose(f);
834 return 0;
835}
836
837#endif // _MSC_VER
838
839static char *mystrip(char *str)
840{
841 int i, len;
842
843 len = strlen(str);
844 for (i = 0; i < len; i++)
845 if (str[i] != ' ') break;
846 if (i > 0) memmove(str, str + i, len - i + 1);
847
848 len = strlen(str);
849 for (i = len - 1; i >= 0; i--)
850 if (str[i] != ' ') break;
851 str[i+1] = 0;
852
853 return str;
854}
855
856/* returns:
857 * 0 - EOF, end
858 * 1 - parsed ok
859 * -1 - failed to parse line
860 */
861int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
862{
863 char *var, *val, *tmp;
864 FILE *f = file;
865 int len, i;
866
867 tmp = fgets(line, lsize, f);
868 if (tmp == NULL) return 0;
869
870 if (line[0] == '[') return 0; // other section
871
872 // strip comments, linefeed, spaces..
873 len = strlen(line);
874 for (i = 0; i < len; i++)
875 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
876 mystrip(line);
877 len = strlen(line);
878 if (len <= 0) return -1;;
879
880 // get var and val
881 for (i = 0; i < len; i++)
882 if (line[i] == '=') break;
883 if (i >= len || strchr(&line[i+1], '=') != NULL) {
884 lprintf("config_readsect: can't parse: %s\n", line);
885 return -1;
886 }
887 line[i] = 0;
888 var = line;
889 val = &line[i+1];
890 mystrip(var);
891 mystrip(val);
892
893#ifndef _MSC_VER
894 if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
895 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
896 return -1;;
897 }
898#endif
899
900 *rvar = var;
901 *rval = val;
902 return 1;
903}
904