Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix: gtk UI bugs

Zack Bartel184db2b44c10
diff --git a/src/gtk/daemon_client.cpp b/src/gtk/daemon_client.cpp
index a51e5a8..8a83e81 100644
--- a/src/gtk/daemon_client.cpp
+++ b/src/gtk/daemon_client.cpp
@@ -1,6 +1,7 @@
#include "daemon_client.hpp"
#include "ui_util.hpp"
+#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
@@ -23,6 +24,7 @@ namespace tether::ui {
guint g_event_watch_id = 0;
guint g_event_retry_id = 0;
DaemonEventFn g_on_event;
+ DaemonDisconnectFn g_on_disconnect;
gboolean start_event_subscription(gpointer);
@@ -46,11 +48,17 @@ namespace tether::ui {
g_event_retry_id = g_timeout_add_seconds(2, start_event_subscription, nullptr);
}
+ void handle_disconnect() {
+ stop_event_subscription();
+ schedule_event_retry();
+ set_status_main("Daemon Offline");
+ if (g_on_disconnect)
+ g_on_disconnect();
+ }
+
gboolean on_event_channel(GIOChannel* source, GIOCondition condition, gpointer) {
if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
- stop_event_subscription();
- schedule_event_retry();
- set_status_main("Daemon Offline");
+ handle_disconnect();
return FALSE;
}
if (condition & G_IO_IN) {
@@ -71,9 +79,7 @@ namespace tether::ui {
}
if (line)
g_free(line);
- stop_event_subscription();
- schedule_event_retry();
- set_status_main("Daemon Offline");
+ handle_disconnect();
return FALSE;
}
if (line && length > 0) {
@@ -151,6 +157,10 @@ namespace tether::ui {
if (write(fd, kSubscribe, sizeof(kSubscribe) - 1) < 0) {
debug::log(ERR, "subscribe write error\n");
}
+ // The daemon answers subscribe with the current Bluetooth connection
+ // status, but ask anyway: an older daemon will not, and a view whose
+ // composer is gated on that status stays shut until it arrives.
+ daemon_send({{"command", "bt_connection"}});
set_status_main("Daemon Online");
return G_SOURCE_REMOVE;
}
@@ -170,15 +180,43 @@ namespace tether::ui {
}
}
- void daemon_send(const nlohmann::json& message) {
- if (g_event_fd >= 0) {
- std::string payload = message.dump() + "\n";
- if (::write(g_event_fd, payload.c_str(), payload.size()) < 0) {
- debug::log(ERR, "daemon write error\n");
+ bool daemon_send(const nlohmann::json& message) {
+ if (g_event_fd < 0)
+ return false;
+
+ // The socket is non-blocking, and these are newline-delimited commands:
+ // a short write leaves the daemon parsing a fragment and every later
+ // command on this socket is misframed. Write it all or report failure.
+ const std::string payload = message.dump() + "\n";
+ size_t written = 0;
+ int stalls = 0;
+ while (written < payload.size()) {
+ const ssize_t n = ::write(g_event_fd, payload.data() + written, payload.size() - written);
+ if (n > 0) {
+ written += static_cast<size_t>(n);
+ stalls = 0;
+ continue;
+ }
+ if (n < 0 && errno == EINTR)
+ continue;
+ if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
+ // Blocking the UI thread is the lesser evil against dropping a
+ // command the user just asked for, but only briefly.
+ if (++stalls > 200) {
+ debug::log(ERR, "daemon send stalled; dropped {} command", message.value("command", "?"));
+ return false;
+ }
+ g_usleep(1000);
+ continue;
}
+ debug::log(ERR, "daemon write error: {}", std::strerror(errno));
+ return false;
}
+ return true;
}
+ void daemon_client_on_disconnect(DaemonDisconnectFn on_disconnect) { g_on_disconnect = std::move(on_disconnect); }
+
bool daemon_connected() { return g_event_fd >= 0; }
} // namespace tether::ui
diff --git a/src/gtk/daemon_client.hpp b/src/gtk/daemon_client.hpp
index f066386..12b234b 100644
--- a/src/gtk/daemon_client.hpp
+++ b/src/gtk/daemon_client.hpp
@@ -12,7 +12,14 @@ namespace tether::ui {
void daemon_client_start(DaemonEventFn on_event);
void daemon_client_stop();
- void daemon_send(const nlohmann::json& message);
+ // Returns false when the command could not be handed to the daemon, so a
+ // caller that latches UI state on having sent something can tell.
+ bool daemon_send(const nlohmann::json& message);
bool daemon_connected();
+ // Called after the event feed goes away, so views can drop any state that
+ // was waiting on a reply that is never going to arrive.
+ using DaemonDisconnectFn = std::function<void()>;
+ void daemon_client_on_disconnect(DaemonDisconnectFn on_disconnect);
+
} // namespace tether::ui
diff --git a/src/gtk/main.cpp b/src/gtk/main.cpp
index da02e2e..294d6e4 100644
--- a/src/gtk/main.cpp
+++ b/src/gtk/main.cpp
@@ -62,6 +62,7 @@ namespace {
// One feed, dispatched to whichever view owns the event; the views never
// hold the socket themselves.
+ daemon_client_on_disconnect([] { messages_view_handle_disconnect(); });
daemon_client_start([](const nlohmann::json& event) {
if (devices_view_handle_event(event))
return;
@@ -73,11 +74,10 @@ namespace {
devices_view_trigger_discovery();
devices_view_refresh();
- // Ask once at startup so the messages banner reflects reality before the
- // user ever opens that view.
- nlohmann::json connection;
- connection["command"] = "bt_connection";
- daemon_send(connection);
+ // Bluetooth connection status is requested by the client on every
+ // subscription, including reconnects, so there is nothing to ask for
+ // here: doing it once from this point would be lost whenever the daemon
+ // had to be autostarted and no socket existed yet.
gtk_widget_show_all(window);
gtk_stack_set_visible_child_name(GTK_STACK(stack), "devices");
diff --git a/src/gtk/messages_view.cpp b/src/gtk/messages_view.cpp
index 6abf803..70420f6 100644
--- a/src/gtk/messages_view.cpp
+++ b/src/gtk/messages_view.cpp
@@ -3,7 +3,9 @@
#include "ui_util.hpp"
#include <ctime>
+#include <set>
#include <string>
+#include <vector>
namespace tether::ui {
@@ -12,6 +14,7 @@ namespace tether::ui {
struct MessagesState {
GtkWidget* banner = nullptr;
GtkWidget* banner_label = nullptr;
+ GtkWidget* banner_action = nullptr;
GtkWidget* thread_list = nullptr;
GtkWidget* conversation = nullptr;
GtkWidget* conversation_scroll = nullptr;
@@ -24,6 +27,16 @@ namespace tether::ui {
bool visible = false;
bool map_open = false;
bool sending = false;
+ // Rebuilding the thread list destroys and recreates the selected row.
+ // The handler is blocked across that so the churn is not mistaken for
+ // the user opening a conversation.
+ gulong thread_selected_handler = 0;
+ // A send whose result never arrives must not lock the composer for the
+ // rest of the session.
+ guint send_watchdog_id = 0;
+ // Handles already asked about, so reopening a conversation does not
+ // re-issue a blocking OBEX write per message every time.
+ std::set<std::string> marked_read;
// Whether the daemon says the selected thread can be replied to, and
// why not when it cannot.
bool selected_repliable = false;
@@ -32,7 +45,13 @@ namespace tether::ui {
MessagesState g_messages;
+ // Sending is a real OBEX transfer; this only has to outlast a healthy one.
+ constexpr int SEND_TIMEOUT_SECONDS = 45;
+
void update_composer_sensitivity();
+ void apply_row_selection(GtkWidget* row);
+ void clear_selection();
+ void set_banner(const std::string& text);
std::string format_timestamp(int64_t epoch) {
if (epoch <= 0)
@@ -159,10 +178,19 @@ namespace tether::ui {
}
void show_threads(const nlohmann::json& event) {
- clear_list_box(g_messages.thread_list);
if (!event.contains("threads") || !event["threads"].is_array())
return;
+ // Clearing the list destroys the selected row, which fires
+ // row-selected(nullptr) and then again for the row that replaces it.
+ // Unblocked, that reads as the user opening a conversation: it
+ // re-requests the messages, which re-sends their mark-read requests,
+ // which produce another refresh — a loop that never settles.
+ if (g_messages.thread_selected_handler)
+ g_signal_handler_block(g_messages.thread_list, g_messages.thread_selected_handler);
+
+ clear_list_box(g_messages.thread_list);
+
GtkWidget* reselect = nullptr;
for (const auto& thread : event["threads"]) {
GtkWidget* row = build_thread_row(thread);
@@ -172,10 +200,18 @@ namespace tether::ui {
}
gtk_widget_show_all(g_messages.thread_list);
- // Rebuilding the list drops the selection; restoring it keeps the
- // open conversation from closing every time a message arrives.
- if (reselect)
+ // Restoring the selection keeps the open conversation from closing
+ // every time a message arrives. The row is new, so the reply
+ // eligibility attached to it has to be re-read.
+ if (reselect) {
gtk_list_box_select_row(GTK_LIST_BOX(g_messages.thread_list), GTK_LIST_BOX_ROW(reselect));
+ apply_row_selection(reselect);
+ } else if (!g_messages.selected_thread.empty()) {
+ clear_selection();
+ }
+
+ if (g_messages.thread_selected_handler)
+ g_signal_handler_unblock(g_messages.thread_list, g_messages.thread_selected_handler);
}
void show_messages(const nlohmann::json& event) {
@@ -183,25 +219,38 @@ namespace tether::ui {
return;
clear_list_box(g_messages.conversation);
- if (event.contains("messages") && event["messages"].is_array()) {
- for (const auto& message : event["messages"])
- gtk_list_box_insert(GTK_LIST_BOX(g_messages.conversation), build_message_row(message), -1);
+ if (!event.contains("messages") || !event["messages"].is_array()) {
+ gtk_widget_show_all(g_messages.conversation);
+ return;
}
+
+ for (const auto& message : event["messages"])
+ gtk_list_box_insert(GTK_LIST_BOX(g_messages.conversation), build_message_row(message), -1);
gtk_widget_show_all(g_messages.conversation);
scroll_conversation_to_end();
- // Opening a conversation marks it read here and on the phone, which
- // is what makes the unread badge agree with what the user has seen.
- if (event.contains("messages") && event["messages"].is_array()) {
- for (const auto& message : event["messages"]) {
- if (message.value("read", true) || message.value("outgoing", false))
- continue;
- nlohmann::json j;
- j["command"] = "bt_mark_read";
- j["handle"] = message.value("handle", "");
- j["read"] = true;
- daemon_send(j);
- }
+ // Opening a conversation marks it read here and on the phone, which is
+ // what makes the unread badge agree with what the user has seen. One
+ // request for the whole batch, and never twice for the same message:
+ // each one costs the daemon a blocking OBEX write.
+ std::vector<std::string> pending;
+ for (const auto& message : event["messages"]) {
+ if (message.value("read", true) || message.value("outgoing", false))
+ continue;
+ const std::string handle = message.value("handle", "");
+ if (!handle.empty() && !g_messages.marked_read.count(handle))
+ pending.push_back(handle);
+ }
+ if (!pending.empty()) {
+ nlohmann::json j;
+ j["command"] = "bt_mark_read";
+ j["handles"] = pending;
+ j["read"] = true;
+ // Only remember them once the request is actually on its way, so a
+ // command lost to a dead socket is retried next time rather than
+ // leaving the conversation permanently stuck unread.
+ if (daemon_send(j))
+ g_messages.marked_read.insert(pending.begin(), pending.end());
}
}
@@ -216,6 +265,17 @@ namespace tether::ui {
gtk_widget_show_all(g_messages.banner);
}
+ // The advertisement that makes iOS reveal its Messages and Contacts
+ // permission toggles expires a few minutes after pairing. Without this the
+ // only way back to those toggles is to remove the bond and pair again.
+ void on_solicit_clicked(GtkWidget*, gpointer) {
+ if (!daemon_send({{"command", "bt_solicit"}})) {
+ set_status_main("Could not reach the Tether daemon.");
+ return;
+ }
+ set_status_main("Asking the iPhone to show its Bluetooth permissions…");
+ }
+
void update_connection(const nlohmann::json& event) {
const bool map_open = event.value("map_open", false);
g_messages.map_open = map_open;
@@ -273,6 +333,24 @@ namespace tether::ui {
return out;
}
+ void clear_sending() {
+ if (g_messages.send_watchdog_id != 0) {
+ g_source_remove(g_messages.send_watchdog_id);
+ g_messages.send_watchdog_id = 0;
+ }
+ g_messages.sending = false;
+ }
+
+ gboolean on_send_timeout(gpointer) {
+ g_messages.send_watchdog_id = 0;
+ if (g_messages.sending) {
+ g_messages.sending = false;
+ update_composer_sensitivity();
+ set_status_main("No answer about that message; it may still have been sent.");
+ }
+ return G_SOURCE_REMOVE;
+ }
+
void on_send_clicked(GtkWidget*, gpointer) {
const std::string body = composer_text();
if (body.empty() || g_messages.selected_thread.empty())
@@ -282,18 +360,23 @@ namespace tether::ui {
j["command"] = "bt_send_message";
j["thread"] = g_messages.selected_thread;
j["body"] = body;
- daemon_send(j);
+ if (!daemon_send(j)) {
+ set_status_main("Could not reach the Tether daemon; the message was not sent.");
+ return;
+ }
// The composer stays locked until the phone answers. Sending is a
// real OBEX transfer and takes a moment; an unlocked box invites a
- // second copy of the same message.
+ // second copy of the same message. The watchdog is what stops a
+ // result that never arrives from locking it for the whole session.
g_messages.sending = true;
+ g_messages.send_watchdog_id = g_timeout_add_seconds(SEND_TIMEOUT_SECONDS, on_send_timeout, nullptr);
update_composer_sensitivity();
set_status_main("Sending…");
}
void on_send_result(const nlohmann::json& event) {
- g_messages.sending = false;
+ clear_sending();
update_composer_sensitivity();
if (event.value("success", false)) {
@@ -307,14 +390,18 @@ namespace tether::ui {
set_status_main(event.value("message", "The message was not sent."));
}
- void on_thread_selected(GtkListBox*, GtkListBoxRow* row, gpointer) {
- if (!row) {
- g_messages.selected_thread.clear();
- g_messages.selected_repliable = false;
- g_messages.selected_block_reason.clear();
- update_composer_sensitivity();
- return;
- }
+ void clear_selection() {
+ g_messages.selected_thread.clear();
+ g_messages.selected_name.clear();
+ g_messages.selected_repliable = false;
+ g_messages.selected_block_reason.clear();
+ update_composer_sensitivity();
+ gtk_stack_set_visible_child_name(GTK_STACK(g_messages.placeholder_stack), "placeholder");
+ }
+
+ // Reads the thread identity and reply eligibility the daemon attached to
+ // this row. The daemon owns that decision; the UI must not re-derive it.
+ void apply_row_selection(GtkWidget* row) {
const char* thread = (const char*)g_object_get_data(G_OBJECT(row), "thread");
const char* name = (const char*)g_object_get_data(G_OBJECT(row), "name");
const char* block_reason = (const char*)g_object_get_data(G_OBJECT(row), "reply_reason");
@@ -323,12 +410,30 @@ namespace tether::ui {
g_messages.selected_repliable = g_object_get_data(G_OBJECT(row), "repliable") != nullptr;
g_messages.selected_block_reason = block_reason ? block_reason : "";
update_composer_sensitivity();
+ }
+
+ void on_thread_selected(GtkListBox*, GtkListBoxRow* row, gpointer) {
+ if (!row) {
+ clear_selection();
+ return;
+ }
+ apply_row_selection(GTK_WIDGET(row));
gtk_stack_set_visible_child_name(GTK_STACK(g_messages.placeholder_stack), "conversation");
request_messages(g_messages.selected_thread);
}
} // namespace
+ void messages_view_handle_disconnect() {
+ clear_sending();
+ g_messages.map_open = false;
+ // The next session re-lists messages under fresh object paths, so nothing
+ // is owed from the one that just ended.
+ g_messages.marked_read.clear();
+ update_composer_sensitivity();
+ set_banner("The Tether daemon is not running.");
+ }
+
void messages_view_set_visible(bool visible) {
g_messages.visible = visible;
if (visible)
@@ -370,6 +475,10 @@ namespace tether::ui {
request_threads();
return true;
}
+ if (command == "bt_solicit_result") {
+ set_status_main(event.value("message", ""));
+ return true;
+ }
return false;
}
@@ -384,6 +493,15 @@ namespace tether::ui {
gtk_label_set_xalign(GTK_LABEL(g_messages.banner_label), 0.0);
gtk_label_set_line_wrap(GTK_LABEL(g_messages.banner_label), TRUE);
gtk_box_pack_start(GTK_BOX(g_messages.banner), g_messages.banner_label, TRUE, TRUE, 0);
+
+ g_messages.banner_action = gtk_button_new_with_label("Show iPhone Permissions");
+ gtk_widget_set_valign(g_messages.banner_action, GTK_ALIGN_CENTER);
+ gtk_widget_set_tooltip_text(g_messages.banner_action,
+ "Re-advertise so the iPhone shows its Show Message Notifications and "
+ "Sync Contacts toggles under Settings > Bluetooth > (i).");
+ g_signal_connect(g_messages.banner_action, "clicked", G_CALLBACK(on_solicit_clicked), nullptr);
+ gtk_box_pack_start(GTK_BOX(g_messages.banner), g_messages.banner_action, FALSE, FALSE, 0);
+
gtk_box_pack_start(GTK_BOX(root), g_messages.banner, FALSE, FALSE, 0);
GtkWidget* paned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
@@ -393,7 +511,8 @@ namespace tether::ui {
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(thread_scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_widget_set_size_request(thread_scroll, 240, -1);
g_messages.thread_list = gtk_list_box_new();
- g_signal_connect(g_messages.thread_list, "row-selected", G_CALLBACK(on_thread_selected), nullptr);
+ g_messages.thread_selected_handler =
+ g_signal_connect(g_messages.thread_list, "row-selected", G_CALLBACK(on_thread_selected), nullptr);
gtk_container_add(GTK_CONTAINER(thread_scroll), g_messages.thread_list);
gtk_paned_pack1(GTK_PANED(paned), thread_scroll, FALSE, FALSE);
diff --git a/src/gtk/messages_view.hpp b/src/gtk/messages_view.hpp
index 40b3563..3dff564 100644
--- a/src/gtk/messages_view.hpp
+++ b/src/gtk/messages_view.hpp
@@ -16,4 +16,8 @@ namespace tether::ui {
// to be told when it comes and goes.
void messages_view_set_visible(bool visible);
+ // Drops anything that was waiting on a daemon reply. Without this a send in
+ // flight when the daemon goes away leaves the composer disabled for good.
+ void messages_view_handle_disconnect();
+
} // namespace tether::ui