settings_common.h 7.8 KB

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