palcfg.c 26 KB

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