palcfg.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Copyright (c) 2011-2017, SDLPAL development team.
  5. // All rights reserved.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // SDLPAL is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. //
  22. // palcfg.c: Configuration definition.
  23. // @Author: Lou Yihua <louyihua@21cn.com>, 2016.
  24. //
  25. #include "global.h"
  26. #include "palcfg.h"
  27. #include "util.h"
  28. #include "resampler.h"
  29. #include <stdint.h>
  30. #if !defined(PAL_HAS_TOUCH)
  31. #define PAL_HAS_TOUCH 0
  32. #endif
  33. #define MAKE_VALUE(defv, minv, maxv) {(LPCSTR)(defv)}, {(LPCSTR)(minv)}, {(LPCSTR)(maxv)}
  34. static const ConfigItem gConfigItems[PALCFG_ALL_MAX] = {
  35. { PALCFG_FULLSCREEN, PALCFG_BOOLEAN, "FullScreen", 10, MAKE_VALUE(FALSE, FALSE, TRUE) },
  36. { PALCFG_KEEPASPECTRATIO, PALCFG_BOOLEAN, "KeepAspectRatio", 15, MAKE_VALUE(TRUE, FALSE, TRUE) },
  37. { PALCFG_LAUNCHSETTING, PALCFG_BOOLEAN, "LaunchSetting", 13, MAKE_VALUE(PAL_HAS_CONFIG_PAGE, FALSE, TRUE) },
  38. { PALCFG_STEREO, PALCFG_BOOLEAN, "Stereo", 6, MAKE_VALUE(TRUE, FALSE, TRUE) }, // Default for stereo audio
  39. { PALCFG_USESURROUNDOPL, PALCFG_BOOLEAN, "UseSurroundOPL", 14, MAKE_VALUE(TRUE, FALSE, TRUE) }, // Default for using surround opl
  40. { PALCFG_USETOUCHOVERLAY, PALCFG_BOOLEAN, "UseTouchOverlay", 15, MAKE_VALUE(PAL_HAS_TOUCH, FALSE, TRUE) },
  41. { PALCFG_ENABLEAVIPLAY, PALCFG_BOOLEAN, "EnableAviPlay", 13, MAKE_VALUE(TRUE, FALSE, TRUE) },
  42. { PALCFG_SURROUNDOPLOFFSET, PALCFG_INTEGER, "SurroundOPLOffset", 17, MAKE_VALUE(384, INT32_MIN, INT32_MAX) },
  43. { PALCFG_LOGLEVEL, PALCFG_INTEGER, "LogLevel", 8, MAKE_VALUE(PAL_DEFAULT_LOGLEVEL, LOGLEVEL_MIN, LOGLEVEL_MAX) },
  44. { PALCFG_AUDIOBUFFERSIZE, PALCFG_UNSIGNED, "AudioBufferSize", 15, MAKE_VALUE(PAL_AUDIO_DEFAULT_BUFFER_SIZE, 2, 32768) },
  45. { PALCFG_OPLSAMPLERATE, PALCFG_UNSIGNED, "OPLSampleRate", 13, MAKE_VALUE(49716, 0, UINT32_MAX) },
  46. { PALCFG_RESAMPLEQUALITY, PALCFG_UNSIGNED, "ResampleQuality", 15, MAKE_VALUE(RESAMPLER_QUALITY_MAX, RESAMPLER_QUALITY_MIN, RESAMPLER_QUALITY_MAX) }, // Default for best quality
  47. { PALCFG_SAMPLERATE, PALCFG_UNSIGNED, "SampleRate", 10, MAKE_VALUE(44100, 0, PAL_MAX_SAMPLERATE) },
  48. { PALCFG_MUSICVOLUME, PALCFG_UNSIGNED, "MusicVolume", 11, MAKE_VALUE(PAL_MAX_VOLUME, 0, PAL_MAX_VOLUME) }, // Default for maximum volume
  49. { PALCFG_SOUNDVOLUME, PALCFG_UNSIGNED, "SoundVolume", 11, MAKE_VALUE(PAL_MAX_VOLUME, 0, PAL_MAX_VOLUME) }, // Default for maximum volume
  50. { PALCFG_WINDOWHEIGHT, PALCFG_UNSIGNED, "WindowHeight", 12, MAKE_VALUE(PAL_DEFAULT_WINDOW_HEIGHT, 0, UINT32_MAX) },
  51. { PALCFG_WINDOWWIDTH, PALCFG_UNSIGNED, "WindowWidth", 11, MAKE_VALUE(PAL_DEFAULT_WINDOW_WIDTH, 0, UINT32_MAX) },
  52. { PALCFG_CD, PALCFG_STRING, "CD", 2, MAKE_VALUE("OGG", NULL, NULL) },
  53. { PALCFG_GAMEPATH, PALCFG_STRING, "GamePath", 8, MAKE_VALUE(NULL, NULL, NULL) },
  54. { PALCFG_SAVEPATH, PALCFG_STRING, "SavePath", 8, MAKE_VALUE(NULL, NULL, NULL) },
  55. { PALCFG_MESSAGEFILE, PALCFG_STRING, "MessageFileName", 15, MAKE_VALUE(NULL, NULL, NULL) },
  56. { PALCFG_FONTFILE, PALCFG_STRING, "FontFileName", 12, MAKE_VALUE(NULL, NULL, NULL) },
  57. { PALCFG_MUSIC, PALCFG_STRING, "Music", 5, MAKE_VALUE("RIX", NULL, NULL) },
  58. { PALCFG_OPL, PALCFG_STRING, "OPL", 3, MAKE_VALUE("DOSBOX", NULL, NULL) },
  59. { PALCFG_LOGFILE, PALCFG_STRING, "LogFileName", 11, MAKE_VALUE(NULL, NULL, NULL) },
  60. { PALCFG_RIXEXTRAINIT, PALCFG_STRING, "RIXExtraInit", 12, MAKE_VALUE(NULL, NULL, NULL) },
  61. { PALCFG_CLIMIDIPLAYER, PALCFG_STRING, "CLIMIDIPlayer", 13, MAKE_VALUE(NULL, NULL, NULL) },
  62. { PALCFG_SCALEQUALITY, PALCFG_STRING, "ScaleQuality", 12, MAKE_VALUE("0", NULL, NULL) },
  63. };
  64. static const char *music_types[] = { "MIDI", "RIX", "MP3", "OGG", "RAW" };
  65. static const char *opl_types[] = { "DOSBOX", "MAME", "DOSBOXNEW" };
  66. static char * ParseStringValue(const char *sValue, char *original)
  67. {
  68. int n = strlen(sValue);
  69. while (n > 0 && isspace(sValue[n - 1])) n--;
  70. if (n > 0)
  71. {
  72. char *newval = (char *)realloc(original, n + 1);
  73. memcpy(newval, sValue, n);
  74. newval[n] = '\0';
  75. return newval;
  76. }
  77. return original;
  78. }
  79. BOOL
  80. PAL_ParseConfigLine(
  81. const char * line,
  82. const ConfigItem ** ppItem,
  83. ConfigValue * pValue
  84. )
  85. {
  86. //
  87. // Skip leading spaces
  88. //
  89. while (*line && isspace(*line)) line++;
  90. //
  91. // Skip comments
  92. //
  93. if (*line && *line != '#')
  94. {
  95. const char *ptr;
  96. if ((ptr = strchr(line, '=')) != NULL)
  97. {
  98. const char *end = ptr++;
  99. //
  100. // Skip tailing spaces
  101. //
  102. while (end > line && isspace(end[-1])) end--;
  103. int len = end - line;
  104. for (int i = 0; i < sizeof(gConfigItems) / sizeof(ConfigItem); i++)
  105. {
  106. if (gConfigItems[i].NameLength == len &&
  107. SDL_strncasecmp(line, gConfigItems[i].Name, len) == 0)
  108. {
  109. if (ppItem) *ppItem = &gConfigItems[i];
  110. if (pValue)
  111. {
  112. switch (gConfigItems[i].Type)
  113. {
  114. case PALCFG_UNSIGNED:
  115. sscanf(ptr, "%u", &pValue->uValue);
  116. if (pValue->uValue < gConfigItems[i].MinValue.uValue)
  117. pValue->uValue = gConfigItems[i].MinValue.uValue;
  118. else if (pValue->uValue > gConfigItems[i].MaxValue.uValue)
  119. pValue->uValue = gConfigItems[i].MaxValue.uValue;
  120. break;
  121. case PALCFG_INTEGER:
  122. sscanf(ptr, "%d", &pValue->iValue);
  123. if (pValue->iValue < gConfigItems[i].MinValue.iValue)
  124. pValue->iValue = gConfigItems[i].MinValue.iValue;
  125. else if (pValue->iValue > gConfigItems[i].MaxValue.iValue)
  126. pValue->iValue = gConfigItems[i].MaxValue.iValue;
  127. break;
  128. case PALCFG_BOOLEAN:
  129. sscanf(ptr, "%d", &pValue->bValue);
  130. pValue->bValue = pValue->bValue ? TRUE : FALSE;
  131. break;
  132. case PALCFG_STRING:
  133. //
  134. // Skip leading spaces
  135. //
  136. while (*ptr && isspace(*ptr)) ptr++;
  137. pValue->sValue = ptr;
  138. break;
  139. }
  140. return TRUE;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. return FALSE;
  147. }
  148. const char *
  149. PAL_ConfigName(
  150. PALCFG_ITEM item
  151. )
  152. {
  153. return gConfigItems[item].Name;
  154. }
  155. PALCFG_ITEM
  156. PAL_ConfigIndex(
  157. const char *name
  158. )
  159. {
  160. for (int index = PALCFG_ALL_MIN; index < PALCFG_ALL_MAX; index++)
  161. {
  162. if (SDL_strcasecmp(gConfigItems[index].Name, name) == 0)
  163. {
  164. return index;
  165. }
  166. }
  167. return -1;
  168. }
  169. PALCFG_TYPE
  170. PAL_ConfigType(
  171. PALCFG_ITEM item
  172. )
  173. {
  174. return gConfigItems[item].Type;
  175. }
  176. BOOL
  177. PAL_LimitConfig(
  178. PALCFG_ITEM item,
  179. ConfigValue * pValue
  180. )
  181. {
  182. if (!pValue) return FALSE;
  183. switch (gConfigItems[item].Type)
  184. {
  185. case PALCFG_UNSIGNED:
  186. if (pValue->uValue < gConfigItems[item].MinValue.uValue)
  187. {
  188. pValue->uValue = gConfigItems[item].MinValue.uValue;
  189. return TRUE;
  190. }
  191. else if (pValue->uValue > gConfigItems[item].MaxValue.uValue)
  192. {
  193. pValue->uValue = gConfigItems[item].MaxValue.uValue;
  194. return TRUE;
  195. }
  196. else
  197. return FALSE;
  198. case PALCFG_INTEGER:
  199. if (pValue->iValue < gConfigItems[item].MinValue.iValue)
  200. {
  201. pValue->iValue = gConfigItems[item].MinValue.iValue;
  202. return TRUE;
  203. }
  204. else if (pValue->iValue > gConfigItems[item].MaxValue.iValue)
  205. {
  206. pValue->iValue = gConfigItems[item].MaxValue.iValue;
  207. return TRUE;
  208. }
  209. else
  210. return FALSE;
  211. case PALCFG_BOOLEAN:
  212. if (pValue->bValue != TRUE && pValue->bValue != FALSE)
  213. {
  214. pValue->bValue = pValue->bValue ? TRUE : FALSE;
  215. return TRUE;
  216. }
  217. else
  218. return FALSE;
  219. default:
  220. return FALSE;
  221. }
  222. }
  223. void
  224. PAL_FreeConfig(
  225. void
  226. )
  227. {
  228. #if USE_RIX_EXTRA_INIT
  229. free(gConfig.pExtraFMRegs);
  230. free(gConfig.pExtraFMVals);
  231. free(gConfig.dwExtraLength);
  232. #endif
  233. free(gConfig.pszMsgFile);
  234. free(gConfig.pszFontFile);
  235. free(gConfig.pszGamePath);
  236. free(gConfig.pszSavePath);
  237. free(gConfig.pszScaleQuality);
  238. free(gConfig.pszLogFile);
  239. memset(&gConfig, 0, sizeof(CONFIGURATION));
  240. }
  241. void
  242. PAL_LoadConfig(
  243. BOOL fFromFile
  244. )
  245. {
  246. FILE *fp;
  247. ConfigValue values[PALCFG_ALL_MAX];
  248. MUSICTYPE eMusicType = MUSIC_RIX;
  249. MUSICTYPE eCDType = MUSIC_OGG;
  250. OPLTYPE eOPLType = OPL_DOSBOX;
  251. SCREENLAYOUT screen_layout = {
  252. // Equipment Screen
  253. PAL_XY(8, 8), PAL_XY(2, 95), PAL_XY(5, 70), PAL_XY(51, 57),
  254. { PAL_XY(92, 11), PAL_XY(92, 33), PAL_XY(92, 55), PAL_XY(92, 77), PAL_XY(92, 99), PAL_XY(92, 121) },
  255. { PAL_XY(130, 11), PAL_XY(130, 33), PAL_XY(130, 55), PAL_XY(130, 77), PAL_XY(130, 99), PAL_XY(130, 121) },
  256. { PAL_XY(226, 10), PAL_XY(226, 32), PAL_XY(226, 54), PAL_XY(226, 76), PAL_XY(226, 98) },
  257. { PAL_XY(260, 14), PAL_XY(260, 36), PAL_XY(260, 58), PAL_XY(260, 80), PAL_XY(260, 102) },
  258. // Status Screen
  259. PAL_XY(110, 8), PAL_XY(110, 30), PAL_XY(6, 6), PAL_XY(6, 32), PAL_XY(6, 54), PAL_XY(6, 76),
  260. { PAL_XY(6, 98), PAL_XY(6, 118), PAL_XY(6, 138), PAL_XY(6, 158), PAL_XY(6, 178) },
  261. PAL_XY(58, 6), PAL_XY(58, 15), PAL_XY(0, 0), PAL_XY(54, 35), PAL_XY(42, 56),
  262. PAL_XY(63, 61), PAL_XY(65, 58), PAL_XY(42, 78), PAL_XY(63, 83), PAL_XY(65, 80),
  263. { PAL_XY(42, 102), PAL_XY(42, 122), PAL_XY(42, 142), PAL_XY(42, 162), PAL_XY(42, 182) },
  264. { PAL_XY(189, -1), PAL_XY(247, 39), PAL_XY(251, 101), PAL_XY(201, 133), PAL_XY(141, 141), PAL_XY(81, 125) },
  265. { PAL_XY(195, 38), PAL_XY(253, 78), PAL_XY(257, 140), PAL_XY(207, 172), PAL_XY(147, 180), PAL_XY(87, 164) },
  266. { PAL_XY(185, 58), PAL_XY(185, 76), PAL_XY(185, 94), PAL_XY(185, 112), PAL_XY(185, 130), PAL_XY(185, 148), PAL_XY(185, 166), PAL_XY(185, 184), PAL_XY(185, 184), PAL_XY(185, 184) },
  267. // Extra Lines
  268. PAL_XY(0, 0), PAL_XY(0, 0)
  269. };
  270. for (PALCFG_ITEM i = PALCFG_ALL_MIN; i < PALCFG_ALL_MAX; i++) values[i] = gConfigItems[i].DefaultValue;
  271. if (fFromFile && (fp = UTIL_OpenFileAtPathForMode(PAL_CONFIG_PREFIX, "sdlpal.cfg", "r")))
  272. {
  273. PAL_LARGE char buf[512];
  274. //
  275. // Load the configuration data
  276. //
  277. while (fgets(buf, 512, fp) != NULL)
  278. {
  279. ConfigValue value;
  280. const ConfigItem * item;
  281. if (PAL_ParseConfigLine(buf, &item, &value))
  282. {
  283. switch (item->Item)
  284. {
  285. case PALCFG_AUDIOBUFFERSIZE:
  286. if ((value.uValue & (value.uValue - 1)) != 0)
  287. {
  288. /* Make sure iAudioBufferSize is power of 2 */
  289. int n = 0;
  290. while (value.uValue) { value.uValue >>= 1; n++; }
  291. value.uValue = 1 << (n - 1);
  292. }
  293. values[item->Item] = value;
  294. break;
  295. case PALCFG_MESSAGEFILE:
  296. gConfig.pszMsgFile = ParseStringValue(value.sValue, gConfig.pszMsgFile);
  297. break;
  298. case PALCFG_FONTFILE:
  299. gConfig.pszFontFile = ParseStringValue(value.sValue, gConfig.pszFontFile);
  300. break;
  301. case PALCFG_GAMEPATH:
  302. gConfig.pszGamePath = ParseStringValue(value.sValue, gConfig.pszGamePath);
  303. break;
  304. case PALCFG_SAVEPATH:
  305. gConfig.pszSavePath = ParseStringValue(value.sValue, gConfig.pszSavePath);
  306. break;
  307. case PALCFG_LOGFILE:
  308. gConfig.pszLogFile = ParseStringValue(value.sValue, gConfig.pszLogFile);
  309. break;
  310. case PALCFG_CD:
  311. {
  312. if (PAL_HAS_MP3 && SDL_strncasecmp(value.sValue, "MP3", 3) == 0)
  313. eCDType = MUSIC_MP3;
  314. else if (PAL_HAS_OGG && SDL_strncasecmp(value.sValue, "OGG", 3) == 0)
  315. eCDType = MUSIC_OGG;
  316. else if (PAL_HAS_SDLCD && SDL_strncasecmp(value.sValue, "RAW", 3) == 0)
  317. eCDType = MUSIC_SDLCD;
  318. break;
  319. }
  320. case PALCFG_MUSIC:
  321. {
  322. if (PAL_HAS_NATIVEMIDI && SDL_strncasecmp(value.sValue, "MIDI", 4) == 0)
  323. eMusicType = MUSIC_MIDI;
  324. else if (PAL_HAS_MP3 && SDL_strncasecmp(value.sValue, "MP3", 3) == 0)
  325. eMusicType = MUSIC_MP3;
  326. else if (PAL_HAS_OGG && SDL_strncasecmp(value.sValue, "OGG", 3) == 0)
  327. eMusicType = MUSIC_OGG;
  328. else if (SDL_strncasecmp(value.sValue, "RIX", 3) == 0)
  329. eMusicType = MUSIC_RIX;
  330. break;
  331. }
  332. case PALCFG_OPL:
  333. {
  334. if (SDL_strncasecmp(value.sValue, "DOSBOXNEW", 9) == 0)
  335. eOPLType = OPL_DOSBOX_NEW;
  336. else if (SDL_strncasecmp(value.sValue, "DOSBOX", 6) == 0)
  337. eOPLType = OPL_DOSBOX;
  338. else if (SDL_strncasecmp(value.sValue, "MAME", 4) == 0)
  339. eOPLType = OPL_MAME;
  340. break;
  341. }
  342. case PALCFG_RIXEXTRAINIT:
  343. {
  344. #if USE_RIX_EXTRA_INIT
  345. int n = 1;
  346. char *p;
  347. for (p = ptr; *p < *end; p++)
  348. {
  349. if (*p == ',')
  350. n++;
  351. }
  352. n &= ~0x1;
  353. if (n > 0)
  354. {
  355. uint32_t *regs = malloc(sizeof(uint32_t) * (n >> 1));
  356. uint8_t *vals = malloc(sizeof(uint8_t) * (n >> 1));
  357. uint32_t d, i, v = 1;
  358. if (regs && vals)
  359. {
  360. for (p = ptr, i = 0; *p < *end; p++, i++)
  361. {
  362. if (sscanf(p, "%u", &regs[i]) == 0) { v = 0; break; }
  363. while (*p < *end && *p != ',') p++; p++;
  364. if (sscanf(p, "%u", &d) == 0) { v = 0; break; }
  365. while (*p < *end && *p != ',') p++;
  366. vals[i] = (uint8_t)d;
  367. }
  368. if (v)
  369. {
  370. gConfig.pExtraFMRegs = regs;
  371. gConfig.pExtraFMVals = vals;
  372. gConfig.dwExtraLength = n >> 1;
  373. }
  374. else
  375. {
  376. free(regs);
  377. free(vals);
  378. }
  379. }
  380. }
  381. #endif
  382. break;
  383. }
  384. case PALCFG_CLIMIDIPLAYER:
  385. gConfig.pszCLIMIDIPlayerPath = ParseStringValue(value.sValue, gConfig.pszCLIMIDIPlayerPath);
  386. break;
  387. case PALCFG_SCALEQUALITY:
  388. gConfig.pszScaleQuality = ParseStringValue(value.sValue, gConfig.pszScaleQuality);
  389. break;
  390. default:
  391. values[item->Item] = value;
  392. break;
  393. }
  394. }
  395. }
  396. UTIL_CloseFile(fp);
  397. }
  398. //
  399. // Set configurable global options
  400. //
  401. if (!gConfig.pszSavePath) gConfig.pszSavePath = gConfig.pszGamePath ? strdup(gConfig.pszGamePath) : strdup(PAL_SAVE_PREFIX);
  402. if (!gConfig.pszGamePath) gConfig.pszGamePath = strdup(PAL_PREFIX);
  403. gConfig.eMusicType = eMusicType;
  404. gConfig.eCDType = eCDType;
  405. gConfig.eOPLType = eOPLType;
  406. gConfig.dwWordLength = 10; // This is the default value for Chinese version
  407. gConfig.ScreenLayout = screen_layout;
  408. gConfig.fIsWIN95 = FALSE; // Default for DOS version
  409. gConfig.fUseSurroundOPL = values[PALCFG_STEREO].bValue && values[PALCFG_USESURROUNDOPL].bValue;
  410. gConfig.fLaunchSetting = values[PALCFG_LAUNCHSETTING].bValue;
  411. gConfig.fUseTouchOverlay = values[PALCFG_USETOUCHOVERLAY].bValue;
  412. gConfig.fKeepAspectRatio = values[PALCFG_KEEPASPECTRATIO].bValue;
  413. gConfig.fFullScreen = values[PALCFG_FULLSCREEN].bValue;
  414. gConfig.fEnableAviPlay = values[PALCFG_ENABLEAVIPLAY].bValue;
  415. gConfig.iAudioChannels = values[PALCFG_STEREO].bValue ? 2 : 1;
  416. gConfig.iSurroundOPLOffset = values[PALCFG_SURROUNDOPLOFFSET].iValue;
  417. gConfig.iLogLevel = values[PALCFG_LOGLEVEL].iValue;
  418. gConfig.iSampleRate = values[PALCFG_SAMPLERATE].uValue;
  419. gConfig.iOPLSampleRate = values[PALCFG_OPLSAMPLERATE].uValue;
  420. gConfig.iResampleQuality = values[PALCFG_RESAMPLEQUALITY].uValue;
  421. gConfig.wAudioBufferSize = (WORD)values[PALCFG_AUDIOBUFFERSIZE].uValue;
  422. gConfig.iMusicVolume = values[PALCFG_MUSICVOLUME].uValue;
  423. gConfig.iSoundVolume = values[PALCFG_SOUNDVOLUME].uValue;
  424. if (UTIL_GetScreenSize(&values[PALCFG_WINDOWWIDTH].uValue, &values[PALCFG_WINDOWHEIGHT].uValue))
  425. {
  426. gConfig.dwScreenWidth = values[PALCFG_WINDOWWIDTH].uValue;
  427. gConfig.dwScreenHeight = values[PALCFG_WINDOWHEIGHT].uValue;
  428. }
  429. else
  430. {
  431. gConfig.dwScreenWidth = PAL_DEFAULT_WINDOW_WIDTH;
  432. gConfig.dwScreenHeight = PAL_DEFAULT_WINDOW_HEIGHT;
  433. }
  434. }
  435. BOOL
  436. PAL_SaveConfig(
  437. void
  438. )
  439. {
  440. char buf[512];
  441. FILE *fp = UTIL_OpenFileAtPathForMode(PAL_CONFIG_PREFIX, "sdlpal.cfg", "w");
  442. if (fp)
  443. {
  444. sprintf(buf, "%s=%d\n", PAL_ConfigName(PALCFG_KEEPASPECTRATIO), gConfig.fKeepAspectRatio); fputs(buf, fp);
  445. sprintf(buf, "%s=%d\n", PAL_ConfigName(PALCFG_FULLSCREEN), gConfig.fFullScreen); fputs(buf, fp);
  446. sprintf(buf, "%s=%d\n", PAL_ConfigName(PALCFG_LAUNCHSETTING), gConfig.fLaunchSetting); fputs(buf, fp);
  447. sprintf(buf, "%s=%d\n", PAL_ConfigName(PALCFG_STEREO), gConfig.iAudioChannels == 2 ? TRUE : FALSE); fputs(buf, fp);
  448. sprintf(buf, "%s=%d\n", PAL_ConfigName(PALCFG_USESURROUNDOPL), gConfig.fUseSurroundOPL); fputs(buf, fp);
  449. sprintf(buf, "%s=%d\n", PAL_ConfigName(PALCFG_USETOUCHOVERLAY), gConfig.fUseTouchOverlay); fputs(buf, fp);
  450. sprintf(buf, "%s=%d\n", PAL_ConfigName(PALCFG_ENABLEAVIPLAY), gConfig.fEnableAviPlay); fputs(buf, fp);
  451. sprintf(buf, "%s=%d\n", PAL_ConfigName(PALCFG_SURROUNDOPLOFFSET), gConfig.iSurroundOPLOffset); fputs(buf, fp);
  452. sprintf(buf, "%s=%d\n", PAL_ConfigName(PALCFG_LOGLEVEL), gConfig.iLogLevel); fputs(buf, fp);
  453. sprintf(buf, "%s=%u\n", PAL_ConfigName(PALCFG_AUDIOBUFFERSIZE), gConfig.wAudioBufferSize); fputs(buf, fp);
  454. sprintf(buf, "%s=%u\n", PAL_ConfigName(PALCFG_OPLSAMPLERATE), gConfig.iOPLSampleRate); fputs(buf, fp);
  455. sprintf(buf, "%s=%u\n", PAL_ConfigName(PALCFG_RESAMPLEQUALITY), gConfig.iResampleQuality); fputs(buf, fp);
  456. sprintf(buf, "%s=%u\n", PAL_ConfigName(PALCFG_SAMPLERATE), gConfig.iSampleRate); fputs(buf, fp);
  457. sprintf(buf, "%s=%u\n", PAL_ConfigName(PALCFG_MUSICVOLUME), gConfig.iMusicVolume); fputs(buf, fp);
  458. sprintf(buf, "%s=%u\n", PAL_ConfigName(PALCFG_SOUNDVOLUME), gConfig.iSoundVolume); fputs(buf, fp);
  459. sprintf(buf, "%s=%u\n", PAL_ConfigName(PALCFG_WINDOWHEIGHT), gConfig.dwScreenHeight); fputs(buf, fp);
  460. sprintf(buf, "%s=%u\n", PAL_ConfigName(PALCFG_WINDOWWIDTH), gConfig.dwScreenWidth); fputs(buf, fp);
  461. sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_CD), music_types[gConfig.eCDType]); fputs(buf, fp);
  462. sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_MUSIC), music_types[gConfig.eMusicType]); fputs(buf, fp);
  463. sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_OPL), opl_types[gConfig.eOPLType]); fputs(buf, fp);
  464. if (gConfig.pszGamePath && *gConfig.pszGamePath && strcmp(gConfig.pszGamePath, PAL_PREFIX) != 0) { sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_GAMEPATH), gConfig.pszGamePath); fputs(buf, fp); }
  465. if (gConfig.pszSavePath && *gConfig.pszSavePath && strcmp(gConfig.pszSavePath, PAL_SAVE_PREFIX) != 0) { sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_SAVEPATH), gConfig.pszSavePath); fputs(buf, fp); }
  466. if (gConfig.pszMsgFile && *gConfig.pszMsgFile) { sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_MESSAGEFILE), gConfig.pszMsgFile); fputs(buf, fp); }
  467. if (gConfig.pszFontFile && *gConfig.pszFontFile) { sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_FONTFILE), gConfig.pszFontFile); fputs(buf, fp); }
  468. if (gConfig.pszLogFile && *gConfig.pszLogFile) { sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_LOGFILE), gConfig.pszLogFile); fputs(buf, fp); }
  469. if (gConfig.pszCLIMIDIPlayerPath && *gConfig.pszCLIMIDIPlayerPath) { sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_CLIMIDIPLAYER), gConfig.pszCLIMIDIPlayerPath); fputs(buf, fp); }
  470. if (gConfig.pszScaleQuality && *gConfig.pszScaleQuality) { sprintf(buf, "%s=%s\n", PAL_ConfigName(PALCFG_SCALEQUALITY), gConfig.pszScaleQuality); fputs(buf, fp); }
  471. fclose(fp);
  472. return TRUE;
  473. }
  474. else
  475. return FALSE;
  476. }
  477. ConfigValue
  478. PAL_GetConfigItem(
  479. PALCFG_ITEM item,
  480. BOOL default_value
  481. )
  482. {
  483. ConfigValue value = gConfigItems[item].DefaultValue;
  484. if (!default_value)
  485. {
  486. switch (item)
  487. {
  488. case PALCFG_FULLSCREEN: value.bValue = gConfig.fFullScreen; break;
  489. case PALCFG_KEEPASPECTRATIO: value.bValue = gConfig.fKeepAspectRatio; break;
  490. case PALCFG_LAUNCHSETTING: value.bValue = gConfig.fLaunchSetting; break;
  491. case PALCFG_STEREO: value.bValue = (gConfig.iAudioChannels == 2); break;
  492. case PALCFG_USESURROUNDOPL: value.bValue = gConfig.fUseSurroundOPL; break;
  493. case PALCFG_USETOUCHOVERLAY: value.bValue = gConfig.fUseTouchOverlay; break;
  494. case PALCFG_ENABLEAVIPLAY: value.bValue = gConfig.fEnableAviPlay; break;
  495. case PALCFG_SURROUNDOPLOFFSET: value.iValue = gConfig.iSurroundOPLOffset; break;
  496. case PALCFG_LOGLEVEL: value.iValue = gConfig.iLogLevel; break;
  497. case PALCFG_AUDIOBUFFERSIZE: value.uValue = gConfig.wAudioBufferSize; break;
  498. case PALCFG_OPLSAMPLERATE: value.uValue = gConfig.iOPLSampleRate; break;
  499. case PALCFG_RESAMPLEQUALITY: value.uValue = gConfig.iResampleQuality; break;
  500. case PALCFG_SAMPLERATE: value.uValue = gConfig.iSampleRate; break;
  501. case PALCFG_MUSICVOLUME: value.uValue = gConfig.iMusicVolume; break;
  502. case PALCFG_SOUNDVOLUME: value.uValue = gConfig.iSoundVolume; break;
  503. case PALCFG_WINDOWHEIGHT: value.uValue = gConfig.dwScreenHeight; break;
  504. case PALCFG_WINDOWWIDTH: value.uValue = gConfig.dwScreenWidth; break;
  505. case PALCFG_CD: value.sValue = music_types[gConfig.eCDType]; break;
  506. case PALCFG_GAMEPATH: value.sValue = gConfig.pszGamePath; break;
  507. case PALCFG_SAVEPATH: value.sValue = gConfig.pszSavePath; break;
  508. case PALCFG_MESSAGEFILE: value.sValue = gConfig.pszMsgFile; break;
  509. case PALCFG_FONTFILE: value.sValue = gConfig.pszFontFile; break;
  510. case PALCFG_LOGFILE: value.sValue = gConfig.pszLogFile; break;
  511. case PALCFG_CLIMIDIPLAYER: value.sValue = gConfig.pszCLIMIDIPlayerPath; break;
  512. case PALCFG_SCALEQUALITY: value.sValue = gConfig.pszScaleQuality; break;
  513. case PALCFG_MUSIC: value.sValue = music_types[gConfig.eMusicType]; break;
  514. case PALCFG_OPL: value.sValue = opl_types[gConfig.eOPLType]; break;
  515. default: break;
  516. }
  517. }
  518. return value;
  519. }
  520. void
  521. PAL_SetConfigItem(
  522. PALCFG_ITEM item,
  523. const ConfigValue value
  524. )
  525. {
  526. switch (item)
  527. {
  528. case PALCFG_FULLSCREEN: gConfig.fFullScreen = value.bValue; break;
  529. case PALCFG_KEEPASPECTRATIO: gConfig.fKeepAspectRatio = value.bValue; break;
  530. case PALCFG_LAUNCHSETTING: gConfig.fLaunchSetting = value.bValue; break;
  531. case PALCFG_STEREO: gConfig.iAudioChannels = value.bValue ? 2 : 1; break;
  532. case PALCFG_USESURROUNDOPL: gConfig.fUseSurroundOPL = value.bValue; break;
  533. case PALCFG_USETOUCHOVERLAY: gConfig.fUseTouchOverlay = value.bValue; break;
  534. case PALCFG_ENABLEAVIPLAY: gConfig.fEnableAviPlay = value.bValue; break;
  535. case PALCFG_SURROUNDOPLOFFSET: gConfig.iSurroundOPLOffset = value.iValue; break;
  536. case PALCFG_LOGLEVEL: gConfig.iLogLevel = value.iValue; break;
  537. case PALCFG_AUDIOBUFFERSIZE: gConfig.wAudioBufferSize = value.uValue; break;
  538. case PALCFG_OPLSAMPLERATE: gConfig.iOPLSampleRate = value.uValue; break;
  539. case PALCFG_RESAMPLEQUALITY: gConfig.iResampleQuality = value.uValue; break;
  540. case PALCFG_SAMPLERATE: gConfig.iSampleRate = value.uValue; break;
  541. case PALCFG_MUSICVOLUME: gConfig.iMusicVolume = value.uValue; break;
  542. case PALCFG_SOUNDVOLUME: gConfig.iSoundVolume = value.uValue; break;
  543. case PALCFG_WINDOWHEIGHT: gConfig.dwScreenHeight = value.uValue; break;
  544. case PALCFG_WINDOWWIDTH: gConfig.dwScreenWidth = value.uValue; break;
  545. case PALCFG_GAMEPATH:
  546. if (gConfig.pszGamePath) free(gConfig.pszGamePath);
  547. gConfig.pszGamePath = value.sValue && value.sValue[0] ? strdup(value.sValue) : strdup(PAL_PREFIX);
  548. break;
  549. case PALCFG_SAVEPATH:
  550. if (gConfig.pszSavePath) free(gConfig.pszSavePath);
  551. gConfig.pszSavePath = value.sValue && value.sValue[0] ? strdup(value.sValue) : (gConfig.pszGamePath ? strdup(gConfig.pszGamePath) : strdup(PAL_SAVE_PREFIX));
  552. break;
  553. case PALCFG_MESSAGEFILE:
  554. if (gConfig.pszMsgFile) free(gConfig.pszMsgFile);
  555. gConfig.pszMsgFile = value.sValue && value.sValue[0] ? strdup(value.sValue) : NULL;
  556. break;
  557. case PALCFG_FONTFILE:
  558. if (gConfig.pszFontFile) free(gConfig.pszFontFile);
  559. gConfig.pszFontFile = value.sValue && value.sValue[0] ? strdup(value.sValue) : NULL;
  560. break;
  561. case PALCFG_LOGFILE:
  562. if (gConfig.pszLogFile) free(gConfig.pszLogFile);
  563. gConfig.pszLogFile = value.sValue && value.sValue[0] ? strdup(value.sValue) : NULL;
  564. break;
  565. case PALCFG_CLIMIDIPLAYER:
  566. if (gConfig.pszCLIMIDIPlayerPath) free(gConfig.pszCLIMIDIPlayerPath);
  567. gConfig.pszCLIMIDIPlayerPath = value.sValue && value.sValue[0] ? strdup(value.sValue) : NULL;
  568. break;
  569. case PALCFG_SCALEQUALITY:
  570. if (gConfig.pszScaleQuality) free(gConfig.pszScaleQuality);
  571. gConfig.pszScaleQuality = value.sValue && value.sValue[0] ? strdup(value.sValue) : NULL;
  572. break;
  573. case PALCFG_CD:
  574. for (int i = 0; i < sizeof(music_types) / sizeof(music_types[0]); i++)
  575. {
  576. if (SDL_strcasecmp(value.sValue, music_types[i]) == 0)
  577. {
  578. gConfig.eCDType = (MUSICTYPE)i;
  579. return;
  580. }
  581. }
  582. break;
  583. case PALCFG_MUSIC:
  584. for (int i = 0; i < sizeof(music_types) / sizeof(music_types[0]); i++)
  585. {
  586. if (SDL_strcasecmp(value.sValue, music_types[i]) == 0)
  587. {
  588. gConfig.eMusicType = (MUSICTYPE)i;
  589. return;
  590. }
  591. }
  592. break;
  593. case PALCFG_OPL:
  594. for (int i = 0; i < sizeof(opl_types) / sizeof(opl_types[0]); i++)
  595. {
  596. if (SDL_strcasecmp(value.sValue, opl_types[i]) == 0)
  597. {
  598. gConfig.eOPLType = (OPLTYPE)i;
  599. return;
  600. }
  601. }
  602. break;
  603. default:
  604. break;
  605. }
  606. }
  607. BOOL
  608. PAL_GetConfigBoolean(
  609. PALCFG_ITEM item,
  610. BOOL default_value
  611. )
  612. {
  613. return gConfigItems[item].Type == PALCFG_BOOLEAN ? PAL_GetConfigItem(item, default_value).bValue : FALSE;
  614. }
  615. long
  616. PAL_GetConfigNumber(
  617. PALCFG_ITEM item,
  618. BOOL default_value
  619. )
  620. {
  621. switch (gConfigItems[item].Type)
  622. {
  623. case PALCFG_INTEGER: return (long)PAL_GetConfigItem(item, default_value).iValue;
  624. case PALCFG_UNSIGNED: return (long)PAL_GetConfigItem(item, default_value).uValue;
  625. default: return 0;
  626. }
  627. }
  628. int
  629. PAL_GetConfigInteger(
  630. PALCFG_ITEM item,
  631. BOOL default_value
  632. )
  633. {
  634. return gConfigItems[item].Type == PALCFG_INTEGER ? PAL_GetConfigItem(item, default_value).iValue : 0;
  635. }
  636. unsigned int
  637. PAL_GetConfigUnsigned(
  638. PALCFG_ITEM item,
  639. BOOL default_value
  640. )
  641. {
  642. return gConfigItems[item].Type == PALCFG_UNSIGNED ? PAL_GetConfigItem(item, default_value).uValue : 0;
  643. }
  644. const char *
  645. PAL_GetConfigString(
  646. PALCFG_ITEM item,
  647. BOOL default_value
  648. )
  649. {
  650. return gConfigItems[item].Type == PALCFG_STRING ? PAL_GetConfigItem(item, default_value).sValue : NULL;
  651. }
  652. BOOL
  653. PAL_SetConfigBoolean(
  654. PALCFG_ITEM item,
  655. BOOL value
  656. )
  657. {
  658. if (gConfigItems[item].Type == PALCFG_BOOLEAN)
  659. {
  660. ConfigValue val = { (const char *)(intptr_t)value };
  661. PAL_SetConfigItem(item, val);
  662. return TRUE;
  663. }
  664. else
  665. {
  666. return FALSE;
  667. }
  668. }
  669. BOOL
  670. PAL_SetConfigNumber(
  671. PALCFG_ITEM item,
  672. long value
  673. )
  674. {
  675. if (gConfigItems[item].Type == PALCFG_INTEGER || gConfigItems[item].Type == PALCFG_UNSIGNED)
  676. {
  677. ConfigValue val = { (const char *)(intptr_t)value };
  678. PAL_SetConfigItem(item, val);
  679. return TRUE;
  680. }
  681. else
  682. {
  683. return FALSE;
  684. }
  685. }
  686. BOOL
  687. PAL_SetConfigInteger(
  688. PALCFG_ITEM item,
  689. int value
  690. )
  691. {
  692. if (gConfigItems[item].Type == PALCFG_INTEGER)
  693. {
  694. ConfigValue val = { (const char *)(intptr_t)value };
  695. PAL_SetConfigItem(item, val);
  696. return TRUE;
  697. }
  698. else
  699. {
  700. return FALSE;
  701. }
  702. }
  703. BOOL
  704. PAL_SetConfigUnsigned(
  705. PALCFG_ITEM item,
  706. unsigned int value
  707. )
  708. {
  709. if (gConfigItems[item].Type == PALCFG_UNSIGNED)
  710. {
  711. ConfigValue val = { (const char *)(intptr_t)value };
  712. PAL_SetConfigItem(item, val);
  713. return TRUE;
  714. }
  715. else
  716. {
  717. return FALSE;
  718. }
  719. }
  720. BOOL
  721. PAL_SetConfigString(
  722. PALCFG_ITEM item,
  723. const char *value
  724. )
  725. {
  726. if (gConfigItems[item].Type == PALCFG_STRING)
  727. {
  728. ConfigValue val = { value };
  729. PAL_SetConfigItem(item, val);
  730. return TRUE;
  731. }
  732. else
  733. {
  734. return FALSE;
  735. }
  736. }