# 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+SEP` in UTF-16LE, raw-DEFLATE compressed (`Clipboard.cs:190-201`). The HTML flavour on Windows is **CF_HTML** (the `Version:…StartHTML:…` header block, `DataFormats.Html`); on receive we strip that header to the fragment before writing `NSPasteboard.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` `.png` if present, else `.tiff` re-encoded via `NSBitmapImageRep`. Files: exactly one `fileURL`; a directory is refused with the Windows message (`SocketStuff.cs:1693`). - **Foundation's `.zlib` algorithm is raw DEFLATE** (no zlib header), the same stream .NET `DeflateStream` writes. The Dev confirms this once in the scratchpad against Python `zlib.decompress(d, -15)` and records the result in §10.7; if it is not, `compression_stream` with `COMPRESSION_ZLIB` is used instead. - **Small payloads** (≤ 1 MB after compression, `Clipboard.cs:43`) go in-band: 64-byte `ClipboardText (124)` / `ClipboardImage (125)` packets with 48 payload bytes at offset 16, `Des = ALL`, terminated by `ClipboardDataEnd (76)`, last chunk zero-padded with no length field (`Clipboard.cs:253-306`). They are app-stamped and forwarded (C1). The receiver keeps one `Reassembler` per sender: appends chunks, finishes on `ClipboardDataEnd`, 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 broadcasts `Clipboard (69)` ("I have big data") and remembers `clipboardCopiedTime` (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-byte `DATA` (`Clipboard (69)` from the puller, checked with `MachinePool.resolveID(name) == Src` and connected, `:1009`), then the source writes the 1024-byte UTF-16LE header `"*"` and the bytes in 1 MB chunks, files padded to 32 bytes; the puller reads exactly `len`. If the puller has no outbound client to the source it asks with `ClipboardAsk (78)` and the source **pushes** (`ClipboardPush (79)` as the first `DATA`). Whether the 64-byte `DATA` on the bulk connection is framed with checksum/magic (§1.3) is decided by reading `Clipboard.ShakeHand` and recorded in §10.7. - **Destination**: `~/Desktop/MouseWithoutBorders/` (`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`). Files > 100 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 to `NSPasteboard.general` and the resulting `changeCount` remembered so it is not re-shared. - **Pasteboard watching**: poll `changeCount` every 500 ms on a background queue; ignore changes within 1 s of the previous one (`Clipboard.cs:100-107`); ignore our own writes; only when `ShareClipboard` is on; files only when `TransferFile` is on. - **Ownership**: the bulk listener/connections live in `MWBNet/BulkChannel` on 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 in `mwb/ClipboardBridge.swift`, driven by the Engine's `.packet` events. The Engine's `dispatch` gains one line per clipboard packet type that hands the packet to the bridge (authorised edit of `Engine.swift`, those cases only). `PostAction` values (`ClipboardPostAction.cs`): `Desktop` → file to the Desktop folder; `Mspaint` → out of scope. - **Never act before trust**: bulk connections are accepted only from a `Src` that 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`, `Switcher` state via the Engine. - `control-channel`: `ControlChannel.forward` (in-band chunks), `PeerStatus`. - `app-engine`: `Engine` `.packet` dispatch hook, `Settings.shareClipboard/transferFile/tcpPort`. ### Promised (to extras, polish) ```swift // 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.swift` if needed): listener on port, pull/push connections, preamble, the 64-byte `DATA` exchange, 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; on `MachineSwitched` pull or `ClipboardAsk`; 30 s window; `ShareClipboard`/`TransferFile`); `Engine.swift` dispatch hooks (authorised). - [ ] `docs/protocol.md`: append §10.7 (DEFLATE check, bulk `DATA` framing answer, CF_HTML), cited. - [ ] **Checkpoint CP2** is the lead's after merge. ## Work log ## QA log ## Review