config_file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * Human-readable config file management for PicoDrive
  3. * (C) notaz, 2008-2010
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #ifdef __EPOC32__
  12. #include <unistd.h>
  13. #endif
  14. #include "../libpicofe/input.h"
  15. #include "../libpicofe/plat.h"
  16. #include "../libpicofe/lprintf.h"
  17. #include "config_file.h"
  18. #ifdef USE_LIBRETRO_VFS
  19. #include "file_stream_transforms.h"
  20. #endif
  21. static char *mystrip(char *str);
  22. #ifndef _MSC_VER
  23. #include "menu_pico.h"
  24. #include "emu.h"
  25. #include <pico/pico.h>
  26. // always output DOS endlines
  27. #ifdef _WIN32
  28. #define NL "\n"
  29. #else
  30. #define NL "\r\n"
  31. #endif
  32. static int seek_sect(FILE *f, const char *section)
  33. {
  34. char line[640], *tmp;
  35. int len;
  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. if (line[0] != '[') continue; // not section start
  43. if (strncmp(line + 1, section, len) == 0 && line[len+1] == ']')
  44. return 1; // found it
  45. }
  46. return 0;
  47. }
  48. static void keys_write(FILE *fn, int dev_id, const int *binds)
  49. {
  50. char act[48];
  51. int key_count = 0, k, i;
  52. in_get_config(dev_id, IN_CFG_BIND_COUNT, &key_count);
  53. for (k = 0; k < key_count; k++)
  54. {
  55. const char *name;
  56. int mask;
  57. act[0] = act[31] = 0;
  58. name = in_get_key_name(dev_id, k);
  59. for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
  60. mask = me_ctrl_actions[i].mask;
  61. if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
  62. strncpy(act, me_ctrl_actions[i].name, 31);
  63. fprintf(fn, "bind %s = player1 %s" NL, name, mystrip(act));
  64. }
  65. mask = me_ctrl_actions[i].mask << 16;
  66. if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
  67. strncpy(act, me_ctrl_actions[i].name, 31);
  68. fprintf(fn, "bind %s = player2 %s" NL, name, mystrip(act));
  69. }
  70. }
  71. for (i = 0; emuctrl_actions[i].name != NULL; i++) {
  72. mask = emuctrl_actions[i].mask;
  73. if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
  74. strncpy(act, emuctrl_actions[i].name, 31);
  75. fprintf(fn, "bind %s = %s" NL, name, mystrip(act));
  76. }
  77. }
  78. }
  79. }
  80. int config_write(const char *fname)
  81. {
  82. FILE *fn = NULL;
  83. menu_entry *me;
  84. int t;
  85. char line[640];
  86. fn = fopen(fname, "w");
  87. if (fn == NULL)
  88. return -1;
  89. for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
  90. {
  91. int dummy;
  92. if (!me->need_to_save || !me->enabled)
  93. continue;
  94. if (me->name == NULL || me->name[0] == 0)
  95. continue;
  96. if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
  97. fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
  98. }
  99. else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
  100. fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
  101. }
  102. else if (me->beh == MB_OPT_ENUM && me->data != NULL) {
  103. const char **names = (const char **)me->data;
  104. for (t = 0; names[t] != NULL; t++) {
  105. if (*(int *)me->var == t) {
  106. strncpy(line, names[t], sizeof(line)-1);
  107. line[sizeof(line)-1] = '\0';
  108. goto write_line;
  109. }
  110. }
  111. }
  112. else if (me->generate_name != NULL) {
  113. strncpy(line, me->generate_name(me->id, &dummy), sizeof(line)-1);
  114. line[sizeof(line)-1] = '\0';
  115. goto write_line;
  116. }
  117. else
  118. lprintf("config: unhandled write: %i\n", me->id);
  119. continue;
  120. write_line:
  121. line[sizeof(line) - 1] = 0;
  122. mystrip(line);
  123. fprintf(fn, "%s = %s" NL, me->name, line);
  124. }
  125. /* input: save binds */
  126. for (t = 0; t < IN_MAX_DEVS; t++)
  127. {
  128. const int *binds = in_get_dev_binds(t);
  129. const char *name = in_get_dev_name(t, 0, 0);
  130. int count = 0;
  131. if (binds == NULL || name == NULL)
  132. continue;
  133. fprintf(fn, "binddev = %s" NL, name);
  134. in_get_config(t, IN_CFG_BIND_COUNT, &count);
  135. keys_write(fn, t, binds);
  136. }
  137. fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
  138. fprintf(fn, NL);
  139. fclose(fn);
  140. return 0;
  141. }
  142. int config_writelrom(const char *fname)
  143. {
  144. char line[640], *tmp, *optr = NULL;
  145. char *old_data = NULL;
  146. int size;
  147. FILE *f;
  148. if (strlen(rom_fname_loaded) == 0) return -1;
  149. f = fopen(fname, "r");
  150. if (f != NULL)
  151. {
  152. fseek(f, 0, SEEK_END);
  153. size = ftell(f);
  154. fseek(f, 0, SEEK_SET);
  155. old_data = malloc(size + size/8);
  156. if (old_data != NULL)
  157. {
  158. optr = old_data;
  159. while (!feof(f))
  160. {
  161. tmp = fgets(line, sizeof(line), f);
  162. if (tmp == NULL) break;
  163. mystrip(line);
  164. if (strncasecmp(line, "LastUsedROM", 11) == 0)
  165. continue;
  166. sprintf(optr, "%s", line);
  167. optr += strlen(optr);
  168. }
  169. }
  170. fclose(f);
  171. }
  172. f = fopen(fname, "w");
  173. if (f == NULL) return -1;
  174. if (old_data != NULL) {
  175. fwrite(old_data, 1, optr - old_data, f);
  176. free(old_data);
  177. }
  178. fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
  179. fclose(f);
  180. return 0;
  181. }
  182. /* --------------------------------------------------------------------------*/
  183. int config_readlrom(const char *fname)
  184. {
  185. char line[640], *tmp;
  186. int i, len, ret = -1;
  187. FILE *f;
  188. f = fopen(fname, "r");
  189. if (f == NULL) return -1;
  190. // seek to the section needed
  191. while (!feof(f))
  192. {
  193. tmp = fgets(line, sizeof(line), f);
  194. if (tmp == NULL) break;
  195. if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
  196. len = strlen(line);
  197. for (i = 0; i < len; i++)
  198. if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
  199. tmp = strchr(line, '=');
  200. if (tmp == NULL) break;
  201. tmp++;
  202. mystrip(tmp);
  203. len = sizeof(rom_fname_loaded)-1;
  204. strncpy(rom_fname_loaded, tmp, len);
  205. rom_fname_loaded[len] = 0;
  206. ret = 0;
  207. break;
  208. }
  209. fclose(f);
  210. return ret;
  211. }
  212. static int custom_read(menu_entry *me, const char *var, const char *val)
  213. {
  214. char *tmp;
  215. switch (me->id)
  216. {
  217. case MA_OPT_FRAMESKIP:
  218. if (strcasecmp(var, "Frameskip") != 0) return 0;
  219. if (strcasecmp(val, "Auto") == 0)
  220. currentConfig.Frameskip = -1;
  221. else currentConfig.Frameskip = atoi(val);
  222. return 1;
  223. case MA_OPT_SOUND_QUALITY:
  224. if (strcasecmp(var, "Sound Quality") != 0) return 0;
  225. PicoIn.sndRate = strtoul(val, &tmp, 10);
  226. if (PicoIn.sndRate < 8000 || PicoIn.sndRate > 44100)
  227. PicoIn.sndRate = 22050;
  228. if (*tmp == 'H' || *tmp == 'h') tmp++;
  229. if (*tmp == 'Z' || *tmp == 'z') tmp++;
  230. while (*tmp == ' ') tmp++;
  231. if (strcasecmp(tmp, "stereo") == 0) {
  232. PicoIn.opt |= POPT_EN_STEREO;
  233. } else if (strcasecmp(tmp, "mono") == 0) {
  234. PicoIn.opt &= ~POPT_EN_STEREO;
  235. } else
  236. return 0;
  237. return 1;
  238. case MA_OPT_REGION:
  239. if (strcasecmp(var, "Region") != 0) return 0;
  240. if (strncasecmp(val, "Auto: ", 6) == 0)
  241. {
  242. const char *p = val + 5, *end = val + strlen(val);
  243. int i;
  244. PicoIn.regionOverride = PicoIn.autoRgnOrder = 0;
  245. for (i = 0; p < end && i < 3; i++)
  246. {
  247. while (*p == ' ') p++;
  248. if (p[0] == 'J' && p[1] == 'P') {
  249. PicoIn.autoRgnOrder |= 1 << (i*4);
  250. } else if (p[0] == 'U' && p[1] == 'S') {
  251. PicoIn.autoRgnOrder |= 4 << (i*4);
  252. } else if (p[0] == 'E' && p[1] == 'U') {
  253. PicoIn.autoRgnOrder |= 8 << (i*4);
  254. }
  255. while (*p != ' ' && *p != 0) p++;
  256. if (*p == 0) break;
  257. }
  258. }
  259. else if (strcasecmp(val, "Auto") == 0) {
  260. PicoIn.regionOverride = 0;
  261. } else if (strcasecmp(val, "Japan NTSC") == 0) {
  262. PicoIn.regionOverride = 1;
  263. } else if (strcasecmp(val, "Japan PAL") == 0) {
  264. PicoIn.regionOverride = 2;
  265. } else if (strcasecmp(val, "USA") == 0) {
  266. PicoIn.regionOverride = 4;
  267. } else if (strcasecmp(val, "Europe") == 0) {
  268. PicoIn.regionOverride = 8;
  269. } else
  270. return 0;
  271. return 1;
  272. case MA_32XOPT_MSH2_CYCLES:
  273. currentConfig.msh2_khz = atoi(val);
  274. Pico32xSetClocks(currentConfig.msh2_khz * 1000, 0);
  275. return 1;
  276. case MA_32XOPT_SSH2_CYCLES:
  277. currentConfig.ssh2_khz = atoi(val);
  278. Pico32xSetClocks(0, currentConfig.ssh2_khz * 1000);
  279. return 1;
  280. case MA_OPT2_GAMMA:
  281. currentConfig.gamma = atoi(val);
  282. return 1;
  283. case MA_OPT2_MAX_FRAMESKIP:
  284. currentConfig.max_skip = atoi(val);
  285. return 1;
  286. /* PSP */
  287. case MA_OPT3_SCALE:
  288. if (strcasecmp(var, "Scale factor") != 0) return 0;
  289. currentConfig.scale = atof(val);
  290. return 1;
  291. case MA_OPT3_HSCALE32:
  292. if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
  293. currentConfig.hscale32 = atof(val);
  294. return 1;
  295. case MA_OPT3_HSCALE40:
  296. if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
  297. currentConfig.hscale40 = atof(val);
  298. return 1;
  299. case MA_OPT3_VSYNC:
  300. // XXX: use enum
  301. if (strcasecmp(var, "Wait for vsync") != 0) return 0;
  302. if (strcasecmp(val, "never") == 0) {
  303. currentConfig.EmuOpt &= ~0x12000;
  304. } else if (strcasecmp(val, "sometimes") == 0) {
  305. currentConfig.EmuOpt |= 0x12000;
  306. } else if (strcasecmp(val, "always") == 0) {
  307. currentConfig.EmuOpt &= ~0x12000;
  308. currentConfig.EmuOpt |= 0x02000;
  309. } else
  310. return 0;
  311. return 1;
  312. default:
  313. lprintf("unhandled custom_read %i: %s\n", me->id, var);
  314. return 0;
  315. }
  316. }
  317. static int parse_bind_val(const char *val, int *type)
  318. {
  319. int i;
  320. *type = IN_BINDTYPE_NONE;
  321. if (val[0] == 0)
  322. return 0;
  323. if (strncasecmp(val, "player", 6) == 0)
  324. {
  325. int player, shift = 0;
  326. player = atoi(val + 6) - 1;
  327. if (player > 1)
  328. return -1;
  329. if (player == 1)
  330. shift = 16;
  331. *type = IN_BINDTYPE_PLAYER12;
  332. for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
  333. if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
  334. return me_ctrl_actions[i].mask << shift;
  335. }
  336. }
  337. for (i = 0; emuctrl_actions[i].name != NULL; i++) {
  338. if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
  339. *type = IN_BINDTYPE_EMU;
  340. return emuctrl_actions[i].mask;
  341. }
  342. }
  343. return -1;
  344. }
  345. static void keys_parse_all(FILE *f)
  346. {
  347. char line[640], *var, *val;
  348. int dev_id = -1;
  349. int acts, type;
  350. int ret;
  351. while (!feof(f))
  352. {
  353. ret = config_get_var_val(f, line, sizeof(line), &var, &val);
  354. if (ret == 0) break;
  355. if (ret == -1) continue;
  356. if (strcasecmp(var, "binddev") == 0) {
  357. dev_id = in_config_parse_dev(val);
  358. if (dev_id < 0) {
  359. printf("input: can't handle dev: %s\n", val);
  360. continue;
  361. }
  362. in_unbind_all(dev_id, -1, -1);
  363. continue;
  364. }
  365. if (dev_id < 0 || strncasecmp(var, "bind ", 5) != 0)
  366. continue;
  367. acts = parse_bind_val(val, &type);
  368. if (acts == -1) {
  369. lprintf("config: unhandled action \"%s\"\n", val);
  370. continue;
  371. }
  372. mystrip(var + 5);
  373. in_config_bind_key(dev_id, var + 5, acts, type);
  374. }
  375. in_clean_binds();
  376. }
  377. static void parse(const char *var, const char *val, int *keys_encountered)
  378. {
  379. menu_entry *me;
  380. int tmp;
  381. if (strcasecmp(var, "LastUsedROM") == 0)
  382. return; /* handled elsewhere */
  383. if (strncasecmp(var, "bind", 4) == 0) {
  384. *keys_encountered = 1;
  385. return; /* handled elsewhere */
  386. }
  387. if (strcasecmp(var, "Sound Volume") == 0) {
  388. currentConfig.volume = atoi(val);
  389. return;
  390. }
  391. for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
  392. {
  393. char *p;
  394. if (!me->need_to_save)
  395. continue;
  396. if (me->name == NULL || strcasecmp(var, me->name) != 0)
  397. continue;
  398. if (me->beh == MB_OPT_ONOFF) {
  399. tmp = strtol(val, &p, 0);
  400. if (*p != 0)
  401. goto bad_val;
  402. if (tmp) *(int *)me->var |= me->mask;
  403. else *(int *)me->var &= ~me->mask;
  404. return;
  405. }
  406. else if (me->beh == MB_OPT_RANGE) {
  407. tmp = strtol(val, &p, 0);
  408. if (*p != 0)
  409. goto bad_val;
  410. if (tmp < me->min) tmp = me->min;
  411. if (tmp > me->max) tmp = me->max;
  412. *(int *)me->var = tmp;
  413. return;
  414. }
  415. else if (me->beh == MB_OPT_ENUM) {
  416. const char **names, *p1;
  417. int i;
  418. names = (const char **)me->data;
  419. if (names == NULL)
  420. goto bad_val;
  421. for (i = 0; names[i] != NULL; i++) {
  422. for (p1 = names[i]; *p1 == ' '; p1++)
  423. ;
  424. if (strcasecmp(p1, val) == 0) {
  425. *(int *)me->var = i;
  426. return;
  427. }
  428. }
  429. goto bad_val;
  430. }
  431. else if (custom_read(me, var, val))
  432. return;
  433. }
  434. lprintf("config_readsect: unhandled var: \"%s\"\n", var);
  435. return;
  436. bad_val:
  437. lprintf("config_readsect: unhandled val for \"%s\": \"%s\"\n", var, val);
  438. }
  439. int config_readsect(const char *fname, const char *section)
  440. {
  441. char line[640], *var, *val;
  442. int keys_encountered = 0;
  443. FILE *f;
  444. int ret;
  445. f = fopen(fname, "r");
  446. if (f == NULL) return -1;
  447. if (section != NULL)
  448. {
  449. /* for game_def.cfg only */
  450. ret = seek_sect(f, section);
  451. if (!ret) {
  452. lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
  453. fclose(f);
  454. return -1;
  455. }
  456. }
  457. emu_set_defconfig();
  458. while (!feof(f))
  459. {
  460. ret = config_get_var_val(f, line, sizeof(line), &var, &val);
  461. if (ret == 0) break;
  462. if (ret == -1) continue;
  463. parse(var, val, &keys_encountered);
  464. }
  465. if (keys_encountered) {
  466. rewind(f);
  467. keys_parse_all(f);
  468. }
  469. fclose(f);
  470. lprintf("config_readsect: loaded from %s", fname);
  471. if (section != NULL)
  472. lprintf(" [%s]", section);
  473. printf("\n");
  474. return 0;
  475. }
  476. #endif // _MSC_VER
  477. static char *mystrip(char *str)
  478. {
  479. int i, len;
  480. len = strlen(str);
  481. for (i = 0; i < len; i++)
  482. if (str[i] != ' ') break;
  483. if (i > 0) memmove(str, str + i, len - i + 1);
  484. len = strlen(str);
  485. for (i = len - 1; i >= 0; i--)
  486. if (str[i] != ' ') break;
  487. str[i+1] = 0;
  488. return str;
  489. }
  490. /* returns:
  491. * 0 - EOF, end
  492. * 1 - parsed ok
  493. * -1 - failed to parse line
  494. */
  495. int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
  496. {
  497. char *var, *val, *tmp;
  498. FILE *f = file;
  499. int len, i;
  500. tmp = fgets(line, lsize, f);
  501. if (tmp == NULL) return 0;
  502. if (line[0] == '[') return 0; // other section
  503. // strip comments, linefeed, spaces..
  504. len = strlen(line);
  505. for (i = 0; i < len; i++)
  506. if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
  507. mystrip(line);
  508. len = strlen(line);
  509. if (len <= 0) return -1;;
  510. // get var and val
  511. for (i = 0; i < len; i++)
  512. if (line[i] == '=') break;
  513. if (i >= len || strchr(&line[i+1], '=') != NULL) {
  514. lprintf("config_readsect: can't parse: %s\n", line);
  515. return -1;
  516. }
  517. line[i] = 0;
  518. var = line;
  519. val = &line[i+1];
  520. mystrip(var);
  521. mystrip(val);
  522. #ifndef _MSC_VER
  523. if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
  524. lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
  525. return -1;;
  526. }
  527. #endif
  528. *rvar = var;
  529. *rval = val;
  530. return 1;
  531. }