win32.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. // win32.cpp: WIN32-specific codes.
  23. // @Author: Lou Yihua <louyihua@21cn.com>, 2016.
  24. //
  25. #define UNICODE
  26. #define _UNICODE
  27. #define _CRT_SECURE_NO_WARNINGS
  28. #include <tchar.h>
  29. #include <windows.h>
  30. #include <commctrl.h>
  31. #include <shlobj.h>
  32. #include <string>
  33. #include "resource.h"
  34. #include "../global.h"
  35. #include "../util.h"
  36. #include "../palcfg.h"
  37. #include "../resampler.h"
  38. #define ComboBox_AddString(hwndDlg, idCtrl, lpsz) \
  39. (BOOL)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), CB_ADDSTRING, (WPARAM)(0), (LPARAM)(lpsz))
  40. #define ComboBox_SetCurSel(hwndDlg, idCtrl, index) \
  41. (BOOL)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), CB_SETCURSEL, (WPARAM)(index), (LPARAM)(0))
  42. #define ComboBox_GetCurSel(hwndDlg, idCtrl) \
  43. (int)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), CB_GETCURSEL, (WPARAM)(0), (LPARAM)(0))
  44. #define TrackBar_SetRange(hwndDlg, idCtrl, min, max, redraw) \
  45. (BOOL)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), TBM_SETRANGE, (WPARAM)(redraw), (LPARAM)(MAKELONG((min), (max))))
  46. #define TrackBar_SetPos(hwndDlg, idCtrl, pos, redraw) \
  47. (BOOL)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), TBM_SETPOS, (WPARAM)(redraw), (LPARAM)(pos))
  48. #define TrackBar_GetPos(hwndDlg, idCtrl) \
  49. (int)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), TBM_GETPOS, (WPARAM)(0), (LPARAM)(0))
  50. #define EnableDlgItem(hwnd, nIDControl, bEnable) \
  51. EnableWindow(GetDlgItem((hwnd), (nIDControl)), (bEnable))
  52. HINSTANCE g_hInstance;
  53. WORD g_wLanguage;
  54. int WINAPI LoadStringEx(
  55. HINSTANCE hInstance,
  56. UINT uID,
  57. LANGID wLang,
  58. LPTSTR lpBuffer,
  59. int nBufferMax
  60. )
  61. {
  62. auto hrc = FindResourceEx(hInstance, RT_STRING, MAKEINTRESOURCE((uID >> 4) + 1), wLang);
  63. if (nullptr == hrc) return 0;
  64. auto begin = (LPCWSTR)LockResource(LoadResource(hInstance, hrc));
  65. for (int idx = 0; idx < (int)(uID & 0xf); idx++)
  66. begin += *begin + 1;
  67. if (nBufferMax == 0)
  68. {
  69. *((LPCWSTR*)lpBuffer) = begin;
  70. return sizeof(LPCWSTR);
  71. }
  72. else
  73. {
  74. wcsncpy(lpBuffer, begin + 1, min(nBufferMax, *begin));
  75. if (nBufferMax <= *begin)
  76. {
  77. lpBuffer[nBufferMax - 1] = '\0';
  78. return nBufferMax - 1;
  79. }
  80. else
  81. {
  82. lpBuffer[*begin] = '\0';
  83. return *begin;
  84. }
  85. }
  86. }
  87. void SaveSettings(HWND hwndDlg, BOOL fWriteFile)
  88. {
  89. int textLen;
  90. if (IsDlgButtonChecked(hwndDlg, IDC_CHS))
  91. gConfig.uCodePage = CP_GBK;
  92. else
  93. gConfig.uCodePage = CP_BIG5;
  94. if ((textLen = GetWindowTextLengthA(GetDlgItem(hwndDlg, IDC_MSGFILE))) > 0)
  95. {
  96. gConfig.pszMsgFile = (char*)realloc(gConfig.pszMsgFile, textLen + 1);
  97. GetDlgItemTextA(hwndDlg, IDC_MSGFILE, gConfig.pszMsgFile, textLen + 1);
  98. }
  99. else
  100. {
  101. free(gConfig.pszMsgFile); gConfig.pszMsgFile = nullptr;
  102. }
  103. if ((textLen = GetWindowTextLength(GetDlgItem(hwndDlg, IDC_GAMEPATH))) > 0)
  104. {
  105. gConfig.pszGamePath = (char*)realloc(gConfig.pszGamePath, textLen + 1);
  106. GetDlgItemTextA(hwndDlg, IDC_GAMEPATH, gConfig.pszGamePath, textLen + 1);
  107. }
  108. else
  109. {
  110. free(gConfig.pszGamePath); gConfig.pszGamePath = nullptr;
  111. }
  112. gConfig.fFullScreen = IsDlgButtonChecked(hwndDlg, IDC_FULLSCREEN);
  113. gConfig.fUseTouchOverlay = IsDlgButtonChecked(hwndDlg, IDC_TOUCHOVERLAY);
  114. gConfig.fUseEmbeddedFonts = IsDlgButtonChecked(hwndDlg, IDC_EMBEDFONT);
  115. gConfig.fKeepAspectRatio = IsDlgButtonChecked(hwndDlg, IDC_ASPECTRATIO);
  116. gConfig.eCDType = (MUSICTYPE)(ComboBox_GetCurSel(hwndDlg, IDC_CD) + MUSIC_MP3);
  117. gConfig.eMusicType = (MUSICTYPE)ComboBox_GetCurSel(hwndDlg, IDC_BGM);
  118. gConfig.eOPLType = (OPLTYPE)(ComboBox_GetCurSel(hwndDlg, IDC_OPL));
  119. gConfig.iAudioChannels = IsDlgButtonChecked(hwndDlg, IDC_STEREO) ? 2 : 1;
  120. gConfig.iSampleRate = GetDlgItemInt(hwndDlg, IDC_SAMPLERATE, nullptr, FALSE);
  121. gConfig.wAudioBufferSize = GetDlgItemInt(hwndDlg, IDC_AUDIOBUFFER, nullptr, FALSE);
  122. gConfig.iMusicVolume = TrackBar_GetPos(hwndDlg, IDC_MUSICVOLUME);
  123. gConfig.iSoundVolume = TrackBar_GetPos(hwndDlg, IDC_SOUNDVOLUME);
  124. gConfig.iResampleQuality = TrackBar_GetPos(hwndDlg, IDC_QUALITY);
  125. if (gConfig.eMusicType == MUSIC_RIX)
  126. {
  127. gConfig.fUseSurroundOPL = IsDlgButtonChecked(hwndDlg, IDC_SURROUNDOPL);
  128. gConfig.iOPLSampleRate = GetDlgItemInt(hwndDlg, IDC_OPLSR, nullptr, FALSE);
  129. }
  130. if (fWriteFile) PAL_SaveConfig();
  131. }
  132. void ResetControls(HWND hwndDlg)
  133. {
  134. TCHAR buffer[100];
  135. EnableDlgItem(hwndDlg, IDC_OPL, gConfig.eMusicType == MUSIC_RIX);
  136. EnableDlgItem(hwndDlg, IDC_SURROUNDOPL, gConfig.eMusicType == MUSIC_RIX);
  137. EnableDlgItem(hwndDlg, IDC_OPLSR, gConfig.eMusicType == MUSIC_RIX);
  138. CheckRadioButton(hwndDlg, IDC_CHT, IDC_CHS, IDC_CHT + gConfig.uCodePage);
  139. CheckDlgButton(hwndDlg, IDC_FULLSCREEN, gConfig.fFullScreen);
  140. CheckDlgButton(hwndDlg, IDC_TOUCHOVERLAY, gConfig.fUseTouchOverlay);
  141. CheckDlgButton(hwndDlg, IDC_EMBEDFONT, gConfig.fUseEmbeddedFonts);
  142. CheckDlgButton(hwndDlg, IDC_ASPECTRATIO, gConfig.fKeepAspectRatio);
  143. CheckDlgButton(hwndDlg, IDC_SURROUNDOPL, gConfig.fUseSurroundOPL);
  144. CheckDlgButton(hwndDlg, IDC_STEREO, gConfig.iAudioChannels == 2);
  145. ComboBox_SetCurSel(hwndDlg, IDC_CD, gConfig.eCDType - MUSIC_MP3);
  146. ComboBox_SetCurSel(hwndDlg, IDC_BGM, gConfig.eMusicType);
  147. ComboBox_SetCurSel(hwndDlg, IDC_OPL, gConfig.eOPLType);
  148. SetDlgItemText(hwndDlg, IDC_SAMPLERATE, _itot(gConfig.iSampleRate, buffer, 10));
  149. SetDlgItemText(hwndDlg, IDC_OPLSR, _itot(gConfig.iOPLSampleRate, buffer, 10));
  150. SetDlgItemText(hwndDlg, IDC_AUDIOBUFFER, _itot(gConfig.wAudioBufferSize, buffer, 10));
  151. if (gConfig.pszGamePath) SetDlgItemTextA(hwndDlg, IDC_GAMEPATH, gConfig.pszGamePath);
  152. if (gConfig.pszMsgFile) SetDlgItemTextA(hwndDlg, IDC_MSGFILE, gConfig.pszMsgFile);
  153. TrackBar_SetPos(hwndDlg, IDC_QUALITY, gConfig.iResampleQuality, TRUE);
  154. TrackBar_SetPos(hwndDlg, IDC_MUSICVOLUME, gConfig.iMusicVolume, TRUE);
  155. TrackBar_SetPos(hwndDlg, IDC_SOUNDVOLUME, gConfig.iSoundVolume, TRUE);
  156. }
  157. INT_PTR InitProc(HWND hwndDlg, HWND hwndCtrl, LPARAM lParam)
  158. {
  159. InitCommonControls();
  160. ComboBox_AddString(hwndDlg, IDC_CD, TEXT("MP3"));
  161. ComboBox_AddString(hwndDlg, IDC_CD, TEXT("OGG"));
  162. ComboBox_AddString(hwndDlg, IDC_BGM, TEXT("MIDI"));
  163. ComboBox_AddString(hwndDlg, IDC_BGM, TEXT("RIX"));
  164. ComboBox_AddString(hwndDlg, IDC_BGM, TEXT("MP3"));
  165. ComboBox_AddString(hwndDlg, IDC_BGM, TEXT("OGG"));
  166. ComboBox_AddString(hwndDlg, IDC_OPL, TEXT("DOSBOX"));
  167. ComboBox_AddString(hwndDlg, IDC_OPL, TEXT("MAME"));
  168. ComboBox_AddString(hwndDlg, IDC_OPL, TEXT("DOSBOXNEW"));
  169. TrackBar_SetRange(hwndDlg, IDC_QUALITY, RESAMPLER_QUALITY_MIN, RESAMPLER_QUALITY_MAX, FALSE);
  170. TrackBar_SetRange(hwndDlg, IDC_MUSICVOLUME, 0, PAL_MAX_VOLUME, FALSE);
  171. TrackBar_SetRange(hwndDlg, IDC_SOUNDVOLUME, 0, PAL_MAX_VOLUME, FALSE);
  172. ResetControls(hwndDlg);
  173. WINDOWINFO wi = { sizeof(WINDOWINFO) };
  174. RECT rcWork;
  175. if (SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWork, 0) && GetWindowInfo(hwndDlg, &wi))
  176. {
  177. int x = ((rcWork.right - rcWork.left) - (wi.rcWindow.right - wi.rcWindow.left)) / 2;
  178. int y = ((rcWork.bottom - rcWork.top) - (wi.rcWindow.bottom - wi.rcWindow.top)) / 2;
  179. SetWindowPos(hwndDlg, 0, x, y, 0, 0, SWP_NOSIZE | SWP_NOOWNERZORDER | SWP_SHOWWINDOW);
  180. }
  181. return TRUE;
  182. }
  183. INT_PTR ButtonProc(HWND hwndDlg, WORD idControl, HWND hwndCtrl)
  184. {
  185. switch (idControl)
  186. {
  187. case IDOK:
  188. gConfig.fLaunchSetting = FALSE;
  189. SaveSettings(hwndDlg, TRUE);
  190. EndDialog(hwndDlg, IDOK);
  191. return TRUE;
  192. case IDCANCEL:
  193. EndDialog(hwndDlg, IDCANCEL);
  194. return TRUE;
  195. case IDC_DEFAULT:
  196. PAL_LoadConfig(FALSE);
  197. ResetControls(hwndDlg);
  198. return TRUE;
  199. case IDC_BRGAME:
  200. {
  201. TCHAR szName[MAX_PATH * 2], szTitle[200];
  202. BROWSEINFO bi = { hwndDlg, nullptr, szName, szTitle, BIF_USENEWUI, nullptr, NULL, 0 };
  203. LoadStringEx(g_hInstance, idControl, g_wLanguage, szTitle, 200);
  204. auto pidl = SHBrowseForFolder(&bi);
  205. if (pidl)
  206. {
  207. SHGetPathFromIDList(pidl, szName);
  208. int n = _tcslen(szName);
  209. if (szName[n - 1] != '\\') _tcscat(szName, L"\\");
  210. SetDlgItemText(hwndDlg, IDC_GAMEPATH, szName);
  211. }
  212. return TRUE;
  213. }
  214. default: return FALSE;
  215. }
  216. }
  217. INT_PTR ComboBoxProc(HWND hwndDlg, WORD idControl, HWND hwndCtrl)
  218. {
  219. switch (idControl)
  220. {
  221. case IDC_BGM:
  222. EnableDlgItem(hwndDlg, IDC_OPL, ComboBox_GetCurSel(hwndDlg, IDC_BGM) == MUSIC_RIX);
  223. EnableDlgItem(hwndDlg, IDC_SURROUNDOPL, ComboBox_GetCurSel(hwndDlg, IDC_BGM) == MUSIC_RIX);
  224. EnableDlgItem(hwndDlg, IDC_OPLSR, ComboBox_GetCurSel(hwndDlg, IDC_BGM) == MUSIC_RIX);
  225. return TRUE;
  226. default:
  227. return FALSE;
  228. }
  229. }
  230. INT_PTR CommandProc(HWND hwndDlg, WORD command, WORD idControl, HWND hwndCtrl)
  231. {
  232. switch (command)
  233. {
  234. case BN_CLICKED: return ButtonProc(hwndDlg, idControl, hwndCtrl);
  235. case CBN_SELCHANGE: return ComboBoxProc(hwndDlg, idControl, hwndCtrl);
  236. default: return FALSE;
  237. }
  238. }
  239. INT_PTR CALLBACK LauncherDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  240. {
  241. switch (uMsg)
  242. {
  243. case WM_INITDIALOG: return InitProc(hwndDlg, (HWND)wParam, lParam);
  244. case WM_COMMAND: return CommandProc(hwndDlg, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
  245. case WM_CLOSE: return EndDialog(hwndDlg, 0);
  246. default: return FALSE;
  247. }
  248. }
  249. extern "C" int UTIL_Platform_Init(int argc, char* argv[])
  250. {
  251. // Defaults log to debug output
  252. UTIL_LogAddOutputCallback([](LOGLEVEL, const char* str, const char*)->void {
  253. OutputDebugStringA(str);
  254. }, PAL_DEFAULT_LOGLEVEL);
  255. g_hInstance = GetModuleHandle(nullptr);
  256. #if !defined(__MINGW32__) || _WIN32_WINNT > _WIN32_WINNT_WS03 // compile time switch; use `make CCFLAGS=-D_WIN32_WINNT=_WIN32_WINNT_VISTA` for vista+ only automatic language detection
  257. g_wLanguage = GetThreadUILanguage();
  258. #else // default XP compatible CodePage detection hack.
  259. g_wLanguage = GetSystemDefaultLangID();
  260. #endif
  261. if (PRIMARYLANGID(g_wLanguage) == LANG_CHINESE)
  262. {
  263. if (SUBLANGID(g_wLanguage) == SUBLANG_CHINESE_SIMPLIFIED || SUBLANGID(g_wLanguage) == SUBLANG_CHINESE_SINGAPORE)
  264. g_wLanguage = MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED);
  265. else
  266. g_wLanguage = MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL);
  267. }
  268. else
  269. g_wLanguage = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
  270. auto dlg = (LPCDLGTEMPLATE)LockResource(LoadResource(g_hInstance, FindResourceEx(g_hInstance, RT_DIALOG, MAKEINTRESOURCE(IDD_LAUNCHER), g_wLanguage)));
  271. if (gConfig.fLaunchSetting && DialogBoxIndirect(GetModuleHandle(nullptr), dlg, nullptr, LauncherDialogProc) != IDOK)
  272. return -1;
  273. else
  274. return 0;
  275. }
  276. extern "C" VOID UTIL_Platform_Quit(VOID) {}
  277. extern "C" BOOL
  278. UTIL_GetScreenSize(
  279. DWORD *pdwScreenWidth,
  280. DWORD *pdwScreenHeight
  281. )
  282. {
  283. return (pdwScreenWidth && pdwScreenHeight && *pdwScreenWidth && *pdwScreenHeight);
  284. }
  285. extern "C"
  286. BOOL UTIL_IsAbsolutePath(LPCSTR lpszFileName)
  287. {
  288. char szDrive[_MAX_DRIVE], szDir[_MAX_DIR], szFname[_MAX_FNAME], szExt[_MAX_EXT];
  289. #if !defined(__MINGW32__) // MinGW Distro's win32 api lacks this...Anyway, winxp lacks this too
  290. if (_splitpath_s(lpszFileName, szDrive, szDir, szFname, szExt) == 0)
  291. #else
  292. _splitpath(lpszFileName, szDrive, szDir, szFname, szExt);
  293. if ( errno !=EINVAL )
  294. #endif
  295. return (strlen(szDrive) > 0 && (szDir[0] == '\\' || szDir[0] == '/'));
  296. else
  297. return FALSE;
  298. }