Files
app/ios/DModem/Sources/TerminalModel.swift
T
NeoandClaude 2642a752a7 DEC terminals: VT52, line sizes, 132 columns, keypad, printer port (#34)
The DEC levels get what their terminals had: a VT52 profile with its own
interpreter (ESC Y addressing, ESC F graphics from the DECscope manual,
ESC Z), and a VT100's VT52 mode (DECANM) hosted by the VT interpreter.
Double-width and double-height lines (DECDWL, DECDHL) are drawn scaled on
Android and iOS. DECCOLM asks for 80 or 132 columns and both apps refit
the font to the width (xterm only once mode 40 allows it). Character sets
become GSet (ASCII, DEC graphics, UK, VT52 graphics). DECLL keeps the
LEDs, DECREQTPARM and DECID answer, and a printer port (VT102 on: MC
controller mode, auto print, print screen) hands output to the listener,
which Android saves to a text file.

The key bar is now shared: KeyBars has the pages each profile offers, the
DEC ones adding a keypad page (PF1-PF4, digits, minus, comma, period,
Enter) that KeyEncoder sends in numeric or application mode, VT52 style
where the terminal is one. Android maps a hardware numpad onto it.


Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-10 16:09:09 -08:00

96 lines
3.3 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
/// 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
revision = bridge.revision
}
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 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)
}
}