Remote: copy from the phone to this device's clipboard
CI / test (pull_request) Successful in 1m22s

- Web UI: Ctrl+C, Ctrl+X, Ctrl+Shift+C (Cmd+C on Mac) also put what they copied, text or PNG, on this device's clipboard; Copy, Copy Selection and Copy Clipboard buttons
- chandra-remote: GET /api/clipboard/data, with the same authorization as the upload
- Compositor: clipboard-get-fd has the clipboard's owner write into a pipe, refused while locked; input-copy presses Ctrl+Shift+C in terminals, Ctrl+C elsewhere
- Pending clipboard transfers keep the main loop awake

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-30 18:26:53 -08:00
1 parent c5dbe028e6
commit 589d167f79
11 files changed
+552 -19

No files matched your search

+56
View File
@@ -271,6 +271,62 @@ int remote_clipboard_set(CtlConn *c, const char *mime, const void *data, size_t
return parse_serial(reply, serial);
}
int remote_clipboard_get(CtlConn *c, const char **mime, uint8_t **data, size_t *len,
unsigned long *serial) {
*data = NULL;
*len = 0;
int p[2];
if (pipe2(p, O_CLOEXEC) != 0) return REMOTE_CLIP_FAILED;
fcntl(p[1], F_SETPIPE_SZ, 1 << 20); /* an image in fewer turns; the default works too */
char reply[160] = {0}, type[64] = {0};
int r = ctl_cmd_fd(c, "clipboard-get-fd", p[1], reply, sizeof reply);
close(p[1]);
int res = REMOTE_CLIP_FAILED;
if (r != 0) {
if (strcmp(reply, "error empty") == 0) res = REMOTE_CLIP_EMPTY;
else if (strcmp(reply, "error locked") == 0) res = REMOTE_CLIP_LOCKED;
else if (strstr(reply, "unknown command")) res = REMOTE_CLIP_OLD;
goto out;
}
if (sscanf(reply, "ok serial=%lu type=%63s", serial, type) != 2) goto out;
if (strcmp(type, "text/plain") == 0) *mime = "text/plain; charset=utf-8";
else if (strcmp(type, "image/png") == 0) *mime = "image/png";
else goto out;
/* The app that owns the clipboard writes it, and may be slow, stuck or endless. */
size_t cap = 0;
uint64_t deadline = remote_now_ms() + 10000;
for (;;) {
if (*len == cap) {
size_t ncap = cap ? cap * 2 : 65536;
if (ncap > REMOTE_CLIPBOARD_MAX + 1) ncap = REMOTE_CLIPBOARD_MAX + 1;
uint8_t *nd = realloc(*data, ncap);
if (!nd) goto out;
*data = nd;
cap = ncap;
}
uint64_t now = remote_now_ms();
struct pollfd pfd = { .fd = p[0], .events = POLLIN };
int pr = now >= deadline ? 0 : poll(&pfd, 1, (int)(deadline - now));
if (pr < 0 && errno == EINTR) continue;
if (pr <= 0) goto out;
ssize_t n = read(p[0], *data + *len, cap - *len);
if (n < 0 && errno == EINTR) continue;
if (n < 0) goto out;
if (n == 0) break;
*len += (size_t)n;
if (*len > REMOTE_CLIPBOARD_MAX) { res = REMOTE_CLIP_TOO_LARGE; goto out; }
}
res = *len > 0 ? REMOTE_CLIP_OK : REMOTE_CLIP_EMPTY;
out:
close(p[0]);
if (res != REMOTE_CLIP_OK) {
free(*data);
*data = NULL;
*len = 0;
}
return res;
}
void remote_osk_suppress_acquire(RemoteApp *app, int *client_flag) {
if (*client_flag) return;
pthread_mutex_lock(&app->osk_mu);