Skip to content

Linux + iPhone Continuity / iMessage / SMS

stub out cli

Zack Bartel3116c5100df0
diff --git a/src/cli/main.cpp b/src/cli/main.cpp
index 064ccff..3c95f45 100644
--- a/src/cli/main.cpp
+++ b/src/cli/main.cpp
@@ -1,7 +1,157 @@
+#include <cstring>
+#include <filesystem>
#include <iostream>
-#include <tether/core.hpp>
+#include <nlohmann/json.hpp>
+#include <string>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+#include <tether/net.hpp> // for get_runtime_dir
+#include <unistd.h>
+#include <vector>
-int main(int argc, char** argv) {
- std::cout << "tether cli version " << tether::get_version() << "\n";
+void print_help() {
+ std::cout << "tether - Wayland companion CLI\n\n"
+ << "Options:\n"
+ << " -h, --help Show this help message.\n"
+ << " -g, --get-clipboard Retrieve the current Wayland clipboard text.\n"
+ << " -s, --set-clipboard Take string input and copy it to the local Wayland clipboard.\n"
+ << " If no argument is given, it reads from STDIN.\n\n"
+ << "Examples:\n"
+ << " tether -g\n"
+ << " tether -s \"Hello world\"\n"
+ << " echo \"pipe me\" | tether -s\n";
+}
+
+int connect_to_daemon(bool retry = true) {
+ int sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (sock < 0)
+ return -1;
+
+ sockaddr_un addr{};
+ addr.sun_family = AF_UNIX;
+ std::string path = tether::get_runtime_dir() + "/tetherd.sock";
+ std::strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path) - 1);
+
+ if (connect(sock, (sockaddr*)&addr, sizeof(addr)) < 0) {
+ close(sock);
+ if (!retry)
+ return -1;
+
+ // Let's spawn the daemon in the background
+ pid_t pid = fork();
+ if (pid == 0) {
+ // Child process
+ std::filesystem::path self_path = std::filesystem::read_symlink("/proc/self/exe");
+ std::string daemon_path = (self_path.parent_path() / "tetherd").string();
+
+ // Double fork trick to detach completely without zombies
+ if (fork() == 0) {
+ // Grandchild - disconnects standard output to not spam CLI calling terminal
+ freopen("/dev/null", "w", stdout);
+ freopen("/dev/null", "w", stderr);
+ freopen("/dev/null", "r", stdin);
+
+ execl(daemon_path.c_str(), "tetherd", nullptr);
+ // Fallback to path lookup if it wasn't strictly adjacent
+ execlp("tetherd", "tetherd", nullptr);
+ exit(1); // Abort if launch failed
+ }
+ exit(0); // Intermediate child exits immediately
+ } else if (pid > 0) {
+ // Parent waits for intermediate child
+ int status;
+ waitpid(pid, &status, 0);
+
+ // Give daemon a generous 300ms to spin up epoll and wayland
+ usleep(300000);
+ return connect_to_daemon(false);
+ }
+ return -1;
+ }
+ return sock;
+}
+
+std::string send_and_wait(int sock, const std::string& cmd) {
+ write(sock, cmd.c_str(), cmd.size());
+ char buf[4096];
+ ssize_t n = read(sock, buf, sizeof(buf) - 1);
+ if (n > 0) {
+ buf[n] = '\0';
+ return std::string(buf);
+ }
+ return "";
+}
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ print_help();
+ return 0;
+ }
+
+ std::string action;
+ std::string arg_val;
+
+ for (int i = 1; i < argc; ++i) {
+ std::string arg = argv[i];
+ if (arg == "-h" || arg == "--help") {
+ print_help();
+ return 0;
+ } else if (arg == "-g" || arg == "--get-clipboard") {
+ action = "get";
+ } else if (arg == "-s" || arg == "--set-clipboard") {
+ action = "set";
+ // Check if there is an argument immediately following the flag
+ if (i + 1 < argc && argv[i + 1][0] != '-') {
+ arg_val = argv[++i];
+ }
+ }
+ }
+
+ if (action.empty()) {
+ std::cerr << "Unknown action. Run tether --help for options\n";
+ return 1;
+ }
+
+ if (action == "set" && arg_val.empty()) {
+ // Read from stdin
+ std::string line;
+ while (std::getline(std::cin, line)) {
+ arg_val += line + "\n";
+ }
+ if (!arg_val.empty() && arg_val.back() == '\n') {
+ arg_val.pop_back(); // clip final stray newline from stream end
+ }
+ }
+
+ int sock = connect_to_daemon();
+ if (sock < 0) {
+ std::cerr << "Failed to connect to daemon and automatic launch failed.\n";
+ return 1;
+ }
+
+ if (action == "get") {
+ nlohmann::json j;
+ j["command"] = "clipboard_get";
+ std::string payload = j.dump() + "\n";
+
+ std::string response = send_and_wait(sock, payload);
+ try {
+ nlohmann::json r = nlohmann::json::parse(response);
+ if (r.contains("content")) {
+ std::cout << r["content"].get<std::string>() << std::flush;
+ }
+ } catch (...) {
+ // fallback if protocol didn't match / ignore error
+ }
+ } else if (action == "set") {
+ nlohmann::json j;
+ j["command"] = "clipboard_set";
+ j["content"] = arg_val;
+ std::string payload = j.dump() + "\n";
+ send_and_wait(sock, payload); // Ignore daemon's "OK\n"
+ }
+
+ close(sock);
return 0;
}
diff --git a/src/core/include/tether/wayland.hpp b/src/core/include/tether/wayland.hpp
index bb9615f..3dc408a 100644
--- a/src/core/include/tether/wayland.hpp
+++ b/src/core/include/tether/wayland.hpp
@@ -5,6 +5,7 @@
#include <memory>
#include <functional>
#include <string>
+#include <mutex>
class CCWlDisplay;
class CCWlRegistry;
@@ -22,8 +23,11 @@ public:
bool init();
void set_clipboard_callback(std::function<void(const std::string&)> cb);
void copy_to_clipboard(const std::string& text);
+ std::string get_clipboard();
private:
+ std::mutex clip_mutex_;
+ std::string cached_clipboard_;
EpollEventLoop& loop_;
wl_display* raw_display_ = nullptr;
std::unique_ptr<CCWlDisplay> display_;
@@ -36,4 +40,6 @@ private:
std::function<void(const std::string&)> clipboard_cb_;
};
+extern WaylandContext* g_wayland;
+
} // namespace tether
diff --git a/src/core/src/net.cpp b/src/core/src/net.cpp
index a29d2be..00db88d 100644
--- a/src/core/src/net.cpp
+++ b/src/core/src/net.cpp
@@ -157,11 +157,18 @@ void UnixServer::handle_client(int client_fd) {
if (j.contains("command") && j["command"] == "clipboard_set" && j.contains("content")) {
std::string content = j["content"];
- // Externally hooked up in main
- extern WaylandContext* g_wayland;
if (g_wayland) {
g_wayland->copy_to_clipboard(content);
}
+ } else if (j.contains("command") && j["command"] == "clipboard_get") {
+ if (g_wayland) {
+ nlohmann::json resp;
+ resp["command"] = "clipboard_content";
+ resp["content"] = g_wayland->get_clipboard();
+ std::string payload = resp.dump() + "\n";
+ write(client_fd, payload.c_str(), payload.size());
+ return;
+ }
}
} catch (...) {
// Ignore parse errors for raw text protocol
@@ -269,10 +276,18 @@ void TcpServer::handle_client(int client_fd) {
if (j.contains("command") && j["command"] == "clipboard_set" && j.contains("content")) {
std::string content = j["content"];
- extern WaylandContext* g_wayland;
if (g_wayland) {
g_wayland->copy_to_clipboard(content);
}
+ } else if (j.contains("command") && j["command"] == "clipboard_get") {
+ if (g_wayland) {
+ nlohmann::json resp;
+ resp["command"] = "clipboard_content";
+ resp["content"] = g_wayland->get_clipboard();
+ std::string payload = resp.dump() + "\n";
+ write(client_fd, payload.c_str(), payload.size());
+ return;
+ }
}
} catch (...) {
// Ignore parse errors for raw text protocol
diff --git a/src/core/src/wayland.cpp b/src/core/src/wayland.cpp
index a37dc20..48c2168 100644
--- a/src/core/src/wayland.cpp
+++ b/src/core/src/wayland.cpp
@@ -8,6 +8,8 @@
namespace tether {
+WaylandContext* g_wayland = nullptr;
+
WaylandContext::WaylandContext(EpollEventLoop& loop) : loop_(loop) {}
WaylandContext::~WaylandContext() {
@@ -51,9 +53,15 @@ bool WaylandContext::init() {
clipboard_ = std::make_unique<ClipboardManager>(data_control_manager_.get(), seat_.get());
- if (clipboard_cb_) {
- clipboard_->set_update_callback(clipboard_cb_);
- }
+ clipboard_->set_update_callback([this](const std::string& text) {
+ {
+ std::lock_guard<std::mutex> lock(clip_mutex_);
+ cached_clipboard_ = text;
+ }
+ if (clipboard_cb_) {
+ clipboard_cb_(text);
+ }
+ });
// Attach to event loop!
int fd = wl_display_get_fd(raw_display_);
@@ -72,16 +80,22 @@ bool WaylandContext::init() {
void WaylandContext::set_clipboard_callback(std::function<void(const std::string&)> cb) {
clipboard_cb_ = std::move(cb);
- if (clipboard_) {
- clipboard_->set_update_callback(clipboard_cb_);
- }
}
void WaylandContext::copy_to_clipboard(const std::string& text) {
+ {
+ std::lock_guard<std::mutex> lock(clip_mutex_);
+ cached_clipboard_ = text;
+ }
if (clipboard_) {
clipboard_->copy(text);
wl_display_flush(raw_display_);
}
}
+std::string WaylandContext::get_clipboard() {
+ std::lock_guard<std::mutex> lock(clip_mutex_);
+ return cached_clipboard_;
+}
+
} // namespace tether
diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp
index 2f52cd6..03c133b 100644
--- a/src/daemon/main.cpp
+++ b/src/daemon/main.cpp
@@ -8,10 +8,6 @@
tether::EpollEventLoop* g_loop = nullptr;
-namespace tether {
- WaylandContext* g_wayland = nullptr;
-}
-
void signal_handler(int) {
if (g_loop) {
std::cout << "\nStopping tetherd..." << std::endl;