[GTK3] Save settings to key file
This commit is contained in:
parent
3709a88e6e
commit
d60df680bb
125
gtk3/settings.c
125
gtk3/settings.c
@ -3,7 +3,7 @@
|
||||
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) {
|
||||
if (!g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND) && !g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND)) {
|
||||
g_printerr("Config error: %s\n", error->message);
|
||||
}
|
||||
}
|
||||
@ -23,7 +23,7 @@ void print_config(config_t *config) {
|
||||
}
|
||||
|
||||
void load_config_from_key_file(config_t *config, GKeyFile *key_file) {
|
||||
static GError *error = NULL;
|
||||
GError *error = NULL;
|
||||
gchar *group_name;
|
||||
|
||||
#define EXPAND_GROUP(name, members) \
|
||||
@ -35,7 +35,7 @@ void load_config_from_key_file(config_t *config, GKeyFile *key_file) {
|
||||
#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);
|
||||
g_clear_error(&error);
|
||||
|
||||
EXPAND_CONFIG
|
||||
|
||||
@ -47,7 +47,45 @@ void load_config_from_key_file(config_t *config, GKeyFile *key_file) {
|
||||
#undef EXPAND_GROUP_MEMBER
|
||||
}
|
||||
|
||||
void init_settings(gchar *path) {
|
||||
void save_config_to_key_file(config_t *config, GKeyFile *key_file) {
|
||||
GError *error = NULL;
|
||||
gchar *group_name;
|
||||
|
||||
#define EXPAND_GROUP(name, members) \
|
||||
group_name = #name; \
|
||||
members
|
||||
|
||||
#define EXPAND_GROUP_MEMBER_IF_0(member, key_type) \
|
||||
g_key_file_set_##key_type(key_file, group_name, #member, config->member);
|
||||
|
||||
#define EXPAND_GROUP_MEMBER_IF_1(member, key_type) \
|
||||
if (config->member != NULL) { \
|
||||
g_key_file_set_##key_type(key_file, group_name, #member, config->member); \
|
||||
} \
|
||||
else { \
|
||||
GError *error = NULL; \
|
||||
g_key_file_remove_key(key_file, group_name, #member, &error); \
|
||||
if (error != NULL) { \
|
||||
g_printerr("%s\n", error->message); \
|
||||
g_clear_error(&error); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define EXPAND_GROUP_MEMBER_IF_EVAL(y, member, key_type) EXPAND_GROUP_MEMBER_IF_ ## y(member, key_type)
|
||||
#define EXPAND_GROUP_MEMBER_IF(member, key_type, is_pointer) EXPAND_GROUP_MEMBER_IF_EVAL(is_pointer, member, key_type)
|
||||
#define EXPAND_GROUP_MEMBER(member, key_type) EXPAND_GROUP_MEMBER_IF(member, key_type, GTYPE_IS_POINTER(key_type))
|
||||
|
||||
EXPAND_CONFIG
|
||||
|
||||
#undef EXPAND_GROUP
|
||||
#undef EXPAND_GROUP_MEMBER
|
||||
#undef EXPAND_GROUP_MEMBER_IF
|
||||
#undef EXPAND_GROUP_MEMBER_IF_EVAL
|
||||
#undef EXPAND_GROUP_MEMBER_IF_0
|
||||
#undef EXPAND_GROUP_MEMBER_IF_1
|
||||
}
|
||||
|
||||
void init_settings(gchar *path, GtkWindow *preferences) {
|
||||
free_settings();
|
||||
key_file = g_key_file_new();
|
||||
|
||||
@ -89,6 +127,8 @@ void save_settings(void) {
|
||||
|
||||
g_print("Trying to save settings to %s\n", settings_file_path);
|
||||
|
||||
save_config_to_key_file(&config, key_file);
|
||||
|
||||
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);
|
||||
@ -102,3 +142,80 @@ void free_settings(void) {
|
||||
key_file = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
enum menubar_override get_show_menubar(void) {
|
||||
if (g_strcmp0(config.menubar_override, "show") == 0) {
|
||||
return MENUBAR_SHOW;
|
||||
}
|
||||
else if (g_strcmp0(config.menubar_override, "hide") == 0) {
|
||||
return MENUBAR_HIDE;
|
||||
}
|
||||
|
||||
return MENUBAR_AUTO;
|
||||
}
|
||||
|
||||
void set_show_menubar(enum menubar_override value) {
|
||||
switch (value) {
|
||||
case MENUBAR_AUTO:
|
||||
config.menubar_override = "auto";
|
||||
break;
|
||||
case MENUBAR_SHOW:
|
||||
config.menubar_override = "show";
|
||||
break;
|
||||
case MENUBAR_HIDE:
|
||||
config.menubar_override = "hide";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT void on_highpass_filter_changed(GtkWidget *w, gpointer user_data_gptr) {
|
||||
GtkComboBox *box = GTK_COMBO_BOX(w);
|
||||
g_print("New value: %s\n", gtk_combo_box_get_active_id(box));
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT void on_use_integer_scaling_changed(GtkWidget *w, gpointer user_data_gptr) {
|
||||
GtkCheckButton *button = GTK_CHECK_BUTTON(w);
|
||||
gboolean value = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
|
||||
config.use_integer_scaling = value;
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT void on_keep_aspect_ratio_changed(GtkWidget *w, gpointer user_data_gptr) {
|
||||
GtkCheckButton *button = GTK_CHECK_BUTTON(w);
|
||||
gboolean value = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
|
||||
config.keep_aspect_ratio = value;
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT void on_color_correction_changed(GtkWidget *w, gpointer user_data_gptr) {
|
||||
GtkComboBox *box = GTK_COMBO_BOX(w);
|
||||
g_print("New value: %s\n", gtk_combo_box_get_active_id(box));
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT void on_graphic_filter_changed(GtkWidget *w, gpointer user_data_gptr) {
|
||||
GtkComboBox *box = GTK_COMBO_BOX(w);
|
||||
config.shader = (gchar *)gtk_combo_box_get_active_id(box);
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT void on_cgb_model_changed(GtkWidget *w, gpointer user_data_gptr) {
|
||||
GtkComboBox *box = GTK_COMBO_BOX(w);
|
||||
g_print("New value: %s\n", gtk_combo_box_get_active_id(box));
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT void on_sgb_model_changed(GtkWidget *w, gpointer user_data_gptr) {
|
||||
GtkComboBox *box = GTK_COMBO_BOX(w);
|
||||
g_print("New value: %s\n", gtk_combo_box_get_active_id(box));
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT void on_dmg_model_changed(GtkWidget *w, gpointer user_data_gptr) {
|
||||
GtkComboBox *box = GTK_COMBO_BOX(w);
|
||||
g_print("New value: %s\n", gtk_combo_box_get_active_id(box));
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT void on_rewind_duration_changed(GtkWidget *w, gpointer user_data_gptr) {
|
||||
GtkComboBox *box = GTK_COMBO_BOX(w);
|
||||
g_print("New value: %s\n", gtk_combo_box_get_active_id(box));
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT void on_boot_rom_location_changed(GtkWidget *w, gpointer user_data_gptr) {
|
||||
GtkComboBox *box = GTK_COMBO_BOX(w);
|
||||
g_print("New value: %s\n", gtk_combo_box_get_active_id(box));
|
||||
}
|
||||
|
@ -1,20 +1,24 @@
|
||||
#ifndef settings_h
|
||||
#define settings_h
|
||||
|
||||
#include <glib.h>
|
||||
#include <gio/gio.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#define SETTINGS_FILE "sameboy-gtk3-settings.ini"
|
||||
|
||||
#define FORMAT_FOR_KEY_TYPE(type) KEY_FORMAT_##type
|
||||
#define KEY_FORMAT_string "%s"
|
||||
#define KEY_FORMAT_integer "%d"
|
||||
#define KEY_FORMAT_boolean "%d"
|
||||
#define FORMAT_FOR_KEY_TYPE(type) FORMAT_FOR_##type
|
||||
#define FORMAT_FOR_string "%s"
|
||||
#define FORMAT_FOR_integer "%d"
|
||||
#define FORMAT_FOR_boolean "%d"
|
||||
|
||||
#define GTYPE_FOR_KEY_TYPE(type) GTYPE_##type
|
||||
#define GTYPE_string gchar *
|
||||
#define GTYPE_integer gint
|
||||
#define GTYPE_boolean gboolean
|
||||
#define GTYPE_OF_KEY_TYPE(type) GTYPE_OF_##type
|
||||
#define GTYPE_OF_string gchar *
|
||||
#define GTYPE_OF_integer gint
|
||||
#define GTYPE_OF_boolean gboolean
|
||||
|
||||
#define GTYPE_IS_POINTER(type) GTYPE_IS_POINTER_##type
|
||||
#define GTYPE_IS_POINTER_string 1
|
||||
#define GTYPE_IS_POINTER_integer 0
|
||||
#define GTYPE_IS_POINTER_boolean 0
|
||||
|
||||
// Note: Make sure to use a member name only once for the whole config struct
|
||||
#define EXPAND_CONFIG \
|
||||
@ -30,6 +34,7 @@
|
||||
EXPAND_GROUP_MEMBER(color_correction_id, string) \
|
||||
EXPAND_GROUP_MEMBER(keep_aspect_ratio, boolean) \
|
||||
EXPAND_GROUP_MEMBER(use_integer_scaling, boolean) \
|
||||
EXPAND_GROUP_MEMBER(menubar_override, string) \
|
||||
) \
|
||||
EXPAND_GROUP(Audio, \
|
||||
EXPAND_GROUP_MEMBER(high_pass_filter_id, string) \
|
||||
@ -40,12 +45,18 @@
|
||||
|
||||
typedef struct config_t {
|
||||
#define EXPAND_GROUP(group_name, members) members
|
||||
#define EXPAND_GROUP_MEMBER(member, key_type) GTYPE_FOR_KEY_TYPE(key_type) member;
|
||||
#define EXPAND_GROUP_MEMBER(member, key_type) GTYPE_OF_KEY_TYPE(key_type) member;
|
||||
EXPAND_CONFIG
|
||||
#undef EXPAND_GROUP
|
||||
#undef EXPAND_GROUP_MEMBER
|
||||
} config_t;
|
||||
|
||||
enum menubar_override {
|
||||
MENUBAR_AUTO,
|
||||
MENUBAR_SHOW,
|
||||
MENUBAR_HIDE
|
||||
};
|
||||
|
||||
gchar* settings_file_path;
|
||||
GKeyFile *key_file;
|
||||
config_t config;
|
||||
@ -53,12 +64,12 @@ config_t config;
|
||||
void print_config(config_t *config);
|
||||
void load_config_from_key_file(config_t *config, GKeyFile *key_file);
|
||||
|
||||
void init_settings(gchar *path);
|
||||
void init_settings(gchar *path, GtkWindow *preferences);
|
||||
int load_settings(void);
|
||||
void save_settings(void);
|
||||
void free_settings(void);
|
||||
|
||||
enum scaling_mode get_scaling_mode(void);
|
||||
void set_scaling_mode(enum scaling_mode);
|
||||
enum menubar_override get_show_menubar(void);
|
||||
void set_show_menubar(enum menubar_override);
|
||||
|
||||
#endif /* settings_h */
|
||||
|
Loading…
Reference in New Issue
Block a user