config.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * Human-readable config file management for PicoDrive
  3. * (c)
  4. */
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include "config.h"
  8. #include "menu.h"
  9. #include "emu.h"
  10. #include "lprintf.h"
  11. #include <Pico/Pico.h>
  12. extern menu_entry opt_entries[];
  13. extern menu_entry opt2_entries[];
  14. extern menu_entry cdopt_entries[];
  15. extern const int opt_entry_count;
  16. extern const int opt2_entry_count;
  17. extern const int cdopt_entry_count;
  18. #ifdef PSP
  19. extern menu_entry opt3_entries[];
  20. extern const int opt3_entry_count;
  21. #endif
  22. static menu_entry *cfg_opts[] =
  23. {
  24. opt_entries,
  25. opt2_entries,
  26. cdopt_entries,
  27. #ifdef PSP
  28. opt3_entries,
  29. #endif
  30. };
  31. static const int *cfg_opt_counts[] =
  32. {
  33. &opt_entry_count,
  34. &opt2_entry_count,
  35. &cdopt_entry_count,
  36. #ifdef PSP
  37. &opt3_entry_count,
  38. #endif
  39. };
  40. #define NL "\r\n"
  41. static char *mystrip(char *str)
  42. {
  43. int i, len;
  44. len = strlen(str);
  45. for (i = 0; i < len; i++)
  46. if (str[i] != ' ') break;
  47. if (i > 0) memmove(str, str + i, len - i + 1);
  48. len = strlen(str);
  49. for (i = len - 1; i >= 0; i--)
  50. if (str[i] != ' ') break;
  51. str[i+1] = 0;
  52. return str;
  53. }
  54. static int seek_sect(FILE *f, const char *section)
  55. {
  56. char line[128], *tmp;
  57. int len;
  58. len = strlen(section);
  59. // seek to the section needed
  60. while (!feof(f))
  61. {
  62. tmp = fgets(line, sizeof(line), f);
  63. if (tmp == NULL) break;
  64. if (line[0] != '[') continue; // not section start
  65. if (strncmp(line + 1, section, len) == 0 && line[len+1] == ']')
  66. return 1; // found it
  67. }
  68. return 0;
  69. }
  70. static void custom_write(FILE *f, const menu_entry *me, int no_def)
  71. {
  72. char *str, str24[24];
  73. switch (me->id)
  74. {
  75. case MA_OPT_RENDERER:
  76. if (no_def && !((defaultConfig.s_PicoOpt^PicoOpt)&POPT_ALT_RENDERER) &&
  77. !((defaultConfig.EmuOpt^currentConfig.EmuOpt)&0x80)) return;
  78. if (PicoOpt&POPT_ALT_RENDERER)
  79. str =
  80. #ifndef PSP
  81. "8bit "
  82. #endif
  83. "fast";
  84. else if (currentConfig.EmuOpt&0x80)
  85. str =
  86. #ifndef PSP
  87. "16bit "
  88. #endif
  89. "accurate";
  90. else
  91. str = "8bit accurate";
  92. fprintf(f, "Renderer = %s", str);
  93. break;
  94. case MA_OPT_SCALING:
  95. if (no_def && defaultConfig.scaling == currentConfig.scaling) return;
  96. #ifdef __GP2X__
  97. switch (currentConfig.scaling) {
  98. default: str = "OFF"; break;
  99. case 1: str = "hw horizontal"; break;
  100. case 2: str = "hw horiz. + vert."; break;
  101. case 3: str = "sw horizontal"; break;
  102. case 1: str = "ON"; break;
  103. }
  104. fprintf(f, "Scaling = %s", str);
  105. #endif
  106. break;
  107. case MA_OPT_FRAMESKIP:
  108. if (no_def && defaultConfig.Frameskip == currentConfig.Frameskip) return;
  109. if (currentConfig.Frameskip < 0)
  110. strcpy(str24, "Auto");
  111. else sprintf(str24, "%i", currentConfig.Frameskip);
  112. fprintf(f, "Frameskip = %s", str24);
  113. break;
  114. case MA_OPT_SOUND_QUALITY:
  115. if (no_def && !((defaultConfig.s_PicoOpt^PicoOpt)&POPT_EN_STEREO) &&
  116. defaultConfig.s_PsndRate == PsndRate) return;
  117. str = (PicoOpt&POPT_EN_STEREO)?"stereo":"mono";
  118. fprintf(f, "Sound Quality = %i %s", PsndRate, str);
  119. break;
  120. case MA_OPT_REGION:
  121. if (no_def && defaultConfig.s_PicoRegion == PicoRegionOverride &&
  122. defaultConfig.s_PicoAutoRgnOrder == PicoAutoRgnOrder) return;
  123. strncpy(str24, me_region_name(PicoRegionOverride, PicoAutoRgnOrder), 23); str24[23] = 0;
  124. fprintf(f, "Region = %s", mystrip(str24));
  125. break;
  126. case MA_OPT_CONFIRM_STATES:
  127. if (no_def && !((defaultConfig.EmuOpt^currentConfig.EmuOpt)&(5<<9))) return;
  128. switch ((currentConfig.EmuOpt >> 9) & 5) {
  129. default: str = "OFF"; break;
  130. case 1: str = "writes"; break;
  131. case 4: str = "loads"; break;
  132. case 5: str = "both"; break;
  133. }
  134. fprintf(f, "Confirm savestate = %s", str);
  135. break;
  136. case MA_OPT_CPU_CLOCKS:
  137. if (no_def && defaultConfig.CPUclock == currentConfig.CPUclock) return;
  138. #ifdef __GP2X__
  139. fprintf(f, "GP2X CPU clocks = %i", currentConfig.CPUclock);
  140. #elif defined(PSP)
  141. fprintf(f, "PSP CPU clock = %i", currentConfig.CPUclock);
  142. #endif
  143. break;
  144. case MA_OPT2_GAMMA:
  145. if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
  146. fprintf(f, "Gamma correction = %.3f", (double)currentConfig.gamma / 100.0);
  147. break;
  148. case MA_OPT2_SQUIDGEHACK:
  149. if (no_def && !((defaultConfig.EmuOpt^currentConfig.EmuOpt)&0x0010)) return;
  150. fprintf(f, "Squidgehack = %i", (currentConfig.EmuOpt&0x0010)>>4);
  151. break;
  152. case MA_CDOPT_READAHEAD:
  153. if (no_def && defaultConfig.s_PicoCDBuffers == PicoCDBuffers) return;
  154. sprintf(str24, "%i", PicoCDBuffers * 2);
  155. fprintf(f, "ReadAhead buffer = %s", str24);
  156. break;
  157. /* PSP */
  158. case MA_OPT3_SCALE:
  159. if (no_def && defaultConfig.scale == currentConfig.scale) return;
  160. fprintf(f, "Scale factor = %.2f", currentConfig.scale);
  161. break;
  162. case MA_OPT3_HSCALE32:
  163. if (no_def && defaultConfig.hscale32 == currentConfig.hscale32) return;
  164. fprintf(f, "Hor. scale (for low res. games) = %.2f", currentConfig.hscale32);
  165. break;
  166. case MA_OPT3_HSCALE40:
  167. if (no_def && defaultConfig.hscale40 == currentConfig.hscale40) return;
  168. fprintf(f, "Hor. scale (for hi res. games) = %.2f", currentConfig.hscale40);
  169. break;
  170. case MA_OPT3_FILTERING:
  171. if (no_def && defaultConfig.scaling == currentConfig.scaling) return;
  172. fprintf(f, "Bilinear filtering = %i", currentConfig.scaling);
  173. break;
  174. case MA_OPT3_GAMMAA:
  175. if (no_def && defaultConfig.gamma == currentConfig.gamma) return;
  176. fprintf(f, "Gamma adjustment = %i", currentConfig.gamma);
  177. break;
  178. case MA_OPT3_BLACKLVL:
  179. if (no_def && defaultConfig.gamma2 == currentConfig.gamma2) return;
  180. fprintf(f, "Black level = %i", currentConfig.gamma2);
  181. break;
  182. case MA_OPT3_VSYNC:
  183. if (no_def && (defaultConfig.EmuOpt&0x12000) == (currentConfig.gamma2&0x12000)) return;
  184. strcpy(str24, "never");
  185. if (currentConfig.EmuOpt & 0x2000)
  186. strcpy(str24, (currentConfig.EmuOpt & 0x10000) ? "sometimes" : "always");
  187. fprintf(f, "Wait for vsync = %s", str24);
  188. break;
  189. default:
  190. lprintf("unhandled custom_write: %i\n", me->id);
  191. return;
  192. }
  193. fprintf(f, NL);
  194. }
  195. static const char *joyKeyNames[32] =
  196. {
  197. "UP", "DOWN", "LEFT", "RIGHT", "b1", "b2", "b3", "b4",
  198. "b5", "b6", "b7", "b8", "b9", "b10", "b11", "b12",
  199. "b13", "b14", "b15", "b16", "b17", "b19", "b19", "b20",
  200. "b21", "b22", "b23", "b24", "b25", "b26", "b27", "b28"
  201. };
  202. static void keys_write(FILE *fn, const char *bind_str, const int binds[32],
  203. const int def_binds[32], const char * const names[32], int no_defaults)
  204. {
  205. int t, i;
  206. char act[48];
  207. for (t = 0; t < 32; t++)
  208. {
  209. act[0] = act[31] = 0;
  210. if (no_defaults && binds[t] == def_binds[t])
  211. continue;
  212. if (strcmp(names[t], "???") == 0) continue;
  213. #ifdef __GP2X__
  214. if (strcmp(names[t], "SELECT") == 0) continue;
  215. #endif
  216. if (binds[t] == 0 && def_binds[t] != 0) {
  217. fprintf(fn, "%s %s =" NL, bind_str, names[t]); // no binds
  218. continue;
  219. }
  220. for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
  221. if (me_ctrl_actions[i].mask & binds[t]) {
  222. strncpy(act, me_ctrl_actions[i].name, 31);
  223. fprintf(fn, "%s %s = player%i %s" NL, bind_str, names[t],
  224. ((binds[t]>>16)&1)+1, mystrip(act));
  225. }
  226. }
  227. for (i = 0; emuctrl_actions[i].name != NULL; i++) {
  228. if (emuctrl_actions[i].mask & binds[t]) {
  229. strncpy(act, emuctrl_actions[i].name, 31);
  230. fprintf(fn, "%s %s = %s" NL, bind_str, names[t], mystrip(act));
  231. }
  232. }
  233. }
  234. }
  235. static int default_var(const menu_entry *me)
  236. {
  237. switch (me->id)
  238. {
  239. case MA_OPT_ACC_TIMING:
  240. case MA_OPT_ACC_SPRITES:
  241. case MA_OPT_ARM940_SOUND:
  242. case MA_OPT_6BUTTON_PAD:
  243. case MA_OPT2_ENABLE_Z80:
  244. case MA_OPT2_ENABLE_YM2612:
  245. case MA_OPT2_ENABLE_SN76496:
  246. case MA_OPT2_SVP_DYNAREC:
  247. case MA_CDOPT_CDDA:
  248. case MA_CDOPT_PCM:
  249. case MA_CDOPT_SAVERAM:
  250. case MA_CDOPT_SCALEROT_CHIP:
  251. case MA_CDOPT_BETTER_SYNC:
  252. return defaultConfig.s_PicoOpt;
  253. case MA_OPT_SHOW_FPS:
  254. case MA_OPT_ENABLE_SOUND:
  255. case MA_OPT_SRAM_STATES:
  256. case MA_OPT2_A_SN_GAMMA:
  257. case MA_OPT2_VSYNC:
  258. case MA_OPT2_GZIP_STATES:
  259. case MA_OPT2_NO_LAST_ROM:
  260. case MA_OPT2_RAMTIMINGS:
  261. case MA_CDOPT_LEDS:
  262. return defaultConfig.EmuOpt;
  263. case MA_OPT_SAVE_SLOT:
  264. default:
  265. return 0;
  266. }
  267. }
  268. int config_writesect(const char *fname, const char *section)
  269. {
  270. FILE *fo = NULL, *fn = NULL; // old and new
  271. int no_defaults = 0; // avoid saving defaults
  272. menu_entry *me;
  273. int t, i, tlen, ret;
  274. char line[128], *tmp;
  275. if (section != NULL)
  276. {
  277. no_defaults = 1;
  278. fo = fopen(fname, "r");
  279. if (fo == NULL) {
  280. fn = fopen(fname, "w");
  281. goto write;
  282. }
  283. ret = seek_sect(fo, section);
  284. if (!ret) {
  285. // sect not found, we can simply append
  286. fclose(fo); fo = NULL;
  287. fn = fopen(fname, "a");
  288. goto write;
  289. }
  290. // use 2 files..
  291. fclose(fo);
  292. rename(fname, "tmp.cfg");
  293. fo = fopen("tmp.cfg", "r");
  294. fn = fopen(fname, "w");
  295. if (fo == NULL || fn == NULL) goto write;
  296. // copy everything until sect
  297. tlen = strlen(section);
  298. while (!feof(fo))
  299. {
  300. tmp = fgets(line, sizeof(line), fo);
  301. if (tmp == NULL) break;
  302. if (line[0] == '[' && strncmp(line + 1, section, tlen) == 0 && line[tlen+1] == ']')
  303. break;
  304. fputs(line, fn);
  305. }
  306. // now skip to next sect
  307. while (!feof(fo))
  308. {
  309. tmp = fgets(line, sizeof(line), fo);
  310. if (tmp == NULL) break;
  311. if (line[0] == '[') {
  312. fseek(fo, -strlen(line), SEEK_CUR);
  313. break;
  314. }
  315. }
  316. if (feof(fo))
  317. {
  318. fclose(fo); fo = NULL;
  319. remove("tmp.cfg");
  320. }
  321. }
  322. else
  323. {
  324. fn = fopen(fname, "w");
  325. }
  326. write:
  327. if (fn == NULL) {
  328. if (fo) fclose(fo);
  329. return -1;
  330. }
  331. if (section != NULL)
  332. fprintf(fn, "[%s]" NL, section);
  333. for (t = 0; t < sizeof(cfg_opts) / sizeof(cfg_opts[0]); t++)
  334. {
  335. me = cfg_opts[t];
  336. tlen = *(cfg_opt_counts[t]);
  337. for (i = 0; i < tlen; i++, me++)
  338. {
  339. if (!me->need_to_save) continue;
  340. if ((me->beh != MB_ONOFF && me->beh != MB_RANGE) || me->name == NULL)
  341. custom_write(fn, me, no_defaults);
  342. else if (me->beh == MB_ONOFF) {
  343. if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask))
  344. fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
  345. } else if (me->beh == MB_RANGE) {
  346. if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask))
  347. fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
  348. }
  349. }
  350. }
  351. // save key config
  352. keys_write(fn, "bind", currentConfig.KeyBinds, defaultConfig.KeyBinds, keyNames, no_defaults);
  353. keys_write(fn, "bind_joy0", currentConfig.JoyBinds[0], defaultConfig.JoyBinds[0], joyKeyNames, 1);
  354. keys_write(fn, "bind_joy1", currentConfig.JoyBinds[1], defaultConfig.JoyBinds[1], joyKeyNames, 1);
  355. keys_write(fn, "bind_joy2", currentConfig.JoyBinds[2], defaultConfig.JoyBinds[2], joyKeyNames, 1);
  356. keys_write(fn, "bind_joy3", currentConfig.JoyBinds[3], defaultConfig.JoyBinds[3], joyKeyNames, 1);
  357. #ifndef PSP
  358. if (section == NULL)
  359. fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
  360. #endif
  361. fprintf(fn, NL);
  362. if (fo != NULL)
  363. {
  364. // copy whatever is left
  365. while (!feof(fo))
  366. {
  367. tmp = fgets(line, sizeof(line), fo);
  368. if (tmp == NULL) break;
  369. fputs(line, fn);
  370. }
  371. fclose(fo);
  372. remove("tmp.cfg");
  373. }
  374. fclose(fn);
  375. return 0;
  376. }
  377. int config_writelrom(const char *fname)
  378. {
  379. char line[128], *tmp, *optr = NULL;
  380. char *old_data = NULL;
  381. int size;
  382. FILE *f;
  383. if (strlen(lastRomFile) == 0) return -1;
  384. f = fopen(fname, "r");
  385. if (f != NULL)
  386. {
  387. fseek(f, 0, SEEK_END);
  388. size = ftell(f);
  389. fseek(f, 0, SEEK_SET);
  390. old_data = malloc(size + size/8);
  391. if (old_data != NULL)
  392. {
  393. optr = old_data;
  394. while (!feof(f))
  395. {
  396. tmp = fgets(line, sizeof(line), f);
  397. if (tmp == NULL) break;
  398. mystrip(line);
  399. if (strncasecmp(line, "LastUsedROM", 11) == 0)
  400. continue;
  401. sprintf(optr, "%s", line);
  402. optr += strlen(optr);
  403. }
  404. }
  405. fclose(f);
  406. }
  407. f = fopen(fname, "w");
  408. if (f == NULL) return -1;
  409. if (old_data != NULL) {
  410. fwrite(old_data, 1, optr - old_data, f);
  411. free(old_data);
  412. }
  413. fprintf(f, "LastUsedROM = %s" NL, lastRomFile);
  414. fclose(f);
  415. return 0;
  416. }
  417. /* --------------------------------------------------------------------------*/
  418. int config_readlrom(const char *fname)
  419. {
  420. char line[128], *tmp;
  421. int i, len, ret = -1;
  422. FILE *f;
  423. f = fopen(fname, "r");
  424. if (f == NULL) return -1;
  425. // seek to the section needed
  426. while (!feof(f))
  427. {
  428. tmp = fgets(line, sizeof(line), f);
  429. if (tmp == NULL) break;
  430. if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
  431. len = strlen(line);
  432. for (i = 0; i < len; i++)
  433. if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
  434. tmp = strchr(line, '=');
  435. if (tmp == NULL) break;
  436. tmp++;
  437. mystrip(tmp);
  438. len = sizeof(lastRomFile);
  439. strncpy(lastRomFile, tmp, len);
  440. lastRomFile[len-1] = 0;
  441. ret = 0;
  442. break;
  443. }
  444. fclose(f);
  445. return ret;
  446. }
  447. static int custom_read(menu_entry *me, const char *var, const char *val)
  448. {
  449. char *tmp;
  450. int tmpi;
  451. switch (me->id)
  452. {
  453. case MA_OPT_RENDERER:
  454. if (strcasecmp(var, "Renderer") != 0) return 0;
  455. if (strcasecmp(val, "8bit fast") == 0 || strcasecmp(val, "fast") == 0) {
  456. PicoOpt |= POPT_ALT_RENDERER;
  457. }
  458. else if (strcasecmp(val, "16bit accurate") == 0 || strcasecmp(val, "accurate") == 0) {
  459. PicoOpt &= ~POPT_ALT_RENDERER;
  460. currentConfig.EmuOpt |= 0x80;
  461. }
  462. else if (strcasecmp(val, "8bit accurate") == 0) {
  463. PicoOpt &= ~POPT_ALT_RENDERER;
  464. currentConfig.EmuOpt &= ~0x80;
  465. }
  466. else
  467. return 0;
  468. return 1;
  469. case MA_OPT_SCALING:
  470. #ifdef __GP2X__
  471. if (strcasecmp(var, "Scaling") != 0) return 0;
  472. if (strcasecmp(val, "OFF") == 0) {
  473. currentConfig.scaling = 0;
  474. } else if (strcasecmp(val, "hw horizontal") == 0) {
  475. currentConfig.scaling = 1;
  476. } else if (strcasecmp(val, "hw horiz. + vert.") == 0) {
  477. currentConfig.scaling = 2;
  478. } else if (strcasecmp(val, "sw horizontal") == 0) {
  479. currentConfig.scaling = 3;
  480. } else
  481. return 0;
  482. return 1;
  483. #else
  484. return 0;
  485. #endif
  486. case MA_OPT_FRAMESKIP:
  487. if (strcasecmp(var, "Frameskip") != 0) return 0;
  488. if (strcasecmp(val, "Auto") == 0)
  489. currentConfig.Frameskip = -1;
  490. else currentConfig.Frameskip = atoi(val);
  491. return 1;
  492. case MA_OPT_SOUND_QUALITY:
  493. if (strcasecmp(var, "Sound Quality") != 0) return 0;
  494. PsndRate = strtoul(val, &tmp, 10);
  495. if (PsndRate < 8000 || PsndRate > 44100)
  496. PsndRate = 22050;
  497. while (*tmp == ' ') tmp++;
  498. if (strcasecmp(tmp, "stereo") == 0) {
  499. PicoOpt |= POPT_EN_STEREO;
  500. } else if (strcasecmp(tmp, "mono") == 0) {
  501. PicoOpt &= ~POPT_EN_STEREO;
  502. } else
  503. return 0;
  504. return 1;
  505. case MA_OPT_REGION:
  506. if (strcasecmp(var, "Region") != 0) return 0;
  507. if (strncasecmp(val, "Auto: ", 6) == 0)
  508. {
  509. const char *p = val + 5, *end = val + strlen(val);
  510. int i;
  511. PicoRegionOverride = PicoAutoRgnOrder = 0;
  512. for (i = 0; p < end && i < 3; i++)
  513. {
  514. while (*p == ' ') p++;
  515. if (p[0] == 'J' && p[1] == 'P') {
  516. PicoAutoRgnOrder |= 1 << (i*4);
  517. } else if (p[0] == 'U' && p[1] == 'S') {
  518. PicoAutoRgnOrder |= 4 << (i*4);
  519. } else if (p[0] == 'E' && p[1] == 'U') {
  520. PicoAutoRgnOrder |= 8 << (i*4);
  521. }
  522. while (*p != ' ' && *p != 0) p++;
  523. if (*p == 0) break;
  524. }
  525. }
  526. else if (strcasecmp(val, "Auto") == 0) {
  527. PicoRegionOverride = 0;
  528. } else if (strcasecmp(val, "Japan NTSC") == 0) {
  529. PicoRegionOverride = 1;
  530. } else if (strcasecmp(val, "Japan PAL") == 0) {
  531. PicoRegionOverride = 2;
  532. } else if (strcasecmp(val, "USA") == 0) {
  533. PicoRegionOverride = 4;
  534. } else if (strcasecmp(val, "Europe") == 0) {
  535. PicoRegionOverride = 8;
  536. } else
  537. return 0;
  538. return 1;
  539. case MA_OPT_CONFIRM_STATES:
  540. if (strcasecmp(var, "Confirm savestate") != 0) return 0;
  541. if (strcasecmp(val, "OFF") == 0) {
  542. currentConfig.EmuOpt &= ~(5<<9);
  543. } else if (strcasecmp(val, "writes") == 0) {
  544. currentConfig.EmuOpt &= ~(5<<9);
  545. currentConfig.EmuOpt |= 1<<9;
  546. } else if (strcasecmp(val, "loads") == 0) {
  547. currentConfig.EmuOpt &= ~(5<<9);
  548. currentConfig.EmuOpt |= 4<<9;
  549. } else if (strcasecmp(val, "both") == 0) {
  550. currentConfig.EmuOpt &= ~(5<<9);
  551. currentConfig.EmuOpt |= 5<<9;
  552. } else
  553. return 0;
  554. return 1;
  555. case MA_OPT_CPU_CLOCKS:
  556. #ifdef __GP2X__
  557. if (strcasecmp(var, "GP2X CPU clocks") != 0) return 0;
  558. #elif defined(PSP)
  559. if (strcasecmp(var, "PSP CPU clock") != 0) return 0;
  560. #endif
  561. currentConfig.CPUclock = atoi(val);
  562. return 1;
  563. case MA_OPT2_GAMMA:
  564. if (strcasecmp(var, "Gamma correction") != 0) return 0;
  565. currentConfig.gamma = (int) (atof(val) * 100.0);
  566. return 1;
  567. case MA_OPT2_SQUIDGEHACK:
  568. if (strcasecmp(var, "Squidgehack") != 0) return 0;
  569. tmpi = atoi(val);
  570. if (tmpi) *(int *)me->var |= me->mask;
  571. else *(int *)me->var &= ~me->mask;
  572. return 1;
  573. case MA_CDOPT_READAHEAD:
  574. if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
  575. PicoCDBuffers = atoi(val) / 2;
  576. return 1;
  577. /* PSP */
  578. case MA_OPT3_SCALE:
  579. if (strcasecmp(var, "Scale factor") != 0) return 0;
  580. currentConfig.scale = atof(val);
  581. return 1;
  582. case MA_OPT3_HSCALE32:
  583. if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
  584. currentConfig.hscale32 = atof(val);
  585. return 1;
  586. case MA_OPT3_HSCALE40:
  587. if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
  588. currentConfig.hscale40 = atof(val);
  589. return 1;
  590. case MA_OPT3_FILTERING:
  591. if (strcasecmp(var, "Bilinear filtering") != 0) return 0;
  592. currentConfig.scaling = atoi(val);
  593. return 1;
  594. case MA_OPT3_GAMMAA:
  595. if (strcasecmp(var, "Gamma adjustment") != 0) return 0;
  596. currentConfig.gamma = atoi(val);
  597. return 1;
  598. case MA_OPT3_BLACKLVL:
  599. if (strcasecmp(var, "Black level") != 0) return 0;
  600. currentConfig.gamma2 = atoi(val);
  601. return 1;
  602. case MA_OPT3_VSYNC:
  603. if (strcasecmp(var, "Wait for vsync") != 0) return 0;
  604. if (strcasecmp(val, "never") == 0) {
  605. currentConfig.EmuOpt &= ~0x12000;
  606. } else if (strcasecmp(val, "sometimes") == 0) {
  607. currentConfig.EmuOpt |= 0x12000;
  608. } else if (strcasecmp(val, "always") == 0) {
  609. currentConfig.EmuOpt &= ~0x12000;
  610. currentConfig.EmuOpt |= 0x02000;
  611. } else
  612. return 0;
  613. return 1;
  614. default:
  615. lprintf("unhandled custom_read: %i\n", me->id);
  616. return 0;
  617. }
  618. }
  619. static unsigned int keys_encountered = 0;
  620. static void keys_parse(const char *var, const char *val, int binds[32], const char * const names[32])
  621. {
  622. int t, i;
  623. unsigned int player;
  624. for (t = 0; t < 32; t++)
  625. {
  626. if (strcmp(names[t], var) == 0) break;
  627. }
  628. if (t == 32) {
  629. lprintf("unhandled bind \"%s\"\n", var);
  630. return;
  631. }
  632. if (binds == currentConfig.KeyBinds && !(keys_encountered & (1<<t))) { // hack
  633. binds[t] = 0;
  634. keys_encountered |= 1<<t;
  635. }
  636. if (val[0] == 0)
  637. return;
  638. if (strncasecmp(val, "player", 6) == 0)
  639. {
  640. player = atoi(val + 6) - 1;
  641. if (player > 1) goto fail;
  642. for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) {
  643. if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0) {
  644. binds[t] |= me_ctrl_actions[i].mask | (player<<16);
  645. return;
  646. }
  647. }
  648. }
  649. for (i = 0; emuctrl_actions[i].name != NULL; i++) {
  650. if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
  651. binds[t] |= emuctrl_actions[i].mask;
  652. return;
  653. }
  654. }
  655. fail:
  656. lprintf("unhandled action \"%s\"\n", val);
  657. return;
  658. }
  659. #define try_joy_parse(num) { \
  660. if (strncasecmp(var, "bind_joy"#num " ", 10) == 0) { \
  661. keys_parse(var + 10, val, currentConfig.JoyBinds[num], joyKeyNames); \
  662. return; \
  663. } \
  664. }
  665. static void parse(const char *var, const char *val)
  666. {
  667. menu_entry *me;
  668. int t, i, tlen, tmp, ret = 0;
  669. if (strcasecmp(var, "LastUsedROM") == 0)
  670. return; /* handled elsewhere */
  671. if (strcasecmp(var, "Sound Volume") == 0) {
  672. currentConfig.volume = atoi(val);
  673. return;
  674. }
  675. // key binds
  676. if (strncasecmp(var, "bind ", 5) == 0) {
  677. keys_parse(var + 5, val, currentConfig.KeyBinds, keyNames);
  678. return;
  679. }
  680. try_joy_parse(0)
  681. try_joy_parse(1)
  682. try_joy_parse(2)
  683. try_joy_parse(3)
  684. for (t = 0; t < sizeof(cfg_opts) / sizeof(cfg_opts[0]) && ret == 0; t++)
  685. {
  686. me = cfg_opts[t];
  687. tlen = *(cfg_opt_counts[t]);
  688. for (i = 0; i < tlen && ret == 0; i++, me++)
  689. {
  690. if (!me->need_to_save) continue;
  691. if (me->name != NULL) {
  692. if (strcasecmp(var, me->name) != 0) continue; // surely not this one
  693. if (me->beh == MB_ONOFF) {
  694. tmp = atoi(val);
  695. if (tmp) *(int *)me->var |= me->mask;
  696. else *(int *)me->var &= ~me->mask;
  697. return;
  698. } else if (me->beh == MB_RANGE) {
  699. tmp = atoi(val);
  700. if (tmp < me->min) tmp = me->min;
  701. if (tmp > me->max) tmp = me->max;
  702. *(int *)me->var = tmp;
  703. return;
  704. }
  705. }
  706. ret = custom_read(me, var, val);
  707. }
  708. }
  709. if (!ret) lprintf("config_readsect: unhandled var: %s\n", var);
  710. }
  711. int config_havesect(const char *fname, const char *section)
  712. {
  713. FILE *f;
  714. int ret;
  715. f = fopen(fname, "r");
  716. if (f == NULL) return 0;
  717. ret = seek_sect(f, section);
  718. fclose(f);
  719. return ret;
  720. }
  721. int config_readsect(const char *fname, const char *section)
  722. {
  723. char line[128], *var, *val, *tmp;
  724. int len, i, ret;
  725. FILE *f;
  726. f = fopen(fname, "r");
  727. if (f == NULL) return -1;
  728. if (section != NULL)
  729. {
  730. ret = seek_sect(f, section);
  731. if (!ret) {
  732. lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
  733. fclose(f);
  734. return -1;
  735. }
  736. }
  737. keys_encountered = 0;
  738. while (!feof(f))
  739. {
  740. tmp = fgets(line, sizeof(line), f);
  741. if (tmp == NULL) break;
  742. if (line[0] == '[') break; // other section
  743. // strip comments, linefeed, spaces..
  744. len = strlen(line);
  745. for (i = 0; i < len; i++)
  746. if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
  747. mystrip(line);
  748. len = strlen(line);
  749. if (len <= 0) continue;
  750. // get var and val
  751. for (i = 0; i < len; i++)
  752. if (line[i] == '=') break;
  753. if (i >= len || strchr(&line[i+1], '=') != NULL) {
  754. lprintf("config_readsect: can't parse: %s\n", line);
  755. continue;
  756. }
  757. line[i] = 0;
  758. var = line;
  759. val = &line[i+1];
  760. mystrip(var);
  761. mystrip(val);
  762. if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
  763. lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
  764. continue;
  765. }
  766. parse(var, val);
  767. }
  768. fclose(f);
  769. return 0;
  770. }