hex/src/linux_clipboard.rs
Dax 83e0825909
fix(linux): restore the previous clipboard after a paste (#415)
Linux paste now snapshots every format of the clipboard before writing the
transcript and restores it 500 ms after the paste shortcut, as macOS does,
unless a later paste, copy, or another client replaced it. Wayland reads and
serves through wlr-data-control; X11 through Hex's own selection owner with
INCR. Empty clipboards stay empty; clipboards over 32 MiB are left, with a log
line. Thanks to @shashankx86 for the report.

Fixes #391
2026-10-29 17:22:56 -04:00

810 lines
28 KiB
Rust

//! The clipboard a Linux paste puts back afterwards: every format of the
//! CLIPBOARD selection, read within a size cap and deadline, then served
//! again from Hex until another client takes the clipboard. Wayland reads
//! and serves through wlr-data-control. X11 uses its own selection owner,
//! because `x11-clipboard` serves a single target.
use std::io::{ErrorKind, Read, Write};
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd, RawFd};
use std::thread;
use std::time::{Duration, Instant};
use color_eyre::eyre::{Result, WrapErr, eyre};
/// The most clipboard data a paste preserves. Larger clipboards (big
/// images) are left replaced by the transcript.
pub(crate) const CAP: usize = 32 * 1024 * 1024;
/// How long a paste waits for the clipboard's owner to hand over its formats.
const READ_DEADLINE: Duration = Duration::from_secs(1);
/// How long a restored clipboard waits for a slow reader.
const SERVE_DEADLINE: Duration = Duration::from_secs(2);
pub(crate) enum Snapshot {
/// Nothing owned the clipboard; restoring clears it.
Empty,
Wayland(Vec<(String, Vec<u8>)>),
X11(Vec<X11Format>),
}
pub(crate) struct X11Format {
target: u32,
type_: u32,
format: u8,
data: Vec<u8>,
}
/// The session's clipboard.
#[derive(Clone, Copy)]
pub(crate) enum Selection {
Wayland,
/// `transcript_owner` is the window `x11-clipboard` pastes from.
X11 {
transcript_owner: u32,
},
}
impl Selection {
pub(crate) fn snapshot(self) -> Result<Snapshot> {
match self {
Self::Wayland => wayland::snapshot(None),
Self::X11 { .. } => x11::snapshot(),
}
}
/// Whether the clipboard still holds what Hex pasted: the same bytes on
/// Wayland, the paste's own selection owner on X11.
pub(crate) fn holds(self, mime: &str, bytes: &[u8]) -> bool {
match self {
Self::Wayland => matches!(
wayland::snapshot(Some(mime)),
Ok(Snapshot::Wayland(formats)) if formats.first().is_some_and(|(_, data)| data == bytes)
),
Self::X11 { transcript_owner } => {
x11::owner().is_ok_and(|owner| owner == transcript_owner)
}
}
}
pub(crate) fn restore(self, snapshot: Snapshot) -> Result<()> {
match snapshot {
Snapshot::Empty => match self {
Self::Wayland => wayland::serve(Vec::new()),
Self::X11 { .. } => x11::serve(Vec::new()),
},
Snapshot::Wayland(formats) => wayland::serve(formats),
Snapshot::X11(formats) => x11::serve(formats),
}
}
}
fn too_large() -> color_eyre::eyre::Report {
eyre!("the clipboard holds more than {} MiB", CAP / (1024 * 1024))
}
fn pipe() -> Result<(OwnedFd, OwnedFd)> {
let mut fds = [0; 2];
if unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) } < 0 {
return Err(std::io::Error::last_os_error().into());
}
Ok(unsafe { (OwnedFd::from_raw_fd(fds[0]), OwnedFd::from_raw_fd(fds[1])) })
}
fn set_nonblocking(fd: &OwnedFd) -> Result<()> {
let flags = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETFL) };
if flags < 0
|| unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_SETFL, flags | libc::O_NONBLOCK) } < 0
{
return Err(std::io::Error::last_os_error().into());
}
Ok(())
}
/// Waits until `fd` is ready for `events` or the deadline passes.
fn wait_for(fd: RawFd, events: libc::c_short, deadline: Instant) -> Result<()> {
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
return Err(eyre!("timed out"));
}
let mut poll = libc::pollfd {
fd,
events,
revents: 0,
};
let result = unsafe { libc::poll(&mut poll, 1, remaining.as_millis().max(1) as libc::c_int) };
if result < 0 {
let error = std::io::Error::last_os_error();
if error.kind() != ErrorKind::Interrupted {
return Err(error.into());
}
}
Ok(())
}
/// Reads `fd` to its end, failing past `budget` bytes or the deadline.
fn read_bounded(fd: OwnedFd, budget: usize, deadline: Instant) -> Result<Vec<u8>> {
set_nonblocking(&fd)?;
let mut file = std::fs::File::from(fd);
let mut data = Vec::new();
let mut buffer = [0; 64 * 1024];
loop {
match file.read(&mut buffer) {
Ok(0) => return Ok(data),
Ok(read) => {
if data.len() + read > budget {
return Err(too_large());
}
data.extend_from_slice(&buffer[..read]);
}
Err(error) if error.kind() == ErrorKind::Interrupted => {}
Err(error) if error.kind() == ErrorKind::WouldBlock => {
wait_for(file.as_raw_fd(), libc::POLLIN, deadline)?;
}
Err(error) => return Err(error.into()),
}
}
}
/// Writes all of `data` to a reader's `fd`, giving up at the deadline.
fn write_bounded(fd: OwnedFd, data: &[u8], deadline: Instant) -> Result<()> {
set_nonblocking(&fd)?;
let mut file = std::fs::File::from(fd);
let mut remaining = data;
while !remaining.is_empty() {
match file.write(remaining) {
Ok(0) => return Err(eyre!("the reader closed the clipboard pipe")),
Ok(written) => remaining = &remaining[written..],
Err(error) if error.kind() == ErrorKind::Interrupted => {}
Err(error) if error.kind() == ErrorKind::WouldBlock => {
wait_for(file.as_raw_fd(), libc::POLLOUT, deadline)?;
}
Err(error) => return Err(error.into()),
}
}
Ok(())
}
mod wayland {
use std::collections::HashMap;
use std::os::fd::AsFd;
use wayland_client::backend::ObjectId;
use wayland_client::globals::{GlobalListContents, registry_queue_init};
use wayland_client::protocol::wl_registry::{self, WlRegistry};
use wayland_client::protocol::wl_seat::{self, WlSeat};
use wayland_client::{
Connection, Dispatch, EventQueue, Proxy, QueueHandle, event_created_child,
};
use wayland_protocols_wlr::data_control::v1::client::zwlr_data_control_device_v1::{
self, ZwlrDataControlDeviceV1,
};
use wayland_protocols_wlr::data_control::v1::client::zwlr_data_control_manager_v1::{
self, ZwlrDataControlManagerV1,
};
use wayland_protocols_wlr::data_control::v1::client::zwlr_data_control_offer_v1::{
self, ZwlrDataControlOfferV1,
};
use wayland_protocols_wlr::data_control::v1::client::zwlr_data_control_source_v1::{
self, ZwlrDataControlSourceV1,
};
use super::*;
#[derive(Default)]
struct State {
offers: HashMap<ObjectId, Vec<String>>,
/// `None` until the device reports the selection.
selection: Option<Option<ZwlrDataControlOfferV1>>,
served: Vec<(String, Vec<u8>)>,
done: bool,
}
struct Session {
connection: Connection,
queue: EventQueue<State>,
state: State,
manager: ZwlrDataControlManagerV1,
device: ZwlrDataControlDeviceV1,
}
fn connect() -> Result<Session> {
let connection =
Connection::connect_to_env().wrap_err("could not connect to the compositor")?;
let (globals, mut queue) = registry_queue_init::<State>(&connection)?;
let handle = queue.handle();
let manager: ZwlrDataControlManagerV1 = globals
.bind(&handle, 1..=1, ())
.wrap_err("the compositor offers no wlr-data-control clipboard access")?;
let seat: WlSeat = globals
.bind(&handle, 1..=1, ())
.wrap_err("the compositor offers no seat")?;
let device = manager.get_data_device(&seat, &handle, ());
let mut state = State::default();
// The device reports the current selection as soon as it exists.
for _ in 0..2 {
if state.selection.is_some() {
break;
}
queue.roundtrip(&mut state)?;
}
Ok(Session {
connection,
queue,
state,
manager,
device,
})
}
/// Every format of the selection, or only `only`.
pub(super) fn snapshot(only: Option<&str>) -> Result<Snapshot> {
let deadline = Instant::now() + READ_DEADLINE;
let session = connect()?;
let Some(Some(offer)) = session.state.selection.clone() else {
return Ok(Snapshot::Empty);
};
let mimes = session
.state
.offers
.get(&offer.id())
.cloned()
.unwrap_or_default();
let mut formats = Vec::new();
let mut total = 0;
for mime in mimes
.into_iter()
.filter(|mime| only.is_none_or(|only| only == mime))
{
let (read, write) = pipe()?;
offer.receive(mime.clone(), write.as_fd());
drop(write);
session.connection.flush()?;
let data = read_bounded(read, CAP - total, deadline)
.wrap_err_with(|| format!("could not read the clipboard's {mime}"))?;
total += data.len();
formats.push((mime, data));
}
Ok(if formats.is_empty() {
Snapshot::Empty
} else {
Snapshot::Wayland(formats)
})
}
/// Takes the selection with `formats`, or clears it when there are none,
/// and serves it on a thread until another client replaces it.
pub(super) fn serve(formats: Vec<(String, Vec<u8>)>) -> Result<()> {
let mut session = connect()?;
if formats.is_empty() {
session.device.set_selection(None);
session.queue.roundtrip(&mut session.state)?;
session.device.destroy();
return Ok(());
}
let source = session
.manager
.create_data_source(&session.queue.handle(), ());
for (mime, _) in &formats {
source.offer(mime.clone());
}
session.device.set_selection(Some(&source));
session.state.served = formats;
session.queue.roundtrip(&mut session.state)?;
thread::Builder::new()
.name("hex-clipboard-restore".into())
.spawn(move || {
while !session.state.done {
if session.queue.blocking_dispatch(&mut session.state).is_err() {
break;
}
}
source.destroy();
session.device.destroy();
let _ = session.connection.flush();
})?;
Ok(())
}
impl Dispatch<WlRegistry, GlobalListContents> for State {
fn event(
_: &mut Self,
_: &WlRegistry,
_: wl_registry::Event,
_: &GlobalListContents,
_: &Connection,
_: &QueueHandle<Self>,
) {
}
}
impl Dispatch<WlSeat, ()> for State {
fn event(
_: &mut Self,
_: &WlSeat,
_: wl_seat::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
}
}
impl Dispatch<ZwlrDataControlManagerV1, ()> for State {
fn event(
_: &mut Self,
_: &ZwlrDataControlManagerV1,
_: zwlr_data_control_manager_v1::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
}
}
impl Dispatch<ZwlrDataControlDeviceV1, ()> for State {
fn event(
state: &mut Self,
_: &ZwlrDataControlDeviceV1,
event: zwlr_data_control_device_v1::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
match event {
zwlr_data_control_device_v1::Event::DataOffer { id } => {
state.offers.insert(id.id(), Vec::new());
}
zwlr_data_control_device_v1::Event::Selection { id } => {
if let Some(Some(previous)) = state.selection.take()
&& Some(&previous) != id.as_ref()
{
state.offers.remove(&previous.id());
previous.destroy();
}
state.selection = Some(id);
}
zwlr_data_control_device_v1::Event::Finished => state.done = true,
_ => {}
}
}
event_created_child!(State, ZwlrDataControlDeviceV1, [
zwlr_data_control_device_v1::EVT_DATA_OFFER_OPCODE => (ZwlrDataControlOfferV1, ()),
]);
}
impl Dispatch<ZwlrDataControlOfferV1, ()> for State {
fn event(
state: &mut Self,
offer: &ZwlrDataControlOfferV1,
event: zwlr_data_control_offer_v1::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
if let zwlr_data_control_offer_v1::Event::Offer { mime_type } = event {
state.offers.entry(offer.id()).or_default().push(mime_type);
}
}
}
impl Dispatch<ZwlrDataControlSourceV1, ()> for State {
fn event(
state: &mut Self,
_: &ZwlrDataControlSourceV1,
event: zwlr_data_control_source_v1::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
match event {
zwlr_data_control_source_v1::Event::Send { mime_type, fd } => {
if let Some((_, data)) =
state.served.iter().find(|(mime, _)| *mime == mime_type)
&& let Err(error) = write_bounded(fd, data, Instant::now() + SERVE_DEADLINE)
{
tracing::debug!(
error = format!("{error:#}"),
mime_type,
"could not serve the restored clipboard"
);
}
}
zwlr_data_control_source_v1::Event::Cancelled => state.done = true,
_ => {}
}
}
}
}
mod x11 {
use std::collections::HashMap;
use x11rb::connection::{Connection, RequestConnection};
use x11rb::protocol::Event;
use x11rb::protocol::xproto::{
AtomEnum, ChangeWindowAttributesAux, ConnectionExt, CreateWindowAux, EventMask, PropMode,
Property, SELECTION_NOTIFY_EVENT, SelectionNotifyEvent, Window, WindowClass,
};
use x11rb::rust_connection::RustConnection;
use x11rb::wrapper::ConnectionExt as _;
use x11rb::{COPY_DEPTH_FROM_PARENT, COPY_FROM_PARENT, CURRENT_TIME, NONE};
use super::*;
x11rb::atom_manager! {
Atoms: AtomsCookie {
CLIPBOARD,
TARGETS,
MULTIPLE,
TIMESTAMP,
SAVE_TARGETS,
DELETE,
INCR,
HEX_CLIPBOARD,
}
}
fn connect() -> Result<(RustConnection, usize, Atoms)> {
let (connection, screen) =
x11rb::connect(None).wrap_err("could not connect to the X server")?;
let atoms = Atoms::new(&connection)?.reply()?;
Ok((connection, screen, atoms))
}
fn window(connection: &RustConnection, screen: usize) -> Result<Window> {
let root = connection.setup().roots[screen].root;
let window = connection.generate_id()?;
connection
.create_window(
COPY_DEPTH_FROM_PARENT,
window,
root,
0,
0,
1,
1,
0,
WindowClass::INPUT_OUTPUT,
COPY_FROM_PARENT,
&CreateWindowAux::new().event_mask(EventMask::PROPERTY_CHANGE),
)?
.check()?;
Ok(window)
}
pub(super) fn owner() -> Result<u32> {
let (connection, _, atoms) = connect()?;
Ok(connection
.get_selection_owner(atoms.CLIPBOARD)?
.reply()?
.owner)
}
fn next_event(connection: &RustConnection, deadline: Instant) -> Result<Event> {
loop {
if let Some(event) = connection.poll_for_event()? {
return Ok(event);
}
if Instant::now() >= deadline {
return Err(eyre!("the clipboard owner did not answer in time"));
}
thread::sleep(Duration::from_millis(2));
}
}
pub(super) fn snapshot() -> Result<Snapshot> {
let deadline = Instant::now() + READ_DEADLINE;
let (connection, screen, atoms) = connect()?;
if connection
.get_selection_owner(atoms.CLIPBOARD)?
.reply()?
.owner
== NONE
{
return Ok(Snapshot::Empty);
}
let window = window(&connection, screen)?;
let result = (|| {
let Some(targets) = convert(&connection, &atoms, window, atoms.TARGETS, CAP, deadline)?
else {
return Ok(Snapshot::Empty);
};
let skipped = [
atoms.TARGETS,
atoms.MULTIPLE,
atoms.TIMESTAMP,
atoms.SAVE_TARGETS,
atoms.DELETE,
];
let mut formats = Vec::new();
let mut total = 0;
for target in targets
.data
.as_chunks::<4>()
.0
.iter()
.map(|bytes| u32::from_ne_bytes(*bytes))
.filter(|target| !skipped.contains(target))
{
if let Some(format) =
convert(&connection, &atoms, window, target, CAP - total, deadline)?
{
total += format.data.len();
formats.push(format);
}
}
Ok(if formats.is_empty() {
Snapshot::Empty
} else {
Snapshot::X11(formats)
})
})();
let _ = connection.destroy_window(window);
let _ = connection.flush();
result
}
/// One target of the clipboard, following INCR transfers.
fn convert(
connection: &RustConnection,
atoms: &Atoms,
window: Window,
target: u32,
budget: usize,
deadline: Instant,
) -> Result<Option<X11Format>> {
let property = atoms.HEX_CLIPBOARD;
connection.delete_property(window, property)?;
connection.convert_selection(window, atoms.CLIPBOARD, target, property, CURRENT_TIME)?;
connection.flush()?;
let notify = loop {
if let Event::SelectionNotify(notify) = next_event(connection, deadline)?
&& notify.requestor == window
&& notify.target == target
{
break notify;
}
};
if notify.property == NONE {
return Ok(None);
}
let reply = connection
.get_property(true, window, property, AtomEnum::ANY, 0, u32::MAX)?
.reply()?;
if reply.type_ != atoms.INCR {
if reply.value.len() > budget {
return Err(too_large());
}
return Ok(Some(X11Format {
target,
type_: reply.type_,
format: reply.format,
data: reply.value,
}));
}
connection.flush()?;
let mut format = X11Format {
target,
type_: NONE,
format: 8,
data: Vec::new(),
};
loop {
match next_event(connection, deadline)? {
Event::PropertyNotify(event)
if event.window == window
&& event.atom == property
&& event.state == Property::NEW_VALUE =>
{
let chunk = connection
.get_property(true, window, property, AtomEnum::ANY, 0, u32::MAX)?
.reply()?;
connection.flush()?;
if chunk.value.is_empty() {
return Ok(Some(format));
}
if format.data.len() + chunk.value.len() > budget {
return Err(too_large());
}
format.type_ = chunk.type_;
format.format = chunk.format;
format.data.extend_from_slice(&chunk.value);
}
_ => {}
}
}
}
/// Takes the clipboard with `formats`, or clears it when there are none,
/// and serves it on a thread until another client takes it.
pub(super) fn serve(formats: Vec<X11Format>) -> Result<()> {
let (connection, screen, atoms) = connect()?;
if formats.is_empty() {
connection
.set_selection_owner(NONE, atoms.CLIPBOARD, CURRENT_TIME)?
.check()?;
return Ok(());
}
let window = window(&connection, screen)?;
connection
.set_selection_owner(window, atoms.CLIPBOARD, CURRENT_TIME)?
.check()?;
if connection
.get_selection_owner(atoms.CLIPBOARD)?
.reply()?
.owner
!= window
{
return Err(eyre!("could not own the X11 clipboard"));
}
thread::Builder::new()
.name("hex-clipboard-restore".into())
.spawn(move || serve_until_cleared(&connection, &atoms, window, &formats))?;
Ok(())
}
fn serve_until_cleared(
connection: &RustConnection,
atoms: &Atoms,
window: Window,
formats: &[X11Format],
) {
// Whole units of every format, well inside the request limit.
let chunk = (connection.maximum_request_bytes() / 2).min(256 * 1024) & !3;
// INCR transfers by requestor window and property: format and offset.
let mut transfers = HashMap::<(Window, u32), (usize, usize)>::new();
while let Ok(event) = connection.wait_for_event() {
match event {
Event::SelectionRequest(request) => {
// Obsolete clients name no property; use the target.
let property = if request.property == NONE {
request.target
} else {
request.property
};
let served = if request.target == atoms.TARGETS {
let targets: Vec<u32> = std::iter::once(atoms.TARGETS)
.chain(formats.iter().map(|format| format.target))
.collect();
connection
.change_property32(
PropMode::REPLACE,
request.requestor,
property,
AtomEnum::ATOM,
&targets,
)
.is_ok()
} else if let Some(index) = formats
.iter()
.position(|format| format.target == request.target)
{
let format = &formats[index];
if format.data.len() <= chunk {
put(
connection,
request.requestor,
property,
format,
&format.data,
)
} else {
let _ = connection.change_window_attributes(
request.requestor,
&ChangeWindowAttributesAux::new()
.event_mask(EventMask::PROPERTY_CHANGE),
);
transfers.insert((request.requestor, property), (index, 0));
connection
.change_property32(
PropMode::REPLACE,
request.requestor,
property,
atoms.INCR,
&[format.data.len() as u32],
)
.is_ok()
}
} else {
false
};
let _ = connection.send_event(
false,
request.requestor,
EventMask::default(),
SelectionNotifyEvent {
response_type: SELECTION_NOTIFY_EVENT,
sequence: 0,
time: request.time,
requestor: request.requestor,
selection: request.selection,
target: request.target,
property: if served { property } else { NONE },
},
);
}
Event::PropertyNotify(event) if event.state == Property::DELETE => {
let key = (event.window, event.atom);
if let Some((index, offset)) = transfers.get_mut(&key) {
let format = &formats[*index];
let end = (*offset + chunk).min(format.data.len());
put(
connection,
event.window,
event.atom,
format,
&format.data[*offset..end],
);
if *offset == end {
transfers.remove(&key);
} else {
*offset = end;
}
}
}
Event::SelectionClear(_) => break,
_ => {}
}
let _ = connection.flush();
}
let _ = connection.destroy_window(window);
let _ = connection.flush();
}
fn put(
connection: &RustConnection,
window: Window,
property: u32,
format: &X11Format,
data: &[u8],
) -> bool {
let unit = usize::from(format.format / 8).max(1);
connection
.change_property(
PropMode::REPLACE,
window,
property,
format.type_,
format.format,
(data.len() / unit) as u32,
data,
)
.is_ok()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reading_stops_at_the_cap() {
let (read, write) = pipe().unwrap();
let writer = thread::spawn(move || {
let mut file = std::fs::File::from(write);
let _ = file.write_all(&[7; 4096]);
});
let result = read_bounded(read, 1024, Instant::now() + Duration::from_secs(2));
writer.join().unwrap();
assert!(result.is_err());
}
#[test]
fn reading_returns_everything_within_the_cap() {
let (read, write) = pipe().unwrap();
let writer = thread::spawn(move || {
std::fs::File::from(write).write_all(b"original").unwrap();
});
let data = read_bounded(read, 1024, Instant::now() + Duration::from_secs(2)).unwrap();
writer.join().unwrap();
assert_eq!(data, b"original");
}
#[test]
fn a_silent_owner_times_out() {
let (read, _write) = pipe().unwrap();
let start = Instant::now();
assert!(read_bounded(read, 1024, Instant::now() + Duration::from_millis(100)).is_err());
assert!(start.elapsed() < Duration::from_secs(1));
}
}