Skip to content

Commit

Permalink
data_device: return PostAction for the Read/Write pipe
Browse files Browse the repository at this point in the history
This should simplify client handling, since they can properly model
the read/write loop.
  • Loading branch information
kchibisov authored and wash2 committed Oct 8, 2023
1 parent 468d23a commit 7951a66
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
46 changes: 21 additions & 25 deletions examples/data_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::{
time::Duration,
};

use smithay_client_toolkit::reexports::calloop::{EventLoop, LoopHandle, RegistrationToken};
use smithay_client_toolkit::reexports::calloop::{
EventLoop, LoopHandle, PostAction, RegistrationToken,
};
use smithay_client_toolkit::reexports::calloop_wayland_source::WaylandSource;
use smithay_client_toolkit::{
compositor::{CompositorHandler, CompositorState},
Expand Down Expand Up @@ -642,27 +644,25 @@ impl DataDeviceHandler for DataDeviceWindow {
return;
}
};
let loop_handle = self.loop_handle.clone();
let cur_offer_ = cur_offer.0.clone();
if let Ok(token) = self.loop_handle.insert_source(read_pipe, move |_, f, state| {
let offer = match state.selection_offers.iter().position(|o| o.0 == cur_offer_) {
Some(s) => state.selection_offers.remove(s),
None => return,
None => return PostAction::Continue,
};
let (offer, mut data, token) = match offer {
(o, d, Some(t)) => (o, d, t),
_ => return,
_ => return PostAction::Continue,
};
// SAFETY: it's safe as long as we don't close the underlying file.
let f: &mut fs::File = unsafe { f.get_mut() };
let mut reader = BufReader::new(f);
let consumed = match reader.fill_buf() {
Ok(buf) => {
if buf.is_empty() {
loop_handle.remove(token);
println!("selection data: {:?}", String::from_utf8(data.clone()));
state.selection_offers.push((offer, Vec::new(), None));
return;
return PostAction::Remove;
} else {
data.extend_from_slice(buf);
state.selection_offers.push((offer, data, Some(token)));
Expand All @@ -671,17 +671,17 @@ impl DataDeviceHandler for DataDeviceWindow {
}
Err(e) if matches!(e.kind(), std::io::ErrorKind::Interrupted) => {
state.selection_offers.push((offer, data, Some(token)));
return;
return PostAction::Continue;
}
Err(e) => {
eprintln!("Error reading selection data: {}", e);
loop_handle.remove(token);
state.selection_offers.push((offer, Vec::new(), None));

return;
return PostAction::Remove;
}
};
reader.consume(consumed);
PostAction::Continue
}) {
cur_offer.2 = Some(token);
}
Expand Down Expand Up @@ -719,29 +719,27 @@ impl DataDeviceHandler for DataDeviceWindow {
self.accept_counter += 1;
cur_offer.0.accept_mime_type(self.accept_counter, Some(mime_type));
cur_offer.0.set_actions(DndAction::Copy, DndAction::Copy);
let loop_handle = self.loop_handle.clone();
let cur_offer_ = cur_offer.0.clone();
match self.loop_handle.insert_source(read_pipe, move |_, f, state| {
let offer = match state.dnd_offers.iter().position(|o| o.0 == cur_offer_) {
Some(s) => state.dnd_offers.remove(s),
None => return,
None => return PostAction::Continue,
};
let (offer, mut data, token) = match offer {
(o, d, Some(t)) => (o, d, t),
_ => return,
_ => return PostAction::Continue,
};
// SAFETY: it's safe as long as we don't close the underlying file.
let f: &mut fs::File = unsafe { f.get_mut() };
let mut reader = BufReader::new(f);
let consumed = match reader.fill_buf() {
Ok(buf) => {
if buf.is_empty() {
loop_handle.remove(token);
println!("Dropped data: {:?}", String::from_utf8(data.clone()));
offer.finish();
offer.destroy();
state.dnd_offers.push((offer, Vec::new(), None));
return;
return PostAction::Remove;
} else {
data.extend_from_slice(buf);
state.dnd_offers.push((offer, data, Some(token)));
Expand All @@ -750,18 +748,18 @@ impl DataDeviceHandler for DataDeviceWindow {
}
Err(e) if matches!(e.kind(), std::io::ErrorKind::Interrupted) => {
state.dnd_offers.push((offer, data, Some(token)));
return;
return PostAction::Continue;
}
Err(e) => {
eprintln!("Error reading dropped data: {}", e);
offer.finish();
offer.destroy();
loop_handle.remove(token);

return;
return PostAction::Remove;
}
};
reader.consume(consumed);
PostAction::Continue
}) {
Ok(token) => {
cur_offer.2 = Some(token);
Expand Down Expand Up @@ -918,30 +916,28 @@ impl PrimarySelectionDeviceHandler for DataDeviceWindow {
return;
}
};
let loop_handle = self.loop_handle.clone();
if let Ok(token) = self.loop_handle.insert_source(read_pipe, move |_, f, state| {
let offer = match state.primary_selection_offers.iter().position(|of| of.0 == offer)
{
Some(s) => state.primary_selection_offers.remove(s),
None => return,
None => return PostAction::Continue,
};
let (offer, mut data, token) = match offer {
(o, d, Some(t)) => (o, d, t),
_ => return,
_ => return PostAction::Continue,
};
// SAFETY: it's safe as long as we don't close the underlying file.
let f: &mut fs::File = unsafe { f.get_mut() };
let mut reader = BufReader::new(f);
let consumed = match reader.fill_buf() {
Ok(buf) => {
if buf.is_empty() {
loop_handle.remove(token);
println!(
"primary selection data: {:?}",
String::from_utf8(data.clone())
);
state.primary_selection_offers.push((offer, Vec::new(), None));
return;
return PostAction::Remove;
} else {
data.extend_from_slice(buf);
state.primary_selection_offers.push((offer, data, Some(token)));
Expand All @@ -950,17 +946,17 @@ impl PrimarySelectionDeviceHandler for DataDeviceWindow {
}
Err(e) if matches!(e.kind(), std::io::ErrorKind::Interrupted) => {
state.primary_selection_offers.push((offer, data, Some(token)));
return;
return PostAction::Continue;
}
Err(e) => {
eprintln!("Error reading selection data: {}", e);
loop_handle.remove(token);
state.primary_selection_offers.push((offer, Vec::new(), None));

return;
return PostAction::Remove;
}
};
reader.consume(consumed);
PostAction::Continue
}) {
self.primary_selection_offers.last_mut().unwrap().2 = Some(token);
}
Expand Down
9 changes: 3 additions & 6 deletions src/data_device_manager/read_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl calloop::EventSource for ReadPipe {
type Event = ();
type Error = std::io::Error;
type Metadata = calloop::generic::NoIoDrop<fs::File>;
type Ret = ();
type Ret = calloop::PostAction;

fn process_events<F>(
&mut self,
Expand All @@ -138,12 +138,9 @@ impl calloop::EventSource for ReadPipe {
mut callback: F,
) -> std::io::Result<calloop::PostAction>
where
F: FnMut((), &mut calloop::generic::NoIoDrop<fs::File>),
F: FnMut((), &mut calloop::generic::NoIoDrop<fs::File>) -> Self::Ret,
{
self.file.process_events(readiness, token, |_, file| {
callback((), file);
Ok(calloop::PostAction::Continue)
})
self.file.process_events(readiness, token, |_, file| Ok(callback((), file)))
}

fn register(
Expand Down
9 changes: 3 additions & 6 deletions src/data_device_manager/write_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl calloop::EventSource for WritePipe {
type Event = ();
type Error = std::io::Error;
type Metadata = calloop::generic::NoIoDrop<fs::File>;
type Ret = ();
type Ret = calloop::PostAction;

fn process_events<F>(
&mut self,
Expand All @@ -146,12 +146,9 @@ impl calloop::EventSource for WritePipe {
mut callback: F,
) -> std::io::Result<calloop::PostAction>
where
F: FnMut((), &mut calloop::generic::NoIoDrop<fs::File>),
F: FnMut((), &mut calloop::generic::NoIoDrop<fs::File>) -> Self::Ret,
{
self.file.process_events(readiness, token, |_, file| {
callback((), file);
Ok(calloop::PostAction::Continue)
})
self.file.process_events(readiness, token, |_, file| Ok(callback((), file)))
}

fn register(
Expand Down

0 comments on commit 7951a66

Please sign in to comment.