settings.h 16 KB

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