A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
import Foundation
import UIKit
import QuartzCore
import Combine
import SwiftUI
import CoreText
import CoreGraphics
/// The shell's side of the thread contract. `Notify` calls arrive on the
/// core thread; nothing here blocks them, and nothing here calls back into
/// the core from inside them. The display link is the only place `render`
/// is called; the core only ever *asks* for a frame.
///
/// One model per connection: the screen that shows a host makes one and
/// shuts it down when it goes.
final class TerminalModel: ObservableObject, @unchecked Sendable {
// The App's views, refreshed when the core says something changed.
@Published var tabs: TabsView?
@Published var threads: ThreadsView?
/// Whole tabs, as they are split, by the pane a card shows, for the
/// overview's cards, and their pictures, drawn off the main thread
/// (`paintScreen`): asked for while it is up, let go when it closes
/// (`releaseScreens`).
@Published private(set) var screens: [Int: TabPreview] = [:]
@Published private(set) var pictures: [Int: CGImage] = [:]
/// Moves with each screen that comes in: its picture is drawn again.
@Published private(set) var screenSerials: [Int: Int] = [:]
private var screensWanted = false
/// When each pane's screen was asked for, until it comes: one question
/// a pane at a time, asked again only if it goes long unanswered.
private var screenAsks: [Int: ContinuousClock.Instant] = [:]
/// Moves when the screens are let go: a picture drawn before is not kept.
private var paintTurn = 0
private let painter = DispatchQueue(label: "thinkterm.screen-pictures", qos: .userInitiated)
@Published var sidebar: SidebarView?
/// Moves when the tab icon cards change (`Core.tabIconsRevision`): the
/// cards on show are drawn again.
@Published private(set) var iconsRevision: UInt64 = 0
/// A menu action that ends something (`delete-thread:…`), awaiting a yes.
@Published var confirming: String?
/// Counts the sidebar's "+": the screen asks for the folder.
@Published private(set) var nameAsks = 0
@Published var tree: TreeView?
@Published var navs: [NavView] = []
@Published var status: StatusView?
@Published var title = ""
@Published var connection = ""
/// The key panel is up in the keyboard's place.
#if DEBUG
@Published var panelOpen = ProcessInfo.processInfo.arguments.contains("--keypanel")
#else
@Published var panelOpen = false
#endif
/// A thread this phone was on last time it was in this host, put
/// back once the threads are listed after a connect.
private var restoredThread = false
/// The connection whose views were out when this one was asked for:
/// its listing is not this connection's first.
private var connectionBefore: UInt64 = 0
private var lastThreadKey: String? { host.map { "thread.last." + $0.id.uuidString } }
var isConnected: Bool {
!connection.isEmpty && !connection.hasPrefix("disconnected") && !connection.hasPrefix("failed") && !connection.hasPrefix("idle")
}
@Published var attached = false
@Published var composing: String?
@Published var ctrlSticky = false
@Published var altSticky = false
@Published var logText = ""
@Published var stats = ""
@Published var inset: CGFloat = 0
/// A selection was just copied; the status line says so for a moment.
@Published var copied = false
/// The App's remark for the status line: a passing one goes after a
/// few seconds, a sticky one (the connection is down) stays until the
/// App takes it back.
@Published private(set) var toastText: String?
/// The App says its connection is down and it is redialing.
@Published private(set) var reconnecting = false
private var toastTimer: Timer?
/// The terminal's background, as the App paints it: the chrome around
/// the terminal takes the same colour.
@Published var background = Color(white: 0.11) {
didSet { ink = Self.ink(over: background) }
}
/// The chrome's ink over `background`: white over a dark scheme, near
/// black over a light one.
@Published private(set) var ink = Color.white
private static func ink(over color: Color) -> Color {
var (r, g, b, a): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
guard UIColor(color).getRed(&r, green: &g, blue: &b, alpha: &a) else { return .white }
return 0.2126 * r + 0.7152 * g + 0.0722 * b > 0.6 ? Color(white: 0.1) : .white
}
/// The App's font size in points, from its layout view.
@Published private(set) var fontPt: Double = 11
/// The focused pane's place in its scrollback: rows above the bottom,
/// and rows there are; changes for a moment show the scrollbar.
@Published private(set) var scroll: (Double, Int) = (0, 0)
@Published private(set) var scrollShown = false
private var scrollTimer: Timer?
/// The background task that holds the connection after the app leaves
/// the screen, for as long as iOS allows.
private var backgroundTask: UIBackgroundTaskIdentifier = .invalid
@Published var animating = false
/// The log view is open.
var logShown = false
/// The log view is open or developer mode is on: the stats are worth
/// refreshing.
var wantsStats: Bool { logShown || settings.devMode }
/// The views last read from the core.
private var lastViews: ViewSet?
/// The focused pane's rows for the text input, kept current by the
/// core while they are wanted (the keyboard is up, or a selection).
private(set) var screenWanted = false
/// A swipe from the screen's left edge pulls the sidebar out (a narrow
/// screen, the drawer shut): a flick that starts there is not a tab's.
var edgePullsDrawer = false
private(set) var screenJSON = "null"
private var screenRevision: UInt64 = 0
/// This connection's terminal is on screen, and the app in front: the
/// core is told, so a session behind the screen takes nothing.
private var onScreen = false
private var inFront = true
/// The preferences, followed as they change.
let settings = AppSettings.shared
var smoothScroll: Bool { settings.smoothScroll }
private var subscriptions: [AnyCancellable] = []
/// The App's cell height in points, from its layout view.
private(set) var cellHeight: Double = 20
let core: Core
/// The host this screen shows; edited in place from the failure card.
@Published var host: Host?
struct HostKeyChallenge: Identifiable {
let id: String
let hostname: String
let port: Int
let hostID: UUID?
let fingerprint: String
}
@Published var pendingHostKey: HostKeyChallenge?
private var activeRequestID: String?
private var requestedEndpoint: (hostname: String, port: Int, hostID: UUID?)?
weak var store: HostStore?
weak var inputView: TerminalInputView?
var cursorRect = CGRect(x: 8, y: 8, width: 2, height: 20)
private let frameNeeded = AtomicFlag()
private var displayLink: CADisplayLink?
private var statsTimer: Timer?
private var logLines: [String] = []
private var hostKeyToRemember: String?
weak var layer: CAMetalLayer?
private(set) var generation: UInt64 = 0
private var wantsSurface = false
private var lastSize: (Int, Int, Double) = (0, 0, 0)
#if DEBUG
// The probe's ssh setup, for the automated flows: a throwaway key and
// a wrapper that points the remote proxy at an isolated HOME.
private var probeKeyPath = "/tmp/ttp-ssh/userkey"
private var probeRemoteCommand = "/tmp/ttp-ssh/thinkterm-remote cli --prefer-mux proxy"
private var probeUser = probeUserName()
// The acceptance flow's requests answered, and its stall gauge.
private var acceptTimer: Timer?
private var acceptAnswered = 0
private var measuringTicks = false
private var lastTickAt: CFTimeInterval = 0
private var longestTickGap: CFTimeInterval = 0
#endif
init(host: Host?, store: HostStore?) {
self.host = host
self.store = store
let sink = NotifySink()
core = Core(notify: sink)
sink.model = self
let args = ProcessInfo.processInfo.arguments
func arg(_ name: String) -> String? {
guard let i = args.firstIndex(of: name), i + 1 < args.count else { return nil }
return args[i + 1]
}
#if DEBUG
if let u = arg("--user") { probeUser = u }
if let k = arg("--key") { probeKeyPath = k }
if let c = arg("--remote-command") { probeRemoteCommand = c }
#endif
let link = CADisplayLink(target: self, selector: #selector(tick))
link.add(to: .main, forMode: .common)
displayLink = link
// Every published change redraws the screen (and closes an open
// menu): the stats tick only while the log shows them.
statsTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] _ in
guard let self, self.wantsStats else { return }
let stats = self.core.stats()
if stats != self.stats { self.stats = stats }
}
#if DEBUG
if let name = arg("--scheme") { settings.schemeName = name }
#endif
applyScrollMode()
applyScheme()
settings.$schemeName.dropFirst().sink { [weak self] _ in self?.applyScheme() }.store(in: &subscriptions)
settings.$smoothScroll.dropFirst().sink { [weak self] _ in self?.applyScrollMode() }.store(in: &subscriptions)
applyTerminalPrefs()
settings.$cursorStyle.dropFirst().sink { [weak self] _ in self?.applyTerminalPrefs() }.store(in: &subscriptions)
settings.$cursorBlink.dropFirst().sink { [weak self] _ in self?.applyTerminalPrefs() }.store(in: &subscriptions)
settings.$contrast.dropFirst().sink { [weak self] _ in self?.applyTerminalPrefs() }.store(in: &subscriptions)
settings.$resizeMode.dropFirst().sink { [weak self] _ in self?.applyTerminalPrefs() }.store(in: &subscriptions)
settings.$autoReconnect.dropFirst().sink { [weak self] _ in self?.applyTerminalPrefs() }.store(in: &subscriptions)
settings.$paneBars.dropFirst().sink { [weak self] _ in self?.applyTerminalPrefs() }.store(in: &subscriptions)
settings.$language.dropFirst().sink { [weak self] _ in self?.applyTerminalPrefs() }.store(in: &subscriptions)
// A face is shaped at connect time: the connection is made again.
settings.$fontFamily.dropFirst().removeDuplicates().sink { [weak self] _ in
guard let self, self.connection.hasPrefix("pane ") else { return }
self.disconnect()
self.connect()
}.store(in: &subscriptions)
// The size, though, every pane takes live, from either settings page.
applyFontSize(settings.fontSize)
settings.$fontSize.dropFirst().removeDuplicates().sink { [weak self] pt in self?.applyFontSize(pt) }.store(in: &subscriptions)
#if DEBUG
if args.contains("--autotest") { scheduleAutotest() }
if args.contains("--autoconnect") { scheduleAutoconnect() }
if args.contains("--imetest") { scheduleImeTest() }
if args.contains("--seltest") { scheduleSelectionTest() }
if args.contains("--treetest") { scheduleTreeTest() }
if args.contains("--uitest") { scheduleUiTest() }
if args.contains("--accepttest") { scheduleAcceptTest() }
#endif
}
/// The sidebar's "+": the screen asks for the folder.
func askName() { nameAsks += 1 }
/// The screen went away but the connection stays: the frame loop
/// rests until the screen is back. The surface goes with the view.
func leaveScreen() {
// Its text input goes with it: nothing reads the rows.
setScreenWanted(false)
displayLink?.isPaused = true
onScreen = false
updateShown()
}
func enterScreen() {
displayLink?.isPaused = false
onScreen = true
updateShown()
}
private func updateShown() { core.setShown(shown: onScreen && inFront) }
func shutdown() {
clearHostKeyRequest()
displayLink?.invalidate()
statsTimer?.invalidate()
detach()
core.disconnect()
core.shutdown()
}
// MARK: connection
/// The chosen face first, the symbols fallback after it.
var fontPaths: [String] {
[terminalFace, "SymbolsNerdFontMono-Regular"].compactMap {
Bundle.main.path(forResource: $0, ofType: "ttf")
}
}
/// This install's name to the servers, made once: the tab this phone
/// holds stays its own across launches.
static let deviceId: String = {
if let id = UserDefaults.standard.string(forKey: "client.id") { return id }
let id = UUID().uuidString
UserDefaults.standard.set(id, forKey: "client.id")
return id
}()
/// Connect to the model's host with the secret from the Keychain.
func connect() {
restoredThread = false
// A new connection's views are all new, even ones that read the
// same: the first listing puts the phone back on its thread. The
// core may still have the last connection's out until it gets here.
lastViews = nil
connectionBefore = core.viewsConnection()
guard let host else {
// Only the automated flows open a screen with no host.
#if DEBUG
connectProbe()
#endif
return
}
let account = host.id.uuidString
let secret = Keychain.load(account: account) ?? ""
let passphrase = Keychain.load(account: account + ".passphrase")
log("shell: \(host.auth == .password ? "password" : "key text") \(secret.count) chars from the Keychain" + (passphrase == nil ? "" : ", with a passphrase"))
connect(
hostname: host.hostname, port: host.port, user: host.user,
authKind: host.auth == .password ? "password" : "key", secret: secret,
passphrase: passphrase, knownHost: host.knownHost, remoteCommand: host.remoteCommand
)
}
#if DEBUG
/// The probe's connection: the private sshd on this Mac.
func connectProbe() {
connect(
hostname: "127.0.0.1", port: 2299, user: probeUser, authKind: "key-path",
secret: probeKeyPath, passphrase: nil, knownHost: hostKeyToRemember, remoteCommand: probeRemoteCommand
)
}
#endif
private func connect(hostname: String, port: Int, user: String, authKind: String, secret: String, passphrase: String?, knownHost: String?, remoteCommand: String) {
clearHostKeyRequest()
let requestID = UUID().uuidString
activeRequestID = requestID
requestedEndpoint = (hostname, port, host?.id)
let paths = fontPaths
guard paths.count == 2 else {
log("shell: fonts missing from the bundle (\(paths))")
return
}
core.connect(
requestId: requestID,
host: hostname, port: UInt16(clamping: port), user: user,
authKind: authKind, secret: secret, passphrase: passphrase, knownHost: knownHost,
remoteCommand: remoteCommand, deviceId: Self.deviceId, keepaliveSecs: UInt32(max(settings.keepAliveSeconds, 0)),
fontPaths: paths, sizePt: settings.fontSize,
painter: CoreTextPainter()
)
}
private func clearHostKeyRequest() {
activeRequestID = nil
requestedEndpoint = nil
pendingHostKey = nil
}
func disconnect() { clearHostKeyRequest(); core.disconnect() }
func approveHostKey(_ challenge: HostKeyChallenge) {
guard activeRequestID == challenge.id,
requestedEndpoint?.hostname == challenge.hostname,
requestedEndpoint?.port == challenge.port,
host?.id == challenge.hostID else { return }
if var host = host {
guard host.hostname == challenge.hostname, host.port == challenge.port,
host.knownHost == nil else { return }
host.knownHost = challenge.fingerprint
store?.rememberHostKey(challenge.fingerprint, for: host.id)
self.host = host
} else {
hostKeyToRemember = challenge.fingerprint
}
connect()
}
// MARK: input
func key(_ name: String, ctrl: Bool = false, alt: Bool = false, shift: Bool = false) {
// A sticky modifier is spent by the next key whether or not that
// key brought the modifier itself, so it never lingers past it.
let stickyCtrl = takeSticky(\.ctrlSticky)
let stickyAlt = takeSticky(\.altSticky)
core.key(name: name, ctrl: ctrl || stickyCtrl, alt: alt || stickyAlt, shift: shift)
}
/// Text from the keyboard. A sticky Ctrl or Alt turns a single
/// character into a chord.
func text(_ text: String) {
if (ctrlSticky || altSticky), text.count == 1 {
key(text)
return
}
core.text(text: text)
}
func pasteFromClipboard() {
if let text = UIPasteboard.general.string, !text.isEmpty {
core.paste(text: text)
}
}
private func takeSticky(_ path: ReferenceWritableKeyPath<TerminalModel, Bool>) -> Bool {
let on = self[keyPath: path]
if on { self[keyPath: path] = false }
return on
}
func pointer(_ kind: String, at point: CGPoint) {
core.pointer(kind: kind, x: point.x, y: point.y)
}
func wheel(at point: CGPoint, lines: Double) {
core.wheel(x: point.x, y: point.y, lines: lines)
}
func wheelPx(at point: CGPoint, px: Double) {
core.wheelPx(x: point.x, y: point.y, px: px)
}
private func showToast(_ toast: Toast?) {
toastTimer?.invalidate()
let sticky = toast?.sticky ?? false
if reconnecting != sticky { reconnecting = sticky }
guard let toast, !toast.text.isEmpty else {
if toastText != nil { toastText = nil }
return
}
toastText = toast.text
if !sticky {
toastTimer = Timer.scheduledTimer(withTimeInterval: 4, repeats: false) { [weak self] _ in
guard let self, self.toastText == toast.text else { return }
self.toastText = nil
}
}
}
private func applyScheme() {
let schemeName = settings.schemeName
if schemeName == Schemes.followDesktop || Schemes.json(named: schemeName) == nil {
core.setSetting(key: "terminal-scheme", value: "\"desktop\"")
core.setPalette(scheme: nil)
} else {
core.setSetting(key: "terminal-scheme", value: "\"" + schemeName.replacingOccurrences(of: "\"", with: "\\\"") + "\"")
core.setPalette(scheme: Schemes.json(named: schemeName))
}
}
fileprivate func corePublished(_ key: String, _ value: String) {
guard key == "bg", let color = Color(hex: value) else { return }
DispatchQueue.main.async {
if self.background != color { self.background = color }
}
}
private func applyScrollMode() {
core.setSetting(key: "scroll-mode", value: smoothScroll ? "\"smooth\"" : "\"stepped\"")
}
/// The preferences the shared App honours, as its JSON values.
private func applyTerminalPrefs() {
let style = ["auto", "block", "bar", "underline"].contains(settings.cursorStyle) ? settings.cursorStyle : "auto"
core.setSetting(key: "cursor-style", value: "\"\(style)\"")
core.setSetting(key: "cursor-blink", value: settings.cursorBlink ? "true" : "false")
let contrast: Double = ["3": 3, "45": 4.5, "7": 7][settings.contrast] ?? 0
core.setSetting(key: "min-contrast", value: String(contrast))
core.setSetting(key: "resize-mode", value: settings.resizeMode == "release" ? "\"release\"" : "\"live\"")
core.setSetting(key: "auto-reconnect", value: settings.autoReconnect ? "true" : "false")
core.setSetting(key: "pane-bars", value: settings.paneBars ? "true" : "false")
// The core's own menus and messages follow the app's language.
core.setSetting(key: "language", value: "\"\(L10n.currentTag)\"")
}
/// The base text size of every pane; a pinch scales one pane on top.
private func applyFontSize(_ pt: Double) {
core.setSetting(key: "font", value: "{\"mode\":\"pinned\",\"pt\":\(pt)}")
}
/// The tab `by` places along the strip from the current one, shown.
func switchTab(by: Int) {
guard let tabs = tabs?.tabs, !tabs.isEmpty,
let at = tabs.firstIndex(where: { $0.current }) else { return }
let next = at + by
guard tabs.indices.contains(next) else { return }
chromeClick("pane", pane: tabs[next].target)
}
/// The overview opened: the screens asked for are kept until it
/// closes (`releaseScreens`).
func keepScreens() { screensWanted = true }
/// The whole tab a pane is in, as it is split, for a picture of it;
/// `screens` fills in. Asked only while they are kept.
func requestScreen(pane: Int) {
guard screensWanted else { return }
let now = ContinuousClock.now
if let asked = screenAsks[pane], now - asked < .seconds(5) { return }
screenAsks[pane] = now
core.requestScreenPreview(pane: UInt32(pane))
}
/// The overview closed: its screens and pictures go, and answers on
/// the way find nobody waiting.
func releaseScreens() {
screensWanted = false
paintTurn += 1
screens = [:]
pictures = [:]
screenSerials = [:]
screenAsks = [:]
}
fileprivate func coreScreen(_ pane: Int, _ screen: TabPreview) {
screenAsks[pane] = nil
// One as it was is not drawn again.
guard screensWanted, screens[pane] != screen else { return }
screens[pane] = screen
screenSerials[pane, default: 0] += 1
}
/// Draw a pane's screen `pixels` large off the main thread: `done` has
/// it, unless the screens were let go meanwhile. Not kept: a flight to
/// the terminal's size wants it once.
func paint(pane: Int, pixels: CGSize, done: @escaping (CGImage) -> Void) {
guard let screen = screens[pane] else { return }
let face = terminalFace
let turn = paintTurn
painter.async { [weak self] in
let picture = ScreenPainter.paint(screen, pixels: pixels, face: face)
DispatchQueue.main.async {
guard let self, self.screensWanted, turn == self.paintTurn, let picture else { return }
done(picture)
}
}
}
/// Draw a pane's screen for its card; `pictures` has it once it is.
func paintScreen(pane: Int, pixels: CGSize) {
paint(pane: pane, pixels: pixels) { [weak self] picture in self?.pictures[pane] = picture }
}
/// The face the terminal draws in, by its PostScript name, which is
/// its file's name too.
var terminalFace: String { settings.fontFamily == "Fira Code" ? "FiraCode-Regular" : "JetBrainsMono-Regular" }
/// A pane rang its bell: a buzz, if the setting says so.
fileprivate func coreBell() {
DispatchQueue.main.async {
guard self.settings.bell else { return }
UINotificationFeedbackGenerator().notificationOccurred(.warning)
}
}
func stepFont(_ by: Double) { core.stepFont(by: by) }
// MARK: the App's chrome
func chromeClick(_ action: String, pane: Int? = nil, tab: Int? = nil) {
core.chromeClick(action: action, pane: pane.map { UInt32($0) }, tab: tab.map { UInt32($0) })
}
func sideClick(_ kind: String, id: String? = nil, flag: Bool? = nil) {
core.sideClick(kind: kind, id: id, flag: flag)
}
func sideKey(_ key: String, value: String) {
core.sideKey(key: key, value: value)
}
/// Show a thread, switching the Space on show to its own first when
/// it is in another one, so the strips follow.
/// The thread on show is written down for the next connect; the first
/// listing after a connect puts the phone back on the one it left.
private func rememberThread(_ threads: ThreadsView?) {
guard let key = lastThreadKey, let list = threads?.threads,
let current = list.first(where: { $0.current })?.id else { return }
if !restoredThread {
restoredThread = true
if let saved = UserDefaults.standard.string(forKey: key), saved != current,
list.contains(where: { $0.id == saved }) {
openThread(saved, space: nil)
return
}
}
UserDefaults.standard.set(current, forKey: key)
}
func openThread(_ id: String, space: String?) {
if let space, tree?.spaces.first(where: { $0.current })?.id != space { core.setSpace(id: space) }
sideClick("thread", id: id)
}
func setSpace(_ id: String) { core.setSpace(id: id) }
func contextMenu(_ kind: String, id: String) -> [MenuItem] {
ViewJSON.decode([MenuItem].self, core.contextMenu(kind: kind, id: id)) ?? []
}
/// Run a menu row. Copy and paste are the shell's to do.
func menuAction(_ id: String) {
guard let outcome = ViewJSON.decode(MenuOutcome.self, core.menuAction(id: id)) else { return }
if let text = outcome.copy {
UIPasteboard.general.string = text
}
if outcome.paste {
pasteFromClipboard()
}
}
func takeOver() { core.takeOver() }
/// The text between two visible cells of the focused pane, for the
/// clipboard, as the core reads it from the cells; the pane's own
/// selection is left be. A copy waits for the core.
func copyText(from first: (row: Int, col: Int), to last: (row: Int, col: Int)) -> String? {
core.textBetween(anchorRow: UInt32(first.row), anchorCol: UInt32(first.col),
headRow: UInt32(last.row), headCol: UInt32(last.col))
}
/// The text input reads the rows (the keyboard is up, or there is a
/// selection): the core keeps them current while it does.
func setScreenWanted(_ wanted: Bool) {
guard wanted != screenWanted else { return }
screenWanted = wanted
if wanted {
// What is out was made while nobody read it: the next made (the
// core makes one at once) is read, and until then the rows are
// asked for. Taken before asking, so that one is not missed.
screenRevision = core.screenRevision()
} else {
screenJSON = "null"
}
core.setScreenWanted(wanted: wanted)
}
/// Read the views the core last published. Called on the main thread
/// each frame the revision moved; a burst of changes is one read, and
/// it never waits for the core thread.
func refreshViews() {
let views = core.views()
guard views.revision != lastViews?.revision else { return }
let last = lastViews
lastViews = views
// A change of cards marks the views stale, so it is seen here.
let icons = core.tabIconsRevision()
if icons != iconsRevision { iconsRevision = icons }
// Decoded only when the text changed, and assigned only on a
// change: a published write redraws the whole screen, and the
// core publishes as often as output comes.
if views.tabs != last?.tabs {
let tabs = ViewJSON.decode(TabsView.self, views.tabs)
if tabs != self.tabs { self.tabs = tabs }
}
if views.sidebar != last?.sidebar {
let sidebar = ViewJSON.decode(SidebarView.self, views.sidebar)
if sidebar != self.sidebar { self.sidebar = sidebar }
}
let threadsChanged = views.threads != last?.threads
if threadsChanged {
let threads = ViewJSON.decode(ThreadsView.self, views.threads)
if threads != self.threads { self.threads = threads }
}
if views.connection > connectionBefore, threadsChanged || !restoredThread {
rememberThread(threads)
}
if views.tree != last?.tree {
let tree = ViewJSON.decode(TreeView.self, views.tree)
if tree != self.tree { self.tree = tree }
}
if views.navs != last?.navs {
let navs = ViewJSON.decode([NavView].self, views.navs) ?? []
if navs != self.navs { self.navs = navs }
}
if views.status != last?.status {
let status = ViewJSON.decode(StatusView.self, views.status)
if status != self.status {
self.status = status
showToast(status?.toast)
}
}
guard views.layout != last?.layout else { return }
let layout = views.layout
if let r = layout.range(of: "\"cell\":["),
let end = layout[r.upperBound...].firstIndex(of: "]") {
let parts = layout[r.upperBound..<end].split(separator: ",")
if parts.count == 2, let h = Double(parts[1]), h > 0 { cellHeight = h }
}
if let r = layout.range(of: "\"scroll\":["),
let end = layout[r.upperBound...].firstIndex(of: "]") {
let parts = layout[r.upperBound..<end].split(separator: ",")
if parts.count == 2, let a = Double(parts[0]), let b = Int(parts[1]), (a, b) != scroll {
scroll = (a, b)
scrollShown = true
scrollTimer?.invalidate()
scrollTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { [weak self] _ in
self?.scrollShown = false
}
}
}
if let r = layout.range(of: "\"font_pt\":"),
let end = layout[r.upperBound...].firstIndex(where: { $0 == "," || $0 == "}" }),
let pt = Double(layout[r.upperBound..<end]), pt > 0, pt != fontPt {
fontPt = pt
}
}
// MARK: surface lifecycle, all on the main thread
func surfaceReady(_ layer: CAMetalLayer, width: Int, height: Int, scale: Double) {
self.layer = layer
wantsSurface = true
if generation == 0 {
attach(layer, width: width, height: height, scale: scale)
} else if lastSize != (width, height, scale) {
lastSize = (width, height, scale)
log("shell: surface \(width)x\(height) @\(scale)")
core.resize(generation: generation, width: UInt32(width), height: UInt32(height), scale: scale)
}
}
func surfaceGone() {
wantsSurface = false
detach()
}
private func attach(_ layer: CAMetalLayer, width: Int, height: Int, scale: Double) {
let ptr = UInt64(UInt(bitPattern: Unmanaged.passUnretained(layer).toOpaque()))
let gen = core.attachSurface(layer: ptr, width: UInt32(width), height: UInt32(height), scale: scale)
generation = gen
lastSize = (width, height, scale)
attached = gen != 0
if gen == 0 { log("shell: attach failed") }
}
private func detach() {
guard generation != 0 else { return }
let started = CACurrentMediaTime()
core.detachSurface(generation: generation)
let ms = (CACurrentMediaTime() - started) * 1000
log(String(format: "shell: detach %d took %.2f ms", generation, ms))
generation = 0
attached = false
}
func toggleAttached() {
if attached {
detach()
} else if let layer, let size = drawableSize(of: layer) {
attach(layer, width: size.0, height: size.1, scale: size.2)
}
}
/// The background took the connection down; the foreground brings it back.
private var pausedForBackground = false
func detachForBackground() {
inFront = false
updateShown()
detach()
report("background")
if settings.keepSessionInBackground {
// iOS grants about half a minute; the socket lives that long.
backgroundTask = UIApplication.shared.beginBackgroundTask(withName: "thinkterm-connection") { [weak self] in
self?.endBackgroundTask()
}
} else if !connection.hasPrefix("idle"), !connection.hasPrefix("disconnected"), !connection.hasPrefix("failed") {
// Attached or still on the way: torn down for the background,
// and dialled again on the way back -- whatever the status
// reads by then.
pausedForBackground = true
disconnect()
}
}
private func endBackgroundTask() {
if backgroundTask != .invalid {
UIApplication.shared.endBackgroundTask(backgroundTask)
backgroundTask = .invalid
}
}
func reattachAfterBackground() {
inFront = true
updateShown()
endBackgroundTask()
if pausedForBackground {
pausedForBackground = false
connect()
}
guard wantsSurface, generation == 0, let layer, let size = drawableSize(of: layer) else { return }
attach(layer, width: size.0, height: size.1, scale: size.2)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { self.report("foreground") }
}
func cycleAttachDetach(times: Int) {
guard let layer, let size = drawableSize(of: layer) else { return }
let started = CACurrentMediaTime()
for _ in 0..<times {
detach()
attach(layer, width: size.0, height: size.1, scale: size.2)
}
let ms = (CACurrentMediaTime() - started) * 1000
log(String(format: "shell: %d detach/attach cycles in %.1f ms", times, ms))
}
private func drawableSize(of layer: CAMetalLayer) -> (Int, Int, Double)? {
let scale = Double(layer.contentsScale)
let w = Int((Double(layer.bounds.width) * scale).rounded())
let h = Int((Double(layer.bounds.height) * scale).rounded())
guard w > 0, h > 0 else { return nil }
return (w, h, scale)
}
// MARK: frames
/// The display link: paint if asked, then read what the core published
/// -- the counts say whether there is anything new, without a wait.
@objc private func tick() {
#if DEBUG
if measuringTicks {
let now = CACurrentMediaTime()
if lastTickAt > 0 { longestTickGap = max(longestTickGap, now - lastTickAt) }
lastTickAt = now
}
#endif
if frameNeeded.take() {
core.render()
// Unwanted rows are not kept current: read again when asked.
if !screenWanted { inputView?.dropScreen() }
}
if core.viewsRevision() != lastViews?.revision {
refreshViews()
}
if screenWanted {
let revision = core.screenRevision()
if revision != screenRevision {
screenRevision = revision
screenJSON = core.screen()
inputView?.screenChanged()
}
}
}
func requestOneFrame() { frameNeeded.set() }
func toggleAnimating() {
animating.toggle()
core.setAnimating(on: animating)
}
func toggleInset() { inset = inset > 0 ? 0 : 60 }
func focusKeyboard() { inputView?.becomeFirstResponder() }
func showCopied() {
copied = true
DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) { self.copied = false }
}
func dismissKeyboard() { inputView?.resignFirstResponder() }
func compositionChanged(_ text: String?) {
DispatchQueue.main.async { self.composing = text }
}
// MARK: notify (core thread)
fileprivate func coreWantsFrame() { frameNeeded.set() }
fileprivate func coreStatus(_ status: String) {
DispatchQueue.main.async {
self.connection = status
self.log("status: " + status)
if status.hasPrefix("pane "), let host = self.host {
self.store?.touchConnected(host.id)
}
if status.hasPrefix("connecting") || status.contains("reconnect") || status.hasPrefix("disconnected") {
self.inputView?.dropComposition()
}
}
}
fileprivate func coreTitle(_ title: String) {
DispatchQueue.main.async {
if self.title != title { self.title = title }
}
}
fileprivate func coreHostKeyRequired(_ requestID: String, _ fingerprint: String) {
DispatchQueue.main.async {
guard self.activeRequestID == requestID, let endpoint = self.requestedEndpoint,
self.host?.id == endpoint.hostID else { return }
if let host = self.host {
guard host.hostname == endpoint.hostname, host.port == endpoint.port,
host.knownHost == nil else { return }
}
let challenge = HostKeyChallenge(id: requestID, hostname: endpoint.hostname,
port: endpoint.port, hostID: endpoint.hostID, fingerprint: fingerprint)
#if DEBUG
// The automated flows dial the probe's throwaway sshd, and no
// one is there to read its fingerprint.
if self.host == nil, Self.automated {
self.approveHostKey(challenge)
return
}
#endif
self.pendingHostKey = challenge
}
}
/// Launched for an automated flow (`--autoconnect`, `--uitest`, ...):
/// the app opens straight on the probe's terminal and connects on the
/// flow's schedule. Never in a release.
static let automated: Bool = {
#if DEBUG
return ProcessInfo.processInfo.arguments.contains { $0.hasPrefix("--auto") || $0.hasSuffix("test") }
#else
return false
#endif
}()
fileprivate func coreLogged(_ line: String) {
print("core: " + line)
DispatchQueue.main.async { self.log("core: " + line) }
}
private func log(_ line: String) {
#if DEBUG
NSLog("%@", line)
#endif
logLines.append(line)
if logLines.count > 60 { logLines.removeFirst(logLines.count - 60) }
logText = logLines.joined(separator: "\n")
}
// MARK: automated flows (launch arguments; results on the console)
private func report(_ step: String) {
let line = "AUTOTEST \(step): \(core.stats())"
print(line)
NSLog("%@", line)
log(line)
}
#if DEBUG
private func scheduleAutotest() {
let q = DispatchQueue.main
q.asyncAfter(deadline: .now() + 2.0) { self.report("start") }
q.asyncAfter(deadline: .now() + 2.1) { self.toggleAnimating() }
q.asyncAfter(deadline: .now() + 4.1) { self.toggleAnimating(); self.report("after 2s animating") }
q.asyncAfter(deadline: .now() + 4.5) { self.cycleAttachDetach(times: 20); self.report("after 20 cycles") }
q.asyncAfter(deadline: .now() + 5.0) { self.requestOneFrame() }
q.asyncAfter(deadline: .now() + 5.5) { self.toggleInset() }
q.asyncAfter(deadline: .now() + 6.5) { self.report("after shrink"); self.toggleInset() }
q.asyncAfter(deadline: .now() + 7.5) { self.report("after grow"); self.detach() }
q.asyncAfter(deadline: .now() + 8.0) { self.requestOneFrame(); self.report("detached, frame requested") }
q.asyncAfter(deadline: .now() + 8.5) { self.toggleAttached() }
q.asyncAfter(deadline: .now() + 9.5) { self.report("reattached"); print("AUTOTEST DONE") }
}
private func scheduleAutoconnect() {
let q = DispatchQueue.main
q.asyncAfter(deadline: .now() + 1.5) { self.connectProbe(); self.report("connect sent") }
q.asyncAfter(deadline: .now() + 5.0) { self.report("after connect") }
q.asyncAfter(deadline: .now() + 5.2) {
self.core.text(text: "echo '你好, 世界 🙂 hello from iOS'")
self.key("Enter")
}
q.asyncAfter(deadline: .now() + 7.5) { self.report("after typing") }
q.asyncAfter(deadline: .now() + 7.6) {
self.core.text(text: "sleep 2; echo WOKE-BY-OUTPUT")
self.key("Enter")
}
q.asyncAfter(deadline: .now() + 8.6) { self.report("before wake") }
q.asyncAfter(deadline: .now() + 11.0) { self.report("after wake") }
q.asyncAfter(deadline: .now() + 11.2) { self.cycleAttachDetach(times: 5); self.report("after 5 cycles connected") }
q.asyncAfter(deadline: .now() + 12.5) { self.report("final") }
q.asyncAfter(deadline: .now() + 15.0) { self.report("during reconnect") }
q.asyncAfter(deadline: .now() + 19.0) {
self.core.text(text: "echo AFTER-RECONNECT")
self.key("Enter")
}
q.asyncAfter(deadline: .now() + 21.0) { self.report("after reconnect"); print("AUTOCONNECT DONE") }
}
/// `--accepttest`: connect to the probe and do what ios/tools/accept.sh
/// asks, the driver that cuts the connection, restarts the server and
/// sends the app to the background from the Mac. Its requests come one
/// at a time through a file the simulator shares with the Mac, as
/// "<n> <verb> <argument>"; each is answered on the console with
/// "ACCEPT ack <n> <detail>".
private func scheduleAcceptTest() {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { self.connectProbe() }
acceptTimer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { [weak self] _ in
self?.pollAcceptRequest()
}
}
private static let acceptRequests = "/tmp/ttp-ssh/accept-request"
private func pollAcceptRequest() {
guard let text = try? String(contentsOfFile: Self.acceptRequests, encoding: .utf8) else { return }
let parts = text.trimmingCharacters(in: .whitespacesAndNewlines)
.split(separator: " ", maxSplits: 2).map(String.init)
guard parts.count >= 2, let n = Int(parts[0]), n > acceptAnswered else { return }
acceptAnswered = n
let argument = parts.count > 2 ? parts[2] : ""
switch parts[1] {
case "state":
acceptAnswer(n, acceptState())
case "type":
core.text(text: argument)
key("Enter")
acceptAnswer(n, "typed")
case "expect":
// The rows as they are now, not as last published.
let rows = ViewJSON.decode(ScreenText.self, core.screenText())?.text ?? ""
acceptAnswer(n, rows.contains(argument) ? "found" : "missing")
case "measure":
// From now until "measured", asked for once what is measured
// has been set going: a stall it starts with is in it.
longestTickGap = 0
lastTickAt = 0
measuringTicks = true
acceptAnswer(n, "measuring")
case "measured":
// The longest the main thread went without a frame callback.
measuringTicks = false
acceptAnswer(n, String(format: "longest_gap_ms=%.0f", longestTickGap * 1000))
default:
acceptAnswer(n, "unknown")
}
}
/// One line the driver parses: the connection, and the tabs and bars
/// the phone shows.
private func acceptState() -> String {
refreshViews()
let current = tabs?.tabs.first(where: { $0.current })
let pane = connection.hasPrefix("pane ") ? connection.dropFirst(5).prefix(while: { $0.isNumber }) : "-"
// The status line stays on the pane through the App's own redials;
// its sticky remark is what says the link is down.
let connected = connection.hasPrefix("pane ") && !reconnecting ? "yes" : "no"
return "connected=\(connected) pane=\(pane) tab=\(current.map { String($0.tab) } ?? "-")"
+ " tabs=\(tabs?.tabs.count ?? 0) panes=\(current?.panes.count ?? 0) bars=\(navs.count)"
}
private func acceptAnswer(_ n: Int, _ detail: String) {
let line = "ACCEPT ack \(n) \(detail)"
print(line)
log(line)
}
/// The App's chrome over the FFI: split, list the tabs and bars, open a
/// new tab, switch, close, and the sidebar rows.
private func scheduleUiTest() {
let q = DispatchQueue.main
q.asyncAfter(deadline: .now() + 1.5) { self.connectProbe() }
q.asyncAfter(deadline: .now() + 5.0) {
self.refreshViews()
self.check("tabs view", (self.tabs?.tabs.count ?? 0) >= 1, "\(self.tabs?.tabs.count ?? -1) tabs")
// A lone pane has no bar on the phone (Platform::bare_lone_pane).
self.check("lone pane, no bar", self.navs.isEmpty, "\(self.navs.count) bars")
self.chromeClick("split-right")
}
q.asyncAfter(deadline: .now() + 7.0) {
self.refreshViews()
self.check("split drew two bars", self.navs.count == 2, "\(self.navs.count) bars")
let sidebar = self.sidebar?.rows.count ?? 0
self.check("sidebar rows", sidebar >= 1, "\(sidebar) rows")
self.chromeClick("new-tab")
}
q.asyncAfter(deadline: .now() + 9.0) {
self.refreshViews()
let n = self.tabs?.tabs.count ?? 0
self.check("new tab listed", n >= 2, "\(n) tabs")
if let first = self.tabs?.tabs.first(where: { !$0.current }) {
self.chromeClick("pane", pane: first.target)
}
}
q.asyncAfter(deadline: .now() + 11.0) {
self.refreshViews()
self.check("switched tab", self.tabs?.tabs.first(where: { $0.current })?.panes.count == 2, "current has \(self.tabs?.tabs.first(where: { $0.current })?.panes.count ?? -1) panes")
let menu = self.contextMenu("pane", id: String(self.tabs?.tabs.first(where: { $0.current })?.target ?? 0))
self.check("pane menu", !menu.isEmpty, "\(menu.count) items")
self.chromeClick("close")
}
q.asyncAfter(deadline: .now() + 13.0) {
self.refreshViews()
self.check("closed pane, bars gone", self.navs.isEmpty, "\(self.navs.count) bars")
// The size from Settings reaches the open terminal, not only the next one.
let size = self.settings.fontSize
self.settings.fontSize = size + 2
q.asyncAfter(deadline: .now() + 1.5) {
self.refreshViews()
self.check("text size is live", abs(self.fontPt - (size + 2)) < 0.01, "\(size) -> \(self.fontPt) pt")
self.settings.fontSize = size
self.report("ui done")
print("UITEST DONE")
}
}
}
/// The native selection's document against the core: the rows read
/// back, a programmatic selection lands in the core and copies.
/// Threads on a bare server: a project, two threads in it, and the
/// two-level strip that follows.
private func scheduleTreeTest() {
let q = DispatchQueue.main
var project: String?
q.asyncAfter(deadline: .now() + 1.5) { self.connectProbe() }
q.asyncAfter(deadline: .now() + 4.0) { self.sideClick("new-project") }
q.asyncAfter(deadline: .now() + 4.6) { self.sideKey("Enter", value: "/tmp/ttp-srv") }
q.asyncAfter(deadline: .now() + 6.5) {
self.refreshViews()
project = self.sidebar?.rows.compactMap { row -> String? in
if case .project(let id, _, _, _, false) = row { return id }
return nil
}.first
self.check("project made", project != nil, self.sidebar?.new_project_error ?? "\(self.sidebar?.rows.count ?? -1) rows")
if let project { self.sideClick("new-thread", id: project) }
}
q.asyncAfter(deadline: .now() + 9.5) {
self.refreshViews()
let threads = self.threads?.threads ?? []
self.check("thread listed", threads.count >= 1, "\(threads.count) threads")
self.check("thread current with a tab", threads.contains { $0.current && !$0.tabs.isEmpty }, threads.map { "\($0.name):\($0.tabs.count):\($0.current)" }.joined(separator: " "))
if let project { self.sideClick("new-thread", id: project) }
}
q.asyncAfter(deadline: .now() + 12.5) {
self.refreshViews()
let threads = self.threads?.threads ?? []
self.check("second thread", threads.count >= 2, "\(threads.count) threads")
self.check("one current", threads.filter(\.current).count == 1, threads.map { "\($0.name):\($0.current)" }.joined(separator: " "))
self.report("tree done")
print("TREETEST DONE")
}
}
private func scheduleSelectionTest() {
let q = DispatchQueue.main
q.asyncAfter(deadline: .now() + 1.5) { self.connectProbe() }
q.asyncAfter(deadline: .now() + 4.0) { self.core.text(text: "echo SELECT-ME-PLEASE\n") }
q.asyncAfter(deadline: .now() + 6.0) {
guard let input = self.inputView else { return self.check("input view", false, "none") }
let json = self.core.screenText()
guard let screen = ViewJSON.decode(ScreenText.self, json) else {
return self.check("screen text", false, String(json.prefix(80)))
}
let stride = screen.cols + 1
self.check("screen text shape", screen.text.utf16.count == screen.rows * stride, "\(screen.rows)x\(screen.cols), \(screen.text.utf16.count) units")
// The echoed line: the last row holding it.
let rows = screen.text.split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
guard let row = rows.lastIndex(where: { $0.contains("SELECT-ME-PLEASE") }),
let col = rows[row].range(of: "SELECT-ME-PLEASE")?.lowerBound.utf16Offset(in: rows[row]) else {
return self.check("echoed line on screen", false, "not found")
}
let a = row * stride + col
let range = TerminalInputView.Range(a, a + "SELECT-ME-PLEASE".count)
input.selectedTextRange = range
let read = input.text(in: range) ?? ""
self.check("document text", read == "SELECT-ME-PLEASE", read)
q.asyncAfter(deadline: .now() + 0.5) {
let copied = input.selectedText() ?? ""
self.check("shell selection", copied == "SELECT-ME-PLEASE", copied)
let back = input.selectedTextRange as? TerminalInputView.Range
self.check("selection read back", back?.a == a && back?.b == a + "SELECT-ME-PLEASE".count, "\(back?.a ?? -1)..\(back?.b ?? -1)")
let rects = input.selectionRects(for: range)
self.check("selection rects", rects.count == 1 && rects[0].rect.width > 0, "\(rects.count) rects, first \(rects.first?.rect ?? .zero)")
let under = input.closestPosition(to: CGPoint(x: rects.first?.rect.midX ?? 0, y: rects.first?.rect.midY ?? 0)) as? TerminalInputView.Pos
self.check("point to cell", under.map { $0.i >= a && $0.i < a + 16 } ?? false, "\(under?.i ?? -1)")
// Wide characters and one outside the BMP: the rows carry
// stand-ins for both, the copy must not.
self.core.text(text: "echo 复制中文😀OK\n")
}
q.asyncAfter(deadline: .now() + 2.5) {
let wide = "复制中文😀OK"
guard let screen = ViewJSON.decode(ScreenText.self, self.core.screenText()) else {
return self.check("wide line on screen", false, "no rows")
}
let rows = screen.text.split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
guard let row = rows.lastIndex(where: { $0.contains("OK") && $0.contains("文") && !$0.contains("echo") }) else {
return self.check("wide line on screen", false, "not found")
}
let start = row * (screen.cols + 1)
input.selectedTextRange = TerminalInputView.Range(start, start + screen.cols)
let copied = input.selectedText() ?? ""
self.check("copy keeps wide text", copied == wide, copied)
self.report("selection done")
print("SELTEST DONE")
}
}
}
private func scheduleImeTest() {
let q = DispatchQueue.main
var sent: Int { Int(inputsSent()) }
var sizeBefore = ""
q.asyncAfter(deadline: .now() + 1.5) { self.connectProbe() }
q.asyncAfter(deadline: .now() + 5.0) {
sizeBefore = self.sizeString()
let asked = Date()
self.focusKeyboard()
// The first keyboard in a while is slow to come up: wait for the
// resize, up to 5 s, rather than a fixed time.
func look() {
let after = self.sizeString()
let waited = Date().timeIntervalSince(asked)
if after != sizeBefore || waited > 5 {
self.check(
"keyboard resize", after != sizeBefore,
"\(sizeBefore) -> \(after) after \(String(format: "%.1f", waited)) s"
)
} else {
q.asyncAfter(deadline: .now() + 0.1, execute: look)
}
}
look()
}
q.asyncAfter(deadline: .now() + 7.0) {
guard let v = self.inputView else { self.check("input view", false, "missing"); return }
let base = sent
v.setMarkedText("n", selectedRange: NSRange(location: 1, length: 0))
v.setMarkedText("ni", selectedRange: NSRange(location: 2, length: 0))
v.setMarkedText("nih", selectedRange: NSRange(location: 3, length: 0))
q.asyncAfter(deadline: .now() + 0.3) {
self.check("composing sends nothing", sent == base, "sent \(sent - base), composing=\(self.composingFlag())")
v.insertText("你")
v.unmarkText()
q.asyncAfter(deadline: .now() + 0.3) {
self.check("candidate commits once", sent == base + 1, "sent \(sent - base)")
let b2 = sent
v.setMarkedText("hao", selectedRange: NSRange(location: 3, length: 0))
v.setMarkedText("", selectedRange: NSRange(location: 0, length: 0))
q.asyncAfter(deadline: .now() + 0.3) {
self.check("cancel sends nothing", sent == b2, "sent \(sent - b2)")
v.setMarkedText("hao", selectedRange: NSRange(location: 3, length: 0))
v.deleteBackward()
v.setMarkedText("ha", selectedRange: NSRange(location: 2, length: 0))
q.asyncAfter(deadline: .now() + 0.3) {
self.check("backspace edits composition only", sent == b2 && v.marked == "ha", "sent \(sent - b2), marked=\(v.marked ?? "nil")")
v.unmarkText()
q.asyncAfter(deadline: .now() + 0.3) {
self.check("leftover composition commits once", sent == b2 + 1, "sent \(sent - b2)")
self.key("Enter")
q.asyncAfter(deadline: .now() + 1.5) {
self.report("ime done")
print("IMETEST DONE")
}
}
}
}
}
}
}
}
private func check(_ name: String, _ ok: Bool, _ detail: String) {
let line = "UICHECK \(ok ? "PASS" : "FAIL") \(name): \(detail)"
print(line)
log(line)
}
private func statsField(_ key: String) -> String {
let s = core.stats()
guard let r = s.range(of: "\"\(key)\":") else { return "" }
let rest = s[r.upperBound...]
return String(rest.prefix { $0 != "," && $0 != "}" }).replacingOccurrences(of: "\"", with: "")
}
private func inputsSent() -> Int64 { Int64(statsField("inputs_sent")) ?? -1 }
private func composingFlag() -> String { statsField("composing") }
private func sizeString() -> String { statsField("size") }
#endif
}
#if DEBUG
/// The Mac user the probe's key belongs to: the `user@host` comment
/// ssh-keygen wrote at the end of the public key. The simulator gives an
/// app neither USER nor a real NSUserName.
func probeUserName() -> String {
if let pub = try? String(contentsOfFile: "/tmp/ttp-ssh/userkey.pub", encoding: .utf8),
let comment = pub.split(separator: " ").dropFirst(2).first,
let user = comment.split(separator: "@").first, !user.isEmpty {
return String(user)
}
let env = ProcessInfo.processInfo.environment["USER"] ?? ""
return env.isEmpty ? NSUserName() : env
}
#endif
/// UniFFI hands the callback object to the core thread; it forwards to the
/// model without touching anything that needs the main thread.
private final class NotifySink: Notify, @unchecked Sendable {
weak var model: TerminalModel?
private let screenReader = DispatchQueue(label: "thinkterm.screens", qos: .userInitiated)
func onFrameNeeded() { model?.coreWantsFrame() }
func onStatus(status: String) { model?.coreStatus(status) }
func onLog(line: String) { model?.coreLogged(line) }
func onTitle(title: String) { model?.coreTitle(title) }
/// The display link reads the views' count each frame instead.
func onChange() {}
func onClipboard(text: String) {
DispatchQueue.main.async {
UIPasteboard.general.string = text
self.model?.showCopied()
}
}
/// The App asks for the input's focus after a press, a finished
/// rename and every answered request. On a phone that would raise
/// the keyboard over half the screen at each of them, so the ask is
/// honoured only while the keyboard is already up -- and then the
/// input is already first responder. A tap raises it on its own.
func onFocusInput() {}
func onImeAnchor(left: Double, top: Double, width: Double, height: Double) {
DispatchQueue.main.async {
self.model?.cursorRect = CGRect(x: left, y: top, width: width, height: height)
}
}
func onHostKeyRequired(requestId: String, fingerprint: String) { model?.coreHostKeyRequired(requestId, fingerprint) }
func onPublished(key: String, value: String) { model?.corePublished(key, value) }
func onBell() { model?.coreBell() }
func onPreview(pane: UInt32, rows: String) {
// Only whole screens are asked for here (`requestScreen`), read off
// the core's thread, in the order they come.
screenReader.async {
guard let screen = ViewJSON.decode(TabPreview.self, rows) else { return }
DispatchQueue.main.async { self.model?.coreScreen(Int(pane), screen) }
}
}
}
/// Paints the graphemes the bundled faces lack with CoreText, which falls
/// through the system's cascade list (PingFang for Han, Apple Color Emoji
/// for emoji) on its own. Called on the core thread.
final class CoreTextPainter: GlyphPainter, @unchecked Sendable {
func paint(text: String, px: Double, width: UInt32, height: UInt32, penX: Double, baseline: Double, red: UInt8, green: UInt8, blue: UInt8) -> Data {
let w = Int(width), h = Int(height)
guard w > 0, h > 0 else { return Data() }
let colorSpace = CGColorSpaceCreateDeviceRGB()
guard let ctx = CGContext(
data: nil, width: w, height: h, bitsPerComponent: 8, bytesPerRow: w * 4,
space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
) else { return Data() }
ctx.clear(CGRect(x: 0, y: 0, width: w, height: h))
// Menlo's own cascade list does not reach Apple Color Emoji on iOS;
// asking CoreText for the font that can draw this string does.
let base = CTFontCreateWithName("Menlo" as CFString, CGFloat(px), nil)
let font = CTFontCreateForString(base, text as CFString, CFRange(location: 0, length: (text as NSString).length))
let color = CGColor(red: CGFloat(red) / 255, green: CGFloat(green) / 255, blue: CGFloat(blue) / 255, alpha: 1)
let attrs: [NSAttributedString.Key: Any] = [
NSAttributedString.Key(kCTFontAttributeName as String): font,
NSAttributedString.Key(kCTForegroundColorAttributeName as String): color,
]
let line = CTLineCreateWithAttributedString(NSAttributedString(string: text, attributes: attrs))
// CoreGraphics draws with the origin at the bottom-left, but a
// bitmap context's first row in memory is the top of the image, so
// the baseline is flipped for drawing and the rows are read as-is.
ctx.textMatrix = .identity
ctx.textPosition = CGPoint(x: penX, y: Double(h) - baseline)
CTLineDraw(line, ctx)
guard let base = ctx.data else { return Data() }
let src = base.assumingMemoryBound(to: UInt8.self)
var out = [UInt8](repeating: 0, count: w * h * 4)
for row in 0..<h {
let srcRow = row * w * 4
let dstRow = row * w * 4
for x in 0..<w {
let s = srcRow + x * 4, d = dstRow + x * 4
let a = src[s + 3]
if a == 0 { continue }
let scale = 255.0 / Double(a)
out[d] = UInt8(min(255.0, Double(src[s]) * scale))
out[d + 1] = UInt8(min(255.0, Double(src[s + 1]) * scale))
out[d + 2] = UInt8(min(255.0, Double(src[s + 2]) * scale))
out[d + 3] = a
}
}
return Data(out)
}
}
final class AtomicFlag: @unchecked Sendable {
private let lock = NSLock()
private var value = false
func set() { lock.lock(); value = true; lock.unlock() }
func take() -> Bool { lock.lock(); defer { lock.unlock() }; let v = value; value = false; return v }
}