diff --git a/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/syscalls/jna/SyscallHandler.jna.linux.kt b/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/syscalls/jna/SyscallHandler.jna.linux.kt index f531f66d2..2d3997dd5 100644 --- a/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/syscalls/jna/SyscallHandler.jna.linux.kt +++ b/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/syscalls/jna/SyscallHandler.jna.linux.kt @@ -89,23 +89,18 @@ internal object SyscallHandlerJnaLinux : SyscallHandlerJvmPosix() { oflag = termios.c_oflag.toUInt(), cflag = termios.c_cflag.toUInt(), lflag = termios.c_lflag.toUInt(), - cline = termios.c_line, cc = termios.c_cc.copyOf(), - ispeed = termios.c_ispeed.toUInt(), - ospeed = termios.c_ospeed.toUInt(), ) } override fun setStdinTermios(termios: Termios) { val nativeTermios = PosixLibC.termios() + libC.tcgetattr(STDIN_FILENO, nativeTermios) nativeTermios.c_iflag = termios.iflag.toInt() nativeTermios.c_oflag = termios.oflag.toInt() nativeTermios.c_cflag = termios.cflag.toInt() nativeTermios.c_lflag = termios.lflag.toInt() - nativeTermios.c_line = termios.cline termios.cc.copyInto(nativeTermios.c_cc) - nativeTermios.c_ispeed = termios.ispeed.toInt() - nativeTermios.c_ospeed = termios.ospeed.toInt() libC.tcsetattr(STDIN_FILENO, TCSADRAIN, nativeTermios) } } diff --git a/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/syscalls/jna/SyscallHandler.jna.macos.kt b/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/syscalls/jna/SyscallHandler.jna.macos.kt index 85a93fa82..a872ad489 100644 --- a/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/syscalls/jna/SyscallHandler.jna.macos.kt +++ b/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/syscalls/jna/SyscallHandler.jna.macos.kt @@ -46,9 +46,6 @@ private interface MacosLibC : Library { @JvmField var c_lflag: NativeLong = NativeLong() - @JvmField - var c_line: Byte = 0 - @JvmField var c_cc: ByteArray = ByteArray(20) @@ -100,23 +97,18 @@ internal object SyscallHandlerJnaMacos : SyscallHandlerJvmPosix() { oflag = termios.c_oflag.toInt().toUInt(), cflag = termios.c_cflag.toInt().toUInt(), lflag = termios.c_lflag.toInt().toUInt(), - cline = termios.c_line, cc = termios.c_cc.copyOf(), - ispeed = termios.c_ispeed.toInt().toUInt(), - ospeed = termios.c_ospeed.toInt().toUInt(), ) } override fun setStdinTermios(termios: Termios) { val nativeTermios = MacosLibC.termios() + libC.tcgetattr(STDIN_FILENO, nativeTermios) nativeTermios.c_iflag = NativeLong(termios.iflag.toLong()) nativeTermios.c_oflag = NativeLong(termios.oflag.toLong()) nativeTermios.c_cflag = NativeLong(termios.cflag.toLong()) nativeTermios.c_lflag = NativeLong(termios.lflag.toLong()) - nativeTermios.c_line = termios.cline termios.cc.copyInto(nativeTermios.c_cc) - nativeTermios.c_ispeed = NativeLong(termios.ispeed.toLong()) - nativeTermios.c_ospeed = NativeLong(termios.ospeed.toLong()) libC.tcsetattr(STDIN_FILENO, TCSANOW, nativeTermios) } } diff --git a/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/syscalls/nativeimage/SyscallHandler.nativeimage.posix.kt b/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/syscalls/nativeimage/SyscallHandler.nativeimage.posix.kt index 5bca46268..16292e65c 100644 --- a/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/syscalls/nativeimage/SyscallHandler.nativeimage.posix.kt +++ b/mordant/src/jvmMain/kotlin/com/github/ajalt/mordant/internal/syscalls/nativeimage/SyscallHandler.nativeimage.posix.kt @@ -84,7 +84,7 @@ private object PosixLibC { // external fun tcgetattr(fd: Int, termios: termios): Int // // @CFunction("tcsetattr") -// external fun tcsetattr(fd: Int, cmd: Int, termios: termios) +// external fun tcsetattr(fd: Int, cmd: Int, termios: termios): Int } @Platforms(Platform.LINUX::class, Platform.MACOS::class) @@ -119,29 +119,30 @@ internal class SyscallHandlerNativeImagePosix : SyscallHandlerJvmPosix() { persists. */ // val termios = StackValue.get(PosixLibC.termios::class.java) -// if(PosixLibC.tcgetattr(STDIN_FILENO, termios) != 0) return null +// if (PosixLibC.tcgetattr(STDIN_FILENO, termios) != 0) { +// throw RuntimeException("Error reading terminal attributes") +// } // return Termios( // iflag = termios.c_iflag.toUInt(), // oflag = termios.c_oflag.toUInt(), // cflag = termios.c_cflag.toUInt(), // lflag = termios.c_lflag.toUInt(), -// cline = termios.c_line, // cc = ByteArray(PosixLibC.NCCS()) { termios.c_cc.read(it) }, -// ispeed = termios.c_ispeed.toUInt(), -// ospeed = termios.c_ospeed.toUInt(), // ) } override fun setStdinTermios(termios: Termios) { // val nativeTermios = StackValue.get(PosixLibC.termios::class.java) +// if (PosixLibC.tcgetattr(STDIN_FILENO, nativeTermios) != 0) { +// throw RuntimeException("Error reading terminal attributes") +// } // nativeTermios.c_iflag = termios.iflag.toInt() // nativeTermios.c_oflag = termios.oflag.toInt() // nativeTermios.c_cflag = termios.cflag.toInt() // nativeTermios.c_lflag = termios.lflag.toInt() -// nativeTermios.c_line = termios.cline // termios.cc.forEachIndexed { i, b -> nativeTermios.c_cc.write(i, b) } -// nativeTermios.c_ispeed = termios.ispeed.toInt() -// nativeTermios.c_ospeed = termios.ospeed.toInt() -// PosixLibC.tcsetattr(STDIN_FILENO, PosixLibC.TCSADRAIN(), nativeTermios) +// if (PosixLibC.tcsetattr(STDIN_FILENO, PosixLibC.TCSADRAIN(), nativeTermios) != 0) { +// throw RuntimeException("Error setting terminal attributes") +// } } } diff --git a/mordant/src/nonJsMain/kotlin/com/github/ajalt/mordant/internal/syscalls/SyscallHandler.posix.kt b/mordant/src/nonJsMain/kotlin/com/github/ajalt/mordant/internal/syscalls/SyscallHandler.posix.kt index 9ef1634c1..19e518c77 100644 --- a/mordant/src/nonJsMain/kotlin/com/github/ajalt/mordant/internal/syscalls/SyscallHandler.posix.kt +++ b/mordant/src/nonJsMain/kotlin/com/github/ajalt/mordant/internal/syscalls/SyscallHandler.posix.kt @@ -143,10 +143,7 @@ internal abstract class SyscallHandlerPosix : SyscallHandler { val oflag: UInt, val cflag: UInt, val lflag: UInt, - val cline: Byte, val cc: ByteArray, - val ispeed: UInt, - val ospeed: UInt, ) abstract fun getStdinTermios(): Termios @@ -169,13 +166,10 @@ internal abstract class SyscallHandlerPosix : SyscallHandler { oflag = orig.oflag, cflag = orig.cflag or CS8, lflag = orig.lflag and (ECHO or ICANON or IEXTEN or ISIG).inv(), - cline = orig.cline, cc = orig.cc.copyOf().also { it[VMIN] = 0 // min wait time on read it[VTIME] = 1 // max wait time on read, in 10ths of a second }, - ispeed = orig.ispeed, - ospeed = orig.ospeed, ) setStdinTermios(new) when (mouseTracking) { diff --git a/mordant/src/posixMain/kotlin/com/github/ajalt/mordant/internal/syscalls/SyscallHanlder.native.posix.kt b/mordant/src/posixMain/kotlin/com/github/ajalt/mordant/internal/syscalls/SyscallHanlder.native.posix.kt index 1b6ad2fc0..75929cfcc 100644 --- a/mordant/src/posixMain/kotlin/com/github/ajalt/mordant/internal/syscalls/SyscallHanlder.native.posix.kt +++ b/mordant/src/posixMain/kotlin/com/github/ajalt/mordant/internal/syscalls/SyscallHanlder.native.posix.kt @@ -30,24 +30,25 @@ internal object SyscallHandlerNativePosix : SyscallHandlerPosix() { oflag = termios.c_oflag.convert(), cflag = termios.c_cflag.convert(), lflag = termios.c_lflag.convert(), - cline = termios.c_line.convert(), cc = ByteArray(NCCS) { termios.c_cc[it].convert() }, - ispeed = termios.c_ispeed.convert(), - ospeed = termios.c_ospeed.convert(), ) } override fun setStdinTermios(termios: Termios): Unit = memScoped { val nativeTermios = alloc() + // different platforms have different fields in termios, so we need to read the current + // struct before we set the fields we carre about. + if (tcgetattr(STDIN_FILENO, nativeTermios.ptr) != 0) { + throw RuntimeException("Error reading terminal attributes") + } nativeTermios.c_iflag = termios.iflag.convert() nativeTermios.c_oflag = termios.oflag.convert() nativeTermios.c_cflag = termios.cflag.convert() nativeTermios.c_lflag = termios.lflag.convert() - nativeTermios.c_line = termios.cline.convert() repeat(NCCS) { nativeTermios.c_cc[it] = termios.cc[it].convert() } - nativeTermios.c_ispeed = termios.ispeed.convert() - nativeTermios.c_ospeed = termios.ospeed.convert() - tcsetattr(platform.posix.STDIN_FILENO, TCSADRAIN, nativeTermios.ptr) + if (tcsetattr(platform.posix.STDIN_FILENO, TCSADRAIN, nativeTermios.ptr) != 0) { + throw RuntimeException("Error setting terminal attributes") + } } override fun readRawByte(t0: ComparableTimeMark, timeout: Duration): Char = memScoped {