Files
chandra/compositor/host/screenshot_test.mbt
T
Claude 85f6ff42f4
CI / test (push) Failing after 16s
Screenshot module after Spectacle (Super+Shift+S), with a screen recorder
- Super+Shift+S (or Super+Shift+Print) freezes the screen under a selection overlay, like
  Spectacle's Rectangular Region: drag a rectangle (size readout under it, magnifier while
  drawing), drag inside to move it, drag its handles to resize, or click a window to select it.
  Enter or a double-click takes it, Escape or right-click cancels; F full screen, W active window,
  arrows nudge (Shift+arrows resize). Print takes the full screen, Super+Print the active window,
  Super+Ctrl+Print the window under the pointer.
- Saved as ~/Pictures/Screenshots/Screenshot_YYYYMMDD_HHMMSS.png (XDG_PICTURES_DIR honoured),
  put on the clipboard as image/png (compositor-owned wl_data selection, written to the paste
  pipe without blocking), and confirmed by a notification with a thumbnail.
- Screen recording: Super+Shift+R picks a region on the same overlay, Super+Alt+R the screen,
  Super+Ctrl+R a window; the same shortcut or the REC pill (top centre) stops. Raw frames
  (<= 30 fps, wall-clock stamped) stream into ffmpeg over a unix socket; it writes H.264 MP4 to
  ~/Videos/Screencasts/Screencast_*.mp4. The pointer is drawn in unless switched off.
- renderer: encode_png_compressed (per-row filters + LZ77/fixed-Huffman deflate): a 1280x720
  desktop is ~90 KB instead of 3.7 MB.
- New 'screenshot' capability and 'screenshot' panel module (in the default panel): Spectacle's
  modes and the recording modes with their shortcuts, and a pointer switch; the button becomes
  Stop while recording. Served by both hosts (wasmer stub request 12 / host int 4; host-wasm
  publish-screenshot and a screenshot request). Panel requests wait for a frame without the
  module's popup.
- sys: pwrite with a negative offset is a non-blocking write (pipes, sockets), SIGPIPE ignored.
- Tests: native overlay, shortcuts, window pick, module, recording (with ffmpeg); host-wasm
  module test; renderer compression test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hjMGSLc5tHuzjsxGVa
2026-11-25 19:07:23 +00:00

229 lines
8.9 KiB
MoonBit

///| Screenshots (Super+Shift+S overlay, Print shortcuts, the panel module's requests) and screen
/// recording.
///| A fresh compositor whose pictures and videos go to a temporary folder.
fn shot_comp(tag : String) -> (@host.ChandraCompositor, String) {
let comp = new_comp("shot-" + tag)
let dir = "/tmp/chandra-shot-" + tag + "-" + @sys.getpid().to_string()
comp.shot_dir = dir
(comp, dir)
}
///| Press or release a key.
fn key(comp : @host.ChandraCompositor, code : UInt, down : Bool, t : UInt) -> Unit {
comp.handle_input(@nested.InputEvent::Key(code, down), t)
}
///| Press `code` with modifier keys held (evdev codes) and the matching xkb mask, then let go.
fn chord(comp : @host.ChandraCompositor, mods : Array[UInt], mask : UInt, code : UInt, t : UInt) -> Unit {
for m in mods {
key(comp, m, true, t)
}
comp.handle_input(@nested.InputEvent::Modifiers(mask, 0U, 0U, 0U), t)
key(comp, code, true, t + 1U)
key(comp, code, false, t + 2U)
for m in mods {
key(comp, m, false, t + 3U)
}
comp.handle_input(@nested.InputEvent::Modifiers(0U, 0U, 0U, 0U), t + 3U)
}
///| Width and height in a PNG's IHDR.
fn png_size(png : Bytes) -> (Int, Int) {
let be = fn(o : Int) {
(png[o].to_int() << 24) | (png[o + 1].to_int() << 16) | (png[o + 2].to_int() << 8) | png[o + 3].to_int()
}
(be(16), be(20))
}
///| Drag with the left button from (x0, y0) to (x1, y1).
fn drag(comp : @host.ChandraCompositor, x0 : Int, y0 : Int, x1 : Int, y1 : Int, t : UInt) -> Unit {
comp.handle_input(@nested.InputEvent::PointerMove(x0.to_double(), y0.to_double()), t)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), t + 1U)
comp.handle_input(@nested.InputEvent::PointerMove(((x0 + x1) / 2).to_double(), ((y0 + y1) / 2).to_double()), t + 2U)
comp.handle_input(@nested.InputEvent::PointerMove(x1.to_double(), y1.to_double()), t + 3U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), t + 4U)
}
///|
test "super+shift+s: drag a region, Enter saves it as a PNG and puts it on the clipboard" {
let (comp, dir) = shot_comp("region")
// Super+Shift+S opens the overlay; nothing is saved yet and the launcher stays shut.
chord(comp, [125U, 42U], 64U | 1U, 31U, 1U)
assert_true(comp.screenshot_active())
assert_false(comp.shell.popup_open())
assert_eq(comp.last_screenshot(), "")
ignore(comp.compose_frame())
// Drag out 300 x 200 at (100, 80): the selection follows (either way round).
drag(comp, 400, 280, 100, 80, 10U)
assert_eq(comp.screenshot_selection(), Some(@scene.Rect::new(100, 80, 300, 200)))
ignore(comp.compose_frame())
// Drag inside it to move it by (20, 10).
drag(comp, 200, 150, 220, 160, 20U)
assert_eq(comp.screenshot_selection(), Some(@scene.Rect::new(120, 90, 300, 200)))
// The bottom-right handle resizes it.
drag(comp, 420, 290, 440, 300, 30U)
assert_eq(comp.screenshot_selection(), Some(@scene.Rect::new(120, 90, 320, 210)))
// Shift+Right grows it a pixel.
comp.handle_input(@nested.InputEvent::Modifiers(1U, 0U, 0U, 0U), 40U)
key(comp, 106U, true, 41U)
key(comp, 106U, false, 42U)
comp.handle_input(@nested.InputEvent::Modifiers(0U, 0U, 0U, 0U), 43U)
assert_eq(comp.screenshot_selection(), Some(@scene.Rect::new(120, 90, 321, 210)))
ignore(comp.compose_frame())
// Enter takes it.
key(comp, 28U, true, 50U)
key(comp, 28U, false, 51U)
assert_false(comp.screenshot_active())
let path = comp.last_screenshot()
assert_true(path.has_prefix(dir + "/Screenshot_"))
assert_true(path.has_suffix(".png"))
let png = @sys.read_file_bytes(path)
assert_eq(png[1], b'P')
assert_eq(png_size(png), (321, 210))
assert_true(comp.server.owns_selection())
assert_true(comp.screenshot_toast_shown())
ignore(comp.compose_frame())
}
///|
test "print takes the full screen at once; a second one gets its own name" {
let (comp, _) = shot_comp("full")
key(comp, 99U, true, 1U)
key(comp, 99U, false, 2U)
assert_false(comp.screenshot_active())
let first = comp.last_screenshot()
assert_eq(png_size(@sys.read_file_bytes(first)), (1280, 720))
key(comp, 99U, true, 3U)
key(comp, 99U, false, 4U)
let second = comp.last_screenshot()
assert_true(second.length() > 0 && second != first)
}
///|
test "escape cancels the overlay; a click on a window selects it; W takes the active window" {
let socket_path = "/tmp/wayland-moon-shotwin-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
comp.shot_dir = "/tmp/chandra-shot-win-" + @sys.getpid().to_string()
ignore(open_and_configure_client(comp, socket_path, "A", "a"))
ignore(open_and_configure_client(comp, socket_path, "B", "b"))
ignore(comp.compose_frame())
chord(comp, [125U, 42U], 65U, 31U, 1U)
assert_true(comp.screenshot_active())
key(comp, 1U, true, 5U)
key(comp, 1U, false, 6U)
assert_false(comp.screenshot_active())
assert_eq(comp.last_screenshot(), "")
// A click (no drag) inside window 1 selects it with its title bar and borders.
let g = comp.windows.get(1UL).unwrap().geometry
chord(comp, [125U, 42U], 65U, 31U, 10U)
click_at(comp, g.x + 20, g.y + 20, 20U)
assert_eq(comp.screenshot_selection(), Some(@scene.Rect::new(g.x - 2, g.y - 32, g.w + 4, g.h + 34)))
// W takes the active window (B) instead.
key(comp, 17U, true, 30U)
key(comp, 17U, false, 31U)
let gb = comp.windows.get(2UL).unwrap().geometry
assert_eq(png_size(@sys.read_file_bytes(comp.last_screenshot())), (gb.w + 4, gb.h + 34))
}
///|
test "the screenshot module's request waits for a frame, then opens the overlay" {
let (comp, _) = shot_comp("request")
comp.screenshot_request(0)
comp.step()
assert_false(comp.screenshot_active()) // no frame drawn since the request yet
ignore(comp.compose_frame())
comp.step()
assert_true(comp.screenshot_active())
comp.cancel_screenshot()
comp.screenshot_request(1)
ignore(comp.compose_frame())
comp.step()
assert_eq(png_size(@sys.read_file_bytes(comp.last_screenshot())), (1280, 720))
}
///|
test "super+alt+r records the screen to a video with ffmpeg; the REC pill stops it" {
let (comp, dir) = shot_comp("rec")
chord(comp, [125U, 56U], 64U | 8U, 19U, 1U)
if !@sys.has_exe("ffmpeg") {
assert_false(comp.recording())
assert_true(comp.screenshot_toast_shown())
return
}
assert_true(comp.recording())
assert_eq(comp.recording_rect(), Some(@scene.Rect::new(0, 0, 1280, 720)))
// Run the loop for a little over a second.
let t0 = @sys.monotonic_ms()
while @sys.monotonic_ms() - t0 < 1500UL {
comp.step()
ignore(comp.compose_frame())
@sys.sleep_ms(10)
}
assert_true(comp.recorded_frames() >= 5)
// A click on the pill stops it; ffmpeg then finishes the file.
click_at(comp, 640, 15, 100U)
assert_false(comp.recording())
let path = comp.last_recording()
assert_true(path.has_prefix(dir + "/Screencast_"))
let mut size = 0
let t1 = @sys.monotonic_ms()
while @sys.monotonic_ms() - t1 < 10000UL {
size = @sys.read_file_bytes(path).length()
if size > 0 {
@sys.sleep_ms(300) // let ffmpeg finish writing
size = @sys.read_file_bytes(path).length()
break
}
@sys.sleep_ms(50)
}
assert_true(size > 1000)
}
///|
test "super+shift+r records a region picked on the overlay" {
let (comp, _) = shot_comp("recregion")
if !@sys.has_exe("ffmpeg") {
return
}
chord(comp, [125U, 42U], 65U, 19U, 1U)
assert_true(comp.screenshot_active())
drag(comp, 100, 100, 301, 251, 10U)
key(comp, 28U, true, 20U)
key(comp, 28U, false, 21U)
assert_false(comp.screenshot_active())
// Made even-sized for the encoder.
assert_eq(comp.recording_rect(), Some(@scene.Rect::new(100, 100, 200, 150)))
// The same shortcut again stops it.
chord(comp, [125U, 42U], 65U, 19U, 30U)
assert_false(comp.recording())
}
///|
test "the screenshot panel module takes a full-screen shot without its popup in it" {
let (comp, _) = shot_comp("module")
let slot = comp.shell.slot_of("screenshot")
assert_true(comp.shell.slot_rect(slot).w > 0) // it is in the default panel
click_module(comp, "screenshot", 1U)
let pr = comp.shell.slot_popup_rect(slot)
assert_true(pr.h >= 6 * 2 + 10 * 30)
// Row 2: Full Screen. The popup closes; the picture is taken after the next frame.
click_at(comp, pr.x + 40, pr.y + 6 + 2 * 30 + 5, 5U)
assert_eq(comp.shell.slot_popup_rect(slot).w, 0)
comp.step()
assert_eq(png_size(@sys.read_file_bytes(comp.last_screenshot())), (1280, 720))
// Row 1: Rectangular Region opens the overlay.
click_module(comp, "screenshot", 10U)
click_at(comp, pr.x + 40, pr.y + 6 + 1 * 30 + 5, 15U)
comp.step()
assert_true(comp.screenshot_active())
key(comp, 1U, true, 20U)
key(comp, 1U, false, 21U)
assert_false(comp.screenshot_active())
// The pointer switch (last row) flips whether recordings show the pointer.
assert_true(comp.record_pointer())
click_module(comp, "screenshot", 30U)
click_at(comp, pr.x + 40, pr.y + 6 + 9 * 30 + 5, 35U)
assert_false(comp.record_pointer())
}