Files
app/ios/DModem/Sources/TerminalModel.swift
T
NeoandClaude Opus 5.5 0c448f7d4d VT220 to VT525 and videotex terminals (Phases 2, 3 and 8) (#37)
* VT220 and VT320: G0-G3 and shifts, NRCS, 8-bit replies, DECSCA, DECUDK

Phase 2, part 1. The character sets move into CharsetShifting, which
VtInterpreter composes: G0-G3, GL and GR, the locking shifts (SI, SO,
LS2, LS3, LS1R-LS3R) and single shifts (SS2, SS3, and their C1 forms),
designators of two characters and 96-character sets (ESC - . /), the
national replacement sets, DEC Supplemental, DEC Technical and Latin-1,
each at the level that has it, and DECSC keeping them all.

Also:
- S7C1T/S8C1T: replies with C1 controls
- DECSCA with DECSED and DECSEL (a protected bit SGR leaves alone)
- DECSCL: the operating level and 7- or 8-bit replies
- DSR ?15, ?25, ?26 and ?6 (DECXCPR)
- DECRQPSR (DECCIR, DECTABSR), DECRQUPSS and DECAUPSS
- DECRQSS for DECSCL, DECSCA, DECSLPP, DECSCPP, DECSNLS
- DECUDK in UserKeys: Shift with F6-F20 sends what the host loaded
- F13-F20, Find and Select keys, and LK201 and UDK key bar pages
- a VT320 profile (level 3)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY

* VT320 status line and DECDLD soft characters

Phase 2, part 2:
- DECSSDT and DECSASD: the indicator status line (the cursor's place)
  or the host's own, written with a cursor of its own (StatusLine);
  shown below the screen on Android and iOS, the screen a line shorter
- DECDLD (SoftFont): a soft set's glyphs read from their sixels, kept
  as code points of plane 15, designated by the host's Dscs (a GSet
  SOFT slot in CharsetShifting) and drawn from their bitmaps by both
  apps; BitmapFont now takes a width (rows as Ints), so the soft
  glyphs keep theirs (a VT320's 15x12)
- DECRQSS for DECSSDT and DECSASD; DECCIR names the soft set

Pages (NP, PP, PPA and the rest) are a VT330's and VT420's: they go
with Phase 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY

* VT420, VT510, VT520, VT525: margins, rectangles, pages, colour table

Phase 3. A Vt420Interpreter extends VtInterpreter through its hooks
(an ESC, a CSI, a DCS, DECRQSS and DECRQM a subclass takes):
- DECLRMM and DECSLRM: the emulator's printing, wrapping, CR, scrolling,
  IL/DL and ICH/DCH keep between the margins
- Rectangles: DECCRA, DECERA, DECSERA, DECFRA, DECCARA and DECRARA
  (DECSACE: a stream or a rectangle), DECRQCRA's checksum, DECIC/DECDC
- DECBI and DECFI
- pages: NP, PP, PPA, PPR, PPB over six, DECXCPR telling the page
- DECSNLS, DECSLPP, DECSCPP; DECNKM, DECBKM
- VT5xx: DECRQDE, the bell volume and keyboard sequences taken;
  VT525: DECAC and the colour table (DECRQTSR 2, DECRSTS in RGB or HLS)
- VT420, VT510, VT520 and VT525 profiles

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY

* Viewdata (Prestel) and Minitel terminals

Phase 8. Both build on ByteControlInterpreter through a
VideotexInterpreter (the teletext palette, the mosaics' glyphs on the
emulator, a cursor wrapping round the screen):
- Mosaic: 2x3 block graphics, contiguous and separated, drawn from
  bitmaps the app makes, at plane 15 code points (the soft glyph path)
- Viewdata: the screen kept as its codes, each row drawn again from
  them with teletext Level 1's spacing attributes (set-at and
  set-after); Prestel's controls; # and Enter send 0x5f
- Minitel: row 0 the status row, US addressing, G0/G1, SS2 accents and
  symbols, ESC attributes with the background and separation taken at
  a delimiter, REP, CSI commands, PRO1-PRO3 (ENQROM answered, ROULEAU,
  Mixte's 80 columns, Téléinformatique as a VT100), its function keys
- key bars for both, captures as .vdt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY

* VT525 colour table: DEC's HLS from Sixel's, not a copy

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-10 17:19:36 -08:00

155 lines
5.7 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
/// the next key with Alt (the key bar's sticky Alt)
@Published var alt = false
/// the status line below the screen (a VT320's, DECSSDT): "" for none
@Published private(set) var statusText = ""
/// to the modem
var send: ((Data) -> Void)?
/// the emulator set up for BBSes: CP437, bold as bright, the VGA colours
init() {
// (BBSes: CP437, bold as bright; the VGA colours)
bridge.setLook(themeId: "vga", boldBright: true, iceColors: false, charset: "CP437")
}
/// the kind of terminal (a profile's id, as Settings keeps it under "terminalProfile")
func setProfile(_ id: String) {
bridge.profile = id
glyphCache.removeAll()
revision = bridge.revision
}
/// a bitmap glyph: its rows, top first, bit width - 1 the leftmost pixel
struct Glyph {
let rows: [Int]
let width: Int
}
/// the profile's glyphs, as they're asked for
private var glyphCache: [UInt32: Glyph?] = [:]
/// the bitmap glyph for [cp] (the profile's, 8 wide and 8 or 16 high, or a soft character the host loaded); nil: drawn with the font
func glyph(_ cp: UInt32) -> Glyph? {
// (soft characters can be loaded again: never kept)
let soft = cp >= 0xF0000
if !soft, let g = glyphCache[cp] { return g }
let c = Int32(bitPattern: cp)
let h = Int(bridge.glyphHeightOf(cp: c))
let g: Glyph? = h == 0 ? nil : Glyph(rows: (0..<h).map { Int(bridge.glyphRow(cp: c, row: Int32($0))) },
width: Int(bridge.glyphWidthOf(cp: c)))
if !soft { glyphCache[cp] = g }
return g
}
var cols: Int { Int(bridge.cols) }
var rows: Int { Int(bridge.rows) }
/// ANSI music's notes, played as they come
private let music = MusicPlayer()
/// what the modem said that's still to be shown, at an emulated line speed
private var paced = Data()
/// the pacing under way
private var pacing: Task<Void, Never>?
/// what the modem said, onto the screen (at the line speed the remote asked for, CTerm's CSI * r, if it did); the terminal's answers back
func feed(_ data: Data) {
if pacing != nil || bridge.emulatedBps > 0 {
paced.append(data)
if pacing == nil { pacing = Task { await pace() } }
return
}
show(data)
}
/// the paced octets onto the screen a few at a time, every 20 ms
private func pace() async {
while !paced.isEmpty {
let bps = Int(bridge.emulatedBps)
let n = bps > 0 ? min(paced.count, max(1, bps / 10 / 50)) : paced.count
show(paced.prefix(n))
paced.removeFirst(n)
if bps > 0 { try? await Task.sleep(nanoseconds: 20_000_000) }
}
pacing = nil
}
/// octets onto the screen now, the answers and music they bring dealt with
private func show(_ 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) }
let notes = bridge.takeMusic()
if !notes.isEmpty { music.play(notes) }
let status = bridge.statusText
if status != statusText { statusText = status }
revision = bridge.revision
}
/// text typed (the keyboard's), Ctrl and Alt (a Commodore's C=) if the sticky ones are down
func typed(_ text: String) {
out(bridge.typed(text: text, ctrl: ctrl, alt: alt))
ctrl = false
alt = 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 key bar's key at ([row], [col]) of [page] pressed, Ctrl and Alt if the sticky ones are down
func barKey(page: Int, row: Int, col: Int) {
out(bridge.keyBarPress(page: Int32(page), row: Int32(row), col: Int32(col), ctrl: ctrl, alt: alt))
ctrl = false
alt = 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)) }
/// the emulator's Latin-1 output as octets to the modem (nothing if there's none)
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)
}
}