image-copy: Move duplicate code to a cursor_capture_constraints()

This ensures we avoid advertising a `0,0` buffer size (which will
protocol error to actually allocate), and avoids defining the supported
formats for cursor capture buffers in a bunch of places.
This commit is contained in:
Ian Douglas Scott 2026-06-15 16:23:31 -07:00 • committed by Victoria Brekenfeld
parent 27956e6e00
commit 2648aea0df
3 changed files with 68 additions and 82 deletions

View file

@ -45,6 +45,10 @@ pub use self::render::*;
use self::user_data::*;
pub use self::user_data::{FrameHolder, ImageCopySessions, SessionData, SessionHolder};
fn default_cursor_size() -> Size<i32, BufferCoords> {
Size::new(64, 64)
}
fn seat_for_wl_pointer<'a>(shell: &'a Shell, pointer: &WlPointer) -> Option<&'a Seat<State>> {
let pointer_handle = PointerHandle::<State>::from_resource(pointer)?;
shell
@ -53,6 +57,24 @@ fn seat_for_wl_pointer<'a>(shell: &'a Shell, pointer: &WlPointer) -> Option<&'a
.find(|seat| seat.get_pointer().is_some_and(|p| p == pointer_handle))
}
pub fn cursor_capture_constraints(cursor_geometry: Option<CursorGeometry>) -> BufferConstraints {
let size = if let Some(cursor_geometry) = cursor_geometry {
let mut size = cursor_geometry.geometry.size;
// Client shouldn't try to allocate 0x0 buffer
if size == Size::new(0, 0) {
size = Size::new(1, 1);
}
size
} else {
default_cursor_size()
};
BufferConstraints {
size,
shm: vec![ShmFormat::Argb8888],
dma: None,
}
}
impl ImageCopyCaptureHandler for State {
fn image_copy_capture_state(&mut self) -> &mut ImageCopyCaptureState {
&mut self.common.image_copy_capture_state
@ -86,19 +108,8 @@ impl ImageCopyCaptureHandler for State {
) -> Option<BufferConstraints> {
let shell = self.common.shell.read();
let seat = seat_for_wl_pointer(&shell, pointer)?;
let size = if let Some(CursorGeometry { geometry, .. }) =
seat.cursor_geometry((0.0, 0.0), self.common.clock.now())
{
geometry.size
} else {
Size::from((64, 64))
};
Some(BufferConstraints {
size,
shm: vec![ShmFormat::Argb8888],
dma: None,
})
let cursor_geometry = seat.cursor_geometry((0.0, 0.0), self.common.clock.now());
Some(cursor_capture_constraints(cursor_geometry))
}
fn new_session(&mut self, session: Session) {
@ -165,12 +176,12 @@ impl ImageCopyCaptureHandler for State {
{
(geometry.size, hotspot)
} else {
(Size::from((64, 64)), Point::from((0, 0)))
(default_cursor_size(), Point::from((0, 0)))
};
(pointer_loc, pointer_size, hotspot)
} else {
(Point::new(0, 0), Size::from((64, 64)), Point::from((0, 0)))
(Point::new(0, 0), default_cursor_size(), Point::from((0, 0)))
}
};

View file

@ -20,16 +20,14 @@ use smithay::{
desktop::space::SpaceElement,
input::Seat,
output::{Output, OutputNoMode},
reexports::wayland_server::protocol::{wl_buffer::WlBuffer, wl_shm::Format as ShmFormat},
reexports::wayland_server::protocol::wl_buffer::WlBuffer,
utils::{
Buffer as BufferCoords, IsAlive, Logical, Physical, Point, Rectangle, Scale, Size,
Transform,
},
wayland::{
dmabuf::get_dmabuf,
image_copy_capture::{
BufferConstraints, CaptureFailureReason, CursorSessionRef, Frame, SessionRef,
},
image_copy_capture::{CaptureFailureReason, CursorSessionRef, Frame, SessionRef},
seat::WaylandFocus,
shm::{shm_format_to_fourcc, with_buffer_contents, with_buffer_contents_mut},
},
@ -45,7 +43,7 @@ use crate::{
render_workspace,
wayland::SurfaceRenderElement,
},
shell::{CosmicMappedRenderElement, CosmicSurface, CursorGeometry, WorkspaceRenderElement},
shell::{CosmicMappedRenderElement, CosmicSurface, WorkspaceRenderElement},
state::{Common, KmsNodes, State},
utils::prelude::{PointExt, PointGlobalExt, RectExt, RectLocalExt, SeatExt},
wayland::{
@ -56,7 +54,9 @@ use crate::{
},
};
use super::{super::data_device::get_dnd_icon, user_data::SessionHolder};
use super::{
super::data_device::get_dnd_icon, cursor_capture_constraints, user_data::SessionHolder,
};
pub fn render_element_buffers<R, E>(
renderer: &mut R,
@ -802,25 +802,17 @@ pub fn render_cursor_to_buffer(
seat: &Seat<State>,
) {
let buffer = frame.buffer();
let mut cursor_size = seat
.cursor_geometry((0.0, 0.0), state.common.clock.now())
.map(|CursorGeometry { geometry, .. }| geometry.size)
.unwrap_or_else(|| Size::from((64, 64)));
let cursor_geometry = seat.cursor_geometry((0.0, 0.0), state.common.clock.now());
let constraints = cursor_capture_constraints(cursor_geometry);
let buffer_size = buffer_dimensions(&buffer).unwrap();
// Client shouldn't try to allocate 0x0 buffer
if cursor_size == Size::new(0, 0) {
cursor_size = Size::new(1, 1);
}
if buffer_size != cursor_size {
let constraints = BufferConstraints {
size: cursor_size,
shm: vec![ShmFormat::Argb8888],
dma: None,
};
session.update_constraints(constraints);
if buffer_size != constraints.size {
session.update_constraints(constraints.clone());
if let Some(data) = session.user_data().get::<SessionData>() {
*data.lock().unwrap() = SessionUserData::new(OutputDamageTracker::new(
cursor_size.to_logical(1, Transform::Normal).to_physical(1),
constraints
.size
.to_logical(1, Transform::Normal)
.to_physical(1),
1.0,
Transform::Normal,
));