[GTK3] Move config handling into settings.c

This commit is contained in:
Maximilian Mader 2019-09-24 16:14:06 +02:00
parent 4caa1d0266
commit 511d65fa45
Signed by: Max
GPG Key ID: F71D56A3151C4FB3
3 changed files with 66 additions and 34 deletions

View File

@ -5,17 +5,18 @@
#include <stdio.h>
#include <string.h>
#include <Core/gb.h>
#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", "<file path>" },
{ "model", 'm', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, NULL, "Override the model type to emulate", "<model type>" },
{ "bootrom", 'b', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &user_data.bootrom_path, "Path to the boot ROM to use", "<file path>" },
{ "model", 'm', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, NULL, "Override the model type to emulate", "<model type>" },
{ "config", 'c', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &user_data.config_path, "Override the path of the configuration file", "<file path>" },
{ NULL }
};
// Setup our command line information

39
gtk3/settings.c Normal file
View File

@ -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;
}
}

16
gtk3/settings.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef settings_h
#define settings_h
#include <glib.h>
#include <gio/gio.h>
#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 */