platform-windows.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <algorithm>
  15. #include <sstream>
  16. #include "obs-config.h"
  17. #include "obs-app.hpp"
  18. #include "platform.hpp"
  19. using namespace std;
  20. #include <util/windows/win-version.h>
  21. #include <util/platform.h>
  22. #define WIN32_LEAN_AND_MEAN
  23. #include <windows.h>
  24. #include <shellapi.h>
  25. #include <shlobj.h>
  26. #include <Dwmapi.h>
  27. #include <mmdeviceapi.h>
  28. #include <audiopolicy.h>
  29. #include <util/windows/HRError.hpp>
  30. #include <util/windows/ComPtr.hpp>
  31. static inline bool check_path(const char* data, const char *path,
  32. string &output)
  33. {
  34. ostringstream str;
  35. str << path << data;
  36. output = str.str();
  37. printf("Attempted path: %s\n", output.c_str());
  38. return os_file_exists(output.c_str());
  39. }
  40. bool GetDataFilePath(const char *data, string &output)
  41. {
  42. if (check_path(data, "data/obs-studio/", output))
  43. return true;
  44. return check_path(data, OBS_DATA_PATH "/obs-studio/", output);
  45. }
  46. bool InitApplicationBundle()
  47. {
  48. return true;
  49. }
  50. string GetDefaultVideoSavePath()
  51. {
  52. wchar_t path_utf16[MAX_PATH];
  53. char path_utf8[MAX_PATH] = {};
  54. SHGetFolderPathW(NULL, CSIDL_MYVIDEO, NULL, SHGFP_TYPE_CURRENT,
  55. path_utf16);
  56. os_wcs_to_utf8(path_utf16, wcslen(path_utf16), path_utf8, MAX_PATH);
  57. return string(path_utf8);
  58. }
  59. static vector<string> GetUserPreferredLocales()
  60. {
  61. vector<string> result;
  62. ULONG num, length = 0;
  63. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
  64. nullptr, &length))
  65. return result;
  66. vector<wchar_t> buffer(length);
  67. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
  68. &buffer.front(), &length))
  69. return result;
  70. result.reserve(num);
  71. auto start = begin(buffer);
  72. auto end_ = end(buffer);
  73. decltype(start) separator;
  74. while ((separator = find(start, end_, 0)) != end_) {
  75. if (result.size() == num)
  76. break;
  77. char conv[MAX_PATH] = {};
  78. os_wcs_to_utf8(&*start, separator - start, conv, MAX_PATH);
  79. result.emplace_back(conv);
  80. start = separator + 1;
  81. }
  82. return result;
  83. }
  84. vector<string> GetPreferredLocales()
  85. {
  86. vector<string> windows_locales = GetUserPreferredLocales();
  87. auto obs_locales = GetLocaleNames();
  88. auto windows_to_obs = [&obs_locales](string windows) {
  89. string lang_match;
  90. for (auto &locale_pair : obs_locales) {
  91. auto &locale = locale_pair.first;
  92. if (locale == windows.substr(0, locale.size()))
  93. return locale;
  94. if (lang_match.size())
  95. continue;
  96. if (locale.substr(0, 2) == windows.substr(0, 2))
  97. lang_match = locale;
  98. }
  99. return lang_match;
  100. };
  101. vector<string> result;
  102. result.reserve(obs_locales.size());
  103. for (const string &locale : windows_locales) {
  104. string match = windows_to_obs(locale);
  105. if (!match.size())
  106. continue;
  107. if (find(begin(result), end(result), match) != end(result))
  108. continue;
  109. result.emplace_back(match);
  110. }
  111. return result;
  112. }
  113. uint32_t GetWindowsVersion()
  114. {
  115. static uint32_t ver = 0;
  116. if (ver == 0) {
  117. struct win_version_info ver_info;
  118. get_win_ver(&ver_info);
  119. ver = (ver_info.major << 8) | ver_info.minor;
  120. }
  121. return ver;
  122. }
  123. void SetAeroEnabled(bool enable)
  124. {
  125. static HRESULT (WINAPI *func)(UINT) = nullptr;
  126. static bool failed = false;
  127. if (!func) {
  128. if (failed) {
  129. return;
  130. }
  131. HMODULE dwm = LoadLibraryW(L"dwmapi");
  132. if (!dwm) {
  133. failed = true;
  134. return;
  135. }
  136. func = reinterpret_cast<decltype(func)>(GetProcAddress(dwm,
  137. "DwmEnableComposition"));
  138. if (!func) {
  139. failed = true;
  140. return;
  141. }
  142. }
  143. func(enable ? DWM_EC_ENABLECOMPOSITION : DWM_EC_DISABLECOMPOSITION);
  144. }
  145. bool IsAlwaysOnTop(QWidget *window)
  146. {
  147. DWORD exStyle = GetWindowLong((HWND)window->winId(), GWL_EXSTYLE);
  148. return (exStyle & WS_EX_TOPMOST) != 0;
  149. }
  150. void SetAlwaysOnTop(QWidget *window, bool enable)
  151. {
  152. HWND hwnd = (HWND)window->winId();
  153. SetWindowPos(hwnd, enable ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0,
  154. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  155. }
  156. void SetProcessPriority(const char *priority)
  157. {
  158. if (!priority)
  159. return;
  160. if (strcmp(priority, "High") == 0)
  161. SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
  162. else if (strcmp(priority, "AboveNormal") == 0)
  163. SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
  164. else if (strcmp(priority, "Normal") == 0)
  165. SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
  166. else if (strcmp(priority, "Idle") == 0)
  167. SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  168. }
  169. void SetWin32DropStyle(QWidget *window)
  170. {
  171. HWND hwnd = (HWND)window->winId();
  172. LONG_PTR ex_style = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  173. ex_style |= WS_EX_ACCEPTFILES;
  174. SetWindowLongPtr(hwnd, GWL_EXSTYLE, ex_style);
  175. }
  176. bool DisableAudioDucking(bool disable)
  177. {
  178. ComPtr<IMMDeviceEnumerator> devEmum;
  179. ComPtr<IMMDevice> device;
  180. ComPtr<IAudioSessionManager2> sessionManager2;
  181. ComPtr<IAudioSessionControl> sessionControl;
  182. ComPtr<IAudioSessionControl2> sessionControl2;
  183. HRESULT result = CoCreateInstance(__uuidof(MMDeviceEnumerator),
  184. nullptr, CLSCTX_INPROC_SERVER,
  185. __uuidof(IMMDeviceEnumerator),
  186. (void **)&devEmum);
  187. if (FAILED(result))
  188. return false;
  189. result = devEmum->GetDefaultAudioEndpoint(eRender, eConsole, &device);
  190. if (FAILED(result))
  191. return false;
  192. result = device->Activate(__uuidof(IAudioSessionManager2),
  193. CLSCTX_INPROC_SERVER, nullptr,
  194. (void **)&sessionManager2);
  195. if (FAILED(result))
  196. return false;
  197. result = sessionManager2->GetAudioSessionControl(nullptr, 0,
  198. &sessionControl);
  199. if (FAILED(result))
  200. return false;
  201. result = sessionControl->QueryInterface(&sessionControl2);
  202. if (FAILED(result))
  203. return false;
  204. result = sessionControl2->SetDuckingPreference(disable);
  205. return SUCCEEDED(result);
  206. }