settings_common.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <forward_list>
  5. #include <functional>
  6. #include <map>
  7. #include <string>
  8. #include <typeindex>
  9. #include "common/common_types.h"
  10. namespace Settings {
  11. enum class Category : u32 {
  12. Audio,
  13. Core,
  14. Cpu,
  15. CpuDebug,
  16. CpuUnsafe,
  17. Renderer,
  18. RendererAdvanced,
  19. RendererDebug,
  20. System,
  21. SystemAudio,
  22. DataStorage,
  23. Debugging,
  24. DebuggingGraphics,
  25. Miscellaneous,
  26. Network,
  27. WebService,
  28. AddOns,
  29. Controls,
  30. Ui,
  31. UiGeneral,
  32. UiLayout,
  33. UiGameList,
  34. Screenshots,
  35. Shortcuts,
  36. Multiplayer,
  37. Services,
  38. Paths,
  39. MaxEnum,
  40. };
  41. bool IsConfiguringGlobal();
  42. void SetConfiguringGlobal(bool is_global);
  43. class BasicSetting;
  44. class Linkage {
  45. public:
  46. explicit Linkage(u32 initial_count = 0);
  47. ~Linkage();
  48. std::map<Category, std::forward_list<BasicSetting*>> by_category{};
  49. std::vector<std::function<void()>> restore_functions{};
  50. u32 count;
  51. };
  52. /**
  53. * BasicSetting is an abstract class that only keeps track of metadata. The string methods are
  54. * available to get data values out.
  55. */
  56. class BasicSetting {
  57. protected:
  58. explicit BasicSetting(Linkage& linkage, const std::string& name, enum Category category_,
  59. bool save_, bool runtime_modifiable_);
  60. public:
  61. virtual ~BasicSetting();
  62. /*
  63. * Data retrieval
  64. */
  65. /**
  66. * Returns a string representation of the internal data. If the Setting is Switchable, it
  67. * respects the internal global state: it is based on GetValue().
  68. *
  69. * @returns A string representation of the internal data.
  70. */
  71. [[nodiscard]] virtual std::string ToString() const = 0;
  72. /**
  73. * Returns a string representation of the global version of internal data. If the Setting is
  74. * not Switchable, it behaves like ToString.
  75. *
  76. * @returns A string representation of the global version of internal data.
  77. */
  78. [[nodiscard]] virtual std::string ToStringGlobal() const;
  79. /**
  80. * @returns A string representation of the Setting's default value.
  81. */
  82. [[nodiscard]] virtual std::string DefaultToString() const = 0;
  83. /**
  84. * Returns a string representation of the minimum value of the setting. If the Setting is not
  85. * ranged, the string represents the default initialization of the data type.
  86. *
  87. * @returns A string representation of the minimum value of the setting.
  88. */
  89. [[nodiscard]] virtual std::string MinVal() const = 0;
  90. /**
  91. * Returns a string representation of the maximum value of the setting. If the Setting is not
  92. * ranged, the string represents the default initialization of the data type.
  93. *
  94. * @returns A string representation of the maximum value of the setting.
  95. */
  96. [[nodiscard]] virtual std::string MaxVal() const = 0;
  97. /**
  98. * Takes a string input, converts it to the internal data type if necessary, and then runs
  99. * SetValue with it.
  100. *
  101. * @param load String of the input data.
  102. */
  103. virtual void LoadString(const std::string& load) = 0;
  104. /**
  105. * Returns a string representation of the data. If the data is an enum, it returns a string of
  106. * the enum value. If the internal data type is not an enum, this is equivalent to ToString.
  107. *
  108. * e.g. renderer_backend.Canonicalize() == "OpenGL"
  109. *
  110. * @returns Canonicalized string representation of the internal data
  111. */
  112. [[nodiscard]] virtual std::string Canonicalize() const = 0;
  113. /*
  114. * Metadata
  115. */
  116. /**
  117. * @returns A unique identifier for the Setting's internal data type.
  118. */
  119. [[nodiscard]] virtual std::type_index TypeId() const = 0;
  120. /**
  121. * Returns true if the Setting's internal data type is an enum.
  122. *
  123. * @returns True if the Setting's internal data type is an enum
  124. */
  125. [[nodiscard]] virtual constexpr bool IsEnum() const = 0;
  126. /**
  127. * Returns true if the current setting is Switchable.
  128. *
  129. * @returns If the setting is a SwitchableSetting
  130. */
  131. [[nodiscard]] virtual constexpr bool Switchable() const {
  132. return false;
  133. }
  134. /**
  135. * Returns true to suggest that a frontend can read or write the setting to a configuration
  136. * file.
  137. *
  138. * @returns The save preference
  139. */
  140. [[nodiscard]] bool Save() const;
  141. /**
  142. * @returns true if the current setting can be changed while the guest is running.
  143. */
  144. [[nodiscard]] bool RuntimeModfiable() const;
  145. /**
  146. * @returns A unique number corresponding to the setting.
  147. */
  148. [[nodiscard]] constexpr u32 Id() const {
  149. return id;
  150. }
  151. /**
  152. * Returns the setting's category AKA INI group.
  153. *
  154. * @returns The setting's category
  155. */
  156. [[nodiscard]] Category Category() const;
  157. /**
  158. * Returns the label this setting was created with.
  159. *
  160. * @returns A reference to the label
  161. */
  162. [[nodiscard]] const std::string& GetLabel() const;
  163. /**
  164. * @returns If the Setting checks input values for valid ranges.
  165. */
  166. [[nodiscard]] virtual constexpr bool Ranged() const = 0;
  167. /*
  168. * Switchable settings
  169. */
  170. /**
  171. * Sets a setting's global state. True means use the normal setting, false to use a custom
  172. * value. Has no effect if the Setting is not Switchable.
  173. *
  174. * @param global The desired state
  175. */
  176. virtual void SetGlobal(bool global);
  177. /**
  178. * Returns true if the setting is using the normal setting value. Always true if the setting is
  179. * not Switchable.
  180. *
  181. * @returns The Setting's global state
  182. */
  183. [[nodiscard]] virtual bool UsingGlobal() const;
  184. private:
  185. const std::string label; ///< The setting's label
  186. const enum Category category; ///< The setting's category AKA INI group
  187. const u32 id; ///< Unique integer for the setting
  188. const bool save; ///< Suggests if the setting should be saved and read to a frontend config
  189. const bool
  190. runtime_modifiable; ///< Suggests if the setting can be modified while a guest is running
  191. };
  192. } // namespace Settings