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

+11
View File
@@ -1113,6 +1113,7 @@ pub fn ChandraCompositor::has_timed_work(self : ChandraCompositor) -> Bool {
self.shot_toast is Some(_) ||
self.auth_animating() ||
self.thumbs_pending() ||
self.server.pending_transfers() > 0 ||
self.overview_animating() ||
self.shell.animating() ||
self.shell.face_busy() ||
@@ -2252,6 +2253,16 @@ pub fn ChandraCompositor::remote_paste(
false
}
///| Press the focused window's copy keys: Ctrl+C, or Ctrl+Shift+C in terminals, where Ctrl+C
/// interrupts. False where there is nothing to copy from: a dialog or popup of the shell's own.
pub fn ChandraCompositor::remote_copy(self : ChandraCompositor) -> Bool {
if self.locked || self.auth is Some(_) || self.shell.popup_open() || self.kbd_win is None {
return false
}
self.remote_key_tap(46U, if self.terminal_focused() { 5U } else { 4U }, @sys.monotonic_ms().to_uint())
true
}
///| Whether the window with keyboard focus looks like a terminal, by its app id.
fn ChandraCompositor::terminal_focused(self : ChandraCompositor) -> Bool {
match self.kbd_win {
+27
View File
@@ -191,6 +191,9 @@ fn ChandraCompositor::control_tick(self : ChandraCompositor) -> Unit {
let line = @utf8.decode_lossy(Bytes::from_array(cl.pending))
cl.pending.clear()
let (out, _) = self.control_command(line, file=cl.file)
if cl.file >= 0 && self.server.is_transfer(cl.file) {
cl.file = -1 // the clipboard is still being written into it
}
cl.drop_file()
let reply = StringBuilder::new()
for l in out {
@@ -908,6 +911,30 @@ pub fn ChandraCompositor::control_command(
}
"clipboard-status" =>
(["ok serial=" + self.server.selection_serial.to_string()], true)
// chandra-remote: write the clipboard into `file`, a pipe, and say what it is.
"clipboard-get-fd" => {
if words.length() != 1 || file < 0 {
return (["error usage: clipboard-get-fd, with a pipe sent along"], false)
}
if self.locked {
return (["error locked"], false)
}
match self.server.send_clipboard_to(file) {
Some(mime) =>
(["ok serial=" + self.server.selection_serial.to_string() + " type=" + mime], true)
None => (["error empty"], false)
}
}
"input-copy" => {
if words.length() != 1 {
return (["error usage: input-copy"], false)
}
if self.remote_copy() {
(["ok keys"], true)
} else {
(["ok nothing"], true)
}
}
"input-paste" => {
let chord = if words.length() == 3 {
match (parse_uint_opt(words[1]), parse_uint_opt(words[2])) {
+75
View File
@@ -322,3 +322,78 @@ test "a paste still being written keeps its data when the clipboard changes" {
@sys.unlink(sink_path)
comp.close()
}
///|
test "the clipboard written into a pipe sent along: the compositor's own, a client's, none, locked" {
let (comp, path) = control_comp("clipget")
let fd = @sys.memfd_create("out", 0)
assert_eq(ask_with_file(comp, path, "clipboard-get-fd", fd), "error empty")
assert_true(ask(comp, path, "clipboard-get-fd")[0].has_prefix("error usage"))
// The compositor's own: more than fits at once, so it is written over several turns.
let sink_path = path + ".sink"
let listener = @sys.unix_listen(sink_path)
let reader = @sys.unix_connect(sink_path)
let writer = @sys.unix_accept(listener)
let data = Bytes::makei(3000000, i => (i * 7 % 251).to_byte())
comp.server.set_own_selection(["image/png"], data)
assert_eq(ask_with_file(comp, path, "clipboard-get-fd", writer), "ok serial=1 type=image/png")
@sys.close_fd(writer)
assert_true(comp.has_timed_work())
let buf : FixedArray[Byte] = FixedArray::make(65536, b'\x00')
let mut got = 0
let mut same = true
let mut eof = false
for _i = 0; _i < 2000 && !eof; _i = _i + 1 {
comp.step()
if @sys.poll_read(reader, 5) > 0 {
let (n, _) = @sys.recvmsg_fd(reader, buf, 65536)
if n == 0 {
eof = true
}
for j = 0; j < n; j = j + 1 {
same = same && buf[j] == data[got + j]
}
if n > 0 {
got = got + n
}
}
}
assert_true(eof) // the compositor closed its end
assert_true(same)
assert_eq(got, data.length())
@sys.close_fd(reader)
@sys.close_fd(listener)
@sys.unlink(sink_path)
// A client's: it is asked to write, as text when there is any.
let (a, _, _, _, _, _) = open_and_configure_client(comp, comp.server.socket_path, "A", "foot")
let (mgr, dev) = data_device_for(a)
let src = a.alloc_id()
let w1 = @wayland.WireWriter::new()
w1.u32(src)
a.send(w1.finish_msg(mgr, 0U))
for m in ["image/png", "UTF8_STRING"] {
let w = @wayland.WireWriter::new()
w.str(m)
a.send(w.finish_msg(src, 0U))
}
let w2 = @wayland.WireWriter::new()
w2.u32(src)
w2.u32(1U)
a.send(w2.finish_msg(dev, 1U))
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
ignore(drain_args(a))
assert_eq(ask_with_file(comp, path, "clipboard-get-fd", fd), "ok serial=2 type=text/plain")
assert_true(has_msg(drain_args(a).map(m => (m.0, m.1)), src, 1U)) // send(mime, fd)
// Copy keys: to the focused window, never into the shell's own popups.
assert_eq(ask(comp, path, "input-copy"), ["ok keys"])
assert_eq(ask(comp, path, "shortcut launcher"), ["ok launcher"])
assert_eq(ask(comp, path, "input-copy"), ["ok nothing"])
assert_eq(ask(comp, path, "shortcut launcher"), ["ok launcher"])
comp.locked = true
assert_eq(ask_with_file(comp, path, "clipboard-get-fd", fd), "error locked")
@sys.close_fd(fd)
comp.close()
}
+53
View File
@@ -2638,6 +2638,59 @@ pub fn WaylandServer::own_selection_text(self : WaylandServer) -> Bytes? {
}
}
///| Have the clipboard written into `fd` (a pipe), as text when it has any, else as a PNG image.
/// What it is: "text/plain" or "image/png", or None when it holds neither. The compositor's own
/// data is written by `pump_transfers`, which then owns `fd` (see `is_transfer`); a client's is
/// written by that client, and `fd` stays the caller's.
pub fn WaylandServer::send_clipboard_to(self : WaylandServer, fd : Int) -> String? {
let mimes : Array[String] = match self.selection {
Some((0U, 0U)) => self.own_mimes
Some((ocid, osid)) =>
match self.find_client(ocid) {
Some(oc) if oc.alive => oc.data_sources.get(osid).unwrap_or([])
_ => []
}
None => []
}
let (mime, kind) = match
clipboard_text_mimes.iter().find_first(m => mimes.contains(m)) {
Some(m) => (m, "text/plain")
None =>
if mimes.contains("image/png") {
("image/png", "image/png")
} else {
return None
}
}
match self.selection {
Some((0U, 0U)) =>
self.transfers.push((fd, self.own_data, 0, @sys.monotonic_ms() + 10000UL))
Some((ocid, osid)) =>
match self.find_client(ocid) {
Some(oc) => {
let w = WireWriter::new()
w.str(mime)
if @sys.sendmsg_fd(oc.fd, w.finish_msg(osid, 1U), fd) < 0 {
return None
}
}
None => return None
}
None => return None
}
Some(kind)
}
///| The types UTF-8 text goes by on the clipboard, best first.
let clipboard_text_mimes : Array[String] = [
"text/plain;charset=utf-8", "text/plain;charset=UTF-8", "UTF8_STRING", "text/plain",
]
///| Whether `fd` is a paste of the compositor's clipboard still being written (and closed) here.
pub fn WaylandServer::is_transfer(self : WaylandServer, fd : Int) -> Bool {
self.transfers.iter().any(t => t.0 == fd)
}
///| Write what the pipes of pending pastes can take without blocking; close the finished ones
/// and any a reader stopped taking for ten seconds.
pub fn WaylandServer::pump_transfers(self : WaylandServer) -> Unit {