add: signal handling WINCH, user input thread with waiting blocking
This commit is contained in:
123
src/app.zig
123
src/app.zig
@@ -1,11 +1,11 @@
|
||||
//! Application type for TUI-applications
|
||||
const std = @import("std");
|
||||
const terminal = @import("terminal.zig");
|
||||
const code_point = terminal.code_point;
|
||||
|
||||
const mergeTaggedUnions = @import("event.zig").mergeTaggedUnions;
|
||||
const isTaggedUnion = @import("event.zig").isTaggedUnion;
|
||||
|
||||
const BuiltinEvent = @import("event.zig").BuiltinEvent;
|
||||
const SystemEvent = @import("event.zig").SystemEvent;
|
||||
const Queue = @import("queue.zig").Queue;
|
||||
|
||||
const log = std.log.scoped(.app);
|
||||
@@ -18,25 +18,40 @@ pub fn App(comptime E: type) type {
|
||||
@compileError("Provided user event `E` for `App(comptime E: type)` is not of type `union(enum)`.");
|
||||
}
|
||||
return struct {
|
||||
pub const Event = mergeTaggedUnions(BuiltinEvent, E);
|
||||
pub const Event = mergeTaggedUnions(SystemEvent, E);
|
||||
pub const Layout = @import("layout.zig").Layout(Event);
|
||||
pub const Widget = @import("widget.zig").Widget(Event);
|
||||
|
||||
queue: Queue(Event, 256) = .{},
|
||||
thread: ?std.Thread = null,
|
||||
quit: bool = false,
|
||||
quit: std.Thread.ResetEvent = .{},
|
||||
termios: ?std.posix.termios = null,
|
||||
|
||||
// TODO: event loop function?
|
||||
// layout handling?
|
||||
pub const SignalHandler = struct {
|
||||
context: *anyopaque,
|
||||
callback: *const fn (context: *anyopaque) void,
|
||||
};
|
||||
|
||||
pub fn init() @This() {
|
||||
var winch_act = std.posix.Sigaction{
|
||||
.handler = .{ .handler = @This().handleWinch },
|
||||
.mask = std.posix.empty_sigset,
|
||||
.flags = 0,
|
||||
};
|
||||
std.posix.sigaction(std.posix.SIG.WINCH, &winch_act, null) catch @panic("could not attach signal WINCH");
|
||||
|
||||
return .{};
|
||||
}
|
||||
|
||||
pub fn start(this: *@This()) !void {
|
||||
if (this.thread) |_| return;
|
||||
|
||||
try registerWinch(.{
|
||||
.context = this,
|
||||
.callback = @This().winsizeCallback,
|
||||
});
|
||||
|
||||
this.quit.reset();
|
||||
this.thread = try std.Thread.spawn(.{}, @This().run, .{this});
|
||||
var termios: std.posix.termios = undefined;
|
||||
try terminal.enableRawMode(&termios);
|
||||
@@ -49,7 +64,7 @@ pub fn App(comptime E: type) type {
|
||||
try terminal.disableRawMode(termios);
|
||||
try terminal.restoreScreen();
|
||||
}
|
||||
this.quit = true;
|
||||
this.quit.set();
|
||||
if (this.thread) |thread| {
|
||||
thread.join();
|
||||
this.thread = null;
|
||||
@@ -66,46 +81,70 @@ pub fn App(comptime E: type) type {
|
||||
this.queue.push(event);
|
||||
}
|
||||
|
||||
fn winsizeCallback(ptr: *anyopaque) void {
|
||||
const this: *@This() = @ptrCast(@alignCast(ptr));
|
||||
this.postEvent(.{ .resize = terminal.getTerminalSize() });
|
||||
}
|
||||
|
||||
var winch_handler: ?SignalHandler = null;
|
||||
|
||||
fn registerWinch(handler: SignalHandler) !void {
|
||||
if (winch_handler) |_| {
|
||||
@panic("Cannot register another WINCH handler.");
|
||||
}
|
||||
winch_handler = handler;
|
||||
}
|
||||
|
||||
fn handleWinch(_: c_int) callconv(.C) void {
|
||||
if (winch_handler) |handler| {
|
||||
handler.callback(handler.context);
|
||||
}
|
||||
}
|
||||
|
||||
fn run(this: *@This()) !void {
|
||||
// thread to read user inputs
|
||||
// send initial terminal size
|
||||
// changes are handled by the winch signal handler (see `init` and `registerWinch`)
|
||||
const size = terminal.getTerminalSize();
|
||||
this.postEvent(.{ .resize = size });
|
||||
// read input in loop
|
||||
|
||||
// thread to read user inputs
|
||||
var buf: [256]u8 = undefined;
|
||||
while (!this.quit) {
|
||||
// FIXME: here is a race-condition -> i.e. there could be events
|
||||
// in the queue, but they will not be executed because the main
|
||||
// loop will close!
|
||||
const read_bytes = try terminal.read(buf[0..]);
|
||||
// escape key presses
|
||||
if (buf[0] == 0x1b and read_bytes > 1) {
|
||||
switch (buf[1]) {
|
||||
// TODO: parse corresponding codes
|
||||
else => {},
|
||||
while (true) {
|
||||
// FIX: I still think that there is a race condition (I'm just waiting 'long' enough)
|
||||
this.quit.timedWait(20 * std.time.ns_per_ms) catch {
|
||||
const read_bytes = try terminal.read(buf[0..]);
|
||||
// escape key presses
|
||||
if (buf[0] == 0x1b and read_bytes > 1) {
|
||||
switch (buf[1]) {
|
||||
// TODO: parse corresponding codes
|
||||
else => {},
|
||||
}
|
||||
} else {
|
||||
const b = buf[0];
|
||||
const key: terminal.Key = switch (b) {
|
||||
0x00 => .{ .cp = '@', .mod = .{ .ctrl = true } },
|
||||
0x08 => .{ .cp = terminal.Key.backspace },
|
||||
0x09 => .{ .cp = terminal.Key.tab },
|
||||
0x0a, 0x0d => .{ .cp = terminal.Key.enter },
|
||||
0x01...0x07, 0x0b...0x0c, 0x0e...0x1a => .{ .cp = b + 0x60, .mod = .{ .ctrl = true } },
|
||||
0x1b => escape: {
|
||||
std.debug.assert(read_bytes == 1);
|
||||
break :escape .{ .cp = terminal.Key.escape };
|
||||
},
|
||||
0x7f => .{ .cp = terminal.Key.backspace },
|
||||
else => {
|
||||
var iter = terminal.code_point.Iterator{ .bytes = buf[0..read_bytes] };
|
||||
while (iter.next()) |cp| {
|
||||
this.postEvent(.{ .key = .{ .cp = cp.code } });
|
||||
}
|
||||
continue;
|
||||
},
|
||||
};
|
||||
this.postEvent(.{ .key = key });
|
||||
}
|
||||
} else {
|
||||
const b = buf[0];
|
||||
const key: terminal.Key = switch (b) {
|
||||
0x00 => .{ .cp = '@', .mod = .{ .ctrl = true } },
|
||||
0x08 => .{ .cp = terminal.Key.backspace },
|
||||
0x09 => .{ .cp = terminal.Key.tab },
|
||||
0x0a, 0x0d => .{ .cp = terminal.Key.enter },
|
||||
0x01...0x07, 0x0b...0x0c, 0x0e...0x1a => .{ .cp = b + 0x60, .mod = .{ .ctrl = true } },
|
||||
0x1b => escape: {
|
||||
std.debug.assert(read_bytes == 1);
|
||||
break :escape .{ .cp = terminal.Key.escape };
|
||||
},
|
||||
0x7f => .{ .cp = terminal.Key.backspace },
|
||||
else => {
|
||||
var iter = code_point.Iterator{ .bytes = buf[0..read_bytes] };
|
||||
while (iter.next()) |cp| {
|
||||
this.postEvent(.{ .key = .{ .cp = cp.code } });
|
||||
}
|
||||
continue;
|
||||
},
|
||||
};
|
||||
this.postEvent(.{ .key = key });
|
||||
}
|
||||
continue;
|
||||
};
|
||||
break;
|
||||
}
|
||||
this.postEvent(.quit);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user