SDL_windows.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if defined(__WIN32__) || defined(__WINRT__) || defined(__GDK__)
  20. #include "SDL_windows.h"
  21. #include <objbase.h> /* for CoInitialize/CoUninitialize (Win32 only) */
  22. #if defined(HAVE_ROAPI_H)
  23. #include <roapi.h> /* For RoInitialize/RoUninitialize (Win32 only) */
  24. #else
  25. typedef enum RO_INIT_TYPE
  26. {
  27. RO_INIT_SINGLETHREADED = 0,
  28. RO_INIT_MULTITHREADED = 1
  29. } RO_INIT_TYPE;
  30. #endif
  31. #ifndef _WIN32_WINNT_VISTA
  32. #define _WIN32_WINNT_VISTA 0x0600
  33. #endif
  34. #ifndef _WIN32_WINNT_WIN7
  35. #define _WIN32_WINNT_WIN7 0x0601
  36. #endif
  37. #ifndef _WIN32_WINNT_WIN8
  38. #define _WIN32_WINNT_WIN8 0x0602
  39. #endif
  40. #ifndef LOAD_LIBRARY_SEARCH_SYSTEM32
  41. #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
  42. #endif
  43. /* Sets an error message based on an HRESULT */
  44. int WIN_SetErrorFromHRESULT(const char *prefix, HRESULT hr)
  45. {
  46. TCHAR buffer[1024];
  47. char *message;
  48. TCHAR *p = buffer;
  49. DWORD c = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0,
  50. buffer, SDL_arraysize(buffer), NULL);
  51. buffer[c] = 0;
  52. /* kill CR/LF that FormatMessage() sticks at the end */
  53. while (*p) {
  54. if (*p == '\r') {
  55. *p = 0;
  56. break;
  57. }
  58. ++p;
  59. }
  60. message = WIN_StringToUTF8(buffer);
  61. SDL_SetError("%s%s%s", prefix ? prefix : "", prefix ? ": " : "", message);
  62. SDL_free(message);
  63. return -1;
  64. }
  65. /* Sets an error message based on GetLastError() */
  66. int WIN_SetError(const char *prefix)
  67. {
  68. return WIN_SetErrorFromHRESULT(prefix, GetLastError());
  69. }
  70. HRESULT
  71. WIN_CoInitialize(void)
  72. {
  73. /* SDL handles any threading model, so initialize with the default, which
  74. is compatible with OLE and if that doesn't work, try multi-threaded mode.
  75. If you need multi-threaded mode, call CoInitializeEx() before SDL_Init()
  76. */
  77. #ifdef __WINRT__
  78. /* DLudwig: On WinRT, it is assumed that COM was initialized in main().
  79. CoInitializeEx is available (not CoInitialize though), however
  80. on WinRT, main() is typically declared with the [MTAThread]
  81. attribute, which, AFAIK, should initialize COM.
  82. */
  83. return S_OK;
  84. #elif defined(__XBOXONE__) || defined(__XBOXSERIES__)
  85. /* On Xbox, there's no need to call CoInitializeEx (and it's not implemented) */
  86. return S_OK;
  87. #else
  88. HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  89. if (hr == RPC_E_CHANGED_MODE) {
  90. hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  91. }
  92. /* S_FALSE means success, but someone else already initialized. */
  93. /* You still need to call CoUninitialize in this case! */
  94. if (hr == S_FALSE) {
  95. return S_OK;
  96. }
  97. return hr;
  98. #endif
  99. }
  100. void WIN_CoUninitialize(void)
  101. {
  102. #ifndef __WINRT__
  103. CoUninitialize();
  104. #endif
  105. }
  106. #ifndef __WINRT__
  107. void *
  108. WIN_LoadComBaseFunction(const char *name)
  109. {
  110. static SDL_bool s_bLoaded;
  111. static HMODULE s_hComBase;
  112. if (!s_bLoaded) {
  113. s_hComBase = LoadLibraryEx(TEXT("combase.dll"), NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
  114. s_bLoaded = SDL_TRUE;
  115. }
  116. if (s_hComBase) {
  117. return GetProcAddress(s_hComBase, name);
  118. } else {
  119. return NULL;
  120. }
  121. }
  122. #endif
  123. HRESULT
  124. WIN_RoInitialize(void)
  125. {
  126. #ifdef __WINRT__
  127. return S_OK;
  128. #else
  129. typedef HRESULT(WINAPI * RoInitialize_t)(RO_INIT_TYPE initType);
  130. RoInitialize_t RoInitializeFunc = (RoInitialize_t)WIN_LoadComBaseFunction("RoInitialize");
  131. if (RoInitializeFunc) {
  132. /* RO_INIT_SINGLETHREADED is equivalent to COINIT_APARTMENTTHREADED */
  133. HRESULT hr = RoInitializeFunc(RO_INIT_SINGLETHREADED);
  134. if (hr == RPC_E_CHANGED_MODE) {
  135. hr = RoInitializeFunc(RO_INIT_MULTITHREADED);
  136. }
  137. /* S_FALSE means success, but someone else already initialized. */
  138. /* You still need to call RoUninitialize in this case! */
  139. if (hr == S_FALSE) {
  140. return S_OK;
  141. }
  142. return hr;
  143. } else {
  144. return E_NOINTERFACE;
  145. }
  146. #endif
  147. }
  148. void WIN_RoUninitialize(void)
  149. {
  150. #ifndef __WINRT__
  151. typedef void(WINAPI * RoUninitialize_t)(void);
  152. RoUninitialize_t RoUninitializeFunc = (RoUninitialize_t)WIN_LoadComBaseFunction("RoUninitialize");
  153. if (RoUninitializeFunc) {
  154. RoUninitializeFunc();
  155. }
  156. #endif
  157. }
  158. #if !defined(__WINRT__) && !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  159. static BOOL IsWindowsVersionOrGreater(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor)
  160. {
  161. OSVERSIONINFOEXW osvi;
  162. DWORDLONG const dwlConditionMask = VerSetConditionMask(
  163. VerSetConditionMask(
  164. VerSetConditionMask(
  165. 0, VER_MAJORVERSION, VER_GREATER_EQUAL),
  166. VER_MINORVERSION, VER_GREATER_EQUAL),
  167. VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
  168. SDL_zero(osvi);
  169. osvi.dwOSVersionInfoSize = sizeof(osvi);
  170. osvi.dwMajorVersion = wMajorVersion;
  171. osvi.dwMinorVersion = wMinorVersion;
  172. osvi.wServicePackMajor = wServicePackMajor;
  173. return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
  174. }
  175. #endif
  176. BOOL WIN_IsWindowsVistaOrGreater(void)
  177. {
  178. #if defined(__WINRT__) || defined(__XBOXONE__) || defined(__XBOXSERIES__)
  179. return TRUE;
  180. #else
  181. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 0);
  182. #endif
  183. }
  184. BOOL WIN_IsWindows7OrGreater(void)
  185. {
  186. #if defined(__WINRT__) || defined(__XBOXONE__) || defined(__XBOXSERIES__)
  187. return TRUE;
  188. #else
  189. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN7), LOBYTE(_WIN32_WINNT_WIN7), 0);
  190. #endif
  191. }
  192. BOOL WIN_IsWindows8OrGreater(void)
  193. {
  194. #if defined(__WINRT__) || defined(__XBOXONE__) || defined(__XBOXSERIES__)
  195. return TRUE;
  196. #else
  197. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN8), LOBYTE(_WIN32_WINNT_WIN8), 0);
  198. #endif
  199. }
  200. /*
  201. WAVExxxCAPS gives you 31 bytes for the device name, and just truncates if it's
  202. longer. However, since WinXP, you can use the WAVExxxCAPS2 structure, which
  203. will give you a name GUID. The full name is in the Windows Registry under
  204. that GUID, located here: HKLM\System\CurrentControlSet\Control\MediaCategories
  205. Note that drivers can report GUID_NULL for the name GUID, in which case,
  206. Windows makes a best effort to fill in those 31 bytes in the usual place.
  207. This info summarized from MSDN:
  208. http://web.archive.org/web/20131027093034/http://msdn.microsoft.com/en-us/library/windows/hardware/ff536382(v=vs.85).aspx
  209. Always look this up in the registry if possible, because the strings are
  210. different! At least on Win10, I see "Yeti Stereo Microphone" in the
  211. Registry, and a unhelpful "Microphone(Yeti Stereo Microph" in winmm. Sigh.
  212. (Also, DirectSound shouldn't be limited to 32 chars, but its device enum
  213. has the same problem.)
  214. WASAPI doesn't need this. This is just for DirectSound/WinMM.
  215. */
  216. char *
  217. WIN_LookupAudioDeviceName(const WCHAR *name, const GUID *guid)
  218. {
  219. #if defined(__WINRT__) || defined(__XBOXONE__) || defined(__XBOXSERIES__)
  220. return WIN_StringToUTF8(name); /* No registry access on WinRT/UWP and Xbox, go with what we've got. */
  221. #else
  222. static const GUID nullguid = { 0 };
  223. const unsigned char *ptr;
  224. char keystr[128];
  225. WCHAR *strw = NULL;
  226. SDL_bool rc;
  227. HKEY hkey;
  228. DWORD len = 0;
  229. char *retval = NULL;
  230. if (WIN_IsEqualGUID(guid, &nullguid)) {
  231. return WIN_StringToUTF8(name); /* No GUID, go with what we've got. */
  232. }
  233. ptr = (const unsigned char *)guid;
  234. (void)SDL_snprintf(keystr, sizeof keystr,
  235. "System\\CurrentControlSet\\Control\\MediaCategories\\{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  236. ptr[3], ptr[2], ptr[1], ptr[0], ptr[5], ptr[4], ptr[7], ptr[6],
  237. ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15]);
  238. strw = WIN_UTF8ToString(keystr);
  239. rc = (RegOpenKeyExW(HKEY_LOCAL_MACHINE, strw, 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS);
  240. SDL_free(strw);
  241. if (!rc) {
  242. return WIN_StringToUTF8(name); /* oh well. */
  243. }
  244. rc = (RegQueryValueExW(hkey, L"Name", NULL, NULL, NULL, &len) == ERROR_SUCCESS);
  245. if (!rc) {
  246. RegCloseKey(hkey);
  247. return WIN_StringToUTF8(name); /* oh well. */
  248. }
  249. strw = (WCHAR *)SDL_malloc(len + sizeof(WCHAR));
  250. if (strw == NULL) {
  251. RegCloseKey(hkey);
  252. return WIN_StringToUTF8(name); /* oh well. */
  253. }
  254. rc = (RegQueryValueExW(hkey, L"Name", NULL, NULL, (LPBYTE)strw, &len) == ERROR_SUCCESS);
  255. RegCloseKey(hkey);
  256. if (!rc) {
  257. SDL_free(strw);
  258. return WIN_StringToUTF8(name); /* oh well. */
  259. }
  260. strw[len / 2] = 0; /* make sure it's null-terminated. */
  261. retval = WIN_StringToUTF8(strw);
  262. SDL_free(strw);
  263. return retval ? retval : WIN_StringToUTF8(name);
  264. #endif /* if __WINRT__ / else */
  265. }
  266. BOOL WIN_IsEqualGUID(const GUID *a, const GUID *b)
  267. {
  268. return SDL_memcmp(a, b, sizeof(*a)) == 0;
  269. }
  270. BOOL WIN_IsEqualIID(REFIID a, REFIID b)
  271. {
  272. return SDL_memcmp(a, b, sizeof(*a)) == 0;
  273. }
  274. void WIN_RECTToRect(const RECT *winrect, SDL_Rect *sdlrect)
  275. {
  276. sdlrect->x = winrect->left;
  277. sdlrect->w = (winrect->right - winrect->left) + 1;
  278. sdlrect->y = winrect->top;
  279. sdlrect->h = (winrect->bottom - winrect->top) + 1;
  280. }
  281. void WIN_RectToRECT(const SDL_Rect *sdlrect, RECT *winrect)
  282. {
  283. winrect->left = sdlrect->x;
  284. winrect->right = sdlrect->x + sdlrect->w - 1;
  285. winrect->top = sdlrect->y;
  286. winrect->bottom = sdlrect->y + sdlrect->h - 1;
  287. }
  288. /* SDL_Win32RunApp(), which does most of the SDL_main work for Win32 */
  289. #ifdef __WIN32__
  290. #include <shellapi.h> /* CommandLineToArgvW() */
  291. /* Pop up an out of memory message, returns to Windows */
  292. static int
  293. OutOfMemory(void)
  294. {
  295. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
  296. return -1;
  297. }
  298. DECLSPEC int
  299. SDL_Win32RunApp(SDL_main_func mainFunction, void * xamlBackgroundPanel)
  300. {
  301. /* Gets the arguments with GetCommandLine, converts them to argc and argv
  302. and calls SDL_main */
  303. LPWSTR *argvw;
  304. char **argv;
  305. int i, argc, result;
  306. argvw = CommandLineToArgvW(GetCommandLineW(), &argc);
  307. if (argvw == NULL) {
  308. return OutOfMemory();
  309. }
  310. /* Note that we need to be careful about how we allocate/free memory here.
  311. * If the application calls SDL_SetMemoryFunctions(), we can't rely on
  312. * SDL_free() to use the same allocator after SDL_main() returns.
  313. */
  314. /* Parse it into argv and argc */
  315. argv = (char **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (argc + 1) * sizeof(*argv));
  316. if (argv == NULL) {
  317. return OutOfMemory();
  318. }
  319. for (i = 0; i < argc; ++i) {
  320. DWORD len;
  321. char *arg = WIN_StringToUTF8W(argvw[i]);
  322. if (arg == NULL) {
  323. return OutOfMemory();
  324. }
  325. len = (DWORD)SDL_strlen(arg);
  326. argv[i] = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len + 1);
  327. if (!argv[i]) {
  328. return OutOfMemory();
  329. }
  330. SDL_memcpy(argv[i], arg, len);
  331. SDL_free(arg);
  332. }
  333. argv[i] = NULL;
  334. LocalFree(argvw);
  335. SDL_SetMainReady();
  336. /* Run the application main() code */
  337. result = mainFunction(argc, argv);
  338. /* Free argv, to avoid memory leak */
  339. for (i = 0; i < argc; ++i) {
  340. HeapFree(GetProcessHeap(), 0, argv[i]);
  341. }
  342. HeapFree(GetProcessHeap(), 0, argv);
  343. return result;
  344. }
  345. #endif /* __WIN32__ */
  346. #endif /* defined(__WIN32__) || defined(__WINRT__) || defined(__GDK__) */
  347. /*
  348. * Public APIs
  349. */
  350. #if !defined(SDL_VIDEO_DRIVER_WINDOWS)
  351. #if defined(__WIN32__) || defined(__GDK__)
  352. int SDL_RegisterApp(const char *name, Uint32 style, void *hInst)
  353. {
  354. (void)name;
  355. (void)style;
  356. (void)hInst;
  357. return 0;
  358. }
  359. void SDL_UnregisterApp(void)
  360. {
  361. }
  362. void SDL_SetWindowsMessageHook(SDL_WindowsMessageHook callback, void *userdata)
  363. {
  364. }
  365. #endif /* __WIN32__ || __GDK__ */
  366. #if defined(__WIN32__) || defined(__WINGDK__)
  367. int SDL_Direct3D9GetAdapterIndex(int displayIndex)
  368. {
  369. (void)displayIndex;
  370. return 0; /* D3DADAPTER_DEFAULT */
  371. }
  372. SDL_bool
  373. SDL_DXGIGetOutputInfo(int displayIndex, int *adapterIndex, int *outputIndex)
  374. {
  375. (void)displayIndex;
  376. if (adapterIndex) {
  377. *adapterIndex = -1;
  378. }
  379. if (outputIndex) {
  380. *outputIndex = -1;
  381. }
  382. return SDL_FALSE;
  383. }
  384. #endif /* __WIN32__ || __WINGDK__ */
  385. #endif /* !SDL_VIDEO_DRIVER_WINDOWS */
  386. /* vi: set ts=4 sw=4 expandtab: */