///| 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 - 1, g.y - 38, g.w + 2, g.h + 39))) // 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 + 2, gb.h + 39)) } ///| 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)) } ///| Recording tests need ffmpeg (and ffprobe, from the same package) on PATH. fn need_ffmpeg() -> Unit raise { if !@sys.has_exe("ffmpeg") || !@sys.has_exe("ffprobe") { fail("recording tests need ffmpeg and ffprobe on PATH (apt install ffmpeg)") } } ///| Run a shell command to completion and give its stdout (via a file; `spawn` doesn't wait). fn run_capture(cmd : String, tag : String) -> String { let out = "/tmp/chandra-rec-" + tag + "-" + @sys.getpid().to_string() + ".txt" let done = out + ".done" ignore(@sys.spawn("(" + cmd + ") > '" + out + "' 2>&1; touch '" + done + "'", "")) let t0 = @sys.monotonic_ms() while @sys.read_file_bytes(done).length() == 0 && @sys.monotonic_ms() - t0 < 20000UL { @sys.sleep_ms(20) } let bytes = @sys.read_file_bytes(out) let text = @utf8.decode_lossy(bytes) @sys.unlink(out) @sys.unlink(done) text } ///| test "each recorded frame is a 42-byte Matroska cluster header, then the pixels" { let h = @host.mkv_frame_header(0x0102030405UL, 16) assert_eq(h.length(), 42) // Cluster, size 30 + 16. assert_eq(h[0:12].to_bytes(), b"\x1f\x43\xb6\x75\x01\x00\x00\x00\x00\x00\x00\x2e") // Timestamp (ms), 8 bytes. assert_eq(h[12:26].to_bytes(), b"\xe7\x01\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x01\x02") // SimpleBlock of 4 + 16 bytes: track 1, relative time 0, keyframe. assert_eq(h[26:42].to_bytes(), b"\x03\x04\x05\xa3\x01\x00\x00\x00\x00\x00\x00\x14\x81\x00\x00\x80") } ///| test "ffmpeg reads the recording stream with each frame's own time" { need_ffmpeg() let path = "/tmp/chandra-rec-stream-" + @sys.getpid().to_string() + ".mkv" let buf = @buffer.new() buf.write_bytes(@host.mkv_stream_header(4, 2)) // Frames drawn at uneven times, as an animation that stalls once. for t in [5000UL, 5016UL, 5033UL, 5150UL] { buf.write_bytes(@host.mkv_frame_header(t, 32)) for _ in 0..<8 { buf.write_bytes(b"\x10\x20\x30\xff") } } ignore(@sys.write_file_bytes(path, buf.to_bytes())) let out = run_capture( "ffprobe -v error -show_entries stream=codec_name,pix_fmt,width,height -show_entries frame=pts_time -of csv=p=0 '" + path + "'", "probe", ) @sys.unlink(path) assert_eq(out, "5.000000\n5.016000\n5.033000\n5.150000\nrawvideo,4,2,rgba\n") } ///| test "super+alt+r records the screen to a video with ffmpeg; the REC pill stops it" { need_ffmpeg() let (comp, dir) = shot_comp("rec") 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))) // 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() let mut i = 0 while @sys.monotonic_ms() - t0 < 1500UL { i = i + 1 comp.handle_input(@nested.InputEvent::PointerMove((300 + i % 400).to_double(), 300.0), 50U + i.reinterpret_as_uint()) comp.step() ignore(comp.compose_frame()) @sys.sleep_ms(5) } let frames = comp.recorded_frames() let elapsed = @sys.monotonic_ms() - t0 // Never more than 60 a second, and not far under it either. assert_true(frames.to_uint64() <= elapsed * 60UL / 1000UL + 2UL) assert_true(frames >= 20) // 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) // Constant 60 fps, as long as the recording ran (the frames carry their own times). let probe = run_capture( "ffprobe -v error -select_streams v -show_entries stream=r_frame_rate,duration -of csv=p=0 '" + path + "'", "dur", ) let parts = probe.trim_space().split(",").collect() assert_eq(parts[0].to_string(), "60/1") let dur_ms = (@string.parse_double(parts[1]) * 1000.0).to_int() assert_true(dur_ms > 1200 && dur_ms < 2200) } ///| test "super+shift+r records a region picked on the overlay" { need_ffmpeg() let (comp, _) = shot_comp("recregion") 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()) }