diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..5df7dd3 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,66 @@ +# moerx project instructions + +moerx is a deliberately small terminal multiplexer: one C99 source file +(`moerx.c`), no dependencies, no config file. + +## Build + +```sh +make # cc -O2 -std=c99 -Wall -Wextra -o moerx moerx.c +make clean +``` + +There is no test suite. Build must stay warning-free except for the known +pre-existing `g_spawn_err` unused-variable warning. Use a throwaway harness +outside the repo for geometry/logic checks; do not leave test files behind. + +## Conventions + +- Keep changes inside `moerx.c` where possible; the single-file design is + intentional. +- `README.md` is user-facing documentation. Update it when shortcuts, behavior, + or limitations change. +- Match the existing style: 4-space indentation, C99 declarations, `/* */` + comments. +- Do not remove existing user changes in the working tree; check `git diff` + before editing and keep unrelated edits intact. + +## Layout model (important for navigation/resizing) + +- Windows use a binary `Layout` tree: `L_COLS` (left/right split) or + `L_ROWS` (top/bottom split), with `Layout` x/y/w/h plus a `Pane` leaf. +- `layout_apply()` assigns pane content rectangles. Each pane has a title row + above its content, so `pane->y = layout->y + 1`. +- Splits reserve one border column/row between children, so pane rectangles do + not touch; vertical stacks have a two-row visual gap including the title row. +- Pane movement must be based on rectangle edges, not centres. A pane is only + to the right if `p->x >= a->x + a->w` (and similar for the other directions). + Centre comparison wrongly classifies tall/wide panes that merely extend past + the active pane. + +## Current work (2026-10-30) + +Reworked `select_pane_dir()` in `moerx.c` so `Ctrl-A h/j/k/l` and the arrow +keys choose the pane exactly left/down/up/right: + +1. Only panes strictly beyond the active pane's edge in the requested direction + are candidates. +2. Prefer candidates that overlap on the perpendicular axis. +3. Then choose the smallest edge distance in the direction of travel. +4. Break ties by perpendicular gap, then centre alignment. + +Verified with a temporary randomized split-layout simulation: the old +centre-based algorithm chose a pane outside the requested direction in many +cases; the new one never did and always preferred an overlapping candidate when +one existed. `make` succeeds with only the pre-existing warning above. + +### Terminal resize robustness + +The client now reconciles its size with `TIOCGWINSZ` every `SIZE_CHECK_MS` +(250 ms) instead of only reacting to `SIGWINCH`. Some terminals update the pty +size without delivering a signal, or deliver it before the new size is +visible; the periodic check corrects both. Keep the SIGWINCH self-pipe and flag +as the low-latency path. Note that the session is intentionally sized to the +smallest attached client, so a larger client still shows the session centred +with a border; use `Ctrl-A *` to check for extra attached clients when +diagnosing "it did not grow" reports. diff --git a/README.md b/README.md index f96663a..d0a5de5 100644 --- a/README.md +++ b/README.md @@ -107,4 +107,4 @@ This is "very basic" by design: ## Build notes Requires a C99 compiler and POSIX. Tested on macOS with clang; the code also -builds with gcc on Linux. +builds with gcc on Linux and with clang on FreeBSD. diff --git a/moerx b/moerx index eda594b..0c5faf4 100755 Binary files a/moerx and b/moerx differ diff --git a/moerx.c b/moerx.c index a724e1b..78eff3a 100644 --- a/moerx.c +++ b/moerx.c @@ -1,7 +1,19 @@ /* Feature macros must come before any system header. */ +#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || \ + defined(__OpenBSD__) +/* + * The BSDs expose flock(), usleep(), cfmakeraw() and SIGWINCH only when + * __BSD_VISIBLE is set, which defining _XOPEN_SOURCE would disable. Their + * default environment already includes everything we use. + */ +#else #ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE 700 #endif +#ifndef _DEFAULT_SOURCE +#define _DEFAULT_SOURCE +#endif +#endif #ifndef _DARWIN_C_SOURCE #define _DARWIN_C_SOURCE #endif @@ -14,7 +26,7 @@ * moerx [session] attach to session (default "0"), or start it * moerx -h|--help show usage and all Ctrl-A shortcut chains * - * Pure C99 + POSIX (macOS / Linux). + * Pure C99 + POSIX (macOS / Linux / FreeBSD). */ #include @@ -87,6 +99,7 @@ static int send_msg(int fd, uint32_t type, const void *data, uint32_t len); #define MIN_ROWS 5 #define MSG_TIME 3000 #define STATUS_INTERVAL 2 +#define SIZE_CHECK_MS 250 /* ------------------------------------------------------------------ */ /* small helpers */ @@ -2201,6 +2214,14 @@ static int rect_overlap(int a0, int a1, int b0, int b1) { return e - s; } +/* Gap between two intervals: 0 when they overlap, otherwise the distance + * between the closest edges. */ +static int rect_gap(int a0, int a1, int b0, int b1) { + if (b0 >= a1) return b0 - a1; + if (b1 <= a0) return a0 - b1; + return 0; +} + static void select_pane_dir(Session *s, int dx, int dy) { Window *w = s->cur; if (!w || !w->active) return; @@ -2208,33 +2229,80 @@ static void select_pane_dir(Session *s, int dx, int dy) { Pane *list[128]; int n = 0; layout_collect(w->root, list, &n); + Pane *best = NULL; - long bestscore = 0; + long best_primary = 0, best_gap = 0, best_center = 0; + int best_overlap = 0; + for (int i = 0; i < n; i++) { Pane *p = list[i]; if (p == a) continue; - int acx = a->x * 2 + a->w; - int acy = a->y * 2 + a->h; - int pcx = p->x * 2 + p->w; - int pcy = p->y * 2 + p->h; - int ddx = pcx - acx; - int ddy = pcy - acy; - int ok = 0; - if (dx < 0) ok = ddx < 0; - else if (dx > 0) ok = ddx > 0; - else if (dy < 0) ok = ddy < 0; - else if (dy > 0) ok = ddy > 0; - if (!ok) continue; - int overlap = (dx != 0) - ? rect_overlap(a->y, a->y + a->h, p->y, p->y + p->h) - : rect_overlap(a->x, a->x + a->w, p->x, p->x + p->w); - long d = (dx != 0) ? labs((long)ddx) : labs((long)ddy); - long score = d * 1000 - overlap; - if (!best || score < bestscore) { + + int primary, gap, overlap, ac, pc; + + /* Only panes strictly beyond the active pane's edge in the wanted + * direction are candidates. Comparing centres is not enough: a + * tall pane beside us extends further down and would look like it + * is "below", while a wide pane beside us would look like it is + * "right". */ + if (dx > 0) { + if (p->x < a->x + a->w) continue; + primary = p->x - (a->x + a->w); + gap = rect_gap(a->y, a->y + a->h, p->y, p->y + p->h); + overlap = rect_overlap(a->y, a->y + a->h, p->y, p->y + p->h); + ac = a->y * 2 + a->h; + pc = p->y * 2 + p->h; + } else if (dx < 0) { + if (p->x + p->w > a->x) continue; + primary = a->x - (p->x + p->w); + gap = rect_gap(a->y, a->y + a->h, p->y, p->y + p->h); + overlap = rect_overlap(a->y, a->y + a->h, p->y, p->y + p->h); + ac = a->y * 2 + a->h; + pc = p->y * 2 + p->h; + } else if (dy > 0) { + if (p->y < a->y + a->h) continue; + primary = p->y - (a->y + a->h); + gap = rect_gap(a->x, a->x + a->w, p->x, p->x + p->w); + overlap = rect_overlap(a->x, a->x + a->w, p->x, p->x + p->w); + ac = a->x * 2 + a->w; + pc = p->x * 2 + p->w; + } else if (dy < 0) { + if (p->y + p->h > a->y) continue; + primary = a->y - (p->y + p->h); + gap = rect_gap(a->x, a->x + a->w, p->x, p->x + p->w); + overlap = rect_overlap(a->x, a->x + a->w, p->x, p->x + p->w); + ac = a->x * 2 + a->w; + pc = p->x * 2 + p->w; + } else { + continue; + } + + /* Prefer panes that overlap the active pane on the perpendicular + * axis, then the closest edge in the wanted direction, then the + * smallest perpendicular gap and the best centre alignment. */ + int aligned = overlap > 0; + long center = labs((long)ac - (long)pc); + int better; + if (!best) { + better = 1; + } else if (aligned != best_overlap) { + better = aligned > best_overlap; + } else if (primary != best_primary) { + better = primary < best_primary; + } else if (gap != best_gap) { + better = gap < best_gap; + } else { + better = center < best_center; + } + if (better) { best = p; - bestscore = score; + best_overlap = aligned; + best_primary = primary; + best_gap = gap; + best_center = center; } } + if (best) { w->active = best; invalidate_clients(s); @@ -3032,6 +3100,21 @@ static void server_cleanup(Session *s) { unlink(s->sock_path); } +static char g_spawn_err[200]; + +/* Tell the spawning client why the server could not come up. */ +static void server_fail(int readyfd, const char *stage, int err) { + if (readyfd < 0) return; + char buf[200]; + int n = snprintf(buf, sizeof buf, "%s: %s", stage, strerror(err)); + if (n > 0) { + if ((size_t)n >= sizeof buf) n = (int)sizeof buf - 1; + ssize_t w = write(readyfd, buf, (size_t)n); + (void)w; + } + close(readyfd); +} + static void server_start(const char *path, const char *name, int readyfd) { signal(SIGPIPE, SIG_IGN); signal(SIGHUP, SIG_IGN); @@ -3047,7 +3130,7 @@ static void server_start(const char *path, const char *name, int readyfd) { int lfd = socket(AF_UNIX, SOCK_STREAM, 0); if (lfd < 0) { - if (readyfd >= 0) close(readyfd); + server_fail(readyfd, "socket", errno); _exit(1); } struct sockaddr_un saun; @@ -3055,10 +3138,16 @@ static void server_start(const char *path, const char *name, int readyfd) { saun.sun_family = AF_UNIX; snprintf(saun.sun_path, sizeof saun.sun_path, "%s", path); unlink(path); - if (bind(lfd, (struct sockaddr *)&saun, sizeof saun) < 0 || - listen(lfd, 16) < 0) { + if (bind(lfd, (struct sockaddr *)&saun, sizeof saun) < 0) { + int err = errno; close(lfd); - if (readyfd >= 0) close(readyfd); + server_fail(readyfd, "bind", err); + _exit(1); + } + if (listen(lfd, 16) < 0) { + int err = errno; + close(lfd); + server_fail(readyfd, "listen", err); _exit(1); } chmod(path, 0600); @@ -3328,11 +3417,21 @@ static int run_client(int fd, const char *name) { uint32_t attach[3] = { (uint32_t)cols, (uint32_t)rows, (uint32_t)terminal_truecolor() }; if (send_msg(fd, C_ATTACH, attach, sizeof attach) < 0) { + /* errno is the only clue: the server accepted the connection but the + attach write failed, usually because it closed the socket while + shutting down. Save it before close()/tcsetattr() clobber it. */ + int err = errno; if (g_winch_pipe[0] >= 0) close(g_winch_pipe[0]); if (g_winch_pipe[1] >= 0) close(g_winch_pipe[1]); g_winch_pipe[0] = g_winch_pipe[1] = -1; tty_restore(&saved); - fprintf(stderr, "moerx: cannot attach\n"); + if (err == EPIPE || err == ECONNRESET) { + fprintf(stderr, "moerx: cannot attach to session '%s': server closed " + "the connection (%s)\n", name, strerror(err)); + } else { + fprintf(stderr, "moerx: cannot attach to session '%s': %s\n", + name, strerror(err)); + } return 1; } @@ -3341,6 +3440,7 @@ static int run_client(int fd, const char *name) { char exitmsg[256]; exitmsg[0] = '\0'; int done = 0; + long long last_size_check = now_ms(); while (!done) { /* Drain the self-pipe before blocking. A SIGWINCH that arrived while @@ -3350,11 +3450,23 @@ static int run_client(int fd, const char *name) { char junk[64]; while (read(g_winch_pipe[0], junk, sizeof junk) > 0) g_winch = 1; } - if (g_winch) { + + /* Do not rely on SIGWINCH alone: some terminals have been seen to + update the pty size without delivering a signal, or to deliver it + before the size is visible. Reconcile against TIOCGWINSZ + periodically so a missed event cannot leave us at the old size. */ + long long t = now_ms(); + if (g_winch || t - last_size_check >= SIZE_CHECK_MS) { g_winch = 0; - tty_size(&cols, &rows); - uint32_t sz[2] = { (uint32_t)cols, (uint32_t)rows }; - send_msg(fd, C_RESIZE, sz, sizeof sz); + last_size_check = t; + int ncols, nrows; + tty_size(&ncols, &nrows); + if (ncols != cols || nrows != rows) { + cols = ncols; + rows = nrows; + uint32_t sz[2] = { (uint32_t)cols, (uint32_t)rows }; + if (send_msg(fd, C_RESIZE, sz, sizeof sz) < 0) break; + } } struct pollfd pfds[3]; @@ -3368,7 +3480,7 @@ static int run_client(int fd, const char *name) { pfds[2].events = POLLIN; nfds = 3; } - int pr = poll(pfds, (nfds_t)nfds, -1); + int pr = poll(pfds, (nfds_t)nfds, SIZE_CHECK_MS); if (pr < 0) { if (errno == EINTR) { if (g_client_quit) break;