diff options
Diffstat (limited to 'src/tty/tty.zig')
| -rw-r--r-- | src/tty/tty.zig | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 264cfbef..b7bb2d54 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -1521,8 +1521,10 @@ const Shell = struct { /// PARDES_TEST_CLOCK's virtual time (host_io.testClock), and where the /// running-out wait moves it: the core's next wake. test_clock: ?u64 = null, - /// The tick the last paced frame was due at (waitInput). + /// The tick the last paced frame was due at (waitInput), and whether + /// this wait read an input (its frame is drawn at once). pace_ns: u64 = 0, + input_seen: bool = false, wake_to: u64 = 0, fn of(ctx: ?*anyopaque) *Shell { @@ -1574,7 +1576,6 @@ const Shell = struct { fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { const s = of(ctx); - var batch: usize = 0; // The wait is the 9P connections' turn with the core. A timeout (the // core's next wake) arrives through the same queue (`wakeWatch`). // While anything moves, a frame a tick (animation.frame_ns) on a grid @@ -1583,14 +1584,32 @@ const Shell = struct { // ends the wait at once and its frame is drawn then; the ticks after // it run from it. var wait = timeout_ms; - if (s.test_clock == null and s.core.continuous()) { + const paced = s.test_clock == null and s.core.continuous(); + var tick: u64 = 0; + if (paced) { const period = pardes.animation.frame_ns; const now: u64 = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); - var tick = if (s.pace_ns != 0 and now -| s.pace_ns < 2 * period) s.pace_ns + period else now; + tick = if (s.pace_ns != 0 and now -| s.pace_ns < 2 * period) s.pace_ns + period else now; if (tick < now) tick += (now - tick) / period * period; s.pace_ns = tick; wait = @intCast(@max(1, (tick -| now) / std.time.ns_per_ms)); } + s.input_seen = false; + // Paced, what is not an input (a frame's ack, a pty's output) is + // taken in and the wait goes on to the tick: the acks alone ran the + // loop at the terminal's round trip. + while (true) { + s.waitOnce(wait); + if (!paced or s.input_seen or s.core.quit) break; + const now: u64 = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); + if (now + std.time.ns_per_ms > tick) break; + wait = @intCast((tick - now) / std.time.ns_per_ms); + } + if (paced and s.input_seen) s.pace_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); + } + + fn waitOnce(s: *Shell, wait: u32) void { + var batch: usize = 0; if (wait != 0) { s.wake_to = s.core.nextWake() orelse 0; requestWake(s.io, wait); @@ -1638,8 +1657,15 @@ const Shell = struct { // A key's or a click's time, for its latency to the frame that // answers it (FrameStats): when the loop takes it in. switch (event) { - .key_press => core.frames.input(pardes.FrameStats.nowNs()), - .mouse => |m| if (m.type == .press) core.frames.input(pardes.FrameStats.nowNs()), + .key_press => { + core.frames.input(pardes.FrameStats.nowNs()); + s.input_seen = true; + }, + .mouse => |m| if (m.type == .press) { + core.frames.input(pardes.FrameStats.nowNs()); + s.input_seen = true; + }, + .paste, .paste_start, .winsize => s.input_seen = true, else => {}, } switch (event) { |
