-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate native code to use Java FFM API
- Loading branch information
1 parent
78434fa
commit 35a7f5b
Showing
18 changed files
with
217 additions
and
332 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
72 changes: 72 additions & 0 deletions
72
app/desktop/src/main/java/dev/schlaubi/tonbrett/app/desktop/NativeUtil.java
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,72 @@ | ||
package dev.schlaubi.tonbrett.app.desktop; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.lang.foreign.*; | ||
import java.lang.invoke.MethodHandle; | ||
import java.net.URI; | ||
|
||
/** | ||
* Utility class for calling {@code uwp_helper} functions. | ||
* <p> | ||
* Implemented in Java because {@code @PolymorphicSignature} doesn't seem to work in Kotlin | ||
*/ | ||
public class NativeUtil { | ||
|
||
private static final NativeUtil instance = new NativeUtil(); | ||
private final Linker linker = Linker.nativeLinker(); | ||
private final SymbolLookup uwpHelper = SymbolLookup.libraryLookup("uwp_helper", SegmentScope.global()); | ||
|
||
private final MethodHandle launchUriMethod = linker.downcallHandle( | ||
uwpHelper.find("launch_uri").orElseThrow(), | ||
FunctionDescriptor.ofVoid(ValueLayout.ADDRESS) | ||
); | ||
|
||
private final MethodHandle getAppdataFolder = linker.downcallHandle( | ||
uwpHelper.find("get_appdata_folder").orElseThrow(), | ||
FunctionDescriptor.ofVoid(ValueLayout.ADDRESS) | ||
); | ||
|
||
private final MethodHandle getAppdataFolderPathLength = linker.downcallHandle( | ||
uwpHelper.find("get_appdata_folder_path_length").orElseThrow(), | ||
FunctionDescriptor.of(ValueLayout.JAVA_LONG) | ||
); | ||
|
||
/** | ||
* Tries to launch the URI using the UWP {@code Launcher}. | ||
* | ||
* @param uri the {@link URI} to launch | ||
* @throws Throwable if an error occurs | ||
*/ | ||
public static void launchUri(@NotNull URI uri) throws Throwable { | ||
try (var arena = Arena.openConfined()) { | ||
var url = arena.allocateUtf8String(uri.toString()); | ||
|
||
instance.launchUriMethod.invoke(url); | ||
} | ||
} | ||
|
||
private static long getAppdataFolderLength() throws Throwable{ | ||
return (long) instance.getAppdataFolderPathLength.invoke(); | ||
} | ||
|
||
/** | ||
* Tries to retrieve the current UWP app data folder. | ||
* | ||
* @return the absolute path to the folder | ||
* @throws Throwable if an error occurrs | ||
*/ | ||
@Nullable | ||
public static String getAppdataFolder() throws Throwable { | ||
var length = getAppdataFolderLength(); | ||
if (length == 0) { | ||
return null; | ||
} | ||
try(var arena = Arena.openConfined()) { | ||
var buffer = arena.allocateArray(ValueLayout.JAVA_CHAR, length); | ||
instance.getAppdataFolder.invoke(buffer); | ||
return new String(buffer.toArray(ValueLayout.JAVA_CHAR)); | ||
} | ||
} | ||
} |
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
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,9 @@ | ||
package dev.schlaubi.tonbrett.app.desktop | ||
|
||
import dev.schlaubi.tonbrett.app.api.AppContext | ||
|
||
abstract class ConfigBasedAppContext : AppContext() { | ||
override var token: String | ||
get() = getConfig().sessionToken ?: error("Please sign in") | ||
set(value) = saveConfig(Config(sessionToken = value)) | ||
} |
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
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
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
Oops, something went wrong.