hex/crates/capture/src/win/helper.rs
Kit Langton fb617e5082 Import scrawl-capture into crates/capture
Copy Scrawl's `crates/capture` from kitlangton/scrawl, branch
`engine/split`, commit e351ddb28be47c77481cb6821cc72d7f8fe7c769 (e351ddb),
without history, keeping the package name `scrawl-capture`, and its
`docs/app-handover-spec.md` as `docs/plans/capture-handover-spec.md`. Hex
depends on it by path instead of the Git pin at 86d7d6c; the Nix
`outputHashes."scrawl-capture-0.3.13"` entry goes.

Workspace: `crates/capture` is in `[workspace] exclude`, so `cargo build`,
`cargo test`, and `--workspace` never build it (Linux, Windows, and Nix
included). It compiles only as the `capture` feature's macOS dependency,
sharing the root Cargo.lock and target/. The root package stays a member
without being listed, since listing "." defeats `exclude`. On macOS
`scripts/check` and the macOS workflow name it (`-p voice-control
-p scrawl-capture --features capture`) to lint and test its 257 tests.

API adaptations in capture_host:
- `Finished` gains `gaps` and `missing_transcript`; the host records them
  and `hex capture status` (human and JSON `last`) shows them.
- `Engine::new` bridges the crate's log to `Host::log` when no sink is
  set, so Hex's own `TracingLog` sink goes and each line is logged once.
- Scrawl now writes its bundle files 0600; Hex still tightens
  recording.mp4, the window log, and folders.
- Nothing on Hex's path reads ~/.config/scrawl or writes under
  Application Support/scrawl: the crate does that only after
  `enable_scrawl_host_defaults`/`enable_file_log`, which Hex never calls.

Scrubbed (synthetic values): a file:///Users/<name> path in ax.rs, the
com.kitlangton.UpDown / dev.kitlangton.UpDown test bundle IDs in
handoff.rs and phone.rs (now com.example.UpDown), and a personal device
name in a phone.rs test. Kept: Scrawl's own product IDs
(dev.kitlangton.scrawl, .helper) and its public GitHub releases URL.

fmt and clippy -D warnings, no behaviour change: `cargo fmt` on five
files; `chunks_exact(N)` to `as_chunks::<N>()` (19 sites); collapsed
`if let` chains in capture/macos.rs and pipeline.rs and a `match` in
page.rs; `type` aliases for a drag start and two ingest hooks; explicit
lower bounds on two test `match` range arms in keyframes.rs;
`slice::from_ref` for two one-element slices; `and_then` for
`map().flatten()`; a dropped `to_owned`; `#[allow]` for one
`too_many_arguments` and one `single_range_in_vec_init`.
2026-10-25 19:14:23 -07:00

133 lines
4.3 KiB
Rust

//! The Windows side of `scrawl-helper`: the same calls as `src/helper.rs`, answered in process.
//! Movies go through Media Foundation (`win::media`), transcription through `transcribe.cpp`
//! with Hex's model or Scrawl's own copy of it (`win::speech`), and permissions through the privacy settings
//! (`win::privacy`). There is no cabled-iPhone mirror on Windows.
use std::path::{Path, PathBuf};
use anyhow::{Result, bail};
use serde::Deserialize;
use crate::model::{RgbaFrame, TranscriptData};
/// There is no helper executable on Windows; its work happens in process.
pub fn path() -> Result<PathBuf> {
bail!("scrawl-helper is macOS-only")
}
pub fn ui_path() -> Result<PathBuf> {
bail!("scrawl-ui-helper is macOS-only")
}
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct PhoneDevice {
pub id: String,
pub name: String,
pub model: String,
pub width: u32,
pub height: u32,
}
/// What Windows lets Scrawl use: the microphone and camera privacy switches. Screen capture,
/// input monitoring and pasting need no permission.
pub fn permissions()
-> Result<std::collections::BTreeMap<crate::permissions::Permission, crate::permissions::Status>> {
Ok(crate::win::privacy::permissions())
}
/// Cabled iPhones need AVFoundation's screen capture devices, which only macOS has.
pub fn devices() -> Result<Vec<PhoneDevice>> {
Ok(Vec::new())
}
/// One line of macOS's `scrawl-helper speech-model [--install]`.
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
pub struct SpeechModelLine {
pub status: String,
#[serde(default)]
pub engine: Option<String>,
#[serde(default)]
pub locale: Option<String>,
#[serde(default)]
pub locales: Option<Vec<String>>,
#[serde(default)]
pub progress: Option<f64>,
#[serde(default)]
pub error: Option<String>,
}
type InstallSpeechFn = fn(&mut dyn FnMut(SpeechModelLine)) -> Result<()>;
type TranscribeSpeechFn = fn(&Path) -> Result<TranscriptData>;
static SPEECH_HOOKS: std::sync::OnceLock<(InstallSpeechFn, TranscribeSpeechFn)> =
std::sync::OnceLock::new();
pub fn register_speech_hooks(install: InstallSpeechFn, transcribe: TranscribeSpeechFn) {
let _ = SPEECH_HOOKS.set((install, transcribe));
}
/// Windows transcribes with Parakeet (`win::speech`): Hex's installed model, else Scrawl's own
/// copy, downloaded and checked here.
pub fn install_speech_model(
_stop: &dyn Fn() -> bool,
mut line: impl FnMut(SpeechModelLine),
) -> Result<()> {
let Some((install, _)) = SPEECH_HOOKS.get() else {
bail!("Windows Parakeet speech model installer is not registered");
};
install(&mut line)
}
/// On-device transcription with measured word timings (Parakeet through `transcribe.cpp`).
pub fn transcribe(audio: &Path) -> Result<TranscriptData> {
let Some((_, transcribe_fn)) = SPEECH_HOOKS.get() else {
bail!("Windows Parakeet transcriber is not registered");
};
transcribe_fn(audio)
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Probe {
pub duration_ms: u64,
#[serde(default)]
pub width: u32,
#[serde(default)]
pub height: u32,
#[serde(default)]
pub audio_tracks: u32,
/// The video track's nominal frame rate (0 when unknown).
#[serde(default)]
pub fps: f64,
}
pub fn probe(movie: &Path) -> Result<Probe> {
crate::win::media::probe(movie)
}
/// Re-encodes `movie` into `out` as H.264 with its long edge at most `max_edge`.
pub fn shrink(movie: &Path, out: &Path, max_edge: u32) -> Result<crate::retention::Shrunk> {
crate::win::media::shrink(movie, out, max_edge)
}
/// Mixes a movie's audio to 16 kHz mono WAV, aligned to the movie's start.
pub fn export_audio(movie: &Path, wav: &Path) -> Result<()> {
crate::win::media::export_audio(movie, wav)
}
pub enum FrameTimes<'a> {
/// Nearest decodable frame every N ms, stamped with its actual time.
Every(u64),
/// Exactly these times.
At(&'a [u64]),
}
/// Decodes movie frames into memory; `max_edge` 0 keeps native size.
pub fn frames(movie: &Path, times: FrameTimes, max_edge: u32) -> Result<Vec<RgbaFrame>> {
crate::win::media::frames(movie, times, max_edge)
}
/// Renders `text` as a compact Unicode half-block QR code for the terminal.
pub fn qr(_text: &str) -> Result<String> {
bail!("QR codes need CoreImage (macOS)")
}