platform-windows.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <psapi.h>
  28. #include <mmdeviceapi.h>
  29. #include <audiopolicy.h>
  30. #include <util/windows/WinHandle.hpp>
  31. #include <util/windows/HRError.hpp>
  32. #include <util/windows/ComPtr.hpp>
  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,
  66. nullptr, &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)>(GetProcAddress(dwm,
  139. "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(), ABOVE_NORMAL_PRIORITY_CLASS);
  166. else if (strcmp(priority, "Normal") == 0)
  167. SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
  168. else if (strcmp(priority, "BelowNormal") == 0)
  169. SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
  170. else if (strcmp(priority, "Idle") == 0)
  171. SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  172. }
  173. void SetWin32DropStyle(QWidget *window)
  174. {
  175. HWND hwnd = (HWND)window->winId();
  176. LONG_PTR ex_style = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  177. ex_style |= WS_EX_ACCEPTFILES;
  178. SetWindowLongPtr(hwnd, GWL_EXSTYLE, ex_style);
  179. }
  180. bool DisableAudioDucking(bool disable)
  181. {
  182. ComPtr<IMMDeviceEnumerator> devEmum;
  183. ComPtr<IMMDevice> device;
  184. ComPtr<IAudioSessionManager2> sessionManager2;
  185. ComPtr<IAudioSessionControl> sessionControl;
  186. ComPtr<IAudioSessionControl2> sessionControl2;
  187. HRESULT result = CoCreateInstance(__uuidof(MMDeviceEnumerator),
  188. nullptr, CLSCTX_INPROC_SERVER,
  189. __uuidof(IMMDeviceEnumerator),
  190. (void **)&devEmum);
  191. if (FAILED(result))
  192. return false;
  193. result = devEmum->GetDefaultAudioEndpoint(eRender, eConsole, &device);
  194. if (FAILED(result))
  195. return false;
  196. result = device->Activate(__uuidof(IAudioSessionManager2),
  197. CLSCTX_INPROC_SERVER, nullptr,
  198. (void **)&sessionManager2);
  199. if (FAILED(result))
  200. return false;
  201. result = sessionManager2->GetAudioSessionControl(nullptr, 0,
  202. &sessionControl);
  203. if (FAILED(result))
  204. return false;
  205. result = sessionControl->QueryInterface(&sessionControl2);
  206. if (FAILED(result))
  207. return false;
  208. result = sessionControl2->SetDuckingPreference(disable);
  209. return SUCCEEDED(result);
  210. }
  211. uint64_t CurrentMemoryUsage()
  212. {
  213. PROCESS_MEMORY_COUNTERS pmc = {};
  214. pmc.cb = sizeof(pmc);
  215. if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
  216. return 0;
  217. return (uint64_t)pmc.WorkingSetSize;
  218. }
  219. struct RunOnceMutexData {
  220. WinHandle handle;
  221. inline RunOnceMutexData(HANDLE h) : handle(h) {}
  222. };
  223. RunOnceMutex::RunOnceMutex(RunOnceMutex &&rom)
  224. {
  225. delete data;
  226. data = rom.data;
  227. rom.data = nullptr;
  228. }
  229. RunOnceMutex::~RunOnceMutex()
  230. {
  231. delete data;
  232. }
  233. RunOnceMutex &RunOnceMutex::operator=(RunOnceMutex &&rom)
  234. {
  235. delete data;
  236. data = rom.data;
  237. rom.data = nullptr;
  238. return *this;
  239. }
  240. RunOnceMutex GetRunOnceMutex(bool &already_running)
  241. {
  242. string name;
  243. if (!portable_mode) {
  244. name = "OBSStudioCore";
  245. } else {
  246. char path[500];
  247. *path = 0;
  248. GetConfigPath(path, sizeof(path), "");
  249. name = "OBSStudioPortable";
  250. name += path;
  251. }
  252. BPtr<wchar_t> wname;
  253. os_utf8_to_wcs_ptr(name.c_str(), name.size(), &wname);
  254. if (wname) {
  255. wchar_t *temp = wname;
  256. while (*temp) {
  257. if (!iswalnum(*temp))
  258. *temp = L'_';
  259. temp++;
  260. }
  261. }
  262. HANDLE h = OpenMutexW(SYNCHRONIZE, false, wname.Get());
  263. already_running = !!h;
  264. if (!already_running)
  265. h = CreateMutexW(nullptr, false, wname.Get());
  266. RunOnceMutex rom(h ? new RunOnceMutexData(h) : nullptr);
  267. return rom;
  268. }