Skip to main content

tilo_process_services/
capture.rs

1//! Screen capture, as the process provides it.
2//!
3//! An app asks [`crate::process::ProcessServices::screen_capture`] for a stream and
4//! drains the returned channel once per frame; the UI thread never blocks
5//! while the process shows its source picker. The process mediates the permission
6//! prompt and never invents a stream: without a grant the answer is
7//! [`ScreenCaptureOutcome::Cancelled`] or [`ScreenCaptureOutcome::Unavailable`].
8
9use std::sync::mpsc::Receiver;
10
11/// What the app would like the user to choose from.
12#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13pub struct ScreenCaptureRequest {
14    /// Offer whole monitors.
15    pub monitors: bool,
16    /// Offer individual windows.
17    pub windows: bool,
18    /// Draw the pointer into the captured frames.
19    pub embed_cursor: bool,
20}
21
22impl Default for ScreenCaptureRequest {
23    fn default() -> Self {
24        Self {
25            monitors: true,
26            windows: true,
27            embed_cursor: true,
28        }
29    }
30}
31
32/// Where the granted frames can be read from.
33#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34#[non_exhaustive]
35pub enum CaptureStream {
36    /// A `PipeWire` node to open (for example with `pipewiresrc`).
37    PipeWire { node_id: u32 },
38}
39
40/// A stream the user granted.
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42pub struct ScreenCaptureGrant {
43    pub stream: CaptureStream,
44    /// The source's size in pixels, when the process reports it.
45    pub size: Option<(u32, u32)>,
46}
47
48/// The result of a screen-capture request.
49#[derive(Clone, Debug, PartialEq, Eq)]
50pub enum ScreenCaptureOutcome {
51    Granted(ScreenCaptureGrant),
52    /// The user dismissed the picker.
53    Cancelled,
54    /// The process cannot capture the screen, or the request failed. Carries a
55    /// human-readable reason.
56    Unavailable(String),
57}
58
59/// A pending screen-capture request: the receiver the UI thread drains each
60/// frame.
61pub type ScreenCaptureChannel = Receiver<ScreenCaptureOutcome>;