settings_common.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <functional>
  5. #include <map>
  6. #include <string>
  7. #include <typeindex>
  8. #include "common/common_types.h"
  9. namespace Settings {
  10. enum class Category : u32 {
  11. Android,
  12. Audio,
  13. Core,
  14. Cpu,
  15. CpuDebug,
  16. CpuUnsafe,
  17. Overlay,
  18. Renderer,
  19. RendererAdvanced,
  20. RendererDebug,
  21. System,
  22. SystemAudio,
  23. DataStorage,
  24. Debugging,
  25. DebuggingGraphics,
  26. GpuDriver,
  27. Miscellaneous,
  28. Network,
  29. WebService,
  30. AddOns,
  31. Controls,
  32. Ui,
  33. UiAudio,
  34. UiGeneral,
  35. UiLayout,
  36. UiGameList,
  37. Screenshots,
  38. Shortcuts,
  39. Multiplayer,
  40. Services,
  41. Paths,
  42. Linux,
  43. LibraryApplet,
  44. MaxEnum,
  45. };
  46. constexpr u8 SpecializationTypeMask = 0xf;
  47. constexpr u8 SpecializationAttributeMask = 0xf0;
  48. constexpr u8 SpecializationAttributeOffset = 4;
  49. // Scalar and countable could have better names
  50. enum Specialization : u8 {
  51. Default = 0,
  52. Time = 1, // Duration or specific moment in time
  53. Hex = 2, // Hexadecimal number
  54. List = 3, // Setting has specific members
  55. RuntimeList = 4, // Members of the list are determined during runtime
  56. Scalar = 5, // Values are continuous
  57. Countable = 6, // Can be stepped through
  58. Paired = 7, // Another setting is associated with this setting
  59. Radio = 8, // Setting should be presented in a radio group
  60. Percentage = (1 << SpecializationAttributeOffset), // Should be represented as a percentage
  61. };
  62. class BasicSetting;
  63. class Linkage {
  64. public:
  65. explicit Linkage(u32 initial_count = 0);
  66. ~Linkage();
  67. std::map<Category, std::vector<BasicSetting*>> by_category{};
  68. std::map<std::string, Settings::BasicSetting*> by_key{};
  69. std::vector<std::function<void()>> restore_functions{};
  70. u32 count;
  71. };
  72. /**
  73. * BasicSetting is an abstract class that only keeps track of metadata. The string methods are
  74. * available to get data values out.
  75. */
  76. class BasicSetting {
  77. protected:
  78. explicit BasicSetting(Linkage& linkage, const std::string& name, Category category_, bool save_,
  79. bool runtime_modifiable_, u32 specialization,
  80. BasicSetting* other_setting);
  81. public:
  82. virtual ~BasicSetting();
  83. /*
  84. * Data retrieval
  85. */
  86. /**
  87. * Returns a string representation of the internal data. If the Setting is Switchable, it
  88. * respects the internal global state: it is based on GetValue().
  89. *
  90. * @returns A string representation of the internal data.
  91. */
  92. [[nodiscard]] virtual std::string ToString() const = 0;
  93. /**
  94. * Returns a string representation of the global version of internal data. If the Setting is
  95. * not Switchable, it behaves like ToString.
  96. *
  97. * @returns A string representation of the global version of internal data.
  98. */
  99. [[nodiscard]] virtual std::string ToStringGlobal() const;
  100. /**
  101. * @returns A string representation of the Setting's default value.
  102. */
  103. [[nodiscard]] virtual std::string DefaultToString() const = 0;
  104. /**
  105. * Returns a string representation of the minimum value of the setting. If the Setting is not
  106. * ranged, the string represents the default initialization of the data type.
  107. *
  108. * @returns A string representation of the minimum value of the setting.
  109. */
  110. [[nodiscard]] virtual std::string MinVal() const = 0;
  111. /**
  112. * Returns a string representation of the maximum value of the setting. If the Setting is not
  113. * ranged, the string represents the default initialization of the data type.
  114. *
  115. * @returns A string representation of the maximum value of the setting.
  116. */
  117. [[nodiscard]] virtual std::string MaxVal() const = 0;
  118. /**
  119. * Takes a string input, converts it to the internal data type if necessary, and then runs
  120. * SetValue with it.
  121. *
  122. * @param load String of the input data.
  123. */
  124. virtual void LoadString(const std::string& load) = 0;
  125. /**
  126. * Returns a string representation of the data. If the data is an enum, it returns a string of
  127. * the enum value. If the internal data type is not an enum, this is equivalent to ToString.
  128. *
  129. * e.g. renderer_backend.Canonicalize() == "OpenGL"
  130. *
  131. * @returns Canonicalized string representation of the internal data
  132. */
  133. [[nodiscard]] virtual std::string Canonicalize() const = 0;
  134. /*
  135. * Metadata
  136. */
  137. /**
  138. * @returns A unique identifier for the Setting's internal data type.
  139. */
  140. [[nodiscard]] virtual std::type_index TypeId() const = 0;
  141. /**
  142. * Returns true if the Setting's internal data type is an enum.
  143. *
  144. * @returns True if the Setting's internal data type is an enum
  145. */
  146. [[nodiscard]] virtual constexpr bool IsEnum() const = 0;
  147. /**
  148. * Returns true if the current setting is Switchable.
  149. *
  150. * @returns If the setting is a SwitchableSetting
  151. */
  152. [[nodiscard]] virtual constexpr bool Switchable() const {
  153. return false;
  154. }
  155. /**
  156. * Returns true to suggest that a frontend can read or write the setting to a configuration
  157. * file.
  158. *
  159. * @returns The save preference
  160. */
  161. [[nodiscard]] bool Save() const;
  162. /**
  163. * @returns true if the current setting can be changed while the guest is running.
  164. */
  165. [[nodiscard]] bool RuntimeModifiable() const;
  166. /**
  167. * @returns A unique number corresponding to the setting.
  168. */
  169. [[nodiscard]] constexpr u32 Id() const {
  170. return id;
  171. }
  172. /**
  173. * Returns the setting's category AKA INI group.
  174. *
  175. * @returns The setting's category
  176. */
  177. [[nodiscard]] Category GetCategory() const;
  178. /**
  179. * @returns Extra metadata for data representation in frontend implementations.
  180. */
  181. [[nodiscard]] u32 Specialization() const;
  182. /**
  183. * @returns Another BasicSetting if one is paired, or nullptr otherwise.
  184. */
  185. [[nodiscard]] BasicSetting* PairedSetting() const;
  186. /**
  187. * Returns the label this setting was created with.
  188. *
  189. * @returns A reference to the label
  190. */
  191. [[nodiscard]] const std::string& GetLabel() const;
  192. /**
  193. * @returns If the Setting checks input values for valid ranges.
  194. */
  195. [[nodiscard]] virtual constexpr bool Ranged() const = 0;
  196. /**
  197. * @returns The index of the enum if the underlying setting type is an enum, else max of u32.
  198. */
  199. [[nodiscard]] virtual constexpr u32 EnumIndex() const = 0;
  200. /**
  201. * @returns True if the underlying type is a floating point storage
  202. */
  203. [[nodiscard]] virtual constexpr bool IsFloatingPoint() const = 0;
  204. /**
  205. * @returns True if the underlying type is an integer storage
  206. */
  207. [[nodiscard]] virtual constexpr bool IsIntegral() const = 0;
  208. /*
  209. * Switchable settings
  210. */
  211. /**
  212. * Sets a setting's global state. True means use the normal setting, false to use a custom
  213. * value. Has no effect if the Setting is not Switchable.
  214. *
  215. * @param global The desired state
  216. */
  217. virtual void SetGlobal(bool global);
  218. /**
  219. * Returns true if the setting is using the normal setting value. Always true if the setting is
  220. * not Switchable.
  221. *
  222. * @returns The Setting's global state
  223. */
  224. [[nodiscard]] virtual bool UsingGlobal() const;
  225. private:
  226. const std::string label; ///< The setting's label
  227. const Category category; ///< The setting's category AKA INI group
  228. const u32 id; ///< Unique integer for the setting
  229. const bool save; ///< Suggests if the setting should be saved and read to a frontend config
  230. const bool
  231. runtime_modifiable; ///< Suggests if the setting can be modified while a guest is running
  232. const u32 specialization; ///< Extra data to identify representation of a setting
  233. BasicSetting* const other_setting; ///< A paired setting
  234. };
  235. } // namespace Settings