Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(platform-linux): remove zero-caller input, portal screencast, sway and overlay functions (#5958)

injaneity1ba0facdd95e
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/input/mod.rs b/libs/cua-driver/rust/crates/platform-linux/src/input/mod.rs
index f382aee62f..bdf0a5b185 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/input/mod.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/input/mod.rs
@@ -3393,107 +3393,6 @@ pub fn send_click_with_modifiers(
settle_synthetic_events(&conn);
Ok(())
}
-
-/// Send a press-drag-release gesture via XSendEvent (ButtonPress + MotionNotify steps + ButtonRelease).
-///
-/// `xid` — target window XID. `from_x/y`, `to_x/y` — window-local coords.
-/// `duration_ms` — total budget. `steps` — interpolated MotionNotify events.
-/// `button` — X11 button number (1=left, 2=middle, 3=right).
-pub fn send_drag(
- xid: u64,
- from: (i32, i32),
- to: (i32, i32),
- duration_ms: u64,
- steps: usize,
- button: u8,
-) -> Result<()> {
- let ((from_x, from_y), (to_x, to_y)) = (from, to);
- let (conn, _) = connect_x11_for_input()?;
- let root = conn.setup().roots[0].root;
- let steps = steps.max(1);
- let step_delay_ms = if steps > 1 {
- duration_ms / steps as u64
- } else {
- duration_ms
- };
- let press_target = resolve_event_target(&conn, xid, from_x, from_y)?;
-
- // ButtonPress at start.
- let press = ButtonPressEvent {
- response_type: BUTTON_PRESS_EVENT,
- detail: button,
- sequence: 0,
- time: x11rb::CURRENT_TIME,
- root,
- event: press_target.window,
- child: x11rb::NONE,
- root_x: press_target.root_x,
- root_y: press_target.root_y,
- event_x: press_target.local_x,
- event_y: press_target.local_y,
- state: KeyButMask::from(0u16),
- same_screen: true,
- };
- conn.send_event(false, press_target.window, EventMask::BUTTON_PRESS, press)?;
- conn.flush()?;
- sleep(Duration::from_millis(CLICK_DELAY_MS));
-
- // Interpolated MotionNotify steps.
- for i in 1..=steps {
- let t = i as f64 / steps as f64;
- let ix = from_x + ((to_x - from_x) as f64 * t).round() as i32;
- let iy = from_y + ((to_y - from_y) as f64 * t).round() as i32;
- let target = resolve_event_target(&conn, xid, ix, iy)?;
- let motion = MotionNotifyEvent {
- response_type: MOTION_NOTIFY_EVENT,
- detail: Motion::NORMAL,
- sequence: 0,
- time: x11rb::CURRENT_TIME,
- root,
- event: target.window,
- child: x11rb::NONE,
- root_x: target.root_x,
- root_y: target.root_y,
- event_x: target.local_x,
- event_y: target.local_y,
- state: button_state_mask(button),
- same_screen: true,
- };
- conn.send_event(false, target.window, EventMask::POINTER_MOTION, motion)?;
- conn.flush()?;
- if step_delay_ms > 0 {
- sleep(Duration::from_millis(step_delay_ms));
- }
- }
-
- // ButtonRelease at end.
- let release_target = resolve_event_target(&conn, xid, to_x, to_y)?;
- let release = ButtonReleaseEvent {
- response_type: BUTTON_RELEASE_EVENT,
- detail: button,
- sequence: 0,
- time: x11rb::CURRENT_TIME,
- root,
- event: release_target.window,
- child: x11rb::NONE,
- root_x: release_target.root_x,
- root_y: release_target.root_y,
- event_x: release_target.local_x,
- event_y: release_target.local_y,
- state: button_state_mask(button),
- same_screen: true,
- };
- conn.send_event(
- false,
- release_target.window,
- EventMask::BUTTON_RELEASE,
- release,
- )?;
- conn.flush()?;
- settle_synthetic_events(&conn);
- Ok(())
-}
-
pub fn send_button_down(xid: u64, x: i32, y: i32, button: u8) -> Result<()> {
let (conn, _) = connect_x11_for_input()?;
let root = conn.setup().roots[0].root;
@@ -4031,32 +3930,6 @@ pub fn send_scroll_xtest_desktop(x: i32, y: i32, direction: &str, amount: usize)
Ok(())
}
-/// Screen-absolute drag via XTest. The caller activates the target first; XTest
-/// then supplies one real press, interpolated pointer motion, and one release.
-/// This is the foreground counterpart to the window-addressed XSendEvent drag.
-pub fn send_drag_xtest_desktop(
- from_x: i32,
- from_y: i32,
- to_x: i32,
- to_y: i32,
- button: u8,
- duration_ms: u64,
- steps: usize,
-) -> Result<()> {
- send_drag_xtest_desktop_with_modifiers(
- from_x,
- from_y,
- to_x,
- to_y,
- button,
- duration_ms,
- steps,
- &[],
- )
-}
-
-/// Screen-absolute drag via XTest with physical modifier transitions around
-/// the complete press/motion/release gesture.
pub fn send_drag_xtest_desktop_with_modifiers(
from_x: i32,
from_y: i32,
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/overlay.rs b/libs/cua-driver/rust/crates/platform-linux/src/overlay.rs
index af7d31b44c..d0e4356105 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/overlay.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/overlay.rs
@@ -84,7 +84,9 @@ fn arrival_register(key: CursorKey, tx: tokio::sync::oneshot::Sender<()>) {
}
}
-fn arrival_fire(key: &CursorKey) {
+/// Arrival hook for the native Wayland renderer (`wayland::overlay`), which
+/// fires once the frame at the glide target is committed.
+pub(crate) fn arrival_fire(key: &CursorKey) {
if let Ok(mut guard) = ARRIVAL_TX.lock() {
if let Some(map) = guard.as_mut() {
if let Some(tx) = map.remove(key) {
@@ -95,20 +97,6 @@ fn arrival_fire(key: &CursorKey) {
}
}
-/// Arrival hook for the native Wayland renderer (`wayland::overlay`), which
-/// fires once the frame at the glide target is committed.
-#[cfg(target_os = "linux")]
-pub(crate) fn fire_arrival(key: &CursorKey) {
- arrival_fire(key);
-}
-
-/// Unblock every waiting pointer action; the renderer that would have fired
-/// their arrivals is gone.
-#[cfg(target_os = "linux")]
-pub(crate) fn release_arrivals() {
- release_all_arrivals();
-}
-
fn arrival_cancel(key: &CursorKey) {
if let Ok(mut guard) = ARRIVAL_TX.lock() {
if let Some(map) = guard.as_mut() {
@@ -117,7 +105,9 @@ fn arrival_cancel(key: &CursorKey) {
}
}
-fn release_all_arrivals() {
+/// Unblock every waiting pointer action; the renderer that would have fired
+/// their arrivals is gone.
+pub(crate) fn release_all_arrivals() {
if let Ok(mut guard) = ARRIVAL_TX.lock() {
clear_arrivals(&mut guard);
}
@@ -620,10 +610,6 @@ fn dispatch_shell_helper_command(key: &str, cmd: &OverlayCommand, semantic: bool
}
}
-pub fn is_enabled() -> bool {
- is_enabled_for("default")
-}
-
pub fn is_enabled_for(key: &str) -> bool {
RENDER
.lock()
@@ -650,10 +636,6 @@ pub fn is_visible_for_session(key: &str) -> bool {
.unwrap_or(false)
}
-pub fn current_position() -> Option<(f64, f64)> {
- current_position_for("default")
-}
-
pub fn current_position_for(key: &str) -> Option<(f64, f64)> {
RENDER.lock().ok().and_then(|g| {
g.as_ref()
@@ -706,10 +688,6 @@ fn seed_start_if_unplaced(key: &CursorKey, target_x: f64, target_y: f64) -> bool
map.seed_start_if_unplaced(key, target_x, target_y, Some(frame))
}
-pub async fn animate_cursor_to(x: f64, y: f64) {
- animate_cursor_to_for("default".to_owned(), x, y).await;
-}
-
pub async fn animate_cursor_to_for(key: CursorKey, x: f64, y: f64) {
animate_cursor_to_target_for(key, x, y, None).await;
}
@@ -3415,7 +3393,7 @@ mod tests {
fn wait_for_cursor_move_from(x: f64, y: f64, phase: &str) -> anyhow::Result<()> {
let deadline = Instant::now() + Duration::from_secs(3);
while Instant::now() < deadline {
- let position = current_position().expect("placed cursor");
+ let position = current_position_for("default").expect("placed cursor");
if (position.0 - x).hypot(position.1 - y) > 32.0 {
eprintln!(
"{phase}: cursor moved from ({x}, {y}) to ({}, {})",
@@ -3425,7 +3403,7 @@ mod tests {
}
std::thread::sleep(Duration::from_millis(10));
}
- let position = current_position().expect("placed cursor");
+ let position = current_position_for("default").expect("placed cursor");
anyhow::bail!(
"cursor position remained ({}, {}) near ({x}, {y}) during {phase}",
position.0,
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/wayland/mod.rs b/libs/cua-driver/rust/crates/platform-linux/src/wayland/mod.rs
index 22d76aac46..059d2a0608 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/wayland/mod.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/wayland/mod.rs
@@ -25,11 +25,9 @@ pub mod shell_helper;
pub mod sway_ipc;
mod virtual_keyboard;
// RemoteDesktop/libei input is portable and ships in release binaries.
-// PipeWire ScreenCast capture remains separately gated for modern/Nix builds.
+// PipeWire ScreenCast capture is no longer built here (the portal-capture feature is kept for downstream forwarding).
#[cfg(feature = "portal-input")]
pub mod libei;
-#[cfg(feature = "portal-capture")]
-pub mod portal_screencast;
/// Whether the GNOME/KDE RemoteDesktop + libei input backend is compiled in.
pub const PORTAL_INPUT_ENABLED: bool = cfg!(feature = "portal-input");
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/wayland/overlay.rs b/libs/cua-driver/rust/crates/platform-linux/src/wayland/overlay.rs
index d720fa8807..818fea8330 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/wayland/overlay.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/wayland/overlay.rs
@@ -130,7 +130,7 @@ pub fn ensure_started() -> bool {
}
// No frame will ever be painted again: unblock every pointer
// action still waiting for its glide to arrive.
- crate::overlay::release_arrivals();
+ crate::overlay::release_all_arrivals();
})
.expect("spawn cua-overlay-wl thread");
tx
@@ -787,7 +787,7 @@ fn owner_thread(rx: Receiver<WlOverlayCmd>) -> anyhow::Result<()> {
// a pointer action lands after the viewer saw the glide end (the X11
// renderer's contract).
for key in arrived {
- crate::overlay::fire_arrival(&key);
+ crate::overlay::arrival_fire(&key);
}
frame_tick_needed = next_frame_tick_needed;
}
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/wayland/portal_screencast.rs b/libs/cua-driver/rust/crates/platform-linux/src/wayland/portal_screencast.rs
deleted file mode 100644
index 14f1712dce..0000000000
--- a/libs/cua-driver/rust/crates/platform-linux/src/wayland/portal_screencast.rs
+++ /dev/null
@@ -1,465 +0,0 @@
-//! Per-window capture via `xdg-desktop-portal` ScreenCast + PipeWire.
-//!
-//! Reaches GNOME/Mutter and KDE/KWin per-window selection — the
-//! cross-DE complement to wlroots `ext-image-copy-capture-v1`. Sequence:
-//!
-//! 1. ashpd ScreenCast::create_session.
-//! 2. select_sources with types=Window|Monitor, cursor_mode=Embedded,
-//! persist_mode=Application (consent dialog fires ONCE per process,
-//! cached for the lifetime of the requesting binary).
-//! 3. start(session, parent_window) — user picks the source window in
-//! the portal's system dialog the first time; subsequent calls within
-//! the same process reuse the cached choice.
-//! 4. open_pipe_wire_remote returns a Unix file descriptor; Streams
-//! carries the published node ids.
-//! 5. Connect a pipewire client to the fd, open a Stream targeting the
-//! advertised node id, negotiate Video/Raw format, dequeue one frame
-//! with `STREAM_FLAG_MAP_BUFFERS`, copy pixels, PNG-encode, stop.
-//!
-//! The session + restore_token are persisted in a process-local
-//! `OnceLock<PortalScreencastSession>` so only the first call across the
-//! process lifetime triggers the consent dialog; later calls reuse the
-//! same PipeWire node id.
-
-use std::os::fd::AsRawFd;
-use std::sync::OnceLock;
-
-use ashpd::desktop::screencast::{
- CursorMode, OpenPipeWireRemoteOptions, Screencast, SelectSourcesOptions, SourceType,
- StartCastOptions, Streams,
-};
-use ashpd::desktop::{CreateSessionOptions, PersistMode, Session};
-use ashpd::enumflags2::BitFlags;
-
-/// A live portal ScreenCast session held across calls. Expensive to
-/// create (system consent dialog) and cheap to reuse — the OnceLock
-/// keeps it alive until the requesting process exits.
-struct PortalScreencastSession {
- _session: Session<Screencast>,
- streams: Streams,
- fd: std::os::fd::OwnedFd,
-}
-
-unsafe impl Send for PortalScreencastSession {}
-unsafe impl Sync for PortalScreencastSession {}
-
-static SESSION: OnceLock<PortalScreencastSession> = OnceLock::new();
-
-/// Capture the user-selected window via `xdg-desktop-portal` ScreenCast.
-/// On first invocation per process the portal dialog asks the user to
-/// pick a window or monitor; subsequent calls reuse the same node
-/// transparently. Returns PNG bytes of the latest frame on the stream.
-pub fn screenshot_window_via_portal() -> anyhow::Result<Vec<u8>> {
- let rt = tokio::runtime::Builder::new_current_thread()
- .enable_all()
- .build()
- .map_err(|e| anyhow::anyhow!("failed to build tokio runtime for ashpd: {e}"))?;
-
- if SESSION.get().is_none() {
- let sess = rt.block_on(create_portal_session())?;
- let _ = SESSION.set(sess);
- }
- let session = SESSION
- .get()
- .ok_or_else(|| anyhow::anyhow!("portal ScreenCast session unavailable"))?;
-
- let node_id = session
- .streams
- .streams()
- .iter()
- .next()
- .map(|s| s.pipe_wire_node_id())
- .ok_or_else(|| anyhow::anyhow!("portal advertised no streams"))?;
-
- capture_one_frame(session.fd.as_raw_fd(), node_id)
-}
-
-async fn create_portal_session() -> anyhow::Result<PortalScreencastSession> {
- let proxy = Screencast::new()
- .await
- .map_err(|e| anyhow::anyhow!("portal ScreenCast proxy unreachable: {e}. Install xdg-desktop-portal-gnome / xdg-desktop-portal-kde."))?;
-
- let session = proxy
- .create_session(CreateSessionOptions::default())
- .await
- .map_err(|e| anyhow::anyhow!("portal create_session failed: {e}"))?;
-
- let select_opts = SelectSourcesOptions::default()
- .set_sources(BitFlags::<SourceType>::from(SourceType::Window) | SourceType::Monitor)
- .set_cursor_mode(CursorMode::Embedded)
- .set_persist_mode(PersistMode::Application);
-
- proxy
- .select_sources(&session, select_opts)
- .await
- .map_err(|e| anyhow::anyhow!("portal select_sources failed: {e}"))?
- .response()
- .map_err(|e| anyhow::anyhow!("portal select_sources response error: {e}"))?;
-
- let streams = proxy
- .start(&session, None, StartCastOptions::default())
- .await
- .map_err(|e| anyhow::anyhow!("portal start failed: {e}"))?
- .response()
- .map_err(|e| anyhow::anyhow!("portal start response error (user denied?): {e}"))?;
-
- let fd = proxy
- .open_pipe_wire_remote(&session, OpenPipeWireRemoteOptions::default())
- .await
- .map_err(|e| anyhow::anyhow!("portal open_pipe_wire_remote failed: {e}"))?;
-
- Ok(PortalScreencastSession {
- _session: session,
- streams,
- fd,
- })
-}
-
-/// Pull one frame off the PipeWire stream advertised by the portal,
-/// encode it as PNG, and return the bytes.
-///
-/// Pipeline:
-/// 1. `pipewire::MainLoop` + `Context` + `Core::connect_fd(portal_fd)` —
-/// join the portal-published PipeWire remote on the same fd ashpd
-/// returned from `open_pipe_wire_remote`.
-/// 2. `Stream::new` with `media.type=Video, media.category=Capture,
-/// media.role=Screen` so PipeWire knows to route the cast properly.
-/// 3. Build an `EnumFormat` SPA pod listing the channel-orders we accept
-/// (BGRx, BGRA, RGBx, RGBA) at any size up to 8K — the compositor
-/// picks one and announces it via `param_changed`. We track w/h/fmt
-/// from that callback.
-/// 4. The `process` callback dequeues exactly one buffer, copies the
-/// pixels out into a shared `Arc<Mutex<...>>`, and quits the main
-/// loop. Subsequent buffers are dropped (queued back via Buffer::Drop)
-/// once `frame.is_some()`.
-/// 5. After `mainloop.run()` returns, channel-swap the captured bytes
-/// into RGBA and PNG-encode them via the `image` crate.
-fn capture_one_frame(pipewire_fd: i32, node_id: u32) -> anyhow::Result<Vec<u8>> {
- use std::os::fd::FromRawFd;
- use std::sync::{Arc, Mutex};
-
- use pipewire as pw;
- use pw::{properties::properties, spa};
- use spa::pod::Pod;
-
- pw::init();
-
- let mainloop = pw::main_loop::MainLoop::new(None)
- .map_err(|e| anyhow::anyhow!("pipewire MainLoop::new failed: {e}"))?;
- let context = pw::context::Context::new(&mainloop)
- .map_err(|e| anyhow::anyhow!("pipewire Context::new failed: {e}"))?;
-
- // dup the portal fd so the OwnedFd we hand to connect_fd has an
- // independent lifecycle from the PortalScreencastSession's master copy.
- let dup_fd = unsafe { libc::dup(pipewire_fd) };
- if dup_fd < 0 {
- anyhow::bail!(
- "dup of portal PipeWire fd failed: {}",
- std::io::Error::last_os_error()
- );
- }
- let owned_fd = unsafe { std::os::fd::OwnedFd::from_raw_fd(dup_fd) };
- let core = context
- .connect_fd(owned_fd, None)
- .map_err(|e| anyhow::anyhow!("pipewire connect_fd failed: {e}"))?;
-
- // Shared state populated by the param_changed + process callbacks.
- // FrameBuf carries the negotiated geometry alongside the pixel data so
- // the PNG encoder picks the right stride / channel order on exit.
- struct FrameBuf {
- bytes: Vec<u8>,
- width: u32,
- height: u32,
- stride: u32,
- format: spa::param::video::VideoFormat,
- }
- let frame: Arc<Mutex<Option<FrameBuf>>> = Arc::new(Mutex::new(None));
- let neg_format: Arc<Mutex<spa::param::video::VideoInfoRaw>> =
- Arc::new(Mutex::new(Default::default()));
-
- let stream = pw::stream::Stream::new(
- &core,
- "cua-driver-capture",
- properties! {
- *pw::keys::MEDIA_TYPE => "Video",
- *pw::keys::MEDIA_CATEGORY => "Capture",
- *pw::keys::MEDIA_ROLE => "Screen",
- },
- )
- .map_err(|e| anyhow::anyhow!("pipewire Stream::new failed: {e}"))?;
-
- let mainloop_for_process = mainloop.clone();
- let neg_format_for_param = neg_format.clone();
- let frame_for_process = frame.clone();
- let neg_format_for_process = neg_format.clone();
-
- let _listener = stream
- .add_local_listener::<()>()
- .param_changed(move |_stream, _user, id, param| {
- // The compositor confirms the negotiated VideoInfoRaw via the
- // Format param event right after stream.connect — capture it
- // so the process callback knows the width/height/format.
- let Some(param) = param else { return };
- if id != spa::param::ParamType::Format.as_raw() {
- return;
- }
- let (media_type, media_subtype) = match spa::param::format_utils::parse_format(param) {
- Ok(v) => v,
- Err(_) => return,
- };
- if media_type != spa::param::format::MediaType::Video
- || media_subtype != spa::param::format::MediaSubtype::Raw
- {
- return;
- }
- let mut info = spa::param::video::VideoInfoRaw::default();
- if info.parse(param).is_ok() {
- if let Ok(mut g) = neg_format_for_param.lock() {
- *g = info;
- }
- }
- })
- .process(move |stream, _user| {
- // Already captured? Drop further buffers (they'll be queued
- // back via Buffer::Drop) so the run loop can finish.
- if frame_for_process
- .lock()
- .ok()
- .map(|g| g.is_some())
- .unwrap_or(false)
- {
- let _ = stream.dequeue_buffer();
- return;
- }
- let mut buffer = match stream.dequeue_buffer() {
- Some(b) => b,
- None => return,
- };
- let datas = buffer.datas_mut();
- if datas.is_empty() {
- return;
- }
- let info = match neg_format_for_process.lock() {
- Ok(g) => *g,
- Err(_) => return,
- };
- let size_struct = info.size();
- let width = size_struct.width;
- let height = size_struct.height;
- if width == 0 || height == 0 {
- return;
- }
- let chunk_stride = datas[0].chunk().stride();
- let chunk_size = datas[0].chunk().size();
- // PipeWire sometimes leaves stride at 0 (when the producer
- // doesn't fill it in); fall back to width*4 for the packed
- // BGRx/BGRA/RGBx/RGBA formats we negotiate.
- let stride = if chunk_stride > 0 {
- chunk_stride as u32
- } else if chunk_size > 0 && height > 0 {
- chunk_size / height
- } else {
- width * 4
- };
- let payload = match datas[0].data() {
- Some(p) => p,
- None => return,
- };
- let payload_len = (stride as usize) * (height as usize);
- if payload.len() < payload_len {
- return;
- }
- let bytes = payload[..payload_len].to_vec();
- if let Ok(mut g) = frame_for_process.lock() {
- *g = Some(FrameBuf {
- bytes,
- width,
- height,
- stride,
- format: info.format(),
- });
- }
- // Quit the main loop so the caller can encode + return.
- mainloop_for_process.quit();
- })
- .register()
- .map_err(|e| anyhow::anyhow!("pipewire stream listener register failed: {e}"))?;
-
- // Build an EnumFormat SPA pod listing the formats + sizes we accept.
- // Compositors typically pick BGRx/BGRA on Linux desktops; we list a
- // few alternates so negotiation succeeds on more sources.
- let obj = pw::spa::pod::object!(
- pw::spa::utils::SpaTypes::ObjectParamFormat,
- pw::spa::param::ParamType::EnumFormat,
- pw::spa::pod::property!(
- pw::spa::param::format::FormatProperties::MediaType,
- Id,
- pw::spa::param::format::MediaType::Video
- ),
- pw::spa::pod::property!(
- pw::spa::param::format::FormatProperties::MediaSubtype,
- Id,
- pw::spa::param::format::MediaSubtype::Raw
- ),
- pw::spa::pod::property!(
- pw::spa::param::format::FormatProperties::VideoFormat,
- Choice,
- Enum,
- Id,
- pw::spa::param::video::VideoFormat::BGRx,
- pw::spa::param::video::VideoFormat::BGRx,
- pw::spa::param::video::VideoFormat::BGRA,
- pw::spa::param::video::VideoFormat::RGBx,
- pw::spa::param::video::VideoFormat::RGBA,
- ),
- pw::spa::pod::property!(
- pw::spa::param::format::FormatProperties::VideoSize,
- Choice,
- Range,
- Rectangle,
- pw::spa::utils::Rectangle {
- width: 1920,
- height: 1080
- },
- pw::spa::utils::Rectangle {
- width: 1,
- height: 1
- },
- pw::spa::utils::Rectangle {
- width: 7680,
- height: 4320
- }
- ),
- pw::spa::pod::property!(
- pw::spa::param::format::FormatProperties::VideoFramerate,
- Choice,
- Range,
- Fraction,
- pw::spa::utils::Fraction { num: 30, denom: 1 },
- pw::spa::utils::Fraction { num: 0, denom: 1 },
- pw::spa::utils::Fraction {
- num: 1000,
- denom: 1
- }
- ),
- );
- let values: Vec<u8> = pw::spa::pod::serialize::PodSerializer::serialize(
- std::io::Cursor::new(Vec::new()),
- &pw::spa::pod::Value::Object(obj),
- )
- .map_err(|e| anyhow::anyhow!("EnumFormat pod serialization failed: {e}"))?
- .0
- .into_inner();
- let mut params = [Pod::from_bytes(&values)
- .ok_or_else(|| anyhow::anyhow!("EnumFormat pod parse-back failed"))?];
-
- stream
- .connect(
- spa::utils::Direction::Input,
- Some(node_id),
- pw::stream::StreamFlags::AUTOCONNECT | pw::stream::StreamFlags::MAP_BUFFERS,
- &mut params,
- )
- .map_err(|e| anyhow::anyhow!("pipewire stream.connect failed: {e}"))?;
-
- // Install a 10-second timeout source so a producer that never delivers
- // a frame (eg the user closed the consent dialog mid-cast) doesn't
- // hang the main loop forever.
- let timeout_loop = mainloop.clone();
- let _timeout_source = mainloop
- .loop_()
- .add_timer(move |_expirations: u64| timeout_loop.quit());
- _timeout_source
- .update_timer(Some(std::time::Duration::from_secs(10)), None)
- .into_result()
- .map_err(|e| anyhow::anyhow!("pipewire timeout source failed: {e:?}"))?;
-
- mainloop.run();
-
- // Tear down the stream before we read the frame, so the producer
- // can't keep mutating the buffer underneath us.
- drop(_listener);
- drop(stream);
-
- let frame = frame
- .lock()
- .map_err(|_| anyhow::anyhow!("frame mutex poisoned"))?
- .take()
- .ok_or_else(|| anyhow::anyhow!("portal ScreenCast: no frame arrived within timeout"))?;
-
- encode_frame_to_png(
- &frame.bytes,
- frame.width,
- frame.height,
- frame.stride,
- frame.format,
- )
-}
-
-/// Channel-swap a PipeWire video frame into RGBA8888 and PNG-encode it.
-///
-/// Mirrors `ext_screencopy::encode_buffer_to_png` — the input layout
-/// depends on the negotiated VideoFormat: BGRx/BGRA are little-endian
-/// BGRA (so swap R↔B), RGBx/RGBA are already RGBA in memory.
-fn encode_frame_to_png(
- pixels: &[u8],
- width: u32,
- height: u32,
- stride: u32,
- fmt: libspa::param::video::VideoFormat,
-) -> anyhow::Result<Vec<u8>> {
- use libspa::param::video::VideoFormat;
- let mut rgba: Vec<u8> = Vec::with_capacity((width as usize) * (height as usize) * 4);
- for y in 0..(height as usize) {
- let row_start = y * (stride as usize);
- for x in 0..(width as usize) {
- let i = row_start + x * 4;
- let (r, g, b, a) = match fmt {
- VideoFormat::BGRx => (pixels[i + 2], pixels[i + 1], pixels[i], 0xFF),
- VideoFormat::BGRA => (pixels[i + 2], pixels[i + 1], pixels[i], pixels[i + 3]),
- VideoFormat::RGBx => (pixels[i], pixels[i + 1], pixels[i + 2], 0xFF),
- VideoFormat::RGBA => (pixels[i], pixels[i + 1], pixels[i + 2], pixels[i + 3]),
- // Fall through: treat unknown formats as BGRA (the most
- // common compositor output on Linux).
- _ => (pixels[i + 2], pixels[i + 1], pixels[i], pixels[i + 3]),
- };
- rgba.extend_from_slice(&[r, g, b, a]);
- }
- }
- use image::{codecs::png::PngEncoder, ImageBuffer, ImageEncoder, Rgba};
- let img: ImageBuffer<Rgba<u8>, _> = ImageBuffer::from_raw(width, height, rgba)
- .ok_or_else(|| anyhow::anyhow!("internal: buffer dims mismatch ({}x{})", width, height))?;
- let mut out: Vec<u8> = Vec::new();
- PngEncoder::new(&mut out).write_image(
- img.as_raw(),
- width,
- height,
- image::ExtendedColorType::Rgba8,
- )?;
- Ok(out)
-}
-
-/// Probe whether the portal ScreenCast interface is reachable on the
-/// session bus without taking an actual capture (avoids cached consent).
-pub fn probe_screencast_portal() -> anyhow::Result<bool> {
- let rt = tokio::runtime::Builder::new_current_thread()
- .enable_all()
- .build()
- .map_err(|e| anyhow::anyhow!("failed to build tokio runtime: {e}"))?;
- rt.block_on(async {
- let conn = zbus::Connection::session()
- .await
- .map_err(|e| anyhow::anyhow!("session bus unreachable: {e}"))?;
- let proxy = zbus::fdo::DBusProxy::new(&conn)
- .await
- .map_err(|e| anyhow::anyhow!("dbus proxy creation failed: {e}"))?;
- let bus_name: zbus::names::BusName = "org.freedesktop.portal.Desktop"
- .try_into()
- .map_err(|e| anyhow::anyhow!("bus name parse failed: {e}"))?;
- let has_owner = proxy
- .name_has_owner(bus_name)
- .await
- .map_err(|e| anyhow::anyhow!("name_has_owner failed: {e}"))?;
- Ok(has_owner)
- })
-}
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/wayland/sway_ipc.rs b/libs/cua-driver/rust/crates/platform-linux/src/wayland/sway_ipc.rs
index 5600334a44..ca2e538a3a 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/wayland/sway_ipc.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/wayland/sway_ipc.rs
@@ -238,13 +238,6 @@ pub fn window_origin_for_pid(pid: u32) -> Option<(i32, i32)> {
window_for_pid(pid).map(|window| (window.x, window.y))
}
-/// Offset of the application content inside Sway's captured toplevel.
-/// Server-side decorations are part of `rect`/window screenshots but not of
-/// WebKitGTK's AT-SPI `CoordType::Window` descendants.
-pub fn window_content_offset_for_pid(pid: u32) -> Option<(i32, i32)> {
- window_for_pid(pid).map(|window| (window.content_x, window.content_y))
-}
-
pub fn window_origin_for_title(title: &str) -> Option<(i32, i32)> {
let window = window_for_title(title)?;
Some((window.x, window.y))