From 63f708e2ef4525e9e38a7d6cb7140910a718cbac Mon Sep 17 00:00:00 2001 From: Thomas White Date: Mon, 21 Jun 2021 22:44:31 +0200 Subject: Fixture display: Shut down REPL connection gracefully at exit This avoids horrible spew on the main thread REPL (leaving only minor spew instead). --- src/repl-connection.c | 27 +++++++++++++++++++++++++++ src/repl-connection.h | 2 ++ src/starlet-fixture-display.c | 28 ++++++++++++++++++++++++---- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/repl-connection.c b/src/repl-connection.c index a159c0b..7d68d25 100644 --- a/src/repl-connection.c +++ b/src/repl-connection.c @@ -129,6 +129,21 @@ static void input_ready(GObject *source, GAsyncResult *res, gpointer vp) char *remaining; len = g_input_stream_read_finish(G_INPUT_STREAM(source), res, &error); + + if ( len == 0 ) { + printf("%p: EOF\n", repl); + g_object_unref(repl->conn); + repl->conn = NULL; + return; + } + + if ( len == -1 ) { + printf("%p: Error: %s\n", repl, error->message); + g_object_unref(repl->conn); + repl->conn = NULL; + return; + } + repl->inbuf[len] = '\0'; for ( i=0; iconn == NULL; +} + + +void repl_connection_close(ReplConnection *repl) +{ + repl_send(repl, ",q"); +} diff --git a/src/repl-connection.h b/src/repl-connection.h index f8b82f6..69f3f0b 100644 --- a/src/repl-connection.h +++ b/src/repl-connection.h @@ -30,5 +30,7 @@ extern ReplConnection *repl_connection_new(const char *socket, void (*process_func)(SCM sexp, void *data), void *data); extern int repl_send(ReplConnection *repl, const char *line); +extern void repl_connection_close(ReplConnection *repl); +extern int repl_closed(ReplConnection *repl); #endif /* REPL_CONNECTION_H */ diff --git a/src/starlet-fixture-display.c b/src/starlet-fixture-display.c index 4ac3e84..d04afa7 100644 --- a/src/starlet-fixture-display.c +++ b/src/starlet-fixture-display.c @@ -53,6 +53,7 @@ struct fixture_display int n_fixtures; GtkWidget *da; ReplConnection *repl; + int shutdown; }; @@ -176,6 +177,13 @@ static void redraw(struct fixture_display *fixd) } +static void shutdown_sig(GtkWidget *window, struct fixture_display *fixd) +{ + repl_connection_close(fixd->repl); + fixd->shutdown = TRUE; +} + + static gboolean key_press_sig(GtkWidget *da, GdkEventKey *event, struct fixture_display *fixd) { int claim = 1; @@ -215,8 +223,19 @@ static gint realise_sig(GtkWidget *da, struct fixture_display *fixd) static gboolean redraw_cb(gpointer data) { - redraw((struct fixture_display *)data); - return G_SOURCE_CONTINUE; + struct fixture_display *fixd = data; + if ( !fixd->shutdown ) { + redraw(fixd); + return G_SOURCE_CONTINUE; + } else { + if ( repl_closed(fixd->repl) ) { + gtk_main_quit(); + return G_SOURCE_REMOVE; + } else { + return G_SOURCE_CONTINUE; + } + } + } @@ -346,8 +365,8 @@ int main(int argc, char *argv[]) /* Create main window */ mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size(GTK_WINDOW(mainwindow), 1024, 768); - g_signal_connect_swapped(G_OBJECT(mainwindow), "destroy", - gtk_main_quit, NULL); + g_signal_connect(G_OBJECT(mainwindow), "destroy", + G_CALLBACK(shutdown_sig), &fixd); gtk_window_set_title(GTK_WINDOW(mainwindow), "Starlet fixture display"); da = gtk_drawing_area_new(); @@ -356,6 +375,7 @@ int main(int argc, char *argv[]) fixd.fixtures = NULL; fixd.n_fixtures = 0; fixd.da = da; + fixd.shutdown = FALSE; gtk_container_add(GTK_CONTAINER(mainwindow), GTK_WIDGET(da)); gtk_widget_set_can_focus(GTK_WIDGET(da), TRUE); -- cgit v1.2.3