platform-windows.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. bool InitApplicationBundle()
  43. {
  44. return true;
  45. }
  46. string GetDefaultVideoSavePath()
  47. {
  48. wchar_t path_utf16[MAX_PATH];
  49. char path_utf8[MAX_PATH] = {};
  50. SHGetFolderPathW(NULL, CSIDL_MYVIDEO, NULL, SHGFP_TYPE_CURRENT,
  51. path_utf16);
  52. os_wcs_to_utf8(path_utf16, wcslen(path_utf16), path_utf8, MAX_PATH);
  53. return string(path_utf8);
  54. }
  55. static vector<string> GetUserPreferredLocales()
  56. {
  57. vector<string> result;
  58. ULONG num, length = 0;
  59. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
  60. nullptr, &length))
  61. return result;
  62. vector<wchar_t> buffer(length);
  63. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
  64. &buffer.front(), &length))
  65. return result;
  66. result.reserve(num);
  67. auto start = begin(buffer);
  68. auto end_ = end(buffer);
  69. decltype(start) separator;
  70. while ((separator = find(start, end_, 0)) != end_) {
  71. if (result.size() == num)
  72. break;
  73. char conv[MAX_PATH] = {};
  74. os_wcs_to_utf8(&*start, separator - start, conv, MAX_PATH);
  75. result.emplace_back(conv);
  76. start = separator + 1;
  77. }
  78. return result;
  79. }
  80. vector<string> GetPreferredLocales()
  81. {
  82. vector<string> windows_locales = GetUserPreferredLocales();
  83. auto obs_locales = GetLocaleNames();
  84. auto windows_to_obs = [&obs_locales](string windows) {
  85. string lang_match;
  86. for (auto &locale_pair : obs_locales) {
  87. auto &locale = locale_pair.first;
  88. if (locale == windows.substr(0, locale.size()))
  89. return locale;
  90. if (lang_match.size())
  91. continue;
  92. if (locale.substr(0, 2) == windows.substr(0, 2))
  93. lang_match = locale;
  94. }
  95. return lang_match;
  96. };
  97. vector<string> result;
  98. result.reserve(obs_locales.size());
  99. for (const string &locale : windows_locales) {
  100. string match = windows_to_obs(locale);
  101. if (!match.size())
  102. continue;
  103. if (find(begin(result), end(result), match) != end(result))
  104. continue;
  105. result.emplace_back(match);
  106. }
  107. return result;
  108. }
  109. uint32_t GetWindowsVersion()
  110. {
  111. static uint32_t ver = 0;
  112. if (ver == 0) {
  113. struct win_version_info ver_info;
  114. get_win_ver(&ver_info);
  115. ver = (ver_info.major << 8) | ver_info.minor;
  116. }
  117. return ver;
  118. }
  119. void SetAeroEnabled(bool enable)
  120. {
  121. static HRESULT (WINAPI *func)(UINT) = nullptr;
  122. static bool failed = false;
  123. if (!func) {
  124. if (failed) {
  125. return;
  126. }
  127. HMODULE dwm = LoadLibraryW(L"dwmapi");
  128. if (!dwm) {
  129. failed = true;
  130. return;
  131. }
  132. func = reinterpret_cast<decltype(func)>(GetProcAddress(dwm,
  133. "DwmEnableComposition"));
  134. if (!func) {
  135. failed = true;
  136. return;
  137. }
  138. }
  139. func(enable ? DWM_EC_ENABLECOMPOSITION : DWM_EC_DISABLECOMPOSITION);
  140. }
  141. bool IsAlwaysOnTop(QWidget *window)
  142. {
  143. DWORD exStyle = GetWindowLong((HWND)window->winId(), GWL_EXSTYLE);
  144. return (exStyle & WS_EX_TOPMOST) != 0;
  145. }
  146. void SetAlwaysOnTop(QWidget *window, bool enable)
  147. {
  148. HWND hwnd = (HWND)window->winId();
  149. SetWindowPos(hwnd, enable ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0,
  150. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  151. }
  152. void SetProcessPriority(const char *priority)
  153. {
  154. if (!priority)
  155. return;
  156. if (strcmp(priority, "High") == 0)
  157. SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
  158. else if (strcmp(priority, "AboveNormal") == 0)
  159. SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
  160. else if (strcmp(priority, "Normal") == 0)
  161. SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
  162. else if (strcmp(priority, "Idle") == 0)
  163. SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  164. }
  165. void SetWin32DropStyle(QWidget *window)
  166. {
  167. HWND hwnd = (HWND)window->winId();
  168. LONG_PTR ex_style = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  169. ex_style |= WS_EX_ACCEPTFILES;
  170. SetWindowLongPtr(hwnd, GWL_EXSTYLE, ex_style);
  171. }