SDL_Libretro
SDL3-power libretro frontend.
Loading...
Searching...
No Matches
SDL_libretro_config.h
Go to the documentation of this file.
1
7#if defined(SDL_LIBRETRO_IMPLEMENTATION) && !defined(SDL_LIBRETRO_CONFIG_IMPL_ONCE)
8#define SDL_LIBRETRO_CONFIG_IMPL_ONCE
9
10static void SDL_Libretro_SanitizeSectionName(char* dst, size_t dstSize, const char* name) {
11 SDL_strlcpy(dst, name, dstSize);
12 for (char* p = dst; *p; ++p) {
13 if (*p == '[' || *p == ']' || *p == '\n' || *p == '\r') {
14 *p = '_';
15 }
16 }
17}
18
26bool SDL_Libretro_InitConfigFile(SDL_Libretro* lr, const char* file) {
27 if (!lr || !file) return false;
28
29 // Clear our any existing config.
30 SDL_free(lr->iniFile);
31 lr->iniFile = NULL;
32 INI_Destroy(lr->ini);
33 lr->ini = NULL;
34
35 // Load the file.
36 SDL_ini* ini = INI_Load(file);
37 if (!ini) {
38 ini = INI_Create();
39 if (!ini) return false;
40 }
41
42 // Capture the ini file path.
43 lr->iniFile = SDL_strdup(file);
44 if (!lr->iniFile) {
45 INI_Destroy(ini);
46 return false;
47 }
48 lr->ini = ini;
49
50 // Get all the configs.
51 if (INI_HasValue(ini, NULL, "volume"))
52 SDL_Libretro_SetVolume(lr, INI_GetFloat(ini, NULL, "volume", SDL_Libretro_GetVolume(lr)));
53 if (INI_HasValue(ini, NULL, "username"))
54 SDL_Libretro_SetUsername(lr, INI_GetString(ini, NULL, "username", SDL_Libretro_GetUsername(lr)));
55 if (INI_HasValue(ini, NULL, "audiolatency"))
56 SDL_Libretro_SetAudioLatency(lr, (unsigned)INI_GetInt(ini, NULL, "audiolatency", SDL_Libretro_GetAudioLatency(lr)));
57 if (INI_HasValue(ini, NULL, "fitmode"))
58 SDL_Libretro_SetFitMode(lr, INI_GetInt(ini, NULL, "fitmode", SDL_Libretro_GetFitMode(lr)));
59 if (INI_HasValue(ini, NULL, "savedirectory"))
60 SDL_Libretro_SetSaveDirectory(lr, INI_GetString(ini, NULL, "savedirectory", SDL_Libretro_GetSaveDirectory(lr)));
61 if (INI_HasValue(ini, NULL, "systemdirectory"))
62 SDL_Libretro_SetSystemDirectory(lr, INI_GetString(ini, NULL, "systemdirectory", SDL_Libretro_GetSystemDirectory(lr)));
63 if (INI_HasValue(ini, NULL, "coredirectory"))
64 SDL_Libretro_SetCoreDirectory(lr, INI_GetString(ini, NULL, "coredirectory", SDL_Libretro_GetCoreDirectory(lr)));
65 if (INI_HasValue(ini, NULL, "coreassetsdirectory"))
66 SDL_Libretro_SetCoreAssetsDirectory(lr, INI_GetString(ini, NULL, "coreassetsdirectory", SDL_Libretro_GetCoreAssetsDirectory(lr)));
67
68 return true;
69}
70
80bool SDL_Libretro_InitConfig(SDL_Libretro* lr, const char* org, const char* app) {
81 if (!lr) return false;
82 if (!org || org[0] == '\0') org = "SDL_libretro";
83 if (!app || app[0] == '\0') app = "SDL_libretro";
84 char* prefPath = SDL_GetPrefPath(org, app);
85 if (!prefPath) return false;
86 char path[SDL_LIBRETRO_MAX_PATH];
87 SDL_snprintf(path, sizeof(path), "%s%s.cfg", prefPath, app);
88 SDL_free(prefPath);
89 return SDL_Libretro_InitConfigFile(lr, path);
90}
91
95static void SDL_Libretro_SetCoreConfigOption(void *userdata, const SDL_ini *ini, const char* section, const char *key, const char *value) {
96 (void)ini;
97 (void)section;
98 SDL_Libretro_SetOptionValue((SDL_Libretro*)userdata, key, value);
99}
100
104static bool SDL_Libretro_LoadCoreConfig(SDL_Libretro* lr) {
105 if (!lr || !lr->ini || !SDL_Libretro_IsCoreReady(lr)) return false;
106
107 // Set the library name as the section name.
108 char section[256];
109 SDL_Libretro_SanitizeSectionName(section, sizeof(section), lr->core.libraryName);
110
111 // Ensure there's a valid section name to enumerate.
112 if (section[0] == '\0') {
113 return false;
114 }
115 INI_EnumerateKeys(lr->ini, section, &SDL_Libretro_SetCoreConfigOption, (void*)lr);
116 return true;
117}
118
122static bool SDL_Libretro_SaveCoreConfig(SDL_Libretro* lr) {
123 if (!lr || !lr->ini || !SDL_Libretro_IsCoreReady(lr)) return false;
124 char section[256];
125 SDL_Libretro_SanitizeSectionName(section, sizeof(section), lr->core.libraryName);
126 if (section[0] == '\0') {
127 return false;
128 }
129 int count = SDL_Libretro_GetOptionCount(lr);
130 for (int i = 0; i < count; i++) {
132 if (!option) continue;
133 const char* val = option->value ? option->value : option->defaultValue;
134 // Only set the value in the config if they're different from the defaults.
135 if (option->defaultValue && SDL_strcmp(val, option->defaultValue) == 0) {
136 INI_RemoveKey(lr->ini, section, option->key);
137 continue;
138 }
139 INI_SetString(lr->ini, section, option->key, val);
140 }
141 return true;
142}
143
147static bool SDL_Libretro_SaveConfig(SDL_Libretro* lr) {
148 if (!lr || !lr->ini || !lr->iniFile) return false;
149
150 // Save the core options if needed.
151 if (lr->core.loaded) {
152 SDL_Libretro_SaveCoreConfig(lr);
153 }
154
155 INI_SetFloat(lr->ini, NULL, "volume", SDL_Libretro_GetVolume(lr));
156 INI_SetString(lr->ini, NULL, "username", SDL_Libretro_GetUsername(lr));
157 INI_SetInt(lr->ini, NULL, "audiolatency", (Sint64)SDL_Libretro_GetAudioLatency(lr));
158 INI_SetInt(lr->ini, NULL, "fitmode", (Sint64)SDL_Libretro_GetFitMode(lr));
159 INI_SetString(lr->ini, NULL, "savedirectory", SDL_Libretro_GetSaveDirectory(lr));
160 INI_SetString(lr->ini, NULL, "systemdirectory", SDL_Libretro_GetSystemDirectory(lr));
161 INI_SetString(lr->ini, NULL, "coredirectory", SDL_Libretro_GetCoreDirectory(lr));
162 INI_SetString(lr->ini, NULL, "coreassetsdirectory", SDL_Libretro_GetCoreAssetsDirectory(lr));
163
164 return INI_Save(lr->ini, lr->iniFile);
165}
166
170static bool SDL_Libretro_CloseConfig(SDL_Libretro* lr) {
171 if (!lr) return false;
172 bool ok = SDL_Libretro_SaveConfig(lr);
173 SDL_free(lr->iniFile);
174 lr->iniFile = NULL;
175 INI_Destroy(lr->ini);
176 lr->ini = NULL;
177 return ok;
178}
179
180#endif
bool SDL_Libretro_SetCoreDirectory(SDL_Libretro *lr, const char *path)
Sets the associated libretro core directory, where the default set of cores will be loaded from.
bool SDL_Libretro_InitConfig(SDL_Libretro *lr, const char *org, const char *app)
Sets up the config file to be the default path, accoring to the organization and app name.
bool SDL_Libretro_InitConfigFile(SDL_Libretro *lr, const char *file)
Initializes the config system based on the given file.
const SDL_LibretroOption * SDL_Libretro_GetOptionByIndex(const SDL_Libretro *lr, unsigned index)
Retrieve a core option by its registration index, or NULL if out of range.
void SDL_Libretro_SetVolume(SDL_Libretro *lr, float volume)
Set the audio volume when playing sounds.
void SDL_Libretro_SetAudioLatency(SDL_Libretro *lr, unsigned latencyMs)
Set the desired minimum audio latency for the audio stream.
bool SDL_Libretro_SetOptionValue(SDL_Libretro *lr, const char *key, const char *value)
Set a core option's value; fails if the value isn't one the core declared.
unsigned SDL_Libretro_GetOptionCount(const SDL_Libretro *lr)
Get the number of core options that have been set.
void SDL_Libretro_SetFitMode(SDL_Libretro *lr, SDL_LibretroFitMode mode)
Sets the desired scale mode for the libretro context when it's displayed.
A core option and its current state.
const char * value
Description / help text; may be empty.
const char * defaultValue
The current value.