Files
app/ios/DModem/Sources/TerminalModel.swift
T
Claude 133d5acb61 iOS app: SwiftUI on DModemKit and the shared terminal
ios/DModem (XcodeGen's project.yml, iOS 16): Home (the modem's state, the
rates and what the CONNECT was, the call's time, start, hang up, stop, an
AT command line and its last words), the terminal (the shared Kotlin
emulator through TerminalBridge, drawn run by run in a Canvas; CP437 and
the VGA colours; the keyboard and a hardware one's arrows and Esc; Esc,
a sticky Ctrl, Tab and the arrows on a bar; pinch for the font), the log
(shared as text) and the settings (the SIP account or user:password@host,
incoming calls, T.38, the speaker, the AT commands at start, the log
levels). The modem's speaker on AVAudioEngine; the audio session (its
sound, or quiet) keeps a call going in the background. The DNS servers
from the resolver's state (SystemDNS.c: iOS has no resolv.conf for the
engine).

shared: TerminalBridge, the terminal for Swift (octets as Latin-1 strings
both ways, rows as runs, keys, replies, the title, the look and the
character set), tested on the JVM and natively; the framework also as an
XCFramework (assembleDModemSharedXCFramework).

CI: the macOS job builds the shared XCFramework, runs DModemKit's tests
and builds the app for the simulator; the Linux job runs DModemKit's
tests on the host engine. Not built here: no Xcode on Linux (DModemKit,
TerminalBridge and SystemDNS.c are).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
2026-10-18 02:15:16 +00:00

79 lines
2.6 KiB
Swift

// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Neo-Desktop
import DModemShared
import Foundation
import SwiftUI
/// The terminal on the modem: the shared Kotlin's emulator
/// (TerminalBridge) fed what the modem says, its keys and replies to the
/// modem ([send]). Octets cross to Kotlin as Latin-1 strings, one
/// character each.
@MainActor
final class TerminalModel: ObservableObject {
let bridge = TerminalBridge(cols: 80, rows: 24)
/// the screen changed: redraw
@Published private(set) var revision: Int64 = 0
/// the next key with Ctrl (the extra keys' sticky Ctrl)
@Published var ctrl = false
/// to the modem
var send: ((Data) -> Void)?
init() {
// (BBSes: CP437, bold as bright; the VGA colours)
bridge.setLook(themeId: "vga", boldBright: true, iceColors: false, charset: "CP437")
}
var cols: Int { Int(bridge.cols) }
var rows: Int { Int(bridge.rows) }
/// what the modem said, onto the screen; the terminal's answers back
func feed(_ data: Data) {
guard let s = String(data: data, encoding: .isoLatin1) else { return }
bridge.feed(latin1: s)
let replies = bridge.takeReplies()
if !replies.isEmpty { out(replies) }
revision = bridge.revision
}
/// text typed (the keyboard's), Ctrl if the sticky one is down
func typed(_ text: String) {
out(bridge.typed(text: text, ctrl: ctrl, alt: false))
ctrl = false
}
/// a key that isn't a character: UP, DOWN, LEFT, RIGHT, ESCAPE, TAB, BACKSPACE, ENTER, F1 ...
func key(_ name: String) {
out(bridge.key(name: name, shift: false, alt: false, ctrl: ctrl))
ctrl = false
}
/// the screen to [cols] by [rows] (the view's size)
func resize(cols: Int, rows: Int) {
guard cols > 0, rows > 0, cols != self.cols || rows != self.rows else { return }
bridge.resize(cols: Int32(cols), rows: Int32(rows))
revision = bridge.revision
}
/// a row's runs
func runs(_ row: Int) -> [StyledRun] { bridge.runs(row: Int32(row)) }
private func out(_ latin1: String) {
guard !latin1.isEmpty, let d = latin1.data(using: .isoLatin1) else { return }
send?(d)
}
}
extension Color {
/// an ARGB colour as Kotlin has it
init(argb: Int32) {
let u = UInt32(bitPattern: argb)
self.init(
.sRGB,
red: Double((u >> 16) & 0xff) / 255,
green: Double((u >> 8) & 0xff) / 255,
blue: Double(u & 0xff) / 255,
opacity: Double((u >> 24) & 0xff) / 255)
}
}