diff --git a/build.sc b/build.sc index cb72b5b..80f568c 100644 --- a/build.sc +++ b/build.sc @@ -20,12 +20,35 @@ object `windows-jni-utils` extends WindowsUtils with HasCSources with JniUtilsPu def windowsJavaHome = sharedWindowsJavaHome() } +object `windows-jni-utils-bootstrap` extends WindowsUtils with JniUtilsPublishModule { + def moduleDeps = Seq( + `windows-jni-utils` + ) +} + +object `windows-jni-utils-lmcoursier` extends WindowsUtils with JniUtilsPublishModule { + def moduleDeps = Seq( + `windows-jni-utils` + ) +} + +object `windows-jni-utils-coursierapi` extends WindowsUtils with JniUtilsPublishModule { + def moduleDeps = Seq( + `windows-jni-utils` + ) +} + object `windows-jni-utils-tests` extends ScalaModule with JniUtilsPublishModule { def scalaVersion = Scala.scala213 def moduleDeps = Seq( `windows-jni-utils` ) object test extends Tests { + def moduleDeps = super.moduleDeps ++ Seq( + `windows-jni-utils-bootstrap`, + `windows-jni-utils-lmcoursier`, + `windows-jni-utils-coursierapi` + ) def ivyDeps = Agg( Deps.utest ) @@ -36,6 +59,24 @@ object `windows-jni-utils-tests` extends ScalaModule with JniUtilsPublishModule // compile these projects to generate or update JNI header files object headers extends Module { object `windows-jni-utils` extends WindowsUtils with GenerateHeaders + object `windows-jni-utils-bootstrap` extends WindowsUtils with GenerateHeaders { + def moduleDeps = Seq( + `windows-jni-utils` + ) + def cDirectory = `windows-jni-utils`.cDirectory() + } + object `windows-jni-utils-lmcoursier` extends WindowsUtils with GenerateHeaders { + def moduleDeps = Seq( + `windows-jni-utils` + ) + def cDirectory = `windows-jni-utils`.cDirectory() + } + object `windows-jni-utils-coursierapi` extends WindowsUtils with GenerateHeaders { + def moduleDeps = Seq( + `windows-jni-utils` + ) + def cDirectory = `windows-jni-utils`.cDirectory() + } implicit def millModuleBasePath: define.BasePath = define.BasePath(super.millModuleBasePath.value / os.up) diff --git a/settings.sc b/settings.sc index 8651a85..3f78ead 100644 --- a/settings.sc +++ b/settings.sc @@ -8,13 +8,16 @@ import org.codehaus.plexus.archiver.zip.ZipUnArchiver import scala.concurrent.duration.Duration trait GenerateHeaders extends JavaModule { + def cDirectory = T{ + millSourcePath / "src" / "main" / "c" + } def javacOptions = T{ - val dest = millSourcePath / "src" / "main" / "c" + val dest = cDirectory() Seq("-h", dest.toNIO.toAbsolutePath.toString) } def compile = T{ val res = super.compile() - val dir = millSourcePath / "src" / "main" / "c" + val dir = cDirectory() val headerFiles = Seq(dir).flatMap { dir => if (os.isDir(dir)) os.walk(dir) diff --git a/windows-jni-utils-bootstrap/src/main/java/coursier/bootstrap/launcher/jniutils/NativeCalls.java b/windows-jni-utils-bootstrap/src/main/java/coursier/bootstrap/launcher/jniutils/NativeCalls.java new file mode 100644 index 0000000..75ebd85 --- /dev/null +++ b/windows-jni-utils-bootstrap/src/main/java/coursier/bootstrap/launcher/jniutils/NativeCalls.java @@ -0,0 +1,46 @@ +package coursier.bootstrap.launcher.jniutils; + +import coursier.jniutils.LoadWindowsLibrary; +import coursier.jniutils.NativeApi; + +public final class NativeCalls { + + static native String terminalSizeNative(); + + static native String enableAnsiOutputNative(); + + static native byte[] GetUserEnvironmentVariableNative(byte[] key); + static native byte[] SetUserEnvironmentVariableNative(byte[] key, byte[] value); + static native byte[] DeleteUserEnvironmentVariableNative(byte[] key); + + static native String GetKnownFolderPathNative(String rfid); + + + public static void setup() { + LoadWindowsLibrary.ensureInitialized(); + NativeApi nativeApi = new NativeApi() { + public String terminalSize() { + return terminalSizeNative(); + } + + public String enableAnsiOutput() { + return enableAnsiOutputNative(); + } + + public byte[] GetUserEnvironmentVariable(byte[] key) { + return GetUserEnvironmentVariableNative(key); + } + public byte[] SetUserEnvironmentVariable(byte[] key, byte[] value) { + return SetUserEnvironmentVariableNative(key, value); + } + public byte[] DeleteUserEnvironmentVariable(byte[] key) { + return DeleteUserEnvironmentVariableNative(key); + } + + public String GetKnownFolderPath(String rfid) { + return GetKnownFolderPathNative(rfid); + } + }; + NativeApi.set(nativeApi); + } +} diff --git a/windows-jni-utils-coursierapi/src/main/java/coursierapi/internal/jniutils/NativeCalls.java b/windows-jni-utils-coursierapi/src/main/java/coursierapi/internal/jniutils/NativeCalls.java new file mode 100644 index 0000000..2fea0db --- /dev/null +++ b/windows-jni-utils-coursierapi/src/main/java/coursierapi/internal/jniutils/NativeCalls.java @@ -0,0 +1,46 @@ +package coursierapi.internal.jniutils; + +import coursier.jniutils.LoadWindowsLibrary; +import coursier.jniutils.NativeApi; + +public final class NativeCalls { + + static native String terminalSizeNative(); + + static native String enableAnsiOutputNative(); + + static native byte[] GetUserEnvironmentVariableNative(byte[] key); + static native byte[] SetUserEnvironmentVariableNative(byte[] key, byte[] value); + static native byte[] DeleteUserEnvironmentVariableNative(byte[] key); + + static native String GetKnownFolderPathNative(String rfid); + + + public static void setup() { + LoadWindowsLibrary.ensureInitialized(); + NativeApi nativeApi = new NativeApi() { + public String terminalSize() { + return terminalSizeNative(); + } + + public String enableAnsiOutput() { + return enableAnsiOutputNative(); + } + + public byte[] GetUserEnvironmentVariable(byte[] key) { + return GetUserEnvironmentVariableNative(key); + } + public byte[] SetUserEnvironmentVariable(byte[] key, byte[] value) { + return SetUserEnvironmentVariableNative(key, value); + } + public byte[] DeleteUserEnvironmentVariable(byte[] key) { + return DeleteUserEnvironmentVariableNative(key); + } + + public String GetKnownFolderPath(String rfid) { + return GetKnownFolderPathNative(rfid); + } + }; + NativeApi.set(nativeApi); + } +} diff --git a/windows-jni-utils-lmcoursier/src/main/java/lmcoursier/internal/jniutils/NativeCalls.java b/windows-jni-utils-lmcoursier/src/main/java/lmcoursier/internal/jniutils/NativeCalls.java new file mode 100644 index 0000000..1696101 --- /dev/null +++ b/windows-jni-utils-lmcoursier/src/main/java/lmcoursier/internal/jniutils/NativeCalls.java @@ -0,0 +1,46 @@ +package lmcoursier.internal.jniutils; + +import coursier.jniutils.LoadWindowsLibrary; +import coursier.jniutils.NativeApi; + +public final class NativeCalls { + + static native String terminalSizeNative(); + + static native String enableAnsiOutputNative(); + + static native byte[] GetUserEnvironmentVariableNative(byte[] key); + static native byte[] SetUserEnvironmentVariableNative(byte[] key, byte[] value); + static native byte[] DeleteUserEnvironmentVariableNative(byte[] key); + + static native String GetKnownFolderPathNative(String rfid); + + + public static void setup() { + LoadWindowsLibrary.ensureInitialized(); + NativeApi nativeApi = new NativeApi() { + public String terminalSize() { + return terminalSizeNative(); + } + + public String enableAnsiOutput() { + return enableAnsiOutputNative(); + } + + public byte[] GetUserEnvironmentVariable(byte[] key) { + return GetUserEnvironmentVariableNative(key); + } + public byte[] SetUserEnvironmentVariable(byte[] key, byte[] value) { + return SetUserEnvironmentVariableNative(key, value); + } + public byte[] DeleteUserEnvironmentVariable(byte[] key) { + return DeleteUserEnvironmentVariableNative(key); + } + + public String GetKnownFolderPath(String rfid) { + return GetKnownFolderPathNative(rfid); + } + }; + NativeApi.set(nativeApi); + } +} diff --git a/windows-jni-utils-tests/src/coursier/jniutils/AnsiTerminal.scala b/windows-jni-utils-tests/src/coursier/jniutils/AnsiTerminal.scala index a1bd39f..f64364d 100644 --- a/windows-jni-utils-tests/src/coursier/jniutils/AnsiTerminal.scala +++ b/windows-jni-utils-tests/src/coursier/jniutils/AnsiTerminal.scala @@ -1,7 +1,5 @@ package coursier.jniutils -import coursier.jniutils.windowsansiterminal.WindowsAnsiTerminal - object AnsiTerminal { def main(args: Array[String]): Unit = { val verbose = args match { diff --git a/windows-jni-utils-tests/src/coursier/jniutils/EnvironmentVariable.scala b/windows-jni-utils-tests/src/coursier/jniutils/EnvironmentVariable.scala index 121ecec..8f062f4 100644 --- a/windows-jni-utils-tests/src/coursier/jniutils/EnvironmentVariable.scala +++ b/windows-jni-utils-tests/src/coursier/jniutils/EnvironmentVariable.scala @@ -1,7 +1,5 @@ package coursier.jniutils -import coursier.jniutils.windowsenvironmentvariables.WindowsEnvironmentVariables - object EnvironmentVariable { def main(args: Array[String]): Unit = args match { diff --git a/windows-jni-utils-tests/src/coursier/jniutils/KnownFolders.scala b/windows-jni-utils-tests/src/coursier/jniutils/KnownFolders.scala index dafd42e..388eaf2 100644 --- a/windows-jni-utils-tests/src/coursier/jniutils/KnownFolders.scala +++ b/windows-jni-utils-tests/src/coursier/jniutils/KnownFolders.scala @@ -1,7 +1,5 @@ package coursier.jniutils -import coursier.jniutils.windowsknownfolders.WindowsKnownFolders - object KnownFolders { def main(args: Array[String]): Unit = for (arg <- args) { diff --git a/windows-jni-utils-tests/test/src/coursier/jniutils/tests/WindowsKnownFoldersTests.scala b/windows-jni-utils-tests/test/src/coursier/jniutils/tests/WindowsKnownFoldersTests.scala index 566fd94..de13042 100644 --- a/windows-jni-utils-tests/test/src/coursier/jniutils/tests/WindowsKnownFoldersTests.scala +++ b/windows-jni-utils-tests/test/src/coursier/jniutils/tests/WindowsKnownFoldersTests.scala @@ -1,11 +1,27 @@ package coursier.jniutils.tests -import coursier.jniutils.windowsknownfolders.WindowsKnownFolders +import coursier.jniutils.WindowsKnownFolders import utest._ object WindowsKnownFoldersTests extends TestSuite { val tests = Tests { - test { + test("default") synchronized { + coursier.jniutils.NativeApi.set(null); + WindowsKnownFolders.knownFolderPath("{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}") + } + + test("bootstrap") synchronized { + coursier.bootstrap.launcher.jniutils.NativeCalls.setup() + WindowsKnownFolders.knownFolderPath("{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}") + } + + test("lmcoursier") synchronized { + lmcoursier.internal.jniutils.NativeCalls.setup() + WindowsKnownFolders.knownFolderPath("{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}") + } + + test("coursierapi") synchronized { + coursierapi.internal.jniutils.NativeCalls.setup() WindowsKnownFolders.knownFolderPath("{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}") } } diff --git a/windows-jni-utils/src/main/c/coursier_bootstrap_launcher_jniutils_NativeCalls.c b/windows-jni-utils/src/main/c/coursier_bootstrap_launcher_jniutils_NativeCalls.c new file mode 100644 index 0000000..24913e2 --- /dev/null +++ b/windows-jni-utils/src/main/c/coursier_bootstrap_launcher_jniutils_NativeCalls.c @@ -0,0 +1,32 @@ +#include "coursier_bootstrap_launcher_jniutils_NativeCalls.h" +#include "coursier_jniutils_NativeCalls.h" + +JNIEXPORT jstring JNICALL Java_coursier_bootstrap_launcher_jniutils_NativeCalls_terminalSizeNative + (JNIEnv *env, jclass class) { + return Java_coursier_jniutils_NativeCalls_terminalSizeNative(env, class); +} + +JNIEXPORT jstring JNICALL Java_coursier_bootstrap_launcher_jniutils_NativeCalls_enableAnsiOutputNative + (JNIEnv *env, jclass class) { + return Java_coursier_jniutils_NativeCalls_enableAnsiOutputNative(env, class); +} + +JNIEXPORT jbyteArray JNICALL Java_coursier_bootstrap_launcher_jniutils_NativeCalls_GetUserEnvironmentVariableNative + (JNIEnv *env, jclass class, jbyteArray key) { + return Java_coursier_jniutils_NativeCalls_GetUserEnvironmentVariableNative(env, class, key); +} + +JNIEXPORT jbyteArray JNICALL Java_coursier_bootstrap_launcher_jniutils_NativeCalls_SetUserEnvironmentVariableNative + (JNIEnv *env, jclass class, jbyteArray key, jbyteArray value) { + return Java_coursier_jniutils_NativeCalls_SetUserEnvironmentVariableNative(env, class, key, value); +} + +JNIEXPORT jbyteArray JNICALL Java_coursier_bootstrap_launcher_jniutils_NativeCalls_DeleteUserEnvironmentVariableNative + (JNIEnv *env, jclass class, jbyteArray key) { + return Java_coursier_jniutils_NativeCalls_DeleteUserEnvironmentVariableNative(env, class, key); +} + +JNIEXPORT jstring JNICALL Java_coursier_bootstrap_launcher_jniutils_NativeCalls_GetKnownFolderPathNative + (JNIEnv *env, jclass class, jstring rfid) { + return Java_coursier_jniutils_NativeCalls_GetKnownFolderPathNative(env, class, rfid); +} diff --git a/windows-jni-utils/src/main/c/coursier_bootstrap_launcher_jniutils_NativeCalls.h b/windows-jni-utils/src/main/c/coursier_bootstrap_launcher_jniutils_NativeCalls.h new file mode 100644 index 0000000..a1e33c5 --- /dev/null +++ b/windows-jni-utils/src/main/c/coursier_bootstrap_launcher_jniutils_NativeCalls.h @@ -0,0 +1,61 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class coursier_bootstrap_launcher_jniutils_NativeCalls */ + +#ifndef _Included_coursier_bootstrap_launcher_jniutils_NativeCalls +#define _Included_coursier_bootstrap_launcher_jniutils_NativeCalls +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: coursier_bootstrap_launcher_jniutils_NativeCalls + * Method: terminalSizeNative + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_coursier_bootstrap_launcher_jniutils_NativeCalls_terminalSizeNative + (JNIEnv *, jclass); + +/* + * Class: coursier_bootstrap_launcher_jniutils_NativeCalls + * Method: enableAnsiOutputNative + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_coursier_bootstrap_launcher_jniutils_NativeCalls_enableAnsiOutputNative + (JNIEnv *, jclass); + +/* + * Class: coursier_bootstrap_launcher_jniutils_NativeCalls + * Method: GetUserEnvironmentVariableNative + * Signature: ([B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_coursier_bootstrap_launcher_jniutils_NativeCalls_GetUserEnvironmentVariableNative + (JNIEnv *, jclass, jbyteArray); + +/* + * Class: coursier_bootstrap_launcher_jniutils_NativeCalls + * Method: SetUserEnvironmentVariableNative + * Signature: ([B[B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_coursier_bootstrap_launcher_jniutils_NativeCalls_SetUserEnvironmentVariableNative + (JNIEnv *, jclass, jbyteArray, jbyteArray); + +/* + * Class: coursier_bootstrap_launcher_jniutils_NativeCalls + * Method: DeleteUserEnvironmentVariableNative + * Signature: ([B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_coursier_bootstrap_launcher_jniutils_NativeCalls_DeleteUserEnvironmentVariableNative + (JNIEnv *, jclass, jbyteArray); + +/* + * Class: coursier_bootstrap_launcher_jniutils_NativeCalls + * Method: GetKnownFolderPathNative + * Signature: (Ljava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_coursier_bootstrap_launcher_jniutils_NativeCalls_GetKnownFolderPathNative + (JNIEnv *, jclass, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/windows-jni-utils/src/main/c/coursier_jniutils_NativeCalls.h b/windows-jni-utils/src/main/c/coursier_jniutils_NativeCalls.h new file mode 100644 index 0000000..410d0c3 --- /dev/null +++ b/windows-jni-utils/src/main/c/coursier_jniutils_NativeCalls.h @@ -0,0 +1,61 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class coursier_jniutils_NativeCalls */ + +#ifndef _Included_coursier_jniutils_NativeCalls +#define _Included_coursier_jniutils_NativeCalls +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: coursier_jniutils_NativeCalls + * Method: terminalSizeNative + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_coursier_jniutils_NativeCalls_terminalSizeNative + (JNIEnv *, jclass); + +/* + * Class: coursier_jniutils_NativeCalls + * Method: enableAnsiOutputNative + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_coursier_jniutils_NativeCalls_enableAnsiOutputNative + (JNIEnv *, jclass); + +/* + * Class: coursier_jniutils_NativeCalls + * Method: GetUserEnvironmentVariableNative + * Signature: ([B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_NativeCalls_GetUserEnvironmentVariableNative + (JNIEnv *, jclass, jbyteArray); + +/* + * Class: coursier_jniutils_NativeCalls + * Method: SetUserEnvironmentVariableNative + * Signature: ([B[B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_NativeCalls_SetUserEnvironmentVariableNative + (JNIEnv *, jclass, jbyteArray, jbyteArray); + +/* + * Class: coursier_jniutils_NativeCalls + * Method: DeleteUserEnvironmentVariableNative + * Signature: ([B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_NativeCalls_DeleteUserEnvironmentVariableNative + (JNIEnv *, jclass, jbyteArray); + +/* + * Class: coursier_jniutils_NativeCalls + * Method: GetKnownFolderPathNative + * Signature: (Ljava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_coursier_jniutils_NativeCalls_GetKnownFolderPathNative + (JNIEnv *, jclass, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/windows-jni-utils/src/main/c/coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal.c b/windows-jni-utils/src/main/c/coursier_jniutils_WindowsAnsiTerminal.c similarity index 54% rename from windows-jni-utils/src/main/c/coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal.c rename to windows-jni-utils/src/main/c/coursier_jniutils_WindowsAnsiTerminal.c index 4ad4e85..7b9f363 100644 --- a/windows-jni-utils/src/main/c/coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal.c +++ b/windows-jni-utils/src/main/c/coursier_jniutils_WindowsAnsiTerminal.c @@ -1,8 +1,8 @@ #include #include -#include "coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal.h" +#include "coursier_jniutils_NativeCalls.h" -jstring _Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_lastError +jstring _Java_coursier_jniutils_NativeCalls_Terminal_lastError (JNIEnv *env, const char *origin) { DWORD err = GetLastError(); @@ -15,7 +15,7 @@ jstring _Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_lastErro return result; } -jstring _Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_format +jstring _Java_coursier_jniutils_NativeCalls_Terminal_format (JNIEnv *env, const char *format, ...) { char dummy[1]; @@ -32,43 +32,43 @@ jstring _Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_format return result; } -JNIEXPORT jstring JNICALL Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_terminalSizeNative +JNIEXPORT jstring JNICALL Java_coursier_jniutils_NativeCalls_terminalSizeNative (JNIEnv *env, jclass class) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); if (handle == INVALID_HANDLE_VALUE) { - return _Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_lastError(env, "GetStdHandle"); + return _Java_coursier_jniutils_NativeCalls_Terminal_lastError(env, "GetStdHandle"); } CONSOLE_SCREEN_BUFFER_INFO info; memset(&info, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO)); BOOL res = GetConsoleScreenBufferInfo(handle, &info); if (!res) { - return _Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_lastError(env, "GetConsoleScreenBufferInfo"); + return _Java_coursier_jniutils_NativeCalls_Terminal_lastError(env, "GetConsoleScreenBufferInfo"); } - return _Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_format(env, "%hu:%hu", info.dwSize.X, info.dwSize.Y); + return _Java_coursier_jniutils_NativeCalls_Terminal_format(env, "%hu:%hu", info.dwSize.X, info.dwSize.Y); } -JNIEXPORT jstring JNICALL Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_enableAnsiOutputNative +JNIEXPORT jstring JNICALL Java_coursier_jniutils_NativeCalls_enableAnsiOutputNative (JNIEnv *env, jclass class) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); if (handle == INVALID_HANDLE_VALUE) { - return _Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_lastError(env, "GetStdHandle"); + return _Java_coursier_jniutils_NativeCalls_Terminal_lastError(env, "GetStdHandle"); } DWORD mode = 0; BOOL res = GetConsoleMode(handle, &mode); if (!res) { - return _Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_lastError(env, "GetConsoleMode"); + return _Java_coursier_jniutils_NativeCalls_Terminal_lastError(env, "GetConsoleMode"); } char *ret = "false"; if (!(mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)) { res = SetConsoleMode(handle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING); if (!res) { - return _Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_lastError(env, "GetConsoleMode"); + return _Java_coursier_jniutils_NativeCalls_Terminal_lastError(env, "GetConsoleMode"); } ret = "true"; } diff --git a/windows-jni-utils/src/main/c/coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables.c b/windows-jni-utils/src/main/c/coursier_jniutils_WindowsEnvironmentVariables.c similarity index 83% rename from windows-jni-utils/src/main/c/coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables.c rename to windows-jni-utils/src/main/c/coursier_jniutils_WindowsEnvironmentVariables.c index 9dedbbd..05c8999 100644 --- a/windows-jni-utils/src/main/c/coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables.c +++ b/windows-jni-utils/src/main/c/coursier_jniutils_WindowsEnvironmentVariables.c @@ -4,9 +4,9 @@ #include #include -#include "coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables.h" +#include "coursier_jniutils_NativeCalls.h" -JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables_SetUserEnvironmentVariableNative +JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_NativeCalls_SetUserEnvironmentVariableNative (JNIEnv *env, jclass class, jbyteArray key, jbyteArray value) { const jbyte *keyStr = (*env)->GetByteArrayElements(env, key, NULL); @@ -37,7 +37,7 @@ JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_windowsenvironmentvariables_ return (*env)->NewByteArray(env, 0); } -JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables_GetUserEnvironmentVariableNative +JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_NativeCalls_GetUserEnvironmentVariableNative (JNIEnv *env, jclass class, jbyteArray key) { const jbyte *keyStr = (*env)->GetByteArrayElements(env, key, NULL); @@ -97,7 +97,7 @@ JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_windowsenvironmentvariables_ return arr; } -JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables_DeleteUserEnvironmentVariableNative +JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_NativeCalls_DeleteUserEnvironmentVariableNative (JNIEnv *env, jclass class, jbyteArray key) { const jbyte *keyStr = (*env)->GetByteArrayElements(env, key, NULL); diff --git a/windows-jni-utils/src/main/c/coursier_jniutils_windowsknownfolders_WindowsKnownFolders.c b/windows-jni-utils/src/main/c/coursier_jniutils_WindowsKnownFolders.c similarity index 77% rename from windows-jni-utils/src/main/c/coursier_jniutils_windowsknownfolders_WindowsKnownFolders.c rename to windows-jni-utils/src/main/c/coursier_jniutils_WindowsKnownFolders.c index 38db2ff..13b07f4 100644 --- a/windows-jni-utils/src/main/c/coursier_jniutils_windowsknownfolders_WindowsKnownFolders.c +++ b/windows-jni-utils/src/main/c/coursier_jniutils_WindowsKnownFolders.c @@ -2,9 +2,9 @@ #define _WIN32_WINNT 0x0600 #include -#include "coursier_jniutils_windowsknownfolders_WindowsKnownFolders.h" +#include "coursier_jniutils_NativeCalls.h" -JNIEXPORT jstring JNICALL Java_coursier_jniutils_windowsknownfolders_WindowsKnownFolders_GetKnownFolderPath +JNIEXPORT jstring JNICALL Java_coursier_jniutils_NativeCalls_GetKnownFolderPathNative (JNIEnv *env, jclass cls, jstring rfid) { long res = 0; @@ -22,6 +22,7 @@ JNIEXPORT jstring JNICALL Java_coursier_jniutils_windowsknownfolders_WindowsKnow WCHAR *ppszPath = NULL; res = SHGetKnownFolderPath(&id, 0, NULL, &ppszPath); if (res != 0) { + CoTaskMemFree(ppszPath); return NULL; } diff --git a/windows-jni-utils/src/main/c/coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal.h b/windows-jni-utils/src/main/c/coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal.h deleted file mode 100644 index 05cbc9f..0000000 --- a/windows-jni-utils/src/main/c/coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal.h +++ /dev/null @@ -1,29 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal */ - -#ifndef _Included_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal -#define _Included_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal - * Method: terminalSizeNative - * Signature: ()Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_terminalSizeNative - (JNIEnv *, jclass); - -/* - * Class: coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal - * Method: enableAnsiOutputNative - * Signature: ()Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_coursier_jniutils_windowsansiterminal_WindowsAnsiTerminal_enableAnsiOutputNative - (JNIEnv *, jclass); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/windows-jni-utils/src/main/c/coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables.h b/windows-jni-utils/src/main/c/coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables.h deleted file mode 100644 index c778bfe..0000000 --- a/windows-jni-utils/src/main/c/coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables.h +++ /dev/null @@ -1,37 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables */ - -#ifndef _Included_coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables -#define _Included_coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables - * Method: GetUserEnvironmentVariableNative - * Signature: ([B)[B - */ -JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables_GetUserEnvironmentVariableNative - (JNIEnv *, jclass, jbyteArray); - -/* - * Class: coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables - * Method: SetUserEnvironmentVariableNative - * Signature: ([B[B)[B - */ -JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables_SetUserEnvironmentVariableNative - (JNIEnv *, jclass, jbyteArray, jbyteArray); - -/* - * Class: coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables - * Method: DeleteUserEnvironmentVariableNative - * Signature: ([B)[B - */ -JNIEXPORT jbyteArray JNICALL Java_coursier_jniutils_windowsenvironmentvariables_WindowsEnvironmentVariables_DeleteUserEnvironmentVariableNative - (JNIEnv *, jclass, jbyteArray); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/windows-jni-utils/src/main/c/coursier_jniutils_windowsknownfolders_WindowsKnownFolders.h b/windows-jni-utils/src/main/c/coursier_jniutils_windowsknownfolders_WindowsKnownFolders.h deleted file mode 100644 index 58be81a..0000000 --- a/windows-jni-utils/src/main/c/coursier_jniutils_windowsknownfolders_WindowsKnownFolders.h +++ /dev/null @@ -1,21 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class coursier_jniutils_windowsknownfolders_WindowsKnownFolders */ - -#ifndef _Included_coursier_jniutils_windowsknownfolders_WindowsKnownFolders -#define _Included_coursier_jniutils_windowsknownfolders_WindowsKnownFolders -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: coursier_jniutils_windowsknownfolders_WindowsKnownFolders - * Method: GetKnownFolderPath - * Signature: (Ljava/lang/String;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_coursier_jniutils_windowsknownfolders_WindowsKnownFolders_GetKnownFolderPath - (JNIEnv *, jclass, jstring); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/windows-jni-utils/src/main/c/coursierapi_internal_jniutils_NativeCalls.c b/windows-jni-utils/src/main/c/coursierapi_internal_jniutils_NativeCalls.c new file mode 100644 index 0000000..0b72555 --- /dev/null +++ b/windows-jni-utils/src/main/c/coursierapi_internal_jniutils_NativeCalls.c @@ -0,0 +1,32 @@ +#include "coursierapi_internal_jniutils_NativeCalls.h" +#include "coursier_jniutils_NativeCalls.h" + +JNIEXPORT jstring JNICALL Java_coursierapi_internal_jniutils_NativeCalls_terminalSizeNative + (JNIEnv *env, jclass class) { + return Java_coursier_jniutils_NativeCalls_terminalSizeNative(env, class); +} + +JNIEXPORT jstring JNICALL Java_coursierapi_internal_jniutils_NativeCalls_enableAnsiOutputNative + (JNIEnv *env, jclass class) { + return Java_coursier_jniutils_NativeCalls_enableAnsiOutputNative(env, class); +} + +JNIEXPORT jbyteArray JNICALL Java_coursierapi_internal_jniutils_NativeCalls_GetUserEnvironmentVariableNative + (JNIEnv *env, jclass class, jbyteArray key) { + return Java_coursier_jniutils_NativeCalls_GetUserEnvironmentVariableNative(env, class, key); +} + +JNIEXPORT jbyteArray JNICALL Java_coursierapi_internal_jniutils_NativeCalls_SetUserEnvironmentVariableNative + (JNIEnv *env, jclass class, jbyteArray key, jbyteArray value) { + return Java_coursier_jniutils_NativeCalls_SetUserEnvironmentVariableNative(env, class, key, value); +} + +JNIEXPORT jbyteArray JNICALL Java_coursierapi_internal_jniutils_NativeCalls_DeleteUserEnvironmentVariableNative + (JNIEnv *env, jclass class, jbyteArray key) { + return Java_coursier_jniutils_NativeCalls_DeleteUserEnvironmentVariableNative(env, class, key); +} + +JNIEXPORT jstring JNICALL Java_coursierapi_internal_jniutils_NativeCalls_GetKnownFolderPathNative + (JNIEnv *env, jclass class, jstring rfid) { + return Java_coursier_jniutils_NativeCalls_GetKnownFolderPathNative(env, class, rfid); +} diff --git a/windows-jni-utils/src/main/c/coursierapi_internal_jniutils_NativeCalls.h b/windows-jni-utils/src/main/c/coursierapi_internal_jniutils_NativeCalls.h new file mode 100644 index 0000000..4c56182 --- /dev/null +++ b/windows-jni-utils/src/main/c/coursierapi_internal_jniutils_NativeCalls.h @@ -0,0 +1,61 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class coursierapi_internal_jniutils_NativeCalls */ + +#ifndef _Included_coursierapi_internal_jniutils_NativeCalls +#define _Included_coursierapi_internal_jniutils_NativeCalls +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: coursierapi_internal_jniutils_NativeCalls + * Method: terminalSizeNative + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_coursierapi_internal_jniutils_NativeCalls_terminalSizeNative + (JNIEnv *, jclass); + +/* + * Class: coursierapi_internal_jniutils_NativeCalls + * Method: enableAnsiOutputNative + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_coursierapi_internal_jniutils_NativeCalls_enableAnsiOutputNative + (JNIEnv *, jclass); + +/* + * Class: coursierapi_internal_jniutils_NativeCalls + * Method: GetUserEnvironmentVariableNative + * Signature: ([B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_coursierapi_internal_jniutils_NativeCalls_GetUserEnvironmentVariableNative + (JNIEnv *, jclass, jbyteArray); + +/* + * Class: coursierapi_internal_jniutils_NativeCalls + * Method: SetUserEnvironmentVariableNative + * Signature: ([B[B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_coursierapi_internal_jniutils_NativeCalls_SetUserEnvironmentVariableNative + (JNIEnv *, jclass, jbyteArray, jbyteArray); + +/* + * Class: coursierapi_internal_jniutils_NativeCalls + * Method: DeleteUserEnvironmentVariableNative + * Signature: ([B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_coursierapi_internal_jniutils_NativeCalls_DeleteUserEnvironmentVariableNative + (JNIEnv *, jclass, jbyteArray); + +/* + * Class: coursierapi_internal_jniutils_NativeCalls + * Method: GetKnownFolderPathNative + * Signature: (Ljava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_coursierapi_internal_jniutils_NativeCalls_GetKnownFolderPathNative + (JNIEnv *, jclass, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/windows-jni-utils/src/main/c/lmcoursier_internal_jniutils_NativeCalls.c b/windows-jni-utils/src/main/c/lmcoursier_internal_jniutils_NativeCalls.c new file mode 100644 index 0000000..daf1cf5 --- /dev/null +++ b/windows-jni-utils/src/main/c/lmcoursier_internal_jniutils_NativeCalls.c @@ -0,0 +1,32 @@ +#include "lmcoursier_internal_jniutils_NativeCalls.h" +#include "coursier_jniutils_NativeCalls.h" + +JNIEXPORT jstring JNICALL Java_lmcoursier_internal_jniutils_NativeCalls_terminalSizeNative + (JNIEnv *env, jclass class) { + return Java_coursier_jniutils_NativeCalls_terminalSizeNative(env, class); +} + +JNIEXPORT jstring JNICALL Java_lmcoursier_internal_jniutils_NativeCalls_enableAnsiOutputNative + (JNIEnv *env, jclass class) { + return Java_coursier_jniutils_NativeCalls_enableAnsiOutputNative(env, class); +} + +JNIEXPORT jbyteArray JNICALL Java_lmcoursier_internal_jniutils_NativeCalls_GetUserEnvironmentVariableNative + (JNIEnv *env, jclass class, jbyteArray key) { + return Java_coursier_jniutils_NativeCalls_GetUserEnvironmentVariableNative(env, class, key); +} + +JNIEXPORT jbyteArray JNICALL Java_lmcoursier_internal_jniutils_NativeCalls_SetUserEnvironmentVariableNative + (JNIEnv *env, jclass class, jbyteArray key, jbyteArray value) { + return Java_coursier_jniutils_NativeCalls_SetUserEnvironmentVariableNative(env, class, key, value); +} + +JNIEXPORT jbyteArray JNICALL Java_lmcoursier_internal_jniutils_NativeCalls_DeleteUserEnvironmentVariableNative + (JNIEnv *env, jclass class, jbyteArray key) { + return Java_coursier_jniutils_NativeCalls_DeleteUserEnvironmentVariableNative(env, class, key); +} + +JNIEXPORT jstring JNICALL Java_lmcoursier_internal_jniutils_NativeCalls_GetKnownFolderPathNative + (JNIEnv *env, jclass class, jstring rfid) { + return Java_coursier_jniutils_NativeCalls_GetKnownFolderPathNative(env, class, rfid); +} diff --git a/windows-jni-utils/src/main/c/lmcoursier_internal_jniutils_NativeCalls.h b/windows-jni-utils/src/main/c/lmcoursier_internal_jniutils_NativeCalls.h new file mode 100644 index 0000000..543328d --- /dev/null +++ b/windows-jni-utils/src/main/c/lmcoursier_internal_jniutils_NativeCalls.h @@ -0,0 +1,61 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class lmcoursier_internal_jniutils_NativeCalls */ + +#ifndef _Included_lmcoursier_internal_jniutils_NativeCalls +#define _Included_lmcoursier_internal_jniutils_NativeCalls +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: lmcoursier_internal_jniutils_NativeCalls + * Method: terminalSizeNative + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_lmcoursier_internal_jniutils_NativeCalls_terminalSizeNative + (JNIEnv *, jclass); + +/* + * Class: lmcoursier_internal_jniutils_NativeCalls + * Method: enableAnsiOutputNative + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_lmcoursier_internal_jniutils_NativeCalls_enableAnsiOutputNative + (JNIEnv *, jclass); + +/* + * Class: lmcoursier_internal_jniutils_NativeCalls + * Method: GetUserEnvironmentVariableNative + * Signature: ([B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_lmcoursier_internal_jniutils_NativeCalls_GetUserEnvironmentVariableNative + (JNIEnv *, jclass, jbyteArray); + +/* + * Class: lmcoursier_internal_jniutils_NativeCalls + * Method: SetUserEnvironmentVariableNative + * Signature: ([B[B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_lmcoursier_internal_jniutils_NativeCalls_SetUserEnvironmentVariableNative + (JNIEnv *, jclass, jbyteArray, jbyteArray); + +/* + * Class: lmcoursier_internal_jniutils_NativeCalls + * Method: DeleteUserEnvironmentVariableNative + * Signature: ([B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_lmcoursier_internal_jniutils_NativeCalls_DeleteUserEnvironmentVariableNative + (JNIEnv *, jclass, jbyteArray); + +/* + * Class: lmcoursier_internal_jniutils_NativeCalls + * Method: GetKnownFolderPathNative + * Signature: (Ljava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_lmcoursier_internal_jniutils_NativeCalls_GetKnownFolderPathNative + (JNIEnv *, jclass, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/windows-jni-utils/src/main/java/coursier/jniutils/NativeApi.java b/windows-jni-utils/src/main/java/coursier/jniutils/NativeApi.java new file mode 100644 index 0000000..1107fdb --- /dev/null +++ b/windows-jni-utils/src/main/java/coursier/jniutils/NativeApi.java @@ -0,0 +1,27 @@ +package coursier.jniutils; + +public abstract class NativeApi { + + public abstract String terminalSize(); + + public abstract String enableAnsiOutput(); + + public abstract byte[] GetUserEnvironmentVariable(byte[] key); + public abstract byte[] SetUserEnvironmentVariable(byte[] key, byte[] value); + public abstract byte[] DeleteUserEnvironmentVariable(byte[] key); + + public abstract String GetKnownFolderPath(String rfid); + + + private static NativeApi instance = null; + public static NativeApi get() { + if (instance == null) { + instance = NativeCalls.nativeApi(); + } + return instance; + } + + public static void set(NativeApi nativeApi) { + instance = nativeApi; + } +} diff --git a/windows-jni-utils/src/main/java/coursier/jniutils/NativeCalls.java b/windows-jni-utils/src/main/java/coursier/jniutils/NativeCalls.java new file mode 100644 index 0000000..b16825e --- /dev/null +++ b/windows-jni-utils/src/main/java/coursier/jniutils/NativeCalls.java @@ -0,0 +1,42 @@ +package coursier.jniutils; + +final class NativeCalls { + + static native String terminalSizeNative(); + + static native String enableAnsiOutputNative(); + + static native byte[] GetUserEnvironmentVariableNative(byte[] key); + static native byte[] SetUserEnvironmentVariableNative(byte[] key, byte[] value); + static native byte[] DeleteUserEnvironmentVariableNative(byte[] key); + + static native String GetKnownFolderPathNative(String rfid); + + static NativeApi nativeApi() { + LoadWindowsLibrary.ensureInitialized(); + return new NativeApi() { + public String terminalSize() { + return terminalSizeNative(); + } + + public String enableAnsiOutput() { + return enableAnsiOutputNative(); + } + + public byte[] GetUserEnvironmentVariable(byte[] key) { + return GetUserEnvironmentVariableNative(key); + } + public byte[] SetUserEnvironmentVariable(byte[] key, byte[] value) { + return SetUserEnvironmentVariableNative(key, value); + } + public byte[] DeleteUserEnvironmentVariable(byte[] key) { + return DeleteUserEnvironmentVariableNative(key); + } + + public String GetKnownFolderPath(String rfid) { + return GetKnownFolderPathNative(rfid); + } + }; + } + +} diff --git a/windows-jni-utils/src/main/java/coursier/jniutils/windowsansiterminal/TerminalSize.java b/windows-jni-utils/src/main/java/coursier/jniutils/TerminalSize.java similarity index 95% rename from windows-jni-utils/src/main/java/coursier/jniutils/windowsansiterminal/TerminalSize.java rename to windows-jni-utils/src/main/java/coursier/jniutils/TerminalSize.java index f1c7b7b..662f4ed 100644 --- a/windows-jni-utils/src/main/java/coursier/jniutils/windowsansiterminal/TerminalSize.java +++ b/windows-jni-utils/src/main/java/coursier/jniutils/TerminalSize.java @@ -1,4 +1,4 @@ -package coursier.jniutils.windowsansiterminal; +package coursier.jniutils; import java.util.Objects; diff --git a/windows-jni-utils/src/main/java/coursier/jniutils/windowsansiterminal/WindowsAnsiTerminal.java b/windows-jni-utils/src/main/java/coursier/jniutils/WindowsAnsiTerminal.java similarity index 70% rename from windows-jni-utils/src/main/java/coursier/jniutils/windowsansiterminal/WindowsAnsiTerminal.java rename to windows-jni-utils/src/main/java/coursier/jniutils/WindowsAnsiTerminal.java index 3725697..1f5a332 100644 --- a/windows-jni-utils/src/main/java/coursier/jniutils/windowsansiterminal/WindowsAnsiTerminal.java +++ b/windows-jni-utils/src/main/java/coursier/jniutils/WindowsAnsiTerminal.java @@ -1,18 +1,11 @@ -package coursier.jniutils.windowsansiterminal; - -import coursier.jniutils.LoadWindowsLibrary; +package coursier.jniutils; import java.io.IOException; public final class WindowsAnsiTerminal { - private static native String terminalSizeNative(); - - private static native String enableAnsiOutputNative(); - public static TerminalSize terminalSize() throws IOException { - LoadWindowsLibrary.ensureInitialized(); - String value = terminalSizeNative(); + String value = NativeApi.get().terminalSize(); if (value.startsWith("error:")) throw new IOException("Error getting terminal size: " + value.substring("error:".length())); String[] elems = value.split(":", 2); @@ -27,8 +20,7 @@ public static TerminalSize terminalSize() throws IOException { * @throws IOException */ public static boolean enableAnsiOutput() throws IOException { - LoadWindowsLibrary.ensureInitialized(); - String value = enableAnsiOutputNative(); + String value = NativeApi.get().enableAnsiOutput(); if (value.startsWith("error:")) throw new IOException("Error enabling ANSI output: " + value.substring("error:".length())); return Boolean.parseBoolean(value); diff --git a/windows-jni-utils/src/main/java/coursier/jniutils/windowsansiterminal/WindowsAnsiTerminalSubstitutions.java b/windows-jni-utils/src/main/java/coursier/jniutils/WindowsAnsiTerminalSubstitutions.java similarity index 82% rename from windows-jni-utils/src/main/java/coursier/jniutils/windowsansiterminal/WindowsAnsiTerminalSubstitutions.java rename to windows-jni-utils/src/main/java/coursier/jniutils/WindowsAnsiTerminalSubstitutions.java index 4cdb026..7ba1eba 100644 --- a/windows-jni-utils/src/main/java/coursier/jniutils/windowsansiterminal/WindowsAnsiTerminalSubstitutions.java +++ b/windows-jni-utils/src/main/java/coursier/jniutils/WindowsAnsiTerminalSubstitutions.java @@ -1,4 +1,4 @@ -package coursier.jniutils.windowsansiterminal; +package coursier.jniutils; import com.oracle.svm.core.annotate.Substitute; import com.oracle.svm.core.annotate.TargetClass; @@ -7,7 +7,7 @@ import java.io.IOException; -@TargetClass(className = "coursier.jniutils.windowsansiterminal.WindowsAnsiTerminal") +@TargetClass(className = "coursier.jniutils.WindowsAnsiTerminal") @Platforms({Platform.DARWIN.class, Platform.LINUX.class}) final class WindowsAnsiTerminalSubstitutions { diff --git a/windows-jni-utils/src/main/java/coursier/jniutils/windowsenvironmentvariables/WindowsEnvironmentVariables.java b/windows-jni-utils/src/main/java/coursier/jniutils/WindowsEnvironmentVariables.java similarity index 52% rename from windows-jni-utils/src/main/java/coursier/jniutils/windowsenvironmentvariables/WindowsEnvironmentVariables.java rename to windows-jni-utils/src/main/java/coursier/jniutils/WindowsEnvironmentVariables.java index 282d78a..f197b01 100644 --- a/windows-jni-utils/src/main/java/coursier/jniutils/windowsenvironmentvariables/WindowsEnvironmentVariables.java +++ b/windows-jni-utils/src/main/java/coursier/jniutils/WindowsEnvironmentVariables.java @@ -1,19 +1,11 @@ -package coursier.jniutils.windowsenvironmentvariables; - -import coursier.jniutils.CString; -import coursier.jniutils.LoadWindowsLibrary; +package coursier.jniutils; import java.io.IOException; public final class WindowsEnvironmentVariables { - private static native byte[] GetUserEnvironmentVariableNative(byte[] key); - private static native byte[] SetUserEnvironmentVariableNative(byte[] key, byte[] value); - private static native byte[] DeleteUserEnvironmentVariableNative(byte[] key); - public static String get(String key) throws IOException { - LoadWindowsLibrary.ensureInitialized(); - String value = CString.fromC(GetUserEnvironmentVariableNative(CString.toC(key))); + String value = CString.fromC(NativeApi.get().GetUserEnvironmentVariable(CString.toC(key))); if (value == null) return null; if (value.startsWith("E")) @@ -23,16 +15,14 @@ public static String get(String key) throws IOException { } public static void set(String key, String value) throws IOException { - LoadWindowsLibrary.ensureInitialized(); - String ret = CString.fromC(SetUserEnvironmentVariableNative(CString.toC(key), CString.toC(value))); + String ret = CString.fromC(NativeApi.get().SetUserEnvironmentVariable(CString.toC(key), CString.toC(value))); if (ret.startsWith("E")) throw new IOException( "Error setting user environment variable " + key + ": " + ret.substring("E".length())); } public static void delete(String key) throws IOException { - LoadWindowsLibrary.ensureInitialized(); - String ret = CString.fromC(DeleteUserEnvironmentVariableNative(CString.toC(key))); + String ret = CString.fromC(NativeApi.get().DeleteUserEnvironmentVariable(CString.toC(key))); if (ret.startsWith("E")) throw new IOException( "Error deleting user environment variable " + key + ": " + ret.substring("E".length())); diff --git a/windows-jni-utils/src/main/java/coursier/jniutils/windowsenvironmentvariables/WindowsEnvironmentVariablesSubstitutions.java b/windows-jni-utils/src/main/java/coursier/jniutils/WindowsEnvironmentVariablesSubstitutions.java similarity index 58% rename from windows-jni-utils/src/main/java/coursier/jniutils/windowsenvironmentvariables/WindowsEnvironmentVariablesSubstitutions.java rename to windows-jni-utils/src/main/java/coursier/jniutils/WindowsEnvironmentVariablesSubstitutions.java index dd70222..444decc 100644 --- a/windows-jni-utils/src/main/java/coursier/jniutils/windowsenvironmentvariables/WindowsEnvironmentVariablesSubstitutions.java +++ b/windows-jni-utils/src/main/java/coursier/jniutils/WindowsEnvironmentVariablesSubstitutions.java @@ -1,4 +1,4 @@ -package coursier.jniutils.windowsenvironmentvariables; +package coursier.jniutils; import com.oracle.svm.core.annotate.Substitute; import com.oracle.svm.core.annotate.TargetClass; @@ -8,17 +8,22 @@ import java.io.IOException; import java.nio.charset.Charset; -@TargetClass(className = "coursier.jniutils.windowsenvironmentvariables.WindowsEnvironmentVariables") +@TargetClass(className = "coursier.jniutils.WindowsEnvironmentVariables") @Platforms({Platform.DARWIN.class, Platform.LINUX.class}) final class WindowsEnvironmentVariablesSubstitutions { @Substitute - public static String get(String key, Charset charset) throws IOException { + public static String get(String key) throws IOException { throw new RuntimeException("Not available on this platform"); } @Substitute - public static void set(String key, String value, Charset charset) throws IOException { + public static void set(String key, String value) throws IOException { + throw new RuntimeException("Not available on this platform"); + } + + @Substitute + public static void delete(String key) throws IOException { throw new RuntimeException("Not available on this platform"); } diff --git a/windows-jni-utils/src/main/java/coursier/jniutils/WindowsKnownFolders.java b/windows-jni-utils/src/main/java/coursier/jniutils/WindowsKnownFolders.java new file mode 100644 index 0000000..8aa4f1d --- /dev/null +++ b/windows-jni-utils/src/main/java/coursier/jniutils/WindowsKnownFolders.java @@ -0,0 +1,8 @@ +package coursier.jniutils; + +public final class WindowsKnownFolders { + + public static String knownFolderPath(String rfid) { + return NativeApi.get().GetKnownFolderPath(rfid); + } +} diff --git a/windows-jni-utils/src/main/java/coursier/jniutils/windowsknownfolders/WindowsKnownFoldersSubstitutions.java b/windows-jni-utils/src/main/java/coursier/jniutils/WindowsKnownFoldersSubstitutions.java similarity index 76% rename from windows-jni-utils/src/main/java/coursier/jniutils/windowsknownfolders/WindowsKnownFoldersSubstitutions.java rename to windows-jni-utils/src/main/java/coursier/jniutils/WindowsKnownFoldersSubstitutions.java index 7321002..2571d64 100644 --- a/windows-jni-utils/src/main/java/coursier/jniutils/windowsknownfolders/WindowsKnownFoldersSubstitutions.java +++ b/windows-jni-utils/src/main/java/coursier/jniutils/WindowsKnownFoldersSubstitutions.java @@ -1,11 +1,11 @@ -package coursier.jniutils.windowsknownfolders; +package coursier.jniutils; import com.oracle.svm.core.annotate.Substitute; import com.oracle.svm.core.annotate.TargetClass; import org.graalvm.nativeimage.Platform; import org.graalvm.nativeimage.Platforms; -@TargetClass(className = "coursier.jniutils.windowsknownfolders.WindowsKnownFolders") +@TargetClass(className = "coursier.jniutils.WindowsKnownFolders") @Platforms({Platform.DARWIN.class, Platform.LINUX.class}) final class WindowsKnownFoldersSubstitutions { diff --git a/windows-jni-utils/src/main/java/coursier/jniutils/windowsknownfolders/WindowsKnownFolders.java b/windows-jni-utils/src/main/java/coursier/jniutils/windowsknownfolders/WindowsKnownFolders.java deleted file mode 100644 index 41e455f..0000000 --- a/windows-jni-utils/src/main/java/coursier/jniutils/windowsknownfolders/WindowsKnownFolders.java +++ /dev/null @@ -1,13 +0,0 @@ -package coursier.jniutils.windowsknownfolders; - -import coursier.jniutils.LoadWindowsLibrary; - -public final class WindowsKnownFolders { - - public static String knownFolderPath(String rfid) { - LoadWindowsLibrary.ensureInitialized(); - return GetKnownFolderPath(rfid); - } - - private static native String GetKnownFolderPath(String rfid); -}