Skip to content

Commit 4ab83c7

Browse files
IAmJSDclaude
andcommitted
Put images on the Linux clipboards, not an empty string
Both Linux backends serialised a ClipboardItem as `item.text().unwrap_or_default()` and nothing else, so copying a `ClipboardItem::new_image` -- a region of a picture -- reached the system clipboard as an empty string and every other application pasted nothing. Reading an image *into* gpui already worked, and copying within one process worked off the cached item, which is what kept this hidden. X11 now offers every entry of the item at once: each image under its own MIME atom, the text as UTF8_STRING. `Clipboard::set_image` had the same bug in miniature -- it computed the format atom and then wrote `image/png` regardless -- and is gone along with `set_text`, both replaced by `set_item`. Serving a selection honestly turned up two smaller faults in the same path. TARGETS advertises the two MIME spellings of UTF8_STRING alongside it, but the content branch only matched the exact atom, so a requestor that picked one of the spellings we offered was refused. And a property write that fails -- there is no INCR on the write side, so a selection too large for one request cannot be sent -- returned early without answering, leaving the requestor to sit until its own timeout; it now replies with None so the paste fails at once. A megabyte goes through in a single request under BIG-REQUESTS, which is the size a copied image actually is. Wayland offers the image MIME types on the data source, and `send` / `send_primary` write the mime type they are handed rather than always the text. The X11 tests request the selection over the wire from a second connection, which is a second X client as far as the server is concerned, so they exercise the real path rather than the owner short-circuit; they need a display and no-op without one. Wayland stays compile-reviewed, as the rest of that backend is. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 84bdb01 commit 4ab83c7

5 files changed

Lines changed: 309 additions & 46 deletions

File tree

‎UPSTREAM.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,35 @@ time.
102102
pads unconditionally and a `new_id` with no registered child handler
103103
panics the queue.
104104

105+
- **Images on the Linux clipboards.** Both Linux backends wrote
106+
`item.text().unwrap_or_default()` and nothing else, so copying a
107+
`ClipboardItem::new_image` -- a region of a picture -- put an empty string
108+
on the system clipboard and every other application pasted nothing.
109+
(Pasting *into* gpui already read images, and copying inside one process
110+
worked off the cached item, which is what hid it.)
111+
112+
X11 now offers every entry of the item at once: each image under its own
113+
MIME atom, the text as `UTF8_STRING`. `Clipboard::set_image` had the same
114+
bug in miniature -- it computed the format atom and then hardcoded
115+
`image/png` -- and is gone along with `set_text`, both replaced by
116+
`set_item`. Two smaller fixes fell out of serving a selection honestly:
117+
`TARGETS` advertises the two MIME spellings of `UTF8_STRING` but the
118+
content path only matched the exact atom, so a requestor that picked one
119+
of them was refused; and a property write that fails (there is no INCR on
120+
the write side, so a selection larger than one request cannot be sent)
121+
now answers with `None` instead of leaving the requestor to time out. A
122+
megabyte goes through in one request under BIG-REQUESTS, which covers
123+
what a copied image weighs.
124+
125+
Wayland offers the image MIME types on the data source, and `send` /
126+
`send_primary` honour the mime type they are handed rather than always
127+
writing the text.
128+
129+
The X11 path has unit tests (`platform::linux::x11::clipboard`) that go
130+
over the wire from a second connection -- a real second X client -- so
131+
they need a display; they no-op when `DISPLAY` is unset. Wayland is
132+
compile-reviewed as ever.
133+
105134
Of these backends only X11 could be exercised on real input during
106135
development, and only for the mouse (pressure-less) path; Xvfb cannot
107136
synthesise gestures or tablets. macOS, Wayland and Windows are

‎src/platform/linux/wayland/client.rs‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ use crate::{
9393
LinuxClient, get_xkb_compose_state, is_within_click_distance, open_uri_internal, read_fd,
9494
reveal_path_internal,
9595
wayland::{
96-
clipboard::{Clipboard, DataOffer, FILE_LIST_MIME_TYPE, TEXT_MIME_TYPES},
96+
clipboard::{Clipboard, DataOffer, FILE_LIST_MIME_TYPE, offered_mime_types},
9797
cursor::Cursor,
9898
serial::{SerialKind, SerialTracker},
9999
window::WaylandWindow,
@@ -971,11 +971,12 @@ impl LinuxClient for WaylandClient {
971971
return;
972972
};
973973
if state.mouse_focused_window.is_some() || state.keyboard_focused_window.is_some() {
974+
let mime_types = offered_mime_types(&item);
974975
state.clipboard.set_primary(item);
975976
let serial = state.serial_tracker.get(SerialKind::KeyPress);
976977
let data_source = primary_selection_manager.create_source(&state.globals.qh, ());
977-
for mime_type in TEXT_MIME_TYPES {
978-
data_source.offer(mime_type.to_string());
978+
for mime_type in mime_types {
979+
data_source.offer(mime_type);
979980
}
980981
data_source.offer(state.clipboard.self_mime());
981982
primary_selection.set_selection(Some(&data_source), serial);
@@ -991,11 +992,12 @@ impl LinuxClient for WaylandClient {
991992
return;
992993
};
993994
if state.mouse_focused_window.is_some() || state.keyboard_focused_window.is_some() {
995+
let mime_types = offered_mime_types(&item);
994996
state.clipboard.set(item);
995997
let serial = state.serial_tracker.get(SerialKind::KeyPress);
996998
let data_source = data_device_manager.create_data_source(&state.globals.qh, ());
997-
for mime_type in TEXT_MIME_TYPES {
998-
data_source.offer(mime_type.to_string());
999+
for mime_type in mime_types {
1000+
data_source.offer(mime_type);
9991001
}
10001002
data_source.offer(state.clipboard.self_mime());
10011003
data_device.set_selection(Some(&data_source), serial);

‎src/platform/linux/wayland/clipboard.rs‎

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,41 @@ impl<T: ReceiveData> DataOffer<T> {
137137
}
138138
}
139139

140+
/// The mime types to offer for `item`: one per image entry, plus the text
141+
/// spellings when it has text.
142+
///
143+
/// An image-only item -- a copied region of a picture -- used to be offered
144+
/// as text alone and then sent as nothing at all, so every other
145+
/// application pasted an empty selection.
146+
pub(crate) fn offered_mime_types(item: &ClipboardItem) -> Vec<String> {
147+
let mut mime_types: Vec<String> = item
148+
.entries()
149+
.iter()
150+
.filter_map(|entry| match entry {
151+
ClipboardEntry::Image(image) => Some(image.format.mime_type().to_string()),
152+
ClipboardEntry::String(_) => None,
153+
})
154+
.collect();
155+
if item.text().is_some() {
156+
mime_types.extend(TEXT_MIME_TYPES.iter().map(|mime| mime.to_string()));
157+
}
158+
mime_types
159+
}
160+
161+
/// What to write down the pipe for the mime type a paste asked for.
162+
fn bytes_for_mime(item: Option<&ClipboardItem>, mime_type: &str) -> Option<Vec<u8>> {
163+
let item = item?;
164+
for entry in item.entries() {
165+
if let ClipboardEntry::Image(image) = entry {
166+
if image.format.mime_type() == mime_type {
167+
return Some(image.bytes.clone());
168+
}
169+
}
170+
}
171+
// Anything else is a text request, including our own `self_mime` probe.
172+
item.text().map(String::into_bytes)
173+
}
174+
140175
impl Clipboard {
141176
pub fn new(
142177
connection: Connection,
@@ -179,19 +214,15 @@ impl Clipboard {
179214
self.self_mime.clone()
180215
}
181216

182-
pub fn send(&self, _mime_type: String, fd: OwnedFd) {
183-
if let Some(text) = self.contents.as_ref().and_then(|contents| contents.text()) {
184-
self.send_internal(fd, text.as_bytes().to_owned());
217+
pub fn send(&self, mime_type: String, fd: OwnedFd) {
218+
if let Some(bytes) = bytes_for_mime(self.contents.as_ref(), &mime_type) {
219+
self.send_internal(fd, bytes);
185220
}
186221
}
187222

188-
pub fn send_primary(&self, _mime_type: String, fd: OwnedFd) {
189-
if let Some(text) = self
190-
.primary_contents
191-
.as_ref()
192-
.and_then(|contents| contents.text())
193-
{
194-
self.send_internal(fd, text.as_bytes().to_owned());
223+
pub fn send_primary(&self, mime_type: String, fd: OwnedFd) {
224+
if let Some(bytes) = bytes_for_mime(self.primary_contents.as_ref(), &mime_type) {
225+
self.send_internal(fd, bytes);
195226
}
196227
}
197228

‎src/platform/linux/x11/client.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,8 +1670,8 @@ impl LinuxClient for X11Client {
16701670
let state = self.0.borrow_mut();
16711671
state
16721672
.clipboard
1673-
.set_text(
1674-
std::borrow::Cow::Owned(item.text().unwrap_or_default()),
1673+
.set_item(
1674+
&item,
16751675
clipboard::ClipboardKind::Primary,
16761676
clipboard::WaitConfig::None,
16771677
)
@@ -1683,8 +1683,8 @@ impl LinuxClient for X11Client {
16831683
let mut state = self.0.borrow_mut();
16841684
state
16851685
.clipboard
1686-
.set_text(
1687-
std::borrow::Cow::Owned(item.text().unwrap_or_default()),
1686+
.set_item(
1687+
&item,
16881688
clipboard::ClipboardKind::Clipboard,
16891689
clipboard::WaitConfig::None,
16901690
)

0 commit comments

Comments
 (0)