platform-windows.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <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. static inline bool check_path(const char *data, const char *path,
  33. string &output)
  34. {
  35. ostringstream str;
  36. str << path << data;
  37. output = str.str();
  38. printf("Attempted path: %s\n", 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. bool InitApplicationBundle()
  48. {
  49. return true;
  50. }
  51. string GetDefaultVideoSavePath()
  52. {
  53. wchar_t path_utf16[MAX_PATH];
  54. char path_utf8[MAX_PATH] = {};
  55. SHGetFolderPathW(NULL, CSIDL_MYVIDEO, NULL, SHGFP_TYPE_CURRENT,
  56. path_utf16);
  57. os_wcs_to_utf8(path_utf16, wcslen(path_utf16), path_utf8, MAX_PATH);
  58. return string(path_utf8);
  59. }
  60. static vector<string> GetUserPreferredLocales()
  61. {
  62. vector<string> result;
  63. ULONG num, length = 0;
  64. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num, nullptr,
  65. &length))
  66. return result;
  67. vector<wchar_t> buffer(length);
  68. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
  69. &buffer.front(), &length))
  70. return result;
  71. result.reserve(num);
  72. auto start = begin(buffer);
  73. auto end_ = end(buffer);
  74. decltype(start) separator;
  75. while ((separator = find(start, end_, 0)) != end_) {
  76. if (result.size() == num)
  77. break;
  78. char conv[MAX_PATH] = {};
  79. os_wcs_to_utf8(&*start, separator - start, conv, MAX_PATH);
  80. result.emplace_back(conv);
  81. start = separator + 1;
  82. }
  83. return result;
  84. }
  85. vector<string> GetPreferredLocales()
  86. {
  87. vector<string> windows_locales = GetUserPreferredLocales();
  88. auto obs_locales = GetLocaleNames();
  89. auto windows_to_obs = [&obs_locales](string windows) {
  90. string lang_match;
  91. for (auto &locale_pair : obs_locales) {
  92. auto &locale = locale_pair.first;
  93. if (locale == windows.substr(0, locale.size()))
  94. return locale;
  95. if (lang_match.size())
  96. continue;
  97. if (locale.substr(0, 2) == windows.substr(0, 2))
  98. lang_match = locale;
  99. }
  100. return lang_match;
  101. };
  102. vector<string> result;
  103. result.reserve(obs_locales.size());
  104. for (const string &locale : windows_locales) {
  105. string match = windows_to_obs(locale);
  106. if (!match.size())
  107. continue;
  108. if (find(begin(result), end(result), match) != end(result))
  109. continue;
  110. result.emplace_back(match);
  111. }
  112. return result;
  113. }
  114. uint32_t GetWindowsVersion()
  115. {
  116. static uint32_t ver = 0;
  117. if (ver == 0) {
  118. struct win_version_info ver_info;
  119. get_win_ver(&ver_info);
  120. ver = (ver_info.major << 8) | ver_info.minor;
  121. }
  122. return ver;
  123. }
  124. void SetAeroEnabled(bool enable)
  125. {
  126. static HRESULT(WINAPI * func)(UINT) = nullptr;
  127. static bool failed = false;
  128. if (!func) {
  129. if (failed) {
  130. return;
  131. }
  132. HMODULE dwm = LoadLibraryW(L"dwmapi");
  133. if (!dwm) {
  134. failed = true;
  135. return;
  136. }
  137. func = reinterpret_cast<decltype(func)>(
  138. GetProcAddress(dwm, "DwmEnableComposition"));
  139. if (!func) {
  140. failed = true;
  141. return;
  142. }
  143. }
  144. func(enable ? DWM_EC_ENABLECOMPOSITION : DWM_EC_DISABLECOMPOSITION);
  145. }
  146. bool IsAlwaysOnTop(QWidget *window)
  147. {
  148. DWORD exStyle = GetWindowLong((HWND)window->winId(), GWL_EXSTYLE);
  149. return (exStyle & WS_EX_TOPMOST) != 0;
  150. }
  151. void SetAlwaysOnTop(QWidget *window, bool enable)
  152. {
  153. HWND hwnd = (HWND)window->winId();
  154. SetWindowPos(hwnd, enable ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0,
  155. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  156. }
  157. void SetProcessPriority(const char *priority)
  158. {
  159. if (!priority)
  160. return;
  161. if (strcmp(priority, "High") == 0)
  162. SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
  163. else if (strcmp(priority, "AboveNormal") == 0)
  164. SetPriorityClass(GetCurrentProcess(),
  165. 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(),
  170. BELOW_NORMAL_PRIORITY_CLASS);
  171. else if (strcmp(priority, "Idle") == 0)
  172. SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  173. }
  174. void SetWin32DropStyle(QWidget *window)
  175. {
  176. HWND hwnd = (HWND)window->winId();
  177. LONG_PTR ex_style = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  178. ex_style |= WS_EX_ACCEPTFILES;
  179. SetWindowLongPtr(hwnd, GWL_EXSTYLE, ex_style);
  180. }
  181. bool DisableAudioDucking(bool disable)
  182. {
  183. ComPtr<IMMDeviceEnumerator> devEmum;
  184. ComPtr<IMMDevice> device;
  185. ComPtr<IAudioSessionManager2> sessionManager2;
  186. ComPtr<IAudioSessionControl> sessionControl;
  187. ComPtr<IAudioSessionControl2> sessionControl2;
  188. HRESULT result = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr,
  189. CLSCTX_INPROC_SERVER,
  190. __uuidof(IMMDeviceEnumerator),
  191. (void **)&devEmum);
  192. if (FAILED(result))
  193. return false;
  194. result = devEmum->GetDefaultAudioEndpoint(eRender, eConsole, &device);
  195. if (FAILED(result))
  196. return false;
  197. result = device->Activate(__uuidof(IAudioSessionManager2),
  198. CLSCTX_INPROC_SERVER, nullptr,
  199. (void **)&sessionManager2);
  200. if (FAILED(result))
  201. return false;
  202. result = sessionManager2->GetAudioSessionControl(nullptr, 0,
  203. &sessionControl);
  204. if (FAILED(result))
  205. return false;
  206. result = sessionControl->QueryInterface(&sessionControl2);
  207. if (FAILED(result))
  208. return false;
  209. result = sessionControl2->SetDuckingPreference(disable);
  210. return SUCCEEDED(result);
  211. }
  212. struct RunOnceMutexData {
  213. WinHandle handle;
  214. inline RunOnceMutexData(HANDLE h) : handle(h) {}
  215. };
  216. RunOnceMutex::RunOnceMutex(RunOnceMutex &&rom)
  217. {
  218. delete data;
  219. data = rom.data;
  220. rom.data = nullptr;
  221. }
  222. RunOnceMutex::~RunOnceMutex()
  223. {
  224. delete data;
  225. }
  226. RunOnceMutex &RunOnceMutex::operator=(RunOnceMutex &&rom)
  227. {
  228. delete data;
  229. data = rom.data;
  230. rom.data = nullptr;
  231. return *this;
  232. }
  233. RunOnceMutex GetRunOnceMutex(bool &already_running)
  234. {
  235. string name;
  236. if (!portable_mode) {
  237. name = "OBSStudioCore";
  238. } else {
  239. char path[500];
  240. *path = 0;
  241. GetConfigPath(path, sizeof(path), "");
  242. name = "OBSStudioPortable";
  243. name += path;
  244. }
  245. BPtr<wchar_t> wname;
  246. os_utf8_to_wcs_ptr(name.c_str(), name.size(), &wname);
  247. if (wname) {
  248. wchar_t *temp = wname;
  249. while (*temp) {
  250. if (!iswalnum(*temp))
  251. *temp = L'_';
  252. temp++;
  253. }
  254. }
  255. HANDLE h = OpenMutexW(SYNCHRONIZE, false, wname.Get());
  256. already_running = !!h;
  257. if (!already_running)
  258. h = CreateMutexW(nullptr, false, wname.Get());
  259. RunOnceMutex rom(h ? new RunOnceMutexData(h) : nullptr);
  260. return rom;
  261. }