105 lines
2.5 KiB
C
105 lines
2.5 KiB
C
#include "settings.h"
|
|
|
|
static void print_config_error(GError *error) {
|
|
if (error == NULL) return;
|
|
|
|
if (error->domain != G_KEY_FILE_ERROR_GROUP_NOT_FOUND && error->domain != G_KEY_FILE_ERROR_KEY_NOT_FOUND) {
|
|
g_printerr("Config error: %s\n", error->message);
|
|
}
|
|
}
|
|
|
|
void print_config(config_t *config) {
|
|
#define EXPAND_GROUP(group_name, members) \
|
|
g_print("[%s]\n", #group_name); \
|
|
members
|
|
|
|
#define EXPAND_GROUP_MEMBER(member, key_type) \
|
|
g_print("%s="FORMAT_FOR_KEY_TYPE(key_type)"\n", #member, config->member);
|
|
|
|
EXPAND_CONFIG
|
|
|
|
#undef EXPAND_GROUP
|
|
#undef EXPAND_GROUP_MEMBER
|
|
}
|
|
|
|
void load_config_from_key_file(config_t *config, GKeyFile *key_file) {
|
|
static GError *error = NULL;
|
|
gchar *group_name;
|
|
|
|
#define EXPAND_GROUP(name, members) \
|
|
group_name = #name; \
|
|
if (g_key_file_has_group(key_file, group_name)) { \
|
|
members \
|
|
}
|
|
|
|
#define EXPAND_GROUP_MEMBER(member, key_type) \
|
|
config->member = g_key_file_get_##key_type(key_file, group_name, #member, &error); \
|
|
print_config_error(error); \
|
|
if (error != NULL) g_clear_error(&error);
|
|
|
|
EXPAND_CONFIG
|
|
|
|
if (config->rewind_duration > 600) {
|
|
g_warning("Setting Emulation.rewind_duration too high might affect performance.\n");
|
|
}
|
|
|
|
#undef EXPAND_GROUP
|
|
#undef EXPAND_GROUP_MEMBER
|
|
}
|
|
|
|
void init_settings(gchar *path) {
|
|
free_settings();
|
|
key_file = g_key_file_new();
|
|
|
|
if (path != NULL) {
|
|
settings_file_path = path;
|
|
}
|
|
else {
|
|
settings_file_path = g_build_filename(g_get_user_config_dir(), SETTINGS_FILE, NULL);
|
|
}
|
|
|
|
load_settings();
|
|
}
|
|
|
|
int load_settings(void) {
|
|
GError *error = NULL;
|
|
|
|
g_print("Trying to load settings from %s\n", settings_file_path);
|
|
|
|
if (!g_key_file_load_from_file(key_file, settings_file_path, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error)) {
|
|
if (error->domain == G_FILE_ERROR) {
|
|
g_warning("Unable to load %s: %s", settings_file_path, error->message);
|
|
}
|
|
else if (error->domain == G_KEY_FILE_ERROR) {
|
|
g_warning("Failed to parse %s: %s", settings_file_path, error->message);
|
|
}
|
|
|
|
g_error_free(error);
|
|
return -1;
|
|
}
|
|
|
|
load_config_from_key_file(&config, key_file);
|
|
print_config(&config);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void save_settings(void) {
|
|
GError *error = NULL;
|
|
|
|
g_print("Trying to save settings to %s\n", settings_file_path);
|
|
|
|
if (!g_key_file_save_to_file(key_file, settings_file_path, &error)) {
|
|
g_warning ("Failed to save %s: %s", settings_file_path, error->message);
|
|
g_error_free(error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
void free_settings(void) {
|
|
if (key_file != NULL) {
|
|
g_key_file_free(key_file);
|
|
key_file = NULL;
|
|
}
|
|
}
|