Added the read_thread to the Vaxis struct and made null handling more idiomatic.

This commit is contained in:
00JCIV00 2024-03-11 16:51:10 -04:00 committed by Tim Culverhouse
parent 8d842c8b44
commit 3e1e6200b4

View file

@ -13,6 +13,8 @@ pub const Queue = @import("queue.zig").Queue;
pub const Screen = @import("Screen.zig"); pub const Screen = @import("Screen.zig");
pub const Tty = @import("Tty.zig"); pub const Tty = @import("Tty.zig");
pub const Window = @import("Window.zig"); pub const Window = @import("Window.zig");
pub const Winsize = Tty.Winsize;
pub const Style = Cell.Style;
pub const ctlseqs = @import("ctlseqs.zig"); pub const ctlseqs = @import("ctlseqs.zig");
pub const gwidth = @import("gwidth.zig"); pub const gwidth = @import("gwidth.zig");
@ -60,8 +62,7 @@ pub fn Vaxis(comptime T: type) type {
queue: Queue(T, 512), queue: Queue(T, 512),
tty: ?Tty, tty: ?Tty,
read_thread: ?std.Thread = null,
read_thread: ?std.Thread,
/// the screen we write to /// the screen we write to
screen: Screen, screen: Screen,
@ -76,6 +77,8 @@ pub fn Vaxis(comptime T: type) type {
kitty_keyboard: bool = false, kitty_keyboard: bool = false,
bracketed_paste: bool = false, bracketed_paste: bool = false,
mouse: bool = false, mouse: bool = false,
/// if our read thread is running
reading: bool = false,
} = .{}, } = .{},
caps: Capabilities = .{}, caps: Capabilities = .{},
@ -112,8 +115,7 @@ pub fn Vaxis(comptime T: type) type {
/// optional so applications can choose to not free resources when the /// optional so applications can choose to not free resources when the
/// application will be exiting anyways /// application will be exiting anyways
pub fn deinit(self: *Self, alloc: ?std.mem.Allocator) void { pub fn deinit(self: *Self, alloc: ?std.mem.Allocator) void {
if (self.tty) |_| { if (self.tty) |*tty| {
var tty = &self.tty.?;
if (self.state.kitty_keyboard) { if (self.state.kitty_keyboard) {
_ = tty.write(ctlseqs.csi_u_pop) catch {}; _ = tty.write(ctlseqs.csi_u_pop) catch {};
} }
@ -144,21 +146,23 @@ pub fn Vaxis(comptime T: type) type {
/// spawns the input thread to start listening to the tty for input /// spawns the input thread to start listening to the tty for input
pub fn startReadThread(self: *Self) !void { pub fn startReadThread(self: *Self) !void {
if (self.state.reading) return;
self.tty = try Tty.init(); self.tty = try Tty.init();
// run our tty read loop in it's own thread // run our tty read loop in it's own thread
self.read_thread = try std.Thread.spawn(.{}, Tty.run, .{ &self.tty.?, T, self }); self.read_thread = try std.Thread.spawn(.{}, Tty.run, .{ &self.tty.?, T, self });
try self.read_thread.?.setName("vaxis_tty");
self.state.reading = true;
} }
/// stops reading from the tty /// stops reading from the tty
pub fn stopReadThread(self: *Self) void { pub fn stopReadThread(self: *Self) void {
if (self.tty) |_| { if (!self.state.reading) return;
var tty = &self.tty.?; if (self.tty) |*tty| tty.stop();
tty.stop(); if (self.read_thread) |*read| {
if (self.read_thread) |thread| { read.join();
thread.join(); self.read_thread = null;
self.read_thread = null;
}
} }
self.state.reading = false;
} }
/// returns the next available event, blocking until one is available /// returns the next available event, blocking until one is available