From 56308f8b73b555a5aa0c0659d956709de5778d82 Mon Sep 17 00:00:00 2001 From: Maximilian Mader Date: Mon, 23 Sep 2019 16:35:10 +0200 Subject: [PATCH] [GTK3] Stop the emulation when quitting --- gtk3/main.c | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/gtk3/main.c b/gtk3/main.c index 2f7df23..86c9596 100644 --- a/gtk3/main.c +++ b/gtk3/main.c @@ -22,6 +22,7 @@ typedef struct{ static void run(UserData *user_data); +GtkApplication *main_application; GtkBuilder *builder; GtkApplicationWindow *main_window; GtkGLArea *gl_area; @@ -37,6 +38,8 @@ static double clock_mutliplier = 1.0; static char *battery_save_path_ptr; static Rect rect; +static bool running = true; + unsigned char number_of_buffers(void) { bool should_blend = true; @@ -168,10 +171,15 @@ static GMenuModel *get_menu_model(GApplication *app, const char *id) { return menu ? G_MENU_MODEL(g_object_ref_sink(menu)) : NULL; } +static void quit(GApplication *app) { + g_application_quit(app); + running = false; +} + // app.quit GAction // Exits the application static void activate_quit(GSimpleAction *action, GVariant *parameter, gpointer user_data) { - g_application_quit(G_APPLICATION(user_data)); + quit(G_APPLICATION(user_data)); } // app.about GAction @@ -188,6 +196,10 @@ static GActionEntry app_entries[] = { { "about", activate_about, NULL, NULL, NULL }, }; +G_MODULE_EXPORT void on_quit(GtkWidget *w, gpointer app) { + quit(G_APPLICATION(app)); +} + G_MODULE_EXPORT void on_show_window(GtkWidget *w, gpointer window) { gtk_widget_show_all(GTK_WIDGET(window)); } @@ -311,10 +323,13 @@ static void activate(GApplication *app, gpointer user_data_gptr) { gtk_window_fullscreen(GTK_WINDOW(main_window)); } - g_signal_connect(main_window, "destroy", G_CALLBACK(gtk_main_quit), NULL); + g_signal_connect(main_window, "destroy", G_CALLBACK(on_quit), app); + gtk_application_add_window(GTK_APPLICATION(app), GTK_WINDOW(main_window)); gtk_widget_show_all(GTK_WIDGET(main_window)); + // Start the emulation loop. + // This loop takes care of the GTK main loop. run(user_data); } @@ -335,6 +350,10 @@ static void open(GApplication *app, GFile **files, gint n_files, const gchar *hi activate(app, user_data_gptr); } +static void shutdown(GApplication *app, GFile **files, gint n_files, const gchar *hint, gpointer user_data_gptr) { + g_print("SHUTDOWN\n"); +} + // This function gets called after the parsing of the commandline options has occurred. static gint handle_local_options(GApplication *app, GVariantDict *options, gpointer user_data_gptr) { UserData *user_data = user_data_gptr; @@ -432,7 +451,7 @@ static void run(UserData *user_data) { } /* Run emulation */ - while (true) { + while (running) { if (paused || rewind_paused) { while (gtk_events_pending()) { gtk_main_iteration(); @@ -456,7 +475,7 @@ static void run(UserData *user_data) { int main(int argc, char *argv[]) { // Create our GApplication and tell GTK that we are able to handle files - GtkApplication *app = gtk_application_new(APP_ID, G_APPLICATION_HANDLES_OPEN); + main_application = gtk_application_new(APP_ID, G_APPLICATION_HANDLES_OPEN); UserData user_data = { NULL }; @@ -467,23 +486,24 @@ int main(int argc, char *argv[]) { { NULL } }; // Setup our command line information - g_application_add_main_option_entries(G_APPLICATION(app), entries); - g_application_set_option_context_parameter_string(G_APPLICATION(app), "[FILE…]"); - g_application_set_option_context_summary(G_APPLICATION(app), "SameBoy is an open source Game Boy (DMG) and Game Boy Color (CGB) emulator."); + g_application_add_main_option_entries(G_APPLICATION(main_application), entries); + g_application_set_option_context_parameter_string(G_APPLICATION(main_application), "[FILE…]"); + g_application_set_option_context_summary(G_APPLICATION(main_application), "SameBoy is an open source Game Boy (DMG) and Game Boy Color (CGB) emulator."); // Add signal handlers - g_signal_connect(app, "handle-local-options", G_CALLBACK(handle_local_options), &user_data); - g_signal_connect(app, "startup", G_CALLBACK(startup), &user_data); - g_signal_connect(app, "activate", G_CALLBACK(activate), &user_data); - g_signal_connect(app, "open", G_CALLBACK(open), &user_data); + g_signal_connect(main_application, "handle-local-options", G_CALLBACK(handle_local_options), &user_data); + g_signal_connect(main_application, "startup", G_CALLBACK(startup), &user_data); + g_signal_connect(main_application, "activate", G_CALLBACK(activate), &user_data); + g_signal_connect(main_application, "open", G_CALLBACK(open), &user_data); + g_signal_connect(main_application, "shutdown", G_CALLBACK(shutdown), &user_data); #ifndef NDEBUG //gtk_window_set_interactive_debugging(true); #endif // Start our GApplication main loop - int status = g_application_run(G_APPLICATION(app), argc, argv); - g_object_unref(app); + int status = g_application_run(G_APPLICATION(main_application), argc, argv); + g_object_unref(main_application); return status; }