feat: send huge Wayland-to-X selections via INCR

Since the connection handshake establishes the most data a request can
send, if the data length exceeds that limit, we can follow ICCCM 2.7.2
sending the INCR property and continuing to send data via PropertyNotify
events.

To test the changes, we create `XState::set_max_req_bytes` to forcefully
trigger the INCR mechanism in integration test runs with a constant,
substantially less amount of data.
This commit is contained in:
En-En 2025-09-28 00:41:31 +00:00 committed by Supreeeme
parent 334933b212
commit 1ec45141e6
4 changed files with 254 additions and 81 deletions

View file

@ -45,6 +45,9 @@ pub trait RunData {
None None
} }
fn xwayland_ready(&self, _display: String, _pid: u32) {} fn xwayland_ready(&self, _display: String, _pid: u32) {}
fn max_req_len_bytes(&self) -> Option<usize> {
None
}
} }
pub const fn timespec_from_millis(millis: u64) -> Timespec { pub const fn timespec_from_millis(millis: u64) -> Timespec {
@ -186,6 +189,10 @@ pub fn main(mut data: impl RunData) -> Option<()> {
} }
let mut xstate = XState::new(xsock_wl.as_fd()); let mut xstate = XState::new(xsock_wl.as_fd());
if let Some(bytes) = data.max_req_len_bytes() {
xstate.set_max_req_bytes(bytes);
}
let mut reader = BufReader::new(&ready_rx); let mut reader = BufReader::new(&ready_rx);
{ {
let mut display = String::new(); let mut display = String::new();

View file

@ -119,6 +119,7 @@ pub struct XState {
wm_window: x::Window, wm_window: x::Window,
selection_state: SelectionState, selection_state: SelectionState,
settings: Settings, settings: Settings,
max_req_bytes: usize,
} }
impl XState { impl XState {
@ -226,6 +227,9 @@ impl XState {
let selection_state = SelectionState::new(&connection, root, &atoms); let selection_state = SelectionState::new(&connection, root, &atoms);
let window_atoms = WindowTypes::intern_all(&connection).unwrap(); let window_atoms = WindowTypes::intern_all(&connection).unwrap();
let settings = Settings::new(&connection, &atoms, root); let settings = Settings::new(&connection, &atoms, root);
// maximum-request-length is returned in units of 4 bytes.
// Additionally, requests use 32 bytes of metadata which cannot store arbitrary data
let max_req_bytes = (connection.get_maximum_request_length() * 4 - 32) as usize;
let mut r = Self { let mut r = Self {
connection, connection,
@ -235,12 +239,21 @@ impl XState {
window_atoms, window_atoms,
selection_state, selection_state,
settings, settings,
max_req_bytes,
}; };
r.create_ewmh_window(); r.create_ewmh_window();
r.set_xsettings_owner(); r.set_xsettings_owner();
r r
} }
pub(super) fn set_max_req_bytes(&mut self, max_req_bytes: usize) {
// `max_req_bytes` is initialized to the largest possible value before transfer problems
// would begin to occur. This function is called once during initialization only in
// integration tests, so this overly simple check is fine.
assert!(self.max_req_bytes >= max_req_bytes);
self.max_req_bytes = max_req_bytes;
}
pub fn server_state_setup( pub fn server_state_setup(
&self, &self,
server_state: super::EarlyServerState, server_state: super::EarlyServerState,
@ -883,9 +896,12 @@ impl XState {
event: x::PropertyNotifyEvent, event: x::PropertyNotifyEvent,
server_state: &mut super::RealServerState, server_state: &mut super::RealServerState,
) { ) {
if event.state() != x::Property::NewValue { if self.handle_selection_property_change(&event) {
return;
}
if event.state() == x::Property::Delete {
debug!( debug!(
"ignoring non newvalue for property {:?}", "ignoring delete for property {:?}",
get_atom_name(&self.connection, event.atom()) get_atom_name(&self.connection, event.atom())
); );
return; return;
@ -929,9 +945,7 @@ impl XState {
} }
} }
_ => { _ => {
if !self.handle_selection_property_change(&event) if log::log_enabled!(log::Level::Debug) {
&& log::log_enabled!(log::Level::Debug)
{
debug!( debug!(
"changed property {:?} for {:?}", "changed property {:?} for {:?}",
get_atom_name(&self.connection, event.atom()), get_atom_name(&self.connection, event.atom()),

View file

@ -162,12 +162,59 @@ impl Selection {
} }
} }
pub struct WaylandIncrInfo {
data: Vec<u8>,
range: std::ops::Range<usize>,
property: x::Atom,
target_window: x::Window,
target_type: x::Atom,
max_req_bytes: usize,
}
pub struct WaylandSelection<T: SelectionType> {
mimes: Vec<SelectionTargetId>,
inner: ForeignSelection<T>,
incr_data: Option<WaylandIncrInfo>,
}
impl<T: SelectionType> WaylandSelection<T> {
fn check_for_incr(&mut self, connection: &xcb::Connection) -> Option<bool> {
let incr_data = self.incr_data.as_mut()?;
let range_start = incr_data.range.start;
let range_len = std::cmp::min(incr_data.max_req_bytes, incr_data.range.end - range_start);
if let Err(e) = connection.send_and_check_request(&x::ChangeProperty {
mode: x::PropMode::Append,
window: incr_data.target_window,
property: incr_data.property,
r#type: incr_data.target_type,
data: &incr_data.data[range_start..][..range_len],
}) {
warn!("failed to write selection data: {e:?}");
self.incr_data = None;
return Some(true);
}
incr_data.range.start += range_len;
if range_len == 0 {
debug!(
"completed incr for mime {}",
get_atom_name(connection, incr_data.target_type)
);
self.incr_data = None;
} else {
debug!(
"received some incr data for {}",
get_atom_name(connection, incr_data.target_type)
);
}
Some(true)
}
}
enum CurrentSelection<T: SelectionType> { enum CurrentSelection<T: SelectionType> {
X11(Rc<Selection>), X11(Rc<Selection>),
Wayland { Wayland(WaylandSelection<T>),
mimes: Vec<SelectionTargetId>,
inner: ForeignSelection<T>,
},
} }
struct SelectionData<T: SelectionType> { struct SelectionData<T: SelectionType> {
@ -198,10 +245,11 @@ trait SelectionDataImpl {
); );
fn x11_selection(&self) -> Option<&Selection>; fn x11_selection(&self) -> Option<&Selection>;
fn handle_selection_request( fn handle_selection_request(
&self, &mut self,
connection: &xcb::Connection, connection: &xcb::Connection,
atoms: &super::Atoms, atoms: &super::Atoms,
request: &x::SelectionRequestEvent, request: &x::SelectionRequestEvent,
max_req_bytes: usize,
success: &dyn Fn(), success: &dyn Fn(),
refuse: &dyn Fn(), refuse: &dyn Fn(),
server_state: &mut RealServerState, server_state: &mut RealServerState,
@ -217,6 +265,12 @@ impl<T: SelectionType> SelectionData<T> {
current_selection: None, current_selection: None,
} }
} }
fn wayland_selection_mut(&mut self) -> Option<&mut WaylandSelection<T>> {
match &mut self.current_selection {
Some(CurrentSelection::Wayland(sel)) => Some(sel),
_ => None,
}
}
} }
impl<T: SelectionType> SelectionDataImpl for SelectionData<T> { impl<T: SelectionType> SelectionDataImpl for SelectionData<T> {
@ -359,15 +413,21 @@ impl<T: SelectionType> SelectionDataImpl for SelectionData<T> {
} }
fn handle_selection_request( fn handle_selection_request(
&self, &mut self,
connection: &xcb::Connection, connection: &xcb::Connection,
atoms: &super::Atoms, atoms: &super::Atoms,
request: &x::SelectionRequestEvent, request: &x::SelectionRequestEvent,
max_req_bytes: usize,
success: &dyn Fn(), success: &dyn Fn(),
refuse: &dyn Fn(), refuse: &dyn Fn(),
server_state: &mut RealServerState, server_state: &mut RealServerState,
) { ) {
let Some(CurrentSelection::Wayland { mimes, inner }) = &self.current_selection else { let Some(CurrentSelection::Wayland(WaylandSelection {
mimes,
inner,
ref mut incr_data,
})) = &mut self.current_selection
else {
warn!("Got selection request, but we don't seem to be the selection owner"); warn!("Got selection request, but we don't seem to be the selection owner");
refuse(); refuse();
return; return;
@ -405,17 +465,52 @@ impl<T: SelectionType> SelectionDataImpl for SelectionData<T> {
.cloned() .cloned()
.unwrap_or_else(|| target.name.clone()); .unwrap_or_else(|| target.name.clone());
let data = inner.receive(mime_name, server_state); let data = inner.receive(mime_name, server_state);
match connection.send_and_check_request(&x::ChangeProperty { if data.len() > max_req_bytes {
mode: x::PropMode::Replace, if let Err(e) = connection.send_and_check_request(&x::ChangeWindowAttributes {
window: request.requestor(), window: request.requestor(),
property: request.property(), value_list: &[x::Cw::EventMask(x::EventMask::PROPERTY_CHANGE)],
r#type: target.atom, }) {
data: &data, warn!("Failed to set up property change notifications: {e:?}");
}) {
Ok(_) => success(),
Err(e) => {
warn!("Failed setting selection property: {e:?}");
refuse(); refuse();
return;
}
if let Err(e) = connection.send_and_check_request(&x::ChangeProperty {
mode: x::PropMode::Replace,
window: request.requestor(),
property: request.property(),
r#type: atoms.incr,
data: &[data.len() as u32],
}) {
warn!("Failed to set incr property for large transfer: {e:?}");
refuse();
return;
}
debug!(
"beginning incr for {}",
get_atom_name(connection, target.atom)
);
*incr_data = Some(WaylandIncrInfo {
range: 0..data.len(),
data,
target_window: request.requestor(),
property: request.property(),
target_type: target.atom,
max_req_bytes,
});
success()
} else {
match connection.send_and_check_request(&x::ChangeProperty {
mode: x::PropMode::Replace,
window: request.requestor(),
property: request.property(),
r#type: target.atom,
data: &data,
}) {
Ok(_) => success(),
Err(e) => {
warn!("Failed setting selection property: {e:?}");
refuse();
}
} }
} }
} }
@ -503,10 +598,12 @@ impl XState {
}); });
} }
self.selection_state.clipboard.current_selection = Some(CurrentSelection::Wayland { self.selection_state.clipboard.current_selection =
mimes, Some(CurrentSelection::Wayland(WaylandSelection {
inner: selection, mimes,
}); inner: selection,
incr_data: None,
}));
self.selection_state self.selection_state
.clipboard .clipboard
.set_owner(&self.connection, self.wm_window); .set_owner(&self.connection, self.wm_window);
@ -559,10 +656,12 @@ impl XState {
}); });
} }
self.selection_state.primary.current_selection = Some(CurrentSelection::Wayland { self.selection_state.primary.current_selection =
mimes, Some(CurrentSelection::Wayland(WaylandSelection {
inner: selection, mimes,
}); inner: selection,
incr_data: None,
}));
self.selection_state self.selection_state
.primary .primary
.set_owner(&self.connection, self.wm_window); .set_owner(&self.connection, self.wm_window);
@ -676,6 +775,7 @@ impl XState {
&self.connection, &self.connection,
&self.atoms, &self.atoms,
e, e,
self.max_req_bytes,
&success, &success,
&refuse, &refuse,
server_state, server_state,
@ -725,15 +825,28 @@ impl XState {
&mut self, &mut self,
event: &x::PropertyNotifyEvent, event: &x::PropertyNotifyEvent,
) -> bool { ) -> bool {
for data in [ fn inner<T: SelectionType>(
&self.selection_state.primary as &dyn SelectionDataImpl, connection: &xcb::Connection,
&self.selection_state.clipboard as _, event: &x::PropertyNotifyEvent,
] { data: &mut SelectionData<T>,
if let Some(selection) = &data.x11_selection() { ) -> Option<bool> {
return selection.check_for_incr(event); match event.state() {
x::Property::NewValue => {
if let Some(selection) = &data.x11_selection() {
return Some(selection.check_for_incr(event));
}
}
x::Property::Delete => {
if let Some(selection) = data.wayland_selection_mut() {
return selection.check_for_incr(connection);
}
}
} }
None
} }
false inner(&self.connection, event, &mut self.selection_state.primary)
.or_else(|| inner(&self.connection, event, &mut self.selection_state.clipboard))
.unwrap_or(false)
} }
} }

View file

@ -82,6 +82,10 @@ impl xwls::RunData for TestData {
assert!(server.is_some()); assert!(server.is_some());
server.take() server.take()
} }
fn max_req_len_bytes(&self) -> Option<usize> {
Some(500)
}
} }
struct Fixture { struct Fixture {
@ -551,6 +555,23 @@ impl Connection {
.unwrap(); .unwrap();
} }
#[track_caller]
fn await_property_notify(&mut self) -> x::PropertyNotifyEvent {
match self.await_event() {
xcb::Event::X(x::Event::PropertyNotify(r)) => r,
other => panic!("Didn't get property notify event, instead got {other:?}"),
}
}
#[track_caller]
fn get_property_change_events(&self, window: x::Window) {
self.send_and_check_request(&x::ChangeWindowAttributes {
window,
value_list: &[x::Cw::EventMask(x::EventMask::PROPERTY_CHANGE)],
})
.unwrap();
}
#[track_caller] #[track_caller]
fn verify_clipboard_owner(&self, window: x::Window) { fn verify_clipboard_owner(&self, window: x::Window) {
let owner = self.get_reply(&x::GetSelectionOwner { let owner = self.get_reply(&x::GetSelectionOwner {
@ -1163,17 +1184,14 @@ fn copy_from_wayland() {
#[test] #[test]
fn incr_copy_from_wayland() { fn incr_copy_from_wayland() {
// After a little binary searching, the test passes on less than 16,777,184 bytes, fails due to const BYTES: usize = 3000;
// a failed assert on matching `dest1_atom` starting at 16,777,185 bytes, and starts crashing
// instead at 16,777,189 bytes.
const BYTES: usize = 16_777_189;
let mut f = Fixture::new(); let mut f = Fixture::new();
let mut connection = Connection::new(&f.display); let mut connection = Connection::new(&f.display);
let window = connection.new_window(connection.root, 0, 0, 20, 20, false); let window = connection.new_window(connection.root, 0, 0, 20, 20, false);
connection.get_selection_owner_change_events(true, window); connection.get_selection_owner_change_events(true, window);
f.map_as_toplevel(&mut connection, window); f.map_as_toplevel(&mut connection, window);
let offer = vec![testwl::PasteData { let mut offer = vec![testwl::PasteData {
mime_type: "text/plain".into(), mime_type: "text/plain".into(),
data: std::iter::successors(Some(0u8), |n| Some(n.wrapping_add(1))) data: std::iter::successors(Some(0u8), |n| Some(n.wrapping_add(1)))
.take(BYTES) .take(BYTES)
@ -1219,49 +1237,70 @@ fn incr_copy_from_wayland() {
let targets: &[x::Atom] = reply.value(); let targets: &[x::Atom] = reply.value();
assert_eq!(targets.len(), 1); assert_eq!(targets.len(), 1);
for testwl::PasteData { mime_type, data } in offer { let offer_data = offer.pop().unwrap();
let atom = connection let (mime_type, data) = (offer_data.mime_type, offer_data.data);
.get_reply(&x::InternAtom { let atom = connection
only_if_exists: true, .get_reply(&x::InternAtom {
name: mime_type.as_bytes(), only_if_exists: true,
}) name: mime_type.as_bytes(),
.atom(); })
assert_ne!(atom, x::ATOM_NONE); .atom();
assert!(targets.contains(&atom)); assert_ne!(atom, x::ATOM_NONE);
assert!(targets.contains(&atom));
std::thread::sleep(std::time::Duration::from_millis(50)); connection
connection .send_and_check_request(&x::ConvertSelection {
.send_and_check_request(&x::ConvertSelection { requestor: window,
requestor: window, selection: connection.atoms.clipboard,
selection: connection.atoms.clipboard, target: atom,
target: atom, property: dest1_atom,
property: dest1_atom, time: x::CURRENT_TIME,
time: x::CURRENT_TIME, })
}) .unwrap();
.unwrap();
f.wait_and_dispatch(); f.wait_and_dispatch();
let request = connection.await_selection_notify(); let request = connection.await_selection_notify();
assert_eq!(request.requestor(), window); assert_eq!(request.requestor(), window);
assert_eq!(request.selection(), connection.atoms.clipboard); assert_eq!(request.selection(), connection.atoms.clipboard);
assert_eq!(request.target(), atom); assert_eq!(request.target(), atom);
// The particular assert which fails in the narrow byte range as mentioned above. assert_eq!(request.property(), dest1_atom);
assert_eq!(request.property(), dest1_atom);
let val: Vec<u8> = connection connection.get_property_change_events(window);
.get_reply(&x::GetProperty { let reply = connection.get_reply(&x::GetProperty {
delete: true, delete: true,
window, window,
property: dest1_atom, property: dest1_atom,
r#type: x::ATOM_ANY, r#type: x::ATOM_ANY,
long_offset: 0, long_offset: 0,
long_length: (BYTES / 4 + BYTES % 4) as u32, long_length: (BYTES / 4 + BYTES % 4) as u32,
}) });
.value() assert_eq!(reply.r#type(), connection.atoms.incr);
.to_vec(); assert_eq!(reply.value::<u32>().len(), 1);
assert!(reply.value::<u32>()[0] <= data.len() as u32);
assert_eq!(val.len(), data.len()); let delete_property = connection.await_property_notify();
assert_eq!(val, data); assert_eq!(delete_property.state(), x::Property::Delete);
assert_eq!(delete_property.atom(), dest1_atom);
for (idx, chunk) in data.chunks(500).chain([]).enumerate() {
let new_property = connection.await_property_notify();
assert_eq!(new_property.state(), x::Property::NewValue, "chunk {idx}");
assert_eq!(new_property.atom(), dest1_atom, "chunk {idx}");
let incr_reply = connection.get_reply(&x::GetProperty {
delete: true,
window,
property: dest1_atom,
r#type: x::ATOM_ANY,
long_offset: 0,
long_length: (BYTES / 4 + BYTES % 4) as u32,
});
assert_eq!(incr_reply.r#type(), atom, "chunk {idx}");
assert_eq!(incr_reply.value::<u8>(), chunk, "chunk {idx}");
let delete_property = connection.await_property_notify();
assert_eq!(delete_property.state(), x::Property::Delete, "chunk {idx}");
assert_eq!(delete_property.atom(), dest1_atom, "chunk {idx}");
} }
} }