Skip to content

Linux + iPhone Continuity / iMessage / SMS

feat(gtk): (dis)connect airpods

Zack Bartel23e869bf8e57
diff --git a/src/core/src/net.cpp b/src/core/src/net.cpp
index 137dc0c..c398e23 100644
--- a/src/core/src/net.cpp
+++ b/src/core/src/net.cpp
@@ -574,6 +574,27 @@ namespace tether {
broadcast_local_event(build_bt_status().dump());
}
+ // AirPods only: a manual disconnect of the supervised iPhone would fight its reconnects.
+ static void run_bt_airpods_connect(const std::string& address, bool connect) {
+ bool ok = false;
+ std::string err;
+ if (!bluetooth::g_bluez) {
+ err = _("Bluetooth is unavailable on this machine.");
+ } else {
+ bool airpods = false;
+ for (const auto& device : bluetooth::g_bluez->snapshot().devices)
+ airpods = airpods || (device.address == address && device.looks_like_airpods());
+ if (!airpods)
+ err = _("Not an AirPods device.");
+ else if (connect)
+ ok = bluetooth::connect_device(*bluetooth::g_bluez, address, err);
+ else
+ ok = bluetooth::disconnect_device(*bluetooth::g_bluez, address, err);
+ }
+ broadcast_local_event(
+ nlohmann::json{{"command", "bt_airpods_connect_result"}, {"success", ok}, {"message", err}}.dump());
+ }
+
nlohmann::json build_bt_threads() {
nlohmann::json result;
result["command"] = "bt_threads";
@@ -1031,6 +1052,10 @@ namespace tether {
} else if (j.contains("command") && j["command"] == "bt_unpair" && j.contains("address")) {
std::string address = j["address"];
std::thread([address]() { run_bt_unpair(address); }).detach();
+ } else if (j.contains("command") && j["command"] == "bt_airpods_connect" && j.contains("address")) {
+ std::string address = j["address"];
+ const bool connect = j.value("connect", true);
+ std::thread([address, connect]() { run_bt_airpods_connect(address, connect); }).detach();
} else if (j.contains("command") && j["command"] == "bt_set_device" && j.contains("address")) {
auto config = bluetooth::load_config();
config.device_address = j["address"];
diff --git a/src/gtk/devices_view.cpp b/src/gtk/devices_view.cpp
index 76c6632..b4d7f78 100644
--- a/src/gtk/devices_view.cpp
+++ b/src/gtk/devices_view.cpp
@@ -69,6 +69,8 @@ namespace tether::ui {
GtkWidget* lbl_airpods_apple_id = nullptr;
GtkWidget* airpods_mode_buttons[4] = {nullptr, nullptr, nullptr, nullptr};
bool airpods_syncing = false;
+ GtkWidget* btn_airpods_connect = nullptr;
+ bool airpods_connecting = false;
GtkWidget* lbl_bt_name = nullptr;
GtkWidget* bt_setup_box = nullptr;
@@ -272,6 +274,21 @@ namespace tether::ui {
daemon_send({{"command", "bt_airpods_mode"}, {"mode", AIRPODS_MODES[GPOINTER_TO_INT(data)].wire}});
}
+ void on_airpods_connect_click(GtkWidget* button, gpointer) {
+ const nlohmann::json* device = find_bt_device(g_devices.selected_bt_address);
+ if (!device)
+ return;
+ if (!daemon_send({{"command", "bt_airpods_connect"},
+ {"address", g_devices.selected_bt_address},
+ {"connect", !device->value("connected", false)}})) {
+ set_status_main(_("Could not reach the Tether daemon."));
+ return;
+ }
+ // Held until the result, so a slow Connect is not sent twice.
+ g_devices.airpods_connecting = true;
+ gtk_widget_set_sensitive(button, FALSE);
+ }
+
void update_airpods_pane() {
const nlohmann::json& airpods = g_devices.bt_airpods;
const bool managed = g_devices.bt_status.value("airpods_enabled", false);
@@ -284,6 +301,12 @@ namespace tether::ui {
: levels.empty() ? _("Reading battery\u2026")
: levels);
+ // bluez link action, so it works whether or not tether manages the buds
+ const nlohmann::json* device = find_bt_device(g_devices.selected_bt_address);
+ gtk_button_set_label(GTK_BUTTON(g_devices.btn_airpods_connect),
+ device && device->value("connected", false) ? _("Disconnect") : _("Connect"));
+ gtk_widget_set_sensitive(g_devices.btn_airpods_connect, device && !g_devices.airpods_connecting);
+
const std::string current = json_string(airpods, "anc");
g_devices.airpods_syncing = true;
for (int i = 0; i < 4; ++i) {
@@ -840,6 +863,7 @@ namespace tether::ui {
void devices_view_handle_disconnect() {
set_bt_progress("");
+ g_devices.airpods_connecting = false;
g_devices.send_queue.clear();
g_devices.send_batch_total = 0;
g_devices.send_failed = 0;
@@ -1363,6 +1387,13 @@ namespace tether::ui {
new std::string(event.value("code", "")));
return true;
}
+ if (command == "bt_airpods_connect_result") {
+ g_devices.airpods_connecting = false;
+ if (!event.value("success", false))
+ set_status_main(event.value("message", ""));
+ update_right_pane();
+ return true;
+ }
if (command == "bt_pair_progress") {
set_bt_progress(event.value("step", "") + " " + event.value("detail", ""));
return true;
@@ -1495,6 +1526,11 @@ namespace tether::ui {
gtk_style_context_add_class(gtk_widget_get_style_context(g_devices.lbl_airpods_battery), "muted");
gtk_box_pack_start(GTK_BOX(airpods_box), g_devices.lbl_airpods_battery, FALSE, FALSE, 0);
+ g_devices.btn_airpods_connect = gtk_button_new_with_label(_("Connect"));
+ gtk_widget_set_halign(g_devices.btn_airpods_connect, GTK_ALIGN_START);
+ g_signal_connect(g_devices.btn_airpods_connect, "clicked", G_CALLBACK(on_airpods_connect_click), nullptr);
+ gtk_box_pack_start(GTK_BOX(airpods_box), g_devices.btn_airpods_connect, FALSE, FALSE, 0);
+
GtkWidget* lbl_mode_title = gtk_label_new(nullptr);
gtk_label_set_xalign(GTK_LABEL(lbl_mode_title), 0.0);
set_markup(lbl_mode_title, "<b>" + escape_markup(_("Listening mode")) + "</b>");