1
0

platform-windows.cpp 9.7 KB

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