Skip to content

Commit

Permalink
Specify variadic arguments for ffm ioctl call (#240)
Browse files Browse the repository at this point in the history
Fixes  #238
  • Loading branch information
ajalt authored Oct 29, 2024
1 parent aab4a28 commit 45baa5b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ internal abstract class MethodHandlesHolder(
protected fun handle(
resLayout: MemoryLayout,
vararg argLayouts: MemoryLayout,
linkerOptions: Array<Linker.Option> = emptyArray(),
) =
PropertyDelegateProvider<MethodHandlesHolder, ReadOnlyProperty<Any?, MethodHandle>> { _, property ->
val name = property.name
Expand All @@ -160,7 +161,8 @@ internal abstract class MethodHandlesHolder(
.map {
linker.downcallHandle(
it,
FunctionDescriptor.of(resLayout, *argLayouts)
FunctionDescriptor.of(resLayout, *argLayouts),
*linkerOptions
)
}
.orElseThrow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.ajalt.mordant.terminal.terminalinterface.ffm
import com.github.ajalt.mordant.rendering.Size
import com.github.ajalt.mordant.terminal.terminalinterface.TerminalInterfaceJvmPosix
import java.lang.foreign.Arena
import java.lang.foreign.Linker
import java.lang.foreign.MemorySegment

@Suppress("ClassName", "PropertyName", "SpellCheckingInspection")
Expand Down Expand Up @@ -51,9 +52,13 @@ private class LinuxCLibrary {

object MethodHandles : MethodHandlesHolder() {
val isatty by handle(Layouts.INT, Layouts.INT)
val ioctl by handle(Layouts.INT, Layouts.INT, Layouts.LONG, Layouts.POINTER)
val tcgetattr by handle(Layouts.INT, Layouts.INT, Layouts.POINTER)
val tcsetattr by handle(Layouts.INT, Layouts.INT, Layouts.INT, Layouts.POINTER)
val ioctl by handle(
Layouts.INT, Layouts.INT, Layouts.LONG, Layouts.POINTER, linkerOptions = arrayOf(
Linker.Option.firstVariadicArg(2)
)
)
}

fun isatty(fd: Int): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.ajalt.mordant.terminal.terminalinterface.ffm
import com.github.ajalt.mordant.rendering.Size
import com.github.ajalt.mordant.terminal.terminalinterface.TerminalInterfaceJvmPosix
import java.lang.foreign.Arena
import java.lang.foreign.Linker
import java.lang.foreign.MemorySegment

@Suppress("ClassName", "PropertyName", "SpellCheckingInspection")
Expand Down Expand Up @@ -43,9 +44,13 @@ private class MacosCLibrary {

object MethodHandles : MethodHandlesHolder() {
val isatty by handle(Layouts.INT, Layouts.INT)
val ioctl by handle(Layouts.INT, Layouts.INT, Layouts.LONG, Layouts.POINTER)
val tcgetattr by handle(Layouts.INT, Layouts.INT, Layouts.POINTER)
val tcsetattr by handle(Layouts.INT, Layouts.INT, Layouts.INT, Layouts.POINTER)
val ioctl by handle(
Layouts.INT, Layouts.INT, Layouts.LONG, Layouts.POINTER, linkerOptions = arrayOf(
Linker.Option.firstVariadicArg(2)
)
)
}

fun isatty(fd: Int): Boolean {
Expand All @@ -69,10 +74,7 @@ internal class TerminalInterfaceFfmMacos : TerminalInterfaceJvmPosix() {
override val termiosConstants: TermiosConstants get() = MacosTermiosConstants

private companion object {
val TIOCGWINSZ = when (System.getProperty("os.arch")) {
"x86", "amd64" -> 0x00005413
else -> 0x40087468
}
const val TIOCGWINSZ = 0x40087468
const val TCSADRAIN: Int = 0x1
}

Expand Down

0 comments on commit 45baa5b

Please sign in to comment.