From a96a3c4b6d59451a92540440db724274421e86e5 Mon Sep 17 00:00:00 2001
From: Vito Caputo <vcaputo@pengaru.com>
Date: Wed, 5 Jul 2023 20:11:21 -0700
Subject: modules/rkt: basic support for Rocket reconnects

This changes things so rkt won't exit with an error @ startup if
RocketEditor isn't already listening.  It also tolerates
RocketEditor going away, and will show a "OFFLINE" overlay status
text should that happen w/connect=on.

Some status text has also been added to the "EXIT SCENE" 99999
scene for both the RocketEditor connection and the scener
enabled/disabled status.  No indicator yet for if scener has a
connection though, only if it's listening or not via listen=on.
---
 src/modules/rkt/rkt.c | 41 ++++++++++++++++++++++++++++++++---------
 src/modules/rkt/rkt.h |  1 +
 2 files changed, 33 insertions(+), 9 deletions(-)

(limited to 'src/modules/rkt')

diff --git a/src/modules/rkt/rkt.c b/src/modules/rkt/rkt.c
index d8bab4b..ab9d460 100644
--- a/src/modules/rkt/rkt.c
+++ b/src/modules/rkt/rkt.c
@@ -181,13 +181,18 @@ static int rkt_pipe_update(void *context, til_stream_pipe_t *pipe, const void *o
 
 static void rkt_update_rocket(rkt_context_t *ctxt, unsigned ticks)
 {
+	rkt_setup_t	*s = (rkt_setup_t *)ctxt->til_module_context.setup;
+
 	if (!ctxt->paused)
 		ctxt->rocket_row += ((double)(ticks - ctxt->last_ticks)) * ctxt->rows_per_ms;
 
 	ctxt->last_ticks = ticks;
 
-	/* ctxt->rocket_row needs to be updated */
-	sync_update(ctxt->sync_device, ctxt->rocket_row, &rkt_sync_cb, ctxt);
+	if (!s->connect)
+		return;
+
+	if (!ctxt->connected || sync_update(ctxt->sync_device, ctxt->rocket_row, &rkt_sync_cb, ctxt) < 0)
+		ctxt->connected = !sync_tcp_connect(ctxt->sync_device, s->host, s->port);
 }
 
 
@@ -212,11 +217,8 @@ static til_module_context_t * rkt_create_context(const til_module_t *module, til
 	if (!ctxt->sync_device)
 		return til_module_context_free(&ctxt->til_module_context);
 
-	if (s->connect) {
-		/* XXX: it'd be better if we just reconnected periodically instead of hard failing */
-		if (sync_tcp_connect(ctxt->sync_device, s->host, s->port))
-			return til_module_context_free(&ctxt->til_module_context);
-	}
+	if (s->connect && !sync_tcp_connect(ctxt->sync_device, s->host, s->port))
+		ctxt->connected = 1;
 
 	ctxt->scene_track = sync_get_trackf(ctxt->sync_device, "%s:scene", setup->path);
 	if (!ctxt->scene_track)
@@ -283,10 +285,12 @@ static void rkt_render_fragment(til_module_context_t *context, til_stream_t *str
 			/* 99999 is treated as an "end of sequence" scene, but only honored when connect=off (player mode) */
 			til_stream_end(stream);
 		} else {
-			txt_t	*msg = txt_newf("%s: %s @ %u",
+			txt_t	*msg = txt_newf("%s: %s @ %u [%s] [%s]",
 						context->setup->path,
 						scene == 99999 ? "EXIT SCENE" : "NO SCENE",
-						scene);
+						scene,
+						((rkt_setup_t *)context->setup)->connect ? (ctxt->connected ? "ONLINE" : "OFFLINE") : "PLAYER",
+						ctxt->scener ? "SCENER" : "NOSCENER");
 
 			/* TODO: creating/destroying this every frame is dumb, but
 			 * as this is a diagnostic it's not so important.
@@ -304,6 +308,25 @@ static void rkt_render_fragment(til_module_context_t *context, til_stream_t *str
 					   });
 			txt_free(msg);
 		}
+
+		if (scene < ctxt->n_scenes &&
+		    scene != 999999 &&
+		    ((rkt_setup_t *)context->setup)->connect && !ctxt->connected) {
+			txt_t	*msg = txt_newf("OFFLINE");
+
+			/* TODO: as mentioned above, creating/destroying this every frame is dumb,
+			 * will revisit the status text in the future.  Not a huge priority since
+			 * none of this should be active in "production" playback mode.
+			 */
+			txt_render_fragment(msg, *fragment_ptr, 0xffffffff,
+					    0, 0,
+					    (txt_align_t){
+						.horiz = TXT_HALIGN_LEFT,
+						.vert = TXT_VALIGN_TOP,
+					   });
+
+			txt_free(msg);
+		}
 	}
 }
 
diff --git a/src/modules/rkt/rkt.h b/src/modules/rkt/rkt.h
index b06c6a4..ae0fa64 100644
--- a/src/modules/rkt/rkt.h
+++ b/src/modules/rkt/rkt.h
@@ -21,6 +21,7 @@ typedef struct rkt_context_t {
 	double			rocket_row;
 	unsigned		last_ticks;
 	unsigned		paused:1;
+	unsigned		connected:1;	/* currently connected to the RocketEditor */
 	size_t			n_scenes;
 	rkt_scene_t		*scenes;
 	unsigned		scene;		/* current scene (usually driven by the scene track data,
-- 
cgit v1.2.3