From 6fe9c541347b7bdd69e3d735f07a17a5d4b124ca Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Sun, 18 Feb 2024 00:41:16 +0000 Subject: [PATCH] fix(clipboard): unable to paste large images into xwayland Thanks to a nightly clippy warning which found I wasn't using `file.write` correctly. Fixes #86. --- src/clients/wayland/wlr_data_control/mod.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/clients/wayland/wlr_data_control/mod.rs b/src/clients/wayland/wlr_data_control/mod.rs index 8c40a840..1cf76a57 100644 --- a/src/clients/wayland/wlr_data_control/mod.rs +++ b/src/clients/wayland/wlr_data_control/mod.rs @@ -179,6 +179,9 @@ impl Environment { MimeTypeCategory::Image => { let mut bytes = vec![]; file.read_to_end(&mut bytes)?; + + debug!("Read bytes: {}", bytes.len()); + let bytes = Bytes::from(&bytes); ClipboardValue::Image(bytes) @@ -234,6 +237,8 @@ impl DataControlDeviceHandler for Environment { return; }; + debug!("Receiving mime type: {}", mime_type.value); + if let Ok(read_pipe) = cur_offer.offer.receive(mime_type.value.clone()) { let offer_clone = cur_offer.offer.clone(); @@ -331,9 +336,9 @@ impl DataControlSourceHandler for Environment { let pipe_size = set_pipe_size(fd.as_raw_fd(), bytes.len()) .expect("Failed to increase pipe size"); - let mut file = File::from(fd.try_clone().expect("Failed to clone fd")); + let mut file = File::from(fd.try_clone().expect("to be able to clone")); - trace!("Num bytes: {}", bytes.len()); + debug!("Writing {} bytes", bytes.len()); let mut events = (0..16).map(|_| EpollEvent::empty()).collect::>(); let epoll_event = EpollEvent::new(EpollFlags::EPOLLOUT, 0); @@ -347,20 +352,23 @@ impl DataControlSourceHandler for Environment { while !bytes.is_empty() { let chunk = &bytes[..min(pipe_size as usize, bytes.len())]; - trace!("Writing {} bytes ({} remain)", chunk.len(), bytes.len()); - epoll_fd .wait(&mut events, 100) .expect("Failed to wait to epoll"); match file.write(chunk) { - Ok(_) => bytes = &bytes[chunk.len()..], + Ok(written) => { + trace!("Wrote {} bytes ({} remain)", written, bytes.len()); + bytes = &bytes[written..]; + } Err(err) => { error!("{err:?}"); break; } } } + + debug!("Done writing"); } else { error!("Failed to find source"); } @@ -388,7 +396,7 @@ impl DataControlSourceHandler for Environment { /// If the requested size is larger than the kernel max (normally 1MB), /// it will be clamped at this. /// -/// Returns the new size if succeeded +/// Returns the new size if succeeded. fn set_pipe_size(fd: RawFd, size: usize) -> io::Result { // clamp size at kernel max let max_pipe_size = fs::read_to_string("/proc/sys/fs/pipe-max-size")