mirror of
https://github.com/D-Modem/app.git
synced 2026-11-28 17:38:05 -08:00
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
This commit is contained in:
20 files changed
+1002
-4
No files matched your search
@@ -0,0 +1,101 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.terminal
|
||||
|
||||
/**
|
||||
* The terminal as the iOS app's Swift has it: a [TerminalEmulator] and its
|
||||
* keys behind a few calls that pass plain values. Octets go both ways as
|
||||
* Latin-1 strings (one character for each octet: Swift's Data to and from
|
||||
* them is exact and cheap), the screen as [StyledRun]s.
|
||||
*/
|
||||
class TerminalBridge(cols: Int, rows: Int) {
|
||||
private val replies = StringBuilder()
|
||||
private val lock = kotlinx.atomicfu.locks.SynchronizedObject()
|
||||
|
||||
val emulator = TerminalEmulator(cols, rows, output = { b ->
|
||||
kotlinx.atomicfu.locks.synchronized(lock) { for (x in b) replies.append((x.toInt() and 0xff).toChar()) }
|
||||
})
|
||||
private val keys = KeyEncoder()
|
||||
private var look = RunLook()
|
||||
|
||||
/** the window's title (OSC 0/2), as the remote last set it */
|
||||
var title = ""
|
||||
private set
|
||||
|
||||
init {
|
||||
emulator.listener = object : TerminalEmulator.Listener {
|
||||
override fun onTitle(title: String) { this@TerminalBridge.title = title }
|
||||
}
|
||||
}
|
||||
|
||||
val cols: Int get() = emulator.cols
|
||||
val rows: Int get() = emulator.rows
|
||||
val cursorX: Int get() = emulator.cursorX
|
||||
val cursorY: Int get() = emulator.cursorY
|
||||
val cursorVisible: Boolean get() = emulator.cursorVisible
|
||||
/** changes with each change to the screen: redraw when it does */
|
||||
val revision: Long get() = emulator.revision
|
||||
|
||||
/** the remote's octets (Latin-1, one character each) onto the screen */
|
||||
fun feed(latin1: String) {
|
||||
val b = ByteArray(latin1.length) { latin1[it].code.toByte() }
|
||||
emulator.append(b, b.size)
|
||||
}
|
||||
|
||||
/** what the terminal answers the remote (cursor reports, its identity …), as octets in Latin-1; taken */
|
||||
fun takeReplies(): String = kotlinx.atomicfu.locks.synchronized(lock) {
|
||||
replies.toString().also { replies.setLength(0) }
|
||||
}
|
||||
|
||||
/** a row (0: the screen's top) as runs */
|
||||
fun runs(row: Int): List<StyledRun> = emulator.runs(row, look)
|
||||
|
||||
/** the screen's background, the theme's (ARGB) */
|
||||
val background: Int get() = look.theme.bgArgb
|
||||
val cursorColor: Int get() = look.theme.cursorArgb
|
||||
|
||||
fun resize(cols: Int, rows: Int) = emulator.resize(cols, rows)
|
||||
|
||||
/** the theme ([TermTheme.ALL]'s ids), bold as bright, and the character set (UTF8, CP437, LATIN1) */
|
||||
fun setLook(themeId: String, boldBright: Boolean, iceColors: Boolean, charset: String) {
|
||||
look = RunLook(TermTheme.byId(themeId), boldBright, iceColors)
|
||||
emulator.decoder.charset = TermCharset.entries.firstOrNull { it.name == charset } ?: TermCharset.UTF8
|
||||
}
|
||||
|
||||
/** text typed: its octets in the character set (Latin-1, one character each); a newline as Enter */
|
||||
fun typed(text: String, ctrl: Boolean = false, alt: Boolean = false): String {
|
||||
val out = StringBuilder()
|
||||
var i = 0
|
||||
while (i < text.length) {
|
||||
val c = text[i]
|
||||
val cp = if (c.isHighSurrogate() && i + 1 < text.length) {
|
||||
i++
|
||||
0x10000 + ((c.code - 0xd800) shl 10) + (text[i].code - 0xdc00)
|
||||
} else {
|
||||
c.code
|
||||
}
|
||||
i++
|
||||
if (cp == '\n'.code || cp == '\r'.code) {
|
||||
out.append(keys.key(Key.ENTER, emulator.appCursorKeys, alt = alt))
|
||||
continue
|
||||
}
|
||||
out.append(keys.char(cp, alt, ctrl))
|
||||
}
|
||||
return octets(out.toString())
|
||||
}
|
||||
|
||||
/** a key that isn't a character ([Key]'s names: UP, ESCAPE, F1 …) as octets in Latin-1; "" for no such key */
|
||||
fun key(name: String, shift: Boolean = false, alt: Boolean = false, ctrl: Boolean = false): String {
|
||||
val k = Key.entries.firstOrNull { it.name == name } ?: return ""
|
||||
if (k == Key.BACKSPACE) return keys.backspace.code.toChar().toString()
|
||||
return octets(keys.key(k, emulator.appCursorKeys, shift, alt, ctrl))
|
||||
}
|
||||
|
||||
/** the screen's text from (row, col) to (row2, col2), for copying */
|
||||
fun text(row: Int, col: Int, row2: Int, col2: Int): String = emulator.text(row, col, row2, col2)
|
||||
|
||||
private fun octets(s: String): String {
|
||||
val b = emulator.decoder.charset.encode(s)
|
||||
return CharArray(b.size) { (b[it].toInt() and 0xff).toChar() }.concatToString()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.terminal
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/** The terminal as Swift has it: octets as Latin-1 strings each way, rows as runs, keys, replies. */
|
||||
class TerminalBridgeTest {
|
||||
@Test
|
||||
fun octetsInScreenOutRepliesBack() {
|
||||
val t = TerminalBridge(20, 4)
|
||||
val r0 = t.revision
|
||||
t.feed("hello\r\nworld")
|
||||
assertTrue(t.revision != r0)
|
||||
assertEquals("hello", t.runs(0).joinToString("") { it.text }.trimEnd())
|
||||
assertEquals(1, t.cursorY)
|
||||
assertEquals(5, t.cursorX)
|
||||
// a cursor position report asked for: the answer to send back
|
||||
t.feed("\u001b[6n")
|
||||
assertEquals("\u001b[2;6R", t.takeReplies())
|
||||
assertEquals("", t.takeReplies())
|
||||
// the title
|
||||
t.feed("\u001b]2;BBS\u0007")
|
||||
assertEquals("BBS", t.title)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun typedAndKeys() {
|
||||
val t = TerminalBridge(20, 4)
|
||||
assertEquals("a\r", t.typed("a\n"))
|
||||
assertEquals("\u0003", t.typed("c", ctrl = true))
|
||||
// UTF-8's octets, one character each
|
||||
assertEquals("é", t.typed("é"))
|
||||
assertEquals("\u001b[A", t.key("UP"))
|
||||
assertEquals("\u007f", t.key("BACKSPACE"))
|
||||
assertEquals("", t.key("NOPE"))
|
||||
// application cursor keys, as the remote asks
|
||||
t.feed("\u001b[?1h")
|
||||
assertEquals("\u001bOA", t.key("UP"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun cp437ForTheBbs() {
|
||||
val t = TerminalBridge(10, 2)
|
||||
t.setLook("vga", boldBright = true, iceColors = false, charset = "CP437")
|
||||
// 0xdb: CP437's full block
|
||||
t.feed("Û")
|
||||
assertEquals("█", t.runs(0)[0].text.take(1))
|
||||
assertEquals("Û", t.typed("█"))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user