I'm writing an application that utilizes GTK to create a GUI.
In the example code below, the function run_gui starts the GTK application.
When something goes wrong inside one of the event handlers, I would like the GTK app to close, and I would like run_gui to know that the app closed because of a failure. A natural way to do that seems to be to make g_application_run return a particular status code upon returning.
I learned that a suitable way to close the app would be to call g_quit_application from an event handler (please correct if not so). However, I would like to know how I can set the status code returned from g_application_run, so run_gui can inspect it and act accordingly.
Code to illustrate what I want to do:
#include <gtk/gtk.h>
static void activate(GtkApplication* app, gpointer user_data) {
GtkWidget* window = gtk_application_window_new (app);
gtk_window_set_title(GTK_WINDOW(window), "Window");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
gtk_widget_show_all(window);
bool success = do_something();
if (!success) {
/* How do I set the exit status of the GTK app? */
g_application_quit(G_APPLICATION(app));
}
}
void run_gui(void) {
GtkApplication* app = gtk_application_new("my.app", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
int status = g_application_run(G_APPLICATION(app), 0, NULL);
g_object_unref(app);
if (status == 0) {
/* Do something about success */
} else {
/* Do something about failure */
}
}