From c742d19d4bacbd9a91627c4859ca8cb0d11bdba7 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:06:42 -0300 Subject: Term: the terminal as the core calls it, src/Term.zig, its lifecycle first (docs/next-terminal.md, Phase 0): Term.open makes an emulator and Term.close frees it, Term.due says when its next kitty frame is due, Term.revive resets a restored terminal's modes under its banner, Term.start and Term.stop run the PNG resize workers; the pane half of each is the core's: panes.createDoc and panes.createTerminal make the pane (the speculation guard there), emulatorGpa and emulator_gpa are the core's choice of allocator, teardownPane closes the emulator, dump.zig's revivePane resets what the old shell was and ensureKind opens an emulator straight into the pane; tape.authorityOnly is the one guard Kind.zig and the terminal both had a copy of; nothing else changes --- src/term/terminal.zig | 110 ++++++++++++++------------------------------------ 1 file changed, 31 insertions(+), 79 deletions(-) (limited to 'src/term/terminal.zig') diff --git a/src/term/terminal.zig b/src/term/terminal.zig index bc5e7fd6..9472cfe1 100644 --- a/src/term/terminal.zig +++ b/src/term/terminal.zig @@ -594,23 +594,7 @@ pub fn takeMarks(state: *State, bytes: []const u8) void { m.finished = if (seq == std.math.maxInt(u32)) null else .{ .seq = seq, .status = if (status == std.math.minInt(i32)) null else status }; } -/// A screen-state replica's emulators' allocator (`State.gpa`). -pub const emulator_gpa = std.heap.c_allocator; - -/// What a terminal's emulator is made with, by `create` or a pane's load -/// (dump.loadPane, a closed terminal brought back): `gpa`, but on a screen- -/// state replica `emulator_gpa`. -pub fn emulatorGpa(gpa: std.mem.Allocator) std.mem.Allocator { - return if (pardes.tape.screenReplica()) emulator_gpa else gpa; -} - -/// A replica's speculation leaves an emulator be: it is the authority's -/// (docs/next-daemon.md, phase 5), so the input waits for its order. -fn authorityOnly(comptime why: []const u8) bool { - if (!pardes.tape.speculating()) return false; - pardes.tape.poison(why); - return true; -} +const authorityOnly = pardes.tape.authorityOnly; /// A speculation that moves a terminal's view: a screen-state replica's /// may (its rollback puts the view back, replica.zig `views`, and a delta @@ -863,21 +847,17 @@ pub fn screenTextLen(pane: *Pane) usize { return @intCast(counting.fullCount()); } -/// Release the emulator's heap. The Pane allocation itself is the core's. -pub fn deinitEmulator(pane: *Pane, gpa: std.mem.Allocator) void { - // A pane with none (a file's, a picker's, a preview's) closes in a - // speculation as anywhere. - const state = pane.terminal orelse return; - if (authorityOnly("close a terminal")) return; +/// Its emulator and what it holds, freed; its kitty images' caches were +/// `gpa`'s. +pub fn close(state: *State, gpa: std.mem.Allocator) void { if (comptime images_enabled) state.images.deinit(gpa); state.stream.deinit(); const own = state.gpa; state.vt.deinit(own); own.destroy(state); - pane.terminal = null; } -/// A terminal pane's emulator, `cols` by `rows`, as `create` makes one. +/// A terminal pane's emulator, `cols` by `rows`, as `open` makes one. fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal { return ghostty_vt.Terminal.init(terminalIo(), gpa, .{ .cols = cols, @@ -894,65 +874,38 @@ fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal { }); } -/// Allocate the live emulator half of a terminal pane. Slot ownership, serial -/// assignment, and spawn effects remain core lifecycle invariants. -pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { - if (authorityOnly("open a terminal")) return error.OutOfMemory; - const pane = try createDoc(gpa, cols, rows); - errdefer gpa.destroy(pane); - const own = emulatorGpa(gpa); - const state = try own.create(State); - errdefer own.destroy(state); +/// A new emulator, `cols` by `rows`, allocated with `gpa`, its own from +/// then on (`State.gpa`). +pub fn open(gpa: std.mem.Allocator, cols: u16, rows: u16) !*State { + const state = try gpa.create(State); + errdefer gpa.destroy(state); // Every other field its default, never what the heap had: a digest // reads them, and two replicas must agree (src/replica.zig). - state.* = .{ .gpa = own, .vt = try emulator(own, cols, rows), .stream = undefined }; + state.* = .{ .gpa = gpa, .vt = try emulator(gpa, cols, rows), .stream = undefined }; state.wire(); - pane.terminal = state; - pane.tty_filter = true; - return pane; -} - -pub fn createDoc(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { - const pane = try gpa.create(Pane); - pane.* = .{ - .gpa = gpa, - .cols = cols, - .rows = rows, - }; - return pane; + return state; } /// Where a restored terminal's history ends and its new shell begins. pub const restored_banner = "\x1b[0m\r\n\x1b[2m\u{2500}\u{2500} restored history \u{2500}\u{2500}\x1b[0m\r\n"; -/// A terminal loaded from a dump, its shell not there, made ready for a new -/// one: its program's screen modes reset (the old program is gone), its -/// history marked off from what the new shell prints, and the pane waiting -/// for it like any new terminal (a line queued for the old one goes to it). -pub fn reviveShell(pane: *Pane) void { - if (pane.terminal == null) return; +/// A terminal loaded from a dump, its shell not there, made ready for a +/// new one: its program's screen modes reset (the old program is gone) and +/// its history marked off from what the new shell prints. +pub fn revive(state: *State) void { // The primary screen, keys, cursor and mouse as a shell starts with // them, the cursor where it was (a soft reset, or leaving a primary // screen for itself, would take it home). - if (pane.terminal.?.vt.screens.active_key == .alternate) ingest(pane, "\x1b[?1049l"); - ingest(pane, "\x1b[?1l\x1b>\x1b[?25h\x1b[?7h\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?2004l\x1b[\x1b[?25h\x1b[?7h\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?2004l\x1b[