rdw: fix run-time panic because we are not on MainContext

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2023-01-17 18:26:35 +04:00
parent 3712905ec1
commit 544a407561

@ -99,41 +99,43 @@ impl ClipboardHandler for InnerHandler {
selection: ClipboardSelection, selection: ClipboardSelection,
mimes: Vec<String>, mimes: Vec<String>,
) -> qemu_display::Result<(String, Vec<u8>)> { ) -> qemu_display::Result<(String, Vec<u8>)> {
// we have to spawn a local future, because clipboard is not Send
let (sender, receiver) = futures::channel::oneshot::channel(); let (sender, receiver) = futures::channel::oneshot::channel();
glib::MainContext::default().spawn_local(async move { glib::MainContext::default().invoke(move || {
let res = if let Some((clipboard, _)) = clipboard_from_selection(selection) { glib::MainContext::default().spawn_local(async move {
let m: Vec<_> = mimes.iter().map(|s| s.as_str()).collect(); let res = if let Some((clipboard, _)) = clipboard_from_selection(selection) {
let res = clipboard.read_future(&m, glib::Priority::default()).await; let m: Vec<_> = mimes.iter().map(|s| s.as_str()).collect();
log::debug!("clipboard-read: {}", res.is_ok()); let res = clipboard.read_future(&m, glib::Priority::default()).await;
match res { log::debug!("clipboard-read: {}", res.is_ok());
Ok((stream, mime)) => { match res {
let out = gio::MemoryOutputStream::new_resizable(); Ok((stream, mime)) => {
let res = out let out = gio::MemoryOutputStream::new_resizable();
.splice_future( let res = out
&stream, .splice_future(
gio::OutputStreamSpliceFlags::CLOSE_SOURCE &stream,
| gio::OutputStreamSpliceFlags::CLOSE_TARGET, gio::OutputStreamSpliceFlags::CLOSE_SOURCE
glib::Priority::default(), | gio::OutputStreamSpliceFlags::CLOSE_TARGET,
) glib::Priority::default(),
.await; )
match res { .await;
Ok(_) => { match res {
let data = out.steal_as_bytes(); Ok(_) => {
Ok((mime.to_string(), data.as_ref().to_vec())) let data = out.steal_as_bytes();
Ok((mime.to_string(), data.as_ref().to_vec()))
}
Err(e) => Err(qemu_display::Error::Failed(format!("{}", e))),
} }
Err(e) => Err(qemu_display::Error::Failed(format!("{}", e))),
} }
Err(e) => Err(qemu_display::Error::Failed(format!("{}", e))),
} }
Err(e) => Err(qemu_display::Error::Failed(format!("{}", e))), } else {
} Err(qemu_display::Error::Failed(
} else { "Clipboard request failed".into(),
Err(qemu_display::Error::Failed( ))
"Clipboard request failed".into(), };
)) sender.send(res).unwrap()
}; });
sender.send(res).unwrap()
}); });
match receiver.await { match receiver.await {
Ok(res) => res, Ok(res) => res,
Err(e) => Err(qemu_display::Error::Failed(format!( Err(e) => Err(qemu_display::Error::Failed(format!(