aboutsummaryrefslogtreecommitdiffstats
path: root/src/client.c
diff options
context:
space:
mode:
authorsl0p <sl0p@sl0p.foo>2026-08-13 22:52:33 +0200
committersl0p <sl0p@sl0p.foo>2026-08-13 22:52:33 +0200
commit7a6fa40a9c49245cf569e10e6ed635f9a6cbbcb7 (patch)
tree93f11a476bcdb6fe50f2f3e28fa865b86d3d06c3 /src/client.c
parentsl0ptty: M1 — layout tree, splits, focus, frames, and the hit list in use (diff)
downloadslosh-7a6fa40a9c49245cf569e10e6ed635f9a6cbbcb7.tar.gz
slosh-7a6fa40a9c49245cf569e10e6ed635f9a6cbbcb7.tar.xz
slosh-7a6fa40a9c49245cf569e10e6ed635f9a6cbbcb7.zip
sl0ptty: M2 — server/client split, detach/reattach, control socket
The session now outlives its client, which is the point: agents keep running while you are gone. The server owns the panes, the layout and the renderer; the client is a terminal in raw mode and a socket, forwarding bytes and painting what it is sent. It does not even decode input, so the decoder can change without redeploying clients. sl0ptty attach to 'main', creating it if nobody is home sl0ptty -s NAME a named session sl0ptty ls live sessions sl0ptty cmd LINE one control command C-a d detach Connections are not clients. A connection becomes the display client only by sending HELLO; control commands and liveness probes are just connections and never displace anybody. This was found the hard way, one hour after the server first ran: 'sl0ptty ls' kicked the attached client off, because ls connects to see whether a socket answers. That is precisely the fault that gave the zellij fork a 20-25% CLI failure rate, rebuilt and then fixed structurally. The regression runs 20 x (ls + control) against a live client and demands 0 failures and a client that can still type. cmd.c now holds the command vocabulary once, shared by the headless driver and the control socket, so a script written against one works against the other — 'sl0ptty cmd "snapshot text"' against a detached session prints its screen. tests/test_session.py: 18 checks over create, split, probe-resistance, detach, survival, drive-while-detached, reattach, displacement and cleanup. 123 checks green overall.
Diffstat (limited to 'src/client.c')
-rw-r--r--src/client.c181
1 files changed, 181 insertions, 0 deletions
diff --git a/src/client.c b/src/client.c
new file mode 100644
index 0000000..f50db7b
--- /dev/null
+++ b/src/client.c
@@ -0,0 +1,181 @@
+/* The client: a terminal in raw mode and a socket. It decodes nothing and
+ * lays out nothing — it forwards bytes and paints what it is sent, so a bug
+ * here can cost you a frame, never a session. */
+#define _GNU_SOURCE
+#include "server.h"
+
+#include <errno.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <termios.h>
+#include <unistd.h>
+
+#include "proto.h"
+
+#define TERM_ENTER \
+ "\x1b[?1049h\x1b[0m\x1b[2J" \
+ "\x1b[?1002h\x1b[?1006h" \
+ "\x1b[?1004h" \
+ "\x1b[?2004h" \
+ "\x1b[>1u"
+#define TERM_LEAVE \
+ "\x1b[<u\x1b[?2004l\x1b[?1004l\x1b[?1006l\x1b[?1002l" \
+ "\x1b[?25h\x1b[0m\x1b[?1049l"
+
+static volatile sig_atomic_t g_winch = 0, g_quit = 0;
+static struct termios g_saved;
+static bool g_saved_ok = false;
+
+static void on_sig(int s) {
+ if (s == SIGWINCH) g_winch = 1;
+ else g_quit = 1;
+}
+
+static void restore(void) {
+ if (g_saved_ok) tcsetattr(STDIN_FILENO, TCSAFLUSH, &g_saved);
+ ssize_t r = write(STDOUT_FILENO, TERM_LEAVE, strlen(TERM_LEAVE));
+ (void)r;
+}
+
+static void term_size(uint16_t *cols, uint16_t *rows) {
+ struct winsize ws;
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_col && ws.ws_row) {
+ *cols = ws.ws_col;
+ *rows = ws.ws_row;
+ } else {
+ *cols = 80;
+ *rows = 24;
+ }
+}
+
+static void send_size(int fd, uint8_t type, uint16_t cols, uint16_t rows) {
+ uint8_t b[4] = {(uint8_t)(cols >> 8), (uint8_t)cols, (uint8_t)(rows >> 8),
+ (uint8_t)rows};
+ msg_send(fd, type, b, sizeof b);
+}
+
+static void write_all(int fd, const void *buf, size_t len) {
+ size_t off = 0;
+ while (off < len) {
+ ssize_t w = write(fd, (const char *)buf + off, len - off);
+ if (w <= 0) {
+ if (errno == EINTR) continue;
+ return;
+ }
+ off += (size_t)w;
+ }
+}
+
+int client_run(int fd) {
+ uint16_t cols, rows;
+ term_size(&cols, &rows);
+
+ if (tcgetattr(STDIN_FILENO, &g_saved) != 0) {
+ fprintf(stderr, "sl0ptty: not a terminal\n");
+ return 1;
+ }
+ g_saved_ok = true;
+ struct termios t = g_saved;
+ cfmakeraw(&t);
+ t.c_cc[VMIN] = 1;
+ t.c_cc[VTIME] = 0;
+ tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
+ atexit(restore);
+ write_all(STDOUT_FILENO, TERM_ENTER, strlen(TERM_ENTER));
+
+ struct sigaction sa = {.sa_handler = on_sig};
+ sigaction(SIGWINCH, &sa, NULL);
+ sigaction(SIGTERM, &sa, NULL);
+ sigaction(SIGHUP, &sa, NULL);
+ signal(SIGPIPE, SIG_IGN);
+
+ send_size(fd, MSG_HELLO, cols, rows);
+
+ msg_reader_t reader;
+ msg_reader_init(&reader);
+ int exit_reason = -1;
+
+ while (!g_quit) {
+ struct pollfd pfds[2] = {
+ {.fd = STDIN_FILENO, .events = POLLIN},
+ {.fd = fd, .events = POLLIN},
+ };
+ int n = poll(pfds, 2, -1);
+
+ if (g_winch) {
+ g_winch = 0;
+ term_size(&cols, &rows);
+ send_size(fd, MSG_RESIZE, cols, rows);
+ }
+ if (n < 0) {
+ if (errno == EINTR) continue;
+ break;
+ }
+
+ if (pfds[0].revents & POLLIN) {
+ uint8_t buf[65536];
+ ssize_t got = read(STDIN_FILENO, buf, sizeof buf);
+ if (got > 0 && msg_send(fd, MSG_INPUT, buf, (size_t)got) != 0) break;
+ if (got == 0) break;
+ }
+
+ if (pfds[1].revents & (POLLIN | POLLHUP)) {
+ uint8_t buf[65536];
+ ssize_t got = read(fd, buf, sizeof buf);
+ if (got <= 0) break;
+ msg_reader_feed(&reader, buf, (size_t)got);
+ msg_t m;
+ while (msg_reader_next(&reader, &m)) {
+ if (m.type == MSG_OUTPUT) {
+ write_all(STDOUT_FILENO, m.data, m.len);
+ } else if (m.type == MSG_EXIT) {
+ exit_reason = m.len ? m.data[0] : EXIT_SESSION_ENDED;
+ g_quit = 1;
+ break;
+ }
+ }
+ }
+ }
+
+ msg_reader_free(&reader);
+ close(fd);
+ restore();
+ g_saved_ok = false;
+
+ if (exit_reason == EXIT_DETACHED) fprintf(stderr, "[detached]\n");
+ else if (exit_reason == EXIT_REPLACED) fprintf(stderr, "[replaced by another client]\n");
+ return 0;
+}
+
+/* One-shot control request: connect, ask, print the answer. */
+int client_control(int fd, const char *json) {
+ if (msg_send(fd, MSG_CMD, json, strlen(json)) != 0) return 1;
+ msg_reader_t reader;
+ msg_reader_init(&reader);
+ uint8_t buf[65536];
+ int rc = 1;
+ for (;;) {
+ ssize_t got = read(fd, buf, sizeof buf);
+ if (got <= 0) break;
+ msg_reader_feed(&reader, buf, (size_t)got);
+ msg_t m;
+ bool done = false;
+ while (msg_reader_next(&reader, &m)) {
+ if (m.type == MSG_REPLY) {
+ fwrite(m.data, 1, m.len, stdout);
+ fputc('\n', stdout);
+ rc = 0;
+ done = true;
+ break;
+ }
+ }
+ if (done) break;
+ }
+ msg_reader_free(&reader);
+ close(fd);
+ return rc;
+}