mirror of https://github.com/stenzek/duckstation
				
				
				
			Add Host/HostSettings
							parent
							
								
									6a6ab6529a
								
							
						
					
					
						commit
						462eb2c155
					
				@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "common/types.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <optional>
 | 
				
			||||||
 | 
					#include <string>
 | 
				
			||||||
 | 
					#include <string_view>
 | 
				
			||||||
 | 
					#include <vector>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Host {
 | 
				
			||||||
 | 
					/// Reads a file from the resources directory of the application.
 | 
				
			||||||
 | 
					/// This may be outside of the "normal" filesystem on platforms such as Mac.
 | 
				
			||||||
 | 
					std::optional<std::vector<u8>> ReadResourceFile(const char* filename);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Reads a resource file file from the resources directory as a string.
 | 
				
			||||||
 | 
					std::optional<std::string> ReadResourceFileToString(const char* filename);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Adds OSD messages, duration is in seconds.
 | 
				
			||||||
 | 
					void AddOSDMessage(std::string message, float duration = 2.0f);
 | 
				
			||||||
 | 
					void AddKeyedOSDMessage(std::string key, std::string message, float duration = 2.0f);
 | 
				
			||||||
 | 
					void AddFormattedOSDMessage(float duration, const char* format, ...);
 | 
				
			||||||
 | 
					void AddKeyedFormattedOSDMessage(std::string key, float duration, const char* format, ...);
 | 
				
			||||||
 | 
					void RemoveKeyedOSDMessage(std::string key);
 | 
				
			||||||
 | 
					void ClearOSDMessages();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Displays an asynchronous error on the UI thread, i.e. doesn't block the caller.
 | 
				
			||||||
 | 
					void ReportErrorAsync(const std::string_view& title, const std::string_view& message);
 | 
				
			||||||
 | 
					void ReportFormattedErrorAsync(const std::string_view& title, const char* format, ...);
 | 
				
			||||||
 | 
					} // namespace Host
 | 
				
			||||||
@ -0,0 +1,70 @@
 | 
				
			|||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "common/types.h"
 | 
				
			||||||
 | 
					#include <mutex>
 | 
				
			||||||
 | 
					#include <string>
 | 
				
			||||||
 | 
					#include <vector>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class SettingsInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Host {
 | 
				
			||||||
 | 
					// Base setting retrieval, bypasses layers.
 | 
				
			||||||
 | 
					std::string GetBaseStringSettingValue(const char* section, const char* key, const char* default_value = "");
 | 
				
			||||||
 | 
					bool GetBaseBoolSettingValue(const char* section, const char* key, bool default_value = false);
 | 
				
			||||||
 | 
					s32 GetBaseIntSettingValue(const char* section, const char* key, s32 default_value = 0);
 | 
				
			||||||
 | 
					u32 GetBaseUIntSettingValue(const char* section, const char* key, u32 default_value = 0);
 | 
				
			||||||
 | 
					float GetBaseFloatSettingValue(const char* section, const char* key, float default_value = 0.0f);
 | 
				
			||||||
 | 
					double GetBaseDoubleSettingValue(const char* section, const char* key, double default_value = 0.0);
 | 
				
			||||||
 | 
					std::vector<std::string> GetBaseStringListSetting(const char* section, const char* key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Allows the emucore to write settings back to the frontend. Use with care.
 | 
				
			||||||
 | 
					// You should call CommitBaseSettingChanges() after finishing writing, or it may not be written to disk.
 | 
				
			||||||
 | 
					void SetBaseBoolSettingValue(const char* section, const char* key, bool value);
 | 
				
			||||||
 | 
					void SetBaseIntSettingValue(const char* section, const char* key, s32 value);
 | 
				
			||||||
 | 
					void SetBaseUIntSettingValue(const char* section, const char* key, u32 value);
 | 
				
			||||||
 | 
					void SetBaseFloatSettingValue(const char* section, const char* key, float value);
 | 
				
			||||||
 | 
					void SetBaseStringSettingValue(const char* section, const char* key, const char* value);
 | 
				
			||||||
 | 
					void SetBaseStringListSettingValue(const char* section, const char* key, const std::vector<std::string>& values);
 | 
				
			||||||
 | 
					void DeleteBaseSettingValue(const char* section, const char* key);
 | 
				
			||||||
 | 
					void CommitBaseSettingChanges();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Settings access, thread-safe.
 | 
				
			||||||
 | 
					std::string GetStringSettingValue(const char* section, const char* key, const char* default_value = "");
 | 
				
			||||||
 | 
					bool GetBoolSettingValue(const char* section, const char* key, bool default_value = false);
 | 
				
			||||||
 | 
					int GetIntSettingValue(const char* section, const char* key, s32 default_value = 0);
 | 
				
			||||||
 | 
					u32 GetUIntSettingValue(const char* section, const char* key, u32 default_value = 0);
 | 
				
			||||||
 | 
					float GetFloatSettingValue(const char* section, const char* key, float default_value = 0.0f);
 | 
				
			||||||
 | 
					double GetDoubleSettingValue(const char* section, const char* key, double default_value = 0.0);
 | 
				
			||||||
 | 
					std::vector<std::string> GetStringListSetting(const char* section, const char* key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Direct access to settings interface. Must hold the lock when calling GetSettingsInterface() and while using it.
 | 
				
			||||||
 | 
					std::unique_lock<std::mutex> GetSettingsLock();
 | 
				
			||||||
 | 
					SettingsInterface* GetSettingsInterface();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Returns the settings interface that controller bindings should be loaded from.
 | 
				
			||||||
 | 
					/// If an input profile is being used, this will be the input layer, otherwise the layered interface.
 | 
				
			||||||
 | 
					SettingsInterface* GetSettingsInterfaceForBindings();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Internal {
 | 
				
			||||||
 | 
					/// Retrieves the base settings layer. Must call with lock held.
 | 
				
			||||||
 | 
					SettingsInterface* GetBaseSettingsLayer();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Retrieves the game settings layer, if present. Must call with lock held.
 | 
				
			||||||
 | 
					SettingsInterface* GetGameSettingsLayer();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Retrieves the input settings layer, if present. Must call with lock held.
 | 
				
			||||||
 | 
					SettingsInterface* GetInputSettingsLayer();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Sets the base settings layer. Should be called by the host at initialization time.
 | 
				
			||||||
 | 
					void SetBaseSettingsLayer(SettingsInterface* sif);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Sets the game settings layer. Called by VMManager when the game changes.
 | 
				
			||||||
 | 
					void SetGameSettingsLayer(SettingsInterface* sif);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Sets the input profile settings layer. Called by VMManager when the game changes.
 | 
				
			||||||
 | 
					void SetInputSettingsLayer(SettingsInterface* sif);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Updates the variables in the EmuFolders namespace, reloading subsystems if needed. Must call with the lock held.
 | 
				
			||||||
 | 
					void UpdateEmuFolders();
 | 
				
			||||||
 | 
					} // namespace Internal
 | 
				
			||||||
 | 
					} // namespace Host
 | 
				
			||||||
@ -0,0 +1,192 @@
 | 
				
			|||||||
 | 
					#include "core/host.h"
 | 
				
			||||||
 | 
					#include "core/host_settings.h"
 | 
				
			||||||
 | 
					#include "common/assert.h"
 | 
				
			||||||
 | 
					#include "common/layered_settings_interface.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static std::mutex s_settings_mutex;
 | 
				
			||||||
 | 
					static LayeredSettingsInterface s_layered_settings_interface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::unique_lock<std::mutex> Host::GetSettingsLock()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return std::unique_lock<std::mutex>(s_settings_mutex);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					SettingsInterface* Host::GetSettingsInterface()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return &s_layered_settings_interface;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					SettingsInterface* Host::GetSettingsInterfaceForBindings()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						SettingsInterface* input_layer = s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_INPUT);
 | 
				
			||||||
 | 
						return input_layer ? input_layer : &s_layered_settings_interface;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::string Host::GetBaseStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->GetStringValue(section, key, default_value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool Host::GetBaseBoolSettingValue(const char* section, const char* key, bool default_value /*= false*/)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->GetBoolValue(section, key, default_value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					s32 Host::GetBaseIntSettingValue(const char* section, const char* key, s32 default_value /*= 0*/)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->GetIntValue(section, key, default_value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					u32 Host::GetBaseUIntSettingValue(const char* section, const char* key, u32 default_value /*= 0*/)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->GetUIntValue(section, key, default_value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					float Host::GetBaseFloatSettingValue(const char* section, const char* key, float default_value /*= 0.0f*/)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->GetFloatValue(section, key, default_value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					double Host::GetBaseDoubleSettingValue(const char* section, const char* key, double default_value /* = 0.0f */)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->GetDoubleValue(section, key, default_value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::vector<std::string> Host::GetBaseStringListSetting(const char* section, const char* key)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->GetStringList(section, key);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Host::SetBaseBoolSettingValue(const char* section, const char* key, bool value)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->SetBoolValue(section, key, value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Host::SetBaseIntSettingValue(const char* section, const char* key, s32 value)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->SetIntValue(section, key, value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Host::SetBaseUIntSettingValue(const char* section, const char* key, u32 value)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->SetUIntValue(section, key, value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Host::SetBaseFloatSettingValue(const char* section, const char* key, float value)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->SetFloatValue(section, key, value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Host::SetBaseStringSettingValue(const char* section, const char* key, const char* value)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->SetStringValue(section, key, value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Host::SetBaseStringListSettingValue(const char* section, const char* key, const std::vector<std::string>& values)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->SetStringList(section, key, values);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Host::DeleteBaseSettingValue(const char* section, const char* key)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->DeleteValue(section, key);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Host::CommitBaseSettingChanges()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE)->Save();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::string Host::GetStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetStringValue(section, key, default_value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool Host::GetBoolSettingValue(const char* section, const char* key, bool default_value /*= false*/)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetBoolValue(section, key, default_value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					s32 Host::GetIntSettingValue(const char* section, const char* key, s32 default_value /*= 0*/)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetIntValue(section, key, default_value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					u32 Host::GetUIntSettingValue(const char* section, const char* key, u32 default_value /*= 0*/)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetUIntValue(section, key, default_value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					float Host::GetFloatSettingValue(const char* section, const char* key, float default_value /*= 0.0f*/)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetFloatValue(section, key, default_value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					double Host::GetDoubleSettingValue(const char* section, const char* key, double default_value /*= 0.0f*/)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetDoubleValue(section, key, default_value);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::vector<std::string> Host::GetStringListSetting(const char* section, const char* key)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetStringList(section, key);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					SettingsInterface* Host::Internal::GetBaseSettingsLayer()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					SettingsInterface* Host::Internal::GetGameSettingsLayer()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_GAME);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					SettingsInterface* Host::Internal::GetInputSettingsLayer()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_INPUT);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Host::Internal::SetBaseSettingsLayer(SettingsInterface* sif)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						AssertMsg(s_layered_settings_interface.GetLayer(LayeredSettingsInterface::LAYER_BASE) == nullptr, "Base layer has not been set");
 | 
				
			||||||
 | 
						s_layered_settings_interface.SetLayer(LayeredSettingsInterface::LAYER_BASE, sif);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Host::Internal::SetGameSettingsLayer(SettingsInterface* sif)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						s_layered_settings_interface.SetLayer(LayeredSettingsInterface::LAYER_GAME, sif);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Host::Internal::SetInputSettingsLayer(SettingsInterface* sif)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::unique_lock lock(s_settings_mutex);
 | 
				
			||||||
 | 
						s_layered_settings_interface.SetLayer(LayeredSettingsInterface::LAYER_INPUT, sif);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Host::Internal::UpdateEmuFolders()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
					Loading…
					
					
				
		Reference in New Issue