Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify variadic arguments for ffm ioctl call #240

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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