Linux + iPhone Continuity / iMessage / SMS
feat(notifications): inline replies to message notifications
Aashish Thapa95079eb0c977
diff --git a/po/tether.pot b/po/tether.potindex 2592323..2e50f3e 100644--- a/po/tether.pot+++ b/po/tether.pot@@ -6,9 +6,9 @@#, fuzzymsgid ""msgstr ""-"Project-Id-Version: tether 0.2.38\n"+"Project-Id-Version: tether 0.2.40\n""Report-Msgid-Bugs-To: [email protected]\n"-"POT-Creation-Date: 2026-10-18 15:38-0700\n"+"POT-Creation-Date: 2026-10-21 23:38-0500\n""PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n""Last-Translator: FULL NAME <EMAIL@ADDRESS>\n""Language-Team: LANGUAGE <[email protected]>\n"@@ -1647,6 +1647,10 @@ msgstr ""msgid "Reject"msgstr ""+#: src/daemon/main.cpp+msgid "Reply not sent"+msgstr ""+#: src/daemon/main.cppmsgid "This PC can't be discovered"msgstr ""@@ -1673,6 +1677,10 @@ msgstr ""msgid "Reply"msgstr ""+#: src/daemon/notification.cpp+msgid "Reply…"+msgstr ""+#: src/daemon/notification.cppmsgid "Copy Code"msgstr ""@@ -1971,6 +1979,10 @@ msgstr ""msgid "({0} of {1})"msgstr ""+#: src/gtk/devices_view.cpp src/tui/devices.cpp+msgid "Daemon unavailable."+msgstr ""+#: src/gtk/devices_view.cppmsgid "Send to…"msgstr ""@@ -1979,10 +1991,6 @@ msgstr ""msgid "_Cancel"msgstr ""-#: src/gtk/devices_view.cpp src/tui/devices.cpp-msgid "Daemon unavailable."-msgstr ""-#: src/gtk/devices_view.cpp src/tui/devices.cppmsgid "Send File"msgstr ""diff --git a/src/core/include/tether/net.hpp b/src/core/include/tether/net.hppindex 5966de9..501b2a5 100644--- a/src/core/include/tether/net.hpp+++ b/src/core/include/tether/net.hpp@@ -32,6 +32,14 @@ namespace tether {void unregister_local_subscriber(int fd);void broadcast_local_event(const std::string& msg, int exclude_fd = -1);+ // Sends an iPhone message over MAP on a worker thread and broadcasts the+ // result (and the sent message, on success) to local subscribers.+ // on_done, if set, runs on that worker thread afterwards.+ void send_bt_message_async(const std::string& thread,+ const std::string& body,+ const std::string& operation_id = {},+ std::function<void(bool sent, const std::string& error)> on_done = {});+// The mDNS peers the daemon currently knows of.void set_discovered_devices(const nlohmann::json& devices);diff --git a/src/core/src/net.cpp b/src/core/src/net.cppindex 8758d15..7fda538 100644--- a/src/core/src/net.cpp+++ b/src/core/src/net.cpp@@ -614,6 +614,34 @@ namespace tether {event["operation_id"] = operation_id;}+ void send_bt_message_async(const std::string& thread,+ const std::string& body,+ const std::string& operation_id,+ std::function<void(bool sent, const std::string& error)> on_done) {+ // PushMessage is a blocking OBEX transfer, so it cannot+ // run on the loop that has to keep serving the UI.+ std::thread([thread, body, operation_id, on_done = std::move(on_done)]() {+ std::string err;+ bluetooth::Message sent;+ nlohmann::json event;+ event["command"] = "bt_send_result";+ event["thread"] = thread;+ set_operation_id(event, operation_id);+ const bool success = bluetooth::send_message(thread, body, sent, err);+ event["success"] = success;+ if (success) {+ nlohmann::json message = bluetooth::to_json(sent);+ message["command"] = "bt_message";+ broadcast_local_event(message.dump());+ } else {+ event["message"] = err;+ }+ broadcast_local_event(event.dump());+ if (on_done)+ on_done(success, err);+ }).detach();+ }+// Runs on the BlueZ monitor's GLib thread, same as the local dialog it replaces.static bool ask_client_to_confirm(const std::string& code, const std::string& operation_id) {{@@ -1365,28 +1393,9 @@ namespace tether {bluetooth::mark_messages_read_async(std::move(handles), j.value("read", true));} else if (j.contains("command") && j["command"] == "bt_send_message" && j.contains("thread") &&j.contains("body")) {- std::string thread = j["thread"];- std::string body = j["body"];- const std::string operation_id = j.value("operation_id", std::string{});- // PushMessage is a blocking OBEX transfer, so it cannot- // run on the loop that has to keep serving the UI.- std::thread([thread, body, operation_id]() {- std::string err;- bluetooth::Message sent;- nlohmann::json event;- event["command"] = "bt_send_result";- event["thread"] = thread;- set_operation_id(event, operation_id);- event["success"] = bluetooth::send_message(thread, body, sent, err);- if (event["success"]) {- nlohmann::json message = bluetooth::to_json(sent);- message["command"] = "bt_message";- broadcast_local_event(message.dump());- } else {- event["message"] = err;- }- broadcast_local_event(event.dump());- }).detach();+ send_bt_message_async(j["thread"].get<std::string>(),+ j["body"].get<std::string>(),+ j.value("operation_id", std::string{}));} else if (j.contains("command") && j["command"] == "bt_solicit") {std::thread([]() {nlohmann::json event;diff --git a/src/daemon/main.cpp b/src/daemon/main.cppindex 70f93fb..9eff20b 100644--- a/src/daemon/main.cpp+++ b/src/daemon/main.cpp@@ -208,6 +208,13 @@ int main(int argc, char** argv) {tether::bluetooth::mark_messages_read_async({handle}, true);});+ notifier.set_reply_handler([¬ifier](const std::string& thread, const std::string& body) {+ tether::send_bt_message_async(thread, body, {}, [¬ifier](bool sent, const std::string& error) {+ if (!sent)+ notifier.notify({_("Messages"), _("Reply not sent"), error, {"dialog-error"}});+ });+ });+if (notifier_ready) {int mdns_warn_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);if (mdns_warn_fd >= 0) {diff --git a/src/daemon/notification.cpp b/src/daemon/notification.cppindex 0b66c78..00a9d1d 100644--- a/src/daemon/notification.cpp+++ b/src/daemon/notification.cpp@@ -14,6 +14,7 @@#include <tether/log.hpp>#include <tether/net.hpp>#include <thread>+#include <unordered_map>#include <unordered_set>namespace tether {@@ -106,6 +107,46 @@ namespace tether {// Set once during startup, before any notification exists.std::function<void(const std::string&, const std::string&)> g_copy_handler;+ std::function<void(const std::string&, const std::string&)> g_reply_handler;++ // Server notification id -> conversation, for notifications showing an+ // inline reply field. Touched only on the notifier's thread.+ std::unordered_map<guint32, std::string> g_reply_threads;++ guint32 notification_id(NotifyNotification* notification) {+ gint id = 0;+ g_object_get(notification, "id", &id, nullptr);+ return static_cast<guint32>(id);+ }++ // Asked per notification rather than cached: the server can be swapped+ // or restarted while tetherd runs.+ bool server_supports_inline_reply() {+ bool supported = false;+ GList* caps = notify_get_server_caps();+ for (GList* cap = caps; cap; cap = cap->next) {+ if (g_strcmp0(static_cast<const char*>(cap->data), "inline-reply") == 0)+ supported = true;+ }+ g_list_free_full(caps, g_free);+ return supported;+ }++ // The servers emit this just before closing the notification.+ void on_notification_replied(+ GDBusConnection*, const char*, const char*, const char*, const char*, GVariant* parameters, gpointer) {+ guint32 id = 0;+ const char* text = nullptr;+ g_variant_get(parameters, "(u&s)", &id, &text);++ const auto it = g_reply_threads.find(id);+ if (it == g_reply_threads.end())+ return;+ const std::string thread = it->second;+ g_reply_threads.erase(it);+ if (g_reply_handler && text && *text)+ g_reply_handler(thread, text);+ }void free_request(gpointer data) { delete static_cast<NotificationRequest*>(data); }@@ -232,7 +273,10 @@ namespace tether {launch_uri(action->payload);}- void on_notification_closed(NotifyNotification* notification, gpointer) { g_object_unref(notification); }+ void on_notification_closed(NotifyNotification* notification, gpointer) {+ g_reply_threads.erase(notification_id(notification));+ g_object_unref(notification);+ }gboolean show_notification_on_main(gpointer user_data) {std::unique_ptr<NotificationRequest> request(static_cast<NotificationRequest*>(user_data));@@ -304,6 +348,8 @@ namespace tether {if (has_actions)g_signal_connect(notification, "closed", G_CALLBACK(on_notification_closed), nullptr);+ const bool inline_reply = !spec->reply_thread.empty() && g_reply_handler && server_supports_inline_reply();+if (!spec->reply_thread.empty()) {notify_notification_add_action(notification,"default",@@ -311,12 +357,26 @@ namespace tether {on_reply_action,new NotificationActionData{spec->reply_thread},free_action_data);- notify_notification_add_action(notification,- "reply",- _("Reply"),- on_reply_action,- new NotificationActionData{spec->reply_thread},- free_action_data);+ if (inline_reply) {+ // The server answers with NotificationReplied rather than+ // ActionInvoked, so this callback only runs on servers that+ // treat "inline-reply" as a plain button.+ notify_notification_add_action(notification,+ "inline-reply",+ _("Reply"),+ on_reply_action,+ new NotificationActionData{spec->reply_thread},+ free_action_data);+ notify_notification_set_hint(+ notification, "x-kde-reply-placeholder-text", g_variant_new_string(_("Reply…")));+ } else {+ notify_notification_add_action(notification,+ "reply",+ _("Reply"),+ on_reply_action,+ new NotificationActionData{spec->reply_thread},+ free_action_data);+ }}if (!spec->otp_code.empty()) {@@ -346,6 +406,8 @@ namespace tether {g_object_unref(notification);return G_SOURCE_REMOVE;}+ if (inline_reply)+ g_reply_threads[notification_id(notification)] = spec->reply_thread;if (!has_actions)g_object_unref(notification);return G_SOURCE_REMOVE;@@ -358,6 +420,8 @@ namespace tether {GMainLoop* loop = nullptr;std::thread thread;bool initialized = false;+ GDBusConnection* bus = nullptr;+ guint reply_subscription = 0;};DesktopNotifier::DesktopNotifier() : impl_(std::make_unique<Impl>()) {}@@ -380,6 +444,10 @@ namespace tether {impl_->thread.join();}+ if (impl_->bus) {+ g_dbus_connection_signal_unsubscribe(impl_->bus, impl_->reply_subscription);+ g_object_unref(impl_->bus);+ }g_main_loop_unref(impl_->loop);g_main_context_unref(impl_->context);notify_uninit();@@ -397,13 +465,32 @@ namespace tether {impl_->context = g_main_context_new();impl_->loop = g_main_loop_new(impl_->context, FALSE);- impl_->thread = std::thread([ctx = impl_->context, loop = impl_->loop]() {- g_main_context_push_thread_default(ctx);+ impl_->thread = std::thread([impl = impl_.get()]() {+ g_main_context_push_thread_default(impl->context);// Walking the icon themes takes long enough to be worth doing before// the first notification rather than during it.installed_icons();- g_main_loop_run(loop);- g_main_context_pop_thread_default(ctx);+ // Subscribed from this thread so replies are dispatched on the same+ // context that owns g_reply_threads.+ GError* error = nullptr;+ impl->bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, &error);+ if (impl->bus) {+ impl->reply_subscription = g_dbus_connection_signal_subscribe(impl->bus,+ "org.freedesktop.Notifications",+ "org.freedesktop.Notifications",+ "NotificationReplied",+ "/org/freedesktop/Notifications",+ nullptr,+ G_DBUS_SIGNAL_FLAGS_NONE,+ on_notification_replied,+ nullptr,+ nullptr);+ } else {+ debug::log(ERR, "No session bus for notification replies: {}", error ? error->message : "unknown");+ g_clear_error(&error);+ }+ g_main_loop_run(impl->loop);+ g_main_context_pop_thread_default(impl->context);});impl_->initialized = true;return true;@@ -414,6 +501,11 @@ namespace tether {g_copy_handler = std::move(handler);}+ void DesktopNotifier::set_reply_handler(+ std::function<void(const std::string& thread, const std::string& body)> handler) {+ g_reply_handler = std::move(handler);+ }+void DesktopNotifier::notify(const NotificationSpec& spec) {if (!impl_ || !impl_->initialized || !desktop_popups_enabled()) {return;diff --git a/src/daemon/notification.hpp b/src/daemon/notification.hppindex 4ddc8b3..d4779fa 100644--- a/src/daemon/notification.hpp+++ b/src/daemon/notification.hpp@@ -42,6 +42,12 @@ namespace tether {// responsible for any thread hop it needs.void set_copy_handler(std::function<void(const std::string& code, const std::string& handle)> handler);+ // Where text typed into a message notification's inline reply field+ // goes, along with the spec's reply_thread. Only servers advertising+ // the "inline-reply" capability show the field; others keep the plain+ // Reply action. Called on the notifier's own thread.+ void set_reply_handler(std::function<void(const std::string& thread, const std::string& body)> handler);+void notify_file_arrived(const std::filesystem::path& path);void notify(const NotificationSpec& spec);