SDL_windows.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 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__)
  20. #include "SDL_windows.h"
  21. #include "SDL_error.h"
  22. #include "SDL_assert.h"
  23. #include <objbase.h> /* for CoInitialize/CoUninitialize (Win32 only) */
  24. #ifndef _WIN32_WINNT_VISTA
  25. #define _WIN32_WINNT_VISTA 0x0600
  26. #endif
  27. /* Sets an error message based on GetLastError() */
  28. int
  29. WIN_SetErrorFromHRESULT(const char *prefix, HRESULT hr)
  30. {
  31. TCHAR buffer[1024];
  32. char *message;
  33. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0,
  34. buffer, SDL_arraysize(buffer), NULL);
  35. message = WIN_StringToUTF8(buffer);
  36. SDL_SetError("%s%s%s", prefix ? prefix : "", prefix ? ": " : "", message);
  37. SDL_free(message);
  38. return -1;
  39. }
  40. /* Sets an error message based on GetLastError() */
  41. int
  42. WIN_SetError(const char *prefix)
  43. {
  44. return WIN_SetErrorFromHRESULT(prefix, GetLastError());
  45. }
  46. HRESULT
  47. WIN_CoInitialize(void)
  48. {
  49. /* SDL handles any threading model, so initialize with the default, which
  50. is compatible with OLE and if that doesn't work, try multi-threaded mode.
  51. If you need multi-threaded mode, call CoInitializeEx() before SDL_Init()
  52. */
  53. #ifdef __WINRT__
  54. /* DLudwig: On WinRT, it is assumed that COM was initialized in main().
  55. CoInitializeEx is available (not CoInitialize though), however
  56. on WinRT, main() is typically declared with the [MTAThread]
  57. attribute, which, AFAIK, should initialize COM.
  58. */
  59. return S_OK;
  60. #else
  61. HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  62. if (hr == RPC_E_CHANGED_MODE) {
  63. hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  64. }
  65. /* S_FALSE means success, but someone else already initialized. */
  66. /* You still need to call CoUninitialize in this case! */
  67. if (hr == S_FALSE) {
  68. return S_OK;
  69. }
  70. return hr;
  71. #endif
  72. }
  73. void
  74. WIN_CoUninitialize(void)
  75. {
  76. #ifndef __WINRT__
  77. CoUninitialize();
  78. #endif
  79. }
  80. #ifndef __WINRT__
  81. static BOOL
  82. IsWindowsVersionOrGreater(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor)
  83. {
  84. OSVERSIONINFOEXW osvi;
  85. DWORDLONG const dwlConditionMask = VerSetConditionMask(
  86. VerSetConditionMask(
  87. VerSetConditionMask(
  88. 0, VER_MAJORVERSION, VER_GREATER_EQUAL ),
  89. VER_MINORVERSION, VER_GREATER_EQUAL ),
  90. VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL );
  91. SDL_zero(osvi);
  92. osvi.dwOSVersionInfoSize = sizeof(osvi);
  93. osvi.dwMajorVersion = wMajorVersion;
  94. osvi.dwMinorVersion = wMinorVersion;
  95. osvi.wServicePackMajor = wServicePackMajor;
  96. return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
  97. }
  98. #endif
  99. BOOL WIN_IsWindowsVistaOrGreater()
  100. {
  101. #ifdef __WINRT__
  102. return TRUE;
  103. #else
  104. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 0);
  105. #endif
  106. }
  107. /*
  108. WAVExxxCAPS gives you 31 bytes for the device name, and just truncates if it's
  109. longer. However, since WinXP, you can use the WAVExxxCAPS2 structure, which
  110. will give you a name GUID. The full name is in the Windows Registry under
  111. that GUID, located here: HKLM\System\CurrentControlSet\Control\MediaCategories
  112. Note that drivers can report GUID_NULL for the name GUID, in which case,
  113. Windows makes a best effort to fill in those 31 bytes in the usual place.
  114. This info summarized from MSDN:
  115. http://web.archive.org/web/20131027093034/http://msdn.microsoft.com/en-us/library/windows/hardware/ff536382(v=vs.85).aspx
  116. Always look this up in the registry if possible, because the strings are
  117. different! At least on Win10, I see "Yeti Stereo Microphone" in the
  118. Registry, and a unhelpful "Microphone(Yeti Stereo Microph" in winmm. Sigh.
  119. (Also, DirectSound shouldn't be limited to 32 chars, but its device enum
  120. has the same problem.)
  121. */
  122. char *
  123. WIN_LookupAudioDeviceName(const WCHAR *name, const GUID *guid)
  124. {
  125. #if __WINRT__
  126. return WIN_StringToUTF8(name); /* No registry access on WinRT/UWP, go with what we've got. */
  127. #else
  128. static const GUID nullguid = { 0 };
  129. const unsigned char *ptr;
  130. char keystr[128];
  131. WCHAR *strw = NULL;
  132. SDL_bool rc;
  133. HKEY hkey;
  134. DWORD len = 0;
  135. char *retval = NULL;
  136. if (SDL_memcmp(guid, &nullguid, sizeof (*guid)) == 0) {
  137. return WIN_StringToUTF8(name); /* No GUID, go with what we've got. */
  138. }
  139. ptr = (const unsigned char *) guid;
  140. SDL_snprintf(keystr, sizeof (keystr),
  141. "System\\CurrentControlSet\\Control\\MediaCategories\\{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  142. ptr[3], ptr[2], ptr[1], ptr[0], ptr[5], ptr[4], ptr[7], ptr[6],
  143. ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15]);
  144. strw = WIN_UTF8ToString(keystr);
  145. rc = (RegOpenKeyExW(HKEY_LOCAL_MACHINE, strw, 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS);
  146. SDL_free(strw);
  147. if (!rc) {
  148. return WIN_StringToUTF8(name); /* oh well. */
  149. }
  150. rc = (RegQueryValueExW(hkey, L"Name", NULL, NULL, NULL, &len) == ERROR_SUCCESS);
  151. if (!rc) {
  152. RegCloseKey(hkey);
  153. return WIN_StringToUTF8(name); /* oh well. */
  154. }
  155. strw = (WCHAR *) SDL_malloc(len + sizeof (WCHAR));
  156. if (!strw) {
  157. RegCloseKey(hkey);
  158. return WIN_StringToUTF8(name); /* oh well. */
  159. }
  160. rc = (RegQueryValueExW(hkey, L"Name", NULL, NULL, (LPBYTE) strw, &len) == ERROR_SUCCESS);
  161. RegCloseKey(hkey);
  162. if (!rc) {
  163. SDL_free(strw);
  164. return WIN_StringToUTF8(name); /* oh well. */
  165. }
  166. strw[len / 2] = 0; /* make sure it's null-terminated. */
  167. retval = WIN_StringToUTF8(strw);
  168. SDL_free(strw);
  169. return retval ? retval : WIN_StringToUTF8(name);
  170. #endif /* if __WINRT__ / else */
  171. }
  172. #endif /* __WIN32__ || __WINRT__ */
  173. /* vi: set ts=4 sw=4 expandtab: */