diff options
Diffstat (limited to 'dev/gui/gui.zig')
| -rw-r--r-- | dev/gui/gui.zig | 598 |
1 files changed, 52 insertions, 546 deletions
diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 9695bf83..ab33abe9 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -370,17 +370,6 @@ fn emitTouchClick(g: *const Gui, in: *Input, point: TouchNormPoint, button: pard in.post(.{ .mouse = .{ .button = button, .kind = .release, .col = cell.col, .row = cell.row, .tag_hit = cell.tag_hit, .body_hit = cell.body_hit } }); } -const Pty = struct { - fd: c_int, - pid: libc.pid_t, - serial: u32, - /// Its reader, blocked in an Io read: `closePty` cancels it. - reader: ?std.Io.Future(void) = null, - /// A command pane's child, watched to its exit (host_io.watchExit); - /// the pty stays open until both its end of file and its exit. - cmd: host_io.CommandWatch = .{}, -}; - const Queue = host_io.Inbox; /// Wakes a blocking SDL_WaitEventTimeout from another thread: a push to @@ -393,32 +382,6 @@ fn sdlWake(_: ?*anyopaque) void { const PipeTasks = selection_pipe.Tasks; const output_capacity = Queue.output_capacity; -fn stopPipe() ![2]c_int { - var fds: [2]c_int = undefined; - if (libc.pipe(&fds) != 0) return error.PipeFailed; - errdefer for (fds) |fd| { - _ = libc.close(fd); - }; - for (fds) |fd| if (libc.fcntl(fd, libc.F.SETFD, @as(c_int, 1)) < 0) return error.PipeFailed; - return fds; -} - -fn watchThread(fd: c_int, stop: c_int, q: *Queue) void { - while (true) { - var fds = [_]libc.pollfd{ - .{ .fd = stop, .events = libc.POLL.IN, .revents = 0 }, - .{ .fd = fd, .events = libc.POLL.IN, .revents = 0 }, - }; - if (libc.poll(&fds, fds.len, -1) < 0) { - if (libc.errno(-1) == .INTR) continue; - return; - } - if (fds[0].revents != 0) return; - if ((fds[1].revents & (libc.POLL.ERR | libc.POLL.HUP | libc.POLL.NVAL)) != 0) return; - if (fds[1].revents != 0 and file_watch.drain(fd)) q.push(.files_changed); - } -} - const Gui = struct { addon_gpu: ?SketchGpu = null, addon_hash: [pardes.MAX_PANES]u64 = @splat(0), @@ -1001,8 +964,9 @@ fn closeWindow(g: *Gui) void { }; } -/// `pardes-gui --attach`: a window again, as the first was made, at the -/// size the last one had. False when the display will not give one. +/// A window again, as the first was made, at the size the last one had +/// (a transparent one: syncWindowOpacity). False when the display will not +/// give one. fn reopenWindow(g: *Gui) bool { const window = c.SDL_CreateWindow("pardes", g.window_w, g.window_h, g.win_flags) orelse return false; if (comptime builtin.os.tag == .macos) c.ui_macos_trackpad_attach(window); @@ -1027,14 +991,7 @@ const Own = struct { g: *Gui, test_mode: bool, detached: bool, - ptys: [pardes.MAX_PANES]?Pty = @splat(null), - gens: [pardes.MAX_PANES]u32 = @splat(0), - queue: Queue = undefined, pipe_tasks: PipeTasks = .{}, - inotify_fd: c_int = -1, - watch_reader: ?std.Thread = null, - watch_stop: [2]c_int = .{ -1, -1 }, - watches: file_watch.Table = @splat(null), shell: Shell = undefined, const Session = session_loop.Loop(Own); @@ -1058,26 +1015,17 @@ const Own = struct { core.view().frontend.native_image_patches = true; observeGuiFont(g, core); syncTaglineFont(g, core); - o.queue = .{ - .io = l.io, - .gpa = l.gpa, - .lsp_allocator = l.allocs.lsp, - .wake_fn = sdlWake, - }; - o.inotify_fd = file_watch.init(true); + l.inbox.wake_fn = sdlWake; o.shell = .{ .core = core, .gui = g, .io = l.io, .gpa = l.gpa, .lsp_allocator = l.allocs.lsp, - .prompt_rcs = &l.prompt_rcs, - .ptys = &o.ptys, - .gens = &o.gens, - .queue = &o.queue, + .ptys = &l.ptys, + .queue = &l.inbox, .pipe_tasks = &o.pipe_tasks, - .inotify_fd = o.inotify_fd, - .watches = &o.watches, + .watches = &l.watches, .fs = l.fs, .test_mode = o.test_mode, .test_clock = if (o.test_mode) host_io.testClock() else null, @@ -1100,43 +1048,16 @@ const Own = struct { const io = l.io; o.shell.fs = null; host_io.setExitWake(null, null); - pardes.lsp.setStatusSink(null, null); pardes.Kind.drawer = null; - o.shell.shutdownPtys(); o.shell.job_tasks.cancelAll(io); - if (o.inotify_fd >= 0) { - if (o.watch_reader) |thread| { - _ = host_io.writeFd(o.watch_stop[1], "x"); - thread.join(); - } - for (o.watch_stop) |fd| if (fd >= 0) { - _ = libc.close(fd); - }; - file_watch.stop(o.inotify_fd); - _ = libc.close(o.inotify_fd); - o.inotify_fd = -1; - } o.pipe_tasks.cancelAll(io); - if (o.queue.lsp_task) |*task| task.future.cancel(io); // Its worker may be waiting on the queue for room. l.core.stream.cancelTask(); - o.queue.deinit(); o.g.hit_core = null; } - pub fn resumePtys(o: *Own, rec: *const upgrade.Record) void { - o.shell.resumePtys(rec); - } - pub fn start(o: *Own, l: *Session) !void { - for (&o.ptys, 0..) |*slot, id| if (slot.*) |*pt| { - pt.reader = try o.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, .at(id), o.gens[id]); - }; - if (o.inotify_fd >= 0) { - o.watch_stop = try stopPipe(); - o.watch_reader = try std.Thread.spawn(.{}, watchThread, .{ o.inotify_fd, o.watch_stop[0], &o.queue }); - } - if (l.fs) |f| try f.watch(&o.queue, wakeFs); + if (l.fs) |f| try f.watch(&l.inbox, wakeFs); _ = c.SDL_StartTextInput(o.g.window); // An upgraded session's window, its view's again. @@ -1144,8 +1065,7 @@ const Own = struct { if (o.test_mode) setStdinRaw() catch {}; o.shell.threads_ok = true; - pardes.lsp.setStatusSink(&o.queue, Queue.lspStatus); - host_io.setExitWake(&o.queue, Queue.wakeForExit); + host_io.setExitWake(&l.inbox, Queue.wakeForExit); if (o.detached) { o.shell.closeTheWindow(); // Nothing else may wake the first wait. @@ -1163,16 +1083,10 @@ const Own = struct { deliverNote(&o.shell, n); } - pub fn watched(o: *Own) session_loop.Watched { - return .{ .fd = o.inotify_fd, .table = &o.watches }; - } - pub fn restoring(o: *Own, nc: *pardes.Pardes) void { o.pipe_tasks.cancelAll(o.shell.io); - o.queue.discardCompletions(); // One view, the window's (host_io.restoreCore). o.shell.gview = 0; - o.shell.stopPtys(); o.g.cells.clearImages(); o.g.prepared_images.clearRetainingCapacity(); nc.view().frontend.native_images = true; @@ -1498,13 +1412,11 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ window.loop = &loop; // A client spawns, watches and serves nothing: the Shell's tables are // empty ones, there for what draws. - var ptys: [pardes.MAX_PANES]?Pty = @splat(null); - var gens: [pardes.MAX_PANES]u32 = @splat(0); var queue: Queue = .{ .io = io, .gpa = gpa, .lsp_allocator = start.allocs.lsp, .wake_fn = sdlWake }; defer queue.deinit(); + var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = undefined }; var pipe_tasks: PipeTasks = .{}; - var watches: file_watch.Table = @splat(null); - const prompt_rcs: host_io.Shell.PromptFiles = .{}; + var no_watches: file_watch.Watches = .{ .io = io, .gpa = gpa, .queue = &queue }; var no_peers: replica.Peers = .{}; var shell: Shell = .{ .core = start.r.core(), @@ -1512,13 +1424,10 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ .io = io, .gpa = gpa, .lsp_allocator = start.allocs.lsp, - .prompt_rcs = &prompt_rcs, .ptys = &ptys, - .gens = &gens, .queue = &queue, .pipe_tasks = &pipe_tasks, - .inotify_fd = -1, - .watches = &watches, + .watches = &no_watches, .test_mode = test_mode, .client = &window, .peers = &no_peers, @@ -2430,27 +2339,23 @@ fn pollGamepad(g: *Gui, in: *Input) void { const Shell = struct { core: *pardes.Pardes, - /// The window's, while one is open; null while detached (`detached` has - /// it). + /// The window's, while one is open; null once detached. gui: ?*Gui = null, - detached: ?*Gui = null, - /// Shells a dead session left (Adopt), each waiting for the spawn of - /// the terminal pane made for it. - adoptions: host_io.Adoptions = .{}, io: std.Io, gpa: std.mem.Allocator, fs: ?*ninep_io.Listener = null, lsp_allocator: std.mem.Allocator, - prompt_rcs: *const host_io.Shell.PromptFiles, - ptys: *[pardes.MAX_PANES]?Pty, - gens: *[pardes.MAX_PANES]u32, + /// The session's ptys (runtime/session.zig's), reading into `queue`; + /// a client's, an empty table. + ptys: *host_io.Ptys, queue: *Queue, /// What drainQueue takes each wakeup, kept rather than rebuilt. batch: Queue.Batch = .{}, pipe_tasks: *PipeTasks, job_tasks: PipeTasks = .{}, - inotify_fd: c_int, - watches: *file_watch.Table, + /// The session's file watches (runtime/session.zig's); a client's, + /// none. + watches: *file_watch.Watches, threads_ok: bool = false, test_mode: bool = false, feed: StdinFeed = .{}, @@ -2485,50 +2390,12 @@ const Shell = struct { took_input: bool = false, held: ?c.SDL_Event = null, - fn closePaneShell(ctx: ?*anyopaque, id: pardes.Pane.Id) void { - const pane = id.index(); - shellOf(ctx).closePty(pane); - } - - fn closePty(s: *Shell, pane: u8) void { - const pt = if (s.ptys[pane]) |*pt| pt else return; - // Out of the queue first, which lets go a reader waiting for room - // in it; then its read is canceled (or, at its end, it is joined). - s.queue.cancelReader(.at(pane), s.gens[pane]); - if (pt.reader) |*reader| { - reader.cancel(s.queue.io); // the Io it was started on (startReader) - pt.reader = null; - } - _ = libc.close(pt.fd); - daemon.closed(pt.pid); - host_io.retireShell(pt.pid); - s.ptys[pane] = null; - } - - fn stopPtys(s: *Shell) void { - for (0..s.ptys.len) |pane| s.closePty(@intCast(pane)); - for (s.gens) |*gen| gen.* +%= 1; - } - - fn shutdownPtys(s: *Shell) void { - s.stopPtys(); - host_io.waitRetired(); - } - - fn reconcilePtys(s: *Shell) void { - for (s.ptys, 0..) |slot, pane| if (slot) |pt| { - const current = s.core.panes[pane]; - if (current == null or current.?.serial != pt.serial or !current.?.isTerminal()) - s.closePty(@intCast(pane)); - }; - } - fn host(s: *Shell) pardes.Host { return .{ .ctx = s, .vtable = &vtable }; } /// Detach: the window closed, the session left running with no - /// window, for `pardes-gui --attach` to open one again. + /// window, detached for good: an attach shows it as a client. fn detachWindow(ctx: ?*anyopaque) void { const s = shellOf(ctx); // A replica client's: its link ends. @@ -2575,41 +2442,19 @@ const Shell = struct { fn closeTheWindow(s: *Shell) void { const g = s.gui orelse return; closeWindow(g); - s.detached = g; s.gui = null; s.presented = false; s.surface = null; // Its clients look at it still (Peers keeps it attended). s.peers.detached = true; s.core.unattended = s.peers.count() == 0; - daemon.setState(true); - } - - fn openTheWindow(s: *Shell) void { - const g = s.detached orelse return; - if (!reopenWindow(g)) { - log.err("--attach: no window: {s}", .{c.SDL_GetError()}); - return daemon.setState(true); - } - s.gui = g; - s.detached = null; - s.peers.detached = false; - s.core.unattended = false; - s.core.needs_frame = true; - resetScroll(g); - daemon.setState(false); + daemon.setDetached(); } fn watched(ctx: ?*anyopaque) bool { return shellOf(ctx).gui != null; } - fn adopt(ctx: ?*anyopaque, id: pardes.Pane.Id) void { - const pane = id.index(); - const s = shellOf(ctx); - s.adoptions.ask(s.core, pane); - } - fn upgradeAsked(ctx: ?*anyopaque, id: pardes.Pane.Id) void { const pane = id.index(); const s = shellOf(ctx); @@ -2644,39 +2489,21 @@ const Shell = struct { // the readers stopped (a reader waiting for room has it then), and // what they pushed meanwhile drained. s.drainQueue(); - for (s.ptys) |*slot| if (slot.*) |*pt| if (pt.reader) |*r| { - r.cancel(s.queue.io); - pt.reader = null; - }; + s.ptys.pause(); s.drainQueue(); core.settle(); // Its replica clients told, their views gone before the dump: each // dials the session again once it is back. s.peers.refuseAll(core, "the session is upgrading to another build of pardes"); var rec: upgrade.Record = .{ .link = daemon.link, .window = s.gui != null, .exe = path }; - for (s.ptys, 0..) |slot, id| if (slot) |pt| if (pt.fd >= 0) rec.addPty(id, pt.pid, pt.fd); + s.ptys.record(&rec); var at_buf: [1024:0]u8 = undefined; const failed = upgrade.dumpInto(core, &rec, &at_buf, &why) orelse upgrade.execAs(s.gpa, path, &rec, &at_buf, s.fs, &why); // As it was: its ptys read on. - for (s.ptys, 0..) |*slot, id| if (slot.*) |*pt| if (pt.fd >= 0 and pt.reader == null) { - pt.reader = s.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, .at(id), s.gens[id]) catch null; - }; + s.ptys.readOn(core); core.reportFailure(pane, failed); } - /// An upgraded session's shells, running on: each its pane's pty again, - /// or, when its dump would not load (`fresh`), adopted as new panes. - fn resumePtys(s: *Shell, rec: *const upgrade.Record) void { - for (rec.ptys[0..rec.nptys]) |k| { - const pane = if (rec.fresh) null else s.core.panes[k.pane]; - if (pane == null or !pane.?.isTerminal()) { - s.adoptions.adoptShell(s.core, k.pid, k.fd); - continue; - } - s.ptys[k.pane] = .{ .fd = k.fd, .pid = k.pid, .serial = pane.?.serial, .cmd = .{ .watched = pane.?.command != null and host_io.watchExit(k.pid), .fd = k.fd } }; - } - } - /// An upgraded session's window, its view's again; every other view /// but a replica client's, kept for its rejoin (views.away), closes. fn resumeWindow(s: *Shell) void { @@ -2695,20 +2522,20 @@ const Shell = struct { .poll_frame = pollFrame, .watched = watched, .detach = detachWindow, - .adopt = adopt, + .adopt = PtyCalls.adopt, .upgrade = upgradeAsked, - .spawn = spawnPane, - .pty_write = ptyWrite, - .pty_resize = ptyResize, - .pty_signal = ptySignal, - .close_pty = closePaneShell, - .tty_taken = ttyTaken, - .fg_name = fgName, - .kill_job = killJob, - .write_file = writeFile, + .spawn = PtyCalls.spawn, + .pty_write = PtyCalls.write, + .pty_resize = PtyCalls.resize, + .pty_signal = PtyCalls.signal, + .close_pty = PtyCalls.close, + .tty_taken = PtyCalls.ttyTaken, + .fg_name = PtyCalls.fgName, + .kill_job = PtyCalls.killJob, + .write_file = WatchCalls.writeFile, .write_dump = writeDump, - .watch_file = watchFile, - .watch_theme = watchTheme, + .watch_file = WatchCalls.watchFile, + .watch_theme = WatchCalls.watchTheme, .dump_themes = dumpThemes, .set_clipboard = setClipboard, .read_clipboard = readClipboard, @@ -2721,13 +2548,12 @@ const Shell = struct { .job = kindJob, }; + const PtyCalls = host_io.Ptys.Calls(Shell); + const WatchCalls = file_watch.Watches.Calls(Shell); + /// The command panes' exits, told once their output is in (host_io). fn takeExits(s: *Shell) void { - host_io.takeExits(s.core, s.ptys, s, closeWatched); - } - - fn closeWatched(s: *Shell, id: usize) void { - s.closePty(@intCast(id)); + s.ptys.takeExits(s.core); } /// A client's queue holds its draws alone: each landed for its pane's @@ -2745,7 +2571,6 @@ const Shell = struct { } fn drainQueue(s: *Shell) void { - s.reconcilePtys(); // After this batch's output, which an exit is told behind. defer s.takeExits(); s.queue.takeInto(&s.batch); @@ -2753,48 +2578,9 @@ const Shell = struct { if (msgs.len > 0) s.mail_only = false; var check_files = false; for (msgs.slice()) |m| switch (m) { - .output => |o| { - if (s.gens[o.pane.index()] == o.gen) - s.core.update(.{ .output = .{ .pane = o.pane, .bytes = o.bytes } }); - s.gpa.free(o.bytes); - }, - .eof => |e| { - if (s.gens[e.pane.index()] == e.gen) eof: { - if (e.failure) |err| { - s.closePty(e.pane.index()); - s.core.readerFailed(e.pane, err); - break :eof; - } - const pt = if (s.ptys[e.pane.index()]) |*pt| pt else { - s.core.update(.{ .eof = .{ .pane = e.pane } }); - break :eof; - }; - // A command's pty stays open until its child has exited: - // closing it would hang up one that runs on without it. - // Its exit, if it came first, is told now its output is in. - if (pt.cmd.watched) { - host_io.commandEof(s.core, s.ptys, e.pane.index(), s, closeWatched); - break :eof; - } - // Unwatched, a command's exit is read here, as its end, - // and a shell's, for a run waiting on it and the log. - const unwatched = if (s.core.panes[e.pane.index()]) |pane| pane.command != null else false; - const status = host_io.exitStatus(pt.pid, 100); - if (status != null) pt.pid = 0; // reaped: no shell to retire - s.closePty(e.pane.index()); - if (unwatched or status != null) s.core.update(.{ .exited = .{ .pane = e.pane, .status = status } }); - s.core.update(.{ .eof = .{ .pane = e.pane } }); - } - }, - .lsp => |l| { - s.core.update(.{ .lsp_resp = .{ .id = l.id, .rows = l.rows } }); - if (l.rows) |rows| s.lsp_allocator.free(rows); - }, - .lsp_status => |text| { - var mbuf: [256]u8 = undefined; - s.core.setStatus(.at(s.core.view().active), message.stamp(&mbuf, "lsp", text)); - s.lsp_allocator.free(text); - }, + .output => |o| s.ptys.output(s.core, o), + .eof => |e| s.ptys.eof(s.core, e), + .lsp, .lsp_status => s.queue.landLsp(s.core, m), .pipe => |response_value| { var response = response_value; s.core.update(.{ .pipe_resp = .{ @@ -2820,11 +2606,9 @@ const Shell = struct { }, .files_changed => check_files = true, .fs_ready => {}, - .window => if (s.gui == null) s.openTheWindow(), - .adopted => |a| s.adoptions.adoptShell(s.core, a.pid, a.fd), + .adopted => |a| s.ptys.adoptions.adoptShell(s.core, a.pid, a.fd), }; - if (check_files and file_watch.reloadChanged(s.core, s.io, s.gpa, s.watches)) - s.queue.push(.files_changed); + if (check_files) s.watches.reload(s.core); } }; @@ -3026,11 +2810,10 @@ fn waitDetached(core: *pardes.Pardes, timeout_ms: u32) void { fn pollFrame(ctx: ?*anyopaque) void { const s = shellOf(ctx); - s.reconcilePtys(); const core = s.core; if (s.fs) |f| if (f.tick().pending) s.queue.push(.fs_ready); // The authority owns its terminals even with its window closed. - pollCwds(core, s.ptys); + s.ptys.pollCwds(core); const g = s.gui orelse { if (core.settings.window_opacity_pending) { var unchanged_opacity: u8 = 100; @@ -3330,68 +3113,12 @@ fn guiNow(ctx: ?*anyopaque) u64 { return s.test_clock orelse s.paced_ns orelse c.SDL_GetTicksNS(); } -fn spawnPane(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8) void { - const pane = id.index(); - const s = shellOf(ctx); - if (s.adoptions.take(id)) |a| return takeShell(s, pane, a); - // The new shell starts before a running one goes: one that cannot - // start leaves the running one be (pty/ctl's exec). - const running = s.ptys[pane] != null; - const child = host_io.forkShell(s.core, pane, s.prompt_rcs, s.core.shellBin(), cwd, s.core.view().screen_h, s.core.view().screen_w) catch |err| { - if (running) return s.core.restartFailed(id, err); - return s.core.shellFailed(id, err); - }; - s.closePty(pane); - if (s.ptys[pane] != null) { - _ = libc.close(child.file.handle); - host_io.retireShell(child.pid); - return s.core.reportError(id, "shell", error.WorkersBusy); - } - s.gens[pane] +%= 1; - const command = s.core.panes[pane].?.command != null; - const pt: Pty = .{ .fd = child.file.handle, .pid = child.pid, .serial = s.core.panes[pane].?.serial, .cmd = .{ .watched = command and host_io.watchExit(child.pid), .fd = child.file.handle } }; - s.ptys[pane] = pt; - var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.shellCwd(pt.pid, &lbuf)) |wd| s.core.setCwd(id, wd); - if (!s.threads_ok) return; - s.ptys[pane].?.reader = s.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, id, s.gens[pane]) catch |err| { - s.closePty(pane); - return s.core.reportError(id, "terminal reader", err); - }; -} - -/// An adopted shell, running already, as pane `pane`'s: told the size twice -/// (one it never had, then the pane's), which makes it redraw. -fn takeShell(s: *Shell, pane: u8, a: host_io.Adoption) void { - s.closePty(pane); - s.gens[pane] +%= 1; - s.ptys[pane] = .{ .fd = a.fd, .pid = a.pid, .serial = s.core.panes[pane].?.serial, .cmd = .{} }; - var exe_buf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - s.core.acknowledgeShell(.at(pane), host_io.processExe(a.pid, &exe_buf) orelse "sh", host_io.marksPrompts(a.pid)); - var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.shellCwd(a.pid, &lbuf)) |wd| s.core.setCwd(.at(pane), wd); - const tiny: posix.winsize = .{ .row = 1, .col = 1, .xpixel = 0, .ypixel = 0 }; - _ = posix.system.ioctl(a.fd, TIOCSWINSZ, @intFromPtr(&tiny)); - if (s.core.panes[pane]) |pn| ptyResize(s, .at(pane), @max(pn.cols, 1), @max(pn.rows, 1)); - if (!s.threads_ok) return; - s.ptys[pane].?.reader = s.queue.startReader(.{ .handle = a.fd, .flags = .{ .nonblocking = false } }, .at(pane), s.gens[pane]) catch |err| { - s.closePty(pane); - return s.core.reportError(.at(pane), "terminal reader", err); - }; -} - /// The daemon's thread: what it says, onto the queue (drainQueue). fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { const s: *Shell = @ptrCast(@alignCast(ctx)); const q = s.queue; switch (note) { - .attach => |a| { - // A terminal for a window session: the daemon refuses that kind - // before it is sent, so only an old one's would be here. - if (a.tty >= 0) _ = libc.close(a.tty); - if (a.stub >= 0) daemon.tellStub(a.stub, "r{s}", .{"a window session takes no terminal"}); - if (a.tty < 0) q.push(.window); - }, + .relinked => q.wake(), .adopted => |a| { for (a.pids[0..a.n], a.fds[0..a.n]) |pid, fd| q.push(.{ .adopted = .{ .pid = pid, .fd = fd } }); q.push(.{ .adopted = .{ .pid = 0, .fd = -1 } }); @@ -3401,78 +3128,10 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { } } -fn ptyWrite(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { - const pane = id.index(); - const s = shellOf(ctx); - if (s.ptys[pane]) |pt| _ = host_io.writeFd(pt.fd, bytes); -} - -fn ptyResize(ctx: ?*anyopaque, id: pardes.Pane.Id, cols: u16, rows: u16) void { - const pane = id.index(); - const s = shellOf(ctx); - if (s.ptys[pane]) |pt| { - const ws = host_io.ptyWinsize(s.core, cols, rows); - _ = posix.system.ioctl(pt.fd, TIOCSWINSZ, @intFromPtr(&ws)); - } -} - -fn ptySignal(ctx: ?*anyopaque, id: pardes.Pane.Id, sig: pardes.PtySignal) void { - const pane = id.index(); - const s = shellOf(ctx); - // A command whose exit is recorded is reaped: its group may be another's. - if (s.ptys[pane]) |pt| if (!pt.cmd.exited) host_io.signalTty(pt.pid, pt.fd, sig, pt.cmd.watched); -} - -fn ttyTaken(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { - const pane = id.index(); - const s = shellOf(ctx); - const pt = s.ptys[pane] orelse return false; - return host_io.ttyTaken(pt.pid, pt.fd); -} - -fn fgName(ctx: ?*anyopaque, id: pardes.Pane.Id, buf: []u8) ?[]const u8 { - const pane = id.index(); - const s = shellOf(ctx); - const pt = s.ptys[pane] orelse return null; - return host_io.foregroundName(pt.pid, pt.fd, buf); -} - -fn killJob(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { - const pane = id.index(); - const s = shellOf(ctx); - const pt = s.ptys[pane] orelse return false; - return host_io.killJob(pt.pid, pt.fd); -} - -fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, bytes: []const u8) void { - const pane = id.index(); - const s = shellOf(ctx); - host_io.writeFile(s.core, s.watches, pane, path, bytes); -} - fn writeDump(ctx: ?*anyopaque, bytes: []const u8) void { host_io.writeDump(shellOf(ctx).core, bytes); } -fn watchFile(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, on: bool, mode: pardes.WatchMode) void { - const pane = id.index(); - const s = shellOf(ctx); - if (file_watch.applyEffect(s.core, s.io, s.inotify_fd, s.watches, pane, on, mode)) - s.queue.push(.files_changed); -} - -fn watchTheme(ctx: ?*anyopaque, generation: u32, on: bool) void { - const s = shellOf(ctx); - if (file_watch.applyThemeEffect( - s.core, - s.gpa, - s.inotify_fd, - s.watches, - generation, - on, - )) s.queue.push(.files_changed); -} - fn dumpThemes(ctx: ?*anyopaque, id: pardes.Pane.Id) void { const pane = id.index(); const s = shellOf(ctx); @@ -3590,13 +3249,6 @@ fn handOver(ctx: ?*anyopaque, seq: u32, bytes: []const u8) void { }; } -fn pollCwds(core: *pardes.Pardes, ptys: *[pardes.MAX_PANES]?Pty) void { - for (ptys, 0..) |slot, id| if (slot) |pt| { - var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.ttyCwd(pt.pid, pt.fd, core.settings.shell.effective.get(), &lbuf)) |cwd| core.setCwd(.at(id), cwd); - }; -} - fn accumulateWheelDelta(pending: f32, raw_y: f32) f32 { if (!std.math.isFinite(raw_y)) return pending; const next = pending - raw_y; @@ -6276,148 +5928,6 @@ test "installed Nerd Symbols fallback covers Yazi directory icons" { try std.testing.expect(atlas.glyphForCodepoint(0xe5ff).glyph != 0); } -test "GUI PTY reader delivers a real shell tail before EOF and joins on close" { - const gpa = std.testing.allocator; - const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); - defer core.deinit(); - var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; - defer queue.deinit(); - var ptys: [pardes.MAX_PANES]?Pty = @splat(null); - var gens: [pardes.MAX_PANES]u32 = @splat(1); - var shell: Shell = undefined; - shell.core = core; - shell.gpa = gpa; - shell.ptys = &ptys; - shell.gens = &gens; - shell.queue = &queue; - defer shell.shutdownPtys(); - const rcs: host_io.Shell.PromptFiles = .{}; - const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80); - ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial }; - ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, .at(0), 1); - try std.testing.expect(host_io.writeFd(child.file.handle, "printf '\\120\\101\\122\\104\\105\\123\\055\\124\\101\\111\\114'; exit\n")); - var bytes: std.ArrayList(u8) = .empty; - defer bytes.deinit(gpa); - var eof = false; - const until = std.Io.Clock.awake.now(std.testing.io).toMilliseconds() + 2_000; - while (!eof and std.Io.Clock.awake.now(std.testing.io).toMilliseconds() < until) { - var batch = queue.take(); - defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); - for (batch.slice()) |msg| switch (msg) { - .output => |o| { - try std.testing.expect(!eof); - try bytes.appendSlice(gpa, o.bytes); - }, - .eof => |e| { - try std.testing.expect(e.failure == null); - eof = true; - }, - else => return error.UnexpectedMessage, - }; - if (!eof) try std.testing.io.sleep(.fromMilliseconds(1), .awake); - } - try std.testing.expect(eof); - try std.testing.expect(std.mem.indexOf(u8, bytes.items, "PARDES-TAIL") != null); - shell.closePty(0); - try std.testing.expect(queue.readers[0] == null); - try std.testing.expect(ptys[0] == null or ptys[0].?.reader == null); - try std.testing.expectEqual(@as(c_int, -1), libc.fcntl(child.file.handle, libc.F.GETFD)); -} - -test "GUI PTY Restore joins a real reader waiting for queue space" { - const gpa = std.testing.allocator; - var core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); - defer core.deinit(); - try dump.dumpState(core); - var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; - defer queue.deinit(); - queue.acceptReader(.at(1), 1); - for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = .at(1), .gen = 1, .bytes = try gpa.dupe(u8, "other pane") } }); - var ptys: [pardes.MAX_PANES]?Pty = @splat(null); - var gens: [pardes.MAX_PANES]u32 = @splat(1); - var shell: Shell = undefined; - shell.core = core; - shell.gpa = gpa; - shell.ptys = &ptys; - shell.gens = &gens; - shell.queue = &queue; - defer shell.shutdownPtys(); - const rcs: host_io.Shell.PromptFiles = .{}; - const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80); - ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial }; - ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, .at(0), 1); - try std.testing.expect(host_io.writeFd(child.file.handle, "printf 'after-full'; exit\n")); - try host_io.PtyTests.waitBlocked(&queue); - const old_serial = core.panes[0].?.serial; - const replacement = try dump.restore(core, core.dump_out.?, "test"); - shell.stopPtys(); - core.deinit(); - core = replacement; - shell.core = replacement; - // The dump's pane, its serial too; its old shell stopped all the same. - try std.testing.expectEqual(old_serial, core.panes[0].?.serial); - try std.testing.expectEqual(@as(usize, 0), queue.waiting); - try std.testing.expect(queue.readers[0] == null); - try std.testing.expectEqual(@as(c_int, -1), libc.fcntl(child.file.handle, libc.F.GETFD)); - queue.cancelReader(.at(1), 1); - queue.acceptReader(.at(0), gens[0]); - queue.push(.{ .eof = .{ .pane = .at(0), .gen = 1 } }); - try std.testing.expect(queue.pushOutput(.at(0), gens[0], try gpa.dupe(u8, "fresh"))); - var batch = queue.take(); - defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); - try std.testing.expectEqual(@as(usize, 1), batch.len); - try std.testing.expectEqualStrings("fresh", batch.items[0].output.bytes); -} - -test "GUI PTY deletion joins an idle reader and its shell is reaped by exit" { - const gpa = std.testing.allocator; - const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); - defer core.deinit(); - var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; - defer queue.deinit(); - var ptys: [pardes.MAX_PANES]?Pty = @splat(null); - var gens: [pardes.MAX_PANES]u32 = @splat(1); - var shell: Shell = undefined; - shell.core = core; - shell.gpa = gpa; - shell.ptys = &ptys; - shell.gens = &gens; - shell.queue = &queue; - defer shell.shutdownPtys(); - const rcs: host_io.Shell.PromptFiles = .{}; - const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80); - ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial }; - ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, .at(0), 1); - try core.removePane(0, null); - shell.reconcilePtys(); - try std.testing.expect(queue.readers[0] == null); - try std.testing.expectEqual(@as(usize, 0), queue.waiting); - try std.testing.expectEqual(@as(usize, 0), queue.take().len); - try std.testing.expectEqual(@as(c_int, -1), libc.fcntl(child.file.handle, libc.F.GETFD)); - shell.shutdownPtys(); - try std.testing.expectEqual(@as(libc.pid_t, -1), libc.waitpid(child.pid, null, libc.W.NOHANG)); - try std.testing.expectEqual(libc.E.CHILD, libc.errno(-1)); -} - -test "GUI PTY file watcher stops and joins without closing its watched descriptor" { - if (!file_watch.supported) return error.SkipZigTest; - const gpa = std.testing.allocator; - var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; - defer queue.deinit(); - const fd = file_watch.init(true); - if (fd < 0) return error.WatchInitFailed; - defer _ = libc.close(fd); - const stop = try stopPipe(); - defer for (stop) |pipe_fd| { - _ = libc.close(pipe_fd); - }; - const thread = try std.Thread.spawn(.{}, watchThread, .{ fd, stop[0], &queue }); - _ = host_io.writeFd(stop[1], "x"); - thread.join(); - try std.testing.expect(libc.fcntl(fd, libc.F.GETFD) >= 0); - try std.testing.expectEqual(@as(usize, 0), queue.take().len); -} - test "GUI completion failure survives backlog and leaves the document unchanged" { const gpa = std.testing.allocator; const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); @@ -6435,14 +5945,12 @@ test "GUI completion failure survives backlog and leaves the document unchanged" .bytes = try gpa.dupe(u8, "output"), } }); queue.push(.{ .lsp = .{ .id = id, .rows = null } }); - var gens: [pardes.MAX_PANES]u32 = @splat(1); - var ptys: [pardes.MAX_PANES]?Pty = @splat(null); + var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = undefined, .gens = @splat(1) }; var shell: Shell = undefined; shell.core = core; shell.queue = &queue; shell.gpa = gpa; shell.lsp_allocator = gpa; - shell.gens = &gens; shell.ptys = &ptys; shell.drainQueue(); try std.testing.expect(core.lsp_ask.wait == null); @@ -8165,16 +7673,14 @@ test "a detached GUI authority follows terminal directory changes with no window _ = core.settings.shell.effective.set("/bin/sh"); var queue: Queue = .{ .io = io, .gpa = gpa, .lsp_allocator = gpa }; defer queue.deinit(); - var ptys: [pardes.MAX_PANES]?Pty = @splat(null); - var gens: [pardes.MAX_PANES]u32 = @splat(1); const rcs: host_io.Shell.PromptFiles = .{}; + var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = &rcs, .gens = @splat(1) }; + defer ptys.deinit(); var pipes: PipeTasks = .{}; - var watches: file_watch.Table = @splat(null); var no_peers: replica.Peers = .{}; - var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .prompt_rcs = &rcs, .ptys = &ptys, .gens = &gens, .queue = &queue, .pipe_tasks = &pipes, .inotify_fd = -1, .watches = &watches, .peers = &no_peers }; - defer shell.shutdownPtys(); + var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .ptys = &ptys, .queue = &queue, .pipe_tasks = &pipes, .watches = undefined, .peers = &no_peers }; const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80); - ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial }; + ptys.slots[0] = .{ .fd = child.file.handle, .pid = child.pid }; try std.testing.expect(host_io.writeFd(child.file.handle, "cd child\n")); const until = std.Io.Clock.awake.now(io).toMilliseconds() + 2_000; var actual: [4096]u8 = undefined; |
