1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
//! The terminal shell: owns the event loop and all IO. Translates vaxis
//! events into core events, performs the core's effects (fork ptys, write
//! them, resize them), and hands the core's Surface to vaxis cell-for-cell —
//! the canonical interface rendered with no interpretation.
const std = @import("std");
const builtin = @import("builtin");
const posix = std.posix;
const libc = std.c;
const vaxis = @import("vaxis");
const pardes = @import("pardes.zig");
const tracy = @import("tracy.zig");
const look = @import("look.zig");
extern "c" fn forkpty(amaster: *c_int, name: ?[*:0]u8, termp: ?*const anyopaque, winp: ?*const posix.winsize) c_int;
extern "c" fn execv(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int;
extern "c" fn chdir(path: [*:0]const u8) c_int;
extern "c" fn _exit(status: c_int) noreturn;
// absolute shell path per OS: execv must not search PATH (no allocation
// between fork and exec)
const bash_path: [*:0]const u8 = if (builtin.os.tag == .linux) "/usr/bin/bash" else "/bin/bash";
// TIOCSWINSZ: absent from std.c.T on darwin — _IOW('t', 103, winsize)
const TIOCSWINSZ: c_int = @bitCast(@as(u32, if (@hasDecl(posix.T, "IOCSWINSZ")) posix.T.IOCSWINSZ else 0x80087467));
pub const Command = struct {
pub var value: union(enum) {
nop,
quit,
tick,
key_press: vaxis.Key,
pty_read: struct { id: usize, bytes: []u8 },
pty_eof: struct { id: usize, gen: u32 },
winsize: vaxis.Winsize,
mouse: vaxis.Mouse,
paste: []const u8,
} = .nop;
};
const Loop = vaxis.Loop(@TypeOf(Command.value));
const Pty = struct {
file: std.Io.File,
pid: posix.pid_t,
reader: std.Io.Future(anyerror!void),
};
pub fn run(init: std.process.Init, opts: pardes.Options) !void {
const io = init.io;
const gpa = init.gpa;
// SIGWINCH must never run vaxis's signal handler: it posts the winsize
// event through std.Io.Mutex/Condition, and when the signal lands on a
// thread blocked inside an Io.Threaded syscall region (pty readers in
// read(2), the main thread parked in queue.pop) a contended lock re-enters
// the Io machinery and Syscall.start hits `unreachable` — panic, then the
// panic-time terminal restore used to write through the same Io and
// recurse until stack overflow. Reproduced by resizing the outer terminal
// (e.g. a font-size change) while shells run. Block it here, before any
// thread exists (threads inherit the mask, so vaxis's handler never
// fires), and take it synchronously on the sigwait thread below instead.
var winch_set = posix.sigemptyset();
posix.sigaddset(&winch_set, posix.SIG.WINCH);
posix.sigprocmask(posix.SIG.BLOCK, &winch_set, null);
var tty_buf: [0x10000]u8 = undefined;
var tty = try vaxis.Tty.init(io, &tty_buf);
defer tty.deinit(); // restore cooked termios LAST, after vx flushed its resets
var vx = try vaxis.init(io, gpa, init.environ_map, .{ .system_clipboard_allocator = gpa });
defer vx.deinit(gpa, tty.writer());
try vx.enterAltScreen(tty.writer());
defer vx.exitAltScreen(tty.writer()) catch {};
// requests 1002;1003;1004;1006 (cell-coordinate SGR; called pre-query, so
// vaxis never upgrades to 1016 pixel mode). Note: ghostty's GTK apprt drops
// middle press+release BEFORE mouse reporting when the desktop sets
// gtk-enable-primary-paste=false — no mode we request can surface middle
// clicks there (see test/snapshots/ghostty-mid.snap).
try vx.setMouseMode(tty.writer(), true);
var frame_arena: std.heap.ArenaAllocator = .init(gpa);
defer frame_arena.deinit();
// stb_image allocator for image panes
pardes.image.start(io, gpa);
defer pardes.image.stop();
var core = if (opts.load_path) |lp| blk: {
const bytes = try look.readFile(gpa, lp);
defer gpa.free(bytes);
break :blk try pardes.Pardes.initFromDump(gpa, opts, bytes);
} else try pardes.Pardes.init(gpa, opts);
defer core.deinit();
// shells emit OSC 133 prompt marks via this rc (prompt hiding, click-move)
writeFile(pardes.bash_rc);
var kitty_handles: [pardes.MAX_PANES]?vaxis.Image = @splat(null);
var ptys: [pardes.MAX_PANES]?Pty = @splat(null);
// per-slot spawn generation: a reused pane id ignores the old shell's
// late pty_eof (which would otherwise close the NEW pty on that slot)
var gens: [pardes.MAX_PANES]u32 = @splat(0);
var loop: Loop = .init(io, &tty, &vx);
defer {
// reap the reader tasks (cancel interrupts a blocked read) before
// closing the masters — the runtime joins those threads on exit and a
// reader stuck in read(2) would hang the process — then drain the
// queue: leftover events own gpa bytes and would dump as leaks.
for (&ptys) |*slot| if (slot.*) |*pt| {
pt.reader.cancel(io) catch {};
_ = libc.close(pt.file.handle);
slot.* = null;
};
while (loop.tryEvent() catch null) |ev| switch (ev) {
.pty_read => |pr| gpa.free(pr.bytes),
.paste => |b| gpa.free(@constCast(b)),
else => {},
};
}
// Perform the initial spawns BEFORE any worker thread exists: forkpty from
// a multithreaded process can wedge the child before exec.
drainEffects(core, &ptys, &gens, io, gpa, &loop, &vx, &tty, false);
try loop.start();
defer loop.stop();
// resize watcher: plain detached thread (not io.concurrent — teardown
// joins those, and sigwait never returns); dies with the process
(try std.Thread.spawn(.{}, winchWatch, .{ &loop, &vx, &tty })).detach();
vx.queryTerminal(tty.writer(), std.Io.Duration.fromMilliseconds(2)) catch {};
// now threads are fine: start a reader task per pty
for (&ptys, 0..) |*slot, id| if (slot.*) |*pt| {
pt.reader = try io.concurrent(readPty, .{ io, gpa, pt.file, id, gens[id], &loop });
};
var pending: ?@TypeOf(Command.value) = .tick;
while (!core.quit) {
var event = if (pending) |ev| blk: {
pending = null;
break :blk ev;
} else try loop.nextEvent();
const tz_event = tracy.zone(@src(), "event");
// Apply every queued INPUT event, then render ONCE — the gui shell
// drains SDL's queue the same way. Without this a wheel flick is fifty
// full render+repaint (and re-highlight) cycles instead of one.
// pty output keeps a frame per chunk: progress paints as it arrives.
var stop = false;
var batch: usize = 0;
while (true) {
var output = false;
switch (event) {
.nop, .tick => {},
.quit => stop = true,
.winsize => |ws| {
try vx.resize(gpa, tty.writer(), ws);
core.update(.{ .resize = .{ .cols = @intCast(ws.cols), .rows = @intCast(ws.rows) } });
},
.pty_read => |pr| {
core.update(.{ .output = .{ .pane = @intCast(pr.id), .bytes = pr.bytes } });
gpa.free(pr.bytes);
output = true;
},
.pty_eof => |e| if (gens[e.id] == e.gen) {
if (ptys[e.id]) |*pt| {
pt.reader.await(io) catch {}; // reader just finished; join it or its future leaks
_ = libc.close(pt.file.handle);
ptys[e.id] = null;
}
core.update(.{ .eof = .{ .pane = @intCast(e.id) } });
},
.key_press => |key| core.update(.{ .key = .{
.cp = mapKey(effCp(key)),
.text = key.text orelse "",
.ctrl = key.mods.ctrl,
.alt = key.mods.alt,
} }),
.mouse => |m| {
const button: ?pardes.Mouse.Button = switch (m.button) {
.left => .left,
.middle => .middle,
.right => .right,
.wheel_up => .wheel_up,
.wheel_down => .wheel_down,
.wheel_left => .wheel_left,
.wheel_right => .wheel_right,
.none => .none, // button-less motion: hover tracking
else => null,
};
if (button) |b| core.update(.{ .mouse = .{
.button = b,
.kind = switch (m.type) {
.press => .press,
.release => .release,
.motion => .motion,
.drag => .drag,
},
.col = @intCast(m.col),
.row = @intCast(m.row),
} });
},
.paste => |bytes| {
core.update(.{ .paste = bytes });
gpa.free(@constCast(bytes));
},
}
batch += 1;
if (stop or output or batch >= 64) break;
event = (try loop.tryEvent()) orelse break;
}
tz_event.end();
if (stop) break;
drainEffects(core, &ptys, &gens, io, gpa, &loop, &vx, &tty, true);
if (core.quit) break;
// Restore builtin: swap in a core rebuilt from the dump; the live
// shells die with their masters (readers canceled, gens bumped so
// their late eofs never touch the replay panes)
if (core.takeRestore()) |rp| blk: {
const bytes = look.readFile(gpa, rp) catch break :blk;
defer gpa.free(bytes);
var o = core.opts;
o.cols = core.screen_w;
o.rows = core.screen_h; // pre-size: dump panes never greet
const nc = pardes.Pardes.initFromDump(gpa, o, bytes) catch break :blk;
for (&ptys, 0..) |*slot, pid| if (slot.*) |*pt| {
pt.reader.cancel(io) catch {};
_ = libc.close(pt.file.handle);
slot.* = null;
gens[pid] +%= 1;
};
kitty_handles = @splat(null);
core.deinit();
core = nc;
}
// live cwd for tags/look: cheap per-pane lookup, per frame
for (&ptys, 0..) |*slot, id| if (slot.*) |pt| {
var lbuf: [1024]u8 = undefined;
if (look.shellCwd(pt.pid, &lbuf)) |cwd| core.setCwd(id, cwd);
};
// ---- render: surface -> vaxis, cell for cell ----
_ = frame_arena.reset(.retain_capacity);
const tz_core = tracy.zone(@src(), "core.render");
const surface = try core.render(frame_arena.allocator());
tz_core.end();
const tz_cells = tracy.zone(@src(), "surface->vaxis");
const win = vx.window();
win.clear();
var y: u16 = 0;
while (y < surface.rows) : (y += 1) {
var x: u16 = 0;
while (x < surface.cols) : (x += 1) {
const cell = surface.at(x, y);
if (cell.default) continue;
win.writeCell(x, y, .{
.char = .{ .grapheme = cell.grapheme() },
.style = vaxisStyle(cell.style),
});
}
}
tz_cells.end();
// pixel attachments (kitty graphics): transmit once per pane, then
// re-place every frame (placements aren't persistent)
for (surface.images[0..surface.nimages]) |maybe| {
const place = maybe orelse continue;
if (kitty_handles[place.pane] == null and vx.caps.kitty_graphics) {
const enc = std.base64.standard.Encoder;
if (gpa.alloc(u8, enc.calcSize(place.rgba.len))) |b64| {
defer gpa.free(b64);
_ = enc.encode(b64, place.rgba);
kitty_handles[place.pane] = vx.transmitPreEncodedImage(tty.writer(), b64, @intCast(place.iw), @intCast(place.ih), .rgba) catch null;
} else |_| {}
}
if (kitty_handles[place.pane]) |h| {
const child = win.child(.{ .x_off = place.x, .y_off = place.y, .width = place.w, .height = place.h });
h.draw(child, .{ .scale = .contain }) catch {};
}
}
if (surface.cursor) |cur| {
win.showCursor(cur.x, cur.y);
// insert = beam, everything else = the terminal's default shape
win.setCursorShape(if (cur.bar) .beam else .default);
}
const tz_render = tracy.zone(@src(), "vx.render");
try vx.render(tty.writer());
tz_render.end();
tracy.frameMark();
}
}
fn drainEffects(
core: *pardes.Pardes,
ptys: *[pardes.MAX_PANES]?Pty,
gens: *[pardes.MAX_PANES]u32,
io: std.Io,
gpa: std.mem.Allocator,
loop: *Loop,
vx: *vaxis.Vaxis,
tty: *vaxis.Tty,
threads_ok: bool,
) void {
while (core.nextEffect()) |effect| switch (effect) {
.spawn => |sp| {
// the core reuses pane ids and there is no close effect: a deleted
// pane's shell lives in its slot until a respawn lands here — reap
// it (cancel joins the reader; its late eof is ignored by gen)
if (ptys[sp.pane]) |*old| {
old.reader.cancel(io) catch {};
_ = libc.close(old.file.handle);
ptys[sp.pane] = null;
}
gens[sp.pane] +%= 1;
const cwd = sp.cwd.slice();
var cwd_buf: [256:0]u8 = undefined;
var cwd_z: ?[*:0]const u8 = null;
if (cwd.len > 0) {
@memcpy(cwd_buf[0..cwd.len], cwd);
cwd_buf[cwd.len] = 0;
cwd_z = @ptrCast(&cwd_buf);
}
const child = forkShell(cwd_z, core.screen_h, core.screen_w);
ptys[sp.pane] = .{ .file = child.file, .pid = child.pid, .reader = .{ .any_future = null, .result = {} } };
// report the pane's starting directory back to the core (tags)
var lbuf: [1024]u8 = undefined;
if (look.shellCwd(child.pid, &lbuf)) |wd| core.setCwd(sp.pane, wd);
if (threads_ok) {
if (ptys[sp.pane]) |*pt| {
pt.reader = io.concurrent(readPty, .{ io, gpa, pt.file, @as(usize, sp.pane), gens[sp.pane], loop }) catch pt.reader;
}
}
},
.write => |w| {
if (ptys[w.pane]) |pt| writeFd(pt.file.handle, w.bytes.slice());
},
.resize_pty => |rs| {
if (ptys[rs.pane]) |pt| {
const ws: posix.winsize = .{ .row = rs.rows, .col = rs.cols, .xpixel = 0, .ypixel = 0 };
_ = posix.system.ioctl(pt.file.handle, TIOCSWINSZ, @intFromPtr(&ws));
}
},
.open_link => {}, // tty: no link handling (look opens panes instead)
.save_file => |sf| {
const pane = core.panes[sf.pane] orelse continue;
const f = pane.file orelse continue;
var pathbuf: [4096:0]u8 = undefined;
if (f.path.len >= pathbuf.len) continue;
@memcpy(pathbuf[0..f.path.len], f.path);
pathbuf[f.path.len] = 0;
const fd = libc.open(pathbuf[0..f.path.len :0], .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644));
if (fd < 0) continue;
writeFd(fd, f.content);
_ = libc.close(fd);
},
.write_dump => {
const out = core.dump_out orelse continue;
var pbuf: [1024:0]u8 = undefined;
const path = pardes.dump.outPath(&pbuf) orelse continue;
const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644));
if (fd < 0) continue;
writeFd(fd, out);
_ = libc.close(fd);
core.setLastDump(path);
},
.set_clipboard => {
// mirror the core's yank register out via OSC 52
if (core.yank) |y| vx.copyToSystemClipboard(tty.writer(), y, gpa) catch {};
},
.get_clipboard => vx.requestSystemClipboard(tty.writer()) catch {},
.quit => {},
};
}
/// Consume SIGWINCH synchronously (it is blocked in every thread) and post
/// the new size as a winsize event from normal thread context — the one place
/// vaxis's Io-backed queue is safe to touch on a resize.
fn winchWatch(loop: *Loop, vx: *vaxis.Vaxis, tty: *vaxis.Tty) void {
var set = posix.sigemptyset();
posix.sigaddset(&set, posix.SIG.WINCH);
while (true) {
var sig: c_int = 0;
if (libc.sigwait(&set, &sig) != 0) continue;
if (vx.state.in_band_resize) continue; // terminal reports via CSI 48
const ws = tty.getWinsize() catch continue;
loop.postEvent(.{ .winsize = ws }) catch {};
}
}
fn forkShell(cwd: ?[*:0]const u8, rows: u16, cols: u16) struct { file: std.Io.File, pid: posix.pid_t } {
var master: c_int = undefined;
const ws = posix.winsize{ .row = rows, .col = cols, .xpixel = 0, .ypixel = 0 };
const pid = forkpty(&master, null, null, &ws);
if (pid == 0) {
// the blocked-SIGWINCH mask survives fork AND exec — unblock it or
// bash/vim in the pane would never see resizes (sigprocmask is
// async-signal-safe)
var set = posix.sigemptyset();
posix.sigaddset(&set, posix.SIG.WINCH);
posix.sigprocmask(posix.SIG.UNBLOCK, &set, null);
if (cwd) |c| _ = chdir(c);
const argv: [4:null]?[*:0]const u8 = .{ bash_path, "--rcfile", "/tmp/pardes-osc133.bash", null };
_ = execv(bash_path, &argv);
_exit(127);
}
return .{ .file = .{ .handle = master, .flags = .{ .nonblocking = false } }, .pid = pid };
}
fn readPty(io: std.Io, gpa: std.mem.Allocator, pty: std.Io.File, id: usize, gen: u32, loop: *Loop) anyerror!void {
var read_buf: [0x10000]u8 = undefined;
var reader = pty.readerStreaming(io, &read_buf);
while (true) {
var buf: [0x10000]u8 = undefined;
var vec = [_][]u8{&buf};
const n = reader.interface.readVec(&vec) catch break;
if (n == 0) break;
const bytes = try gpa.dupe(u8, buf[0..n]);
loop.postEvent(.{ .pty_read = .{ .id = id, .bytes = bytes } }) catch {
gpa.free(bytes);
break;
};
}
// non-blocking: a teardown cancel only unblocks one wait, so a blocking
// post into a full queue here could hang the exit
_ = loop.tryPostEvent(.{ .pty_eof = .{ .id = id, .gen = gen } }) catch {};
}
/// The effective codepoint the way vaxis Key.matches sees it: a single-char
/// text wins (shift resolved by the terminal), else the shifted codepoint.
fn effCp(key: vaxis.Key) u21 {
if (key.text) |t| {
const view = std.unicode.Utf8View.init(t) catch return key.codepoint;
var it = view.iterator();
if (it.nextCodepoint()) |c| {
if (it.nextCodepoint() == null) return c;
}
}
return key.shifted_codepoint orelse key.codepoint;
}
/// vaxis functional-key codepoints -> core Key constants (ASCII ones already
/// coincide: enter/tab/escape/backspace pass through).
fn mapKey(cp: u21) u21 {
return switch (cp) {
vaxis.Key.up => pardes.Key.up,
vaxis.Key.down => pardes.Key.down,
vaxis.Key.left => pardes.Key.left,
vaxis.Key.right => pardes.Key.right,
vaxis.Key.home => pardes.Key.home,
vaxis.Key.end => pardes.Key.end,
vaxis.Key.page_up => pardes.Key.page_up,
vaxis.Key.page_down => pardes.Key.page_down,
vaxis.Key.delete => pardes.Key.delete,
else => cp,
};
}
fn vaxisStyle(s: pardes.CellStyle) vaxis.Style {
return .{
.fg = vaxisColor(s.fg),
.bg = vaxisColor(s.bg),
.bold = s.bold,
.dim = s.dim,
.italic = s.italic,
.blink = s.blink,
.reverse = s.reverse,
.invisible = s.invisible,
.strikethrough = s.strikethrough,
.ul_style = switch (s.ul) {
.off => .off,
.single => .single,
.double => .double,
.curly => .curly,
.dotted => .dotted,
.dashed => .dashed,
},
};
}
fn vaxisColor(c: pardes.Color) vaxis.Color {
return switch (c) {
.default => .default,
.index => |i| .{ .index = i },
.rgb => |rgb| .{ .rgb = rgb },
};
}
fn writeFile(data: []const u8) void {
const fd = libc.open("/tmp/pardes-osc133.bash", .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644));
if (fd < 0) return;
defer _ = libc.close(fd);
writeFd(fd, data);
}
fn writeFd(fd: c_int, data: []const u8) void {
var off: usize = 0;
while (off < data.len) {
const n = libc.write(fd, data[off..].ptr, data.len - off);
if (n < 0) {
if (libc.errno(n) == .INTR) continue;
return;
}
off += @intCast(n);
}
}
|