diff --git a/Makefile b/Makefile index 20469e9..dec817d 100644 --- a/Makefile +++ b/Makefile @@ -83,7 +83,7 @@ endif CFLAGS += -Werror -Wall -Wno-unused-result -Wno-strict-aliasing -Wno-unknown-warning -Wno-unknown-warning-option -Wno-multichar -Wno-int-in-bool-context -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES SDL_LDFLAGS := -lSDL2 -lGL -GTK3_CFLAGS := `pkg-config --cflags gio-2.0 gtk+-3.0 epoxy sdl2` -DGTK_DISABLE_DEPRECATED=1 -DG_DISABLE_DEPRECATED=1 -DRESOURCE_PREFIX=\"/io/github/sameboy/\" -DAPP_ID=\"io.github.sameboy\" +GTK3_CFLAGS := `pkg-config --cflags gio-2.0 gtk+-3.0 epoxy sdl2` -DGTK_DISABLE_DEPRECATED=1 -DG_DISABLE_DEPRECATED=1 -DG_LOG_DOMAIN=\"SameBoy\" -DRESOURCE_PREFIX=\"/io/github/sameboy/\" -DAPP_ID=\"io.github.sameboy\" GTK3_LDFLAGS := `pkg-config --libs gio-2.0 gtk+-3.0 epoxy sdl2` # TODO: REMOVE DISABLE UNUSED WARNINGS diff --git a/gtk3/main.c b/gtk3/main.c index 2b15f24..2fff2da 100644 --- a/gtk3/main.c +++ b/gtk3/main.c @@ -133,7 +133,7 @@ static gint handle_local_options(GApplication *app, GVariantDict *options, gpoin guint32 count; if (g_variant_dict_lookup(options, "version", "b", &count)) { - g_print("SameBoy v" xstr(VERSION) "\n"); + g_message("SameBoy v" xstr(VERSION)); return EXIT_SUCCESS; } @@ -153,7 +153,7 @@ static gint handle_local_options(GApplication *app, GVariantDict *options, gpoin } else { gui_data->cli_options.model = GB_MODEL_DMG_B; - g_printerr("Unsupported revision: %s\nFalling back to DMG-B", model_name); + g_warning("Unsupported revision: %s\nFalling back to DMG-B", model_name); } } else if (g_str_has_prefix(model_name, "SGB")) { @@ -168,7 +168,7 @@ static gint handle_local_options(GApplication *app, GVariantDict *options, gpoin } else { gui_data->cli_options.model = GB_MODEL_SGB2; - g_printerr("Unsupported revision: %s\nFalling back to SGB2", model_name); + g_warning("Unsupported revision: %s\nFalling back to SGB2", model_name); } } else if (g_str_has_prefix(model_name, "CGB")) { @@ -180,14 +180,14 @@ static gint handle_local_options(GApplication *app, GVariantDict *options, gpoin } else { gui_data->cli_options.model = GB_MODEL_CGB_E; - g_printerr("Unsupported revision: %s\nFalling back to CGB-E", model_name); + g_warning("Unsupported revision: %s\nFalling back to CGB-E", model_name); } } else if (g_str_has_prefix(model_name, "AGB")) { gui_data->cli_options.model = GB_MODEL_AGB; } else { - g_printerr("Unknown model: %s\n", model_name); + g_warning("Unknown model: %s", model_name); exit(EXIT_FAILURE); } } @@ -220,7 +220,7 @@ void gl_check_realize(GtkWidget *w, gpointer user_data_gptr) { GdkGLContext *context = gdk_window_create_gl_context(gdk_window, &error); if (error != NULL) { - g_printerr("Failed to create context: %s\n", error->message); + g_warning("Failed to create context: %s", error->message); g_error_free(error); *result = FALSE; } @@ -234,7 +234,7 @@ void gl_check_realize(GtkWidget *w, gpointer user_data_gptr) { gdk_gl_context_clear_current(); - g_print("OpenGL version: %d\n", version); + g_debug("OpenGL version: %d", version); *result = version >= 32; } @@ -242,7 +242,7 @@ void gl_check_realize(GtkWidget *w, gpointer user_data_gptr) { static gboolean init_controllers() { if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) < 0) { - g_print("Failed to initialize game controller support: %s\n", SDL_GetError()); + g_warning("Failed to initialize game controller support: %s", SDL_GetError()); return FALSE; } @@ -258,7 +258,7 @@ static gboolean init_controllers() { const gint val = SDL_GameControllerAddMappingsFromRW(SDL_RWFromMem((void *)db_data, db_data_size), 1); if (val < 0) { - g_warning("Failed to load controller mappings: %s\n", SDL_GetError()); + g_warning("Failed to load controller mappings: %s", SDL_GetError()); } g_bytes_unref(db_f); @@ -274,7 +274,7 @@ static gboolean init_controllers() { break; } else { - g_warning("Could not open gamecontroller %i: %s\n", i, SDL_GetError()); + g_warning("Could not open gamecontroller %i: %s", i, SDL_GetError()); } } } @@ -289,7 +289,7 @@ static gboolean init_audio() { SDL_QuitSubSystem(SDL_INIT_AUDIO); if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { - g_print("Failed to initialize audio: %s\n", SDL_GetError()); + g_warning("Failed to initialize audio: %s", SDL_GetError()); return FALSE; } @@ -319,7 +319,7 @@ static gboolean init_audio() { device_id = SDL_OpenAudioDevice(0, 0, &want_aspec, &have_aspec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE); - g_print("Requested Sample Rate: %d Hz\nUsed Sample Rate: %d Hz\n", want_aspec.freq, have_aspec.freq); + g_debug("Requested Sample Rate: %d Hz\nUsed Sample Rate: %d Hz", want_aspec.freq, have_aspec.freq); SDL_PauseAudioDevice(device_id, audio_playing? 0 : 1); GB_set_sample_rate(&gb, have_aspec.freq); @@ -491,7 +491,7 @@ static void setup_menu(GApplication *app) { if (desktop == NULL || g_str_equal(desktop, "")) desktop = (gchar *)gdm_session; if (desktop == NULL || g_str_equal(desktop, "")) desktop = (gchar *)desktop_session; - g_print("XDG_CURRENT_DESKTOP: %s\nGDMSESSION: %s\nDESKTOP_SESSION: %s\nChosen value: %s\nShow menu in shell: %d\n", xdg_current_desktop, gdm_session, desktop_session, desktop, show_in_shell); + g_debug("XDG_CURRENT_DESKTOP: %s\nGDMSESSION: %s\nDESKTOP_SESSION: %s\nChosen value: %s\nShow menu in shell: %d", xdg_current_desktop, gdm_session, desktop_session, desktop, show_in_shell); if (desktop != NULL && show_in_shell) { menubar_type = MENUBAR_SHOW_IN_SHELL; @@ -514,23 +514,23 @@ static void setup_menu(GApplication *app) { switch (menubar_type) { case MENUBAR_AUTO: - g_error("Unreachable\n"); + g_warning("Unreachable"); break; case MENUBAR_SHOW_IN_SHELL: - g_print("Showing menu in the shell\n"); + g_debug("Showing menu in the shell"); gtk_application_set_menubar(GTK_APPLICATION(app), menubar_model); break; case MENUBAR_SHOW_IN_WINDOW: { - g_print("Showing menu in the window\n"); + g_debug("Showing menu in the window"); GtkMenuBar *menubar = GTK_MENU_BAR(gtk_menu_bar_new_from_model(menubar_model)); gtk_box_pack_start(GTK_BOX(main_window_container), GTK_WIDGET(menubar), FALSE, FALSE, 0); break; } case MENUBAR_SHOW_HAMBURGER: { - g_print("Showing hamburger\n"); + g_debug("Showing hamburger"); // Attach a custom title bar GtkWidget *titlebar = builder_get(GTK_WIDGET, "main_header_bar"); gtk_header_bar_set_title(GTK_HEADER_BAR(titlebar), gtk_window_get_title(GTK_WINDOW(main_window))); @@ -626,7 +626,7 @@ static void startup(GApplication *app, gpointer gui_data_gptr) { // in this callback create a GdkGLContext on this window. But instead of running the GTK main loop // we just realize and destroy the dummy window and compare the context’s version in the realize callback. supports_gl = test_gl_support(); - g_print("OpenGL supported: %s\n", supports_gl? "Yes" : "No"); + g_debug("OpenGL supported: %s", supports_gl? "Yes" : "No"); builder = gtk_builder_new_from_resource(RESOURCE_PREFIX "ui/window.ui"); gtk_builder_connect_signals(builder, NULL); @@ -792,7 +792,7 @@ static void activate(GApplication *app, gpointer gui_data_gptr) { // This function gets called when the application is closed. static void shutdown(GApplication *app, GFile **files, gint n_files, const gchar *hint, gpointer gui_data_gptr) { - g_print("SHUTDOWN\n"); + g_debug("SHUTDOWN"); stop(&gui_data); while (stopping); @@ -818,7 +818,7 @@ static void open(GApplication *app, GFile **files, gint n_files, const gchar *hi GuiData *gui_data = gui_data_gptr; if (n_files > 1) { - g_printerr("More than one file specified\n"); + g_warning("More than one file specified"); exit(EXIT_FAILURE); } @@ -932,7 +932,7 @@ static void activate_open(GSimpleAction *action, GVariant *parameter, gpointer a if (res == GTK_RESPONSE_ACCEPT) { // TODO: Emit an event for our emulation loop - g_print("%s\n", gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(native))); + g_message("%s", gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(native))); } g_object_unref(native); @@ -1026,10 +1026,10 @@ static void on_quit(GtkWidget *w, gpointer app) { static void gl_init(GtkWidget *w) { GtkGLArea *gl_area = GTK_GL_AREA(w); - g_print("GL_INIT\n"); + g_debug("GL_INIT"); const char *renderer; - g_print("GL Context: %p\n", gtk_gl_area_get_context(gl_area)); + g_debug("GL Context: %p", gtk_gl_area_get_context(gl_area)); gtk_gl_area_make_current(gl_area); @@ -1038,7 +1038,7 @@ static void gl_init(GtkWidget *w) { } renderer = (char *)glGetString(GL_RENDERER); - g_print("GtkGLArea on %s\n", renderer ? renderer : "Unknown"); + g_debug("GtkGLArea on %s", renderer ? renderer : "Unknown"); if (config.shader == NULL || (!init_shader_with_name(&shader, config.shader) && !init_shader_with_name(&shader, "NearestNeighbor"))) { GError *error = g_error_new_literal(g_quark_from_string("sameboy-gl-error"), 1, "Failed to initialize shaders"); @@ -1053,7 +1053,7 @@ static void gl_init(GtkWidget *w) { error: if (gtk_gl_area_get_error(gl_area) != NULL) { - g_printerr("GtkGLArea: %s\n", gtk_gl_area_get_error(gl_area)->message); + g_warning("GtkGLArea: %s", gtk_gl_area_get_error(gl_area)->message); } create_fallback_canvas(); @@ -1858,9 +1858,9 @@ static void load_boot_rom(GuiData *gui_data) { gsize boot_rom_size; if (gui_data->cli_options.boot_rom_path != NULL) { - g_print("Trying to load boot ROM from %s\n", gui_data->cli_options.boot_rom_path); + g_message("Trying to load boot ROM from %s", gui_data->cli_options.boot_rom_path); if (GB_load_boot_rom(&gb, gui_data->cli_options.boot_rom_path)) { - g_printerr("Falling back to boot ROM from config\n"); + g_warning("Falling back to boot ROM from config"); goto config_boot_rom; } } @@ -1894,11 +1894,11 @@ static void load_boot_rom(GuiData *gui_data) { if (config.boot_rom_path != NULL && g_strcmp0(config.boot_rom_path, "other") != 0 && g_strcmp0(config.boot_rom_path, "auto") != 0) { boot_rom_path = g_build_filename(config.boot_rom_path, boot_rom_name, NULL); - g_print("Trying to load boot ROM from %s\n", boot_rom_path); + g_message("Trying to load boot ROM from %s", boot_rom_path); if (GB_load_boot_rom(&gb, boot_rom_path)) { g_free(boot_rom_path); - g_printerr("Falling back to internal boot ROM\n"); + g_warning("Falling back to internal boot ROM"); goto internal_boot_rom; } @@ -1910,7 +1910,7 @@ static void load_boot_rom(GuiData *gui_data) { g_free(boot_rom_path); if (boot_rom_f == NULL) { - g_printerr("Failed to load internal boot ROM: %s\n", boot_rom_path); + g_warning("Failed to load internal boot ROM: %s", boot_rom_path); g_error_free(error); exit(EXIT_FAILURE); } @@ -1942,7 +1942,7 @@ static void stop(GuiData *gui_data) { } static void reset(GuiData *gui_data) { - g_print("Reset: %d == %d\n", get_model(), gui_data->prev_model); + g_debug("Reset: %d == %d", get_model(), gui_data->prev_model); GB_model_t current_model = get_model(); if (gui_data->prev_model == -1 || gui_data->prev_model == current_model) { @@ -1968,7 +1968,7 @@ static void reset(GuiData *gui_data) { char *path = g_file_get_path(gui_data->file); if (GB_load_rom(&gb, path) != 0) { - g_print("Failed to load ROM: %s", path); + g_warning("Failed to load ROM: %s", path); } g_free(path); diff --git a/gtk3/main.h b/gtk3/main.h index 80ef074..1fadb01 100644 --- a/gtk3/main.h +++ b/gtk3/main.h @@ -4,6 +4,8 @@ // used for audio and game controllers #include "SDL.h" +#define G_LOG_USE_STRUCTURED + #include #include #include diff --git a/gtk3/settings.c b/gtk3/settings.c index c0a7123..3ff1972 100644 --- a/gtk3/settings.c +++ b/gtk3/settings.c @@ -4,17 +4,17 @@ static void print_config_error(GError *error) { if (error == NULL) return; 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); + g_warning("Config error: %s", error->message); } } -void print_config(config_t *config) { +void _print_config(config_t *config, GLogLevelFlags log_level) { #define EXPAND_GROUP(group_name, members) \ - g_print("[%s]\n", #group_name); \ + g_log(G_LOG_DOMAIN, log_level, "[%s]", #group_name); \ members #define EXPAND_GROUP_MEMBER(member, key_type, default_value) \ - g_print("%s="FORMAT_FOR_KEY_TYPE(key_type)"\n", #member, config->member); + g_log(G_LOG_DOMAIN, log_level, "%s="FORMAT_FOR_KEY_TYPE(key_type)"", #member, config->member); EXPAND_CONFIG @@ -23,7 +23,7 @@ void print_config(config_t *config) { } void load_config_from_key_file(config_t *config, GKeyFile *key_file) { - g_print("Loading config from key file\n"); + g_message("Loading config from key file"); GError *error = NULL; gchar *group_name; @@ -42,15 +42,19 @@ void load_config_from_key_file(config_t *config, GKeyFile *key_file) { EXPAND_CONFIG if (config->rewind_duration > 600) { - g_warning("Setting Emulation.rewind_duration too high might affect performance.\n"); + g_warning("Setting Emulation.rewind_duration too high might affect performance."); } #undef EXPAND_GROUP #undef EXPAND_GROUP_MEMBER } +void print_config(config_t *config) { + _print_config(config, G_LOG_LEVEL_MESSAGE); +} + void save_config_to_key_file(config_t *config, GKeyFile *key_file) { - g_print("Saving config to key file\n"); + g_message("Saving config to key file"); GError *error = NULL; gchar *group_name; @@ -67,12 +71,12 @@ void save_config_to_key_file(config_t *config, GKeyFile *key_file) { } \ else if (g_key_file_has_key(key_file, group_name, #member, &error)) { \ if (error != NULL) { \ - g_printerr("%s\n", error->message); \ + g_warning("%s", error->message); \ g_clear_error(&error); \ } \ g_key_file_remove_key(key_file, group_name, #member, &error); \ if (error != NULL) { \ - g_printerr("%s\n", error->message); \ + g_warning("%s", error->message); \ g_clear_error(&error); \ } \ } @@ -136,7 +140,7 @@ void init_settings(gchar *path, GtkWindow *preferences) { int load_settings(void) { GError *error = NULL; - g_print("Trying to load settings from %s\n", settings_file_path); + g_message("Trying to load settings from %s", 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) { @@ -150,7 +154,7 @@ int load_settings(void) { } load_config_from_key_file(&config, key_file); - print_config(&config); + _print_config(&config, G_LOG_LEVEL_DEBUG); return 0; } @@ -158,7 +162,7 @@ int load_settings(void) { void save_settings(void) { GError *error = NULL; - g_print("Trying to save settings to %s\n", settings_file_path); + g_message("Trying to save settings to %s", settings_file_path); save_config_to_key_file(&config, key_file); @@ -208,7 +212,7 @@ enum menubar_type_t get_show_menubar(void) { } // This should not happen - g_warning("Unknown menubar setting: %s\nFalling back to “Auto”\n", config.menubar_override); + g_warning("Unknown menubar setting: %s\nFalling back to “Auto”", config.menubar_override); default_value: return MENUBAR_AUTO; } @@ -246,7 +250,7 @@ GB_color_correction_mode_t get_color_correction_mode(void) { } // This should not happen - g_warning("Unknown color correction mode: %s\nFalling back to “Emulate Hardware”\n", config.color_correction_id); + g_warning("Unknown color correction mode: %s\nFalling back to “Emulate Hardware”", config.color_correction_id); default_value: return GB_COLOR_CORRECTION_EMULATE_HARDWARE; } @@ -281,7 +285,7 @@ GB_highpass_mode_t get_highpass_mode(void) { } // This should not happen - g_warning("Unknown highpass mode: %s\nFalling back to “Accurate”\n", config.high_pass_filter_id); + g_warning("Unknown highpass mode: %s\nFalling back to “Accurate”", config.high_pass_filter_id); default_value: return GB_HIGHPASS_ACCURATE; } @@ -291,7 +295,7 @@ void set_highpass_mode(GB_highpass_mode_t mode) { config.high_pass_filter_id = "disabled"; break; case GB_HIGHPASS_MAX: - g_warning("GB_HIGHPASS_MAX is not a valid highpass mode, falling back to “Accurate”.\n"); + g_warning("GB_HIGHPASS_MAX is not a valid highpass mode, falling back to “Accurate”."); case GB_HIGHPASS_ACCURATE: config.high_pass_filter_id = "emulate_hardware"; break; diff --git a/gtk3/shader.c b/gtk3/shader.c index 88a1aa8..93bf5b2 100644 --- a/gtk3/shader.c +++ b/gtk3/shader.c @@ -26,7 +26,7 @@ static GLuint create_shader(const char *source, GLenum type) if (status == GL_FALSE) { GLchar messages[1024]; glGetShaderInfoLog(shader, sizeof(messages), 0, &messages[0]); - g_printerr("GLSL Shader Error: %s", messages); + g_warning("GLSL Shader Error: %s", messages); } return shader; } @@ -53,7 +53,7 @@ static GLuint create_program(const char *vsh, const char *fsh) if (status == GL_FALSE) { GLchar messages[1024]; glGetProgramInfoLog(program, sizeof(messages), 0, &messages[0]); - g_printerr("GLSL Program Error: %s", messages); + g_warning("GLSL Program Error: %s", messages); } // Delete shaders @@ -78,7 +78,7 @@ bool init_shader_with_name(shader_t *shader, const char *name) master_shader_code = g_bytes_get_data(master_shader_f, &master_shader_code_size); if (!master_shader_f) { - g_printerr("Failed to load master shader: %s", error->message); + g_warning("Failed to load master shader: %s", error->message); g_error_free(error); return false; } @@ -96,7 +96,7 @@ bool init_shader_with_name(shader_t *shader, const char *name) GBytes *shader_f = g_resources_lookup_data(shader_path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error); if (!shader_f) { - g_printerr("Failed to load shader \"%s\": %s", shader_path, error->message); + g_warning("Failed to load shader \"%s\": %s", shader_path, error->message); g_error_free(error); return false; }