Remote: paste the phone clipboard, including images, on the desktop
CI / test (push) Failing after 1m2s
CI / test (push) Failing after 1m2s
- Web UI: Ctrl+V, Ctrl+Shift+V, Shift+Insert (Cmd+V on Mac) and a Paste button send the phone clipboard (text or PNG, up to 16 MB) to the desktop over POST /api/clipboard - chandra-remote passes clipboard data to the compositor in a sealed memfd (clipboard-set-fd), replacing the 4 KB hex clipboard-set line - Compositor: new clipboard-status and input-paste commands; text is typed into the launcher, password dialog and shell popups, otherwise the paste chord is sent (Ctrl+Shift+V in terminals) - Paste data is captured when a paste starts, so a clipboard change mid-transfer can't corrupt it - VNC clipboard text limit raised from 4 KB to 1 MB - Upload endpoints are authorized with the same token check as /ws; the no-header cross-site case is refused with 403
This commit is contained in:
1 parent
8a6d19d16f
commit
d2e00b4658
11 files changed
+853
-68
No files matched your search
@@ -2172,10 +2172,69 @@ pub fn ChandraCompositor::remote_set_clipboard(
|
||||
self : ChandraCompositor,
|
||||
text : String,
|
||||
) -> Unit {
|
||||
self.server.set_own_selection(
|
||||
["text/plain;charset=utf-8", "text/plain", "UTF8_STRING"],
|
||||
@utf8.encode(text),
|
||||
)
|
||||
self.remote_set_clipboard_data("text/plain", @utf8.encode(text))
|
||||
}
|
||||
|
||||
///| Put `data` on the compositor's own Wayland clipboard as `mime`: UTF-8 text, or an image.
|
||||
fn ChandraCompositor::remote_set_clipboard_data(
|
||||
self : ChandraCompositor,
|
||||
mime : String,
|
||||
data : Bytes,
|
||||
) -> Unit {
|
||||
let mimes = if mime == "text/plain" {
|
||||
["text/plain;charset=utf-8", "text/plain", "UTF8_STRING"]
|
||||
} else {
|
||||
[mime]
|
||||
}
|
||||
self.server.set_own_selection(mimes, data)
|
||||
}
|
||||
|
||||
///| Paste the clipboard. The password dialog and the shell's popups have no paste key of their
|
||||
/// own, so clipboard text is typed into them as one line. Anything else gets the paste chord:
|
||||
/// `chord` as (evdev code, modifiers), or the focused app's usual one. True when typed.
|
||||
pub fn ChandraCompositor::remote_paste(
|
||||
self : ChandraCompositor,
|
||||
chord : (UInt, UInt)?,
|
||||
) -> Bool {
|
||||
let text = self.server.own_selection_text()
|
||||
match text {
|
||||
Some(t) =>
|
||||
if self.auth is Some(_) || self.shell.popup_open() {
|
||||
let line = StringBuilder::new()
|
||||
for ch in @utf8.decode_lossy(t).trim() {
|
||||
line.write_char(if ch.to_int() < 0x20 { ' ' } else { ch })
|
||||
}
|
||||
return self.remote_commit_text(line.to_string())
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
let (code, mods) = match chord {
|
||||
Some(c) => c
|
||||
// Ctrl+V; terminals keep that for themselves and paste text with Ctrl+Shift+V.
|
||||
None => (47U, if text is Some(_) && self.terminal_focused() { 5U } else { 4U })
|
||||
}
|
||||
self.remote_key_tap(code, mods, @sys.monotonic_ms().to_uint())
|
||||
false
|
||||
}
|
||||
|
||||
///| 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 {
|
||||
Some(wid) =>
|
||||
match self.windows.get(wid) {
|
||||
Some(w) => {
|
||||
let id = w.app_id.to_lower()
|
||||
for s in ["term", "konsole", "console", "foot", "kitty", "alacritty", "ghostty", "ptyxis", "org.kde.kwi"] {
|
||||
if id.contains(s) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
None => false
|
||||
}
|
||||
None => false
|
||||
}
|
||||
}
|
||||
|
||||
///| Commit UTF-8 text directly to the active popup, Wayland text-input-v3 surface, or desktop;
|
||||
|
||||
@@ -20,10 +20,12 @@
|
||||
/// The main loop keeps sleeping while idle: the listening socket and the connected clients are
|
||||
/// in `control_fds`, which the loop adds to the fds it waits on.
|
||||
|
||||
///| One connected client: its fd and the bytes of a line not finished yet.
|
||||
///| One connected client: its fd, the bytes of a line not finished yet, and a file sent along
|
||||
/// (SCM_RIGHTS) for the command it is writing (-1 when none).
|
||||
struct ControlClient {
|
||||
fd : Int
|
||||
pending : Array[Byte]
|
||||
mut file : Int
|
||||
}
|
||||
|
||||
///| The listening socket, its path (unlinked on close) and the connected clients.
|
||||
@@ -89,6 +91,7 @@ pub fn ChandraCompositor::stop_control(self : ChandraCompositor) -> Unit {
|
||||
None => ()
|
||||
Some(c) => {
|
||||
for cl in c.clients {
|
||||
cl.drop_file()
|
||||
@sys.close_fd(cl.fd)
|
||||
}
|
||||
@sys.close_fd(c.fd)
|
||||
@@ -112,6 +115,35 @@ pub fn ChandraCompositor::control_fds(self : ChandraCompositor) -> Array[Int] {
|
||||
}
|
||||
}
|
||||
|
||||
///| Close the file the client sent along, if any.
|
||||
fn ControlClient::drop_file(self : ControlClient) -> Unit {
|
||||
if self.file >= 0 {
|
||||
@sys.close_fd(self.file)
|
||||
self.file = -1
|
||||
}
|
||||
}
|
||||
|
||||
///| Largest clipboard taken from a file (`clipboard-set-fd`).
|
||||
let control_max_clipboard : Int = 16 * 1024 * 1024
|
||||
|
||||
///| The first `size` bytes of the file `fd`, or None when it is shorter. Read in pieces: on the
|
||||
/// wasm build every byte of one call is copied as a value of its own.
|
||||
fn read_fd_bytes(fd : Int, size : Int) -> Bytes? {
|
||||
let chunk = 512 * 1024
|
||||
let out : FixedArray[Byte] = FixedArray::make(size, b'\x00')
|
||||
let mut at = 0
|
||||
while at < size {
|
||||
let len = if size - at > chunk { chunk } else { size - at }
|
||||
let part = @sys.mmap_read_at(fd, at, len)
|
||||
if part.length() != len {
|
||||
return None
|
||||
}
|
||||
out.blit_from_bytes(at, part, 0, len)
|
||||
at = at + len
|
||||
}
|
||||
Some(Bytes::from_fixedarray(out))
|
||||
}
|
||||
|
||||
///| Longest command line taken; a client sending more without a newline is dropped.
|
||||
let control_max_line : Int = 4096
|
||||
|
||||
@@ -134,7 +166,7 @@ fn ChandraCompositor::control_tick(self : ChandraCompositor) -> Unit {
|
||||
if fd < 0 {
|
||||
break
|
||||
}
|
||||
c.clients.push({ fd, pending: [] })
|
||||
c.clients.push({ fd, pending: [], file: -1 })
|
||||
}
|
||||
let gone : Array[Int] = []
|
||||
let buf : FixedArray[Byte] = FixedArray::make(1024, b'\x00')
|
||||
@@ -143,7 +175,8 @@ fn ChandraCompositor::control_tick(self : ChandraCompositor) -> Unit {
|
||||
while open {
|
||||
let (n, extra) = @sys.recvmsg_fd(cl.fd, buf, 1024)
|
||||
if extra >= 0 {
|
||||
@sys.close_fd(extra)
|
||||
cl.drop_file()
|
||||
cl.file = extra
|
||||
}
|
||||
if n == -1 {
|
||||
break // nothing more for now
|
||||
@@ -157,7 +190,8 @@ fn ChandraCompositor::control_tick(self : ChandraCompositor) -> Unit {
|
||||
if b == b'\n' {
|
||||
let line = @utf8.decode_lossy(Bytes::from_array(cl.pending))
|
||||
cl.pending.clear()
|
||||
let (out, _) = self.control_command(line)
|
||||
let (out, _) = self.control_command(line, file=cl.file)
|
||||
cl.drop_file()
|
||||
let reply = StringBuilder::new()
|
||||
for l in out {
|
||||
reply.write_string(l)
|
||||
@@ -177,6 +211,7 @@ fn ChandraCompositor::control_tick(self : ChandraCompositor) -> Unit {
|
||||
}
|
||||
}
|
||||
if !open {
|
||||
cl.drop_file()
|
||||
gone.push(cl.fd)
|
||||
}
|
||||
}
|
||||
@@ -275,8 +310,13 @@ fn decode_hex_utf8(s : String) -> String? {
|
||||
}
|
||||
|
||||
///| Run one control command; the reply lines (the last starts with "ok" or "error") and whether
|
||||
/// it succeeded.
|
||||
pub fn ChandraCompositor::control_command(self : ChandraCompositor, line : String) -> (Array[String], Bool) {
|
||||
/// it succeeded. `file` is the file sent along with the command (-1 when none); the caller
|
||||
/// closes it.
|
||||
pub fn ChandraCompositor::control_command(
|
||||
self : ChandraCompositor,
|
||||
line : String,
|
||||
file? : Int = -1,
|
||||
) -> (Array[String], Bool) {
|
||||
let words : Array[String] = []
|
||||
for w in line.trim(chars=" \t\r").split(" ") {
|
||||
let s = w.to_string()
|
||||
@@ -831,6 +871,46 @@ pub fn ChandraCompositor::control_command(self : ChandraCompositor, line : Strin
|
||||
None => (["error invalid hex"], false)
|
||||
}
|
||||
}
|
||||
// chandra-remote: `file` holds SIZE bytes to put on the clipboard as MIME.
|
||||
"clipboard-set-fd" => {
|
||||
if words.length() != 3 || file < 0 {
|
||||
return (["error usage: clipboard-set-fd MIME SIZE, with the file sent along"], false)
|
||||
}
|
||||
let mime = words[1]
|
||||
if mime != "text/plain" && mime != "image/png" {
|
||||
return (["error unsupported type '" + mime + "'"], false)
|
||||
}
|
||||
match parse_int_opt(words[2]) {
|
||||
Some(size) if size > 0 && size <= control_max_clipboard =>
|
||||
match read_fd_bytes(file, size) {
|
||||
Some(data) => {
|
||||
self.remote_set_clipboard_data(mime, data)
|
||||
(["ok serial=" + self.server.selection_serial.to_string()], true)
|
||||
}
|
||||
None => (["error file is shorter than " + words[2] + " bytes"], false)
|
||||
}
|
||||
_ => (["error invalid size"], false)
|
||||
}
|
||||
}
|
||||
"clipboard-status" =>
|
||||
(["ok serial=" + self.server.selection_serial.to_string()], true)
|
||||
"input-paste" => {
|
||||
let chord = if words.length() == 3 {
|
||||
match (parse_uint_opt(words[1]), parse_uint_opt(words[2])) {
|
||||
(Some(code), Some(mods)) => Some((code, mods))
|
||||
_ => return (["error invalid paste args"], false)
|
||||
}
|
||||
} else if words.length() == 1 {
|
||||
None
|
||||
} else {
|
||||
return (["error usage: input-paste [CODE MODS]"], false)
|
||||
}
|
||||
if self.remote_paste(chord) {
|
||||
(["ok typed"], true)
|
||||
} else {
|
||||
(["ok keys"], true)
|
||||
}
|
||||
}
|
||||
other => (["error unknown command '" + other + "' (try: help)"], false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,3 +207,118 @@ test "remote status, refresh, osk, pointer/key/text/clipboard injection and OLED
|
||||
assert_false(comp.manual_screen_off)
|
||||
comp.close()
|
||||
}
|
||||
|
||||
///| Like `ask` for one command, with `file` sent along.
|
||||
fn ask_with_file(
|
||||
comp : @host.ChandraCompositor,
|
||||
path : String,
|
||||
line : String,
|
||||
file : Int,
|
||||
) -> String raise {
|
||||
let fd = @sys.unix_connect(path)
|
||||
assert_true(fd >= 0)
|
||||
assert_true(@sys.sendmsg_fd(fd, @utf8.encode(line + "\n"), file) > 0)
|
||||
let buf : FixedArray[Byte] = FixedArray::make(4096, b'\x00')
|
||||
let raw : Array[Byte] = []
|
||||
for _i = 0; _i < 50 && raw.length() == 0; _i = _i + 1 {
|
||||
comp.step()
|
||||
if @sys.poll_read(fd, 5) > 0 {
|
||||
let (n, _) = @sys.recvmsg_fd(fd, buf, 4096)
|
||||
for i = 0; i < n; i = i + 1 {
|
||||
raw.push(buf[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@sys.close_fd(fd)
|
||||
@utf8.decode_lossy(Bytes::from_array(raw)).trim().to_string()
|
||||
}
|
||||
|
||||
///|
|
||||
test "clipboard from a file sent along: images, long text, limits, and paste" {
|
||||
let (comp, path) = control_comp("clipfd")
|
||||
assert_eq(ask(comp, path, "clipboard-status"), ["ok serial=0"])
|
||||
// An image larger than one read of the file.
|
||||
let size = 700000
|
||||
let png = Bytes::makei(size, i => (i * 7 % 251).to_byte())
|
||||
let fd = @sys.memfd_create("clip", size)
|
||||
assert_eq(@sys.pwrite_bytes(fd, png, 0), size)
|
||||
assert_eq(
|
||||
ask_with_file(comp, path, "clipboard-set-fd image/png " + size.to_string(), fd),
|
||||
"ok serial=1",
|
||||
)
|
||||
assert_true(comp.server.owns_selection())
|
||||
assert_eq(comp.server.own_mimes, ["image/png"])
|
||||
assert_true(comp.server.own_data == png)
|
||||
assert_true(comp.server.own_selection_text() is None)
|
||||
// Refused: no file, a type or size that isn't taken, a file that is too short.
|
||||
assert_true(ask(comp, path, "clipboard-set-fd image/png 4")[0].has_prefix("error usage"))
|
||||
assert_eq(
|
||||
ask_with_file(comp, path, "clipboard-set-fd text/html 4", fd),
|
||||
"error unsupported type 'text/html'",
|
||||
)
|
||||
assert_eq(ask_with_file(comp, path, "clipboard-set-fd image/png 0", fd), "error invalid size")
|
||||
assert_eq(
|
||||
ask_with_file(comp, path, "clipboard-set-fd image/png 99999999", fd),
|
||||
"error invalid size",
|
||||
)
|
||||
assert_eq(
|
||||
ask_with_file(comp, path, "clipboard-set-fd image/png 700001", fd),
|
||||
"error file is shorter than 700001 bytes",
|
||||
)
|
||||
assert_eq(ask(comp, path, "clipboard-status"), ["ok serial=1"])
|
||||
@sys.close_fd(fd)
|
||||
// Text: offered as text, and typed into an open popup as one line.
|
||||
let text = @utf8.encode("fire\nfox\n")
|
||||
let tfd = @sys.memfd_create("clip", text.length())
|
||||
ignore(@sys.pwrite_bytes(tfd, text, 0))
|
||||
assert_eq(
|
||||
ask_with_file(comp, path, "clipboard-set-fd text/plain " + text.length().to_string(), tfd),
|
||||
"ok serial=2",
|
||||
)
|
||||
@sys.close_fd(tfd)
|
||||
assert_eq(comp.server.own_selection_text(), Some(text))
|
||||
assert_eq(ask(comp, path, "input-paste"), ["ok keys"])
|
||||
assert_eq(ask(comp, path, "input-paste 47 4"), ["ok keys"])
|
||||
assert_true(ask(comp, path, "input-paste 47")[0].has_prefix("error usage"))
|
||||
assert_eq(ask(comp, path, "shortcut launcher"), ["ok launcher"])
|
||||
assert_eq(ask(comp, path, "input-paste 47 4"), ["ok typed"])
|
||||
assert_true(comp.shell.launcher_open())
|
||||
comp.close()
|
||||
}
|
||||
|
||||
///|
|
||||
test "a paste still being written keeps its data when the clipboard changes" {
|
||||
let (comp, path) = control_comp("clipswap")
|
||||
// A slow reader: a socket nobody reads from yet, so only part of the data fits.
|
||||
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)
|
||||
assert_true(writer >= 0)
|
||||
let first = Bytes::make(3000000, b'a')
|
||||
comp.server.set_own_selection(["image/png"], first)
|
||||
comp.server.transfers.push((writer, comp.server.own_data, 0, @sys.monotonic_ms() + 10000UL))
|
||||
comp.server.pump_transfers()
|
||||
assert_eq(comp.server.pending_transfers(), 1)
|
||||
comp.server.set_own_selection(["image/png"], Bytes::make(10, b'b'))
|
||||
let buf : FixedArray[Byte] = FixedArray::make(65536, b'\x00')
|
||||
let mut got = 0
|
||||
let mut all_first = true
|
||||
for _i = 0; _i < 2000 && got < first.length(); _i = _i + 1 {
|
||||
comp.server.pump_transfers()
|
||||
if @sys.poll_read(reader, 5) > 0 {
|
||||
let (n, _) = @sys.recvmsg_fd(reader, buf, 65536)
|
||||
for j = 0; j < n; j = j + 1 {
|
||||
all_first = all_first && buf[j] == b'a'
|
||||
}
|
||||
got = got + n
|
||||
}
|
||||
}
|
||||
assert_eq(got, first.length())
|
||||
assert_true(all_first)
|
||||
assert_eq(comp.server.pending_transfers(), 0)
|
||||
@sys.close_fd(reader)
|
||||
@sys.close_fd(listener)
|
||||
@sys.unlink(sink_path)
|
||||
comp.close()
|
||||
}
|
||||
Reference in new issue
Block a user