platform-windows.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. static inline bool check_path(const char* data, const char *path,
  28. string &output)
  29. {
  30. ostringstream str;
  31. str << path << data;
  32. output = str.str();
  33. printf("Attempted path: %s\n", output.c_str());
  34. return os_file_exists(output.c_str());
  35. }
  36. bool GetDataFilePath(const char *data, string &output)
  37. {
  38. if (check_path(data, "data/obs-studio/", output))
  39. return true;
  40. return check_path(data, OBS_DATA_PATH "/obs-studio/", output);
  41. }
  42. static BOOL CALLBACK OBSMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor,
  43. LPRECT rect, LPARAM param)
  44. {
  45. vector<MonitorInfo> &monitors = *(vector<MonitorInfo> *)param;
  46. monitors.emplace_back(
  47. rect->left,
  48. rect->top,
  49. rect->right - rect->left,
  50. rect->bottom - rect->top);
  51. UNUSED_PARAMETER(hMonitor);
  52. UNUSED_PARAMETER(hdcMonitor);
  53. return true;
  54. }
  55. void GetMonitors(vector<MonitorInfo> &monitors)
  56. {
  57. monitors.clear();
  58. EnumDisplayMonitors(NULL, NULL, OBSMonitorEnumProc, (LPARAM)&monitors);
  59. }
  60. bool InitApplicationBundle()
  61. {
  62. return true;
  63. }
  64. string GetDefaultVideoSavePath()
  65. {
  66. wchar_t path_utf16[MAX_PATH];
  67. char path_utf8[MAX_PATH] = {};
  68. SHGetFolderPathW(NULL, CSIDL_MYVIDEO, NULL, SHGFP_TYPE_CURRENT,
  69. path_utf16);
  70. os_wcs_to_utf8(path_utf16, wcslen(path_utf16), path_utf8, MAX_PATH);
  71. return string(path_utf8);
  72. }
  73. static vector<string> GetUserPreferredLocales()
  74. {
  75. vector<string> result;
  76. ULONG num, length = 0;
  77. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
  78. nullptr, &length))
  79. return result;
  80. vector<wchar_t> buffer(length);
  81. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
  82. &buffer.front(), &length))
  83. return result;
  84. result.reserve(num);
  85. auto start = begin(buffer);
  86. auto end_ = end(buffer);
  87. decltype(start) separator;
  88. while ((separator = find(start, end_, 0)) != end_) {
  89. if (result.size() == num)
  90. break;
  91. char conv[MAX_PATH] = {};
  92. os_wcs_to_utf8(&*start, separator - start, conv, MAX_PATH);
  93. result.emplace_back(conv);
  94. start = separator + 1;
  95. }
  96. return result;
  97. }
  98. vector<string> GetPreferredLocales()
  99. {
  100. vector<string> windows_locales = GetUserPreferredLocales();
  101. auto obs_locales = GetLocaleNames();
  102. auto windows_to_obs = [&obs_locales](string windows) {
  103. string lang_match;
  104. for (auto &locale_pair : obs_locales) {
  105. auto &locale = locale_pair.first;
  106. if (locale == windows.substr(0, locale.size()))
  107. return locale;
  108. if (lang_match.size())
  109. continue;
  110. if (locale.substr(0, 2) == windows.substr(0, 2))
  111. lang_match = locale;
  112. }
  113. return lang_match;
  114. };
  115. vector<string> result;
  116. result.reserve(obs_locales.size());
  117. for (const string &locale : windows_locales) {
  118. string match = windows_to_obs(locale);
  119. if (!match.size())
  120. continue;
  121. if (find(begin(result), end(result), match) != end(result))
  122. continue;
  123. result.emplace_back(match);
  124. }
  125. return result;
  126. }
  127. uint32_t GetWindowsVersion()
  128. {
  129. static uint32_t ver = 0;
  130. if (ver == 0) {
  131. struct win_version_info ver_info;
  132. get_win_ver(&ver_info);
  133. ver = (ver_info.major << 8) | ver_info.minor;
  134. }
  135. return ver;
  136. }
  137. void SetAeroEnabled(bool enable)
  138. {
  139. static HRESULT (WINAPI *func)(UINT) = nullptr;
  140. static bool failed = false;
  141. if (!func) {
  142. if (failed) {
  143. return;
  144. }
  145. HMODULE dwm = LoadLibraryW(L"dwmapi");
  146. if (!dwm) {
  147. failed = true;
  148. return;
  149. }
  150. func = reinterpret_cast<decltype(func)>(GetProcAddress(dwm,
  151. "DwmEnableComposition"));
  152. if (!func) {
  153. failed = true;
  154. return;
  155. }
  156. }
  157. func(enable ? DWM_EC_ENABLECOMPOSITION : DWM_EC_DISABLECOMPOSITION);
  158. }
  159. bool IsAlwaysOnTop(QWidget *window)
  160. {
  161. DWORD exStyle = GetWindowLong((HWND)window->winId(), GWL_EXSTYLE);
  162. return (exStyle & WS_EX_TOPMOST) != 0;
  163. }
  164. void SetAlwaysOnTop(QWidget *window, bool enable)
  165. {
  166. HWND hwnd = (HWND)window->winId();
  167. SetWindowPos(hwnd, enable ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0,
  168. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  169. }
  170. void SetProcessPriority(const char *priority)
  171. {
  172. if (!priority)
  173. return;
  174. if (strcmp(priority, "High") == 0)
  175. SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
  176. else if (strcmp(priority, "AboveNormal") == 0)
  177. SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
  178. else if (strcmp(priority, "Normal") == 0)
  179. SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
  180. else if (strcmp(priority, "Idle") == 0)
  181. SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  182. }
  183. void SetWin32DropStyle(QWidget *window)
  184. {
  185. HWND hwnd = (HWND)window->winId();
  186. LONG_PTR ex_style = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  187. ex_style |= WS_EX_ACCEPTFILES;
  188. SetWindowLongPtr(hwnd, GWL_EXSTYLE, ex_style);
  189. }