win32.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. #define NO_DSHOW_STRSAFE
  29. #include <inttypes.h>
  30. #include <string>
  31. #include <tchar.h>
  32. #include <windows.h>
  33. #include <mmsystem.h>
  34. #include <commctrl.h>
  35. #include <shlobj.h>
  36. #include <SDL_syswm.h>
  37. #include <d3d9.h>
  38. #include <dshow.h>
  39. #include <vmr9.h>
  40. #include "resource.h"
  41. #include "global.h"
  42. #include "util.h"
  43. #include "palcfg.h"
  44. #include "resampler.h"
  45. #include "input.h"
  46. #include "video.h"
  47. #undef ComboBox_AddString
  48. #undef ComboBox_SetCurSel
  49. #undef ComboBox_GetCurSel
  50. #define ComboBox_AddString(hwndDlg, idCtrl, lpsz) \
  51. (BOOL)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), CB_ADDSTRING, (WPARAM)(0), (LPARAM)(lpsz))
  52. #define ComboBox_SetCurSel(hwndDlg, idCtrl, index) \
  53. (BOOL)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), CB_SETCURSEL, (WPARAM)(index), (LPARAM)(0))
  54. #define ComboBox_GetCurSel(hwndDlg, idCtrl) \
  55. (int)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), CB_GETCURSEL, (WPARAM)(0), (LPARAM)(0))
  56. #define TrackBar_SetRange(hwndDlg, idCtrl, min, max, redraw) \
  57. (BOOL)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), TBM_SETRANGE, (WPARAM)(redraw), (LPARAM)(MAKELONG((min), (max))))
  58. #define TrackBar_SetPos(hwndDlg, idCtrl, pos, redraw) \
  59. (BOOL)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), TBM_SETPOS, (WPARAM)(redraw), (LPARAM)(pos))
  60. #define TrackBar_GetPos(hwndDlg, idCtrl) \
  61. (int)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), TBM_GETPOS, (WPARAM)(0), (LPARAM)(0))
  62. #define EnableDlgItem(hwnd, nIDControl, bEnable) \
  63. EnableWindow(GetDlgItem((hwnd), (nIDControl)), (bEnable))
  64. HINSTANCE g_hInstance;
  65. WORD g_wLanguage;
  66. int WINAPI LoadStringEx(
  67. HINSTANCE hInstance,
  68. UINT uID,
  69. LANGID wLang,
  70. LPTSTR lpBuffer,
  71. int nBufferMax
  72. )
  73. {
  74. auto hrc = FindResourceEx(hInstance, RT_STRING, MAKEINTRESOURCE((uID >> 4) + 1), wLang);
  75. if (nullptr == hrc) return 0;
  76. auto begin = (LPCWSTR)LockResource(LoadResource(hInstance, hrc));
  77. for (int idx = 0; idx < (int)(uID & 0xf); idx++)
  78. begin += *begin + 1;
  79. if (nBufferMax == 0)
  80. {
  81. *((LPCWSTR*)lpBuffer) = begin;
  82. return sizeof(LPCWSTR);
  83. }
  84. else
  85. {
  86. wcsncpy(lpBuffer, begin + 1, min(nBufferMax, *begin));
  87. if (nBufferMax <= *begin)
  88. {
  89. lpBuffer[nBufferMax - 1] = '\0';
  90. return nBufferMax - 1;
  91. }
  92. else
  93. {
  94. lpBuffer[*begin] = '\0';
  95. return *begin;
  96. }
  97. }
  98. }
  99. std::wstring LoadResourceString(UINT uID)
  100. {
  101. auto hrc = FindResourceEx(g_hInstance, RT_STRING, MAKEINTRESOURCE((uID >> 4) + 1), g_wLanguage);
  102. if (hrc)
  103. {
  104. auto begin = (LPCWSTR)LockResource(LoadResource(g_hInstance, hrc));
  105. for (int idx = 0; idx < (int)(uID & 0xf); idx++)
  106. begin += *begin + 1;
  107. return std::wstring(begin + 1, *begin);
  108. }
  109. return L"";
  110. }
  111. void SaveSettings(HWND hwndDlg, BOOL fWriteFile)
  112. {
  113. int textLen;
  114. if (IsDlgButtonChecked(hwndDlg, IDC_USEMSGFILE) && (textLen = GetWindowTextLengthA(GetDlgItem(hwndDlg, IDC_MSGFILE))) > 0)
  115. {
  116. gConfig.pszMsgFile = (char*)realloc(gConfig.pszMsgFile, textLen + 1);
  117. GetDlgItemTextA(hwndDlg, IDC_MSGFILE, gConfig.pszMsgFile, textLen + 1);
  118. }
  119. else
  120. {
  121. free(gConfig.pszMsgFile); gConfig.pszMsgFile = nullptr;
  122. }
  123. if (IsDlgButtonChecked(hwndDlg, IDC_USELOGFILE) && (textLen = GetWindowTextLengthA(GetDlgItem(hwndDlg, IDC_LOGFILE))) > 0)
  124. {
  125. gConfig.pszLogFile = (char*)realloc(gConfig.pszLogFile, textLen + 1);
  126. GetDlgItemTextA(hwndDlg, IDC_LOGFILE, gConfig.pszLogFile, textLen + 1);
  127. }
  128. else
  129. {
  130. free(gConfig.pszLogFile); gConfig.pszLogFile = nullptr;
  131. }
  132. if (IsDlgButtonChecked(hwndDlg, IDC_USEFONTFILE) && (textLen = GetWindowTextLengthA(GetDlgItem(hwndDlg, IDC_FONTFILE))) > 0)
  133. {
  134. gConfig.pszFontFile = (char*)realloc(gConfig.pszFontFile, textLen + 1);
  135. GetDlgItemTextA(hwndDlg, IDC_FONTFILE, gConfig.pszFontFile, textLen + 1);
  136. }
  137. else
  138. {
  139. free(gConfig.pszFontFile); gConfig.pszFontFile = nullptr;
  140. }
  141. if ((textLen = GetWindowTextLength(GetDlgItem(hwndDlg, IDC_GAMEPATH))) > 0)
  142. {
  143. gConfig.pszGamePath = (char*)realloc(gConfig.pszGamePath, textLen + 1);
  144. GetDlgItemTextA(hwndDlg, IDC_GAMEPATH, gConfig.pszGamePath, textLen + 1);
  145. }
  146. else
  147. {
  148. free(gConfig.pszGamePath); gConfig.pszGamePath = nullptr;
  149. }
  150. gConfig.fFullScreen = IsDlgButtonChecked(hwndDlg, IDC_FULLSCREEN);
  151. gConfig.fUseTouchOverlay = IsDlgButtonChecked(hwndDlg, IDC_TOUCHOVERLAY);
  152. gConfig.fEnableAviPlay = IsDlgButtonChecked(hwndDlg, IDC_ENABLEAVI);
  153. gConfig.fKeepAspectRatio = IsDlgButtonChecked(hwndDlg, IDC_ASPECTRATIO);
  154. gConfig.eCDType = (MUSICTYPE)(ComboBox_GetCurSel(hwndDlg, IDC_CD) + MUSIC_MP3);
  155. gConfig.eMusicType = (MUSICTYPE)ComboBox_GetCurSel(hwndDlg, IDC_BGM);
  156. gConfig.eOPLType = (OPLTYPE)(ComboBox_GetCurSel(hwndDlg, IDC_OPL));
  157. gConfig.iLogLevel = (LOGLEVEL)(ComboBox_GetCurSel(hwndDlg, IDC_LOGLEVEL));
  158. gConfig.iAudioChannels = IsDlgButtonChecked(hwndDlg, IDC_STEREO) ? 2 : 1;
  159. gConfig.iSampleRate = GetDlgItemInt(hwndDlg, IDC_SAMPLERATE, nullptr, FALSE);
  160. gConfig.wAudioBufferSize = GetDlgItemInt(hwndDlg, IDC_AUDIOBUFFER, nullptr, FALSE);
  161. gConfig.iMusicVolume = TrackBar_GetPos(hwndDlg, IDC_MUSICVOLUME);
  162. gConfig.iSoundVolume = TrackBar_GetPos(hwndDlg, IDC_SOUNDVOLUME);
  163. gConfig.iResampleQuality = TrackBar_GetPos(hwndDlg, IDC_QUALITY);
  164. if (gConfig.eMusicType == MUSIC_RIX)
  165. {
  166. gConfig.fUseSurroundOPL = IsDlgButtonChecked(hwndDlg, IDC_SURROUNDOPL);
  167. gConfig.iOPLSampleRate = GetDlgItemInt(hwndDlg, IDC_OPLSR, nullptr, FALSE);
  168. }
  169. if (fWriteFile) PAL_SaveConfig();
  170. }
  171. void ResetControls(HWND hwndDlg)
  172. {
  173. TCHAR buffer[100];
  174. EnableDlgItem(hwndDlg, IDC_OPL, gConfig.eMusicType == MUSIC_RIX);
  175. EnableDlgItem(hwndDlg, IDC_SURROUNDOPL, gConfig.eMusicType == MUSIC_RIX);
  176. EnableDlgItem(hwndDlg, IDC_OPLSR, gConfig.eMusicType == MUSIC_RIX);
  177. CheckDlgButton(hwndDlg, IDC_FULLSCREEN, gConfig.fFullScreen);
  178. CheckDlgButton(hwndDlg, IDC_TOUCHOVERLAY, gConfig.fUseTouchOverlay);
  179. CheckDlgButton(hwndDlg, IDC_ENABLEAVI, gConfig.fEnableAviPlay);
  180. CheckDlgButton(hwndDlg, IDC_ASPECTRATIO, gConfig.fKeepAspectRatio);
  181. CheckDlgButton(hwndDlg, IDC_SURROUNDOPL, gConfig.fUseSurroundOPL);
  182. CheckDlgButton(hwndDlg, IDC_STEREO, gConfig.iAudioChannels == 2);
  183. CheckDlgButton(hwndDlg, IDC_USEMSGFILE, gConfig.pszMsgFile != nullptr);
  184. EnableDlgItem(hwndDlg, IDC_BRMSG, gConfig.pszMsgFile != nullptr);
  185. CheckDlgButton(hwndDlg, IDC_USEFONTFILE, gConfig.pszFontFile != nullptr);
  186. EnableDlgItem(hwndDlg, IDC_BRFONT, gConfig.pszFontFile != nullptr);
  187. CheckDlgButton(hwndDlg, IDC_USELOGFILE, gConfig.pszLogFile != nullptr);
  188. EnableDlgItem(hwndDlg, IDC_BRLOG, gConfig.pszLogFile != nullptr);
  189. ComboBox_SetCurSel(hwndDlg, IDC_CD, gConfig.eCDType - MUSIC_MP3);
  190. ComboBox_SetCurSel(hwndDlg, IDC_BGM, gConfig.eMusicType);
  191. ComboBox_SetCurSel(hwndDlg, IDC_OPL, gConfig.eOPLType);
  192. ComboBox_SetCurSel(hwndDlg, IDC_LOGLEVEL, gConfig.iLogLevel);
  193. SetDlgItemText(hwndDlg, IDC_SAMPLERATE, _itot(gConfig.iSampleRate, buffer, 10));
  194. SetDlgItemText(hwndDlg, IDC_OPLSR, _itot(gConfig.iOPLSampleRate, buffer, 10));
  195. SetDlgItemText(hwndDlg, IDC_AUDIOBUFFER, _itot(gConfig.wAudioBufferSize, buffer, 10));
  196. if (gConfig.pszGamePath) SetDlgItemTextA(hwndDlg, IDC_GAMEPATH, gConfig.pszGamePath);
  197. if (gConfig.pszMsgFile) SetDlgItemTextA(hwndDlg, IDC_MSGFILE, gConfig.pszMsgFile);
  198. if (gConfig.pszFontFile) SetDlgItemTextA(hwndDlg, IDC_FONTFILE, gConfig.pszFontFile);
  199. if (gConfig.pszLogFile) SetDlgItemTextA(hwndDlg, IDC_LOGFILE, gConfig.pszLogFile);
  200. TrackBar_SetPos(hwndDlg, IDC_QUALITY, gConfig.iResampleQuality, TRUE);
  201. TrackBar_SetPos(hwndDlg, IDC_MUSICVOLUME, gConfig.iMusicVolume, TRUE);
  202. TrackBar_SetPos(hwndDlg, IDC_SOUNDVOLUME, gConfig.iSoundVolume, TRUE);
  203. }
  204. INT_PTR InitProc(HWND hwndDlg, HWND hwndCtrl, LPARAM lParam)
  205. {
  206. InitCommonControls();
  207. auto log_levels = LoadResourceString(IDC_LOGLEVEL);
  208. for (size_t pos = 0; pos != std::string::npos; )
  209. {
  210. std::wstring item;
  211. auto next = log_levels.find(L';', pos);
  212. if (next != std::string::npos)
  213. {
  214. item.assign(log_levels.c_str() + pos, next - pos);
  215. pos = next + 1;
  216. }
  217. else
  218. {
  219. item.assign(log_levels.c_str() + pos);
  220. pos = next;
  221. }
  222. if (item.length() > 0)
  223. {
  224. ComboBox_AddString(hwndDlg, IDC_LOGLEVEL, item.c_str());
  225. }
  226. }
  227. ComboBox_AddString(hwndDlg, IDC_CD, TEXT("MP3"));
  228. ComboBox_AddString(hwndDlg, IDC_CD, TEXT("OGG"));
  229. ComboBox_AddString(hwndDlg, IDC_BGM, TEXT("MIDI"));
  230. ComboBox_AddString(hwndDlg, IDC_BGM, TEXT("RIX"));
  231. ComboBox_AddString(hwndDlg, IDC_BGM, TEXT("MP3"));
  232. ComboBox_AddString(hwndDlg, IDC_BGM, TEXT("OGG"));
  233. ComboBox_AddString(hwndDlg, IDC_OPL, TEXT("DOSBOX"));
  234. ComboBox_AddString(hwndDlg, IDC_OPL, TEXT("MAME"));
  235. ComboBox_AddString(hwndDlg, IDC_OPL, TEXT("DOSBOXNEW"));
  236. TrackBar_SetRange(hwndDlg, IDC_QUALITY, RESAMPLER_QUALITY_MIN, RESAMPLER_QUALITY_MAX, FALSE);
  237. TrackBar_SetRange(hwndDlg, IDC_MUSICVOLUME, 0, PAL_MAX_VOLUME, FALSE);
  238. TrackBar_SetRange(hwndDlg, IDC_SOUNDVOLUME, 0, PAL_MAX_VOLUME, FALSE);
  239. ResetControls(hwndDlg);
  240. WINDOWINFO wi = { sizeof(WINDOWINFO) };
  241. RECT rcWork;
  242. if (SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWork, 0) && GetWindowInfo(hwndDlg, &wi))
  243. {
  244. int x = ((rcWork.right - rcWork.left) - (wi.rcWindow.right - wi.rcWindow.left)) / 2;
  245. int y = ((rcWork.bottom - rcWork.top) - (wi.rcWindow.bottom - wi.rcWindow.top)) / 2;
  246. SetWindowPos(hwndDlg, 0, x, y, 0, 0, SWP_NOSIZE | SWP_NOOWNERZORDER | SWP_SHOWWINDOW);
  247. }
  248. return TRUE;
  249. }
  250. INT_PTR ButtonProc(HWND hwndDlg, WORD idControl, HWND hwndCtrl)
  251. {
  252. switch (idControl)
  253. {
  254. case IDOK:
  255. gConfig.fLaunchSetting = FALSE;
  256. SaveSettings(hwndDlg, TRUE);
  257. EndDialog(hwndDlg, IDOK);
  258. return TRUE;
  259. case IDCANCEL:
  260. EndDialog(hwndDlg, IDCANCEL);
  261. return TRUE;
  262. case IDC_DEFAULT:
  263. PAL_LoadConfig(FALSE);
  264. ResetControls(hwndDlg);
  265. return TRUE;
  266. case IDC_BRGAME:
  267. {
  268. TCHAR szName[MAX_PATH * 2], szTitle[200];
  269. BROWSEINFO bi = { hwndDlg, nullptr, szName, szTitle, BIF_USENEWUI, nullptr, NULL, 0 };
  270. LoadStringEx(g_hInstance, IDC_BRGAME, g_wLanguage, szTitle, 200);
  271. auto pidl = SHBrowseForFolder(&bi);
  272. if (pidl)
  273. {
  274. SHGetPathFromIDList(pidl, szName);
  275. size_t n = _tcslen(szName);
  276. if (szName[n - 1] != '\\') _tcscat(szName, L"\\");
  277. SetDlgItemText(hwndDlg, IDC_GAMEPATH, szName);
  278. }
  279. return TRUE;
  280. }
  281. case IDC_BRFONT:
  282. case IDC_BRMSG:
  283. case IDC_BRLOG:
  284. {
  285. TCHAR szFilePath[MAX_PATH * 2] = { 0 };
  286. auto filter = LoadResourceString(idControl + 1);
  287. auto title = LoadResourceString(idControl);
  288. for (auto i = filter.begin(); i != filter.end(); *i = (*i == '|') ? '\0' : *i, i++);
  289. OPENFILENAME ofn = {
  290. sizeof(OPENFILENAME), hwndDlg, nullptr,
  291. filter.c_str(), nullptr, 0, 0,
  292. szFilePath, sizeof(szFilePath) / sizeof(TCHAR),
  293. nullptr, 0, nullptr, title.c_str(),
  294. OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | (idControl != IDC_BRLOG ? OFN_FILEMUSTEXIST : (DWORD)0)
  295. };
  296. if (idControl == IDC_BRLOG ? GetSaveFileName(&ofn) : GetOpenFileName(&ofn))
  297. {
  298. SetDlgItemText(hwndDlg, idControl + 1, ofn.lpstrFile);
  299. }
  300. return TRUE;
  301. }
  302. case IDC_USEMSGFILE:
  303. case IDC_USEFONTFILE:
  304. case IDC_USELOGFILE:
  305. EnableDlgItem(hwndDlg, idControl + 1, IsDlgButtonChecked(hwndDlg, idControl));
  306. return TRUE;
  307. default: return FALSE;
  308. }
  309. }
  310. INT_PTR ComboBoxProc(HWND hwndDlg, WORD idControl, HWND hwndCtrl)
  311. {
  312. switch (idControl)
  313. {
  314. case IDC_BGM:
  315. EnableDlgItem(hwndDlg, IDC_OPL, ComboBox_GetCurSel(hwndDlg, IDC_BGM) == MUSIC_RIX);
  316. EnableDlgItem(hwndDlg, IDC_SURROUNDOPL, ComboBox_GetCurSel(hwndDlg, IDC_BGM) == MUSIC_RIX);
  317. EnableDlgItem(hwndDlg, IDC_OPLSR, ComboBox_GetCurSel(hwndDlg, IDC_BGM) == MUSIC_RIX);
  318. return TRUE;
  319. default:
  320. return FALSE;
  321. }
  322. }
  323. INT_PTR CommandProc(HWND hwndDlg, WORD command, WORD idControl, HWND hwndCtrl)
  324. {
  325. switch (command)
  326. {
  327. case BN_CLICKED: return ButtonProc(hwndDlg, idControl, hwndCtrl);
  328. case CBN_SELCHANGE: return ComboBoxProc(hwndDlg, idControl, hwndCtrl);
  329. default: return FALSE;
  330. }
  331. }
  332. INT_PTR CALLBACK LauncherDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  333. {
  334. switch (uMsg)
  335. {
  336. case WM_INITDIALOG: return InitProc(hwndDlg, (HWND)wParam, lParam);
  337. case WM_COMMAND: return CommandProc(hwndDlg, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
  338. case WM_CLOSE: return EndDialog(hwndDlg, 0);
  339. default: return FALSE;
  340. }
  341. }
  342. typedef LANGID(__stdcall *GETLANGUAGEID)(void);
  343. extern "C" int UTIL_Platform_Init(int argc, char* argv[])
  344. {
  345. // Try to get Vista+ API at runtime, and falls back to XP's API if not found
  346. GETLANGUAGEID GetLanguage = (GETLANGUAGEID)GetProcAddress(GetModuleHandle(TEXT("Kernel32.dll")), "GetThreadUILanguage");
  347. if (!GetLanguage) GetLanguage = GetUserDefaultLangID;
  348. // Defaults log to debug output
  349. UTIL_LogAddOutputCallback([](LOGLEVEL, const char* str, const char*)->void {
  350. OutputDebugStringA(str);
  351. }, PAL_DEFAULT_LOGLEVEL);
  352. g_hInstance = GetModuleHandle(nullptr);
  353. g_wLanguage = GetLanguage();
  354. if (PRIMARYLANGID(g_wLanguage) == LANG_CHINESE)
  355. {
  356. if (SUBLANGID(g_wLanguage) == SUBLANG_CHINESE_SIMPLIFIED || SUBLANGID(g_wLanguage) == SUBLANG_CHINESE_SINGAPORE)
  357. g_wLanguage = MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED);
  358. else
  359. g_wLanguage = MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL);
  360. }
  361. else
  362. g_wLanguage = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
  363. auto dlg = (LPCDLGTEMPLATE)LockResource(LoadResource(g_hInstance, FindResourceEx(g_hInstance, RT_DIALOG, MAKEINTRESOURCE(IDD_LAUNCHER), g_wLanguage)));
  364. if (gConfig.fLaunchSetting && DialogBoxIndirect(GetModuleHandle(nullptr), dlg, nullptr, LauncherDialogProc) != IDOK)
  365. return -1;
  366. else
  367. return 0;
  368. }
  369. extern "C" VOID UTIL_Platform_Quit(VOID) {}
  370. extern "C" BOOL
  371. UTIL_GetScreenSize(
  372. DWORD *pdwScreenWidth,
  373. DWORD *pdwScreenHeight
  374. )
  375. {
  376. return (pdwScreenWidth && pdwScreenHeight && *pdwScreenWidth && *pdwScreenHeight);
  377. }
  378. extern "C"
  379. BOOL UTIL_IsAbsolutePath(LPCSTR lpszFileName)
  380. {
  381. char szDrive[_MAX_DRIVE], szDir[_MAX_DIR], szFname[_MAX_FNAME], szExt[_MAX_EXT];
  382. #if !defined(__MINGW32__) // MinGW Distro's win32 api lacks this...Anyway, winxp lacks this too
  383. if (_splitpath_s(lpszFileName, szDrive, szDir, szFname, szExt) == 0)
  384. #else
  385. _splitpath(lpszFileName, szDrive, szDir, szFname, szExt);
  386. if ( errno !=EINVAL )
  387. #endif
  388. return (strlen(szDrive) > 0 && (szDir[0] == '\\' || szDir[0] == '/'));
  389. else
  390. return FALSE;
  391. }
  392. // DirectShow-based AVI player for Windows
  393. template<class T>
  394. class com_ptr
  395. {
  396. public:
  397. com_ptr() : _obj(nullptr) {}
  398. com_ptr(T* obj) : _obj(obj) {}
  399. ~com_ptr() { _obj && _obj->Release(); }
  400. operator T*() { return _obj; }
  401. T* operator->() { return _obj; }
  402. T** operator&() { return &_obj; }
  403. com_ptr& operator=(T* other)
  404. {
  405. if (_obj != other)
  406. {
  407. _obj && _obj->Release();
  408. (_obj = other) && _obj->AddRef();
  409. }
  410. return *this;
  411. }
  412. private:
  413. T* _obj;
  414. };
  415. extern "C"
  416. BOOL PAL_PlayAVI_Native(const char* lpszPath)
  417. {
  418. SDL_SysWMinfo info;
  419. if (!VIDEO_GetWindowInfo(&info)) return FALSE;
  420. #if SDL_VERSION_ATLEAST(2, 0, 0)
  421. HWND hwnd = info.info.win.window;
  422. #else
  423. HWND hwnd = info.window;
  424. #endif
  425. RECT rc;
  426. if (!GetClientRect(hwnd, &rc)) return FALSE;
  427. int buf_len = MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, nullptr, 0);
  428. wchar_t* szPath = (wchar_t*)alloca(sizeof(wchar_t) * buf_len);
  429. MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, szPath, buf_len);
  430. HANDLE hAviPlayEvent;
  431. HRESULT hr;
  432. com_ptr<IGraphBuilder> pGraph;
  433. com_ptr<IMediaControl> pControl;
  434. com_ptr<IMediaEvent> pEvent;
  435. com_ptr<IEnumFilters> pEnum;
  436. com_ptr<IBaseFilter> pFilter;
  437. com_ptr<IVMRWindowlessControl9> pWindow;
  438. if (FAILED(hr = CoCreateInstance(CLSID_FilterGraph, nullptr, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph))) return FALSE;
  439. if (FAILED(hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl))) return FALSE;
  440. if (FAILED(hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent))) return FALSE;
  441. if (FAILED(hr = pGraph->RenderFile(szPath, nullptr))) return FALSE;
  442. if (FAILED(hr = pGraph->EnumFilters(&pEnum))) return FALSE;
  443. while (S_OK == pEnum->Next(1, &pFilter, nullptr))
  444. {
  445. FILTER_INFO fi;
  446. pFilter->QueryFilterInfo(&fi);
  447. if (lstrcmpW(fi.achName, L"Video Renderer") == 0)
  448. {
  449. com_ptr<IEnumPins> pEnumPins;
  450. com_ptr<IPin> pOutputPin, pPin;
  451. if (FAILED(hr = pFilter->EnumPins(&pEnumPins))) return FALSE;
  452. while (S_OK == pEnumPins->Next(1, &pPin, nullptr))
  453. {
  454. PIN_DIRECTION dir;
  455. if (SUCCEEDED(pPin->QueryDirection(&dir)) && dir == PINDIR_INPUT &&
  456. SUCCEEDED(pPin->ConnectedTo(&pOutputPin)) && pOutputPin)
  457. break;
  458. else
  459. pPin = nullptr;
  460. }
  461. if (!pOutputPin || FAILED(hr = pGraph->RemoveFilter(pFilter))) return FALSE;
  462. pPin = nullptr;
  463. pEnumPins = nullptr;
  464. pFilter = nullptr;
  465. com_ptr<IVMRFilterConfig9> pConfig;
  466. if (FAILED(hr = CoCreateInstance(CLSID_VideoMixingRenderer9, nullptr, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pFilter))) return FALSE;
  467. if (FAILED(hr = pFilter->QueryInterface(&pConfig))) return FALSE;
  468. if (FAILED(hr = pConfig->SetRenderingMode(VMR9Mode_Windowless))) return FALSE;
  469. if (FAILED(hr = pConfig->QueryInterface(&pWindow))) return FALSE;
  470. if (FAILED(hr = pWindow->SetVideoClippingWindow(hwnd))) return FALSE;
  471. if (FAILED(hr = pWindow->SetVideoPosition(nullptr, &rc))) return FALSE;
  472. if (FAILED(hr = pGraph->AddFilter(pFilter, L"Video Renderer"))) return FALSE;
  473. if (FAILED(hr = pFilter->EnumPins(&pEnumPins))) return FALSE;
  474. while (S_OK == pEnumPins->Next(1, &pPin, nullptr))
  475. {
  476. PIN_DIRECTION dir;
  477. if (SUCCEEDED(pPin->QueryDirection(&dir)) && dir == PINDIR_INPUT)
  478. break;
  479. else
  480. pPin = nullptr;
  481. }
  482. if (!pPin || FAILED(hr = pGraph->Connect(pOutputPin, pPin))) return FALSE;
  483. break;
  484. }
  485. pFilter = nullptr;
  486. }
  487. RedrawWindow(hwnd, nullptr, nullptr, RDW_INVALIDATE);
  488. PAL_ClearKeyState();
  489. if (FAILED(hr = pEvent->GetEventHandle((OAEVENT*)&hAviPlayEvent))) return FALSE;
  490. if (FAILED(hr = pControl->Run())) return FALSE;
  491. BOOL looping = TRUE;
  492. while (looping && !(g_InputState.dwKeyPress & (kKeyMenu | kKeySearch)))
  493. {
  494. if (WaitForSingleObject(hAviPlayEvent, 50) == WAIT_OBJECT_0)
  495. {
  496. long evCode;
  497. LONG_PTR param1, param2;
  498. while (S_OK == pEvent->GetEvent(&evCode, &param1, &param2, 0))
  499. {
  500. pEvent->FreeEventParams(evCode, param1, param2);
  501. if (evCode == EC_COMPLETE)
  502. looping = FALSE;
  503. }
  504. }
  505. UTIL_Delay(1);
  506. }
  507. hr = pControl->Stop();
  508. if (looping)
  509. {
  510. //
  511. // Simulate a short delay (like the original game)
  512. //
  513. UTIL_Delay(500);
  514. }
  515. return TRUE;
  516. }