summaryrefslogtreecommitdiff
path: root/src/gui/gui.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/gui.zig')
-rw-r--r--src/gui/gui.zig46
1 files changed, 6 insertions, 40 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index ecd9026f..8415d64e 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -4115,9 +4115,7 @@ const Shell = struct {
grid: bool = false,
/// Shells a dead session left (Adopt), each waiting for the spawn of
/// the terminal pane made for it.
- adoptions: [pardes.MAX_PANES]Adoption = undefined,
- adoptions_len: usize = 0,
- adopted: usize = 0,
+ adoptions: host_io.Adoptions = .{},
io: std.Io,
gpa: std.mem.Allocator,
fs: ?*ninep_io.Listener = null,
@@ -4429,9 +4427,7 @@ const Shell = struct {
fn adopt(ctx: ?*anyopaque, pane: u8) void {
const s = shellOf(ctx);
- if (!daemon.kept()) return s.core.reportFailure(pane, "Adopt: no pardes daemon is keeping shells for this session");
- s.adopted = 0;
- daemon.adopt();
+ s.adoptions.ask(s.core, pane);
}
fn upgradeAsked(ctx: ?*anyopaque, pane: u8) void {
@@ -4510,7 +4506,7 @@ const Shell = struct {
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.adoptShell(k.pid, k.fd);
+ 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 } };
@@ -4532,30 +4528,6 @@ const Shell = struct {
s.follow();
}
- /// One shell of a dead session (`pid` 0: the last of them was told):
- /// a terminal pane made for it, as the Term word makes one, whose spawn
- /// takes this pty (spawnPane).
- fn adoptShell(s: *Shell, pid: libc.pid_t, fd: c_int) void {
- const core = s.core;
- if (pid == 0) {
- var buf: [96]u8 = undefined;
- return core.setMessage(core.active, if (s.adopted == 0) "Adopt: no shell of a session that died is waiting" else std.fmt.bufPrint(&buf, "Adopt: {d} shell{s} taken in; what they showed is gone", .{ s.adopted, if (s.adopted == 1) "" else "s" }) catch "Adopt: shells taken in");
- }
- if (s.adoptions_len < s.adoptions.len) {
- s.adoptions[s.adoptions_len] = .{ .pid = pid, .fd = fd, .pane = null };
- s.adoptions_len += 1;
- if (core.adoptShell()) |id| {
- s.adoptions[s.adoptions_len - 1].pane = id;
- s.adopted += 1;
- return;
- }
- s.adoptions_len -= 1;
- }
- _ = libc.close(fd);
- var buf: [96]u8 = undefined;
- core.setMessage(core.active, std.fmt.bufPrint(&buf, "Adopt: no room for shell {d}; it ends with this session", .{pid}) catch "Adopt: no room for a shell");
- }
-
const vtable: pardes.Host.VTable = .{
.wait_input = waitInput,
.now = guiNow,
@@ -4698,7 +4670,7 @@ const Shell = struct {
s.saw_event = true;
},
.adopted => |a| {
- s.adoptShell(a.pid, a.fd);
+ s.adoptions.adoptShell(s.core, a.pid, a.fd);
s.saw_event = true;
},
};
@@ -5258,11 +5230,7 @@ fn gridPostPresent(ctx: ?*anyopaque) void {
fn spawnPane(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void {
const s = shellOf(ctx);
s.reap();
- for (s.adoptions[0..s.adoptions_len], 0..) |a, i| if (a.pane == pane) {
- std.mem.copyForwards(Adoption, s.adoptions[i .. s.adoptions_len - 1], s.adoptions[i + 1 .. s.adoptions_len]);
- s.adoptions_len -= 1;
- return takeShell(s, pane, a);
- };
+ if (s.adoptions.take(pane)) |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;
@@ -5289,11 +5257,9 @@ fn spawnPane(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void {
};
}
-const Adoption = struct { pid: libc.pid_t, fd: c_int, pane: ?u8 };
-
/// 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: Adoption) void {
+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 = .{} };