clipboard
Goal
Clipboard sharing as PowerToys does it (docs/protocol.md §6): text in
its three flavours, PNG images and single files, sent in-band over the
control channel when small and over the separate bulk connection on
port 15100 when large, pulled by the machine the user switches to or
pushed on request; received files land in ~/Desktop/MouseWithoutBorders/
exactly as on Windows. ShareClipboard and TransferFile gate it.
This lane ends at CP2.
Assumptions
- Formats (
Helper/FormHelper.cs:180-218, 269-330,Clipboard.cs: 1148-1194): text is"TXT"+plain+SEP+"RTF"+rtf+SEP+"HTM"+html+SEPin UTF-16LE, raw-DEFLATE compressed (Clipboard.cs:190-201). The HTML flavour on Windows is CF_HTML (theVersion:…StartHTML:…header block,DataFormats.Html); on receive we strip that header to the fragment before writingNSPasteboard.PasteboardType.html, and on send we wrap our HTML in a CF_HTML header with correct byte offsets. Images are PNG bytes (FormHelper.cs:281);NSPasteboard.pngif present, else.tiffre-encoded viaNSBitmapImageRep. Files: exactly onefileURL; a directory is refused with the Windows message (SocketStuff.cs:1693). - Foundation's
.zlibalgorithm is raw DEFLATE (no zlib header), the same stream .NETDeflateStreamwrites. The Dev confirms this once in the scratchpad against Pythonzlib.decompress(d, -15)and records the result in §10.7; if it is not,compression_streamwithCOMPRESSION_ZLIBis used instead. - Small payloads (≤ 1 MB after compression,
Clipboard.cs:43) go in-band: 64-byteClipboardText (124)/ClipboardImage (125)packets with 48 payload bytes at offset 16,Des = ALL, terminated byClipboardDataEnd (76), last chunk zero-padded with no length field (Clipboard.cs:253-306). They are app-stamped and forwarded (C1). The receiver keeps oneReassemblerper sender: appends chunks, finishes onClipboardDataEnd, aborts after 100 interleaved foreign packets (:308-373); trailing zeros are part of the DEFLATE stream and the decoder tolerates them. - Big payloads and files (
Clipboard.cs:498-923, 961-1060,SocketStuff.cs:1673-1811): on a local copy that is too big, or any file, the machine broadcastsClipboard (69)("I have big data") and remembersclipboardCopiedTime(30 s window,Clipboard.cs:42). When a peer is switched to (MachineSwitched (77),Receiver.cs: 284-291,Event.cs:219-231) and the source has fresh big data, the peer pulls: it connects to the source's port 15100, both sides exchange the cipher preamble (§2.3) and one 64-byteDATA(Clipboard (69)from the puller, checked withMachinePool.resolveID(name) == Srcand connected,:1009), then the source writes the 1024-byte UTF-16LE header"<len>*<name>"and the bytes in 1 MB chunks, files padded to 32 bytes; the puller reads exactlylen. If the puller has no outbound client to the source it asks withClipboardAsk (78)and the source pushes (ClipboardPush (79)as the firstDATA). Whether the 64-byteDATAon the bulk connection is framed with checksum/magic (§1.3) is decided by readingClipboard.ShakeHandand recorded in §10.7. - Destination:
~/Desktop/MouseWithoutBorders/<original name>(Common.cs:1291-1330), written to a temp file in the same directory and renamed into place; incomplete transfers deleted, an existing destination kept until success (ReceivedDestinationFile.cs). Files100 MB refused with the Windows message (
Clipboard.cs:44). After a file lands the folder is revealed in Finder (Clipboard.cs:840-848). Received text/image is written toNSPasteboard.generaland the resultingchangeCountremembered so it is not re-shared. - Pasteboard watching: poll
changeCountevery 500 ms on a background queue; ignore changes within 1 s of the previous one (Clipboard.cs:100-107); ignore our own writes; only whenShareClipboardis on; files only whenTransferFileis on. - Ownership: the bulk listener/connections live in
MWBNet/BulkChannelon their own queue and report through a small sink; the policy (who has big data, when to pull/push/ask, the 30 s window) lives inmwb/ClipboardBridge.swift, driven by the Engine's.packetevents. The Engine'sdispatchgains one line per clipboard packet type that hands the packet to the bridge (authorised edit ofEngine.swift, those cases only).PostActionvalues (ClipboardPostAction.cs):Desktop→ file to the Desktop folder;Mspaint→ out of scope. - Never act before trust: bulk connections are accepted only from
a
Srcthat resolves to a connected, trusted peer; everything else is closed without reading a payload (invariant 2).
Handshakes
Consumed
wire-core:Packet.payload48,CipherWriter/CipherReader,MagicHash.session-matrix:MachinePool.resolveID,Switcherstate via the Engine.control-channel:ControlChannel.forward(in-band chunks),PeerStatus.app-engine:Engine.packetdispatch hook,Settings.shareClipboard/transferFile/tcpPort.
Promised (to extras, polish)
// MWBCore/ClipboardCodec.swift
public struct ClipboardText: Sendable { plain: String?; rtf: Data?; html: String? }
public enum ClipboardContent: Sendable { text(ClipboardText); image(png: Data); file(URL) }
public enum ClipboardCodec { encodeText(_:) throws -> Data; decodeText(_:) throws -> ClipboardText; cfHTML(wrap:) / cfHTML(unwrap:); chunkPackets(payload:type:) -> [Packet]; }
public final class Reassembler { func feed(_ p: Packet) -> Data? /* complete payload */; var abandoned: Bool }
// MWBMac/Pasteboard.swift
public final class PasteboardWatcher { init(onChange: @escaping @Sendable (ClipboardContent) -> Void); start(); stop(); func write(_:) }
// MWBMac/ReceivedFile.swift
public enum ReceivedFile { static func destinationDirectory() throws -> URL; static func stage(name:) throws -> StagedFile; StagedFile.commit()/discard() }
// MWBNet/BulkChannel.swift
public final class BulkChannel { init(config: ChannelConfig, sink: BulkSink); start(); stop(); func serve(_ content: ClipboardContent, until: TimeInterval); func pull(from address: String, peerID:, peerName:) ; func push(_ content:, to address:) }
public protocol BulkSink: AnyObject, Sendable { received(_ content: ClipboardContent, from peerID: UInt32); isTrusted(peerName:peerID:) -> Bool; failed(reason: String) }
Status / TODO
Sources/MWBCore/ClipboardCodec.swift: tag/SEP text codec, UTF-16LE, DEFLATE (verified raw), CF_HTML wrap/unwrap, 48-byte chunking,Reassembler(Clipboard.cs:43-47, 190-201, 253-373, 1148-1194,FormHelper.cs:180-330).Sources/MWBMac/Pasteboard.swift: watcher and writer per Assumptions (Clipboard.cs:100-107,FormHelper.cs).Sources/MWBMac/ReceivedFile.swift: staging + atomic commit + reveal (ReceivedDestinationFile.cs,Common.cs:1291-1330,Clipboard.cs:669-711, 840-848, 943-959).Sources/MWBNet/BulkChannel.swift(+BulkConnection.swiftif needed): listener on port, pull/push connections, preamble, the 64-byteDATAexchange, 1024-byte header, chunked body, trust gate (Clipboard.cs:425-449, 498-923, 961-1060,SocketStuff.cs: 1673-1811, 1832-1836).Sources/mwb/ClipboardBridge.swift: policy (small → in-band, big/file →Clipboard (69)+ serve; onMachineSwitchedpull orClipboardAsk; 30 s window;ShareClipboard/TransferFile);Engine.swiftdispatch hooks (authorised).docs/protocol.md: append §10.7 (DEFLATE check, bulkDATAframing answer, CF_HTML), cited.- Checkpoint CP2 is the lead's after merge.