platform-windows.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/platform.h>
  21. #define WIN32_LEAN_AND_MEAN
  22. #include <windows.h>
  23. #include <shellapi.h>
  24. #include <shlobj.h>
  25. #include <Dwmapi.h>
  26. static inline bool check_path(const char* data, const char *path,
  27. string &output)
  28. {
  29. ostringstream str;
  30. str << path << data;
  31. output = str.str();
  32. printf("Attempted path: %s\n", output.c_str());
  33. return os_file_exists(output.c_str());
  34. }
  35. bool GetDataFilePath(const char *data, string &output)
  36. {
  37. if (check_path(data, "data/obs-studio/", output))
  38. return true;
  39. return check_path(data, OBS_DATA_PATH "/obs-studio/", output);
  40. }
  41. static BOOL CALLBACK OBSMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor,
  42. LPRECT rect, LPARAM param)
  43. {
  44. vector<MonitorInfo> &monitors = *(vector<MonitorInfo> *)param;
  45. monitors.emplace_back(
  46. rect->left,
  47. rect->top,
  48. rect->right - rect->left,
  49. rect->bottom - rect->top);
  50. UNUSED_PARAMETER(hMonitor);
  51. UNUSED_PARAMETER(hdcMonitor);
  52. return true;
  53. }
  54. void GetMonitors(vector<MonitorInfo> &monitors)
  55. {
  56. monitors.clear();
  57. EnumDisplayMonitors(NULL, NULL, OBSMonitorEnumProc, (LPARAM)&monitors);
  58. }
  59. bool InitApplicationBundle()
  60. {
  61. return true;
  62. }
  63. string GetDefaultVideoSavePath()
  64. {
  65. wchar_t path_utf16[MAX_PATH];
  66. char path_utf8[MAX_PATH] = {};
  67. SHGetFolderPathW(NULL, CSIDL_MYVIDEO, NULL, SHGFP_TYPE_CURRENT,
  68. path_utf16);
  69. os_wcs_to_utf8(path_utf16, wcslen(path_utf16), path_utf8, MAX_PATH);
  70. return string(path_utf8);
  71. }
  72. static vector<string> GetUserPreferredLocales()
  73. {
  74. vector<string> result;
  75. ULONG num, length = 0;
  76. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
  77. nullptr, &length))
  78. return result;
  79. vector<wchar_t> buffer(length);
  80. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
  81. &buffer.front(), &length))
  82. return result;
  83. result.reserve(num);
  84. auto start = begin(buffer);
  85. auto end_ = end(buffer);
  86. decltype(start) separator;
  87. while ((separator = find(start, end_, 0)) != end_) {
  88. if (result.size() == num)
  89. break;
  90. char conv[MAX_PATH] = {};
  91. os_wcs_to_utf8(&*start, separator - start, conv, MAX_PATH);
  92. result.emplace_back(conv);
  93. start = separator + 1;
  94. }
  95. return result;
  96. }
  97. vector<string> GetPreferredLocales()
  98. {
  99. vector<string> windows_locales = GetUserPreferredLocales();
  100. auto obs_locales = GetLocaleNames();
  101. auto windows_to_obs = [&obs_locales](string windows) {
  102. string lang_match;
  103. for (auto &locale_pair : obs_locales) {
  104. auto &locale = locale_pair.first;
  105. if (locale == windows.substr(0, locale.size()))
  106. return locale;
  107. if (lang_match.size())
  108. continue;
  109. if (locale.substr(0, 2) == windows.substr(0, 2))
  110. lang_match = locale;
  111. }
  112. return lang_match;
  113. };
  114. vector<string> result;
  115. result.reserve(obs_locales.size());
  116. for (const string &locale : windows_locales) {
  117. string match = windows_to_obs(locale);
  118. if (!match.size())
  119. continue;
  120. if (find(begin(result), end(result), match) != end(result))
  121. continue;
  122. result.emplace_back(match);
  123. }
  124. return result;
  125. }
  126. uint32_t GetWindowsVersion()
  127. {
  128. static uint32_t ver = 0;
  129. if (ver == 0) {
  130. OSVERSIONINFOW osvi = {};
  131. osvi.dwOSVersionInfoSize = sizeof(osvi);
  132. if (GetVersionExW(&osvi)) {
  133. ver = osvi.dwMajorVersion << 8 | osvi.dwMinorVersion;
  134. }
  135. }
  136. return ver;
  137. }
  138. void SetAeroEnabled(bool enable)
  139. {
  140. static HRESULT (WINAPI *func)(UINT) = nullptr;
  141. static bool failed = false;
  142. if (!func) {
  143. if (failed) {
  144. return;
  145. }
  146. HMODULE dwm = LoadLibraryW(L"dwmapi");
  147. if (!dwm) {
  148. failed = true;
  149. return;
  150. }
  151. func = reinterpret_cast<decltype(func)>(GetProcAddress(dwm,
  152. "DwmEnableComposition"));
  153. if (!func) {
  154. failed = true;
  155. return;
  156. }
  157. }
  158. func(enable ? DWM_EC_ENABLECOMPOSITION : DWM_EC_DISABLECOMPOSITION);
  159. }