SDL_windows.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  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 "SDL_error.h"
  22. #include "SDL_system.h"
  23. #include <objbase.h> /* for CoInitialize/CoUninitialize (Win32 only) */
  24. #if defined(HAVE_ROAPI_H)
  25. #include <roapi.h> /* For RoInitialize/RoUninitialize (Win32 only) */
  26. #else
  27. typedef enum RO_INIT_TYPE
  28. {
  29. RO_INIT_SINGLETHREADED = 0,
  30. RO_INIT_MULTITHREADED = 1
  31. } RO_INIT_TYPE;
  32. #endif
  33. #ifndef _WIN32_WINNT_VISTA
  34. #define _WIN32_WINNT_VISTA 0x0600
  35. #endif
  36. #ifndef _WIN32_WINNT_WIN7
  37. #define _WIN32_WINNT_WIN7 0x0601
  38. #endif
  39. #ifndef _WIN32_WINNT_WIN8
  40. #define _WIN32_WINNT_WIN8 0x0602
  41. #endif
  42. #ifndef LOAD_LIBRARY_SEARCH_SYSTEM32
  43. #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
  44. #endif
  45. /* Sets an error message based on an HRESULT */
  46. int WIN_SetErrorFromHRESULT(const char *prefix, HRESULT hr)
  47. {
  48. TCHAR buffer[1024];
  49. char *message;
  50. TCHAR *p = buffer;
  51. DWORD c = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0,
  52. buffer, SDL_arraysize(buffer), NULL);
  53. buffer[c] = 0;
  54. /* kill CR/LF that FormatMessage() sticks at the end */
  55. while (*p) {
  56. if (*p == '\r') {
  57. *p = 0;
  58. break;
  59. }
  60. ++p;
  61. }
  62. message = WIN_StringToUTF8(buffer);
  63. SDL_SetError("%s%s%s", prefix ? prefix : "", prefix ? ": " : "", message);
  64. SDL_free(message);
  65. return -1;
  66. }
  67. /* Sets an error message based on GetLastError() */
  68. int WIN_SetError(const char *prefix)
  69. {
  70. return WIN_SetErrorFromHRESULT(prefix, GetLastError());
  71. }
  72. HRESULT
  73. WIN_CoInitialize(void)
  74. {
  75. /* SDL handles any threading model, so initialize with the default, which
  76. is compatible with OLE and if that doesn't work, try multi-threaded mode.
  77. If you need multi-threaded mode, call CoInitializeEx() before SDL_Init()
  78. */
  79. #ifdef __WINRT__
  80. /* DLudwig: On WinRT, it is assumed that COM was initialized in main().
  81. CoInitializeEx is available (not CoInitialize though), however
  82. on WinRT, main() is typically declared with the [MTAThread]
  83. attribute, which, AFAIK, should initialize COM.
  84. */
  85. return S_OK;
  86. #elif defined(__XBOXONE__) || defined(__XBOXSERIES__)
  87. /* On Xbox, there's no need to call CoInitializeEx (and it's not implemented) */
  88. return S_OK;
  89. #else
  90. HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  91. if (hr == RPC_E_CHANGED_MODE) {
  92. hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  93. }
  94. /* S_FALSE means success, but someone else already initialized. */
  95. /* You still need to call CoUninitialize in this case! */
  96. if (hr == S_FALSE) {
  97. return S_OK;
  98. }
  99. return hr;
  100. #endif
  101. }
  102. void WIN_CoUninitialize(void)
  103. {
  104. #ifndef __WINRT__
  105. CoUninitialize();
  106. #endif
  107. }
  108. #ifndef __WINRT__
  109. void *WIN_LoadComBaseFunction(const char *name)
  110. {
  111. static SDL_bool s_bLoaded;
  112. static HMODULE s_hComBase;
  113. if (!s_bLoaded) {
  114. s_hComBase = LoadLibraryEx(TEXT("combase.dll"), NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
  115. s_bLoaded = SDL_TRUE;
  116. }
  117. if (s_hComBase) {
  118. return GetProcAddress(s_hComBase, name);
  119. } else {
  120. return NULL;
  121. }
  122. }
  123. #endif
  124. HRESULT
  125. WIN_RoInitialize(void)
  126. {
  127. #ifdef __WINRT__
  128. return S_OK;
  129. #else
  130. typedef HRESULT(WINAPI * RoInitialize_t)(RO_INIT_TYPE initType);
  131. RoInitialize_t RoInitializeFunc = (RoInitialize_t)WIN_LoadComBaseFunction("RoInitialize");
  132. if (RoInitializeFunc) {
  133. /* RO_INIT_SINGLETHREADED is equivalent to COINIT_APARTMENTTHREADED */
  134. HRESULT hr = RoInitializeFunc(RO_INIT_SINGLETHREADED);
  135. if (hr == RPC_E_CHANGED_MODE) {
  136. hr = RoInitializeFunc(RO_INIT_MULTITHREADED);
  137. }
  138. /* S_FALSE means success, but someone else already initialized. */
  139. /* You still need to call RoUninitialize in this case! */
  140. if (hr == S_FALSE) {
  141. return S_OK;
  142. }
  143. return hr;
  144. } else {
  145. return E_NOINTERFACE;
  146. }
  147. #endif
  148. }
  149. void WIN_RoUninitialize(void)
  150. {
  151. #ifndef __WINRT__
  152. typedef void(WINAPI * RoUninitialize_t)(void);
  153. RoUninitialize_t RoUninitializeFunc = (RoUninitialize_t)WIN_LoadComBaseFunction("RoUninitialize");
  154. if (RoUninitializeFunc) {
  155. RoUninitializeFunc();
  156. }
  157. #endif
  158. }
  159. #if !defined(__WINRT__) && !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  160. static BOOL IsWindowsVersionOrGreater(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor)
  161. {
  162. OSVERSIONINFOEXW osvi;
  163. DWORDLONG const dwlConditionMask = VerSetConditionMask(
  164. VerSetConditionMask(
  165. VerSetConditionMask(
  166. 0, VER_MAJORVERSION, VER_GREATER_EQUAL),
  167. VER_MINORVERSION, VER_GREATER_EQUAL),
  168. VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
  169. SDL_zero(osvi);
  170. osvi.dwOSVersionInfoSize = sizeof(osvi);
  171. osvi.dwMajorVersion = wMajorVersion;
  172. osvi.dwMinorVersion = wMinorVersion;
  173. osvi.wServicePackMajor = wServicePackMajor;
  174. return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
  175. }
  176. #endif
  177. BOOL WIN_IsWindowsVistaOrGreater(void)
  178. {
  179. #if defined(__WINRT__) || defined(__XBOXONE__) || defined(__XBOXSERIES__)
  180. return TRUE;
  181. #else
  182. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 0);
  183. #endif
  184. }
  185. BOOL WIN_IsWindows7OrGreater(void)
  186. {
  187. #if defined(__WINRT__) || defined(__XBOXONE__) || defined(__XBOXSERIES__)
  188. return TRUE;
  189. #else
  190. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN7), LOBYTE(_WIN32_WINNT_WIN7), 0);
  191. #endif
  192. }
  193. BOOL WIN_IsWindows8OrGreater(void)
  194. {
  195. #if defined(__WINRT__) || defined(__XBOXONE__) || defined(__XBOXSERIES__)
  196. return TRUE;
  197. #else
  198. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN8), LOBYTE(_WIN32_WINNT_WIN8), 0);
  199. #endif
  200. }
  201. /*
  202. WAVExxxCAPS gives you 31 bytes for the device name, and just truncates if it's
  203. longer. However, since WinXP, you can use the WAVExxxCAPS2 structure, which
  204. will give you a name GUID. The full name is in the Windows Registry under
  205. that GUID, located here: HKLM\System\CurrentControlSet\Control\MediaCategories
  206. Note that drivers can report GUID_NULL for the name GUID, in which case,
  207. Windows makes a best effort to fill in those 31 bytes in the usual place.
  208. This info summarized from MSDN:
  209. http://web.archive.org/web/20131027093034/http://msdn.microsoft.com/en-us/library/windows/hardware/ff536382(v=vs.85).aspx
  210. Always look this up in the registry if possible, because the strings are
  211. different! At least on Win10, I see "Yeti Stereo Microphone" in the
  212. Registry, and a unhelpful "Microphone(Yeti Stereo Microph" in winmm. Sigh.
  213. (Also, DirectSound shouldn't be limited to 32 chars, but its device enum
  214. has the same problem.)
  215. WASAPI doesn't need this. This is just for DirectSound/WinMM.
  216. */
  217. char *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. BOOL WIN_IsRectEmpty(const RECT *rect)
  289. {
  290. /* Calculating this manually because UWP and Xbox do not support Win32 IsRectEmpty. */
  291. return (rect->right <= rect->left) || (rect->bottom <= rect->top);
  292. }
  293. #endif /* defined(__WIN32__) || defined(__WINRT__) || defined(__GDK__) */
  294. /*
  295. * Public APIs
  296. */
  297. #if !defined(SDL_VIDEO_DRIVER_WINDOWS)
  298. #if defined(__WIN32__) || defined(__GDK__)
  299. int SDL_RegisterApp(const char *name, Uint32 style, void *hInst)
  300. {
  301. (void)name;
  302. (void)style;
  303. (void)hInst;
  304. return 0;
  305. }
  306. void SDL_UnregisterApp(void)
  307. {
  308. }
  309. void SDL_SetWindowsMessageHook(SDL_WindowsMessageHook callback, void *userdata)
  310. {
  311. }
  312. #endif /* __WIN32__ || __GDK__ */
  313. #if defined(__WIN32__) || defined(__WINGDK__)
  314. int SDL_Direct3D9GetAdapterIndex(int displayIndex)
  315. {
  316. (void)displayIndex;
  317. return 0; /* D3DADAPTER_DEFAULT */
  318. }
  319. SDL_bool SDL_DXGIGetOutputInfo(int displayIndex, int *adapterIndex, int *outputIndex)
  320. {
  321. (void)displayIndex;
  322. if (adapterIndex) {
  323. *adapterIndex = -1;
  324. }
  325. if (outputIndex) {
  326. *outputIndex = -1;
  327. }
  328. return SDL_FALSE;
  329. }
  330. #endif /* __WIN32__ || __WINGDK__ */
  331. #endif /* !SDL_VIDEO_DRIVER_WINDOWS */
  332. /* vi: set ts=4 sw=4 expandtab: */