Skip to content

Commit

Permalink
tty(posix): remove signal handler when we have in-band-resize
Browse files Browse the repository at this point in the history
  • Loading branch information
rockorager committed Oct 11, 2024
1 parent ea5726b commit 306acc7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Loop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ pub fn handleEventGeneric(self: anytype, vx: *Vaxis, cache: *GraphemeCache, Even
if (@hasField(Event, "winsize")) {
return self.postEvent(.{ .winsize = winsize });
}

switch (builtin.os.tag) {
.windows => {},
// Reset the signal handler if we are receiving in_band_resize
else => self.tty.resetSignalHandler(),
}
},
}
},
Expand Down
20 changes: 20 additions & 0 deletions src/posix/Tty.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var handlers: [8]SignalHandler = undefined;
var handler_mutex: std.Thread.Mutex = .{};
var handler_idx: usize = 0;

var handler_installed: bool = false;

/// global tty instance, used in case of a panic. Not guaranteed to work if
/// for some reason there are multiple TTYs open under a single vaxis
/// compilation unit - but this is better than nothing
Expand All @@ -49,6 +51,7 @@ pub fn init() !Posix {
.flags = 0,
};
try posix.sigaction(posix.SIG.WINCH, &act, null);
handler_installed = true;

const self: Posix = .{
.fd = fd,
Expand All @@ -69,6 +72,23 @@ pub fn deinit(self: Posix) void {
posix.close(self.fd);
}

/// Resets the signal handler to it's default
pub fn resetSignalHandler() void {
if (!handler_installed) return;
handler_installed = false;
var act = posix.Sigaction{
.handler = posix.SIG.DFL,
.mask = switch (builtin.os.tag) {
.macos => 0,
.linux => posix.empty_sigset,
.freebsd => posix.empty_sigset,
else => @compileError("os not supported"),
},
.flags = 0,
};
posix.sigaction(posix.SIG.WINCH, &act, null) catch {};
}

/// Write bytes to the tty
pub fn write(self: *const Posix, bytes: []const u8) !usize {
return posix.write(self.fd, bytes);
Expand Down

0 comments on commit 306acc7

Please sign in to comment.