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