mirror of
https://github.com/D-Modem/app.git
synced 2026-11-29 01:48:05 -08:00
* Atari ST terminals, and pure white kept as a colour The Atari ST's TOS VT52 in medium resolution (80x25, four colours) and low (40x25, sixteen): ESC b and ESC c pens, reverse, wrap, the cursor saved and hidden, the extra erases, line insert and delete, and the ST's character set. Vt52Interpreter gains a character hook and an argument hook for it; the machine size and palette set-up is shared with the byte-oriented machines (useMachine, releaseMachine). colorOverride's "none" was -1, which is also opaque white, so a machine's pure white (the ST's screen, Viewdata's and the Minitel's white) fell back to the theme's colour. "None" is now NO_COLOR (transparent black), and overrides are kept opaque. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY * Avatar (AVT/0 and AVT/0+) over ANSI-BBS FidoNet's compact codes taken in ANSI-BBS's intercept hook: ^L, ^Y's repeat, ^V's PC attributes (DLE-quoted too), blink, cursor moves and place, clear to the line's end, and AVT/0+'s insert mode, area scrolls, clears and fills (through Rectangles), character delete and pattern repeat. ANSI, music and the rest of ANSI-BBS work underneath. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY * Amiga ANSI ANSI-BBS in Latin-1, drawn 8x8 (Unscii's glyphs, Topaz's size; the generator now adds Latin-1's to the 8x8 set) in colours from the Amiga's 12-bit palette. An ANSI-BBS profile can now carry its own sixteen colours (AnsiBbsProfile.colors), which its interpreter puts over the theme's and takes back off when another profile takes over. The per-profile render test now uses the profile's character set, as connecting does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY * RIPscrip 1.54 ANSI-BBS whose lines starting !| (or ^A|, ^B|) are RIP's commands on a 640x350 EGA screen over an 80x43 text window: lines in their styles and thickness, rectangles, bars, circles, ellipses, arcs, pies, Beziers, polygons and polylines, flood fills, BGI's fill patterns and the host's, text, the palette, the viewport and text window, the clipboard, text blocks, and mouse regions and buttons. ESC [ ! answers RIP's version, and ESC [ 1 ! and 2 ! turn it off and on. Line continuation and the CR LF ending a RIP line are the line's own. Raster gains ellipses, patterned fills, flood fill, a clip rectangle and grab and put, and draws shrunk glyphs with their rows merged (a thin stroke was lost). Taps: an interpreter can take a tap at a point on the screen (TerminalEmulator.tap); Android's view and the iOS screen (through TerminalBridge.tap) send what it gives, so RIP's buttons work. 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>
184 lines
7.3 KiB
Swift
184 lines
7.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
|
|
/// 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
|
|
}
|
|
|
|
/// a tap at ([fx], [fy]), fractions of the screen from its top left: what the terminal sends for it (a RIP button's text), if anything
|
|
func tap(fx: CGFloat, fy: CGFloat) {
|
|
out(bridge.tap(fx: Double(fx), fy: Double(fy)))
|
|
}
|
|
|
|
/// 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)) }
|
|
|
|
/// a row's cells that show images (sixels, a graphics terminal's picture), asked for from row 0 down each time the screen's drawn
|
|
func imageRuns(_ row: Int) -> [ImageRun] { bridge.imageRuns(row: Int32(row)) }
|
|
|
|
/// the images as CGImages, made once each (by the image's id)
|
|
private var imageCache: [Int64: CGImage] = [:]
|
|
|
|
/// [r]'s image as a CGImage, made the first time it's asked for; nil if it can't be
|
|
func cgImage(_ r: ImageRun) -> CGImage? {
|
|
if let c = imageCache[r.imageId] { return c }
|
|
let s = bridge.imagePixels(imageId: r.imageId)
|
|
guard !s.isEmpty, let d = s.data(using: .isoLatin1), let provider = CGDataProvider(data: d as CFData) else { return nil }
|
|
let w = Int(r.width)
|
|
let h = Int(r.height)
|
|
guard let img = CGImage(width: w, height: h, bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: w * 4,
|
|
space: CGColorSpaceCreateDeviceRGB(),
|
|
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue),
|
|
provider: provider, decode: nil, shouldInterpolate: false, intent: .defaultIntent)
|
|
else { return nil }
|
|
// (a picture's redrawn as a new image each time: the old ones let go)
|
|
if imageCache.count >= 32 { imageCache.removeAll() }
|
|
imageCache[r.imageId] = img
|
|
return img
|
|
}
|
|
|
|
/// 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)
|
|
}
|
|
}
|