Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102// SPDX-License-Identifier: MIT// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>// The serial applet (#tb-serial, its panel #tb-serial-panel): this// browser's Web Serial devices on the guest's /dev/ttyACM0..3 (serial.ts;// Settings, Serial ports, is the same with the help text).// - The item: the plug, dim with no device attached, with the count of// attached ones; red when a port failed (serialUse() says the error), amber// when one hung up. Hidden where the browser has no Web Serial (v.why), and// under sm.// - The panel: the state as the pill (Not running, No devices, N connected,// Error, Hung up); each attached port (#tb-serial-dev-<slot>) with its// label, its /dev/ttyACM and what it is doing (Settings.tsx serialUse()),// and its actions: Terminal (#tb-serial-dev-<slot>-term, an icon: picocom// in the Serial Monitor app; the panel closes) and Disconnect// (#tb-serial-dev-<slot>-disconnect: detach and forget; the panel stays);// with none, a line on what to connect.// - The header's Monitor (the kernel log, where serial.ts logs the ports// attached) and Settings (Serial ports); the footer: Connect…// (#tb-serial-connect, the primary action), disabled until the system runs and while every port// has a device. It opens the browser's chooser from the click itself// (connectSerial()); the panel stays open, so the device shows up in it// (the chooser is no press in the page: BarPopover keeps it). Should the// panel close meanwhile (opened by hover), the device is attached all the// same and the toast says where.import { connectSerial, disconnectSerial, useSerial, type SerialView } from "../../serial.ts";import { launch } from "../../launcher.ts";import { serialTerminal } from "../../apps/tools/serialmon.ts";import type { SerialPortState } from "../../sdk.ts";import type { PopoverProps } from "../ControlCenter.tsx";import { serialUse } from "../Settings.tsx";import { PlugIcon, PromptIcon } from "../icons.tsx";import { AppletButton, AppletPanel, ICON, type AppletItemProps, type AppletTone } from "./Applet.tsx";import { Count, DeviceList, DeviceRow, Empty, RowButton, RowIcon, keepFocus, type DeviceTone } from "./Devices.tsx";
const ID = "serial";
/** A port's state: failed (its error shows), hung up, in use, idle. */function portTone(p: SerialPortState): DeviceTone { if (p.error && !p.guestOpen) return "err"; if (p.hungUp) return "warn"; return p.guestOpen ? "ok" : "idle";}
/** The applet's state: the pill's word and tone, and a one-line summary (the item's tooltip). */function summary(v: SerialView): { word: string; tone: AppletTone; line: string; attached: SerialPortState[] } { const attached = v.ports.filter((p) => p.attached); const tones = attached.map(portTone); const n = attached.length; const devices = `${n} serial device${n === 1 ? "" : "s"}`; if (!v.running) return { word: "Not running", tone: "pending", line: "Serial ports: once the system runs", attached }; if (!n) return { word: "No devices", tone: "off", line: "Serial ports: no device connected", attached }; if (tones.includes("err")) return { word: "Error", tone: "err", line: `Serial ports: ${devices}, one failed`, attached }; if (tones.includes("warn")) return { word: "Hung up", tone: "warn", line: `Serial ports: ${devices}, one hung up`, attached }; return { word: `${n} connected`, tone: "ok", line: `Serial ports: ${attached.map((p) => p.device).join(", ")}`, attached };}
export function SerialItem(props: AppletItemProps) { const v = useSerial(); if (v.why) return null; const s = summary(v); return ( <AppletButton id={ID} title={s.line} wide off={!s.attached.length} tone={s.tone} {...props}> <PlugIcon size={ICON} /> {s.attached.length > 0 && <Count n={s.attached.length} />} </AppletButton> );}
export function SerialPanel(props: PopoverProps) { const v = useSerial(); const s = summary(v); const full = v.running && s.attached.length === v.ports.length; const panelId = `tb-${ID}-panel`; return ( <AppletPanel id={ID} title="Serial ports" icon={<PlugIcon size={16} />} state={s.word} tone={s.tone} monitor="log" settings="serial" width={360} {...props} actions={() => ( <button id="tb-serial-connect" type="button" data-autofocus="" className="btn btn-sm btn-primary my-1 pointer-coarse:h-9" disabled={!v.running || full} title={!v.running ? "Once the system runs" : full ? "Every port has a device; disconnect one first" : "Pick a serial device for the guest"} onClick={() => connectSerial()}>Connect…</button> )}> {(pick) => v.why ? <Empty id="tb-serial-empty">Serial devices are not available: {v.why}.</Empty> : !s.attached.length ? ( <Empty id="tb-serial-empty"> {v.running ? <>No serial device. Connect one (an ESP32, an Arduino) and use it as <code className="font-mono text-fg">/dev/ttyACM0</code> in the guest.</> : "Serial devices attach once the system runs; the ones allowed before come back by themselves."} </Empty> ) : ( <DeviceList id="tb-serial-devs" label="Serial devices"> {s.attached.map((p) => ( <DeviceRow key={p.slot} id={`tb-serial-dev-${p.slot}`} tone={portTone(p)} label={p.info?.label ?? "Serial port"} where={p.device} detail={serialUse(p)}> <RowIcon id={`tb-serial-dev-${p.slot}-term`} label={`Terminal on ${p.device}`} title={`Terminal: picocom on ${p.device} (the Serial Monitor app; it installs picocom the first time)`} onClick={() => pick({ label: "Terminal", run: () => void launch(serialTerminal(p.device)) })}><PromptIcon size={14} /></RowIcon> <RowButton id={`tb-serial-dev-${p.slot}-disconnect`} title="Detach it from the guest (a program using it sees a hangup) and forget it" onClick={() => keepFocus(panelId, () => disconnectSerial(p.slot))}>Disconnect</RowButton> </DeviceRow> ))} </DeviceList> )} </AppletPanel> );}