platform-windows.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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. #include <qt-wrappers.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, string &output)
  34. {
  35. ostringstream str;
  36. str << path << data;
  37. output = str.str();
  38. blog(LOG_DEBUG, "Attempted path: %s", output.c_str());
  39. return os_file_exists(output.c_str());
  40. }
  41. bool GetDataFilePath(const char *data, string &output)
  42. {
  43. if (check_path(data, "data/obs-studio/", output))
  44. return true;
  45. return check_path(data, OBS_DATA_PATH "/obs-studio/", output);
  46. }
  47. string GetDefaultVideoSavePath()
  48. {
  49. wchar_t path_utf16[MAX_PATH];
  50. char path_utf8[MAX_PATH] = {};
  51. SHGetFolderPathW(NULL, CSIDL_MYVIDEO, NULL, SHGFP_TYPE_CURRENT, path_utf16);
  52. os_wcs_to_utf8(path_utf16, wcslen(path_utf16), path_utf8, MAX_PATH);
  53. return string(path_utf8);
  54. }
  55. static vector<string> GetUserPreferredLocales()
  56. {
  57. vector<string> result;
  58. ULONG num, length = 0;
  59. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num, nullptr, &length))
  60. return result;
  61. vector<wchar_t> buffer(length);
  62. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num, &buffer.front(), &length))
  63. return result;
  64. result.reserve(num);
  65. auto start = begin(buffer);
  66. auto end_ = end(buffer);
  67. decltype(start) separator;
  68. while ((separator = find(start, end_, 0)) != end_) {
  69. if (result.size() == num)
  70. break;
  71. char conv[MAX_PATH] = {};
  72. os_wcs_to_utf8(&*start, separator - start, conv, MAX_PATH);
  73. result.emplace_back(conv);
  74. start = separator + 1;
  75. }
  76. return result;
  77. }
  78. vector<string> GetPreferredLocales()
  79. {
  80. vector<string> windows_locales = GetUserPreferredLocales();
  81. auto obs_locales = GetLocaleNames();
  82. auto windows_to_obs = [&obs_locales](string windows) {
  83. string lang_match;
  84. for (auto &locale_pair : obs_locales) {
  85. auto &locale = locale_pair.first;
  86. if (locale == windows.substr(0, locale.size()))
  87. return locale;
  88. if (lang_match.size())
  89. continue;
  90. if (locale.substr(0, 2) == windows.substr(0, 2))
  91. lang_match = locale;
  92. }
  93. return lang_match;
  94. };
  95. vector<string> result;
  96. result.reserve(obs_locales.size());
  97. for (const string &locale : windows_locales) {
  98. string match = windows_to_obs(locale);
  99. if (!match.size())
  100. continue;
  101. if (find(begin(result), end(result), match) != end(result))
  102. continue;
  103. result.emplace_back(match);
  104. }
  105. return result;
  106. }
  107. uint32_t GetWindowsVersion()
  108. {
  109. static uint32_t ver = 0;
  110. if (ver == 0) {
  111. struct win_version_info ver_info;
  112. get_win_ver(&ver_info);
  113. ver = (ver_info.major << 8) | ver_info.minor;
  114. }
  115. return ver;
  116. }
  117. uint32_t GetWindowsBuild()
  118. {
  119. static uint32_t build = 0;
  120. if (build == 0) {
  121. struct win_version_info ver_info;
  122. get_win_ver(&ver_info);
  123. build = ver_info.build;
  124. }
  125. return build;
  126. }
  127. bool IsAlwaysOnTop(QWidget *window)
  128. {
  129. DWORD exStyle = GetWindowLong((HWND)window->winId(), GWL_EXSTYLE);
  130. return (exStyle & WS_EX_TOPMOST) != 0;
  131. }
  132. void SetAlwaysOnTop(QWidget *window, bool enable)
  133. {
  134. HWND hwnd = (HWND)window->winId();
  135. SetWindowPos(hwnd, enable ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0,
  136. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  137. }
  138. void SetProcessPriority(const char *priority)
  139. {
  140. if (!priority)
  141. return;
  142. if (strcmp(priority, "High") == 0)
  143. SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
  144. else if (strcmp(priority, "AboveNormal") == 0)
  145. SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
  146. else if (strcmp(priority, "Normal") == 0)
  147. SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
  148. else if (strcmp(priority, "BelowNormal") == 0)
  149. SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
  150. else if (strcmp(priority, "Idle") == 0)
  151. SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  152. }
  153. void SetWin32DropStyle(QWidget *window)
  154. {
  155. HWND hwnd = (HWND)window->winId();
  156. LONG_PTR ex_style = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  157. ex_style |= WS_EX_ACCEPTFILES;
  158. SetWindowLongPtr(hwnd, GWL_EXSTYLE, ex_style);
  159. }
  160. bool SetDisplayAffinitySupported(void)
  161. {
  162. static bool checked = false;
  163. static bool supported;
  164. /* this has to be version gated as setting WDA_EXCLUDEFROMCAPTURE on
  165. older Windows builds behaves like WDA_MONITOR (black box) */
  166. if (!checked) {
  167. if (GetWindowsVersion() > 0x0A00 || GetWindowsVersion() == 0x0A00 && GetWindowsBuild() >= 19041)
  168. supported = true;
  169. else
  170. supported = false;
  171. checked = true;
  172. }
  173. return supported;
  174. }
  175. bool DisableAudioDucking(bool disable)
  176. {
  177. ComPtr<IMMDeviceEnumerator> devEmum;
  178. ComPtr<IMMDevice> device;
  179. ComPtr<IAudioSessionManager2> sessionManager2;
  180. ComPtr<IAudioSessionControl> sessionControl;
  181. ComPtr<IAudioSessionControl2> sessionControl2;
  182. HRESULT result = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER,
  183. __uuidof(IMMDeviceEnumerator), (void **)&devEmum);
  184. if (FAILED(result))
  185. return false;
  186. result = devEmum->GetDefaultAudioEndpoint(eRender, eConsole, &device);
  187. if (FAILED(result))
  188. return false;
  189. result = device->Activate(__uuidof(IAudioSessionManager2), CLSCTX_INPROC_SERVER, nullptr,
  190. (void **)&sessionManager2);
  191. if (FAILED(result))
  192. return false;
  193. result = sessionManager2->GetAudioSessionControl(nullptr, 0, &sessionControl);
  194. if (FAILED(result))
  195. return false;
  196. result = sessionControl->QueryInterface(&sessionControl2);
  197. if (FAILED(result))
  198. return false;
  199. result = sessionControl2->SetDuckingPreference(disable);
  200. return SUCCEEDED(result);
  201. }
  202. struct RunOnceMutexData {
  203. WinHandle handle;
  204. inline RunOnceMutexData(HANDLE h) : handle(h) {}
  205. };
  206. RunOnceMutex::RunOnceMutex(RunOnceMutex &&rom)
  207. {
  208. delete data;
  209. data = rom.data;
  210. rom.data = nullptr;
  211. }
  212. RunOnceMutex::~RunOnceMutex()
  213. {
  214. delete data;
  215. }
  216. RunOnceMutex &RunOnceMutex::operator=(RunOnceMutex &&rom)
  217. {
  218. delete data;
  219. data = rom.data;
  220. rom.data = nullptr;
  221. return *this;
  222. }
  223. RunOnceMutex CheckIfAlreadyRunning(bool &already_running)
  224. {
  225. string name;
  226. if (!portable_mode) {
  227. name = "OBSStudioCore";
  228. } else {
  229. char path[500];
  230. char absPath[512];
  231. *path = 0;
  232. *absPath = 0;
  233. GetAppConfigPath(path, sizeof(path), "");
  234. os_get_abs_path(path, absPath, sizeof(absPath));
  235. name = "OBSStudioPortable";
  236. name += absPath;
  237. }
  238. BPtr<wchar_t> wname;
  239. os_utf8_to_wcs_ptr(name.c_str(), name.size(), &wname);
  240. if (wname) {
  241. wchar_t *temp = wname;
  242. while (*temp) {
  243. if (!iswalnum(*temp))
  244. *temp = L'_';
  245. temp++;
  246. }
  247. }
  248. HANDLE h = OpenMutexW(SYNCHRONIZE, false, wname.Get());
  249. already_running = !!h;
  250. if (!already_running)
  251. h = CreateMutexW(nullptr, false, wname.Get());
  252. RunOnceMutex rom(h ? new RunOnceMutexData(h) : nullptr);
  253. return rom;
  254. }
  255. struct MonitorData {
  256. const wchar_t *id;
  257. MONITORINFOEX info;
  258. bool found;
  259. };
  260. static BOOL CALLBACK GetMonitorCallback(HMONITOR monitor, HDC, LPRECT, LPARAM param)
  261. {
  262. MonitorData *data = (MonitorData *)param;
  263. if (GetMonitorInfoW(monitor, &data->info)) {
  264. if (wcscmp(data->info.szDevice, data->id) == 0) {
  265. data->found = true;
  266. return false;
  267. }
  268. }
  269. return true;
  270. }
  271. /* Based on https://www.winehq.org/pipermail/wine-devel/2008-September/069387.html */
  272. typedef const char *(CDECL *WINEGETVERSION)(void);
  273. bool IsRunningOnWine()
  274. {
  275. WINEGETVERSION func;
  276. HMODULE nt;
  277. nt = GetModuleHandleW(L"ntdll");
  278. if (!nt)
  279. return false;
  280. func = (WINEGETVERSION)GetProcAddress(nt, "wine_get_version");
  281. if (func) {
  282. blog(LOG_WARNING, "Running on Wine version \"%s\"", func());
  283. return true;
  284. }
  285. return false;
  286. }
  287. HWND hwnd;
  288. void TaskbarOverlayInit()
  289. {
  290. hwnd = (HWND)App()->GetMainWindow()->winId();
  291. }
  292. void TaskbarOverlaySetStatus(TaskbarOverlayStatus status)
  293. {
  294. ITaskbarList4 *taskbarIcon;
  295. auto hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&taskbarIcon));
  296. if (FAILED(hr)) {
  297. taskbarIcon->Release();
  298. return;
  299. }
  300. hr = taskbarIcon->HrInit();
  301. if (FAILED(hr)) {
  302. taskbarIcon->Release();
  303. return;
  304. }
  305. QIcon qicon;
  306. switch (status) {
  307. case TaskbarOverlayStatusActive:
  308. qicon = QIcon::fromTheme("obs-active", QIcon(":/res/images/active.png"));
  309. break;
  310. case TaskbarOverlayStatusPaused:
  311. qicon = QIcon::fromTheme("obs-paused", QIcon(":/res/images/paused.png"));
  312. break;
  313. case TaskbarOverlayStatusInactive:
  314. taskbarIcon->SetOverlayIcon(hwnd, nullptr, nullptr);
  315. taskbarIcon->Release();
  316. return;
  317. }
  318. HICON hicon = nullptr;
  319. if (!qicon.isNull()) {
  320. Q_GUI_EXPORT HICON qt_pixmapToWinHICON(const QPixmap &p);
  321. hicon = qt_pixmapToWinHICON(qicon.pixmap(GetSystemMetrics(SM_CXSMICON)));
  322. if (!hicon)
  323. return;
  324. }
  325. taskbarIcon->SetOverlayIcon(hwnd, hicon, nullptr);
  326. DestroyIcon(hicon);
  327. taskbarIcon->Release();
  328. }
  329. bool HighContrastEnabled()
  330. {
  331. HIGHCONTRAST hc = {};
  332. hc.cbSize = sizeof(HIGHCONTRAST);
  333. if (SystemParametersInfo(SPI_GETHIGHCONTRAST, hc.cbSize, &hc, 0))
  334. return hc.dwFlags & HCF_HIGHCONTRASTON;
  335. return false;
  336. }