1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT
//! The Linux/GTK system clipboard, over GDK.
//!
//! # What this closes
//!
//! Both halves of the clipboard were wrong in the same direction:
//!
//! * `set_clipboard_text` published to the GDK clipboard but `get_clipboard_text` read the
//! in-process `BackendState` record, so what a copy put on the display could not be read back —
//! the read answered from a mirror that only this process writes.
//! * `clipboard_backend()` was never overridden, so `platform_clipboard()` answered `None` on
//! Linux even in a `gtk-native` build, and a host could not tell "no clipboard here" from "no
//! rich clipboard here".
//!
//! This module is the rich half: one GDK-backed [`RichClipboardBackend`], plus the plain-text
//! read/write the backend and the `Platform` methods share, so there is exactly one path to the
//! display clipboard.
//!
//! # Why GDK and not the X11/XWayland selection protocol
//!
//! GDK is already linked in this build (`gtk-native`), it speaks both X11 and XWayland selections,
//! and its `wait_for_*` family runs the nested main loop the asynchronous protocol needs. Writing
//! the protocol by hand would mean reimplementing `TARGETS`/`SELECTION_REQUEST` ownership for no
//! gain, and the owner-callback lifetime problem it brings is the same one GDK already solves.
//!
//! # The main-thread rule
//!
//! Every entry point checks `gtk::is_initialized_main_thread()` first, the same rule the rest of
//! this backend follows (see the `Send` note in `linux/types.rs`): GTK objects may only be touched
//! from the thread that called `gtk::init`. A call from anywhere else answers `None`/`false`
//! rather than touching GTK, which is what lets the caller fall back to its own record.
//!
//! [`RichClipboardBackend`]: crate::platform::clipboard::RichClipboardBackend
use crateString;
use crate;
use ;
/// The MIME type a plain-text payload is published under.
const PLAIN_TEXT: &str = "text/plain";
/// The MIME type an HTML payload is published under.
const HTML: &str = "text/html";
/// The `TargetEntry::info` value that selects the HTML arm of the owner callback.
const HTML_INFO: u32 = 1;
/// The `TargetEntry::info` value that selects the plain-text arm of the owner callback.
const PLAIN_INFO: u32 = 2;
/// Runs `body` with the display's clipboard, or `None` when this thread has no usable one.
/// `true` when this build can reach a display clipboard at all.
///
/// Answered from the same two facts the callers check, so the capability flag, the trait methods
/// and this predicate cannot disagree: GTK is initialised on this thread and a default display
/// exists. A `false` here is a real "no clipboard on this host right now" — the interface has
/// `GtkClipboard` but no X display behind it (a headless SSH session, for instance).
pub
/// A revision bumped by every `owner-change` signal the display clipboard emits.
///
/// # Why a counter rather than a callback (P-202)
///
/// [`Platform::clipboard_generation`](crate::platform::Platform::clipboard_generation) is a
/// polled revision by contract: a clipboard-manager host compares consecutive readings and
/// re-reads the clipboard when they differ, because the library cannot push an event onto a host
/// loop it does not own. GDK publishes exactly the underlying fact — `owner-change` fires whenever
/// the selection's owner changes, including when this process takes it by writing — so the
/// counter is a real measurement, not a derived guess.
static GENERATION: AtomicU64 = new;
/// The clipboard object the `owner-change` handler is attached to, as a raw pointer.
///
/// A display swap (an X session replacing its display) hands out a new `GtkClipboard`, and a
/// handler left on the old object would never fire again. Comparing the pointer re-arms the watch
/// for the new object and leaves at most one stale handler on the dead one, which is harmless.
static WATCHED_CLIPBOARD: AtomicUsize = new;
/// Attaches the `owner-change` handler to `clipboard`, once per clipboard object.
/// A revision that changes whenever the display clipboard's owner changes.
///
/// `0` until a handler could be attached — no GTK main thread, no display, or no clipboard — which
/// is the trait's honest "this backend cannot report it". After that it is always non-zero, so a
/// host that saw a reading can compare it against the next one.
pub
/// Puts `text` on the display clipboard as plain text.
///
/// The answer is a **read-back**: `gtk_clipboard_set_text` returns nothing, and a clipboard the
/// display refused (or a nested main loop that never got the selection) would otherwise be
/// reported as a successful write. GDK owns the selection here, so the read does not need another
/// process to answer it.
///
/// # Why the empty string is confirmed the other way round
///
/// GDK has no "empty string" selection: `set_text("")` leaves the clipboard holding **no text**,
/// and `wait_for_text()` then answers `None` — the same answer `clear()` gives. Measured on a live
/// display during this round (GTK 3, quartz backend):
///
/// ```text
/// after set_text("something"): Some("something")
/// after set_text(""): None
/// after clear(): None
/// ```
///
/// So for `""` the confirmation is "there is now no text", which is what the caller asked for.
/// Comparing the string back in that case would report a successful write as a failure.
pub
/// The display clipboard's plain text, or `None` when it holds none.
pub
/// Publishes `html` (with `plain` as the fallback representation) on the display clipboard.
///
/// HTML has no `gtk_clipboard_set_*` helper: markup travels as an **owner-provided** target, which
/// is the `gtk_clipboard_set_with_data` shape. The callback below is what the display calls when
/// another application asks for the selection, and it must answer for the target it was asked
/// about — the `info` field of the [`gtk::TargetEntry`] is how the request says which one.
///
/// # The two channels, and what "success" means
///
/// `text/html` and a plain-text representation are published together, and the write is reported
/// as successful when **either** landed — the same rule the macOS (`setString:forType:` twice) and
/// iOS backends apply. A receiver without markup support reads the plain channel, and that is a
/// real delivery of what the caller asked for.
///
/// # The verified boundary
///
/// The plain channel is verified end-to-end (see the module tests and `status.md`); the **markup**
/// channel is not. On the one host where GDK could be run against a live display during this
/// round (GTK 3 on macOS, whose quartz backend maps selections onto `NSPasteboard`), a
/// `SelectionData::set` for a non-text target came back **empty** on the reading side: GDK's
/// quartz backend serves only UTF8_STRING from owner-provided data. `SelectionData::set` with
/// format 8 and the requested target is the documented mechanism on X11 and XWayland, which is
/// what this backend targets, but this crate has no X11 host in its gate suite, so the markup arm
/// is reported here as *unverified on this host* rather than claimed.
pub
/// The display clipboard's HTML, or `None` when it holds none.
pub
/// Whether the display clipboard holds data of `content_type` (a MIME type).
pub
/// Whether the display clipboard offers `content_type` as a selection target.
///
/// The read is the evidence: `wait_for_contents` returns `Some` exactly when the requested target
/// is offered, so asking for the contents with an empty payload still answers "the target is
/// there" rather than "the payload is non-empty".
/// Publishes `data` under the application-defined `content_type` and reports whether it was stored.
///
/// This is the generic form of [`set_html`]: the type is interned as its own selection target and
/// served from an owner callback, which is the documented X11/XWayland mechanism for any MIME type.
/// The write is confirmed by reading the target back (the same evidence rule `set_html` follows),
/// because `set_with_data` only reports that the selection was *offered*.
///
/// # The verified boundary
///
/// The same one `set_html`'s markup arm records: the owner-provided-target mechanism is what X11
/// and XWayland specify, and this crate has no X11 host in its gate suite, so a custom target is
/// reported as *verified where a display serves it* (the `MockClipboard` round-trip) and otherwise
/// as *unverified on this host* rather than claimed.
pub
/// The bytes the display clipboard holds under `content_type`, or `None` when it holds none.
pub
/// The Linux/GTK host clipboard.
///
/// # What it carries, and what it does not
///
/// Text, HTML, and any application-defined MIME type (an X11/Wayland selection target). Text and
/// HTML have dedicated helpers above; a custom type goes through the generic owner-provided-target
/// path in [`set_typed`]/[`typed`], which is the same mechanism the HTML markup arm uses.
///
/// The remaining *structured* kinds are refused (`false` / `None`) rather than silently dropped:
///
/// * an image would have to become a `gdk_pixbuf::Pixbuf`, and the RGBA buffer this crate carries
/// is `&[u8]` with no guaranteed lifetime for a zero-copy wrapper — a copy would double the
/// allocation for a payload whose only consumer is another application;
/// * RTF has no GDK target of its own and no verifier in this crate's gate suite;
/// * a file list would have to become `text/uri-list`, and a URI list is a set of *URIs*, not
/// paths, so a `PathBuf` cannot be published without a claim about how it should be encoded.
///
/// An application that wants to move any of those payloads by name can still do so through
/// [`RichClipboardBackend::set_contents_with_type`], which publishes the bytes under its own type.
///
/// # State
///
/// None. GDK is the state: the clipboard is looked up per call, which is also what keeps this type
/// `Send + Sync` (the trait requires it) even though `gtk::Clipboard` is neither.
;