| 1 | import { VMMenu } from "./VMMenu.tsx"; |
| 2 | import { createEffect, createMemo, createSignal, type JSX, on, onCleanup, Show } from "solid-js"; |
| 3 | import { reason } from "../api.ts"; |
| 4 | import Keyboard from "lucide-solid/icons/keyboard"; |
| 5 | import Clipboard from "lucide-solid/icons/clipboard"; |
| 6 | import Maximize from "lucide-solid/icons/maximize"; |
| 7 | import RefreshCw from "lucide-solid/icons/refresh-cw"; |
| 8 | import Check from "lucide-solid/icons/check"; |
| 9 | import RemoteKeyboard from "@novnc/keyboard"; |
| 10 | import { VM_SETUP, type Domain } from "../types/vms.ts"; |
| 11 | import { StatusLabel } from "./Status.tsx"; |
| 12 | import { toast } from "./Toast.tsx"; |
| 13 | |
| 14 | type WindowSurface = { |
| 15 | id: number; x: number; y: number; width: number; height: number; atlas_x: number; atlas_y: number; |
| 16 | caption_left: number; caption_top: number; caption_right: number; caption_bottom: number; |
| 17 | }; |
| 18 | type WindowFrame = { windows: WindowSurface[]; rtpTimestamp: number }; |
| 19 | |
| 20 | export function VMConsole(props: { name: string; platform: Domain["platform"]; enabled: boolean; active: boolean; setup?: Domain["setup"]; booting: boolean; startedAt: number | null; arch: string; unavailable?: string; clipboard?: (text: string) => Promise<void>; toolbar: JSX.Element; content: (toolbar: HTMLDivElement) => JSX.Element }) { |
| 21 | const enabled = createMemo(() => props.enabled); |
| 22 | const [bootScreen, setBootScreen] = createSignal(false); |
| 23 | const [hasFrame, setHasFrame] = createSignal(false); |
| 24 | const preparing = () => !hasFrame() && !bootScreen() && (!!props.setup && props.setup !== "ready" || props.booting || state() === "Connecting…"); |
| 25 | const [now, setNow] = createSignal(Date.now()); |
| 26 | createEffect(() => { |
| 27 | if (!preparing() || !props.active) return; |
| 28 | setNow(Date.now()); |
| 29 | const timer = window.setInterval(() => setNow(Date.now()), 1000); |
| 30 | onCleanup(() => clearInterval(timer)); |
| 31 | }); |
| 32 | const elapsed = () => { |
| 33 | const seconds = Math.max(0, Math.floor(now() / 1000 - (props.startedAt ?? now() / 1000))); |
| 34 | return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`; |
| 35 | }; |
| 36 | let reconnectTimer: number | undefined; |
| 37 | let retryDelay = 1000; |
| 38 | let attempt = 0; |
| 39 | let video!: HTMLVideoElement; |
| 40 | let canvas!: HTMLCanvasElement; |
| 41 | let cursorImage!: HTMLImageElement; |
| 42 | let cursorInvert!: HTMLImageElement; |
| 43 | let panel!: HTMLDivElement; |
| 44 | let input: RTCDataChannel | undefined; |
| 45 | let movement: RTCDataChannel | undefined; |
| 46 | let pointerSequence = 0; |
| 47 | let windowFrame: WindowFrame | undefined; |
| 48 | let pendingFrames: WindowFrame[] = []; |
| 49 | let frameTimestamp: number | undefined; |
| 50 | let drag: { id: number; startX: number; startY: number; x: number; y: number; dx: number; dy: number; released: number } | undefined; |
| 51 | let disconnect = () => {}; |
| 52 | let width = 0; |
| 53 | let height = 0; |
| 54 | let buttons = 0; |
| 55 | let disposed = false; |
| 56 | let cursorFrames: { image: string; invert?: string; duration: number }[] = []; |
| 57 | let cursorFrame = 0; |
| 58 | let cursorTimer: number | undefined; |
| 59 | let cursorX = 0; |
| 60 | let cursorY = 0; |
| 61 | let pointerX = 0; |
| 62 | let pointerY = 0; |
| 63 | let cursorInside = false; |
| 64 | let cursorVisible = true; |
| 65 | const drawCursor = () => { |
| 66 | const frame = cursorFrames[cursorFrame]; |
| 67 | const overlay = !!frame?.invert && cursorInside && cursorVisible; |
| 68 | cursorImage.style.display = cursorInvert.style.display = overlay ? "block" : "none"; |
| 69 | if (!frame || !cursorVisible) { video.style.cursor = cursorVisible ? "default" : "none"; return; } |
| 70 | video.style.cursor = frame.invert ? (cursorInside ? "none" : "default") : `url(${frame.image}) ${cursorX} ${cursorY}, default`; |
| 71 | if (overlay) { |
| 72 | cursorImage.src = frame.image; |
| 73 | cursorInvert.src = frame.invert!; |
| 74 | cursorImage.style.transform = cursorInvert.style.transform = `translate(${pointerX - cursorX}px, ${pointerY - cursorY}px)`; |
| 75 | } |
| 76 | }; |
| 77 | const animateCursor = () => { |
| 78 | const frame = cursorFrames[cursorFrame]; |
| 79 | if (!frame) return; |
| 80 | cursorTimer = window.setTimeout(() => { |
| 81 | cursorFrame = (cursorFrame + 1) % cursorFrames.length; |
| 82 | drawCursor(); |
| 83 | animateCursor(); |
| 84 | }, frame.duration); |
| 85 | }; |
| 86 | const [state, setState] = createSignal<"Connecting…" | "Connected" | "Disconnected">("Connecting…"); |
| 87 | const connected = () => state() === "Connected"; |
| 88 | const acceptsInput = () => props.active && connected() && !preparing(); |
| 89 | const [problem, setProblem] = createSignal(""); |
| 90 | const [clipboard, setClipboard] = createSignal(""); |
| 91 | const [sendingClipboard, setSendingClipboard] = createSignal(false); |
| 92 | const [showClipboard, setShowClipboard] = createSignal(false); |
| 93 | let clipboardButton!: HTMLButtonElement; |
| 94 | const closeClipboard = () => { |
| 95 | setShowClipboard(false); |
| 96 | clipboardButton.closest("details")?.querySelector("summary")?.focus(); |
| 97 | }; |
| 98 | const [windowsAvailable, setWindowsAvailable] = createSignal(false); |
| 99 | const [separateWindows, setSeparateWindows] = createSignal(false); |
| 100 | const [frameSync, setFrameSync] = createSignal(false); |
| 101 | const [windowCapture, setWindowCapture] = createSignal(false); |
| 102 | const [showStats, setShowStats] = createSignal(false); |
| 103 | const [stats, setStats] = createSignal<{ fps: number; bitrate: number; lost: number; dropped: number; delay: number; rtt: number }>(); |
| 104 | const [capture, setCapture] = createSignal<{ fps: number; capture: number; scan: number; write: number }>(); |
| 105 | const [tools, setTools] = createSignal<HTMLDivElement>(); |
| 106 | const send = (message: object) => { if (input?.readyState === "open") input.send(JSON.stringify(message)); }; |
| 107 | const compose = (freeze = false) => { |
| 108 | const frame = windowFrame; |
| 109 | if (!video.srcObject || video.readyState < 2) return; |
| 110 | canvas.hidden = !video.requestVideoFrameCallback && !freeze; |
| 111 | if (canvas.hidden || !props.active || !width || !height) return; |
| 112 | if (canvas.width !== width || canvas.height !== height) { canvas.width = width; canvas.height = height; } |
| 113 | const context = canvas.getContext("2d", { alpha: false }); |
| 114 | if (!context) return; |
| 115 | canvas.dataset.windows = String(frame?.windows.length ?? 0); |
| 116 | if (!frame?.windows.length) { |
| 117 | context.drawImage(video, 0, 0, width, height); |
| 118 | return; |
| 119 | } |
| 120 | context.fillStyle = "#101014"; |
| 121 | context.fillRect(0, 0, width, height); |
| 122 | for (const window of [...frame.windows].reverse()) { |
| 123 | let x = window.x, y = window.y; |
| 124 | if (drag?.id === window.id) { |
| 125 | const acknowledged = Math.abs(window.x - drag.x - drag.dx) <= 3 && Math.abs(window.y - drag.y - drag.dy) <= 3; |
| 126 | if (drag.released && (acknowledged || performance.now() - drag.released > 500)) drag = undefined; |
| 127 | else { x = drag.x + drag.dx; y = drag.y + drag.dy; } |
| 128 | } |
| 129 | context.drawImage(video, window.atlas_x, window.atlas_y, window.width, window.height, x, y, window.width, window.height); |
| 130 | } |
| 131 | }; |
| 132 | const release = () => { buttons = 0; drag = undefined; compose(); send({ type: "release", sequence: ++pointerSequence }); }; |
| 133 | const connect = async () => { |
| 134 | const current = ++attempt; |
| 135 | disconnect(); |
| 136 | input = undefined; |
| 137 | setProblem(""); |
| 138 | setState("Connecting…"); |
| 139 | try { |
| 140 | if (disposed || !enabled() || current !== attempt) return; |
| 141 | if (!window.RTCPeerConnection) { |
| 142 | setProblem("This browser cannot stream VM screens. Open it in a current browser."); |
| 143 | setState("Disconnected"); |
| 144 | return; |
| 145 | } |
| 146 | const url = new URL(`/api/vms/${encodeURIComponent(props.name)}/console`, location.href); |
| 147 | url.protocol = location.protocol === "https:" ? "wss:" : "ws:"; |
| 148 | const socket = new WebSocket(url.href, ["binary"]); |
| 149 | socket.binaryType = "arraybuffer"; |
| 150 | const keyboard = new RemoteKeyboard(video); |
| 151 | const focus = () => { if (acceptsInput() && input?.readyState === "open") keyboard.grab(); }; |
| 152 | const blur = () => { keyboard.ungrab(); release(); }; |
| 153 | const releaseKeyboard = (event: KeyboardEvent) => { |
| 154 | if (event.key !== "Escape" || !event.ctrlKey || !event.altKey) return; |
| 155 | event.preventDefault(); |
| 156 | event.stopImmediatePropagation(); |
| 157 | tools()?.querySelector<HTMLElement>("summary")?.focus(); |
| 158 | }; |
| 159 | video.addEventListener("keydown", releaseKeyboard, true); |
| 160 | video.addEventListener("focus", focus); |
| 161 | video.addEventListener("blur", blur); |
| 162 | let peer: RTCPeerConnection | undefined; |
| 163 | let frameCallback: number | undefined; |
| 164 | let statsTimer: number | undefined; |
| 165 | let recoveryTimer: number | undefined; |
| 166 | let previousStats: { time: number; bytes: number; frames: number } | undefined; |
| 167 | const frameReady: VideoFrameRequestCallback = (_, metadata) => { |
| 168 | if (current !== attempt || disposed) return; |
| 169 | frameTimestamp = (metadata as VideoFrameCallbackMetadata & { rtpTimestamp?: number }).rtpTimestamp; |
| 170 | setFrameSync(Number.isFinite(frameTimestamp)); |
| 171 | if (!frameSync()) { |
| 172 | frameTimestamp = undefined; windowFrame = undefined; pendingFrames = []; |
| 173 | if (separateWindows()) { setSeparateWindows(false); send({ type: "experiment", enabled: false }); } |
| 174 | } |
| 175 | while (pendingFrames[0] && frameTimestamp !== undefined && ((frameTimestamp - pendingFrames[0].rtpTimestamp) | 0) >= 0) { |
| 176 | windowFrame = pendingFrames.shift(); |
| 177 | } |
| 178 | compose(); |
| 179 | frameCallback = video.requestVideoFrameCallback(frameReady); |
| 180 | }; |
| 181 | const measure = async () => { |
| 182 | if (!peer || current !== attempt || disposed) return; |
| 183 | try { |
| 184 | const report = await peer.getStats(); |
| 185 | if (current !== attempt || disposed || peer.connectionState === "closed") return; |
| 186 | let rtt = 0; |
| 187 | report.forEach((value) => { if (value.type === "candidate-pair" && value.state === "succeeded" && value.nominated) rtt = (value.currentRoundTripTime ?? 0) * 1000; }); |
| 188 | report.forEach((value) => { |
| 189 | if (value.type !== "inbound-rtp" || value.kind !== "video") return; |
| 190 | const elapsed = previousStats ? (value.timestamp - previousStats.time) / 1000 : 0; |
| 191 | setStats({ fps: elapsed ? (value.framesDecoded - previousStats!.frames) / elapsed : 0, |
| 192 | bitrate: elapsed ? (value.bytesReceived - previousStats!.bytes) * 8 / elapsed / 1000000 : 0, |
| 193 | lost: value.packetsLost ?? 0, dropped: value.framesDropped ?? 0, |
| 194 | delay: value.jitterBufferEmittedCount ? value.jitterBufferDelay / value.jitterBufferEmittedCount * 1000 : 0, rtt }); |
| 195 | previousStats = { time: value.timestamp, bytes: value.bytesReceived, frames: value.framesDecoded }; |
| 196 | }); |
| 197 | } catch { |
| 198 | if (peer.connectionState !== "closed") return; |
| 199 | } finally { |
| 200 | if (current === attempt && !disposed && peer.connectionState !== "closed") statsTimer = window.setTimeout(measure, 1000); |
| 201 | } |
| 202 | }; |
| 203 | let pending = ""; |
| 204 | let messages = Promise.resolve(); |
| 205 | const decoder = new TextDecoder(); |
| 206 | const candidates: RTCIceCandidateInit[] = []; |
| 207 | const signal = (message: object) => socket.send(JSON.stringify(message) + "\n"); |
| 208 | const fail = (message: string) => { |
| 209 | if (current !== attempt || disposed) return; |
| 210 | setState("Disconnected"); |
| 211 | disconnect(); |
| 212 | setProblem(preparing() && props.setup !== "failed" ? "" : message); |
| 213 | if (enabled() && props.setup !== "failed") { |
| 214 | reconnectTimer = window.setTimeout(connect, retryDelay); |
| 215 | retryDelay = Math.min(retryDelay * 2, 30000); |
| 216 | } |
| 217 | }; |
| 218 | const timeout = window.setTimeout(() => fail("The screen couldn't connect. Reconnect to try again."), 20000); |
| 219 | const ready = () => { |
| 220 | if (current !== attempt || disposed || video.readyState < 2 || input?.readyState !== "open") return; |
| 221 | clearTimeout(timeout); |
| 222 | retryDelay = 1000; |
| 223 | setHasFrame(true); |
| 224 | setState("Connected"); |
| 225 | if (document.activeElement === video) focus(); |
| 226 | }; |
| 227 | disconnect = () => { |
| 228 | clearTimeout(reconnectTimer); |
| 229 | clearTimeout(timeout); |
| 230 | clearTimeout(recoveryTimer); |
| 231 | blur(); |
| 232 | compose(true); |
| 233 | video.removeEventListener("keydown", releaseKeyboard, true); |
| 234 | video.removeEventListener("focus", focus); |
| 235 | video.removeEventListener("blur", blur); |
| 236 | socket.onclose = null; |
| 237 | socket.close(); |
| 238 | peer?.close(); |
| 239 | clearTimeout(statsTimer); |
| 240 | if (frameCallback !== undefined) video.cancelVideoFrameCallback(frameCallback); |
| 241 | windowFrame = undefined; pendingFrames = []; frameTimestamp = undefined; drag = undefined; |
| 242 | movement = undefined; pointerSequence = 0; |
| 243 | setFrameSync(false); setWindowsAvailable(false); setWindowCapture(false); |
| 244 | video.onplaying = null; |
| 245 | video.srcObject = null; |
| 246 | clearTimeout(cursorTimer); |
| 247 | cursorFrames = []; |
| 248 | cursorFrame = 0; |
| 249 | cursorVisible = true; |
| 250 | cursorImage.style.display = cursorInvert.style.display = "none"; |
| 251 | video.style.cursor = "default"; |
| 252 | }; |
| 253 | socket.onclose = () => fail("The screen disconnected. Check that the VM is running, then reconnect."); |
| 254 | socket.onerror = () => fail("Unable to connect the screen. Reconnect to try again."); |
| 255 | socket.onmessage = (event: MessageEvent<ArrayBuffer>) => { |
| 256 | pending += decoder.decode(event.data, { stream: true }); |
| 257 | if (pending.length > 262144) { fail("The screen connection stopped. Reconnect to try again."); return; } |
| 258 | let end: number; |
| 259 | while ((end = pending.indexOf("\n")) !== -1) { |
| 260 | const line = pending.slice(0, end); |
| 261 | pending = pending.slice(end + 1); |
| 262 | messages = messages.then(async () => { |
| 263 | if (current !== attempt || disposed) return; |
| 264 | const message = JSON.parse(line); |
| 265 | if (message.type === "ready") { |
| 266 | peer = new RTCPeerConnection({ iceServers: message.iceServers }); |
| 267 | const transceiver = peer.addTransceiver("video", { direction: "recvonly" }); |
| 268 | const codecs = RTCRtpReceiver.getCapabilities("video")?.codecs.filter((codec) => codec.mimeType === "video/H264" || codec.mimeType === "video/rtx"); |
| 269 | if (codecs?.length) transceiver.setCodecPreferences(codecs); |
| 270 | input = peer.createDataChannel("input"); |
| 271 | movement = peer.createDataChannel("pointer", { ordered: false, maxRetransmits: 0 }); |
| 272 | keyboard.onkeyevent = (key, _code, down) => { if (acceptsInput()) send({ type: "key", key, down }); }; |
| 273 | input.onopen = () => { |
| 274 | ready(); |
| 275 | if (windowsAvailable()) send({ type: "experiment", enabled: separateWindows() }); |
| 276 | }; |
| 277 | peer.onicecandidate = ({ candidate }) => { if (candidate) signal({ type: "candidate", ...candidate.toJSON() }); }; |
| 278 | peer.onconnectionstatechange = () => { |
| 279 | if (peer?.connectionState === "connected") { |
| 280 | clearTimeout(recoveryTimer); |
| 281 | recoveryTimer = undefined; |
| 282 | setProblem(""); |
| 283 | ready(); |
| 284 | } else if (peer?.connectionState === "failed") { |
| 285 | fail("The screen disconnected. Reconnect to try again."); |
| 286 | } else if (peer?.connectionState === "disconnected" && recoveryTimer === undefined) { |
| 287 | setState("Disconnected"); |
| 288 | setProblem("The screen connection was interrupted. Reconnecting…"); |
| 289 | recoveryTimer = window.setTimeout(() => fail("The screen disconnected. Reconnecting…"), 5000); |
| 290 | } |
| 291 | }; |
| 292 | peer.ontrack = ({ track }) => { |
| 293 | const receiver = transceiver.receiver as RTCRtpReceiver & { jitterBufferTarget?: number }; |
| 294 | if ("jitterBufferTarget" in receiver) receiver.jitterBufferTarget = 0; |
| 295 | video.srcObject = new MediaStream([track]); |
| 296 | if (video.requestVideoFrameCallback) frameCallback = video.requestVideoFrameCallback(frameReady); |
| 297 | void measure(); |
| 298 | void video.play().catch(() => { |
| 299 | if (current === attempt && video.srcObject) fail("The browser couldn't play the screen. Reconnect to try again."); |
| 300 | }); |
| 301 | }; |
| 302 | await peer.setLocalDescription(await peer.createOffer()); |
| 303 | signal({ type: "offer", sdp: peer.localDescription!.sdp }); |
| 304 | } else if (message.type === "answer" && peer) { |
| 305 | await peer.setRemoteDescription({ type: "answer", sdp: message.sdp }); |
| 306 | for (const candidate of candidates.splice(0)) await peer.addIceCandidate(candidate); |
| 307 | } else if (message.type === "candidate") { |
| 308 | if (peer?.remoteDescription) await peer.addIceCandidate(message); |
| 309 | else candidates.push(message); |
| 310 | } else if (message.type === "display") { |
| 311 | width = message.width; |
| 312 | height = message.height; |
| 313 | video.dataset.encoder = message.encoder; |
| 314 | video.dataset.source = message.source ?? "qemu"; |
| 315 | } else if (message.type === "capabilities") { |
| 316 | setWindowsAvailable(message.windows); |
| 317 | if (message.windows) send({ type: "experiment", enabled: separateWindows() }); |
| 318 | } else if (message.type === "window-capture") { |
| 319 | setWindowCapture(message.available); |
| 320 | } else if (message.type === "windows") { |
| 321 | pendingFrames.push(message); |
| 322 | if (pendingFrames.length > 16) pendingFrames.shift(); |
| 323 | if (frameTimestamp !== undefined && ((frameTimestamp - message.rtpTimestamp) | 0) >= 0) { |
| 324 | windowFrame = message; pendingFrames = []; compose(); |
| 325 | } |
| 326 | } else if (message.type === "window-positions") { |
| 327 | if (windowFrame?.windows.length && windowFrame.windows.length === message.windows.length && |
| 328 | windowFrame.windows.every((window) => message.windows.some((next: WindowSurface) => window.id === next.id && window.width === next.width && window.height === next.height))) { |
| 329 | windowFrame = { ...windowFrame, windows: message.windows }; compose(); |
| 330 | } |
| 331 | } else if (message.type === "capture-stats") { |
| 332 | setCapture({ fps: message.frames * 1000 / message.elapsed_ms, |
| 333 | capture: message.frames ? message.capture_us / message.frames / 1000 : 0, |
| 334 | scan: message.frames ? message.scan_us / message.frames / 1000 : 0, |
| 335 | write: message.frames ? message.write_us / message.frames / 1000 : 0 }); |
| 336 | } else if (message.type === "cursor") { |
| 337 | if (message.frame === 0) { |
| 338 | clearTimeout(cursorTimer); |
| 339 | cursorFrames = []; |
| 340 | cursorFrame = 0; |
| 341 | cursorX = message.x; |
| 342 | cursorY = message.y; |
| 343 | if (message.source !== "guest") cursorVisible = !!message.fallback || (!!message.width && !!message.height); |
| 344 | video.dataset.cursorSource = message.source; |
| 345 | } |
| 346 | if (!message.image) { drawCursor(); return; } |
| 347 | for (const url of [message.image, message.invert].filter(Boolean)) { |
| 348 | const image = new Image(); |
| 349 | image.src = url; |
| 350 | await image.decode(); |
| 351 | } |
| 352 | if (current !== attempt || disposed) return; |
| 353 | cursorFrames.push({ image: message.image, invert: message.invert, duration: message.duration }); |
| 354 | if (message.frame === 0) drawCursor(); |
| 355 | if (cursorFrames.length === message.frames && message.frames > 1) animateCursor(); |
| 356 | } else if (message.type === "cursor-position") { |
| 357 | cursorVisible = message.visible; |
| 358 | drawCursor(); |
| 359 | } else if (message.type === "clipboard") setClipboard(message.text); |
| 360 | else if (message.type === "error") { |
| 361 | if (message.fatal) fail(message.message); |
| 362 | else setProblem(message.message); |
| 363 | } |
| 364 | }).catch((failure) => { |
| 365 | console.error("VM screen connection", failure); |
| 366 | fail("The browser couldn't connect the screen. Reconnect to try again."); |
| 367 | }); |
| 368 | } |
| 369 | }; |
| 370 | video.onplaying = () => { |
| 371 | if (current !== attempt || disposed) return; |
| 372 | ready(); |
| 373 | compose(); |
| 374 | }; |
| 375 | } catch (failure) { |
| 376 | console.error("VM screen connection", failure); |
| 377 | disconnect(); |
| 378 | setProblem("The browser couldn't connect the screen. Reconnect to try again."); |
| 379 | setState("Disconnected"); |
| 380 | } |
| 381 | }; |
| 382 | createEffect(on(enabled, value => { |
| 383 | if (value) void connect(); |
| 384 | else { |
| 385 | attempt++; |
| 386 | disconnect(); |
| 387 | input = undefined; |
| 388 | setState("Disconnected"); |
| 389 | setProblem(""); |
| 390 | setHasFrame(false); |
| 391 | setStats(undefined); setCapture(undefined); |
| 392 | canvas.hidden = true; |
| 393 | } |
| 394 | })); |
| 395 | createEffect(() => { |
| 396 | if (acceptsInput()) return; |
| 397 | release(); |
| 398 | if (document.activeElement === video && (!props.active || !enabled() || preparing())) { |
| 399 | video.blur(); |
| 400 | if (props.active) tools()?.querySelector<HTMLElement>("summary")?.focus(); |
| 401 | } |
| 402 | }); |
| 403 | createEffect(on(createMemo(() => props.setup), setup => { |
| 404 | if (setup === "ready" && enabled() && state() === "Disconnected") void connect(); |
| 405 | }, { defer: true })); |
| 406 | let playbackFrame: number | undefined; |
| 407 | const resumeVideo = () => { |
| 408 | if (playbackFrame !== undefined) cancelAnimationFrame(playbackFrame); |
| 409 | const current = attempt; |
| 410 | playbackFrame = requestAnimationFrame(() => { |
| 411 | if (disposed || current !== attempt || !props.active || !enabled() || document.hidden || !video.srcObject) return; |
| 412 | compose(); |
| 413 | void video.play().catch(() => { |
| 414 | if (!disposed && current === attempt && props.active && enabled() && !document.hidden && video.srcObject) { |
| 415 | setProblem("The browser couldn't play the screen. Reconnect to try again."); |
| 416 | } |
| 417 | }); |
| 418 | }); |
| 419 | }; |
| 420 | createEffect(on(() => [props.active, enabled(), preparing()], resumeVideo)); |
| 421 | createEffect(on(createMemo(() => props.startedAt), () => { |
| 422 | setBootScreen(false); |
| 423 | setHasFrame(false); |
| 424 | setStats(undefined); setCapture(undefined); |
| 425 | canvas.hidden = true; |
| 426 | })); |
| 427 | document.addEventListener("visibilitychange", resumeVideo); |
| 428 | onCleanup(() => { |
| 429 | disposed = true; |
| 430 | if (playbackFrame !== undefined) cancelAnimationFrame(playbackFrame); |
| 431 | document.removeEventListener("visibilitychange", resumeVideo); |
| 432 | disconnect(); |
| 433 | }); |
| 434 | const pointer = (event: PointerEvent | WheelEvent, mask = buttons) => { |
| 435 | if (!connected() || preparing() || !width || !height) return; |
| 436 | const rect = video.getBoundingClientRect(); |
| 437 | const scale = Math.min(rect.width / width, rect.height / height); |
| 438 | const x = Math.floor((event.clientX - rect.left - (rect.width - width * scale) / 2) / scale); |
| 439 | const y = Math.floor((event.clientY - rect.top - (rect.height - height * scale) / 2) / scale); |
| 440 | pointerX = event.clientX; |
| 441 | pointerY = event.clientY; |
| 442 | cursorInside = x >= 0 && y >= 0 && x < width && y < height; |
| 443 | drawCursor(); |
| 444 | if ((event.type === "pointerdown" || event.type === "wheel") && (x < 0 || y < 0 || x >= width || y >= height)) return false; |
| 445 | if (event.type === "pointerdown" && mask === 1) { |
| 446 | const window = windowFrame?.windows.find((window) => x >= window.x && y >= window.y && x < window.x + window.width && y < window.y + window.height); |
| 447 | if (window && x >= window.caption_left && x < window.caption_right && y >= window.caption_top && y < window.caption_bottom) { |
| 448 | drag = { id: window.id, startX: x, startY: y, x: window.x, y: window.y, dx: 0, dy: 0, released: 0 }; |
| 449 | } |
| 450 | } |
| 451 | if (drag && event.type === "pointermove" && buttons === 1) { drag.dx = x - drag.startX; drag.dy = y - drag.startY; compose(); } |
| 452 | if (drag && event.type === "pointerup" && !mask) { drag.released = performance.now(); window.setTimeout(compose, 510); } |
| 453 | const message = JSON.stringify({ type: "pointer", x, y, buttons: mask, sequence: ++pointerSequence }); |
| 454 | if (event.type === "pointermove" && !mask && movement?.readyState === "open") { |
| 455 | if (movement.bufferedAmount < 16384) movement.send(message); |
| 456 | } else if (input?.readyState === "open" && (event.type !== "pointermove" || input.bufferedAmount < 16384)) input.send(message); |
| 457 | return true; |
| 458 | }; |
| 459 | const ctrlAltDel = () => { |
| 460 | for (const key of [0xffe3, 0xffe9, 0xffff]) send({ type: "key", key, down: true }); |
| 461 | for (const key of [0xffff, 0xffe9, 0xffe3]) send({ type: "key", key, down: false }); |
| 462 | }; |
| 463 | return <div class="vm-console" ref={panel}> |
| 464 | <div class="vm-console-toolbar" ref={setTools}> |
| 465 | {props.toolbar} |
| 466 | <Show when={props.active}> |
| 467 | <VMMenu label="Input"> |
| 468 | <Show when={props.platform !== "macos"}><button disabled={!connected()} onClick={ctrlAltDel}><Keyboard size={14} aria-hidden="true" />Ctrl+Alt+Del</button></Show> |
| 469 | <button ref={clipboardButton} disabled={!connected() && !showClipboard()} aria-expanded={showClipboard()} aria-controls={`clipboard-${props.name}`} onClick={() => showClipboard() ? closeClipboard() : setShowClipboard(true)}><Clipboard size={14} aria-hidden="true" />Clipboard</button> |
| 470 | </VMMenu> |
| 471 | <VMMenu label="View"> |
| 472 | <button disabled={!connected() || !windowsAvailable() || !frameSync()} aria-pressed={separateWindows()} |
| 473 | onClick={() => { const enabled = !separateWindows(); setSeparateWindows(enabled); send({ type: "experiment", enabled }); }}> |
| 474 | <Check size={14} aria-hidden="true" style={{ visibility: separateWindows() ? "visible" : "hidden" }} /> |
| 475 | Separate windows (experimental) |
| 476 | </button> |
| 477 | <button aria-pressed={showStats()} onClick={() => setShowStats(!showStats())}><Check size={14} aria-hidden="true" style={{ visibility: showStats() ? "visible" : "hidden" }} />Stream statistics</button> |
| 478 | <button onClick={() => panel.requestFullscreen().catch((failure) => setProblem(reason(failure)))}><Maximize size={14} aria-hidden="true" />Fullscreen</button> |
| 479 | <button disabled={!props.enabled || state() === "Connecting…"} onClick={() => { void connect(); if (hasFrame()) video.focus(); }}><RefreshCw size={14} aria-hidden="true" />Reconnect</button> |
| 480 | </VMMenu> |
| 481 | <span class="spacer" /> |
| 482 | <Show when={hasFrame() && !preparing()}><span class="vm-console-status" style={{ visibility: connected() ? "visible" : "hidden" }} id={`keyboard-release-${props.name}`}>Ctrl+Alt+Esc releases keyboard</span></Show> |
| 483 | <span class="vm-console-status" role="status">{preparing() ? "" : props.enabled ? hasFrame() && state() === "Connecting…" ? "Reconnecting…" : state() : props.unavailable ? "Unavailable" : "Off"}</span> |
| 484 | </Show> |
| 485 | </div> |
| 486 | <Show when={props.active && showStats()}> |
| 487 | <div class="vm-stream-stats" style={{ visibility: connected() ? "visible" : "hidden" }}> |
| 488 | <Show when={stats()}>{(value) => <><span>{value().fps.toFixed(1)} FPS</span><span>{value().bitrate.toFixed(2)} Mbps</span><span>{value().lost} packets lost</span><span>{value().dropped} frames dropped</span><span>{value().rtt.toFixed(0)} ms round trip</span><span>{value().delay.toFixed(0)} ms buffer</span></>}</Show> |
| 489 | <Show when={capture()}>{(value) => <><span>Guest {value().fps.toFixed(1)} FPS</span><span>Desktop capture {value().capture.toFixed(1)} ms</span><span>Compare {value().scan.toFixed(1)} ms</span><span>Stream work {value().write.toFixed(1)} ms</span></>}</Show> |
| 490 | <Show when={separateWindows()}><span>{windowCapture() ? "Separate windows" : "Desktop fallback"}</span></Show> |
| 491 | </div> |
| 492 | </Show> |
| 493 | <Show when={props.active && showClipboard()}> |
| 494 | <div class="vm-clipboard" id={`clipboard-${props.name}`} onKeyDown={(event) => { |
| 495 | if (event.key !== "Escape") return; |
| 496 | event.preventDefault(); |
| 497 | event.stopPropagation(); |
| 498 | closeClipboard(); |
| 499 | }}> |
| 500 | <label class="field">clipboard<textarea ref={(field) => queueMicrotask(() => field.focus())} class="search" rows="3" value={clipboard()} onInput={(event) => setClipboard(event.currentTarget.value)} /></label> |
| 501 | <span class="hint">{props.clipboard ? "Send text, then paste in the VM with Command+V." : "Clipboard sharing requires support in the guest"}</span> |
| 502 | <div class="row"> |
| 503 | <button class="button small" disabled={!connected() || sendingClipboard()} aria-busy={sendingClipboard()} onClick={async () => { |
| 504 | if (!props.clipboard) { send({ type: "clipboard", text: clipboard() }); return; } |
| 505 | setSendingClipboard(true); |
| 506 | try { await props.clipboard(clipboard()); setProblem(""); toast("Text sent to the VM clipboard."); } |
| 507 | catch (error) { setProblem(reason(error)); } |
| 508 | finally { setSendingClipboard(false); } |
| 509 | }}>send text</button> |
| 510 | <button class="button small" onClick={() => navigator.clipboard.writeText(clipboard()).catch((failure) => setProblem(reason(failure)))}>copy text</button> |
| 511 | <button class="button small" onClick={closeClipboard}>close</button> |
| 512 | </div> |
| 513 | </div> |
| 514 | </Show> |
| 515 | <div class="vm-console-screen" hidden={!props.active || !props.enabled}> |
| 516 | <Show when={!preparing() && problem()}><p class="error vm-console-error" role="alert">{problem()}</p></Show> |
| 517 | <Show when={preparing()}> |
| 518 | <div class="empty vm-setup"> |
| 519 | <div role={props.setup === "failed" ? "alert" : "status"}> |
| 520 | <StatusLabel health={props.setup === "failed" ? "down" : "starting"}>{props.setup && props.setup !== "ready" ? VM_SETUP[props.setup] : props.booting ? "Starting your VM…" : "Connecting to screen…"}</StatusLabel> |
| 521 | <p>{props.setup === "failed" ? "Restart this VM from the Machine menu to retry." : !props.booting ? "Waiting for the screen connection." : props.arch === "aarch64" ? "This preset can take several minutes to boot." : "This preset takes about 1 minute to boot."}</p> |
| 522 | </div> |
| 523 | <Show when={props.startedAt !== null}><span class="vm-setup-elapsed" role="timer" aria-live="off">VM uptime {elapsed()}</span></Show> |
| 524 | <button class="button small" onClick={() => setBootScreen(true)}>View boot screen</button> |
| 525 | </div> |
| 526 | </Show> |
| 527 | <video ref={video} autoplay muted playsinline tabindex={preparing() ? -1 : 0} aria-hidden={preparing()} aria-disabled={!acceptsInput()} aria-label="VM screen" aria-describedby={`keyboard-release-${props.name}`} |
| 528 | onContextMenu={(event) => event.preventDefault()} |
| 529 | onPointerMove={pointer} |
| 530 | onPointerLeave={() => { cursorInside = false; drawCursor(); }} |
| 531 | onPointerDown={(event) => { |
| 532 | event.preventDefault(); |
| 533 | const mask = buttons | (event.button === 0 ? 1 : event.button === 1 ? 2 : event.button === 2 ? 4 : 0); |
| 534 | if (!pointer(event)) return; |
| 535 | pointer(event, mask); |
| 536 | buttons = mask; |
| 537 | video.focus(); video.setPointerCapture(event.pointerId); |
| 538 | }} |
| 539 | onPointerUp={(event) => { |
| 540 | if (buttons) pointer(event); |
| 541 | buttons &= ~(event.button === 0 ? 1 : event.button === 1 ? 2 : event.button === 2 ? 4 : 0); |
| 542 | pointer(event); |
| 543 | if (!event.buttons && video.hasPointerCapture(event.pointerId)) video.releasePointerCapture(event.pointerId); |
| 544 | }} |
| 545 | onPointerCancel={release} |
| 546 | onWheel={(event) => { |
| 547 | event.preventDefault(); |
| 548 | const mask = event.deltaY ? (event.deltaY < 0 ? 8 : 16) : event.deltaX < 0 ? 32 : 64; |
| 549 | pointer(event, buttons | mask); pointer(event); |
| 550 | }} /> |
| 551 | <canvas ref={canvas} class="vm-screen-canvas" hidden aria-hidden="true" /> |
| 552 | <img ref={cursorImage} class="vm-cursor" alt="" aria-hidden="true" /> |
| 553 | <img ref={cursorInvert} class="vm-cursor vm-cursor-invert" alt="" aria-hidden="true" /> |
| 554 | </div> |
| 555 | <Show when={tools()}>{(toolbar) => props.content(toolbar())}</Show> |
| 556 | <Show when={props.active && !props.enabled}><div class="empty"><p>{props.unavailable ?? "The VM is off. Start it from the Machine menu."}</p></div></Show> |
| 557 | </div>; |
| 558 | } |