mirror of
https://github.com/D-Modem/app.git
synced 2026-11-28 17:38:05 -08:00
* 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>
292 lines
13 KiB
Swift
292 lines
13 KiB
Swift
// SPDX-License-Identifier: AGPL-3.0-only
|
|
// Copyright (C) 2026 Neo-Desktop
|
|
import DModemShared
|
|
import SwiftUI
|
|
import UIKit
|
|
|
|
/// The terminal: the screen drawn run by run in its cells, the keyboard
|
|
/// on a tap (KeyInput), the keys a phone's keyboard lacks below it.
|
|
struct TerminalScreen: View {
|
|
@ObservedObject var model: TerminalModel
|
|
@State private var focused = true
|
|
@AppStorage("terminalFontSize") private var fontSize: Double = 12
|
|
@AppStorage("terminalProfile") private var profile = "xterm"
|
|
|
|
var body: some View {
|
|
VStack(spacing: 0) {
|
|
GeometryReader { geo in
|
|
let cell = metrics(geo.size)
|
|
ScreenCanvas(model: model, revision: model.revision, cell: cell)
|
|
.background(Color(argb: model.bridge.background))
|
|
.onAppear { fit(geo.size, cell) }
|
|
.onChange(of: geo.size) { fit($0, metrics($0)) }
|
|
.onChange(of: fontSize) { _ in fit(geo.size, metrics(geo.size)) }
|
|
// (the remote's DECCOLM: 80 or 132 columns fitted to the width)
|
|
.onChange(of: model.bridge.requestedColumns) { _ in fit(geo.size, metrics(geo.size)) }
|
|
// (a machine's own screen: its rows at most, square cells for its glyphs)
|
|
.onChange(of: model.bridge.requestedRows) { _ in fit(geo.size, metrics(geo.size)) }
|
|
.onChange(of: model.bridge.glyphHeight) { _ in fit(geo.size, metrics(geo.size)) }
|
|
.overlay(KeyInput(model: model, focused: $focused).frame(width: 1, height: 1).opacity(0.01), alignment: .topLeading)
|
|
.contentShape(Rectangle())
|
|
.onTapGesture { focused = true }
|
|
.gesture(MagnificationGesture().onEnded { s in fontSize = min(24, max(7, fontSize * Double(s))) })
|
|
}
|
|
// a VT320's status line, below the screen (the screen a line shorter)
|
|
if !model.statusText.isEmpty {
|
|
Text(model.statusText)
|
|
.font(.system(size: 12, design: .monospaced))
|
|
.lineLimit(1)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.padding(.horizontal, 4)
|
|
.background(Color(.secondarySystemBackground))
|
|
}
|
|
ExtraKeys(model: model)
|
|
}
|
|
.onAppear { model.setProfile(profile) }
|
|
.onChange(of: profile) { model.setProfile($0) }
|
|
.navigationTitle(model.bridge.title.isEmpty ? "Terminal" : model.bridge.title)
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
}
|
|
|
|
/// the cells: as many as the remote or the machine asked for (DECCOLM, a Commodore's 40) across [size], else at the font size; square for bitmap glyphs
|
|
private func metrics(_ size: CGSize) -> CellMetrics {
|
|
let asked = Int(model.bridge.requestedColumns)
|
|
let m = asked > 0 && size.width > 0 ? CellMetrics(columns: asked, across: size.width) : CellMetrics(fontSize: fontSize)
|
|
return model.bridge.bitmapGlyphs ? m.shaped(rows: Int(model.bridge.glyphHeight)) : m
|
|
}
|
|
|
|
/// as many cells as fit (no more rows than the machine's screen had)
|
|
private func fit(_ size: CGSize, _ cell: CellMetrics) {
|
|
let asked = Int(model.bridge.requestedColumns)
|
|
let fitRows = max(5, Int(size.height / cell.height))
|
|
let maxRows = Int(model.bridge.requestedRows)
|
|
model.resize(cols: asked > 0 ? asked : max(20, Int(size.width / cell.width)),
|
|
rows: maxRows > 0 ? min(fitRows, maxRows) : fitRows)
|
|
}
|
|
}
|
|
|
|
/// a cell's size in the terminal's font
|
|
struct CellMetrics {
|
|
let font: UIFont
|
|
let width: CGFloat
|
|
let height: CGFloat
|
|
|
|
/// the monospaced system font at [fontSize], and its "M"'s width and line height rounded up
|
|
init(fontSize: Double) {
|
|
font = UIFont.monospacedSystemFont(ofSize: fontSize, weight: .regular)
|
|
width = ceil(("M" as NSString).size(withAttributes: [.font: font]).width)
|
|
height = ceil(font.lineHeight)
|
|
}
|
|
|
|
/// a cell as given
|
|
private init(font: UIFont, width: CGFloat, height: CGFloat) {
|
|
self.font = font
|
|
self.width = width
|
|
self.height = height
|
|
}
|
|
|
|
/// these cells in an 8-wide glyph's shape [rows] high: square for 8x8, twice as tall as wide for 8x16
|
|
func shaped(rows: Int) -> CellMetrics { CellMetrics(font: font, width: width, height: width * CGFloat(rows) / 8) }
|
|
|
|
/// the font size whose cells put [columns] across [width]
|
|
init(columns: Int, across width: CGFloat) {
|
|
let probe = UIFont.monospacedSystemFont(ofSize: 100, weight: .regular)
|
|
let per = ("M" as NSString).size(withAttributes: [.font: probe]).width / 100
|
|
self.init(fontSize: max(4, Double(width / (CGFloat(columns) * per)) - 0.01))
|
|
}
|
|
}
|
|
|
|
/// the screen: each row's runs, their backgrounds, their text, the cursor
|
|
private struct ScreenCanvas: View {
|
|
@ObservedObject var model: TerminalModel
|
|
/// (read here so the canvas redraws with each change)
|
|
let revision: Int64
|
|
let cell: CellMetrics
|
|
|
|
var body: some View {
|
|
Canvas { ctx, _ in
|
|
let regular = Font(cell.font as CTFont)
|
|
let bold = Font(UIFont.monospacedSystemFont(ofSize: cell.font.pointSize, weight: .bold) as CTFont)
|
|
for row in 0..<model.rows {
|
|
let y = CGFloat(row) * cell.height
|
|
// a line of double width or height: its first half's cells drawn twice the size
|
|
// (only the top or bottom half of a double-height one shows on its row)
|
|
let size = Int(model.bridge.lineSize(row: Int32(row)))
|
|
var ctx = ctx
|
|
if size != 0 {
|
|
ctx.clip(to: Path(CGRect(x: 0, y: y, width: .greatestFiniteMagnitude, height: cell.height)))
|
|
ctx.translateBy(x: 0, y: size == 3 ? y - cell.height : y)
|
|
ctx.scaleBy(x: 2, y: size == 1 ? 1 : 2)
|
|
ctx.translateBy(x: 0, y: -y)
|
|
}
|
|
for run in model.runs(row) {
|
|
let x = CGFloat(run.col) * cell.width
|
|
let w = CGFloat(run.cells) * cell.width
|
|
ctx.fill(Path(CGRect(x: x, y: y, width: w, height: cell.height)), with: .color(Color(argb: run.bg)))
|
|
if run.text.trimmingCharacters(in: .whitespaces).isEmpty { continue }
|
|
if model.bridge.bitmapGlyphs || (model.bridge.softGlyphs && run.text.unicodeScalars.contains { $0.value >= 0xF0000 }) {
|
|
drawGlyphs(ctx, run, y: y)
|
|
continue
|
|
}
|
|
var t = Text(run.text).font(run.bold ? bold : regular).foregroundColor(Color(argb: run.fg))
|
|
if run.italic { t = t.italic() }
|
|
if run.underline { t = t.underline() }
|
|
if run.strike { t = t.strikethrough() }
|
|
ctx.draw(t, at: CGPoint(x: x, y: y), anchor: .topLeading)
|
|
}
|
|
}
|
|
if model.bridge.cursorVisible {
|
|
// (twice as wide on a line of double width)
|
|
let wide: CGFloat = model.bridge.lineSize(row: model.bridge.cursorY) != 0 ? 2 : 1
|
|
let r = CGRect(x: CGFloat(model.bridge.cursorX) * cell.width * wide, y: CGFloat(model.bridge.cursorY) * cell.height,
|
|
width: cell.width, height: cell.height)
|
|
ctx.fill(Path(r), with: .color(Color(argb: model.bridge.cursorColor).opacity(0.6)))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
extension ScreenCanvas {
|
|
/// a run's characters one cell each from the profile's bitmap glyphs (a pixel row's runs as one rectangle), the font's for one it hasn't
|
|
func drawGlyphs(_ ctx: GraphicsContext, _ run: StyledRun, y: CGFloat) {
|
|
let color = GraphicsContext.Shading.color(Color(argb: run.fg))
|
|
for (i, ch) in run.text.unicodeScalars.enumerated() {
|
|
let x = CGFloat(Int(run.col) + i) * cell.width
|
|
guard let glyph = model.glyph(ch.value) else {
|
|
if ch != " " { ctx.draw(Text(String(ch)).font(Font(cell.font as CTFont)).foregroundColor(Color(argb: run.fg)), at: CGPoint(x: x, y: y), anchor: .topLeading) }
|
|
continue
|
|
}
|
|
var path = Path()
|
|
let gw = glyph.width
|
|
let high = 1 << (gw - 1)
|
|
for (r, bits) in glyph.rows.enumerated() where bits != 0 {
|
|
var c = 0
|
|
while c < gw {
|
|
if bits & (high >> c) == 0 { c += 1; continue }
|
|
let start = c
|
|
while c < gw && bits & (high >> c) != 0 { c += 1 }
|
|
let h = CGFloat(glyph.rows.count)
|
|
let w = CGFloat(gw)
|
|
path.addRect(CGRect(x: x + CGFloat(start) * cell.width / w, y: y + CGFloat(r) * cell.height / h,
|
|
width: CGFloat(c - start) * cell.width / w, height: cell.height / h))
|
|
}
|
|
}
|
|
ctx.fill(path, with: color)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// the keys a phone's keyboard lacks, as the profile's key bar has them
|
|
/// (the shared Kotlin's pages): Ctrl and Alt sticky, the last key the next page
|
|
private struct ExtraKeys: View {
|
|
@ObservedObject var model: TerminalModel
|
|
@State private var page = 0
|
|
|
|
var body: some View {
|
|
let pages = Int(model.bridge.keyBarPages)
|
|
let p = pages == 0 ? 0 : page % pages
|
|
let rows = model.bridge.keyBarRows(page: Int32(p))
|
|
VStack(spacing: 4) {
|
|
ForEach(Array(rows.enumerated()), id: \.offset) { r, labels in
|
|
HStack(spacing: 4) {
|
|
ForEach(Array(labels.enumerated()), id: \.offset) { c, label in
|
|
key(label, page: p, row: r, col: c, pages: pages)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.padding(.horizontal, 6)
|
|
.padding(.vertical, 4)
|
|
.background(.bar)
|
|
}
|
|
|
|
/// the key at ([row], [col]): Ctrl or Alt latched, the next page, or sent
|
|
private func key(_ label: String, page p: Int, row: Int, col: Int, pages: Int) -> some View {
|
|
let action = model.bridge.keyBarAction(page: Int32(p), row: Int32(row), col: Int32(col))
|
|
let latched = (action == "ctrl" && model.ctrl) || (action == "alt" && model.alt) || (action == "next" && p != 0)
|
|
// (a colour key shows its colour)
|
|
let swatch = model.bridge.keyBarColor(page: Int32(p), row: Int32(row), col: Int32(col))
|
|
return Button {
|
|
switch action {
|
|
case "ctrl": model.ctrl.toggle()
|
|
case "alt": model.alt.toggle()
|
|
case "next": page = (p + 1) % max(pages, 1)
|
|
default: model.barKey(page: p, row: row, col: col)
|
|
}
|
|
} label: {
|
|
Text(label).font(.system(size: 13, design: .monospaced)).lineLimit(1)
|
|
.minimumScaleFactor(0.6).frame(maxWidth: .infinity, minHeight: 30)
|
|
}
|
|
.buttonStyle(.bordered)
|
|
.tint(swatch != 0 ? Color(argb: swatch) : latched ? .accentColor : .secondary)
|
|
}
|
|
}
|
|
|
|
/// a view the keyboard types into: its text and keys to the terminal
|
|
/// (UIKeyInput), the hardware keyboard's arrows and Esc too
|
|
struct KeyInput: UIViewRepresentable {
|
|
let model: TerminalModel
|
|
@Binding var focused: Bool
|
|
|
|
/// the view, typing into [model]
|
|
func makeUIView(context: Context) -> KeyInputView {
|
|
let v = KeyInputView()
|
|
v.model = model
|
|
return v
|
|
}
|
|
|
|
/// the view's model kept, and the keyboard brought up once it's [focused]
|
|
func updateUIView(_ v: KeyInputView, context: Context) {
|
|
v.model = model
|
|
if focused && !v.isFirstResponder {
|
|
DispatchQueue.main.async { v.becomeFirstResponder() }
|
|
}
|
|
}
|
|
}
|
|
|
|
/// (a UIView: on the main actor, as the terminal's model is)
|
|
final class KeyInputView: UIView, UIKeyInput {
|
|
weak var model: TerminalModel?
|
|
|
|
override var canBecomeFirstResponder: Bool { true }
|
|
var hasText: Bool { true }
|
|
|
|
// (a terminal's keyboard: nothing corrected, capitalised or made smart)
|
|
var autocorrectionType: UITextAutocorrectionType = .no
|
|
var autocapitalizationType: UITextAutocapitalizationType = .none
|
|
var spellCheckingType: UITextSpellCheckingType = .no
|
|
var smartQuotesType: UITextSmartQuotesType = .no
|
|
var smartDashesType: UITextSmartDashesType = .no
|
|
var smartInsertDeleteType: UITextSmartInsertDeleteType = .no
|
|
var keyboardType: UIKeyboardType = .asciiCapable
|
|
|
|
/// the keyboard's text to the terminal
|
|
func insertText(_ text: String) {
|
|
model?.typed(text)
|
|
}
|
|
|
|
/// the keyboard's delete as the terminal's BACKSPACE
|
|
func deleteBackward() {
|
|
model?.key("BACKSPACE")
|
|
}
|
|
|
|
override var keyCommands: [UIKeyCommand]? {
|
|
[
|
|
(UIKeyCommand.inputUpArrow, "UP"), (UIKeyCommand.inputDownArrow, "DOWN"),
|
|
(UIKeyCommand.inputLeftArrow, "LEFT"), (UIKeyCommand.inputRightArrow, "RIGHT"),
|
|
(UIKeyCommand.inputEscape, "ESCAPE"), ("\t", "TAB"),
|
|
].map { input, name in
|
|
let c = UIKeyCommand(input: input, modifierFlags: [], action: #selector(keyCommand(_:)))
|
|
c.wantsPriorityOverSystemBehavior = true
|
|
c.title = name
|
|
return c
|
|
}
|
|
}
|
|
|
|
/// a hardware key (named in its command's title) to the terminal
|
|
@objc private func keyCommand(_ c: UIKeyCommand) {
|
|
guard let name = c.title.isEmpty ? nil : c.title else { return }
|
|
model?.key(name)
|
|
}
|
|
}
|