- 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:
1 parent
c5dbe028e6
commit
589d167f79
11 files changed
+552
-19
No files matched your search
+183
-19
@@ -87,7 +87,7 @@
|
||||
position: fixed; left: -9999px; top: 0; width: 1px; height: 1px;
|
||||
opacity: 0; border: 0; padding: 0; font-size: 16px;
|
||||
}
|
||||
#pasteSink {
|
||||
#pasteSink, #copySource {
|
||||
position: fixed; left: -9999px; top: 0; width: 1px; height: 1px;
|
||||
overflow: hidden; opacity: 0; outline: 0;
|
||||
user-select: text; -webkit-user-select: text;
|
||||
@@ -100,6 +100,7 @@
|
||||
font-size: 13px; pointer-events: none;
|
||||
}
|
||||
#toast.open { display: block; }
|
||||
#toast.tap { pointer-events: auto; cursor: pointer; border-color: var(--accent); }
|
||||
#toast.err { border-color: var(--err); }
|
||||
#loginMask {
|
||||
position: fixed; inset: 0; z-index: 50; display: none;
|
||||
@@ -155,7 +156,9 @@
|
||||
<button class="btn" data-ctl="shortcut overview">Overview</button>
|
||||
</div>
|
||||
<div class="row">
|
||||
<button id="clipBtn" class="btn">Send Clipboard</button>
|
||||
<button class="btn copyBtn" title="Copy what is selected on the phone to this device">Copy Selection</button>
|
||||
<button id="getClipBtn" class="btn" title="Copy the phone's clipboard to this device">Copy Clipboard</button>
|
||||
<button id="clipBtn" class="btn" title="Put this device's clipboard on the phone's">Send Clipboard</button>
|
||||
<button id="imgBtn" class="btn">Paste Image…</button>
|
||||
<button id="fsBtn" class="btn">Fullscreen</button>
|
||||
<button class="btn" data-ctl="refresh">Redraw</button>
|
||||
@@ -180,11 +183,13 @@
|
||||
<button class="btn kspecial" data-code="103">↑</button>
|
||||
<button class="btn kspecial" data-code="108">↓</button>
|
||||
<button class="btn kspecial" data-code="106">→</button>
|
||||
<button class="btn copyBtn">Copy</button>
|
||||
<button id="pasteBtn" class="btn">Paste</button>
|
||||
<button id="kbHideBtn" class="btn">Done</button>
|
||||
</div>
|
||||
<input id="kbdInput" type="text" value=" " autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">
|
||||
<div id="pasteSink" contenteditable="true" inputmode="none" tabindex="-1" aria-hidden="true"></div>
|
||||
<textarea id="copySource" readonly inputmode="none" tabindex="-1" aria-hidden="true"></textarea>
|
||||
<input id="imgInput" type="file" accept="image/*" hidden>
|
||||
<div id="toast" role="status"></div>
|
||||
|
||||
@@ -236,7 +241,7 @@
|
||||
let lastPtrSent = 0;
|
||||
|
||||
/* While a paste is uploading, typing waits for it, so "Ctrl+V, Enter" arrives in that order. */
|
||||
const HELD_WHILE_PASTING = new Set(['key', 'ktap', 'text', 'release']);
|
||||
const HELD_WHILE_PASTING = new Set(['key', 'ktap', 'text', 'release', 'clipmark', 'copy']);
|
||||
let pastesInFlight = 0;
|
||||
let heldMessages = [];
|
||||
|
||||
@@ -317,6 +322,7 @@
|
||||
function handleText(str) {
|
||||
try {
|
||||
const m = JSON.parse(str);
|
||||
if (m.t === 'clipmark') followCopy(m.id, m.serial);
|
||||
if (m.t === 'status') {
|
||||
infoLine.textContent = `${m.w}×${m.h} · ${m.rot}° · OLED ${m.screen} · Bright ${m.brightness}%`;
|
||||
screenBtn.classList.toggle('active', m.screen === 'off');
|
||||
@@ -618,6 +624,7 @@
|
||||
if (e.target === passInput || e.target === briSlider || e.target === kbdInput) return;
|
||||
if (e.isComposing || e.key === 'Unidentified' || e.key === 'Process' || e.key === 'Dead') return;
|
||||
if (beginKeyPaste(e)) return;
|
||||
if (beginKeyCopy(e)) return;
|
||||
const sym = eventToKeysym(e);
|
||||
if (sym !== null) {
|
||||
e.preventDefault();
|
||||
@@ -665,17 +672,24 @@
|
||||
let keyPasteMissed = 0; /* when the last one was given up on */
|
||||
let toastTimer = null;
|
||||
|
||||
function toast(msg, isErr) {
|
||||
function toast(msg, isErr, onTap) {
|
||||
clearTimeout(toastTimer);
|
||||
toastEl.onclick = onTap || null;
|
||||
toastEl.classList.toggle('tap', !!onTap);
|
||||
toastEl.textContent = msg;
|
||||
toastEl.classList.toggle('err', !!isErr);
|
||||
toastEl.classList.add('open');
|
||||
toastTimer = setTimeout(() => toastEl.classList.remove('open'), isErr ? 5000 : 1800);
|
||||
toastTimer = setTimeout(() => toastEl.classList.remove('open'), onTap ? 15000 : isErr ? 5000 : 1800);
|
||||
}
|
||||
|
||||
function apiHeaders() {
|
||||
const headers = { 'X-Chandra-Remote': '1' };
|
||||
if (token) headers['Authorization'] = 'Bearer ' + token;
|
||||
return headers;
|
||||
}
|
||||
|
||||
async function clipboardApi(method, type, body) {
|
||||
const headers = { 'X-Chandra-Remote': '1' };
|
||||
if (token) headers['Authorization'] = 'Bearer ' + token;
|
||||
const headers = apiHeaders();
|
||||
if (type) headers['Content-Type'] = type;
|
||||
const ctl = new AbortController();
|
||||
const timer = setTimeout(() => ctl.abort(), method === 'GET' ? 4000 : 45000);
|
||||
@@ -733,8 +747,10 @@
|
||||
}
|
||||
|
||||
/* Send it to the phone's clipboard, unless (not `force`) the phone's own is the one to paste. */
|
||||
async function syncClipboard(p, force) {
|
||||
async function syncClipboard(p, force, gen) {
|
||||
if (!force) {
|
||||
/* Copied from the phone since the paste keys were pressed: `p` is what was there before. */
|
||||
if (gen !== copyGen) return false;
|
||||
if (p.sig === clipLastSig) return false;
|
||||
if (clipBase !== null && await clipboardApi('GET') !== clipBase) {
|
||||
clipLastSig = p.sig;
|
||||
@@ -761,12 +777,13 @@
|
||||
/* Paste text or an image. `chord` is {c, m} (evdev code, modifiers), or null for whatever the
|
||||
focused app uses. Typing must be held by the caller; it is released here. */
|
||||
function runPaste(text, image, chord, force) {
|
||||
const gen = copyGen;
|
||||
pasteChain = pasteChain.then(async () => {
|
||||
let p = null;
|
||||
try {
|
||||
if (text || image) {
|
||||
p = await makePayload(text, image);
|
||||
await syncClipboard(p, force);
|
||||
await syncClipboard(p, force, gen);
|
||||
}
|
||||
sendNow(chord ? { t: 'paste', c: chord.c, m: chord.m } : { t: 'paste' });
|
||||
} catch (err) {
|
||||
@@ -788,6 +805,17 @@
|
||||
return null;
|
||||
}
|
||||
|
||||
/* Command is held on the phone as Super: let go of it, without it counting as a Super tap. */
|
||||
function releaseCommand() {
|
||||
for (const [id, sym] of [...pressedKeys]) {
|
||||
if (sym === 0xFFEB || sym === 0xFFEC) {
|
||||
pressedKeys.delete(id);
|
||||
sendJson({ t: 'ktap', c: 29, m: 0 });
|
||||
sendJson({ t: 'key', k: sym, d: 0 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* A paste key press: let the browser do its paste, into the sink, and pick up the event. */
|
||||
function beginKeyPaste(e) {
|
||||
const chord = pasteChord(e);
|
||||
@@ -796,16 +824,7 @@
|
||||
e.preventDefault();
|
||||
return true;
|
||||
}
|
||||
if (IS_MAC) {
|
||||
/* Command is held on the phone as Super: let go of it, without it counting as a Super tap. */
|
||||
for (const [id, sym] of [...pressedKeys]) {
|
||||
if (sym === 0xFFEB || sym === 0xFFEC) {
|
||||
pressedKeys.delete(id);
|
||||
sendJson({ t: 'ktap', c: 29, m: 0 });
|
||||
sendJson({ t: 'key', k: sym, d: 0 });
|
||||
}
|
||||
}
|
||||
}
|
||||
if (IS_MAC) releaseCommand();
|
||||
const back = document.activeElement === kbdInput ? kbdInput : null;
|
||||
holdTyping();
|
||||
pasteSink.textContent = '';
|
||||
@@ -880,6 +899,133 @@
|
||||
return [prompt(question) || '', null];
|
||||
}
|
||||
|
||||
/* ---- Copy: the phone's clipboard (text or image) onto this device's ----
|
||||
The copy keys go to the phone as ever, after a message that answers with its clipboard's
|
||||
serial. When that changes, the app there copied something, and it is put on this device's
|
||||
clipboard too. Ctrl+C that copies nothing (an interrupt in a terminal) changes nothing. */
|
||||
const copySource = document.getElementById('copySource');
|
||||
let copySeq = 0; /* the latest copy; earlier ones give up */
|
||||
let copyAsked = false; /* it was a button, so say when there was nothing to copy */
|
||||
let copyGen = 0; /* counts what was put on this device's clipboard */
|
||||
|
||||
function startCopy(asked) {
|
||||
copyAsked = asked;
|
||||
sendJson({ t: 'clipmark', id: ++copySeq });
|
||||
}
|
||||
|
||||
async function followCopy(id, serial) {
|
||||
const asked = copyAsked;
|
||||
try {
|
||||
for (const wait of [40, 60, 100, 150, 250, 400, 500]) {
|
||||
await new Promise(res => setTimeout(res, wait));
|
||||
if (id !== copySeq) return;
|
||||
if (await clipboardApi('GET') !== serial) {
|
||||
if (id === copySeq) await takeClipboard(id, asked);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (asked && id === copySeq) toast('Nothing was copied on the phone', true);
|
||||
} catch (err) {
|
||||
copyFailed(err);
|
||||
}
|
||||
}
|
||||
|
||||
function copyFailed(err) {
|
||||
toast('Copy failed: ' + (err.name === 'AbortError' ? 'timed out' : err.message), true);
|
||||
}
|
||||
|
||||
/* Put the phone's clipboard on this device's. */
|
||||
async function takeClipboard(id, asked) {
|
||||
const ctl = new AbortController();
|
||||
const timer = setTimeout(() => ctl.abort(), 45000);
|
||||
let text = '', image = null, serial;
|
||||
try {
|
||||
const r = await fetch('/api/clipboard/data', { headers: apiHeaders(), cache: 'no-store', signal: ctl.signal });
|
||||
if (r.status === 404 && !asked) return; /* something else was copied, such as files */
|
||||
if (!r.ok) throw new Error((await r.json()).error || ('HTTP ' + r.status));
|
||||
serial = Number(r.headers.get('X-Clipboard-Serial'));
|
||||
if ((r.headers.get('Content-Type') || '').startsWith('image/')) image = await r.blob();
|
||||
else text = await r.text();
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
if (id !== copySeq) return;
|
||||
const p = await makePayload(text, image);
|
||||
const done = () => {
|
||||
copyGen++;
|
||||
clipBase = serial;
|
||||
clipLastSig = p.sig;
|
||||
toast(image ? 'Image copied to this device' : 'Copied to this device');
|
||||
};
|
||||
try {
|
||||
await writeLocalClipboard(text, image);
|
||||
} catch (err) {
|
||||
if (image && !canWriteImages()) throw new Error('images need the HTTPS address');
|
||||
/* The browser wants a fresh tap for it. */
|
||||
toast('Tap to copy to this device', false, async () => {
|
||||
try {
|
||||
await writeLocalClipboard(text, image);
|
||||
done();
|
||||
} catch (e) {
|
||||
copyFailed(e);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
done();
|
||||
}
|
||||
|
||||
function canWriteImages() {
|
||||
return !!(navigator.clipboard && navigator.clipboard.write && window.ClipboardItem);
|
||||
}
|
||||
|
||||
async function writeLocalClipboard(text, image) {
|
||||
if (image) {
|
||||
await navigator.clipboard.write([new ClipboardItem({ 'image/png': image })]);
|
||||
return;
|
||||
}
|
||||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
return;
|
||||
} catch (_) {}
|
||||
}
|
||||
/* Plain HTTP. */
|
||||
const back = document.activeElement;
|
||||
copySource.value = text;
|
||||
copySource.focus({ preventScroll: true });
|
||||
copySource.select();
|
||||
let ok = false;
|
||||
try { ok = document.execCommand('copy'); } catch (_) {}
|
||||
copySource.value = '';
|
||||
copySource.blur();
|
||||
if (back && back !== document.body) back.focus({ preventScroll: true });
|
||||
if (!ok) throw new Error('the browser refused');
|
||||
}
|
||||
|
||||
/* Copy or cut keys; on a Mac with Command, as the Ctrl keys (evdev code, modifiers) to press. */
|
||||
function copyChord(e) {
|
||||
if (e.altKey) return null;
|
||||
const key = (k, code) => e.key.toLowerCase() === k || (e.code === code && e.key.charCodeAt(0) > 0x7F);
|
||||
const c = key('c', 'KeyC'), x = key('x', 'KeyX') && !e.shiftKey;
|
||||
if (IS_MAC && e.metaKey && !e.ctrlKey && (c || x)) return { c: c ? 46 : 45, m: e.shiftKey ? 5 : 4 };
|
||||
if (e.ctrlKey && !e.metaKey && (c || x || (e.key === 'Insert' && !e.shiftKey))) return {};
|
||||
return null;
|
||||
}
|
||||
|
||||
/* Watch for what the copy keys copy. True when the keys were sent here. */
|
||||
function beginKeyCopy(e) {
|
||||
const chord = copyChord(e);
|
||||
if (!chord) return false;
|
||||
if (chord.c) releaseCommand();
|
||||
if (!e.repeat) {
|
||||
startCopy(false);
|
||||
if (chord.c) sendJson({ t: 'ktap', c: chord.c, m: chord.m });
|
||||
}
|
||||
if (chord.c) e.preventDefault();
|
||||
return !!chord.c;
|
||||
}
|
||||
|
||||
/* ---- Android Gboard sentinel-space keyboard bridge ---- */
|
||||
function setKbOpen(open) {
|
||||
kbOpen = open;
|
||||
@@ -922,6 +1068,7 @@
|
||||
if (!txt) return;
|
||||
if (stickyMods && txt.length === 1 && txt.charCodeAt(0) >= 0x20 && txt.charCodeAt(0) <= 0x7E) {
|
||||
const m = stickyMods;
|
||||
if ((m & 4) && /[cx]/i.test(txt)) startCopy(false);
|
||||
if (m & 4) sendJson({ t: 'key', k: 0xFFE3, d: 1 });
|
||||
if (m & 8) sendJson({ t: 'key', k: 0xFFE9, d: 1 });
|
||||
sendJson({ t: 'key', k: txt.charCodeAt(0), d: 1 });
|
||||
@@ -938,6 +1085,7 @@
|
||||
kbdInput.addEventListener('keydown', (e) => {
|
||||
keyHandled = false;
|
||||
if (beginKeyPaste(e)) return;
|
||||
if (beginKeyCopy(e)) return;
|
||||
const map = {
|
||||
Enter: 28, Backspace: 14, Tab: 15, Escape: 1, Delete: 111,
|
||||
ArrowLeft: 105, ArrowUp: 103, ArrowRight: 106, ArrowDown: 108,
|
||||
@@ -1040,6 +1188,22 @@
|
||||
}
|
||||
});
|
||||
|
||||
document.querySelectorAll('.copyBtn').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
startCopy(true);
|
||||
sendJson({ t: 'copy' });
|
||||
if (kbOpen) kbdInput.focus();
|
||||
});
|
||||
});
|
||||
|
||||
document.getElementById('getClipBtn').addEventListener('click', async () => {
|
||||
try {
|
||||
await takeClipboard(++copySeq, true);
|
||||
} catch (err) {
|
||||
copyFailed(err);
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('imgBtn').addEventListener('click', () => imgInput.click());
|
||||
imgInput.addEventListener('change', () => {
|
||||
const f = imgInput.files[0];
|
||||
|
||||
Reference in new issue
Block a user