const std = @import("std"); const pardes = @import("core"); const host_io = @import("host_io.zig"); const Lsp = host_io.Lsp; const Host = @This(); ctx: ?*anyopaque = null, vtable: *const VTable = &.{}, pub const VTable = struct { wait_input: ?*const fn (ctx: ?*anyopaque, timeout_ms: u32) void = null, /// The shell's monotonic clock in ns: the only time the core sees. /// Without one, core animation moves only by `.tick` events. now: ?*const fn (ctx: ?*anyopaque) u64 = null, /// The frame drawn on the frontend: false when it could not show it /// now (no image to draw into, the terminal still answering the last), /// which owes the next frame. present: ?*const fn (ctx: ?*anyopaque, surface: *const pardes.Surface) bool = null, post_present: ?*const fn (ctx: ?*anyopaque) void = null, poll_frame: ?*const fn (ctx: ?*anyopaque) void = null, /// Whether anyone sees a frame drawn now. A detached session with no /// client attached draws none (a /screen read draws its own), and /// the frame owed is drawn when one attaches. Absent: always. watched: ?*const fn (ctx: ?*anyopaque) bool = null, /// Detach: the terminal or window let go, the session left running /// for the daemon to attach again (daemon.zig). detach: ?*const fn (ctx: ?*anyopaque) void = null, /// Adopt: the shells a dead session left, asked of the daemon. adopt: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, /// Upgrade: this process exec'd as the pardes installed since, at /// the step's end (upgrade.zig); `pane` hears why if it cannot be. upgrade: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, spawn: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, cwd: []const u8, size: pardes.PtySize) void = null, pty_write: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, bytes: []const u8) void = null, pty_resize: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, size: pardes.PtySize) void = null, pty_signal: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, sig: pardes.PtySignal) void = null, close_pty: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, tty_taken: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) bool = null, /// The name of the program holding the tty, when it is not the /// shell (host_io.foregroundName), in `buf`. fg_name: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, buf: []u8) ?[]const u8 = null, /// Answered at once, not queued as an effect: Kill asks while the /// job it saw running is still the one in the tty (host_io.killJob). kill_job: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) bool = null, write_file: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, path: []const u8, bytes: []const u8) void = null, write_dump: ?*const fn (ctx: ?*anyopaque, bytes: []const u8) void = null, watch_file: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, path: []const u8, on: bool, mode: pardes.WatchMode) void = null, watch_theme: ?*const fn (ctx: ?*anyopaque, generation: u32, on: bool) void = null, dump_themes: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, set_clipboard: ?*const fn (ctx: ?*anyopaque, text: []const u8) void = null, read_clipboard: ?*const fn (ctx: ?*anyopaque) void = null, open_link: ?*const fn (ctx: ?*anyopaque, url: []const u8) void = null, /// A plumbing rule's program (Effect.start): every host's is /// startDetached. Null once it runs, else why not. start: ?*const fn (ctx: ?*anyopaque, line: []const u8, why: []u8) ?[]const u8 = null, lsp: ?*const fn (ctx: ?*anyopaque, req: Lsp.Request) void = null, pipe: ?*const fn (ctx: ?*anyopaque, id: u32) void = null, /// Start the streaming search of this generation on a worker /// (pardes.Stream.start); absent, it runs on the loop. search: ?*const fn (ctx: ?*anyopaque, gen: u32) void = null, /// Carry the dump of handover `seq` to another window on a worker /// (Handover.start), which answers through the inbox's `handed`. hand_over: ?*const fn (ctx: ?*anyopaque, seq: u32, bytes: []const u8) void = null, /// Run job `token` of the pane in slot `pane` (whose serial is /// `serial`) on a worker (`KindJob`), answering with the `job` event; /// absent, it runs on the loop. job: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, serial: u32, token: u32) void = null, }; // What a call names, gives and gets, derived from the table: the core calls // out through its tape, `p.tape.host(p, .pty_write, .{ pane, bytes })`. /// One of the table's functions, by its field's name: `.pty_write`. The /// tape numbers a call by it (the table's order). pub const Fn = std.meta.FieldEnum(VTable); /// What `f` is given after `ctx`, in order. pub fn Args(comptime f: Fn) type { const params = @typeInfo(Of(f)).@"fn".param_types; var types: [params.len - 1]type = undefined; for (params[1..], &types) |P, *T| T.* = P.?; return @Tuple(&types); } /// What `f` answers. pub fn Ret(comptime f: Fn) type { return @typeInfo(Of(f)).@"fn".return_type.?; } fn Of(comptime f: Fn) type { return @typeInfo(@typeInfo(@FieldType(VTable, @tagName(f))).optional.child).pointer.child; } /// Whether this host has `f`. pub fn has(h: Host, comptime f: Fn) bool { return @field(h.vtable.*, @tagName(f)) != null; } /// Which functions it has, a bit each in the table's order. pub fn mask(h: Host) u64 { var m: u64 = 0; inline for (@typeInfo(VTable).@"struct".field_names, 0..) |name, i| { if (@field(h.vtable.*, name) != null) m |= @as(u64, 1) << i; } return m; } /// `f(args)` of this host; null: it has no such function. pub inline fn call(h: Host, comptime f: Fn, args: Args(f)) ?Ret(f) { const func = @field(h.vtable.*, @tagName(f)) orelse return null; return @call(.auto, func, .{h.ctx} ++ args); }