config_file.c 14 KB

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