aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2021-06-21 22:44:31 +0200
committerThomas White <taw@physics.org>2021-06-21 22:45:16 +0200
commit63f708e2ef4525e9e38a7d6cb7140910a718cbac (patch)
tree89e892fc3e80e1ee08194b45403791fffd361c66
parenta460a40e9eed7519def796be8c7fab605465c421 (diff)
Fixture display: Shut down REPL connection gracefully at exit
This avoids horrible spew on the main thread REPL (leaving only minor spew instead).
-rw-r--r--src/repl-connection.c27
-rw-r--r--src/repl-connection.h2
-rw-r--r--src/starlet-fixture-display.c28
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; i<len; i++ ) {
@@ -220,3 +235,15 @@ int repl_send(ReplConnection *repl, const char *line)
}
return 0;
}
+
+
+int repl_closed(ReplConnection *repl)
+{
+ return repl->conn == 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);