-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to a dedicated "Jni" type (#644)
This will aid in future migration to an abstraction which can better support Windows.
- Loading branch information
1 parent
c5f4fb0
commit 387bbff
Showing
4 changed files
with
120 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
mosaic-terminal/src/jvmMain/kotlin/com/jakewharton/mosaic/terminal/Jni.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.jakewharton.mosaic.terminal | ||
|
||
import java.io.IOException | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.nio.file.StandardCopyOption.REPLACE_EXISTING | ||
import java.util.Locale.US | ||
|
||
// TODO @JvmSynthetic https://youtrack.jetbrains.com/issue/KT-24981 | ||
internal object Jni { | ||
init { | ||
loadNativeLibrary("mosaic") | ||
} | ||
|
||
@JvmStatic | ||
external fun enterRawMode(): Long | ||
|
||
@JvmStatic | ||
external fun exitRawMode(savedConfig: Long) | ||
|
||
@JvmStatic | ||
external fun stdinReaderInit(): Long | ||
|
||
@JvmStatic | ||
external fun stdinReaderRead( | ||
reader: Long, | ||
buffer: ByteArray, | ||
offset: Int, | ||
count: Int, | ||
): Int | ||
|
||
@JvmStatic | ||
external fun stdinReaderReadWithTimeout( | ||
reader: Long, | ||
buffer: ByteArray, | ||
offset: Int, | ||
count: Int, | ||
timeoutMillis: Int, | ||
): Int | ||
|
||
@JvmStatic | ||
external fun stdinReaderInterrupt(reader: Long) | ||
|
||
@JvmStatic | ||
external fun stdinReaderFree(reader: Long) | ||
|
||
@JvmStatic | ||
external fun stdinWriterInit(): Long | ||
|
||
@JvmStatic | ||
external fun stdinWriterGetReader(writer: Long): Long | ||
|
||
@JvmStatic | ||
external fun stdinWriterWrite(writer: Long, buffer: ByteArray) | ||
|
||
@JvmStatic | ||
external fun stdinWriterFree(writer: Long) | ||
|
||
@Suppress( | ||
// Only loading from our own JAR contents. | ||
"UnsafeDynamicallyLoadedCode", | ||
// Preserving copy/paste! | ||
"SameParameterValue", | ||
) | ||
private fun loadNativeLibrary(name: String) { | ||
val osName = System.getProperty("os.name").lowercase(US) | ||
val osArch = System.getProperty("os.arch").lowercase(US) | ||
val nativeLibraryJarPath = "/jni/$osArch/" + when { | ||
"linux" in osName -> "lib$name.so" | ||
"mac" in osName -> "lib$name.dylib" | ||
"windows" in osName -> "$name.dll" | ||
else -> throw IllegalStateException("Unsupported OS: $osName $osArch") | ||
} | ||
val nativeLibraryUrl = Tty::class.java.getResource(nativeLibraryJarPath) | ||
?: throw IllegalStateException("Unable to read $nativeLibraryJarPath from JAR") | ||
val nativeLibraryFile: Path | ||
try { | ||
nativeLibraryFile = Files.createTempFile(name, null) | ||
|
||
// File-based deleteOnExit() uses a special internal shutdown hook that always runs last. | ||
nativeLibraryFile.toFile().deleteOnExit() | ||
nativeLibraryUrl.openStream().use { nativeLibrary -> | ||
Files.copy(nativeLibrary, nativeLibraryFile, REPLACE_EXISTING) | ||
} | ||
} catch (e: IOException) { | ||
throw RuntimeException("Unable to extract native library from JAR", e) | ||
} | ||
System.load(nativeLibraryFile.toAbsolutePath().toString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
mosaic-terminal/src/jvmMain/resources/META-INF/proguard/com.jakewharton.mosaic-terminal.pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# Keep native method names which are used by the consumer. Our JNI code only creates JDK types and | ||
# only uses Java built-in types across the boundary. | ||
-keepclasseswithmembernames class com.jakewharton.mosaic.terminal.** { | ||
# Note: Our JNI code only creates JDK types and only uses Java built-in types across the boundary. | ||
-keep,allowoptimization class com.jakewharton.mosaic.terminal.Jni { | ||
native <methods>; | ||
} |