config_file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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_SOUND_ALPHA:
  239. if (strcasecmp(var, "Filter strength") != 0) return 0;
  240. PicoIn.sndFilterAlpha = 0x10000 * atof(val);
  241. return 1;
  242. case MA_OPT_REGION:
  243. if (strcasecmp(var, "Region") != 0) return 0;
  244. if (strncasecmp(val, "Auto: ", 6) == 0)
  245. {
  246. const char *p = val + 5, *end = val + strlen(val);
  247. int i;
  248. PicoIn.regionOverride = PicoIn.autoRgnOrder = 0;
  249. for (i = 0; p < end && i < 3; i++)
  250. {
  251. while (*p == ' ') p++;
  252. if (p[0] == 'J' && p[1] == 'P') {
  253. PicoIn.autoRgnOrder |= 1 << (i*4);
  254. } else if (p[0] == 'U' && p[1] == 'S') {
  255. PicoIn.autoRgnOrder |= 4 << (i*4);
  256. } else if (p[0] == 'E' && p[1] == 'U') {
  257. PicoIn.autoRgnOrder |= 8 << (i*4);
  258. }
  259. while (*p != ' ' && *p != 0) p++;
  260. if (*p == 0) break;
  261. }
  262. }
  263. else if (strcasecmp(val, "Auto") == 0) {
  264. PicoIn.regionOverride = 0;
  265. } else if (strcasecmp(val, "Japan NTSC") == 0) {
  266. PicoIn.regionOverride = 1;
  267. } else if (strcasecmp(val, "Japan PAL") == 0) {
  268. PicoIn.regionOverride = 2;
  269. } else if (strcasecmp(val, "USA") == 0) {
  270. PicoIn.regionOverride = 4;
  271. } else if (strcasecmp(val, "Europe") == 0) {
  272. PicoIn.regionOverride = 8;
  273. } else
  274. return 0;
  275. return 1;
  276. case MA_32XOPT_MSH2_CYCLES:
  277. currentConfig.msh2_khz = atoi(val);
  278. Pico32xSetClocks(currentConfig.msh2_khz * 1000, 0);
  279. return 1;
  280. case MA_32XOPT_SSH2_CYCLES:
  281. currentConfig.ssh2_khz = atoi(val);
  282. Pico32xSetClocks(0, currentConfig.ssh2_khz * 1000);
  283. return 1;
  284. case MA_OPT2_GAMMA:
  285. currentConfig.gamma = atoi(val);
  286. return 1;
  287. case MA_OPT2_MAX_FRAMESKIP:
  288. currentConfig.max_skip = atoi(val);
  289. return 1;
  290. /* PSP */
  291. case MA_OPT3_VSYNC:
  292. // XXX: use enum
  293. if (strcasecmp(var, "Wait for vsync") != 0) return 0;
  294. if (strcasecmp(val, "never") == 0) {
  295. currentConfig.EmuOpt &= ~(EOPT_VSYNC|EOPT_VSYNC_MODE);
  296. } else if (strcasecmp(val, "sometimes") == 0) {
  297. currentConfig.EmuOpt |= (EOPT_VSYNC|EOPT_VSYNC_MODE);
  298. } else if (strcasecmp(val, "always") == 0) {
  299. currentConfig.EmuOpt &= ~EOPT_VSYNC_MODE;
  300. currentConfig.EmuOpt |= EOPT_VSYNC;
  301. } else
  302. return 0;
  303. return 1;
  304. default:
  305. lprintf("unhandled custom_read %i: %s\n", me->id, var);
  306. return 0;
  307. }
  308. }
  309. static int parse_bind_val(const char *val, int *type)
  310. {
  311. int i;
  312. *type = IN_BINDTYPE_NONE;
  313. if (val[0] == 0)
  314. return 0;
  315. if (strncasecmp(val, "player", 6) == 0)
  316. {
  317. int player, shift = 0;
  318. player = atoi(val + 6) - 1;
  319. if (player > 1)
  320. return -1;
  321. if (player == 1)
  322. shift = 16;
  323. *type = IN_BINDTYPE_PLAYER12;
  324. for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
  325. if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
  326. return me_ctrl_actions[i].mask << shift;
  327. }
  328. }
  329. for (i = 0; emuctrl_actions[i].name != NULL; i++) {
  330. if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
  331. *type = IN_BINDTYPE_EMU;
  332. return emuctrl_actions[i].mask;
  333. }
  334. }
  335. return -1;
  336. }
  337. static void keys_parse_all(FILE *f)
  338. {
  339. char line[640], *var, *val;
  340. int dev_id = -1;
  341. int acts, type;
  342. int ret;
  343. while (!feof(f))
  344. {
  345. ret = config_get_var_val(f, line, sizeof(line), &var, &val);
  346. if (ret == 0) break;
  347. if (ret == -1) continue;
  348. if (strcasecmp(var, "binddev") == 0) {
  349. dev_id = in_config_parse_dev(val);
  350. if (dev_id < 0) {
  351. printf("input: can't handle dev: %s\n", val);
  352. continue;
  353. }
  354. in_unbind_all(dev_id, -1, -1);
  355. continue;
  356. }
  357. if (dev_id < 0 || strncasecmp(var, "bind ", 5) != 0)
  358. continue;
  359. acts = parse_bind_val(val, &type);
  360. if (acts == -1) {
  361. lprintf("config: unhandled action \"%s\"\n", val);
  362. continue;
  363. }
  364. mystrip(var + 5);
  365. in_config_bind_key(dev_id, var + 5, acts, type);
  366. }
  367. in_clean_binds();
  368. }
  369. static void parse(const char *var, const char *val, int *keys_encountered)
  370. {
  371. menu_entry *me;
  372. int tmp;
  373. if (strcasecmp(var, "LastUsedROM") == 0)
  374. return; /* handled elsewhere */
  375. if (strncasecmp(var, "bind", 4) == 0) {
  376. *keys_encountered = 1;
  377. return; /* handled elsewhere */
  378. }
  379. if (strcasecmp(var, "Sound Volume") == 0) {
  380. currentConfig.volume = atoi(val);
  381. return;
  382. }
  383. for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
  384. {
  385. char *p;
  386. if (!me->need_to_save)
  387. continue;
  388. if (me->name == NULL || strcasecmp(var, me->name) != 0)
  389. continue;
  390. if (me->beh == MB_OPT_ONOFF) {
  391. tmp = strtol(val, &p, 0);
  392. if (*p != 0)
  393. goto bad_val;
  394. if (tmp) *(int *)me->var |= me->mask;
  395. else *(int *)me->var &= ~me->mask;
  396. return;
  397. }
  398. else if (me->beh == MB_OPT_RANGE) {
  399. tmp = strtol(val, &p, 0);
  400. if (*p != 0)
  401. goto bad_val;
  402. if (tmp < me->min) tmp = me->min;
  403. if (tmp > me->max) tmp = me->max;
  404. *(int *)me->var = tmp;
  405. return;
  406. }
  407. else if (me->beh == MB_OPT_ENUM) {
  408. const char **names, *p1;
  409. int i;
  410. names = (const char **)me->data;
  411. if (names == NULL)
  412. goto bad_val;
  413. for (i = 0; names[i] != NULL; i++) {
  414. for (p1 = names[i]; *p1 == ' '; p1++)
  415. ;
  416. if (strcasecmp(p1, val) == 0) {
  417. *(int *)me->var = i;
  418. return;
  419. }
  420. }
  421. goto bad_val;
  422. }
  423. else if (custom_read(me, var, val))
  424. return;
  425. }
  426. lprintf("config_readsect: unhandled var: \"%s\"\n", var);
  427. return;
  428. bad_val:
  429. lprintf("config_readsect: unhandled val for \"%s\": \"%s\"\n", var, val);
  430. }
  431. int config_readsect(const char *fname, const char *section)
  432. {
  433. char line[640], *var, *val;
  434. int keys_encountered = 0;
  435. FILE *f;
  436. int ret;
  437. f = fopen(fname, "r");
  438. if (f == NULL) return -1;
  439. if (section != NULL)
  440. {
  441. /* for game_def.cfg only */
  442. ret = seek_sect(f, section);
  443. if (!ret) {
  444. lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
  445. fclose(f);
  446. return -1;
  447. }
  448. }
  449. emu_set_defconfig();
  450. while (!feof(f))
  451. {
  452. ret = config_get_var_val(f, line, sizeof(line), &var, &val);
  453. if (ret == 0) break;
  454. if (ret == -1) continue;
  455. parse(var, val, &keys_encountered);
  456. }
  457. if (keys_encountered) {
  458. rewind(f);
  459. keys_parse_all(f);
  460. }
  461. fclose(f);
  462. lprintf("config_readsect: loaded from %s", fname);
  463. if (section != NULL)
  464. lprintf(" [%s]", section);
  465. printf("\n");
  466. return 0;
  467. }
  468. #endif // _MSC_VER
  469. static char *mystrip(char *str)
  470. {
  471. int i, len;
  472. len = strlen(str);
  473. for (i = 0; i < len; i++)
  474. if (str[i] != ' ') break;
  475. if (i > 0) memmove(str, str + i, len - i + 1);
  476. len = strlen(str);
  477. for (i = len - 1; i >= 0; i--)
  478. if (str[i] != ' ') break;
  479. str[i+1] = 0;
  480. return str;
  481. }
  482. /* returns:
  483. * 0 - EOF, end
  484. * 1 - parsed ok
  485. * -1 - failed to parse line
  486. */
  487. int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
  488. {
  489. char *var, *val, *tmp;
  490. FILE *f = file;
  491. int len, i;
  492. tmp = fgets(line, lsize, f);
  493. if (tmp == NULL) return 0;
  494. if (line[0] == '[') return 0; // other section
  495. // strip comments, linefeed, spaces..
  496. len = strlen(line);
  497. for (i = 0; i < len; i++)
  498. if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
  499. mystrip(line);
  500. len = strlen(line);
  501. if (len <= 0) return -1;;
  502. // get var and val
  503. for (i = 0; i < len; i++)
  504. if (line[i] == '=') break;
  505. if (i >= len || strchr(&line[i+1], '=') != NULL) {
  506. lprintf("config_readsect: can't parse: %s\n", line);
  507. return -1;
  508. }
  509. line[i] = 0;
  510. var = line;
  511. val = &line[i+1];
  512. mystrip(var);
  513. mystrip(val);
  514. #ifndef _MSC_VER
  515. if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
  516. lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
  517. return -1;;
  518. }
  519. #endif
  520. *rvar = var;
  521. *rval = val;
  522. return 1;
  523. }