config_file.c 14 KB

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