settings_common.h 7.8 KB

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