diff --git a/compositor/host/input_test.mbt b/compositor/host/input_test.mbt index 5f553a1..53138cf 100644 --- a/compositor/host/input_test.mbt +++ b/compositor/host/input_test.mbt @@ -1385,8 +1385,17 @@ test "compose_scene hands the host a scene only when something changed" { } None => abort("the first scene must be drawn") } - // Nothing happened: nothing to draw. - assert_true(comp.compose_scene() is None) + // Nothing happens: soon there is nothing to draw. (A startup notice may go away or a clock turn + // meanwhile; on a busy machine that takes a few scenes.) + let settles = fn() { + for _ in 0..<20 { + if comp.compose_scene() is None { + return true + } + } + false + } + assert_true(settles()) assert_false(comp.frame_changed) // A new window changes the scene, then it settles again. ignore(open_and_configure_client(comp, socket_path, "A", "a")) @@ -1394,7 +1403,7 @@ test "compose_scene hands the host a scene only when something changed" { Some(sg) => assert_eq(sg.windows.length(), 1) None => abort("a new window must change the scene") } - assert_true(comp.compose_scene() is None) + assert_true(settles()) // The switcher overlay is part of the scene too. comp.handle_input(@nested.InputEvent::Key(56U, true), 1U) ignore(open_and_configure_client(comp, socket_path, "B", "b")) diff --git a/compositor/host/launch_test.mbt b/compositor/host/launch_test.mbt index f75e427..fbc1ebb 100644 --- a/compositor/host/launch_test.mbt +++ b/compositor/host/launch_test.mbt @@ -17,6 +17,7 @@ fn wait_notice(comp : @host.ChandraCompositor, ms : Int) -> Array[String] { ///| test "a launch that fails shows the command, the exit status and what it printed" { let comp = new_comp("launch-fail") + comp.shell.apps.clear() // an installed Kwi would be called by its name // What V's Flatpak kwi did: bwrap can't set up the sandbox, prints why and exits 1. let cmd = "echo starting; for i in 1 2 3; do echo \"bwrap: Can't find source path /run/user/1000/doc/by-app/org.kde.kwi: No such file or directory\" >&2; done; exit 1" assert_true(comp.shell.start_app("org.kde.kwi", cmd)) diff --git a/compositor/host/recorder.mbt b/compositor/host/recorder.mbt index 253128d..84c90ca 100644 --- a/compositor/host/recorder.mbt +++ b/compositor/host/recorder.mbt @@ -36,8 +36,9 @@ pub(all) struct Recording { /// itself; a host with a frame recorder keeps its own). mut pending : Bytes mut sent : Int - /// When the last frame was taken, and the pointer position drawn into it. - mut last_grab : UInt64 + /// When the next frame is due (monotonic microseconds), and the pointer position drawn into the + /// last one. + mut due_us : UInt64 mut grab_px : Int mut grab_py : Int /// A drawn frame not recorded yet (too soon after the last one, or the queue was full). @@ -47,11 +48,39 @@ pub(all) struct Recording { mut frames : Int } -///| Frames a second recorded at most, and the gap that makes. +///| Frames a second recorded at most. let rec_fps : Int = 60 +///| How early a frame may come, in microseconds: a screen's frames at 60 Hz jitter by a few ms. +let rec_early_us : UInt64 = 4667UL + +///| When the next frame is due after one taken at `now` (microseconds) that was itself due at `due`: +/// a 60th of a second on. One taken early doesn't bring the next forward, so the average stays +/// within `rec_fps`; one taken late starts the count over, so a stall isn't made up in a burst. +fn rec_next_due(due : UInt64, now : UInt64) -> UInt64 { + (if due > now { due } else { now }) + 1000000UL / rec_fps.to_uint64() +} + ///| -let rec_gap_ms : UInt64 = 12UL +test "recording takes at most 60 frames a second, and all of a jittery 60 Hz screen's" { + let run = fn(times : Array[UInt64]) { + let mut due = 0UL + let mut n = 0 + for t in times { + if t + rec_early_us >= due { + due = rec_next_due(due, t) + n = n + 1 + } + } + n + } + // A changed picture every 5 ms for ten seconds. + let n = run(Array::makei(2000, i => 1000000UL + i.to_uint64() * 5000UL)) + assert_true(n >= 595 && n <= 601) + // A 60 Hz screen, every other frame 3 ms late: none is dropped. + let jittery = Array::makei(600, i => 1000000UL + i.to_uint64() * 16667UL + (if i % 2 == 1 { 3000UL } else { 0UL })) + assert_eq(run(jittery), 600) +} ///| Screen recording done by the host that draws the screen: `grab` copies a rectangle of the /// screen (with the pointer at a screen position when asked) into the host's queue, stamped with @@ -191,7 +220,7 @@ fn ChandraCompositor::start_recording(self : ChandraCompositor, r : @scene.Rect) header_sent: 0, pending: Bytes::from_array([]), sent: 0, - last_grab: 0UL, + due_us: 0UL, grab_px: -1, grab_py: -1, dirty: true, @@ -218,7 +247,7 @@ fn ChandraCompositor::end_recording(self : ChandraCompositor, error : String?) - if streaming && error is None { // One more frame with the time now, so the last picture lasts until the stop. rec.dirty = true - rec.last_grab = 0UL + rec.due_us = 0UL self.take_recording_frame(rec, @sys.monotonic_ms()) match self.frame_recorder { Some(host) => @@ -379,11 +408,11 @@ pub fn ChandraCompositor::recording_frame_drawn(self : ChandraCompositor) -> Uni } } -///| Take the screen's current frame into the recording, unless the last one was taken less than -/// a 60th of a second ago or the previous frame is still going out (it stays `dirty` then, and +///| Take the screen's current frame into the recording, unless it would make more than +/// `rec_fps` a second or the previous frame is still going out (it stays `dirty` then, and /// `recording_tick` takes it as soon as it can). fn ChandraCompositor::take_recording_frame(self : ChandraCompositor, rec : Recording, now : UInt64) -> Unit { - if rec.last_grab > 0UL && now - rec.last_grab < rec_gap_ms { + if now * 1000UL + rec_early_us < rec.due_us { return } let px = self.ptr_x.to_int() @@ -413,7 +442,7 @@ fn ChandraCompositor::take_recording_frame(self : ChandraCompositor, rec : Recor } if taken { rec.dirty = false - rec.last_grab = now + rec.due_us = rec_next_due(rec.due_us, now * 1000UL) rec.grab_px = px rec.grab_py = py rec.frames = rec.frames + 1 diff --git a/compositor/host/screenshot_test.mbt b/compositor/host/screenshot_test.mbt index 216d1f6..cb28f92 100644 --- a/compositor/host/screenshot_test.mbt +++ b/compositor/host/screenshot_test.mbt @@ -224,6 +224,14 @@ test "super+alt+r records the screen to a video with ffmpeg; the REC pill stops chord(comp, [125U, 56U], 64U | 8U, 19U, 1U) assert_true(comp.recording()) assert_eq(comp.recording_rect(), Some(@scene.Rect::new(0, 0, 1280, 720))) + // ffmpeg starts first: until it listens no frame is taken. + let t_start = @sys.monotonic_ms() + while comp.recorded_frames() == 0 && @sys.monotonic_ms() - t_start < 9000UL { + comp.step() + ignore(comp.compose_frame()) + @sys.sleep_ms(5) + } + assert_true(comp.recorded_frames() > 0) // Run the loop for a little over a second, the pointer moving all the while: each step a // changed picture, so frames are taken as often as the recorder allows. let t0 = @sys.monotonic_ms() diff --git a/compositor/shell/widgets.mbt b/compositor/shell/widgets.mbt index e568669..fa08480 100644 --- a/compositor/shell/widgets.mbt +++ b/compositor/shell/widgets.mbt @@ -240,6 +240,8 @@ fn DesktopShell::panel_slots(self : DesktopShell) -> Array[@panel.SlotIn] { w.module_id == "rotate" || w.module_id == "osk") { 36 + } else if compact && w.module_id == "battery" { + 76 // the percentage, without the time left } else { base_w } diff --git a/lib/battery/battery.mbt b/lib/battery/battery.mbt index b309210..2e94425 100644 --- a/lib/battery/battery.mbt +++ b/lib/battery/battery.mbt @@ -38,7 +38,7 @@ pub let critical_percent : Int = 10 ///| pub fn info() -> (String, String, Int, Int, Int) { // id, name, preferred w, preferred h, min w - ("org.chandra.battery", "Battery", 76, 34, 34) + ("org.chandra.battery", "Battery", 104, 34, 34) } ///|