From 511d65fa45e7bc916a2a436fd1d22ca83ae854b3 Mon Sep 17 00:00:00 2001 From: Maximilian Mader Date: Tue, 24 Sep 2019 16:14:06 +0200 Subject: [PATCH] [GTK3] Move config handling into settings.c --- gtk3/main.c | 45 +++++++++++---------------------------------- gtk3/settings.c | 39 +++++++++++++++++++++++++++++++++++++++ gtk3/settings.h | 16 ++++++++++++++++ 3 files changed, 66 insertions(+), 34 deletions(-) create mode 100644 gtk3/settings.c create mode 100644 gtk3/settings.h diff --git a/gtk3/main.c b/gtk3/main.c index cf72e67..8d253ff 100644 --- a/gtk3/main.c +++ b/gtk3/main.c @@ -5,17 +5,18 @@ #include #include #include + +#include "settings.h" #include "shader.h" #define str(x) #x #define xstr(x) str(x) -#define SETTINGS_FILE "sameboy-gtk3-settings.ini" - typedef struct UserData { bool fullscreen; GFile *file; - const gchar* bootrom_path; + const gchar *bootrom_path; + const gchar *config_path; GB_model_t model; } UserData; @@ -26,13 +27,11 @@ typedef struct{ static void run(UserData *user_data); -static GKeyFile *key_file; static GtkApplication *main_application; static GtkBuilder *builder; static GtkApplicationWindow *main_window; static GtkGLArea *gl_area; static shader_t shader; -static gchar* settings_file_path; static GB_gameboy_t gb; static uint32_t *image_buffers[3]; @@ -46,17 +45,6 @@ static Rect rect; static bool running = true; -static void save_settings(void) { - GError *error = NULL; - - // Save as a file. - if (!g_key_file_save_to_file(key_file, settings_file_path, &error)) { - g_warning ("Error saving %s: %s", settings_file_path, error->message); - g_error_free(error); - return; - } -} - static unsigned char number_of_buffers(void) { bool should_blend = true; @@ -269,7 +257,9 @@ G_MODULE_EXPORT void gl_finish() { } // This functions gets called immediately after registration of the GApplication static void startup(GApplication *app, gpointer user_data_gptr) { - // UserData *user_data = user_data_gptr; + UserData *user_data = user_data_gptr; + + init_settings(user_data->config_path); builder = gtk_builder_new_from_resource(RESOURCE_PREFIX "ui/window.ui"); gtk_builder_connect_signals(builder, NULL); @@ -295,20 +285,6 @@ static void startup(GApplication *app, gpointer user_data_gptr) { g_signal_connect(gl_area, "unrealize", G_CALLBACK(gl_finish), NULL); gtk_container_add(GTK_CONTAINER(main_window), GTK_WIDGET(gl_area)); - GError *error = NULL; - settings_file_path = g_build_filename(g_get_user_config_dir(), SETTINGS_FILE, NULL); - key_file = g_key_file_new(); - - 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 (!g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) { - g_warning("Error loading %s: %s", settings_file_path, error->message); - } - - g_error_free(error); - } - // Handle the whole menubar situation … if (show_menubar()) { // Show a classic menubar @@ -630,10 +606,11 @@ int main(int argc, char *argv[]) { // Define our command line parameters GOptionEntry entries[] = { - { "version", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, "Show the application version", NULL }, + { "version", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, "Show the application version", NULL }, { "fullscreen", 'f', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, "Start in fullscreen mode", NULL }, - { "bootrom", 'b', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &user_data.bootrom_path, "Path to the boot ROM to use", "" }, - { "model", 'm', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, NULL, "Override the model type to emulate", "" }, + { "bootrom", 'b', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &user_data.bootrom_path, "Path to the boot ROM to use", "" }, + { "model", 'm', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, NULL, "Override the model type to emulate", "" }, + { "config", 'c', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &user_data.config_path, "Override the path of the configuration file", "" }, { NULL } }; // Setup our command line information diff --git a/gtk3/settings.c b/gtk3/settings.c new file mode 100644 index 0000000..5463f88 --- /dev/null +++ b/gtk3/settings.c @@ -0,0 +1,39 @@ +#include "settings.h" + +void init_settings(const gchar *path) { + key_file = g_key_file_new(); + + GError *error = NULL; + + if (path != NULL) { + settings_file_path = path; + } + else { + settings_file_path = g_build_filename(g_get_user_config_dir(), SETTINGS_FILE, NULL); + } + + load_settings(); +} + +void 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)) { + g_warning("Error loading %s: %s", settings_file_path, error->message); + g_error_free(error); + } +} + +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 ("Error saving %s: %s", settings_file_path, error->message); + g_error_free(error); + return; + } +} diff --git a/gtk3/settings.h b/gtk3/settings.h new file mode 100644 index 0000000..92c3477 --- /dev/null +++ b/gtk3/settings.h @@ -0,0 +1,16 @@ +#ifndef settings_h +#define settings_h + +#include +#include + +#define SETTINGS_FILE "sameboy-gtk3-settings.ini" + +GKeyFile *key_file; +const gchar* settings_file_path; + +void init_settings(const gchar *path); +void load_settings(void); +void save_settings(void); + +#endif /* settings_h */