Skip to content

Linux + iPhone Continuity / iMessage / SMS

tighten

Zack Bartel0669523b3ea7
diff --git a/src/core/include/tether/clipboard.hpp b/src/core/include/tether/clipboard.hpp
index d74bc85..a04da89 100644
--- a/src/core/include/tether/clipboard.hpp
+++ b/src/core/include/tether/clipboard.hpp
@@ -3,6 +3,7 @@
#include <functional>
#include <memory>
#include <string>
+#include <vector>
class CCZwlrDataControlManagerV1;
class CCWlSeat;
@@ -29,6 +30,7 @@ namespace tether {
std::function<void(const std::string&)> cb_;
std::unique_ptr<CCZwlrDataControlOfferV1> current_offer_;
+ std::vector<std::string> current_mimes_;
};
} // namespace tether
diff --git a/src/core/include/tether/net.hpp b/src/core/include/tether/net.hpp
index 6f71966..727e5a1 100644
--- a/src/core/include/tether/net.hpp
+++ b/src/core/include/tether/net.hpp
@@ -19,7 +19,7 @@ namespace tether {
void register_client_fd(int fd);
void register_client_ssl(int fd, SSL* ssl);
void unregister_client_fd(int fd);
- void broadcast_message(const std::string& msg);
+ void broadcast_message(const std::string& msg, int exclude_fd = -1);
class UnixServer {
public:
diff --git a/src/core/src/clipboard.cpp b/src/core/src/clipboard.cpp
index 0cc0485..f5be7a5 100644
--- a/src/core/src/clipboard.cpp
+++ b/src/core/src/clipboard.cpp
@@ -16,28 +16,49 @@ ClipboardManager::ClipboardManager(CCZwlrDataControlManagerV1* manager, CCWlSeat
device_ = std::make_unique<CCZwlrDataControlDeviceV1>(proxy);
device_->setDataOffer([this](CCZwlrDataControlDeviceV1*, wl_proxy* offer_proxy) {
- // We received a new offer
current_offer_ = std::make_unique<CCZwlrDataControlOfferV1>(offer_proxy);
+ current_mimes_.clear();
- current_offer_->setOffer([](CCZwlrDataControlOfferV1* offer, const char* mime_type) {
- // we ignore the reported mime types for now and just blindly try text/plain
+ current_offer_->setOffer([this](CCZwlrDataControlOfferV1* offer, const char* mime_type) {
+ current_mimes_.push_back(mime_type);
});
});
device_->setSelection([this](CCZwlrDataControlDeviceV1*, wl_proxy* offer_proxy) {
if (!offer_proxy) {
+ if (cb_) cb_("");
return;
}
- // The selection has changed. It's time to read it!
+ if (!current_offer_) return;
+
+ // Selection changed, negotiate best mime type
+ std::string best_mime = "text/plain"; // fallback
+ bool found = false;
+
+ static const std::vector<std::string> priorities = {
+ "text/plain;charset=utf-8",
+ "UTF8_STRING",
+ "text/plain"
+ };
+
+ for (const auto& p : priorities) {
+ for (const auto& m : current_mimes_) {
+ if (m == p) {
+ best_mime = m;
+ found = true;
+ break;
+ }
+ }
+ if (found) break;
+ }
+
int pipefs[2];
if (pipe(pipefs) < 0) return;
- // Ask the offer to send the data into our pipe.
- current_offer_->sendReceive("text/plain", pipefs[1]);
- close(pipefs[1]); // Close our write end, wayland compositor has it now.
+ current_offer_->sendReceive(best_mime.c_str(), pipefs[1]);
+ close(pipefs[1]);
- // Read in a detached thread so we don't block the event loop
std::thread([this, fd = pipefs[0]]() {
std::string result;
char buf[1024];
@@ -48,7 +69,7 @@ ClipboardManager::ClipboardManager(CCZwlrDataControlManagerV1* manager, CCWlSeat
}
close(fd);
- if (!result.empty() && cb_) {
+ if (cb_) {
cb_(result);
}
}).detach();
diff --git a/src/core/src/net.cpp b/src/core/src/net.cpp
index d5970f4..8d7cc60 100644
--- a/src/core/src/net.cpp
+++ b/src/core/src/net.cpp
@@ -61,12 +61,13 @@ namespace tether {
return total_written;
}
- void broadcast_message(const std::string& msg) {
+ void broadcast_message(const std::string& msg, int exclude_fd) {
std::string packet = msg;
if (packet.back() != '\n')
packet += '\n';
for (auto const& [fd, session] : active_sessions) {
+ if (fd == exclude_fd) continue;
if (session.ssl) {
robust_ssl_write(session.ssl, packet.c_str(), packet.size());
} else {
@@ -406,7 +407,7 @@ namespace tether {
nlohmann::json bc;
bc["command"] = "clipboard_updated";
bc["content"] = content;
- broadcast_message(bc.dump());
+ broadcast_message(bc.dump(), client_fd);
} else if (j.contains("command") && j["command"] == "clipboard_get") {
if (g_wayland) {
nlohmann::json resp;
diff --git a/src/core/src/wayland.cpp b/src/core/src/wayland.cpp
index 48c2168..a9eee4e 100644
--- a/src/core/src/wayland.cpp
+++ b/src/core/src/wayland.cpp
@@ -54,11 +54,15 @@ bool WaylandContext::init() {
clipboard_ = std::make_unique<ClipboardManager>(data_control_manager_.get(), seat_.get());
clipboard_->set_update_callback([this](const std::string& text) {
+ bool changed = false;
{
std::lock_guard<std::mutex> lock(clip_mutex_);
- cached_clipboard_ = text;
+ if (cached_clipboard_ != text) {
+ cached_clipboard_ = text;
+ changed = true;
+ }
}
- if (clipboard_cb_) {
+ if (changed && clipboard_cb_) {
clipboard_cb_(text);
}
});