platform-windows.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/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,
  65. nullptr, &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)>(GetProcAddress(dwm,
  138. "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(), ABOVE_NORMAL_PRIORITY_CLASS);
  165. else if (strcmp(priority, "Normal") == 0)
  166. SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
  167. else if (strcmp(priority, "Idle") == 0)
  168. SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  169. }
  170. void SetWin32DropStyle(QWidget *window)
  171. {
  172. HWND hwnd = (HWND)window->winId();
  173. LONG_PTR ex_style = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  174. ex_style |= WS_EX_ACCEPTFILES;
  175. SetWindowLongPtr(hwnd, GWL_EXSTYLE, ex_style);
  176. }
  177. bool DisableAudioDucking(bool disable)
  178. {
  179. ComPtr<IMMDeviceEnumerator> devEmum;
  180. ComPtr<IMMDevice> device;
  181. ComPtr<IAudioSessionManager2> sessionManager2;
  182. ComPtr<IAudioSessionControl> sessionControl;
  183. ComPtr<IAudioSessionControl2> sessionControl2;
  184. HRESULT result = CoCreateInstance(__uuidof(MMDeviceEnumerator),
  185. nullptr, CLSCTX_INPROC_SERVER,
  186. __uuidof(IMMDeviceEnumerator),
  187. (void **)&devEmum);
  188. if (FAILED(result))
  189. return false;
  190. result = devEmum->GetDefaultAudioEndpoint(eRender, eConsole, &device);
  191. if (FAILED(result))
  192. return false;
  193. result = device->Activate(__uuidof(IAudioSessionManager2),
  194. CLSCTX_INPROC_SERVER, nullptr,
  195. (void **)&sessionManager2);
  196. if (FAILED(result))
  197. return false;
  198. result = sessionManager2->GetAudioSessionControl(nullptr, 0,
  199. &sessionControl);
  200. if (FAILED(result))
  201. return false;
  202. result = sessionControl->QueryInterface(&sessionControl2);
  203. if (FAILED(result))
  204. return false;
  205. result = sessionControl2->SetDuckingPreference(disable);
  206. return SUCCEEDED(result);
  207. }
  208. uint64_t CurrentMemoryUsage()
  209. {
  210. PROCESS_MEMORY_COUNTERS pmc = {};
  211. pmc.cb = sizeof(pmc);
  212. if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
  213. return 0;
  214. return (uint64_t)pmc.WorkingSetSize;
  215. }