Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tty(posix): remove signal handler when we have in-band-resize #87

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading