settings.h 15 KB

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