diff options
| author | Peter Geissler <peter@haxx.in> | 2026-08-28 00:11:24 +0200 |
|---|---|---|
| committer | Peter Geissler <peter@haxx.in> | 2026-08-28 00:23:19 +0200 |
| commit | d1b5a64c3855b07074b3c958739df4e16f6084a5 (patch) | |
| tree | 10579e84bd972661738f93fdd8c221674f09b857 /src/app_layout.c | |
| parent | macos zip: carry the same paperwork as the linux tarballs (diff) | |
| download | slosh-d1b5a64c3855b07074b3c958739df4e16f6084a5.tar.gz slosh-d1b5a64c3855b07074b3c958739df4e16f6084a5.tar.xz slosh-d1b5a64c3855b07074b3c958739df4e16f6084a5.zip | |
windows: a native port, on ConPTY and AF_UNIX
slosh builds and runs on Windows 10/11, x86-64 and ARM64, as one exe
with no runtime dependency beyond the OS: panes, splits, tabs,
detachable sessions, the config file and its live reload, the control
socket, the mouse.
The port is a compatibility layer rather than a fork. compat/win holds
stub <poll.h>, <sys/socket.h>, <termios.h> and friends that redirect to
include/compat_win.h, which is force-included ahead of every translation
unit -- so the existing sources keep their POSIX includes and their
POSIX call sites, and the platform differences are answered in one
place.
Two decisions carry most of it.
Everything the event loop waits on is a socket. On POSIX a pty master,
an inotify descriptor and a unix socket are all file descriptors, so one
poll() covers them; Windows has no such union, and WSAPoll waits on
SOCKETs and nothing else. Rather than rewrite server.c's loop around
WaitForMultipleObjects -- which caps at 64 objects and cannot express
POLLOUT -- each non-socket source gets a pump thread that copies it into
one end of a loopback socketpair. The loop is then the POSIX one
unchanged, and only what sits behind the descriptor differs.
Socket descriptors are tagged rather than guessed: a wrapped SOCKET
comes back as SL_SOCK_BASE + slot, so read, write, close and poll can
tell a socket from a CRT descriptor with certainty instead of inferring
it from the numeric range. That is why socket() and accept() are shimmed
too -- an untagged descriptor escaping into the loop is the one failure
mode this scheme has, and it is worth closing completely.
Three things bit hard enough to be worth naming.
A pseudoconsole does not stop the child inheriting your stdio.
CreateProcess copies the parent's standard handles into the child when
STARTF_USESTDHANDLES is absent, and those win. With slosh's own stdout
redirected -- a pipe under ssh, a file under --script, a log under the
daemon -- the shell wrote its banner into slosh's output instead of into
the pane, and the pane had a correct title (ConPTY emits that itself)
over an empty body. The three handles are cleared for the duration of
the call, leaving the child nothing to inherit.
A ConPTY never reports EOF. The output pipe belongs to the
pseudoconsole, not to the child, so it stays open after the child exits
and pane_pump()'s read() -- the entire mechanism by which a pane learns
its program is gone -- never returns 0. A pane whose shell had been told
`exit` sat there looking alive forever. So the exit is waited for on the
process handle, and ClosePseudoConsole is what then produces the EOF.
That third thread is also why the per-pty state is reference counted: a
pump blocked on a dead pipe must not be waited for, or pty_close would
hang the session on the one program that misbehaves, so whoever finishes
last frees.
cmd.exe does not use CommandLineToArgvW's quoting. A literal quote is
\" to an ordinary program and a backslash-then-end-of-quoted-section to
cmd, so building `cmd /c notepad "C:\...\config.kdl"` the first way
handed the editor \"C:\...\config.kdl\", backslashes and all, and
notepad answered "Not a valid file name" about a path that existed.
cmd /c now gets the rest of the line verbatim -- the caller composed a
complete command with its own quoting, exactly as it does for sh -c.
Paths are mixed, and only some programs mind. slosh joins with '/' while
the environment supplies '\', so the config landed at
C:\Users\you/.config/slosh/config.kdl. The CRT opens that without
complaint, which is why --check reported it fine, but a GUI file dialog
rejects it outright. Anything handed to another program now goes through
sl_path_native() first; the internal representation is left alone.
The rest is small. Panes run %ComSpec%, sessions live in
%LOCALAPPDATA%\slosh rather than $XDG_RUNTIME_DIR because a session
socket must not follow a roaming profile to another host, there is no
SIGWINCH so a resize is noticed by comparing the console size on a
bounded poll, no fork so the daemon is a fresh detached process that
reconstructs its arguments on the command line, and no process group on
a pty so the foreground program is the youngest descendant of the pane's
shell. C-a e prefers whichever console editor is actually installed --
notepad is a GUI program, so a pane running it draws nothing and looks
broken -- and says so when it has to fall back.
make -f Makefile.windows ARCH=x86_64 (or aarch64) cross-compiles from a
mac or Linux, and builds natively on the box with zig on PATH.
Diffstat (limited to 'src/app_layout.c')
| -rw-r--r-- | src/app_layout.c | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/app_layout.c b/src/app_layout.c index 8ba4a3f..59a00d7 100644 --- a/src/app_layout.c +++ b/src/app_layout.c @@ -1507,8 +1507,39 @@ bool app_edit_config(app_t *a) { if (!editor || !*editor) editor = "vi"; char cmd[1024]; +#ifdef _WIN32 + /* Windows has no editor it is *required* to have, and the obvious fallback + * is the wrong shape: notepad is a GUI program, so a pane running it draws + * nothing and looks broken. Prefer whichever console editor is actually + * installed, and only then fall back to opening a window. + * + * `editor` here is still whatever EDITOR or the config asked for; the search + * only runs when nothing was asked for, or when the POSIX default of `vi` + * came through and would not exist. */ + bool windowed = false; + if (!editor || !*editor || strcmp(editor, "vi") == 0) { + static const char *const consoles[] = {"nvim", "vim", "vi", "nano", + "micro", "hx", NULL}; + editor = NULL; + for (int i = 0; consoles[i] && !editor; i++) + if (sl_which(consoles[i])) editor = consoles[i]; + if (!editor) { + editor = "notepad"; + windowed = true; + } + } + /* cmd.exe does not treat single quotes as quoting, and the path is handed + * over in Windows spelling -- slosh joins with '/', $HOME arrives with '\\', + * and a GUI editor rejects the mixture outright. */ + char native[PATH_MAX]; + snprintf(cmd, sizeof cmd, "%s \"%s\"", editor, + sl_path_native(path, native, sizeof native)); + if (windowed) + app_toast(a, "no console editor found: opened the config in notepad"); +#else snprintf(cmd, sizeof cmd, "%s '%s'", editor, path); - const char *argv[] = {"/bin/sh", "-c", cmd, NULL}; +#endif + const char *argv[] = {SLOSH_SHELL_DEFAULT, SLOSH_SHELL_CFLAG, cmd, NULL}; /* Down rather than across: a config file is lines, and half the width of a * pane is a worse place to read them than half the height. */ |
