- M0: libghostty SSH 终端(渲染/输入/连接)+ 白屏修复(OutputGate) + 会话状态机/自动重连 - M1: tsnet 用户态组网 + SSH-over-tsnet(fd 桥),shell 级真机验证;R5(Go+gvisor+C+++Swift 同进程) retire - M1.5: tmux -CC 原生 tab(MVP) - 结构: packages/(TXCore·TXTransport), apps/TerminalX, vendor/(libghostty-spm/libssh2/mbedtls/tsnet-bridge), artifacts/ - 文档: CLAUDE.md + docs/HANDOFF.md(新会话入口) - 环境: 认证代理→依赖 vendor 本地化;Go 在 ~/.local/go;仅模拟器/未签名 - 待续: M2 mosh, tmux 多 pane, M4 安全(host key/SE) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
142 lines
3.8 KiB
Swift
142 lines
3.8 KiB
Swift
import Foundation
|
|
import GhosttyKit
|
|
|
|
/// Serializes host output while keeping the raw surface alive for each C call.
|
|
final class InMemoryTerminalSurfaceAccess: @unchecked Sendable {
|
|
typealias Write = @Sendable (ghostty_surface_t, Data) -> Void
|
|
typealias ProcessExit = @Sendable (ghostty_surface_t, UInt32, UInt64) -> Void
|
|
|
|
private let condition = NSCondition()
|
|
private let outputQueue = DispatchQueue(
|
|
label: "com.lakr233.libghostty-spm.in-memory-output",
|
|
qos: .userInitiated
|
|
)
|
|
private let write: Write
|
|
private let processExit: ProcessExit
|
|
|
|
private var surface: ghostty_surface_t?
|
|
/// Invalidates work that was enqueued for a surface that has been replaced.
|
|
private var generation: UInt64 = 0
|
|
/// Prevents the caller from freeing a surface while a C operation uses it.
|
|
private var activeOperations = 0
|
|
|
|
init(
|
|
write: @escaping Write,
|
|
processExit: @escaping ProcessExit
|
|
) {
|
|
self.write = write
|
|
self.processExit = processExit
|
|
}
|
|
|
|
func setSurface(_ surface: ghostty_surface_t?) {
|
|
condition.lock()
|
|
generation &+= 1
|
|
self.surface = nil
|
|
waitForActiveOperations()
|
|
self.surface = surface
|
|
condition.unlock()
|
|
}
|
|
|
|
@discardableResult
|
|
func clearSurface(ifMatches expectedSurface: ghostty_surface_t?) -> Bool {
|
|
condition.lock()
|
|
guard surface == expectedSurface else {
|
|
condition.unlock()
|
|
return false
|
|
}
|
|
|
|
generation &+= 1
|
|
surface = nil
|
|
waitForActiveOperations()
|
|
condition.unlock()
|
|
return true
|
|
}
|
|
|
|
var currentSurface: ghostty_surface_t? {
|
|
condition.lock()
|
|
defer { condition.unlock() }
|
|
return surface
|
|
}
|
|
|
|
@discardableResult
|
|
func enqueueWrite(_ data: Data) -> Bool {
|
|
guard let generation = currentGeneration else { return false }
|
|
outputQueue.async { [self] in
|
|
withSurface(generation: generation) { surface in
|
|
write(surface, data)
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
@discardableResult
|
|
func enqueueProcessExit(
|
|
exitCode: UInt32,
|
|
runtimeMilliseconds: UInt64
|
|
) -> Bool {
|
|
guard let generation = currentGeneration else { return false }
|
|
outputQueue.async { [self] in
|
|
withSurface(generation: generation) { surface in
|
|
processExit(surface, exitCode, runtimeMilliseconds)
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func withCurrentSurface<Result>(
|
|
_ operation: (ghostty_surface_t) -> Result
|
|
) -> Result? {
|
|
condition.lock()
|
|
guard let surface else {
|
|
condition.unlock()
|
|
return nil
|
|
}
|
|
activeOperations += 1
|
|
condition.unlock()
|
|
|
|
defer { finishOperation() }
|
|
return operation(surface)
|
|
}
|
|
|
|
func waitForPendingOutput() {
|
|
outputQueue.sync {}
|
|
}
|
|
|
|
private var currentGeneration: UInt64? {
|
|
condition.lock()
|
|
defer { condition.unlock() }
|
|
return surface == nil ? nil : generation
|
|
}
|
|
|
|
private func withSurface(
|
|
generation expectedGeneration: UInt64,
|
|
_ operation: (ghostty_surface_t) -> Void
|
|
) {
|
|
condition.lock()
|
|
guard generation == expectedGeneration, let surface else {
|
|
condition.unlock()
|
|
return
|
|
}
|
|
activeOperations += 1
|
|
condition.unlock()
|
|
|
|
defer { finishOperation() }
|
|
operation(surface)
|
|
}
|
|
|
|
private func finishOperation() {
|
|
condition.lock()
|
|
activeOperations -= 1
|
|
if activeOperations == 0 {
|
|
condition.broadcast()
|
|
}
|
|
condition.unlock()
|
|
}
|
|
|
|
private func waitForActiveOperations() {
|
|
while activeOperations > 0 {
|
|
condition.wait()
|
|
}
|
|
}
|
|
}
|