platform-windows.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. static inline bool check_path(const char* data, const char *path,
  26. string &output)
  27. {
  28. ostringstream str;
  29. str << path << data;
  30. output = str.str();
  31. printf("Attempted path: %s\n", output.c_str());
  32. return os_file_exists(output.c_str());
  33. }
  34. bool GetDataFilePath(const char *data, string &output)
  35. {
  36. if (check_path(data, "data/obs-studio/", output))
  37. return true;
  38. return check_path(data, OBS_DATA_PATH "/obs-studio/", output);
  39. }
  40. static BOOL CALLBACK OBSMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor,
  41. LPRECT rect, LPARAM param)
  42. {
  43. vector<MonitorInfo> &monitors = *(vector<MonitorInfo> *)param;
  44. monitors.emplace_back(
  45. rect->left,
  46. rect->top,
  47. rect->right - rect->left,
  48. rect->bottom - rect->top);
  49. UNUSED_PARAMETER(hMonitor);
  50. UNUSED_PARAMETER(hdcMonitor);
  51. return true;
  52. }
  53. void GetMonitors(vector<MonitorInfo> &monitors)
  54. {
  55. monitors.clear();
  56. EnumDisplayMonitors(NULL, NULL, OBSMonitorEnumProc, (LPARAM)&monitors);
  57. }
  58. bool InitApplicationBundle()
  59. {
  60. return true;
  61. }
  62. string GetDefaultVideoSavePath()
  63. {
  64. wchar_t path_utf16[MAX_PATH];
  65. char path_utf8[MAX_PATH] = {};
  66. SHGetFolderPathW(NULL, CSIDL_MYVIDEO, NULL, SHGFP_TYPE_CURRENT,
  67. path_utf16);
  68. os_wcs_to_utf8(path_utf16, wcslen(path_utf16), path_utf8, MAX_PATH);
  69. return string(path_utf8);
  70. }
  71. static vector<string> GetUserPreferredLocales()
  72. {
  73. vector<string> result;
  74. ULONG num, length = 0;
  75. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
  76. nullptr, &length))
  77. return result;
  78. vector<wchar_t> buffer(length);
  79. if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
  80. &buffer.front(), &length))
  81. return result;
  82. result.reserve(num);
  83. auto start = begin(buffer);
  84. auto end_ = end(buffer);
  85. decltype(start) separator;
  86. while ((separator = find(start, end_, 0)) != end_) {
  87. if (result.size() == num)
  88. break;
  89. char conv[MAX_PATH] = {};
  90. os_wcs_to_utf8(&*start, separator - start, conv, MAX_PATH);
  91. result.emplace_back(conv);
  92. start = separator + 1;
  93. }
  94. return result;
  95. }
  96. vector<string> GetPreferredLocales()
  97. {
  98. vector<string> windows_locales = GetUserPreferredLocales();
  99. auto obs_locales = GetLocaleNames();
  100. auto windows_to_obs = [&obs_locales](string windows) {
  101. string lang_match;
  102. for (auto &locale_pair : obs_locales) {
  103. auto &locale = locale_pair.first;
  104. if (locale == windows.substr(0, locale.size()))
  105. return locale;
  106. if (lang_match.size())
  107. continue;
  108. if (locale.substr(0, 2) == windows.substr(0, 2))
  109. lang_match = locale;
  110. }
  111. return lang_match;
  112. };
  113. vector<string> result;
  114. result.reserve(obs_locales.size());
  115. for (const string &locale : windows_locales) {
  116. string match = windows_to_obs(locale);
  117. if (!match.size())
  118. continue;
  119. if (find(begin(result), end(result), match) != end(result))
  120. continue;
  121. result.emplace_back(match);
  122. }
  123. return result;
  124. }