Skip to content

Linux + iPhone Continuity / iMessage / SMS

feat: image clipboard transfer

Zack Bartel248b958f52ee
diff --git a/apple/Tether/ViewModels/TetherViewModel.swift b/apple/Tether/ViewModels/TetherViewModel.swift
index c20360a..6b020bd 100644
--- a/apple/Tether/ViewModels/TetherViewModel.swift
+++ b/apple/Tether/ViewModels/TetherViewModel.swift
@@ -36,6 +36,7 @@ struct ClipboardEntry: Identifiable {
let content: String
let timestamp: Date
let source: ClipboardSource
+ var image: Data? = nil // PNG bytes when the entry is an image
enum ClipboardSource: Equatable {
case local(String)
@@ -96,6 +97,10 @@ final class TetherViewModel {
// Name of the connected device, if any.
private(set) var connectedDeviceName: String?
+ // Whether the daemon answered hello with clipboard_image. Older daemons
+ // would save a sent image to Downloads instead of the clipboard.
+ private(set) var daemonSupportsClipboardImages = false
+
// Clipboard history (most recent first).
private(set) var clipboardHistory: [ClipboardEntry] = []
@@ -156,6 +161,7 @@ final class TetherViewModel {
private struct IncomingTransferBuffer {
let filename: String
let expectedSize: Int64
+ var clipboard: String? = nil
var data = Data()
}
@@ -281,7 +287,17 @@ final class TetherViewModel {
// Send the current iOS clipboard content to the daemon.
func sendClipboard() {
#if canImport(UIKit)
- guard let text = UIPasteboard.general.string, !text.isEmpty else {
+ let pasteboard = UIPasteboard.general
+ // Text wins when both are present, matching the daemon.
+ if !pasteboard.hasStrings, pasteboard.hasImages {
+ guard let png = pasteboard.data(forPasteboardType: UTType.png.identifier) ?? pasteboard.image?.pngData() else {
+ errorMessage = "Clipboard is empty."
+ return
+ }
+ sendClipboardImage(png)
+ return
+ }
+ guard let text = pasteboard.string, !text.isEmpty else {
errorMessage = "Clipboard is empty."
return
}
@@ -304,6 +320,27 @@ final class TetherViewModel {
}
}
+ // Send PNG bytes to the daemon's clipboard as a file transfer tagged "set".
+ private func sendClipboardImage(_ png: Data) {
+ guard daemonSupportsClipboardImages else {
+ errorMessage = "Update Tether on your computer to send images."
+ return
+ }
+ // Same cap the daemon enforces.
+ guard png.count <= 32 * 1024 * 1024 else {
+ errorMessage = "Image is too large to send (32 MB max)."
+ return
+ }
+ sendFile(data: png, filename: "clipboard.png", clipboard: "set")
+
+ let size = ByteCountFormatter.string(fromByteCount: Int64(png.count), countStyle: .file)
+ let entry = ClipboardEntry(content: "Image, \(size)", timestamp: Date(), source: .local(certificateManager.localDeviceName), image: png)
+ clipboardHistory.insert(entry, at: 0)
+ if clipboardHistory.count > 50 {
+ clipboardHistory = Array(clipboardHistory.prefix(50))
+ }
+ }
+
// Request the current clipboard from the daemon.
func requestClipboard() {
connection.send(.clipboardGet())
@@ -325,6 +362,11 @@ final class TetherViewModel {
#endif
}
+ // Copy PNG bytes to the iOS pasteboard.
+ func copyImageToLocalClipboard(_ png: Data) {
+ UIPasteboard.general.setData(png, forPasteboardType: UTType.png.identifier)
+ }
+
// MARK: - File Transfer
// Send a file to the daemon using its URL.
@@ -347,8 +389,8 @@ final class TetherViewModel {
}
}
- // Send raw data to the daemon.
- func sendFile(data fileData: Data, filename: String) {
+ // Send raw data to the daemon. A clipboard transfer stays out of the Files tab.
+ func sendFile(data fileData: Data, filename: String, clipboard: String? = nil) {
let transferId = UUID().uuidString
let totalSize = Int64(fileData.count)
@@ -356,14 +398,16 @@ final class TetherViewModel {
guard let self else { return }
do {
- await MainActor.run {
- let transfer = FileTransfer(
- id: transferId,
- filename: filename,
- totalSize: totalSize,
- direction: .outgoing
- )
- self.activeTransfers.append(transfer)
+ if clipboard == nil {
+ await MainActor.run {
+ let transfer = FileTransfer(
+ id: transferId,
+ filename: filename,
+ totalSize: totalSize,
+ direction: .outgoing
+ )
+ self.activeTransfers.append(transfer)
+ }
}
// Send file_start
@@ -371,7 +415,8 @@ final class TetherViewModel {
self.connection.send(.fileStart(
filename: filename,
size: totalSize,
- transferId: transferId
+ transferId: transferId,
+ clipboard: clipboard
))
}
@@ -508,6 +553,7 @@ final class TetherViewModel {
// Already paired — go straight to connected
appState = .connected
connectedDeviceName = certificateManager.knownHosts[serverFP] ?? connectedDeviceName
+ sendHello()
} else {
// Need to pair
pairingIsInbound = isInbound
@@ -680,6 +726,9 @@ final class TetherViewModel {
// The peer's user approved. This, not our own tap, is what makes us paired.
finishPairing()
+ case .hello:
+ daemonSupportsClipboardImages = message.features?.contains("clipboard_image") ?? false
+
case .fileStatus:
if let transferId = message.transferId, message.status == "success" {
if let idx = activeTransfers.firstIndex(where: { $0.id == transferId }) {
@@ -696,8 +745,11 @@ final class TetherViewModel {
incomingTransfers[transferId] = IncomingTransferBuffer(
filename: filename,
- expectedSize: size
+ expectedSize: size,
+ clipboard: message.clipboard
)
+ // Clipboard images land in clipboard history, not the Files tab.
+ if message.clipboard != nil { break }
if let idx = activeTransfers.firstIndex(where: { $0.id == transferId }) {
activeTransfers[idx].filename = filename
@@ -773,11 +825,40 @@ final class TetherViewModel {
showPairingSheet = false
pairingStatus = ""
appState = .connected
+ sendHello()
+ }
+
+ // Announce optional features. Sent only once the peer is pinned, since an
+ // unpinned daemon answers anything else with "unauthorized".
+ private func sendHello() {
+ daemonSupportsClipboardImages = false
+ connection.send(.hello(features: ["clipboard_image"]))
}
private func finalizeIncomingTransfer(transferId: String) {
guard let buffered = incomingTransfers.removeValue(forKey: transferId) else { return }
+ if let kind = buffered.clipboard {
+ guard Int64(buffered.data.count) == buffered.expectedSize, UIImage(data: buffered.data) != nil else {
+ errorMessage = "Received a damaged clipboard image."
+ return
+ }
+ let sourceName = connectedDeviceName ?? "Desktop"
+ let size = ByteCountFormatter.string(fromByteCount: buffered.expectedSize, countStyle: .file)
+ let entry = ClipboardEntry(content: "Image, \(size)", timestamp: Date(), source: .remote(sourceName), image: buffered.data)
+ clipboardHistory.insert(entry, at: 0)
+ if clipboardHistory.count > 50 {
+ clipboardHistory = Array(clipboardHistory.prefix(50))
+ }
+ // "content" answers a manual request, so it always writes, like clipboard_content.
+ if kind == "content" || autoSyncClipboard {
+ Task { @MainActor in
+ copyImageToLocalClipboard(buffered.data)
+ }
+ }
+ return
+ }
+
do {
guard Int64(buffered.data.count) == buffered.expectedSize else {
throw CocoaError(.fileReadCorruptFile)
diff --git a/apple/Tether/Views/ClipboardView.swift b/apple/Tether/Views/ClipboardView.swift
index 31034ee..c31c5c4 100644
--- a/apple/Tether/Views/ClipboardView.swift
+++ b/apple/Tether/Views/ClipboardView.swift
@@ -141,7 +141,11 @@ struct ClipboardView: View {
private func clipboardEntryRow(_ entry: ClipboardEntry) -> some View {
Button {
- viewModel.copyToLocalClipboard(entry.content)
+ if let image = entry.image {
+ viewModel.copyImageToLocalClipboard(image)
+ } else {
+ viewModel.copyToLocalClipboard(entry.content)
+ }
} label: {
HStack(alignment: .top, spacing: 12) {
Image(systemName: entry.source.isRemote ? "desktopcomputer" : "iphone")
@@ -152,10 +156,19 @@ struct ClipboardView: View {
.accessibilityHidden(true)
VStack(alignment: .leading, spacing: 4) {
- Text(entry.content)
- .font(.subheadline)
- .foregroundStyle(.primary)
- .lineLimit(3)
+ if let data = entry.image, let image = UIImage(data: data) {
+ Image(uiImage: image)
+ .resizable()
+ .scaledToFit()
+ .frame(maxHeight: 120)
+ .clipShape(RoundedRectangle(cornerRadius: 8))
+ .accessibilityLabel(entry.content)
+ } else {
+ Text(entry.content)
+ .font(.subheadline)
+ .foregroundStyle(.primary)
+ .lineLimit(3)
+ }
HStack(spacing: 6) {
Text(entry.source.displayName)
diff --git a/apple/TetherFramework/Sources/TetherFramework/TetherMessage.swift b/apple/TetherFramework/Sources/TetherFramework/TetherMessage.swift
index f7f11fe..04545b9 100644
--- a/apple/TetherFramework/Sources/TetherFramework/TetherMessage.swift
+++ b/apple/TetherFramework/Sources/TetherFramework/TetherMessage.swift
@@ -20,6 +20,8 @@ public enum TetherCommand: String, Codable, Sendable {
case fileEnd = "file_end"
case pairRequest = "pair_request"
case newOtp = "new_otp"
+ // Both directions: announces optional protocol features.
+ case hello = "hello"
// Daemon → Client
case clipboardUpdated = "clipboard_updated"
@@ -60,6 +62,11 @@ public struct TetherMessage: Codable, Sendable {
public var transferId: String?
public var chunkIndex: Int?
public var data: String? // Base64 encoded chunk
+ // Set on file_start when the file is a clipboard image: "set", "updated" or "content".
+ public var clipboard: String?
+
+ // hello
+ public var features: [String]?
// Pairing
public var deviceName: String?
@@ -76,7 +83,7 @@ public struct TetherMessage: Codable, Sendable {
case command, content, filename, size
case transferId = "transfer_id"
case chunkIndex = "chunk_index"
- case data
+ case data, clipboard, features
case deviceName = "device_name"
case otp, source
case status, message
@@ -94,7 +101,9 @@ public struct TetherMessage: Codable, Sendable {
otp: String? = nil,
source: String? = nil,
status: String? = nil,
- message: String? = nil
+ message: String? = nil,
+ clipboard: String? = nil,
+ features: [String]? = nil
) {
self.command = command
self.content = content
@@ -108,6 +117,8 @@ public struct TetherMessage: Codable, Sendable {
self.source = source
self.status = status
self.message = message
+ self.clipboard = clipboard
+ self.features = features
}
}
@@ -124,6 +135,11 @@ extension TetherMessage {
TetherMessage(command: TetherCommand.openUrl.rawValue, content: url)
}
+ // Create a `hello` announcing the optional features this client supports.
+ public static func hello(features: [String]) -> TetherMessage {
+ TetherMessage(command: TetherCommand.hello.rawValue, features: features)
+ }
+
// Create a `clipboard_get` request.
public static func clipboardGet() -> TetherMessage {
TetherMessage(command: TetherCommand.clipboardGet.rawValue)
@@ -146,12 +162,13 @@ extension TetherMessage {
}
// Create a `file_start` message.
- public static func fileStart(filename: String, size: Int64, transferId: String) -> TetherMessage {
+ public static func fileStart(filename: String, size: Int64, transferId: String, clipboard: String? = nil) -> TetherMessage {
TetherMessage(
command: TetherCommand.fileStart.rawValue,
filename: filename,
size: size,
- transferId: transferId
+ transferId: transferId,
+ clipboard: clipboard
)
}
diff --git a/apple/TetherFramework/Tests/TetherFrameworkTests/PairingPolicyTests.swift b/apple/TetherFramework/Tests/TetherFrameworkTests/PairingPolicyTests.swift
index 26ebaa6..21a8b6e 100644
--- a/apple/TetherFramework/Tests/TetherFrameworkTests/PairingPolicyTests.swift
+++ b/apple/TetherFramework/Tests/TetherFrameworkTests/PairingPolicyTests.swift
@@ -33,6 +33,7 @@ struct PairingPolicyTests {
#expect(!TetherCommand.allowedWhileUnpaired(.clipboardUpdated, inbound: false))
#expect(!TetherCommand.allowedWhileUnpaired(.clipboardContent, inbound: false))
#expect(!TetherCommand.allowedWhileUnpaired(.fileStart, inbound: false))
+ #expect(!TetherCommand.allowedWhileUnpaired(.hello, inbound: false))
#expect(!TetherCommand.allowedWhileUnpaired(nil, inbound: false))
}
}
diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md
index 93b33c8..d7adf9c 100644
--- a/docs/PROTOCOL.md
+++ b/docs/PROTOCOL.md
@@ -10,6 +10,22 @@ All communication happens via **newline-delimited JSON**. Every individual comma
If a payload arrives that cannot be parsed as JSON, the daemon will gracefully ignore it. After successfully processing a message, the server currently sends an ad-hoc text response `OK\n`, though standard JSON responses may replace this over time.
+### `hello` (both directions, paired only)
+
+**Description**: Feature negotiation. A paired client sends it once connected, and the daemon answers with its own. Each side uses a feature only after the other has announced it. An older daemon answers `OK`, which means no optional features.
+
+**Payload**:
+```json
+{
+ "command": "hello",
+ "features": ["clipboard_image"]
+}
+```
+
+| Feature | Meaning |
+|---|---|
+| `clipboard_image` | [Clipboard images](#clipboard-images-both-directions) |
+
---
## 1. Clipboard Sync
@@ -70,6 +86,18 @@ The clipboard capabilities allow seamless copying and pasting between the Host (
---
+### Clipboard images (both directions)
+
+Needs `clipboard_image` from both sides' [`hello`](#hello-both-directions-paired-only). A PNG travels as a [file transfer](#2-file-transfers) named `clipboard.png`. Its `file_start` carries a `"clipboard"` field:
+
+- `"set"` (Client -> Daemon): treated like `clipboard_set`. The daemon puts the PNG on the Wayland clipboard as `image/png` and relays it to other clients as `"updated"`. It is dropped unless every declared byte arrives and it starts with the PNG signature.
+- `"updated"` (Daemon -> Clients): the Host selection changed to one that offers `image/png` and no text type. Treated like `clipboard_updated`.
+- `"content"` (Daemon -> Client): the reply to `clipboard_get` when the selection is an image. Treated like `clipboard_content`.
+
+Images over 32 MB are not sent or accepted. A client without `clipboard_image` gets no images. Its `clipboard_get` returns the last copied text.
+
+---
+
## 2. File Transfers
Tether supports streaming large binary files seamlessly through JSON using Base64 chunks to adhere to extension native messaging payload restrictions.
diff --git a/src/core/include/tether/clipboard.hpp b/src/core/include/tether/clipboard.hpp
index 302c761..d7e338b 100644
--- a/src/core/include/tether/clipboard.hpp
+++ b/src/core/include/tether/clipboard.hpp
@@ -15,6 +15,28 @@
namespace tether {
+ inline constexpr const char* CLIPBOARD_IMAGE_MIME = "image/png";
+
+ // MIME type to read from a selection offer. Text wins over an image.
+ // Empty when nothing usable is offered.
+ inline std::string pick_clipboard_mime(const std::vector<std::string>& offered) {
+ static const std::vector<std::string> priorities = {"text/plain;charset=utf-8",
+ "text/plain;charset=UTF-8",
+ "UTF8_STRING",
+ "text/plain;charset=utf8",
+ "text/plain",
+ "TEXT",
+ "STRING",
+ CLIPBOARD_IMAGE_MIME};
+ for (const auto& p : priorities) {
+ for (const auto& m : offered) {
+ if (m == p)
+ return m;
+ }
+ }
+ return {};
+ }
+
// Clipboard access needs a data-control protocol. wlroots compositors expose
// zwlr_data_control_v1, KWin exposes ext_data_control_v1. Both have the
// exact same shape, so the implementation is reused.
@@ -22,11 +44,14 @@ namespace tether {
public:
virtual ~ClipboardManager() = default;
- // Callback for native clipboard updates
- virtual void set_update_callback(std::function<void(const std::string&)> cb) = 0;
+ // Callback for native clipboard updates: selection bytes and the MIME type read.
+ // An empty selection reports ("", "").
+ virtual void set_update_callback(std::function<void(const std::string&, const std::string&)> cb) = 0;
// Push text from Tether network to the native clipboard
virtual void copy(const std::string& text) = 0;
+ // Push PNG bytes from Tether network to the native clipboard
+ virtual void copy_image(const std::string& png) = 0;
};
template <typename Manager, typename Device, typename Offer, typename Source>
@@ -66,20 +91,35 @@ namespace tether {
}
}
- void set_update_callback(std::function<void(const std::string&)> cb) override { cb_ = std::move(cb); }
+ void set_update_callback(std::function<void(const std::string&, const std::string&)> cb) override {
+ cb_ = std::move(cb);
+ }
+
+ void copy(const std::string& text) override { offer(text, {"text/plain", "UTF8_STRING"}); }
+
+ void copy_image(const std::string& png) override { offer(png, {CLIPBOARD_IMAGE_MIME}); }
- void copy(const std::string& text) override {
+ private:
+ void offer(const std::string& data, std::initializer_list<const char*> mimes) {
auto source = std::make_unique<Source>(manager_->sendCreateDataSource());
- source->sendOffer("text/plain");
- source->sendOffer("UTF8_STRING");
+ for (const char* mime : mimes)
+ source->sendOffer(mime);
- std::string text_to_send = text;
- source->setSend([text_to_send](Source*, const char*, int32_t fd) {
+ auto data_to_send = std::make_shared<const std::string>(data);
+ source->setSend([data_to_send](Source*, const char*, int32_t fd) {
// detached write thread: touches no manager state
- std::thread([text_to_send, fd]() {
- if (write(fd, text_to_send.c_str(), text_to_send.size()) < 0) {
- debug::log(ERR, "clipboard write error\n");
+ std::thread([data_to_send, fd]() {
+ // a pipe takes large payloads in pieces
+ for (size_t off = 0; off < data_to_send->size();) {
+ ssize_t n = write(fd, data_to_send->data() + off, data_to_send->size() - off);
+ if (n < 0 && errno == EINTR)
+ continue;
+ if (n <= 0) {
+ debug::log(ERR, "clipboard write error\n");
+ break;
+ }
+ off += static_cast<size_t>(n);
}
close(fd);
}).detach();
@@ -95,13 +135,12 @@ namespace tether {
source_ = std::move(source);
}
- private:
void on_selection(wl_proxy* offer_proxy) {
if (!offer_proxy) {
offers_.clear();
offer_mimes_.clear();
if (cb_)
- cb_("");
+ cb_("", "");
return;
}
@@ -112,26 +151,7 @@ namespace tether {
return;
}
- static const std::vector<std::string> priorities = {"text/plain;charset=utf-8",
- "text/plain;charset=UTF-8",
- "UTF8_STRING",
- "text/plain;charset=utf8",
- "text/plain",
- "TEXT",
- "STRING"};
-
- std::string best_mime;
- for (const auto& p : priorities) {
- for (const auto& m : it_mimes->second) {
- if (m == p) {
- best_mime = m;
- break;
- }
- }
- if (!best_mime.empty())
- break;
- }
-
+ const std::string best_mime = pick_clipboard_mime(it_mimes->second);
if (best_mime.empty())
return;
@@ -154,26 +174,29 @@ namespace tether {
offers_[offer_proxy] = std::move(saved_offer);
offer_mimes_[offer_proxy] = std::move(saved_mimes);
- loop_.addFd(pipefs[0], [this](int fd) {
- char buf[4096];
+ const bool is_image = best_mime == CLIPBOARD_IMAGE_MIME;
+ loop_.addFd(pipefs[0], [this, best_mime, is_image](int fd) {
+ char buf[65536];
ssize_t n = read(fd, buf, sizeof(buf));
if (n > 0) {
pending_reads_[fd].append(buf, n);
} else if (n == 0 || (n < 0 && errno != EAGAIN)) {
// Done reading or error
- std::string result = pending_reads_[fd];
+ std::string result = std::move(pending_reads_[fd]);
pending_reads_.erase(fd);
loop_.removeFd(fd);
close(fd);
+ if (n < 0 && is_image)
+ return; // a truncated PNG is useless
- // trim trailing nulls and whitespace
- while (!result.empty() &&
+ // trim trailing nulls and whitespace; image bytes stay intact
+ while (!is_image && !result.empty() &&
(result.back() == '\0' || isspace(static_cast<unsigned char>(result.back())))) {
result.pop_back();
}
if (cb_) {
- cb_(result);
+ cb_(result, best_mime);
}
}
});
@@ -185,7 +208,7 @@ namespace tether {
std::unique_ptr<Device> device_;
std::unique_ptr<Source> source_;
- std::function<void(const std::string&)> cb_;
+ std::function<void(const std::string&, const std::string&)> cb_;
std::map<wl_proxy*, std::unique_ptr<Offer>> offers_;
std::map<wl_proxy*, std::vector<std::string>> offer_mimes_;
std::map<int, std::string> pending_reads_;
diff --git a/src/core/include/tether/net.hpp b/src/core/include/tether/net.hpp
index d7a187b..89a93f9 100644
--- a/src/core/include/tether/net.hpp
+++ b/src/core/include/tether/net.hpp
@@ -10,6 +10,7 @@
#include <string>
#include <string_view>
#include <sys/types.h>
+#include <vector>
namespace tether {
@@ -38,6 +39,33 @@ namespace tether {
void record_received_file(const std::filesystem::path& path, size_t bytes_written);
size_t broadcast_tcp_message(const std::string& msg, int exclude_fd = -1);
+ inline constexpr size_t CLIPBOARD_IMAGE_MAX_BYTES = 32u * 1024 * 1024;
+
+ // A clipboard PNG as file_start/file_chunk/file_end messages tagged with
+ // "clipboard": kind ("updated" or "content"). Empty when the image is too large.
+ std::vector<std::string> clipboard_image_messages(const std::string& png, const std::string& kind);
+
+ // Sends a clipboard PNG as "updated" to every session that announced
+ // clipboard_image in its hello. Older clients would save it as a file.
+ void broadcast_clipboard_image(const std::string& png, int exclude_fd = -1);
+ bool session_accepts_clipboard_images(int fd);
+
+ // Reassembles one clipboard image a peer sends as a file transfer.
+ // A new start replaces any transfer in progress.
+ class ClipboardImageReceiver {
+ public:
+ bool start(const std::string& transfer_id, size_t size);
+ // False when the chunk is not part of this transfer.
+ bool chunk(const std::string& transfer_id, const std::string& b64_data);
+ // The PNG once every byte arrived, else empty.
+ std::string finish(const std::string& transfer_id);
+
+ private:
+ std::string id_;
+ size_t expected_ = 0;
+ std::string data_;
+ };
+
// Bluetooth state, read from the BluezMonitor snapshot.
nlohmann::json build_bt_status();
nlohmann::json build_bt_devices();
@@ -135,6 +163,7 @@ namespace tether {
int server_fd_ = -1;
int bind_port_;
std::map<int, std::string> client_buffers_;
+ std::map<int, ClipboardImageReceiver> clipboard_images_;
std::map<int, SSL*> active_ssl_;
std::map<int, bool> ssl_handshake_complete_;
std::map<int, bool> client_paired_;
diff --git a/src/core/include/tether/wayland.hpp b/src/core/include/tether/wayland.hpp
index ff5b98f..cd1c52f 100644
--- a/src/core/include/tether/wayland.hpp
+++ b/src/core/include/tether/wayland.hpp
@@ -25,12 +25,16 @@ namespace tether {
bool clipboard_available() const { return clipboard_ != nullptr; }
void set_clipboard_callback(std::function<void(const std::string&)> cb);
+ void set_clipboard_image_callback(std::function<void(const std::string&)> cb);
void copy_to_clipboard(const std::string& text);
+ void copy_image_to_clipboard(const std::string& png);
std::string get_clipboard();
+ std::string get_clipboard_image();
private:
std::mutex clip_mutex_;
std::string cached_clipboard_;
+ std::string cached_clipboard_image_;
EpollEventLoop& loop_;
wl_display* raw_display_ = nullptr;
std::unique_ptr<CCWlDisplay> display_;
@@ -42,6 +46,7 @@ namespace tether {
std::unique_ptr<ClipboardManager> clipboard_;
std::function<void(const std::string&)> clipboard_cb_;
+ std::function<void(const std::string&)> clipboard_image_cb_;
};
extern WaylandContext* g_wayland;
diff --git a/src/core/src/net.cpp b/src/core/src/net.cpp
index 22cb26e..ff3a2ac 100644
--- a/src/core/src/net.cpp
+++ b/src/core/src/net.cpp
@@ -15,6 +15,7 @@
#include <sys/wait.h>
#include <unistd.h>
+#include "tether/base64.hpp"
#include "tether/bluetooth/airpods.hpp"
#include "tether/bluetooth/config.hpp"
#include "tether/bluetooth/connection.hpp"
@@ -54,7 +55,8 @@ namespace tether {
struct ClientSession {
int fd;
- SSL* ssl; // nullptr for plain unix clients
+ SSL* ssl; // nullptr for plain unix clients
+ bool clipboard_images = false; // announced in hello
};
// Global list of active connected sessions
@@ -827,6 +829,98 @@ namespace tether {
return recipients;
}
+ std::vector<std::string> clipboard_image_messages(const std::string& png, const std::string& kind) {
+ constexpr size_t CHUNK_BYTES = 512 * 1024;
+ if (png.size() > CLIPBOARD_IMAGE_MAX_BYTES) {
+ debug::log(WARN, "clipboard image of {} bytes exceeds the transfer cap; not sent", png.size());
+ return {};
+ }
+ if (png.empty())
+ return {};
+
+ static std::atomic<unsigned> seq{0};
+ const std::string id = "clip_" + std::to_string(time(nullptr)) + "_" + std::to_string(seq++);
+
+ nlohmann::json start{{"command", "file_start"}, {"filename", "clipboard.png"}, {"transfer_id", id}};
+ start["size"] = png.size();
+ start["clipboard"] = kind;
+ std::vector<std::string> out{start.dump()};
+ for (size_t off = 0, idx = 0; off < png.size(); off += CHUNK_BYTES, ++idx) {
+ const size_t len = std::min(CHUNK_BYTES, png.size() - off);
+ nlohmann::json chunk{{"command", "file_chunk"}, {"transfer_id", id}, {"chunk_index", idx}};
+ chunk["data"] = base64_encode(reinterpret_cast<const unsigned char*>(png.data() + off), len);
+ out.push_back(chunk.dump());
+ }
+ out.push_back(nlohmann::json{{"command", "file_end"}, {"transfer_id", id}}.dump());
+ return out;
+ }
+
+ void broadcast_clipboard_image(const std::string& png, int exclude_fd) {
+ const auto msgs = clipboard_image_messages(png, "updated");
+ std::lock_guard<std::mutex> lock(g_sessions_mutex);
+ for (auto const& [fd, session] : active_sessions) {
+ if (fd == exclude_fd || !session.ssl || !session.clipboard_images)
+ continue;
+ for (const auto& m : msgs) {
+ const std::string packet = m + "\n";
+ robust_ssl_write(session.ssl, packet.c_str(), packet.size());
+ }
+ }
+ }
+
+ bool session_accepts_clipboard_images(int fd) {
+ std::lock_guard<std::mutex> lock(g_sessions_mutex);
+ auto it = active_sessions.find(fd);
+ return it != active_sessions.end() && it->second.clipboard_images;
+ }
+
+ static void set_session_clipboard_images(int fd) {
+ std::lock_guard<std::mutex> lock(g_sessions_mutex);
+ if (auto it = active_sessions.find(fd); it != active_sessions.end())
+ it->second.clipboard_images = true;
+ }
+
+ bool ClipboardImageReceiver::start(const std::string& transfer_id, size_t size) {
+ id_.clear();
+ data_.clear();
+ if (transfer_id.empty() || size == 0 || size > CLIPBOARD_IMAGE_MAX_BYTES) {
+ debug::log(WARN, "clipboard image of {} bytes rejected", size);
+ return false;
+ }
+ id_ = transfer_id;
+ expected_ = size;
+ data_.reserve(size);
+ return true;
+ }
+
+ bool ClipboardImageReceiver::chunk(const std::string& transfer_id, const std::string& b64_data) {
+ if (id_.empty() || transfer_id != id_)
+ return false;
+ auto bytes = base64_decode(b64_data);
+ if (data_.size() + bytes.size() > expected_) {
+ debug::log(WARN, "clipboard image overran its declared size; dropped");
+ id_.clear();
+ data_.clear();
+ return true;
+ }
+ data_.append(bytes.begin(), bytes.end());
+ return true;
+ }
+
+ std::string ClipboardImageReceiver::finish(const std::string& transfer_id) {
+ if (id_.empty() || transfer_id != id_)
+ return {};
+ id_.clear();
+ std::string png = std::move(data_);
+ data_.clear();
+ static constexpr std::string_view PNG_SIGNATURE{"\x89PNG\r\n\x1a\n", 8};
+ if (png.size() != expected_ || !png.starts_with(PNG_SIGNATURE)) {
+ debug::log(WARN, "clipboard image incomplete or not a PNG; dropped");
+ return {};
+ }
+ return png;
+ }
+
std::string get_runtime_dir() {
std::filesystem::path base;
if (const char* xdg_runtime = std::getenv("XDG_RUNTIME_DIR")) {
@@ -1366,10 +1460,14 @@ namespace tether {
continue;
}
} else if (j.contains("command") && j["command"] == "clipboard_send") {
+ const std::string image = g_wayland ? g_wayland->get_clipboard_image() : std::string{};
+ if (!image.empty())
+ broadcast_clipboard_image(image);
nlohmann::json resp;
resp["command"] = "clipboard_content";
resp["content"] = g_wayland ? g_wayland->get_clipboard() : std::string{};
- if (!resp["content"].get_ref<const std::string&>().empty()) {
+ // with an image selected the cached text is stale don't push it
+ if (image.empty() && !resp["content"].get_ref<const std::string&>().empty()) {
nlohmann::json bc;
bc["command"] = "clipboard_updated";
bc["content"] = resp["content"];
@@ -1616,6 +1714,7 @@ namespace tether {
}
client_buffers_.erase(fd);
+ clipboard_images_.erase(fd);
ssl_handshake_complete_.erase(fd);
client_paired_.erase(fd);
client_info_.erase(fd);
@@ -2034,11 +2133,32 @@ namespace tether {
bc["command"] = "clipboard_updated";
bc["content"] = content;
broadcast_message(bc.dump(), client_fd);
+ } else if (j.contains("command") && j["command"] == "hello") {
+ // feature negotiation. old daemons answer "OK"
+ const auto features = j.value("features", nlohmann::json::array());
+ if (features.is_array() &&
+ std::find(features.begin(), features.end(), "clipboard_image") != features.end())
+ set_session_clipboard_images(client_fd);
+ nlohmann::json resp{{"command", "hello"}};
+ resp["features"] = nlohmann::json::array({"clipboard_image"});
+ std::string payload = resp.dump() + "\n";
+ robust_ssl_write(ssl, payload.c_str(), payload.size());
+ continue;
} else if (j.contains("command") && j["command"] == "open_url" && j.contains("content") &&
j["content"].is_string()) {
open_web_url(j["content"].get<std::string>());
} else if (j.contains("command") && j["command"] == "clipboard_get") {
if (g_wayland) {
+ std::vector<std::string> image;
+ if (session_accepts_clipboard_images(client_fd))
+ image = clipboard_image_messages(g_wayland->get_clipboard_image(), "content");
+ if (!image.empty()) {
+ for (auto& m : image) {
+ m += "\n";
+ robust_ssl_write(ssl, m.c_str(), m.size());
+ }
+ continue;
+ }
nlohmann::json resp;
resp["command"] = "clipboard_content";
resp["content"] = g_wayland->get_clipboard();
@@ -2053,12 +2173,25 @@ namespace tether {
robust_ssl_write(ssl, payload.c_str(), payload.size());
continue;
} else if (j.contains("command") && j["command"] == "file_start") {
- if (g_file_manager)
+ // A clipboard image goes to the clipboard, not Downloads.
+ if (j.contains("clipboard"))
+ clipboard_images_[client_fd].start(j["transfer_id"], j["size"]);
+ else if (g_file_manager)
g_file_manager->handle_start(j["transfer_id"], j["filename"], j["size"]);
} else if (j.contains("command") && j["command"] == "file_chunk") {
- if (g_file_manager)
+ if (!clipboard_images_[client_fd].chunk(j["transfer_id"], j["data"]) && g_file_manager)
g_file_manager->handle_chunk(j["transfer_id"], j["chunk_index"], j["data"]);
} else if (j.contains("command") && j["command"] == "file_end") {
+ if (std::string png = clipboard_images_[client_fd].finish(j["transfer_id"]); !png.empty()) {
+ if (g_wayland)
+ g_wayland->copy_image_to_clipboard(png);
+ broadcast_clipboard_image(png, client_fd);
+ nlohmann::json resp{{"command", "file_status"}, {"status", "success"}};
+ resp["transfer_id"] = j["transfer_id"];
+ std::string payload = resp.dump() + "\n";
+ robust_ssl_write(ssl, payload.c_str(), payload.size());
+ continue;
+ }
if (g_file_manager && g_file_manager->handle_end(j["transfer_id"])) {
nlohmann::json resp;
resp["command"] = "file_status";
diff --git a/src/core/src/wayland.cpp b/src/core/src/wayland.cpp
index 6ee3159..bd0ae70 100644
--- a/src/core/src/wayland.cpp
+++ b/src/core/src/wayland.cpp
@@ -24,10 +24,7 @@ namespace tether {
seat_.reset();
registry_.reset();
- // High-level wrapper for the display itself. We must NOT let it call
- // wl_proxy_destroy on the raw_display_, as wl_display_disconnect
- // handles that. We simply release the unique_ptr here to prevent its
- // destructor from running.
+ // High-level wrapper for the display itself.
if (display_) {
display_.release();
}
@@ -94,27 +91,32 @@ namespace tether {
return false;
}
- clipboard_->set_update_callback([this](const std::string& text) {
+ clipboard_->set_update_callback([this](const std::string& data, const std::string& mime) {
+ const bool is_image = mime == CLIPBOARD_IMAGE_MIME;
bool changed = false;
{
std::lock_guard<std::mutex> lock(clip_mutex_);
- if (cached_clipboard_ != text) {
- cached_clipboard_ = text;
- changed = true;
+ if (is_image) {
+ changed = cached_clipboard_image_ != data;
+ cached_clipboard_image_ = data;
+ } else {
+ changed = cached_clipboard_ != data || !cached_clipboard_image_.empty();
+ cached_clipboard_ = data;
+ cached_clipboard_image_.clear();
}
}
- if (changed && clipboard_cb_) {
- clipboard_cb_(text);
+ if (!changed)
+ return;
+ if (is_image && clipboard_image_cb_) {
+ clipboard_image_cb_(data);
+ } else if (!is_image && clipboard_cb_) {
+ clipboard_cb_(data);
}
});
// Attach to event loop!
int fd = wl_display_get_fd(raw_display_);
loop_.addFd(fd, [this](int wfd) {
- // Compositor gone: the wl socket peer closes and epoll is level-triggered,
- // so this callback fires nonstop. wl_display_dispatch keeps returning >=0
- // (never surfaces the hangup), so the old dispatch<0 guard never triggers
- // and we peg a core. POLLHUP/POLLERR is the kernel's ground-truth signal.
struct pollfd pfd{wfd, 0, 0};
if (poll(&pfd, 1, 0) == 1 && (pfd.revents & (POLLHUP | POLLERR))) {
debug::log(ERR, "WaylandContext: compositor hung up; exiting for on-demand restart.");
@@ -122,8 +124,7 @@ namespace tether {
return;
}
if (wl_display_dispatch(raw_display_) < 0) {
- // Display connection is dead (compositor stopped).
- // We choose to exit here rather than attempt to reconnect.
+ // Display connection is dead
debug::log(ERR, "WaylandContext: Display connection lost; shutting down for on-demand restart.");
loop_.stop();
return;
@@ -142,6 +143,10 @@ namespace tether {
clipboard_cb_ = std::move(cb);
}
+ void WaylandContext::set_clipboard_image_callback(std::function<void(const std::string&)> cb) {
+ clipboard_image_cb_ = std::move(cb);
+ }
+
void WaylandContext::copy_to_clipboard(const std::string& text) {
std::string trimmed = text;
while (!trimmed.empty() && (trimmed.back() == '\0' || isspace((unsigned char)trimmed.back()))) {
@@ -151,6 +156,7 @@ namespace tether {
{
std::lock_guard<std::mutex> lock(clip_mutex_);
cached_clipboard_ = trimmed;
+ cached_clipboard_image_.clear();
}
if (clipboard_) {
clipboard_->copy(text);
@@ -158,9 +164,26 @@ namespace tether {
}
}
+ void WaylandContext::copy_image_to_clipboard(const std::string& png) {
+ {
+ // cached first, so the compositor echoing our own selection is not rebroadcast
+ std::lock_guard<std::mutex> lock(clip_mutex_);
+ cached_clipboard_image_ = png;
+ }
+ if (clipboard_) {
+ clipboard_->copy_image(png);
+ wl_display_flush(raw_display_);
+ }
+ }
+
std::string WaylandContext::get_clipboard() {
std::lock_guard<std::mutex> lock(clip_mutex_);
return cached_clipboard_;
}
+ std::string WaylandContext::get_clipboard_image() {
+ std::lock_guard<std::mutex> lock(clip_mutex_);
+ return cached_clipboard_image_;
+ }
+
} // namespace tether
diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp
index b089861..3954455 100644
--- a/src/daemon/main.cpp
+++ b/src/daemon/main.cpp
@@ -162,6 +162,7 @@ int main(int argc, char** argv) {
// app can still mislabel binary as text/plain. Don't abort the daemon.
tether::broadcast_message(j.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace));
});
+ wayland_srv.set_clipboard_image_callback([](const std::string& png) { tether::broadcast_clipboard_image(png); });
}
tether::FileReceiveManager file_mgr;
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 1e16ecd..7922c59 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,5 +1,6 @@
add_executable(tether_test
test_base64.cpp
+ test_clipboard.cpp
test_airpods.cpp
test_audio.cpp
test_bluetooth_objects.cpp
diff --git a/test/test_clipboard.cpp b/test/test_clipboard.cpp
new file mode 100644
index 0000000..94cc5cb
--- /dev/null
+++ b/test/test_clipboard.cpp
@@ -0,0 +1,79 @@
+#include <gtest/gtest.h>
+#include <nlohmann/json.hpp>
+#include <tether/base64.hpp>
+#include <tether/clipboard.hpp>
+#include <tether/net.hpp>
+
+TEST(ClipboardMimeTest, TextWinsOverImage) {
+ EXPECT_EQ(tether::pick_clipboard_mime({"image/png", "text/plain"}), "text/plain");
+ EXPECT_EQ(tether::pick_clipboard_mime({"text/html", "image/png"}), "image/png");
+ EXPECT_EQ(tether::pick_clipboard_mime({"UTF8_STRING"}), "UTF8_STRING");
+ EXPECT_EQ(tether::pick_clipboard_mime({"text/html", "image/jpeg"}), "");
+}
+
+TEST(ClipboardImageTest, ChunksReassembleToTheOriginal) {
+ std::string png(1300 * 1024, '\0');
+ for (size_t i = 0; i < png.size(); ++i)
+ png[i] = static_cast<char>(i * 31);
+
+ auto msgs = tether::clipboard_image_messages(png, "content");
+ ASSERT_EQ(msgs.size(), 5u); // start, 3 chunks, end
+
+ auto start = nlohmann::json::parse(msgs.front());
+ EXPECT_EQ(start["command"], "file_start");
+ EXPECT_EQ(start["clipboard"], "content");
+ EXPECT_EQ(start["size"], png.size());
+ EXPECT_EQ(nlohmann::json::parse(msgs.back())["command"], "file_end");
+
+ std::string joined;
+ for (size_t i = 1; i + 1 < msgs.size(); ++i) {
+ auto chunk = nlohmann::json::parse(msgs[i]);
+ EXPECT_EQ(chunk["chunk_index"], i - 1);
+ auto bytes = tether::base64_decode(chunk["data"].get<std::string>());
+ joined.append(bytes.begin(), bytes.end());
+ }
+ EXPECT_EQ(joined, png);
+}
+
+TEST(ClipboardImageTest, EmptyOrOversizedSendsNothing) {
+ EXPECT_TRUE(tether::clipboard_image_messages("", "updated").empty());
+ EXPECT_TRUE(tether::clipboard_image_messages(std::string(33u * 1024 * 1024, 'x'), "updated").empty());
+}
+
+TEST(ClipboardImageTest, ReceiverRoundTripsSentImage) {
+ std::string png = std::string("\x89PNG\r\n\x1a\n", 8) + std::string(700 * 1024, 'p');
+ tether::ClipboardImageReceiver rx;
+ std::string id;
+ for (const auto& m : tether::clipboard_image_messages(png, "updated")) {
+ auto j = nlohmann::json::parse(m);
+ if (j["command"] == "file_start") {
+ id = j["transfer_id"];
+ ASSERT_TRUE(rx.start(id, j["size"]));
+ } else if (j["command"] == "file_chunk") {
+ ASSERT_TRUE(rx.chunk(id, j["data"]));
+ }
+ }
+ EXPECT_EQ(rx.finish(id), png);
+ EXPECT_EQ(rx.finish(id), ""); // consumed
+}
+
+TEST(ClipboardImageTest, ReceiverDropsBadTransfers) {
+ const std::string png("\x89PNG\r\n\x1a\nxx", 10);
+ const std::string b64 = tether::base64_encode(reinterpret_cast<const unsigned char*>(png.data()), png.size());
+ tether::ClipboardImageReceiver rx;
+
+ EXPECT_FALSE(rx.start("big", 33u * 1024 * 1024));
+ EXPECT_FALSE(rx.chunk("big", b64)); // not ours, falls through to file transfer
+
+ ASSERT_TRUE(rx.start("short", png.size() + 1));
+ ASSERT_TRUE(rx.chunk("short", b64));
+ EXPECT_EQ(rx.finish("short"), "");
+
+ ASSERT_TRUE(rx.start("notpng", 3));
+ ASSERT_TRUE(rx.chunk("notpng", tether::base64_encode(reinterpret_cast<const unsigned char*>("abc"), 3)));
+ EXPECT_EQ(rx.finish("notpng"), "");
+
+ ASSERT_TRUE(rx.start("over", 4));
+ EXPECT_TRUE(rx.chunk("over", b64));
+ EXPECT_EQ(rx.finish("over"), "");
+}