settings.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <version>
  4. #include "common/settings_enums.h"
  5. #if __cpp_lib_chrono >= 201907L
  6. #include <chrono>
  7. #include <exception>
  8. #include <stdexcept>
  9. #endif
  10. #include <compare>
  11. #include <cstddef>
  12. #include <filesystem>
  13. #include <functional>
  14. #include <string_view>
  15. #include <type_traits>
  16. #include <fmt/core.h>
  17. #include "common/assert.h"
  18. #include "common/fs/fs_util.h"
  19. #include "common/fs/path_util.h"
  20. #include "common/logging/log.h"
  21. #include "common/settings.h"
  22. #include "common/time_zone.h"
  23. namespace Settings {
  24. // Clang 14 and earlier have errors when explicitly instantiating these classes
  25. #ifndef CANNOT_EXPLICITLY_INSTANTIATE
  26. #define SETTING(TYPE, RANGED) template class Setting<TYPE, RANGED>
  27. #define SWITCHABLE(TYPE, RANGED) template class SwitchableSetting<TYPE, RANGED>
  28. SETTING(AppletMode, false);
  29. SETTING(AudioEngine, false);
  30. SETTING(bool, false);
  31. SETTING(int, false);
  32. SETTING(std::string, false);
  33. SETTING(u16, false);
  34. SWITCHABLE(AnisotropyMode, true);
  35. SWITCHABLE(AntiAliasing, false);
  36. SWITCHABLE(AspectRatio, true);
  37. SWITCHABLE(AstcDecodeMode, true);
  38. SWITCHABLE(AstcRecompression, true);
  39. SWITCHABLE(AudioMode, true);
  40. SWITCHABLE(CpuBackend, true);
  41. SWITCHABLE(CpuAccuracy, true);
  42. SWITCHABLE(FullscreenMode, true);
  43. SWITCHABLE(GpuAccuracy, true);
  44. SWITCHABLE(Language, true);
  45. SWITCHABLE(MemoryLayout, true);
  46. SWITCHABLE(NvdecEmulation, false);
  47. SWITCHABLE(Region, true);
  48. SWITCHABLE(RendererBackend, true);
  49. SWITCHABLE(ScalingFilter, false);
  50. SWITCHABLE(ShaderBackend, true);
  51. SWITCHABLE(TimeZone, true);
  52. SETTING(VSyncMode, true);
  53. SWITCHABLE(bool, false);
  54. SWITCHABLE(int, false);
  55. SWITCHABLE(int, true);
  56. SWITCHABLE(s64, false);
  57. SWITCHABLE(u16, true);
  58. SWITCHABLE(u32, false);
  59. SWITCHABLE(u8, false);
  60. SWITCHABLE(u8, true);
  61. // Used in UISettings
  62. // TODO see if we can move this to uisettings.cpp
  63. SWITCHABLE(ConfirmStop, true);
  64. SWITCHABLE(DarkModeState, true);
  65. #undef SETTING
  66. #undef SWITCHABLE
  67. #endif
  68. Values values;
  69. std::string GetTimeZoneString(TimeZone time_zone) {
  70. const auto time_zone_index = static_cast<std::size_t>(time_zone);
  71. ASSERT(time_zone_index < Common::TimeZone::GetTimeZoneStrings().size());
  72. std::string location_name;
  73. if (time_zone_index == 0) { // Auto
  74. #if __cpp_lib_chrono >= 201907L && !defined(MINGW)
  75. // Disabled for MinGW -- tzdb always returns Etc/UTC
  76. try {
  77. const struct std::chrono::tzdb& time_zone_data = std::chrono::get_tzdb();
  78. const std::chrono::time_zone* current_zone = time_zone_data.current_zone();
  79. std::string_view current_zone_name = current_zone->name();
  80. location_name = current_zone_name;
  81. } catch (std::runtime_error& runtime_error) {
  82. // VCRUNTIME will throw a runtime_error if the operating system's selected time zone
  83. // cannot be found
  84. location_name = Common::TimeZone::FindSystemTimeZone();
  85. LOG_WARNING(Common,
  86. "Error occurred when trying to determine system time zone:\n{}\nFalling "
  87. "back to hour offset \"{}\"",
  88. runtime_error.what(), location_name);
  89. }
  90. #else
  91. location_name = Common::TimeZone::FindSystemTimeZone();
  92. #endif
  93. } else {
  94. location_name = Common::TimeZone::GetTimeZoneStrings()[time_zone_index];
  95. }
  96. return location_name;
  97. }
  98. void LogSettings() {
  99. const auto log_setting = [](std::string_view name, const auto& value) {
  100. LOG_INFO(Config, "{}: {}", name, value);
  101. };
  102. const auto log_path = [](std::string_view name, const std::filesystem::path& path) {
  103. LOG_INFO(Config, "{}: {}", name, Common::FS::PathToUTF8String(path));
  104. };
  105. LOG_INFO(Config, "suyu Configuration:");
  106. for (auto& [category, settings] : values.linkage.by_category) {
  107. for (const auto& setting : settings) {
  108. if (setting->Id() == values.suyu_token.Id()) {
  109. // Hide the token secret, for security reasons.
  110. continue;
  111. }
  112. const auto name = fmt::format(
  113. "{:c}{:c} {}.{}", setting->ToString() == setting->DefaultToString() ? '-' : 'M',
  114. setting->UsingGlobal() ? '-' : 'C', TranslateCategory(category),
  115. setting->GetLabel());
  116. log_setting(name, setting->Canonicalize());
  117. }
  118. }
  119. log_path("DataStorage_CacheDir", Common::FS::GetSuyuPath(Common::FS::SuyuPath::CacheDir));
  120. log_path("DataStorage_ConfigDir", Common::FS::GetSuyuPath(Common::FS::SuyuPath::ConfigDir));
  121. log_path("DataStorage_LoadDir", Common::FS::GetSuyuPath(Common::FS::SuyuPath::LoadDir));
  122. log_path("DataStorage_NANDDir", Common::FS::GetSuyuPath(Common::FS::SuyuPath::NANDDir));
  123. log_path("DataStorage_SDMCDir", Common::FS::GetSuyuPath(Common::FS::SuyuPath::SDMCDir));
  124. }
  125. void UpdateGPUAccuracy() {
  126. values.current_gpu_accuracy = values.gpu_accuracy.GetValue();
  127. }
  128. bool IsGPULevelExtreme() {
  129. return values.current_gpu_accuracy == GpuAccuracy::Extreme;
  130. }
  131. bool IsGPULevelHigh() {
  132. return values.current_gpu_accuracy == GpuAccuracy::Extreme ||
  133. values.current_gpu_accuracy == GpuAccuracy::High;
  134. }
  135. bool IsFastmemEnabled() {
  136. if (values.cpu_debug_mode) {
  137. return static_cast<bool>(values.cpuopt_fastmem);
  138. }
  139. return true;
  140. }
  141. static bool is_nce_enabled = false;
  142. void SetNceEnabled(bool is_39bit) {
  143. const bool is_nce_selected = values.cpu_backend.GetValue() == CpuBackend::Nce;
  144. if (is_nce_selected && !IsFastmemEnabled()) {
  145. LOG_WARNING(Common, "Fastmem is required to natively execute code in a performant manner, "
  146. "falling back to Dynarmic");
  147. }
  148. if (is_nce_selected && !is_39bit) {
  149. LOG_WARNING(
  150. Common,
  151. "Program does not utilize 39-bit address space, unable to natively execute code");
  152. }
  153. is_nce_enabled = IsFastmemEnabled() && is_nce_selected && is_39bit;
  154. }
  155. bool IsNceEnabled() {
  156. return is_nce_enabled;
  157. }
  158. bool IsDockedMode() {
  159. return values.use_docked_mode.GetValue() == Settings::ConsoleMode::Docked;
  160. }
  161. float Volume() {
  162. if (values.audio_muted) {
  163. return 0.0f;
  164. }
  165. return values.volume.GetValue() / static_cast<f32>(values.volume.GetDefault());
  166. }
  167. const char* TranslateCategory(Category category) {
  168. switch (category) {
  169. case Category::Android:
  170. return "Android";
  171. case Category::Audio:
  172. return "Audio";
  173. case Category::Core:
  174. return "Core";
  175. case Category::Cpu:
  176. case Category::CpuDebug:
  177. case Category::CpuUnsafe:
  178. return "Cpu";
  179. case Category::Overlay:
  180. return "Overlay";
  181. case Category::Renderer:
  182. case Category::RendererAdvanced:
  183. case Category::RendererDebug:
  184. return "Renderer";
  185. case Category::System:
  186. case Category::SystemAudio:
  187. return "System";
  188. case Category::DataStorage:
  189. return "Data Storage";
  190. case Category::Debugging:
  191. case Category::DebuggingGraphics:
  192. return "Debugging";
  193. case Category::GpuDriver:
  194. return "GpuDriver";
  195. case Category::LibraryApplet:
  196. return "LibraryApplet";
  197. case Category::Miscellaneous:
  198. return "Miscellaneous";
  199. case Category::Network:
  200. return "Network";
  201. case Category::WebService:
  202. return "WebService";
  203. case Category::AddOns:
  204. return "DisabledAddOns";
  205. case Category::Controls:
  206. return "Controls";
  207. case Category::Ui:
  208. case Category::UiGeneral:
  209. return "UI";
  210. case Category::UiAudio:
  211. return "UiAudio";
  212. case Category::UiLayout:
  213. return "UILayout";
  214. case Category::UiGameList:
  215. return "UIGameList";
  216. case Category::Screenshots:
  217. return "Screenshots";
  218. case Category::Shortcuts:
  219. return "Shortcuts";
  220. case Category::Multiplayer:
  221. return "Multiplayer";
  222. case Category::Services:
  223. return "Services";
  224. case Category::Paths:
  225. return "Paths";
  226. case Category::Linux:
  227. return "Linux";
  228. case Category::MaxEnum:
  229. break;
  230. }
  231. return "Miscellaneous";
  232. }
  233. void TranslateResolutionInfo(ResolutionSetup setup, ResolutionScalingInfo& info) {
  234. info.downscale = false;
  235. switch (setup) {
  236. case ResolutionSetup::Res1_2X:
  237. info.up_scale = 1;
  238. info.down_shift = 1;
  239. info.downscale = true;
  240. break;
  241. case ResolutionSetup::Res3_4X:
  242. info.up_scale = 3;
  243. info.down_shift = 2;
  244. info.downscale = true;
  245. break;
  246. case ResolutionSetup::Res1X:
  247. info.up_scale = 1;
  248. info.down_shift = 0;
  249. break;
  250. case ResolutionSetup::Res3_2X:
  251. info.up_scale = 3;
  252. info.down_shift = 1;
  253. break;
  254. case ResolutionSetup::Res2X:
  255. info.up_scale = 2;
  256. info.down_shift = 0;
  257. break;
  258. case ResolutionSetup::Res3X:
  259. info.up_scale = 3;
  260. info.down_shift = 0;
  261. break;
  262. case ResolutionSetup::Res4X:
  263. info.up_scale = 4;
  264. info.down_shift = 0;
  265. break;
  266. case ResolutionSetup::Res5X:
  267. info.up_scale = 5;
  268. info.down_shift = 0;
  269. break;
  270. case ResolutionSetup::Res6X:
  271. info.up_scale = 6;
  272. info.down_shift = 0;
  273. break;
  274. case ResolutionSetup::Res7X:
  275. info.up_scale = 7;
  276. info.down_shift = 0;
  277. break;
  278. case ResolutionSetup::Res8X:
  279. info.up_scale = 8;
  280. info.down_shift = 0;
  281. break;
  282. default:
  283. ASSERT(false);
  284. info.up_scale = 1;
  285. info.down_shift = 0;
  286. break;
  287. }
  288. info.up_factor = static_cast<f32>(info.up_scale) / (1U << info.down_shift);
  289. info.down_factor = static_cast<f32>(1U << info.down_shift) / info.up_scale;
  290. info.active = info.up_scale != 1 || info.down_shift != 0;
  291. }
  292. void UpdateRescalingInfo() {
  293. const auto setup = values.resolution_setup.GetValue();
  294. auto& info = values.resolution_info;
  295. TranslateResolutionInfo(setup, info);
  296. }
  297. void RestoreGlobalState(bool is_powered_on) {
  298. // If a game is running, DO NOT restore the global settings state
  299. if (is_powered_on) {
  300. return;
  301. }
  302. for (const auto& reset : values.linkage.restore_functions) {
  303. reset();
  304. }
  305. }
  306. static bool configuring_global = true;
  307. bool IsConfiguringGlobal() {
  308. return configuring_global;
  309. }
  310. void SetConfiguringGlobal(bool is_global) {
  311. configuring_global = is_global;
  312. }
  313. } // namespace Settings