platform-windows.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 "qt-wrappers.hpp"
  19. #include "platform.hpp"
  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/WinHandle.hpp>
  30. #include <util/windows/HRError.hpp>
  31. #include <util/windows/ComPtr.hpp>
  32. using namespace std;
  33. static inline bool check_path(const char *data, const char *path,
  34. string &output)
  35. {
  36. ostringstream str;
  37. str << path << data;
  38. output = str.str();
  39. printf("Attempted path: %s\n", output.c_str());
  40. return os_file_exists(output.c_str());
  41. }
  42. bool GetDataFilePath(const char *data, string &output)
  43. {
  44. if (check_path(data, "data/obs-studio/", output))
  45. return true;
  46. return check_path(data, OBS_DATA_PATH "/obs-studio/", output);
  47. }
  48. string GetDefaultVideoSavePath()
  49. {
  50. wchar_t path_utf16[MAX_PATH];
  51. char path_utf8[MAX_PATH] = {};
  52. SHGetFolderPathW(NULL, CSIDL_MYVIDEO, NULL, SHGFP_TYPE_CURRENT,
  53. path_utf16);
  54. os_wcs_to_utf8(path_utf16, wcslen(path_utf16), path_utf8, MAX_PATH);
  55. return string(path_utf8);
  56. }
  57. static vector<string> GetUserPreferredLocales()
  58. {
  59. vector<string> result;
  60. ULONG num, length = 0;
  61. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num, nullptr,
  62. &length))
  63. return result;
  64. vector<wchar_t> buffer(length);
  65. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
  66. &buffer.front(), &length))
  67. return result;
  68. result.reserve(num);
  69. auto start = begin(buffer);
  70. auto end_ = end(buffer);
  71. decltype(start) separator;
  72. while ((separator = find(start, end_, 0)) != end_) {
  73. if (result.size() == num)
  74. break;
  75. char conv[MAX_PATH] = {};
  76. os_wcs_to_utf8(&*start, separator - start, conv, MAX_PATH);
  77. result.emplace_back(conv);
  78. start = separator + 1;
  79. }
  80. return result;
  81. }
  82. vector<string> GetPreferredLocales()
  83. {
  84. vector<string> windows_locales = GetUserPreferredLocales();
  85. auto obs_locales = GetLocaleNames();
  86. auto windows_to_obs = [&obs_locales](string windows) {
  87. string lang_match;
  88. for (auto &locale_pair : obs_locales) {
  89. auto &locale = locale_pair.first;
  90. if (locale == windows.substr(0, locale.size()))
  91. return locale;
  92. if (lang_match.size())
  93. continue;
  94. if (locale.substr(0, 2) == windows.substr(0, 2))
  95. lang_match = locale;
  96. }
  97. return lang_match;
  98. };
  99. vector<string> result;
  100. result.reserve(obs_locales.size());
  101. for (const string &locale : windows_locales) {
  102. string match = windows_to_obs(locale);
  103. if (!match.size())
  104. continue;
  105. if (find(begin(result), end(result), match) != end(result))
  106. continue;
  107. result.emplace_back(match);
  108. }
  109. return result;
  110. }
  111. uint32_t GetWindowsVersion()
  112. {
  113. static uint32_t ver = 0;
  114. if (ver == 0) {
  115. struct win_version_info ver_info;
  116. get_win_ver(&ver_info);
  117. ver = (ver_info.major << 8) | ver_info.minor;
  118. }
  119. return ver;
  120. }
  121. uint32_t GetWindowsBuild()
  122. {
  123. static uint32_t build = 0;
  124. if (build == 0) {
  125. struct win_version_info ver_info;
  126. get_win_ver(&ver_info);
  127. build = ver_info.build;
  128. }
  129. return build;
  130. }
  131. void SetAeroEnabled(bool enable)
  132. {
  133. static HRESULT(WINAPI * func)(UINT) = nullptr;
  134. static bool failed = false;
  135. if (!func) {
  136. if (failed) {
  137. return;
  138. }
  139. HMODULE dwm = LoadLibraryW(L"dwmapi");
  140. if (!dwm) {
  141. failed = true;
  142. return;
  143. }
  144. func = reinterpret_cast<decltype(func)>(
  145. GetProcAddress(dwm, "DwmEnableComposition"));
  146. if (!func) {
  147. failed = true;
  148. return;
  149. }
  150. }
  151. func(enable ? DWM_EC_ENABLECOMPOSITION : DWM_EC_DISABLECOMPOSITION);
  152. }
  153. bool IsAlwaysOnTop(QWidget *window)
  154. {
  155. DWORD exStyle = GetWindowLong((HWND)window->winId(), GWL_EXSTYLE);
  156. return (exStyle & WS_EX_TOPMOST) != 0;
  157. }
  158. void SetAlwaysOnTop(QWidget *window, bool enable)
  159. {
  160. HWND hwnd = (HWND)window->winId();
  161. SetWindowPos(hwnd, enable ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0,
  162. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  163. }
  164. void SetProcessPriority(const char *priority)
  165. {
  166. if (!priority)
  167. return;
  168. if (strcmp(priority, "High") == 0)
  169. SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
  170. else if (strcmp(priority, "AboveNormal") == 0)
  171. SetPriorityClass(GetCurrentProcess(),
  172. ABOVE_NORMAL_PRIORITY_CLASS);
  173. else if (strcmp(priority, "Normal") == 0)
  174. SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
  175. else if (strcmp(priority, "BelowNormal") == 0)
  176. SetPriorityClass(GetCurrentProcess(),
  177. BELOW_NORMAL_PRIORITY_CLASS);
  178. else if (strcmp(priority, "Idle") == 0)
  179. SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  180. }
  181. void SetWin32DropStyle(QWidget *window)
  182. {
  183. HWND hwnd = (HWND)window->winId();
  184. LONG_PTR ex_style = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  185. ex_style |= WS_EX_ACCEPTFILES;
  186. SetWindowLongPtr(hwnd, GWL_EXSTYLE, ex_style);
  187. }
  188. bool SetDisplayAffinitySupported(void)
  189. {
  190. static bool checked = false;
  191. static bool supported;
  192. /* this has to be version gated as setting WDA_EXCLUDEFROMCAPTURE on
  193. older Windows builds behaves like WDA_MONITOR (black box) */
  194. if (!checked) {
  195. if (GetWindowsVersion() > 0x0A00 ||
  196. GetWindowsVersion() == 0x0A00 && GetWindowsBuild() >= 19041)
  197. supported = true;
  198. else
  199. supported = false;
  200. checked = true;
  201. }
  202. return supported;
  203. }
  204. bool DisableAudioDucking(bool disable)
  205. {
  206. ComPtr<IMMDeviceEnumerator> devEmum;
  207. ComPtr<IMMDevice> device;
  208. ComPtr<IAudioSessionManager2> sessionManager2;
  209. ComPtr<IAudioSessionControl> sessionControl;
  210. ComPtr<IAudioSessionControl2> sessionControl2;
  211. HRESULT result = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr,
  212. CLSCTX_INPROC_SERVER,
  213. __uuidof(IMMDeviceEnumerator),
  214. (void **)&devEmum);
  215. if (FAILED(result))
  216. return false;
  217. result = devEmum->GetDefaultAudioEndpoint(eRender, eConsole, &device);
  218. if (FAILED(result))
  219. return false;
  220. result = device->Activate(__uuidof(IAudioSessionManager2),
  221. CLSCTX_INPROC_SERVER, nullptr,
  222. (void **)&sessionManager2);
  223. if (FAILED(result))
  224. return false;
  225. result = sessionManager2->GetAudioSessionControl(nullptr, 0,
  226. &sessionControl);
  227. if (FAILED(result))
  228. return false;
  229. result = sessionControl->QueryInterface(&sessionControl2);
  230. if (FAILED(result))
  231. return false;
  232. result = sessionControl2->SetDuckingPreference(disable);
  233. return SUCCEEDED(result);
  234. }
  235. struct RunOnceMutexData {
  236. WinHandle handle;
  237. inline RunOnceMutexData(HANDLE h) : handle(h) {}
  238. };
  239. RunOnceMutex::RunOnceMutex(RunOnceMutex &&rom)
  240. {
  241. delete data;
  242. data = rom.data;
  243. rom.data = nullptr;
  244. }
  245. RunOnceMutex::~RunOnceMutex()
  246. {
  247. delete data;
  248. }
  249. RunOnceMutex &RunOnceMutex::operator=(RunOnceMutex &&rom)
  250. {
  251. delete data;
  252. data = rom.data;
  253. rom.data = nullptr;
  254. return *this;
  255. }
  256. RunOnceMutex CheckIfAlreadyRunning(bool &already_running)
  257. {
  258. string name;
  259. if (!portable_mode) {
  260. name = "OBSStudioCore";
  261. } else {
  262. char path[500];
  263. char absPath[512];
  264. *path = 0;
  265. *absPath = 0;
  266. GetConfigPath(path, sizeof(path), "");
  267. os_get_abs_path(path, absPath, sizeof(absPath));
  268. name = "OBSStudioPortable";
  269. name += absPath;
  270. }
  271. BPtr<wchar_t> wname;
  272. os_utf8_to_wcs_ptr(name.c_str(), name.size(), &wname);
  273. if (wname) {
  274. wchar_t *temp = wname;
  275. while (*temp) {
  276. if (!iswalnum(*temp))
  277. *temp = L'_';
  278. temp++;
  279. }
  280. }
  281. HANDLE h = OpenMutexW(SYNCHRONIZE, false, wname.Get());
  282. already_running = !!h;
  283. if (!already_running)
  284. h = CreateMutexW(nullptr, false, wname.Get());
  285. RunOnceMutex rom(h ? new RunOnceMutexData(h) : nullptr);
  286. return rom;
  287. }
  288. struct MonitorData {
  289. const wchar_t *id;
  290. MONITORINFOEX info;
  291. bool found;
  292. };
  293. static BOOL CALLBACK GetMonitorCallback(HMONITOR monitor, HDC, LPRECT,
  294. LPARAM param)
  295. {
  296. MonitorData *data = (MonitorData *)param;
  297. if (GetMonitorInfoW(monitor, &data->info)) {
  298. if (wcscmp(data->info.szDevice, data->id) == 0) {
  299. data->found = true;
  300. return false;
  301. }
  302. }
  303. return true;
  304. }
  305. #define GENERIC_MONITOR_NAME QStringLiteral("Generic PnP Monitor")
  306. QString GetMonitorName(const QString &id)
  307. {
  308. MonitorData data = {};
  309. data.id = (const wchar_t *)id.utf16();
  310. data.info.cbSize = sizeof(data.info);
  311. EnumDisplayMonitors(nullptr, nullptr, GetMonitorCallback,
  312. (LPARAM)&data);
  313. if (!data.found) {
  314. return GENERIC_MONITOR_NAME;
  315. }
  316. UINT32 numPath, numMode;
  317. if (GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &numPath,
  318. &numMode) != ERROR_SUCCESS) {
  319. return GENERIC_MONITOR_NAME;
  320. }
  321. std::vector<DISPLAYCONFIG_PATH_INFO> paths(numPath);
  322. std::vector<DISPLAYCONFIG_MODE_INFO> modes(numMode);
  323. if (QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &numPath, paths.data(),
  324. &numMode, modes.data(),
  325. nullptr) != ERROR_SUCCESS) {
  326. return GENERIC_MONITOR_NAME;
  327. }
  328. DISPLAYCONFIG_TARGET_DEVICE_NAME target;
  329. bool found = false;
  330. paths.resize(numPath);
  331. for (size_t i = 0; i < numPath; ++i) {
  332. const DISPLAYCONFIG_PATH_INFO &path = paths[i];
  333. DISPLAYCONFIG_SOURCE_DEVICE_NAME s;
  334. s.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;
  335. s.header.size = sizeof(s);
  336. s.header.adapterId = path.sourceInfo.adapterId;
  337. s.header.id = path.sourceInfo.id;
  338. if (DisplayConfigGetDeviceInfo(&s.header) == ERROR_SUCCESS &&
  339. wcscmp(data.info.szDevice, s.viewGdiDeviceName) == 0) {
  340. target.header.type =
  341. DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME;
  342. target.header.size = sizeof(target);
  343. target.header.adapterId = path.sourceInfo.adapterId;
  344. target.header.id = path.targetInfo.id;
  345. found = DisplayConfigGetDeviceInfo(&target.header) ==
  346. ERROR_SUCCESS;
  347. break;
  348. }
  349. }
  350. if (!found) {
  351. return GENERIC_MONITOR_NAME;
  352. }
  353. return QString::fromWCharArray(target.monitorFriendlyDeviceName);
  354. }
  355. /* Based on https://www.winehq.org/pipermail/wine-devel/2008-September/069387.html */
  356. typedef const char *(CDECL *WINEGETVERSION)(void);
  357. bool IsRunningOnWine()
  358. {
  359. WINEGETVERSION func;
  360. HMODULE nt;
  361. nt = GetModuleHandleW(L"ntdll");
  362. if (!nt)
  363. return false;
  364. func = (WINEGETVERSION)GetProcAddress(nt, "wine_get_version");
  365. if (func) {
  366. blog(LOG_WARNING, "Running on Wine version \"%s\"", func());
  367. return true;
  368. }
  369. return false;
  370. }