//! The output the pointer is on, asked of the compositor without a surface: //! one image-copy-capture pointer-cursor session per output //! (`ext-image-copy-capture-v1`), and the one that reports `enter`. //! //! The sessions are metadata only: nothing is captured, no frame is //! requested, and no buffer is created. A desktop that doesn't offer the //! staging protocols, or whose cursor is drawn without an output cursor, //! reports nothing and the caller falls back to the ScreenCast portal's one //! remembered screen. //! //! One connection and a fixed two round trips, each blocking until the //! compositor answers, as [`super::output`] already does; this is not a time //! bound. use wayland_client::globals::{GlobalListContents, registry_queue_init}; use wayland_client::protocol::wl_output::{self, WlOutput}; use wayland_client::protocol::wl_pointer::{self, WlPointer}; use wayland_client::protocol::wl_registry::{self, WlRegistry}; use wayland_client::protocol::wl_seat::{self, WlSeat}; use wayland_client::{Connection, Dispatch, Proxy, QueueHandle}; use wayland_protocols::ext::image_capture_source::v1::client::ext_image_capture_source_v1::{ self, ExtImageCaptureSourceV1, }; use wayland_protocols::ext::image_capture_source::v1::client::ext_output_image_capture_source_manager_v1::{ self, ExtOutputImageCaptureSourceManagerV1, }; use wayland_protocols::ext::image_copy_capture::v1::client::ext_image_copy_capture_cursor_session_v1::{ self, ExtImageCopyCaptureCursorSessionV1, }; use wayland_protocols::ext::image_copy_capture::v1::client::ext_image_copy_capture_manager_v1::{ self, ExtImageCopyCaptureManagerV1, }; /// The first `wl_output` version that sends the output's connector name, /// which is what the portal calls `mapping_id`. const NAME_VERSION: u32 = 4; /// How many round trips the sessions get to report their cursor. A desktop /// answers each session when it creates it (and again on a later commit), so /// two are enough and nothing here waits on a frame. const ROUND_TRIPS: usize = 2; /// The name of the output the pointer is on, such as `DP-3`, or `None` when /// it can't be known. One connection and a fixed two round trips (each blocks /// until the compositor answers); every object is released before it returns. pub(crate) fn under_pointer() -> Option { match probe() { Ok(found) => found, Err(why) => { tracing::debug!(why, "could not ask the compositor for the pointer's output"); None } } } fn probe() -> Result, String> { let connection = Connection::connect_to_env().map_err(|error| format!("no Wayland connection: {error}"))?; let (globals, mut queue) = registry_queue_init::(&connection) .map_err(|error| format!("no registry: {error}"))?; let qh = queue.handle(); let seat = globals .bind::(&qh, 1..=9, ()) .map_err(|_| "the desktop offers no seat".to_owned())?; // Only the seat token: a session identifies its seat by this pointer, and // a client with no surface never receives pointer events of its own. let seat_version = seat.version(); let pointer = seat.get_pointer(&qh, ()); let pointer_version = pointer.version(); let sources = globals .bind::(&qh, 1..=1, ()) .map_err(|_| { "the desktop offers no ext_output_image_capture_source_manager_v1".to_owned() })?; let captures = globals .bind::(&qh, 1..=1, ()) .map_err(|_| "the desktop offers no ext_image_copy_capture_manager_v1".to_owned())?; let outputs: Vec<(u32, u32)> = globals.contents().with_list(|list| { list.iter() .filter(|global| global.interface == "wl_output" && global.version >= NAME_VERSION) .map(|global| (global.name, global.version)) .collect() }); if outputs.is_empty() { return Ok(None); } let mut probe = Probe::default(); for (index, (name, version)) in outputs.iter().enumerate() { let output = globals.registry().bind::( *name, (*version).min(NAME_VERSION), &qh, index, ); let source = sources.create_source(&output, &qh, index); let session = captures.create_pointer_cursor_session(&source, &pointer, &qh, index); probe.candidates.push(Candidate { name: None, entered: false, position: None, output, source, session, }); } for _ in 0..ROUND_TRIPS { queue .roundtrip(&mut probe) .map_err(|error| format!("the compositor stopped answering: {error}"))?; } let picked = entered(probe.candidates.iter().map(|candidate| { ( candidate.name.as_deref(), candidate.entered, candidate.position.is_some(), ) })) .map(str::to_owned); if let Some(name) = &picked { tracing::debug!(output = %name, "the pointer's output"); } for candidate in &mut probe.candidates { candidate.session.destroy(); candidate.source.destroy(); candidate.output.release(); } sources.destroy(); captures.destroy(); if pointer_version >= 3 { pointer.release(); } if seat_version >= 5 { seat.release(); } let _ = connection.flush(); let _ = queue.roundtrip(&mut probe); Ok(picked) } /// The name of the output whose cursor session reported `enter`, preferring /// one that also reported a `position` when several entered (a crossing /// between two sessions). Outputs without a name (a compositor below /// `wl_output` v4) are skipped. fn entered<'a>( candidates: impl IntoIterator, bool, bool)>, ) -> Option<&'a str> { let mut fallback = None; for (name, entered, positioned) in candidates { let Some(name) = name.filter(|_| entered) else { continue; }; if positioned { return Some(name); } fallback.get_or_insert(name); } fallback } #[derive(Default)] struct Probe { candidates: Vec, } struct Candidate { name: Option, /// Whether this output's cursor session last said `enter`. entered: bool, /// The cursor's position on this output, once it reported one. position: Option<(i32, i32)>, output: WlOutput, source: ExtImageCaptureSourceV1, session: ExtImageCopyCaptureCursorSessionV1, } impl Dispatch for Probe { fn event( _: &mut Self, _: &WlRegistry, _: wl_registry::Event, _: &GlobalListContents, _: &Connection, _: &QueueHandle, ) { } } impl Dispatch for Probe { fn event( _: &mut Self, _: &WlSeat, _: wl_seat::Event, _: &(), _: &Connection, _: &QueueHandle, ) { } } impl Dispatch for Probe { fn event( _: &mut Self, _: &WlPointer, _: wl_pointer::Event, _: &(), _: &Connection, _: &QueueHandle, ) { } } impl Dispatch for Probe { fn event( probe: &mut Self, _: &WlOutput, event: wl_output::Event, index: &usize, _: &Connection, _: &QueueHandle, ) { if let wl_output::Event::Name { name } = event && let Some(candidate) = probe.candidates.get_mut(*index) { candidate.name = Some(name); } } } impl Dispatch for Probe { fn event( _: &mut Self, _: &ExtOutputImageCaptureSourceManagerV1, _: ext_output_image_capture_source_manager_v1::Event, _: &(), _: &Connection, _: &QueueHandle, ) { } } impl Dispatch for Probe { fn event( _: &mut Self, _: &ExtImageCaptureSourceV1, _: ext_image_capture_source_v1::Event, _: &usize, _: &Connection, _: &QueueHandle, ) { } } impl Dispatch for Probe { fn event( _: &mut Self, _: &ExtImageCopyCaptureManagerV1, _: ext_image_copy_capture_manager_v1::Event, _: &(), _: &Connection, _: &QueueHandle, ) { } } impl Dispatch for Probe { fn event( probe: &mut Self, _: &ExtImageCopyCaptureCursorSessionV1, event: ext_image_copy_capture_cursor_session_v1::Event, index: &usize, _: &Connection, _: &QueueHandle, ) { let Some(candidate) = probe.candidates.get_mut(*index) else { return; }; match event { ext_image_copy_capture_cursor_session_v1::Event::Enter => candidate.entered = true, ext_image_copy_capture_cursor_session_v1::Event::Leave => { candidate.entered = false; candidate.position = None; } ext_image_copy_capture_cursor_session_v1::Event::Position { x, y } => { candidate.position = Some((x, y)); } _ => {} } } } #[cfg(test)] mod tests { use super::entered; #[test] fn the_output_that_entered_is_the_one() { let candidates = [ (Some("DP-3"), false, false), (Some("HDMI-A-1"), true, true), (Some("eDP-1"), false, false), ]; assert_eq!(entered(candidates), Some("HDMI-A-1")); } #[test] fn no_session_entering_names_nothing() { let candidates = [(Some("DP-3"), false, false), (Some("eDP-1"), false, false)]; assert_eq!(entered(candidates), None); } #[test] fn an_unnamed_output_is_skipped() { let candidates = [(None, true, true), (Some("eDP-1"), true, false)]; assert_eq!(entered(candidates), Some("eDP-1")); } #[test] fn a_positioned_session_wins_a_crossing() { let candidates = [(Some("DP-3"), true, false), (Some("HDMI-A-1"), true, true)]; assert_eq!(entered(candidates), Some("HDMI-A-1")); } #[test] fn without_a_position_the_first_entered_output_wins() { let candidates = [(Some("DP-3"), true, false), (Some("HDMI-A-1"), true, false)]; assert_eq!(entered(candidates), Some("DP-3")); } }