platform-windows.cpp 9.6 KB

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