diff --git a/.gitignore b/.gitignore index 58bdca52..28c895be 100644 --- a/.gitignore +++ b/.gitignore @@ -57,6 +57,9 @@ nb-configuration.xml ## OS X .DS_Store +## nix-shell +/shell.nix + # Development todo/ /ci/qemu/ diff --git a/CHANGES.md b/CHANGELOG.md similarity index 90% rename from CHANGES.md rename to CHANGELOG.md index ab09d8ae..20c04de9 100644 --- a/CHANGES.md +++ b/CHANGELOG.md @@ -1,4 +1,11 @@ -# Release (0.4.0) +# Release (0.5.0) + +- Add `shell.nix` as documentation +- Support JDK21 +- Library: Remove dependency to libc +- CI: Update dependencies: gradle:8.5, kotlin:1.9.22 + +# 0.4.0 - Generator: Improve record support (structure and callbacks) - Generator: Java doc for callback class diff --git a/README.md b/README.md index 713a276d..b6f68583 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ There are more demo applications in [examples/src/main/java/examples](examples/s Compile Java library, generate JAR archive and copy JAR archive as artifact to local Maven repository (`~/.m2/repository`). ## Integration -Library and [Javadoc](https://javadoc.jitpack.io/com/github/bailuk/java-gtk/0.4.0/javadoc/) is available via [JitPack](https://jitpack.io). +Library and [Javadoc](https://javadoc.jitpack.io/com/github/bailuk/java-gtk/0.5.0/javadoc/) is available via [JitPack](https://jitpack.io). ```kotlin // build.gradle.kts @@ -87,7 +87,7 @@ repositories { } dependencies { - implementation("com.github.bailuk:java-gtk:0.4.0") + implementation("com.github.bailuk:java-gtk:0.5.0") } application { diff --git a/build.gradle.kts b/build.gradle.kts index 152c96b9..70c8f5be 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,17 +2,9 @@ plugins { /** * https://github.com/allegro/axion-release-plugin * - * 1. Current build version + * Current build version * ./gradlew cV * - * 2. Code freeze - * git tag v0.4.0-rc.1 -m "Code freeze" - * git push origin v0.4.0-rc.1 - * - * 3. Release - * -> Update README.md - * git tag v0.4.0 -m "Release" - * git push origin v0.4.0 */ id ("pl.allegro.tech.build.axion-release") version "1.15.0" } diff --git a/doc/shell.nix b/doc/shell.nix new file mode 100755 index 00000000..030423d2 --- /dev/null +++ b/doc/shell.nix @@ -0,0 +1,21 @@ +# Development environment with nix-shell +{ pkgs ? import {} +}: +pkgs.mkShell { + name="java-gtk"; + buildInputs = [ + pkgs.git + pkgs.jdk21_headless # Non headless is linked against gtk3 and does therefore not work + pkgs.gtk4 + ]; + shellHook = '' + LD_LIBRARY_PATH=${pkgs.gtk4.outPath}/lib + LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pkgs.glib.out.outPath}/lib + LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pkgs.gdk-pixbuf.outPath}/lib + LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pkgs.cairo.outPath}/lib + LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pkgs.pango.out.outPath}/lib + LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pkgs.libadwaita.outPath}/lib + export LD_LIBRARY_PATH + echo "./gradlew generate && ./gradlew build && ./gradlew run" + ''; +} diff --git a/examples/src/main/java/examples/dnd/DragAndDrop.java b/examples/src/main/java/examples/dnd/DragAndDrop.java new file mode 100644 index 00000000..42205c85 --- /dev/null +++ b/examples/src/main/java/examples/dnd/DragAndDrop.java @@ -0,0 +1,53 @@ +package examples.dnd; + +import ch.bailu.gtk.gdk.GdkConstants; +import ch.bailu.gtk.gio.ApplicationFlags; +import ch.bailu.gtk.gio.File; +import ch.bailu.gtk.gtk.Application; +import ch.bailu.gtk.gtk.ApplicationWindow; +import ch.bailu.gtk.gtk.DropTarget; +import ch.bailu.gtk.gtk.Label; +import ch.bailu.gtk.type.Str; +import ch.bailu.gtk.type.Strs; + +/** + * https://docs.gtk.org/gtk4/drag-and-drop.html + */ +public class DragAndDrop { + public final static Str ID = new Str("org.gtk.example.dnd"); + + public static void main(String[] args) { + + var app = new Application(ID, ApplicationFlags.FLAGS_NONE); + app.onActivate(() -> { + // Get and initialize application window + var window = new ApplicationWindow(app); + window.setTitle("Drag and drop demo"); + window.setDefaultSize(400,300); + + // Create a label to display dropped files + var label = new Label("Drop files here"); + label.setHexpand(true); + label.setVexpand(true); + + // Crate drop target + var target = new DropTarget(File.getTypeID(), GdkConstants.ACTION_ALL); + target.onDrop((value, x, y) -> { + System.out.println("dropped"); + var file = new File(value.getObject().cast()); + label.setText(file.getBasename()); + return true; + }); + + // Make the label a drop target + label.addController(target); + + // Compose and display + window.setChild(label); + window.show(); + }); + + + System.exit(app.run(args.length, new Strs(args))); + } +} diff --git a/examples/src/main/java/examples/libadwaita/demo/AdwaitaDemo.java b/examples/src/main/java/examples/libadwaita/demo/AdwaitaDemo.java index b97c2de8..eeed17ef 100644 --- a/examples/src/main/java/examples/libadwaita/demo/AdwaitaDemo.java +++ b/examples/src/main/java/examples/libadwaita/demo/AdwaitaDemo.java @@ -1,18 +1,13 @@ package examples.libadwaita.demo; -import java.io.IOException; - import ch.bailu.gtk.adw.AboutWindow; import ch.bailu.gtk.adw.Application; import ch.bailu.gtk.gio.ApplicationFlags; -import ch.bailu.gtk.gio.Resource; import ch.bailu.gtk.gtk.License; import ch.bailu.gtk.gtk.Window; +import ch.bailu.gtk.lib.bridge.GResource; import ch.bailu.gtk.lib.handler.action.ActionHandler; -import ch.bailu.gtk.lib.util.JavaResource; -import ch.bailu.gtk.type.Bytes; import ch.bailu.gtk.type.Strs; -import ch.bailu.gtk.type.exception.AllocationError; /** * Almost complete port of the official demo from C to Java @@ -20,7 +15,7 @@ * */ public class AdwaitaDemo { - private static Strs developers = new Strs(new String[] { + private static final Strs developers = new Strs(new String[] { "Adrien Plazas", "Alexander Mikhaylenko", "Andrei Lișiță", @@ -33,13 +28,13 @@ public class AdwaitaDemo { null }); - private static Strs designers = new Strs(new String[] { + private static final Strs designers = new Strs(new String[] { "GNOME Design Team", null }); public static void main(String[] args) { - loadAndRegisterGResource("/adw_demo/adwaita-demo.gresources.gresource"); + GResource.loadAndRegister("/adw_demo/adwaita-demo.gresources.gresource"); final var app = new Application("org.gnome.Adwaita1.Demo", ApplicationFlags.NON_UNIQUE); @@ -55,17 +50,6 @@ public static void main(String[] args) { app.unref(); } - private static void loadAndRegisterGResource(String path) { - // TODO wouldn't it be nice if resource could be loaded directly from java resources instead of .gresource? - try (var stream = (new JavaResource(path).asStream())) { - var bytes = new Bytes(stream.readAllBytes()); - var resource = Resource.newFromDataResource(ch.bailu.gtk.glib.Bytes.newStaticBytes(bytes, bytes.getLength())); - resource.register(); - } catch (IOException | AllocationError e) { - System.err.println("Load gresource failed for '" + path + "'"); - } - } - private static void showAbout(Application app) { final var about = new AboutWindow(); about.setTransientFor(app.getActiveWindow()); diff --git a/generator/build.gradle.kts b/generator/build.gradle.kts index e62992f7..4fd9d0d6 100644 --- a/generator/build.gradle.kts +++ b/generator/build.gradle.kts @@ -3,7 +3,7 @@ plugins { java // https://kotlinlang.org/docs/gradle-configure-project.html - kotlin("jvm") version "1.8.20" + kotlin("jvm") version "1.9.22" } repositories { diff --git a/generator/src/main/kotlin/ch/bailu/gtk/writer/java/JavaImpWriter.kt b/generator/src/main/kotlin/ch/bailu/gtk/writer/java/JavaImpWriter.kt index 7083a0f4..5e65fe63 100644 --- a/generator/src/main/kotlin/ch/bailu/gtk/writer/java/JavaImpWriter.kt +++ b/generator/src/main/kotlin/ch/bailu/gtk/writer/java/JavaImpWriter.kt @@ -105,7 +105,7 @@ class JavaImpWriter(private val out: TextWriter) : CodeWriter { _size = new Fields().size(); System.out.println("${structureModel.apiName} size: " + _size + " bytes"); } - return ch.bailu.gtk.lib.jna.CLib.allocate(_size); + return ch.bailu.gtk.type.Imp.allocate(_size); } @com.sun.jna.Structure.FieldOrder({${getFields(structureModel, fields)}}) diff --git a/generator/src/main/resources/cairo-custom.gir b/generator/src/main/resources/cairo-custom.gir index b6cd024c..1c97f7ca 100644 --- a/generator/src/main/resources/cairo-custom.gir +++ b/generator/src/main/resources/cairo-custom.gir @@ -185,6 +185,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 249e5832..c1962a79 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 59bc51a2..3499ded5 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip +networkTimeout=10000 zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index a69d9cb6..aeb74cbb 100755 --- a/gradlew +++ b/gradlew @@ -55,7 +55,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. @@ -80,13 +80,10 @@ do esac done -APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit - -APP_NAME="Gradle" +# This is normally unused +# shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -143,12 +140,16 @@ fi if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then case $MAX_FD in #( max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 MAX_FD=$( ulimit -H -n ) || warn "Could not query maximum file descriptor limit" esac case $MAX_FD in #( '' | soft) :;; #( *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 ulimit -n "$MAX_FD" || warn "Could not set maximum file descriptor limit to $MAX_FD" esac @@ -193,6 +194,10 @@ if "$cygwin" || "$msys" ; then done fi + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + # Collect all arguments for the java command; # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of # shell script including quotes and variable substitutions, so put them in diff --git a/gradlew.bat b/gradlew.bat index 53a6b238..6689b85b 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -26,6 +26,7 @@ if "%OS%"=="Windows_NT" setlocal set DIRNAME=%~dp0 if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% diff --git a/java-gtk/src/main/java/ch/bailu/gtk/lib/bridge/GResource.java b/java-gtk/src/main/java/ch/bailu/gtk/lib/bridge/GResource.java new file mode 100644 index 00000000..92ed5904 --- /dev/null +++ b/java-gtk/src/main/java/ch/bailu/gtk/lib/bridge/GResource.java @@ -0,0 +1,28 @@ +package ch.bailu.gtk.lib.bridge; + +import java.io.IOException; + +import ch.bailu.gtk.gio.Resource; +import ch.bailu.gtk.lib.util.JavaResource; +import ch.bailu.gtk.type.Bytes; +import ch.bailu.gtk.type.exception.AllocationError; + +public class GResource { + + /** + * Load a gresource bundle from java resources path and register it + * See {@link ch.bailu.gtk.gio.Resource} for documentation on how to generate + * gresource bundles. + * + * @param path absolute path to gresource: "/gresource/app.gresource" + */ + public static void loadAndRegister(String path) { + try (var stream = (new JavaResource(path).asStream())) { + var bytes = new Bytes(stream.readAllBytes()); + var resource = Resource.newFromDataResource(ch.bailu.gtk.glib.Bytes.newStaticBytes(bytes, bytes.getLength())); + resource.register(); + } catch (IOException | AllocationError e) { + System.err.println("Load gresource failed for '" + path + "'"); + } + } +} diff --git a/java-gtk/src/main/java/ch/bailu/gtk/lib/jna/CLib.java b/java-gtk/src/main/java/ch/bailu/gtk/lib/jna/CLib.java deleted file mode 100644 index 9aa8c023..00000000 --- a/java-gtk/src/main/java/ch/bailu/gtk/lib/jna/CLib.java +++ /dev/null @@ -1,28 +0,0 @@ -package ch.bailu.gtk.lib.jna; - -import com.sun.jna.Library; - - -public class CLib { - - private static Instance _INST = null; - - public static Instance INST() { - if (_INST == null) { - _INST =Loader.load("c", Instance.class); - } - return _INST; - } - - public interface Instance extends Library { - long malloc(long size); - long memset(long pointer, int value, long size); - void free(long pointer); - } - - public static long allocate(long size) { - long result = INST().malloc(size); - INST().memset(result, 0, size); - return result; - } -} diff --git a/java-gtk/src/main/java/ch/bailu/gtk/type/Array.java b/java-gtk/src/main/java/ch/bailu/gtk/type/Array.java index aea05510..37034ae3 100644 --- a/java-gtk/src/main/java/ch/bailu/gtk/type/Array.java +++ b/java-gtk/src/main/java/ch/bailu/gtk/type/Array.java @@ -1,5 +1,7 @@ package ch.bailu.gtk.type; +import ch.bailu.gtk.glib.Glib; + public abstract class Array extends Wrapper { private final static int SIZE_UNKNOWN = -1; @@ -20,7 +22,7 @@ public Array(PointerContainer pointer, int bytes, int length) { @Override public void destroy() { if (length != 0) { - ImpUtil.destroy(asCPointer()); + Glib.free(asPointer()); length = 0; } } diff --git a/java-gtk/src/main/java/ch/bailu/gtk/type/Flt.java b/java-gtk/src/main/java/ch/bailu/gtk/type/Flt.java index 74ac708a..4ca71e8d 100644 --- a/java-gtk/src/main/java/ch/bailu/gtk/type/Flt.java +++ b/java-gtk/src/main/java/ch/bailu/gtk/type/Flt.java @@ -40,7 +40,7 @@ private static PointerContainer createFloatArray(float[] values) { } private static PointerContainer createFlt(float value) { - return new PointerContainer(ImpDbl.createFlt(value)); + return new PointerContainer(ImpFlt.createFlt(value)); } public static Flt create(float value) { diff --git a/java-gtk/src/main/java/ch/bailu/gtk/type/Imp.java b/java-gtk/src/main/java/ch/bailu/gtk/type/Imp.java new file mode 100644 index 00000000..b74ce407 --- /dev/null +++ b/java-gtk/src/main/java/ch/bailu/gtk/type/Imp.java @@ -0,0 +1,17 @@ +package ch.bailu.gtk.type; + +import ch.bailu.gtk.glib.Glib; + +public class Imp { + public static long createPointerArray(long[] pointers) { + long size = (long) pointers.length * Long.BYTES; + Pointer result = Glib.malloc(size); + Pointer.asJnaPointer(result).write(0, pointers, 0, pointers.length); + return result.asCPointer(); + } + + public static long allocate(long size) { + var result = Glib.malloc0(size); + return result.asCPointer(); + } +} diff --git a/java-gtk/src/main/java/ch/bailu/gtk/type/ImpBytes.java b/java-gtk/src/main/java/ch/bailu/gtk/type/ImpBytes.java index 0c57a0fd..72531d53 100644 --- a/java-gtk/src/main/java/ch/bailu/gtk/type/ImpBytes.java +++ b/java-gtk/src/main/java/ch/bailu/gtk/type/ImpBytes.java @@ -1,20 +1,18 @@ package ch.bailu.gtk.type; -import ch.bailu.gtk.lib.jna.CLib; +import ch.bailu.gtk.glib.Glib; class ImpBytes { public static long createBytes(int size) { - var result = CLib.INST().malloc(size); - CLib.INST().memset(result, 0, size); - return result; + var result = Glib.malloc0(size); + return result.asCPointer(); } public static long createBytes(byte[] bytes) { - long result = CLib.INST().malloc(bytes.length); + var result = Glib.malloc(bytes.length); + result.asJnaPointer().write(0, bytes, 0, bytes.length); - com.sun.jna.Pointer pointer = Pointer.asJnaPointer(result); - pointer.write(0, bytes, 0, bytes.length); - return result; + return result.asCPointer(); } public static byte getByte(long cPointer, int index) { diff --git a/java-gtk/src/main/java/ch/bailu/gtk/type/ImpDbl.java b/java-gtk/src/main/java/ch/bailu/gtk/type/ImpDbl.java index 69677967..ae2433f6 100644 --- a/java-gtk/src/main/java/ch/bailu/gtk/type/ImpDbl.java +++ b/java-gtk/src/main/java/ch/bailu/gtk/type/ImpDbl.java @@ -1,22 +1,22 @@ package ch.bailu.gtk.type; -import ch.bailu.gtk.lib.jna.CLib; +import ch.bailu.gtk.glib.Glib; class ImpDbl { public static long createDoubleArray(double[] doubles) { - long result = CLib.INST().malloc((long) doubles.length * Double.BYTES); - Pointer.asJnaPointer(result).write(0, doubles, 0, doubles.length); - return result; + var result = Glib.malloc((long) doubles.length * Double.BYTES); + result.asJnaPointer().write(0, doubles, 0, doubles.length); + return result.asCPointer(); } public static long createDoubleArrayFromFloats(float[] floats) { - long result = CLib.INST().malloc((long) floats.length * Double.BYTES); - com.sun.jna.Pointer p = Pointer.asJnaPointer(result); + var result = Glib.malloc((long) floats.length * Double.BYTES); + com.sun.jna.Pointer p = result.asJnaPointer(); for (int i = 0; i< floats.length; i++) { p.setDouble((long) i *Double.BYTES, floats[i]); } - return result; + return result.asCPointer(); } @@ -25,27 +25,12 @@ public static void setAt(long cPointer, int index, double value) { } public static long createDbl(double value) { - long result = CLib.INST().malloc(Double.BYTES); - Pointer.asJnaPointer(result).setDouble(0, value); - return result; - } - - public static long createFlt(float value) { - long result = CLib.INST().malloc(Float.BYTES); - Pointer.asJnaPointer(result).setFloat(0, value); - return result; + var result = Glib.malloc(Double.BYTES); + result.asJnaPointer().setDouble(0, value); + return result.asCPointer(); } public static double getAt(long cPointer, int index) { return Pointer.asJnaPointer(cPointer).getDouble((long) index * Double.BYTES); } - - public static long createFloatArrayFromFloats(float[] floats) { - long result = CLib.INST().malloc((long) floats.length * Float.BYTES); - com.sun.jna.Pointer p = Pointer.asJnaPointer(result); - for (int i = 0; i< floats.length; i++) { - p.setDouble((long) i * Float.BYTES, floats[i]); - } - return result; - } } diff --git a/java-gtk/src/main/java/ch/bailu/gtk/type/ImpFlt.java b/java-gtk/src/main/java/ch/bailu/gtk/type/ImpFlt.java index b48e104b..4885551a 100644 --- a/java-gtk/src/main/java/ch/bailu/gtk/type/ImpFlt.java +++ b/java-gtk/src/main/java/ch/bailu/gtk/type/ImpFlt.java @@ -1,6 +1,6 @@ package ch.bailu.gtk.type; -import ch.bailu.gtk.lib.jna.CLib; +import ch.bailu.gtk.glib.Glib; class ImpFlt { public static void setAt(long cPointer, int index, float value) { @@ -8,21 +8,22 @@ public static void setAt(long cPointer, int index, float value) { } public static long createFlt(float value) { - long result = CLib.INST().malloc(Float.BYTES); - Pointer.asJnaPointer(result).setFloat(0, value); - return result; + var result = Glib.malloc(Float.BYTES); + result.asJnaPointer().setFloat(0, value); + return result.asCPointer(); } public static float getAt(long cPointer, int index) { return Pointer.asJnaPointer(cPointer).getFloat((long) index * Float.BYTES); } + public static long createFloatArray(float[] floats) { - long result = CLib.INST().malloc((long) floats.length * Float.BYTES); - com.sun.jna.Pointer p = Pointer.asJnaPointer(result); + var result = Glib.malloc((long) floats.length * Float.BYTES); + com.sun.jna.Pointer p = result.asJnaPointer(); for (int i = 0; i< floats.length; i++) { p.setFloat((long) i * Float.BYTES, floats[i]); } - return result; + return result.asCPointer(); } } diff --git a/java-gtk/src/main/java/ch/bailu/gtk/type/ImpInt.java b/java-gtk/src/main/java/ch/bailu/gtk/type/ImpInt.java index 6cc43bc9..7f0b33f9 100644 --- a/java-gtk/src/main/java/ch/bailu/gtk/type/ImpInt.java +++ b/java-gtk/src/main/java/ch/bailu/gtk/type/ImpInt.java @@ -1,13 +1,13 @@ package ch.bailu.gtk.type; -import ch.bailu.gtk.lib.jna.CLib; +import ch.bailu.gtk.glib.Glib; class ImpInt { public static long createInt(int value) { - long result = CLib.INST().malloc(Integer.BYTES); - Pointer.asJnaPointer(result).setInt(0, value); - return result; + var result = Glib.malloc(Integer.BYTES); + result.asJnaPointer().setInt(0, value); + return result.asCPointer(); } public static void set(long cPointer, int value) { @@ -20,9 +20,9 @@ public static int get(long cPointer) { public static long createLong(long value) { - long result = CLib.INST().malloc(Long.BYTES); - Pointer.asJnaPointer(result).setLong(0, value); - return result; + var result = Glib.malloc(Long.BYTES); + result.asJnaPointer().setLong(0, value); + return result.asCPointer(); } public static void setLong(long cPointer, long value) { diff --git a/java-gtk/src/main/java/ch/bailu/gtk/type/ImpUtil.java b/java-gtk/src/main/java/ch/bailu/gtk/type/ImpUtil.java deleted file mode 100644 index 0ab7d041..00000000 --- a/java-gtk/src/main/java/ch/bailu/gtk/type/ImpUtil.java +++ /dev/null @@ -1,16 +0,0 @@ -package ch.bailu.gtk.type; - -import ch.bailu.gtk.lib.jna.CLib; - -class ImpUtil { - public static long createPointerArray(long[] pointers) { - long size = (long) pointers.length * Long.BYTES; - long result = CLib.INST().malloc(size); - Pointer.asJnaPointer(result).write(0, pointers, 0, pointers.length); - return result; - } - - public static void destroy(long cPointer) { - CLib.INST().free(cPointer); - } -} diff --git a/java-gtk/src/main/java/ch/bailu/gtk/type/Int.java b/java-gtk/src/main/java/ch/bailu/gtk/type/Int.java index 937027a5..37db2c18 100644 --- a/java-gtk/src/main/java/ch/bailu/gtk/type/Int.java +++ b/java-gtk/src/main/java/ch/bailu/gtk/type/Int.java @@ -1,5 +1,7 @@ package ch.bailu.gtk.type; +import ch.bailu.gtk.glib.Glib; + public class Int extends Wrapper { public final static Int NULL = new Int(new PointerContainer(0)); @@ -43,7 +45,7 @@ public boolean is() { @Override public void destroy() { if (created) { - ImpUtil.destroy(asCPointer()); + Glib.free(asPointer()); created = false; } } diff --git a/java-gtk/src/main/java/ch/bailu/gtk/type/Int64.java b/java-gtk/src/main/java/ch/bailu/gtk/type/Int64.java index 45294367..54e3216d 100644 --- a/java-gtk/src/main/java/ch/bailu/gtk/type/Int64.java +++ b/java-gtk/src/main/java/ch/bailu/gtk/type/Int64.java @@ -1,5 +1,7 @@ package ch.bailu.gtk.type; +import ch.bailu.gtk.glib.Glib; + /** * https://www.w3schools.com/java/java_data_types.asp */ @@ -35,7 +37,7 @@ public long get() { @Override public void destroy() { if (created) { - ImpUtil.destroy(asCPointer()); + Glib.free(asPointer()); created = false; } } diff --git a/java-gtk/src/main/java/ch/bailu/gtk/type/Record.java b/java-gtk/src/main/java/ch/bailu/gtk/type/Record.java index 5fae3b84..7c58eab6 100644 --- a/java-gtk/src/main/java/ch/bailu/gtk/type/Record.java +++ b/java-gtk/src/main/java/ch/bailu/gtk/type/Record.java @@ -1,5 +1,7 @@ package ch.bailu.gtk.type; +import ch.bailu.gtk.glib.Glib; + /** * Record: a structure that is not based on gobject * @@ -15,6 +17,6 @@ public Record(PointerContainer pointer) { * Object is invalid afterwards and should not be accessed. */ public void destroy() { - ch.bailu.gtk.lib.jna.CLib.INST().free(asCPointer()); + Glib.free(asPointer()); } } diff --git a/java-gtk/src/main/java/ch/bailu/gtk/type/Strs.java b/java-gtk/src/main/java/ch/bailu/gtk/type/Strs.java index 79cf4fc4..46250d50 100644 --- a/java-gtk/src/main/java/ch/bailu/gtk/type/Strs.java +++ b/java-gtk/src/main/java/ch/bailu/gtk/type/Strs.java @@ -1,5 +1,7 @@ package ch.bailu.gtk.type; +import ch.bailu.gtk.glib.Glib; + public class Strs extends Pointer { public final static Strs NULL = new Strs(PointerContainer.NULL); @@ -17,7 +19,7 @@ public Strs(String[] strings) { } public Strs(Str[] strs) { - this(strs, new PointerContainer(ImpUtil.createPointerArray(Util.toPointerArray(strs)))); + this(strs, new PointerContainer(Imp.createPointerArray(Util.toPointerArray(strs)))); } public Strs(Str[] strs, PointerContainer pointers) { @@ -90,7 +92,7 @@ public void destroy() { public void destroyAll() { if (strs != null) { for (Pointer pointer : strs) { - ImpUtil.destroy(pointer.asCPointer()); + Glib.free(pointer); } destroyArray(); } @@ -99,7 +101,7 @@ public void destroyAll() { private void destroyArray() { if (strs != null) { - ImpUtil.destroy(asCPointer()); + Glib.free(asPointer()); } strs = null; } diff --git a/java-gtk/src/test/java/ch/bailu/gtk/TestGBytes.java b/java-gtk/src/test/java/ch/bailu/gtk/TestGBytes.java index c07a5328..25fb9d1a 100644 --- a/java-gtk/src/test/java/ch/bailu/gtk/TestGBytes.java +++ b/java-gtk/src/test/java/ch/bailu/gtk/TestGBytes.java @@ -16,19 +16,15 @@ public void testBytes() { Bytes bytesA = new Bytes(buffer); - assertEquals(200, bytesA.getSize()); assertEquals(42, bytesA.getByte(0)); assertEquals(99, bytesA.getByte(199)); bytesA.unref(); - assertEquals(0, bytesA.getSize()); - Bytes bytesB = new Bytes(new byte[0]); assertEquals(0, bytesB.getSize()); bytesB.unref(); - Bytes bytesC = new Bytes(new byte[]{4,5,0}); byte[] res = bytesC.toBytes(); assertEquals(3, res.length); diff --git a/java-gtk/src/test/java/ch/bailu/gtk/TestInt.java b/java-gtk/src/test/java/ch/bailu/gtk/TestInt.java index 35f8d705..e34a7cda 100644 --- a/java-gtk/src/test/java/ch/bailu/gtk/TestInt.java +++ b/java-gtk/src/test/java/ch/bailu/gtk/TestInt.java @@ -3,25 +3,49 @@ import org.junit.jupiter.api.Test; import ch.bailu.gtk.type.Int; +import ch.bailu.gtk.type.Int64; import static org.junit.jupiter.api.Assertions.assertEquals; public class TestInt { + @Test + public void testInt() { + var a = new Int(); + assertEquals(0, a.get()); - @Test - public void testInt() { - Int i = new Int(); - assertEquals(0, i.get()); + a.set(290); + assertEquals(290, a.get()); - i.set(290); - assertEquals(290, i.get()); + a.set(Integer.MAX_VALUE); + assertEquals(Integer.MAX_VALUE, a.get()); - i.set(Integer.MAX_VALUE); - assertEquals(Integer.MAX_VALUE, i.get()); + a.set(Integer.MIN_VALUE); + assertEquals(Integer.MIN_VALUE, a.get()); + a.destroy(); - i.set(Integer.MIN_VALUE); - assertEquals(Integer.MIN_VALUE, i.get()); - } + var b = Int.create(-1); + assertEquals(-1, b.get()); + b.destroy(); + } + @Test + public void testInt64() { + var a = new Int64(); + assertEquals(0L, a.get()); + + a.set(290); + assertEquals(290L, a.get()); + + a.set(Long.MAX_VALUE); + assertEquals(Long.MAX_VALUE, a.get()); + + a.set(Long.MIN_VALUE); + assertEquals(Long.MIN_VALUE, a.get()); + a.destroy(); + + var b = Int64.create(-1L); + assertEquals(-1L, b.get()); + b.destroy(); + } }