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