settings.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <array>
  7. #include <atomic>
  8. #include <chrono>
  9. #include <map>
  10. #include <optional>
  11. #include <string>
  12. #include <utility>
  13. #include <vector>
  14. #include "common/common_types.h"
  15. #include "common/settings_input.h"
  16. #include "input_common/udp/client.h"
  17. namespace Settings {
  18. enum class RendererBackend : u32 {
  19. OpenGL = 0,
  20. Vulkan = 1,
  21. };
  22. enum class ShaderBackend : u32 {
  23. GLSL = 0,
  24. GLASM = 1,
  25. SPIRV = 2,
  26. };
  27. enum class GPUAccuracy : u32 {
  28. Normal = 0,
  29. High = 1,
  30. Extreme = 2,
  31. };
  32. enum class CPUAccuracy : u32 {
  33. Auto = 0,
  34. Accurate = 1,
  35. Unsafe = 2,
  36. };
  37. enum class FullscreenMode : u32 {
  38. Borderless = 0,
  39. Exclusive = 1,
  40. };
  41. /** The BasicSetting class is a simple resource manager. It defines a label and default value
  42. * alongside the actual value of the setting for simpler and less-error prone use with frontend
  43. * configurations. Setting a default value and label is required, though subclasses may deviate from
  44. * this requirement.
  45. */
  46. template <typename Type>
  47. class BasicSetting {
  48. protected:
  49. BasicSetting() = default;
  50. /**
  51. * Only sets the setting to the given initializer, leaving the other members to their default
  52. * initializers.
  53. *
  54. * @param global_val Initial value of the setting
  55. */
  56. explicit BasicSetting(const Type& global_val) : global{global_val} {}
  57. public:
  58. /**
  59. * Sets a default value, label, and setting value.
  60. *
  61. * @param default_val Intial value of the setting, and default value of the setting
  62. * @param name Label for the setting
  63. */
  64. explicit BasicSetting(const Type& default_val, const std::string& name)
  65. : default_value{default_val}, global{default_val}, label{name} {}
  66. virtual ~BasicSetting() = default;
  67. /**
  68. * Returns a reference to the setting's value.
  69. *
  70. * @returns A reference to the setting
  71. */
  72. [[nodiscard]] virtual const Type& GetValue() const {
  73. return global;
  74. }
  75. /**
  76. * Sets the setting to the given value.
  77. *
  78. * @param value The desired value
  79. */
  80. virtual void SetValue(const Type& value) {
  81. Type temp{value};
  82. std::swap(global, temp);
  83. }
  84. /**
  85. * Returns the value that this setting was created with.
  86. *
  87. * @returns A reference to the default value
  88. */
  89. [[nodiscard]] const Type& GetDefault() const {
  90. return default_value;
  91. }
  92. /**
  93. * Returns the label this setting was created with.
  94. *
  95. * @returns A reference to the label
  96. */
  97. [[nodiscard]] const std::string& GetLabel() const {
  98. return label;
  99. }
  100. /**
  101. * Assigns a value to the setting.
  102. *
  103. * @param value The desired setting value
  104. *
  105. * @returns A reference to the setting
  106. */
  107. virtual const Type& operator=(const Type& value) {
  108. Type temp{value};
  109. std::swap(global, temp);
  110. return global;
  111. }
  112. /**
  113. * Returns a reference to the setting.
  114. *
  115. * @returns A reference to the setting
  116. */
  117. explicit virtual operator const Type&() const {
  118. return global;
  119. }
  120. protected:
  121. const Type default_value{}; ///< The default value
  122. Type global{}; ///< The setting
  123. const std::string label{}; ///< The setting's label
  124. };
  125. /**
  126. * BasicRangedSetting class is intended for use with quantifiable settings that need a more
  127. * restrictive range than implicitly defined by its type. Implements a minimum and maximum that is
  128. * simply used to sanitize SetValue and the assignment overload.
  129. */
  130. template <typename Type>
  131. class BasicRangedSetting : virtual public BasicSetting<Type> {
  132. public:
  133. /**
  134. * Sets a default value, minimum value, maximum value, and label.
  135. *
  136. * @param default_val Intial value of the setting, and default value of the setting
  137. * @param min_val Sets the minimum allowed value of the setting
  138. * @param max_val Sets the maximum allowed value of the setting
  139. * @param name Label for the setting
  140. */
  141. explicit BasicRangedSetting(const Type& default_val, const Type& min_val, const Type& max_val,
  142. const std::string& name)
  143. : BasicSetting<Type>{default_val, name}, minimum{min_val}, maximum{max_val} {}
  144. virtual ~BasicRangedSetting() = default;
  145. /**
  146. * Like BasicSetting's SetValue, except value is clamped to the range of the setting.
  147. *
  148. * @param value The desired value
  149. */
  150. void SetValue(const Type& value) override {
  151. this->global = std::clamp(value, minimum, maximum);
  152. }
  153. /**
  154. * Like BasicSetting's assignment overload, except value is clamped to the range of the setting.
  155. *
  156. * @param value The desired value
  157. * @returns A reference to the setting's value
  158. */
  159. const Type& operator=(const Type& value) override {
  160. this->global = std::clamp(value, minimum, maximum);
  161. return this->global;
  162. }
  163. const Type minimum; ///< Minimum allowed value of the setting
  164. const Type maximum; ///< Maximum allowed value of the setting
  165. };
  166. /**
  167. * The Setting class is a slightly more complex version of the BasicSetting class. This adds a
  168. * custom setting to switch to when a guest application specifically requires it. The effect is that
  169. * other components of the emulator can access the setting's intended value without any need for the
  170. * component to ask whether the custom or global setting is needed at the moment.
  171. *
  172. * By default, the global setting is used.
  173. *
  174. * Like the BasicSetting, this requires setting a default value and label to use.
  175. */
  176. template <typename Type>
  177. class Setting : virtual public BasicSetting<Type> {
  178. public:
  179. /**
  180. * Sets a default value, label, and setting value.
  181. *
  182. * @param default_val Intial value of the setting, and default value of the setting
  183. * @param name Label for the setting
  184. */
  185. explicit Setting(const Type& default_val, const std::string& name)
  186. : BasicSetting<Type>(default_val, name) {}
  187. virtual ~Setting() = default;
  188. /**
  189. * Tells this setting to represent either the global or custom setting when other member
  190. * functions are used.
  191. *
  192. * @param to_global Whether to use the global or custom setting.
  193. */
  194. void SetGlobal(bool to_global) {
  195. use_global = to_global;
  196. }
  197. /**
  198. * Returns whether this setting is using the global setting or not.
  199. *
  200. * @returns The global state
  201. */
  202. [[nodiscard]] bool UsingGlobal() const {
  203. return use_global;
  204. }
  205. /**
  206. * Returns either the global or custom setting depending on the values of this setting's global
  207. * state or if the global value was specifically requested.
  208. *
  209. * @param need_global Request global value regardless of setting's state; defaults to false
  210. *
  211. * @returns The required value of the setting
  212. */
  213. [[nodiscard]] virtual const Type& GetValue() const override {
  214. if (use_global) {
  215. return this->global;
  216. }
  217. return custom;
  218. }
  219. [[nodiscard]] virtual const Type& GetValue(bool need_global) const {
  220. if (use_global || need_global) {
  221. return this->global;
  222. }
  223. return custom;
  224. }
  225. /**
  226. * Sets the current setting value depending on the global state.
  227. *
  228. * @param value The new value
  229. */
  230. void SetValue(const Type& value) override {
  231. Type temp{value};
  232. if (use_global) {
  233. std::swap(this->global, temp);
  234. } else {
  235. std::swap(custom, temp);
  236. }
  237. }
  238. /**
  239. * Assigns the current setting value depending on the global state.
  240. *
  241. * @param value The new value
  242. *
  243. * @returns A reference to the current setting value
  244. */
  245. const Type& operator=(const Type& value) override {
  246. Type temp{value};
  247. if (use_global) {
  248. std::swap(this->global, temp);
  249. return this->global;
  250. }
  251. std::swap(custom, temp);
  252. return custom;
  253. }
  254. /**
  255. * Returns the current setting value depending on the global state.
  256. *
  257. * @returns A reference to the current setting value
  258. */
  259. virtual explicit operator const Type&() const override {
  260. if (use_global) {
  261. return this->global;
  262. }
  263. return custom;
  264. }
  265. protected:
  266. bool use_global{true}; ///< The setting's global state
  267. Type custom{}; ///< The custom value of the setting
  268. };
  269. /**
  270. * RangedSetting is a Setting that implements a maximum and minimum value for its setting. Intended
  271. * for use with quantifiable settings.
  272. */
  273. template <typename Type>
  274. class RangedSetting final : public BasicRangedSetting<Type>, public Setting<Type> {
  275. public:
  276. /**
  277. * Sets a default value, minimum value, maximum value, and label.
  278. *
  279. * @param default_val Intial value of the setting, and default value of the setting
  280. * @param min_val Sets the minimum allowed value of the setting
  281. * @param max_val Sets the maximum allowed value of the setting
  282. * @param name Label for the setting
  283. */
  284. explicit RangedSetting(const Type& default_val, const Type& min_val, const Type& max_val,
  285. const std::string& name)
  286. : BasicSetting<Type>{default_val, name},
  287. BasicRangedSetting<Type>{default_val, min_val, max_val, name}, Setting<Type>{default_val,
  288. name} {}
  289. virtual ~RangedSetting() = default;
  290. // The following are needed to avoid a MSVC bug
  291. // (source: https://stackoverflow.com/questions/469508)
  292. [[nodiscard]] const Type& GetValue() const override {
  293. return Setting<Type>::GetValue();
  294. }
  295. [[nodiscard]] const Type& GetValue(bool need_global) const override {
  296. return Setting<Type>::GetValue(need_global);
  297. }
  298. explicit operator const Type&() const override {
  299. if (this->use_global) {
  300. return this->global;
  301. }
  302. return this->custom;
  303. }
  304. /**
  305. * Like BasicSetting's SetValue, except value is clamped to the range of the setting. Sets the
  306. * appropriate value depending on the global state.
  307. *
  308. * @param value The desired value
  309. */
  310. void SetValue(const Type& value) override {
  311. const Type temp = std::clamp(value, this->minimum, this->maximum);
  312. if (this->use_global) {
  313. this->global = temp;
  314. }
  315. this->custom = temp;
  316. }
  317. /**
  318. * Like BasicSetting's assignment overload, except value is clamped to the range of the setting.
  319. * Uses the appropriate value depending on the global state.
  320. *
  321. * @param value The desired value
  322. * @returns A reference to the setting's value
  323. */
  324. const Type& operator=(const Type& value) override {
  325. const Type temp = std::clamp(value, this->minimum, this->maximum);
  326. if (this->use_global) {
  327. this->global = temp;
  328. return this->global;
  329. }
  330. this->custom = temp;
  331. return this->custom;
  332. }
  333. };
  334. /**
  335. * The InputSetting class allows for getting a reference to either the global or custom members.
  336. * This is required as we cannot easily modify the values of user-defined types within containers
  337. * using the SetValue() member function found in the Setting class. The primary purpose of this
  338. * class is to store an array of 10 PlayerInput structs for both the global and custom setting and
  339. * allows for easily accessing and modifying both settings.
  340. */
  341. template <typename Type>
  342. class InputSetting final {
  343. public:
  344. InputSetting() = default;
  345. explicit InputSetting(Type val) : BasicSetting<Type>(val) {}
  346. ~InputSetting() = default;
  347. void SetGlobal(bool to_global) {
  348. use_global = to_global;
  349. }
  350. [[nodiscard]] bool UsingGlobal() const {
  351. return use_global;
  352. }
  353. [[nodiscard]] Type& GetValue(bool need_global = false) {
  354. if (use_global || need_global) {
  355. return global;
  356. }
  357. return custom;
  358. }
  359. private:
  360. bool use_global{true}; ///< The setting's global state
  361. Type global{}; ///< The setting
  362. Type custom{}; ///< The custom setting value
  363. };
  364. struct TouchFromButtonMap {
  365. std::string name;
  366. std::vector<std::string> buttons;
  367. };
  368. struct Values {
  369. // Audio
  370. BasicSetting<std::string> audio_device_id{"auto", "output_device"};
  371. BasicSetting<std::string> sink_id{"auto", "output_engine"};
  372. BasicSetting<bool> audio_muted{false, "audio_muted"};
  373. Setting<bool> enable_audio_stretching{true, "enable_audio_stretching"};
  374. RangedSetting<u8> volume{100, 0, 100, "volume"};
  375. // Core
  376. Setting<bool> use_multi_core{true, "use_multi_core"};
  377. // Cpu
  378. RangedSetting<CPUAccuracy> cpu_accuracy{CPUAccuracy::Auto, CPUAccuracy::Auto,
  379. CPUAccuracy::Unsafe, "cpu_accuracy"};
  380. // TODO: remove cpu_accuracy_first_time, migration setting added 8 July 2021
  381. BasicSetting<bool> cpu_accuracy_first_time{true, "cpu_accuracy_first_time"};
  382. BasicSetting<bool> cpu_debug_mode{false, "cpu_debug_mode"};
  383. BasicSetting<bool> cpuopt_page_tables{true, "cpuopt_page_tables"};
  384. BasicSetting<bool> cpuopt_block_linking{true, "cpuopt_block_linking"};
  385. BasicSetting<bool> cpuopt_return_stack_buffer{true, "cpuopt_return_stack_buffer"};
  386. BasicSetting<bool> cpuopt_fast_dispatcher{true, "cpuopt_fast_dispatcher"};
  387. BasicSetting<bool> cpuopt_context_elimination{true, "cpuopt_context_elimination"};
  388. BasicSetting<bool> cpuopt_const_prop{true, "cpuopt_const_prop"};
  389. BasicSetting<bool> cpuopt_misc_ir{true, "cpuopt_misc_ir"};
  390. BasicSetting<bool> cpuopt_reduce_misalign_checks{true, "cpuopt_reduce_misalign_checks"};
  391. BasicSetting<bool> cpuopt_fastmem{true, "cpuopt_fastmem"};
  392. Setting<bool> cpuopt_unsafe_unfuse_fma{true, "cpuopt_unsafe_unfuse_fma"};
  393. Setting<bool> cpuopt_unsafe_reduce_fp_error{true, "cpuopt_unsafe_reduce_fp_error"};
  394. Setting<bool> cpuopt_unsafe_ignore_standard_fpcr{true, "cpuopt_unsafe_ignore_standard_fpcr"};
  395. Setting<bool> cpuopt_unsafe_inaccurate_nan{true, "cpuopt_unsafe_inaccurate_nan"};
  396. Setting<bool> cpuopt_unsafe_fastmem_check{true, "cpuopt_unsafe_fastmem_check"};
  397. // Renderer
  398. RangedSetting<RendererBackend> renderer_backend{
  399. RendererBackend::OpenGL, RendererBackend::OpenGL, RendererBackend::Vulkan, "backend"};
  400. BasicSetting<bool> renderer_debug{false, "debug"};
  401. BasicSetting<bool> renderer_shader_feedback{false, "shader_feedback"};
  402. BasicSetting<bool> enable_nsight_aftermath{false, "nsight_aftermath"};
  403. BasicSetting<bool> disable_shader_loop_safety_checks{false,
  404. "disable_shader_loop_safety_checks"};
  405. Setting<int> vulkan_device{0, "vulkan_device"};
  406. Setting<u16> resolution_factor{1, "resolution_factor"};
  407. // *nix platforms may have issues with the borderless windowed fullscreen mode.
  408. // Default to exclusive fullscreen on these platforms for now.
  409. RangedSetting<FullscreenMode> fullscreen_mode{
  410. #ifdef _WIN32
  411. FullscreenMode::Borderless,
  412. #else
  413. FullscreenMode::Exclusive,
  414. #endif
  415. FullscreenMode::Borderless, FullscreenMode::Exclusive, "fullscreen_mode"};
  416. RangedSetting<int> aspect_ratio{0, 0, 3, "aspect_ratio"};
  417. RangedSetting<int> max_anisotropy{0, 0, 4, "max_anisotropy"};
  418. Setting<bool> use_speed_limit{true, "use_speed_limit"};
  419. RangedSetting<u16> speed_limit{100, 0, 9999, "speed_limit"};
  420. Setting<bool> use_disk_shader_cache{true, "use_disk_shader_cache"};
  421. RangedSetting<GPUAccuracy> gpu_accuracy{GPUAccuracy::High, GPUAccuracy::Normal,
  422. GPUAccuracy::Extreme, "gpu_accuracy"};
  423. Setting<bool> use_asynchronous_gpu_emulation{true, "use_asynchronous_gpu_emulation"};
  424. Setting<bool> use_nvdec_emulation{true, "use_nvdec_emulation"};
  425. Setting<bool> accelerate_astc{true, "accelerate_astc"};
  426. Setting<bool> use_vsync{true, "use_vsync"};
  427. BasicRangedSetting<u16> fps_cap{1000, 1, 1000, "fps_cap"};
  428. BasicSetting<bool> disable_fps_limit{false, "disable_fps_limit"};
  429. RangedSetting<ShaderBackend> shader_backend{ShaderBackend::GLASM, ShaderBackend::GLSL,
  430. ShaderBackend::SPIRV, "shader_backend"};
  431. Setting<bool> use_asynchronous_shaders{false, "use_asynchronous_shaders"};
  432. Setting<bool> use_fast_gpu_time{true, "use_fast_gpu_time"};
  433. Setting<u8> bg_red{0, "bg_red"};
  434. Setting<u8> bg_green{0, "bg_green"};
  435. Setting<u8> bg_blue{0, "bg_blue"};
  436. // System
  437. Setting<std::optional<u32>> rng_seed{std::optional<u32>(), "rng_seed"};
  438. // Measured in seconds since epoch
  439. std::optional<std::chrono::seconds> custom_rtc;
  440. // Set on game boot, reset on stop. Seconds difference between current time and `custom_rtc`
  441. std::chrono::seconds custom_rtc_differential;
  442. BasicSetting<s32> current_user{0, "current_user"};
  443. RangedSetting<s32> language_index{1, 0, 17, "language_index"};
  444. RangedSetting<s32> region_index{1, 0, 6, "region_index"};
  445. RangedSetting<s32> time_zone_index{0, 0, 45, "time_zone_index"};
  446. RangedSetting<s32> sound_index{1, 0, 2, "sound_index"};
  447. // Controls
  448. InputSetting<std::array<PlayerInput, 10>> players;
  449. Setting<bool> use_docked_mode{true, "use_docked_mode"};
  450. Setting<bool> vibration_enabled{true, "vibration_enabled"};
  451. Setting<bool> enable_accurate_vibrations{false, "enable_accurate_vibrations"};
  452. Setting<bool> motion_enabled{true, "motion_enabled"};
  453. BasicSetting<std::string> motion_device{"engine:motion_emu,update_period:100,sensitivity:0.01",
  454. "motion_device"};
  455. BasicSetting<std::string> udp_input_servers{InputCommon::CemuhookUDP::DEFAULT_SRV,
  456. "udp_input_servers"};
  457. BasicSetting<bool> mouse_panning{false, "mouse_panning"};
  458. BasicRangedSetting<u8> mouse_panning_sensitivity{10, 1, 100, "mouse_panning_sensitivity"};
  459. BasicSetting<bool> mouse_enabled{false, "mouse_enabled"};
  460. std::string mouse_device;
  461. MouseButtonsRaw mouse_buttons;
  462. BasicSetting<bool> emulate_analog_keyboard{false, "emulate_analog_keyboard"};
  463. BasicSetting<bool> keyboard_enabled{false, "keyboard_enabled"};
  464. KeyboardKeysRaw keyboard_keys;
  465. KeyboardModsRaw keyboard_mods;
  466. BasicSetting<bool> debug_pad_enabled{false, "debug_pad_enabled"};
  467. ButtonsRaw debug_pad_buttons;
  468. AnalogsRaw debug_pad_analogs;
  469. TouchscreenInput touchscreen;
  470. BasicSetting<bool> use_touch_from_button{false, "use_touch_from_button"};
  471. BasicSetting<std::string> touch_device{"min_x:100,min_y:50,max_x:1800,max_y:850",
  472. "touch_device"};
  473. BasicSetting<int> touch_from_button_map_index{0, "touch_from_button_map"};
  474. std::vector<TouchFromButtonMap> touch_from_button_maps;
  475. std::atomic_bool is_device_reload_pending{true};
  476. // Data Storage
  477. BasicSetting<bool> use_virtual_sd{true, "use_virtual_sd"};
  478. BasicSetting<bool> gamecard_inserted{false, "gamecard_inserted"};
  479. BasicSetting<bool> gamecard_current_game{false, "gamecard_current_game"};
  480. BasicSetting<std::string> gamecard_path{std::string(), "gamecard_path"};
  481. // Debugging
  482. bool record_frame_times;
  483. BasicSetting<bool> use_gdbstub{false, "use_gdbstub"};
  484. BasicSetting<u16> gdbstub_port{0, "gdbstub_port"};
  485. BasicSetting<std::string> program_args{std::string(), "program_args"};
  486. BasicSetting<bool> dump_exefs{false, "dump_exefs"};
  487. BasicSetting<bool> dump_nso{false, "dump_nso"};
  488. BasicSetting<bool> enable_fs_access_log{false, "enable_fs_access_log"};
  489. BasicSetting<bool> reporting_services{false, "reporting_services"};
  490. BasicSetting<bool> quest_flag{false, "quest_flag"};
  491. BasicSetting<bool> disable_macro_jit{false, "disable_macro_jit"};
  492. BasicSetting<bool> extended_logging{false, "extended_logging"};
  493. BasicSetting<bool> use_debug_asserts{false, "use_debug_asserts"};
  494. BasicSetting<bool> use_auto_stub{false, "use_auto_stub"};
  495. // Miscellaneous
  496. BasicSetting<std::string> log_filter{"*:Info", "log_filter"};
  497. BasicSetting<bool> use_dev_keys{false, "use_dev_keys"};
  498. // Network
  499. BasicSetting<std::string> bcat_backend{"none", "bcat_backend"};
  500. BasicSetting<bool> bcat_boxcat_local{false, "bcat_boxcat_local"};
  501. BasicSetting<std::string> network_interface{std::string(), "network_interface"};
  502. // WebService
  503. BasicSetting<bool> enable_telemetry{true, "enable_telemetry"};
  504. BasicSetting<std::string> web_api_url{"https://api.yuzu-emu.org", "web_api_url"};
  505. BasicSetting<std::string> yuzu_username{std::string(), "yuzu_username"};
  506. BasicSetting<std::string> yuzu_token{std::string(), "yuzu_token"};
  507. // Add-Ons
  508. std::map<u64, std::vector<std::string>> disabled_addons;
  509. };
  510. extern Values values;
  511. bool IsConfiguringGlobal();
  512. void SetConfiguringGlobal(bool is_global);
  513. bool IsGPULevelExtreme();
  514. bool IsGPULevelHigh();
  515. bool IsFastmemEnabled();
  516. float Volume();
  517. std::string GetTimeZoneString();
  518. void LogSettings();
  519. // Restore the global state of all applicable settings in the Values struct
  520. void RestoreGlobalState(bool is_powered_on);
  521. } // namespace Settings