medialinker-cli 0.6.2

Share files and text through MediaLinker with 5-character codes, and keep one clipboard on all your devices
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
//! The system clipboard: one thread owns it, watches it for copies and applies clips from other
//! devices.
//!
//! Every content gets a fingerprint, and the thread remembers the fingerprint of whatever is on the
//! clipboard now. After applying a clip it reads the clipboard back and remembers what the system
//! actually stored, so its own write (converted to CRLF on Windows, re-encoded as TIFF on macOS)
//! never comes back as a new copy and no clip ever bounces between devices.
//!
//! Windows and macOS count clipboard changes, so the clipboard is only read when that count moves.
//! On Linux an X11 connection reports owner changes when there is one (Xwayland included), and a
//! slow full check covers anything it misses.

use std::{
    borrow::Cow,
    path::PathBuf,
    sync::mpsc::{self, RecvTimeoutError, Sender},
    thread,
    time::{Duration, Instant},
};

use arboard::{Clipboard, Error as ClipError, ImageData};
use sha2::{Digest, Sha256};

const POLL: Duration = Duration::from_millis(300);
/// Without change notifications, images (costly to read) and anything missed are checked this often.
const FULL_CHECK: Duration = Duration::from_secs(2);

#[derive(Clone, PartialEq, Debug)]
pub enum Content {
    /// Always with `\n` line breaks.
    Text(String),
    /// RGBA pixels.
    Image {
        width: usize,
        height: usize,
        rgba: Vec<u8>,
    },
    Files(Vec<PathBuf>),
}

pub type Fingerprint = [u8; 16];

impl Content {
    pub fn fingerprint(&self) -> Fingerprint {
        let mut h = Sha256::new();
        match self {
            Content::Text(t) => {
                h.update(b"text\0");
                h.update(t.as_bytes());
            }
            Content::Image { width, height, rgba } => {
                h.update(b"image\0");
                h.update((*width as u64).to_le_bytes());
                h.update((*height as u64).to_le_bytes());
                h.update(rgba);
            }
            Content::Files(paths) => {
                h.update(b"files\0");
                for p in paths {
                    h.update(p.to_string_lossy().as_bytes());
                    h.update(b"\0");
                    // Copying the same file again after editing it is a new copy.
                    if let Ok(meta) = std::fs::metadata(p) {
                        h.update(meta.len().to_le_bytes());
                        let modified = meta
                            .modified()
                            .ok()
                            .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok());
                        h.update(modified.unwrap_or_default().as_nanos().to_le_bytes());
                    }
                }
            }
        }
        h.finalize()[..16].try_into().unwrap()
    }
}

pub fn normalize_text(text: String) -> String {
    if text.contains('\r') {
        text.replace("\r\n", "\n")
    } else {
        text
    }
}

pub enum Command {
    /// Puts a clip on the clipboard, unless something was copied here after copy number `copies`,
    /// the last one the caller had seen when it decided: then the local copy is newer and wins.
    Apply { content: Content, copies: u64 },
    /// Stops the thread and hands back the clip it last applied, if that's still on the clipboard.
    Release(Sender<Option<Content>>),
}

/// Tells the clipboard thread what to do; dropping it stops the thread.
pub struct Board {
    tx: Sender<Command>,
}

impl Board {
    pub fn apply(&self, content: Content, copies: u64) {
        let _ = self.tx.send(Command::Apply { content, copies });
    }

    /// Stops watching. Returns the clip from another device that is still on the clipboard: on
    /// Linux it disappears when this process exits, unless something keeps offering it.
    pub fn release(&self) -> Option<Content> {
        let (tx, rx) = mpsc::channel();
        self.tx.send(Command::Release(tx)).ok()?;
        rx.recv_timeout(Duration::from_secs(2)).ok().flatten()
    }
}

/// Opens the clipboard on its own thread and reports each new local copy, numbered from 1, through
/// `copied`.
pub fn spawn(copied: impl Fn(Content, u64) + Send + 'static) -> Result<Board, String> {
    let (tx, rx) = mpsc::channel();
    let (ready_tx, ready_rx) = mpsc::channel();
    thread::Builder::new()
        .name("clipboard".into())
        .spawn(move || {
            let mut clipboard = match Clipboard::new() {
                Ok(c) => {
                    let _ = ready_tx.send(Ok(()));
                    c
                }
                Err(e) => {
                    let _ = ready_tx.send(Err(format!("cannot open the clipboard: {e}")));
                    return;
                }
            };
            let mut w = Watcher {
                changes: Changes::new(),
                last: None,
                seen: None,
                full_check: Instant::now(),
                copies: 0,
                applied: None,
            };
            // Whatever is on the clipboard at start was copied before syncing began; it isn't news.
            w.last = read(&mut clipboard, true).ok().flatten().map(|c| c.fingerprint());
            w.seen = w.changes.count();
            loop {
                match rx.recv_timeout(POLL) {
                    Ok(Command::Apply { content, copies }) => {
                        // A copy made here since the caller decided is newer; it wins, and so does one
                        // that is on the clipboard but not noticed yet.
                        if copies < w.copies || w.check(&mut clipboard, true, &copied) {
                            continue;
                        }
                        let applied = content.fingerprint();
                        // Windows lets one program at a time hold the clipboard, so a busy one is retried.
                        let mut attempt = 0;
                        let written = loop {
                            match write(&mut clipboard, &content) {
                                Ok(()) => break true,
                                Err(e) if attempt == 4 => {
                                    eprintln!("Cannot put the clip on the clipboard: {e}");
                                    break false;
                                }
                                Err(_) => {
                                    attempt += 1;
                                    thread::sleep(Duration::from_millis(100 * attempt));
                                }
                            }
                        };
                        if !written {
                            continue;
                        }
                        // Some systems hand out the new content a moment after it was set.
                        thread::sleep(Duration::from_millis(50));
                        let stored = read(&mut clipboard, true).ok().flatten();
                        w.last = Some(stored.map_or(applied, |c| c.fingerprint()));
                        w.seen = w.changes.count();
                        w.applied = Some(content);
                    }
                    Ok(Command::Release(reply)) => {
                        w.check(&mut clipboard, true, &copied);
                        let _ = reply.send(w.applied.take());
                        return;
                    }
                    Err(RecvTimeoutError::Timeout) => {
                        w.check(&mut clipboard, false, &copied);
                    }
                    Err(RecvTimeoutError::Disconnected) => return,
                }
            }
        })
        .map_err(|e| format!("cannot start the clipboard thread: {e}"))?;
    ready_rx
        .recv()
        .map_err(|_| "the clipboard thread stopped".to_string())??;
    Ok(Board { tx })
}

struct Watcher {
    changes: Changes,
    /// The fingerprint of what's on the clipboard, as far as we know.
    last: Option<Fingerprint>,
    /// The change count when the clipboard was last read.
    seen: Option<u64>,
    full_check: Instant,
    copies: u64,
    /// The clip from another device on the clipboard now, until something is copied here.
    applied: Option<Content>,
}

impl Watcher {
    /// Looks for a new local copy and reports it; returns whether there was one.
    fn check(&mut self, clipboard: &mut Clipboard, now: bool, copied: &impl Fn(Content, u64)) -> bool {
        let count = self.changes.count();
        let full = now || self.full_check.elapsed() >= FULL_CHECK;
        if count.is_some() && count == self.seen && !full {
            return false;
        }
        if full {
            self.full_check = Instant::now();
        }
        // Reading an image means decoding it, so without a change count it waits for the full check.
        // Busy (Windows lets one program hold the clipboard at a time): try again next round.
        let Ok(content) = read(clipboard, count.is_some() || full) else {
            return false;
        };
        self.seen = count;
        let Some(content) = content else { return false };
        let fp = content.fingerprint();
        if self.last == Some(fp) {
            return false;
        }
        self.last = Some(fp);
        self.applied = None;
        self.copies += 1;
        copied(content, self.copies);
        true
    }
}

/// What's on the clipboard, preferring files, then text, then an image; `Err` if it couldn't be read now.
fn read(clipboard: &mut Clipboard, images: bool) -> Result<Option<Content>, ()> {
    fn absent<T>(r: Result<T, ClipError>) -> Result<Option<T>, ()> {
        match r {
            Ok(v) => Ok(Some(v)),
            Err(ClipError::ContentNotAvailable | ClipError::ConversionFailure) => Ok(None),
            Err(ClipError::ClipboardOccupied) => Err(()),
            // Anything else (an odd format, a vanished owner) is as good as empty.
            Err(_) => Ok(None),
        }
    }
    if let Some(files) = absent(clipboard.get().file_list())?
        && !files.is_empty()
    {
        return Ok(Some(Content::Files(files)));
    }
    if let Some(text) = absent(clipboard.get_text())?
        && !text.is_empty()
    {
        return Ok(Some(Content::Text(normalize_text(text))));
    }
    if images && let Some(img) = absent(clipboard.get_image())? {
        return Ok(Some(Content::Image {
            width: img.width,
            height: img.height,
            rgba: img.bytes.into_owned(),
        }));
    }
    Ok(None)
}

fn write(clipboard: &mut Clipboard, content: &Content) -> Result<(), ClipError> {
    match content {
        Content::Text(text) => {
            if cfg!(windows) {
                clipboard.set_text(text.replace('\n', "\r\n"))
            } else {
                clipboard.set_text(text.as_str())
            }
        }
        Content::Image { width, height, rgba } => clipboard.set_image(ImageData {
            width: *width,
            height: *height,
            bytes: Cow::Borrowed(rgba),
        }),
        #[cfg(target_os = "linux")]
        Content::Files(paths) => {
            super::linux_files::set(paths, false).map_err(|description| ClipError::Unknown { description })
        }
        #[cfg(windows)]
        Content::Files(paths) => {
            super::windows_files::set(paths).map_err(|description| ClipError::Unknown { description })
        }
        #[cfg(not(any(target_os = "linux", windows)))]
        Content::Files(paths) => clipboard.set().file_list(paths),
    }
}

/// The system's count of clipboard changes, where there is one.
struct Changes {
    #[cfg(target_os = "linux")]
    x11: Option<std::sync::Arc<std::sync::atomic::AtomicU64>>,
}

#[cfg(windows)]
impl Changes {
    fn new() -> Self {
        Self {}
    }

    fn count(&self) -> Option<u64> {
        // Zero means the count isn't available to this process.
        let n = unsafe { windows_sys::Win32::System::DataExchange::GetClipboardSequenceNumber() };
        (n != 0).then_some(n as u64)
    }
}

#[cfg(target_os = "macos")]
impl Changes {
    fn new() -> Self {
        Self {}
    }

    fn count(&self) -> Option<u64> {
        let n = objc2_app_kit::NSPasteboard::generalPasteboard().changeCount();
        Some(n as u64)
    }
}

#[cfg(target_os = "linux")]
impl Changes {
    fn new() -> Self {
        Self { x11: x11_watch() }
    }

    fn count(&self) -> Option<u64> {
        let n = self.x11.as_ref()?.load(std::sync::atomic::Ordering::Relaxed);
        // u64::MAX marks a connection that died.
        (n != u64::MAX).then_some(n)
    }
}

#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
impl Changes {
    fn new() -> Self {
        Self {}
    }

    fn count(&self) -> Option<u64> {
        None
    }
}

/// Counts XFixes owner changes of the CLIPBOARD selection on a thread of its own.
#[cfg(target_os = "linux")]
fn x11_watch() -> Option<std::sync::Arc<std::sync::atomic::AtomicU64>> {
    use std::sync::{
        Arc,
        atomic::{AtomicU64, Ordering},
    };
    use x11rb::{
        connection::Connection,
        protocol::{
            xfixes::{ConnectionExt as _, SelectionEventMask},
            xproto::ConnectionExt as _,
        },
    };

    let (conn, screen) = x11rb::connect(None).ok()?;
    let root = conn.setup().roots[screen].root;
    let atom = conn.intern_atom(false, b"CLIPBOARD").ok()?.reply().ok()?.atom;
    conn.xfixes_query_version(5, 0).ok()?.reply().ok()?;
    let mask = SelectionEventMask::SET_SELECTION_OWNER
        | SelectionEventMask::SELECTION_WINDOW_DESTROY
        | SelectionEventMask::SELECTION_CLIENT_CLOSE;
    conn.xfixes_select_selection_input(root, atom, mask).ok()?;
    conn.flush().ok()?;
    let count = Arc::new(AtomicU64::new(0));
    let counter = count.clone();
    thread::Builder::new()
        .name("x11-clipboard-events".into())
        .spawn(move || {
            loop {
                match conn.wait_for_event() {
                    Ok(x11rb::protocol::Event::XfixesSelectionNotify(_)) => {
                        counter.fetch_add(1, Ordering::Relaxed);
                    }
                    Ok(_) => {}
                    Err(_) => {
                        counter.store(u64::MAX, Ordering::Relaxed);
                        return;
                    }
                }
            }
        })
        .ok()?;
    Some(count)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn line_endings_do_not_change_the_fingerprint() {
        let windows = Content::Text(normalize_text("a\r\nb\r\n".into()));
        let unix = Content::Text("a\nb\n".into());
        assert_eq!(windows.fingerprint(), unix.fingerprint());
        assert_ne!(unix.fingerprint(), Content::Text("a\nb".into()).fingerprint());
        assert_ne!(
            Content::Files(vec!["/a".into()]).fingerprint(),
            Content::Text("/a".into()).fingerprint()
        );
    }
}