config_file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. PicoIn.opt |= POPT_EN_STEREO;
  228. } else if (strcasecmp(tmp, "mono") == 0) {
  229. PicoIn.opt &= ~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. PicoIn.regionOverride = PicoIn.autoRgnOrder = 0;
  240. for (i = 0; p < end && i < 3; i++)
  241. {
  242. while (*p == ' ') p++;
  243. if (p[0] == 'J' && p[1] == 'P') {
  244. PicoIn.autoRgnOrder |= 1 << (i*4);
  245. } else if (p[0] == 'U' && p[1] == 'S') {
  246. PicoIn.autoRgnOrder |= 4 << (i*4);
  247. } else if (p[0] == 'E' && p[1] == 'U') {
  248. PicoIn.autoRgnOrder |= 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. PicoIn.regionOverride = 0;
  256. } else if (strcasecmp(val, "Japan NTSC") == 0) {
  257. PicoIn.regionOverride = 1;
  258. } else if (strcasecmp(val, "Japan PAL") == 0) {
  259. PicoIn.regionOverride = 2;
  260. } else if (strcasecmp(val, "USA") == 0) {
  261. PicoIn.regionOverride = 4;
  262. } else if (strcasecmp(val, "Europe") == 0) {
  263. PicoIn.regionOverride = 8;
  264. } else
  265. return 0;
  266. return 1;
  267. case MA_32XOPT_MSH2_CYCLES:
  268. currentConfig.msh2_khz = atoi(val);
  269. Pico32xSetClocks(currentConfig.msh2_khz * 1000, 0);
  270. return 1;
  271. case MA_32XOPT_SSH2_CYCLES:
  272. currentConfig.ssh2_khz = atoi(val);
  273. Pico32xSetClocks(0, currentConfig.ssh2_khz * 1000);
  274. return 1;
  275. case MA_OPT2_GAMMA:
  276. currentConfig.gamma = atoi(val);
  277. return 1;
  278. /* PSP */
  279. case MA_OPT3_SCALE:
  280. if (strcasecmp(var, "Scale factor") != 0) return 0;
  281. currentConfig.scale = atof(val);
  282. return 1;
  283. case MA_OPT3_HSCALE32:
  284. if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
  285. currentConfig.hscale32 = atof(val);
  286. return 1;
  287. case MA_OPT3_HSCALE40:
  288. if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
  289. currentConfig.hscale40 = atof(val);
  290. return 1;
  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 &= ~0x12000;
  296. } else if (strcasecmp(val, "sometimes") == 0) {
  297. currentConfig.EmuOpt |= 0x12000;
  298. } else if (strcasecmp(val, "always") == 0) {
  299. currentConfig.EmuOpt &= ~0x12000;
  300. currentConfig.EmuOpt |= 0x02000;
  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[256], *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. }
  368. static void parse(const char *var, const char *val, int *keys_encountered)
  369. {
  370. menu_entry *me;
  371. int tmp;
  372. if (strcasecmp(var, "LastUsedROM") == 0)
  373. return; /* handled elsewhere */
  374. if (strncasecmp(var, "bind", 4) == 0) {
  375. *keys_encountered = 1;
  376. return; /* handled elsewhere */
  377. }
  378. if (strcasecmp(var, "Sound Volume") == 0) {
  379. currentConfig.volume = atoi(val);
  380. return;
  381. }
  382. for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
  383. {
  384. char *p;
  385. if (!me->need_to_save)
  386. continue;
  387. if (me->name == NULL || strcasecmp(var, me->name) != 0)
  388. continue;
  389. if (me->beh == MB_OPT_ONOFF) {
  390. tmp = strtol(val, &p, 0);
  391. if (*p != 0)
  392. goto bad_val;
  393. if (tmp) *(int *)me->var |= me->mask;
  394. else *(int *)me->var &= ~me->mask;
  395. return;
  396. }
  397. else if (me->beh == MB_OPT_RANGE) {
  398. tmp = strtol(val, &p, 0);
  399. if (*p != 0)
  400. goto bad_val;
  401. if (tmp < me->min) tmp = me->min;
  402. if (tmp > me->max) tmp = me->max;
  403. *(int *)me->var = tmp;
  404. return;
  405. }
  406. else if (me->beh == MB_OPT_ENUM) {
  407. const char **names, *p1;
  408. int i;
  409. names = (const char **)me->data;
  410. if (names == NULL)
  411. goto bad_val;
  412. for (i = 0; names[i] != NULL; i++) {
  413. for (p1 = names[i]; *p1 == ' '; p1++)
  414. ;
  415. if (strcasecmp(p1, val) == 0) {
  416. *(int *)me->var = i;
  417. return;
  418. }
  419. }
  420. goto bad_val;
  421. }
  422. else if (custom_read(me, var, val))
  423. return;
  424. }
  425. lprintf("config_readsect: unhandled var: \"%s\"\n", var);
  426. return;
  427. bad_val:
  428. lprintf("config_readsect: unhandled val for \"%s\": \"%s\"\n", var, val);
  429. }
  430. int config_readsect(const char *fname, const char *section)
  431. {
  432. char line[128], *var, *val;
  433. int keys_encountered = 0;
  434. FILE *f;
  435. int ret;
  436. f = fopen(fname, "r");
  437. if (f == NULL) return -1;
  438. if (section != NULL)
  439. {
  440. /* for game_def.cfg only */
  441. ret = seek_sect(f, section);
  442. if (!ret) {
  443. lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
  444. fclose(f);
  445. return -1;
  446. }
  447. }
  448. emu_set_defconfig();
  449. while (!feof(f))
  450. {
  451. ret = config_get_var_val(f, line, sizeof(line), &var, &val);
  452. if (ret == 0) break;
  453. if (ret == -1) continue;
  454. parse(var, val, &keys_encountered);
  455. }
  456. if (keys_encountered) {
  457. rewind(f);
  458. keys_parse_all(f);
  459. }
  460. fclose(f);
  461. lprintf("config_readsect: loaded from %s", fname);
  462. if (section != NULL)
  463. lprintf(" [%s]", section);
  464. printf("\n");
  465. return 0;
  466. }
  467. #endif // _MSC_VER
  468. static char *mystrip(char *str)
  469. {
  470. int i, len;
  471. len = strlen(str);
  472. for (i = 0; i < len; i++)
  473. if (str[i] != ' ') break;
  474. if (i > 0) memmove(str, str + i, len - i + 1);
  475. len = strlen(str);
  476. for (i = len - 1; i >= 0; i--)
  477. if (str[i] != ' ') break;
  478. str[i+1] = 0;
  479. return str;
  480. }
  481. /* returns:
  482. * 0 - EOF, end
  483. * 1 - parsed ok
  484. * -1 - failed to parse line
  485. */
  486. int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
  487. {
  488. char *var, *val, *tmp;
  489. FILE *f = file;
  490. int len, i;
  491. tmp = fgets(line, lsize, f);
  492. if (tmp == NULL) return 0;
  493. if (line[0] == '[') return 0; // other section
  494. // strip comments, linefeed, spaces..
  495. len = strlen(line);
  496. for (i = 0; i < len; i++)
  497. if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
  498. mystrip(line);
  499. len = strlen(line);
  500. if (len <= 0) return -1;;
  501. // get var and val
  502. for (i = 0; i < len; i++)
  503. if (line[i] == '=') break;
  504. if (i >= len || strchr(&line[i+1], '=') != NULL) {
  505. lprintf("config_readsect: can't parse: %s\n", line);
  506. return -1;
  507. }
  508. line[i] = 0;
  509. var = line;
  510. val = &line[i+1];
  511. mystrip(var);
  512. mystrip(val);
  513. #ifndef _MSC_VER
  514. if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
  515. lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
  516. return -1;;
  517. }
  518. #endif
  519. *rvar = var;
  520. *rval = val;
  521. return 1;
  522. }