Skip to content

Commit

Permalink
[GLFW] Rewrite GLFW
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Jan 30, 2024
1 parent 06cfc42 commit b668b71
Show file tree
Hide file tree
Showing 31 changed files with 379 additions and 489 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @author squid233
* @since 0.1.0
*/
@Deprecated(since = "0.1.0")
@FunctionalInterface
public interface Addressable {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @author squid233
* @since 0.1.0
*/
@Deprecated(since = "0.1.0")
public interface ArrayPointer extends Addressable {
/**
* {@return the count of the elements in this array}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* @author squid233
* @since 0.1.0
*/
@Deprecated(since = "0.1.0")
public interface Callback {
/**
* Gets the address with the given arena.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @author squid233
* @since 0.1.0
*/
@Deprecated(since = "0.1.0")
public class Pointer implements Addressable {
/**
* The pointer address.
Expand Down
1 change: 1 addition & 0 deletions modules/overrungl.core/src/main/java/overrungl/Struct.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @author squid233
* @since 0.1.0
*/
@Deprecated(since = "0.1.0")
public class Struct extends Pointer {
/**
* The memory layout of this struct.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Overrun Organization
* Copyright (c) 2023-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,6 +22,7 @@
* @author squid233
* @since 0.1.0
*/
@Deprecated(since = "0.1.0")
public final class Exceptions {
/**
* {@link IllegalStateException}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,6 @@ public static String unboundPointerString(MemorySegment segment, int index) {
return reinterpreting(segment, index, str -> MemoryUtil.strlen(str) + 1).getString(0);
}

/**
* Converts the segment into a string.
*
* @param segment the segment
* @return the string
*/
public static String getString(MemorySegment segment) {
return segment.reinterpret(MemoryUtil.strlen(segment) + 1).getString(0);
}

/**
* Generates a string for unknown token.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -39,6 +39,7 @@
* @see Configurations#DEBUG_STACK
* @since 0.1.0
*/
@Deprecated(since = "0.1.0")
public sealed class MemoryStack extends Pointer implements Arena {
private static final boolean DEBUG = Configurations.DEBUG.get();
private static final boolean DEBUG_STACK = Configurations.DEBUG_STACK.get();
Expand Down
113 changes: 56 additions & 57 deletions modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFW.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,8 +18,8 @@

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* The GLFW window callbacks.
Expand All @@ -28,7 +28,7 @@
* @since 0.1.0
*/
public final class GLFWCallbacks {
private static final Map<MemorySegment, Arena> ARENA_MAP = new HashMap<>();
private static final Map<MemorySegment, Arena> ARENA_MAP = new ConcurrentHashMap<>();

private GLFWCallbacks() {
//no instance
Expand All @@ -42,7 +42,7 @@ private GLFWCallbacks() {
* @return the arena.
*/
public static Arena create(MemorySegment window) {
return ARENA_MAP.computeIfAbsent(window, k -> Arena.ofConfined());
return ARENA_MAP.computeIfAbsent(window, _ -> Arena.ofConfined());
}

/**
Expand All @@ -52,6 +52,22 @@ public static Arena create(MemorySegment window) {
*/
public static void free(MemorySegment window) {
if (ARENA_MAP.containsKey(window)) {
GLFW.setCharCallback(window, null);
GLFW.setCursorEnterCallback(window, null);
GLFW.setCursorPosCallback(window, null);
GLFW.setDropCallback(window, null);
GLFW.setFramebufferSizeCallback(window, null);
GLFW.setKeyCallback(window, null);
GLFW.setMouseButtonCallback(window, null);
GLFW.setScrollCallback(window, null);
GLFW.setWindowCloseCallback(window, null);
GLFW.setWindowContentScaleCallback(window, null);
GLFW.setWindowFocusCallback(window, null);
GLFW.setWindowIconifyCallback(window, null);
GLFW.setWindowMaximizeCallback(window, null);
GLFW.setWindowPosCallback(window, null);
GLFW.setWindowRefreshCallback(window, null);
GLFW.setWindowSizeCallback(window, null);
ARENA_MAP.remove(window).close();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -16,47 +16,40 @@

package overrungl.glfw;

import overrungl.Callback;
import overrun.marshal.Upcall;

import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
* This is the function pointer type for Unicode character callbacks.
* A Unicode character callback function has the following signature:
* {@snippet :
* @Invoker(IGLFWCharFun::invoke)
* void functionName(MemorySegment window, int codepoint);
* void functionName(MemorySegment window, int codepoint); // @link regex="functionName" target="#invoke"
* }
*
* @author squid233
* @see GLFW#setCharCallback
* @since 0.1.0
*/
@FunctionalInterface
public interface IGLFWCharFun extends Callback {
FunctionDescriptor DESC = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_INT);
MethodType MTYPE = DESC.toMethodType();
public interface GLFWCharFun extends Upcall {
/**
* The type.
*/
Type<GLFWCharFun> TYPE = Upcall.type();

/**
* The function pointer type for Unicode character callbacks.
*
* @param window The window that received the event.
* @param codepoint The Unicode code point of the character.
*/
@Stub
void invoke(MemorySegment window, int codepoint);

@Override
default FunctionDescriptor descriptor() {
return DESC;
}

@Override
default MethodHandle handle(MethodHandles.Lookup lookup) throws NoSuchMethodException, IllegalAccessException {
return lookup.findVirtual(IGLFWCharFun.class, "invoke", MTYPE);
default MemorySegment stub(Arena arena) {
return TYPE.of(arena, this);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -16,31 +16,28 @@

package overrungl.glfw;

import overrungl.Callback;
import overrun.marshal.Upcall;

import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
* This is the function pointer type for cursor enter/leave callbacks.
* A cursor enter/leave callback function has the following signature:
* {@snippet :
* @Invoker(IGLFWCursorEnterFun::invoke)
* void functionName(MemorySegment window, boolean entered);
* void functionName(MemorySegment window, boolean entered); // @link regex="functionName" target="#invoke"
* }
*
* @author squid233
* @see GLFW#setCursorEnterCallback
* @since 0.1.0
*/
@FunctionalInterface
public interface IGLFWCursorEnterFun extends Callback {
FunctionDescriptor DESC = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_INT);
MethodType MTYPE = DESC.toMethodType();
public interface GLFWCursorEnterFun extends Upcall {
/**
* The type.
*/
Type<GLFWCursorEnterFun> TYPE = Upcall.type();

/**
* The function pointer type for cursor enter/leave callbacks.
Expand All @@ -51,17 +48,20 @@ public interface IGLFWCursorEnterFun extends Callback {
*/
void invoke(MemorySegment window, boolean entered);

/**
* The function pointer type for cursor enter/leave callbacks.
*
* @param window The window that received the event.
* @param entered {@code true} if the cursor entered the window's content
* area, or {@code false} if it left it.
*/
@Stub
default void ninvoke(MemorySegment window, int entered) {
invoke(window, entered != GLFW.FALSE);
}

@Override
default FunctionDescriptor descriptor() {
return DESC;
}

@Override
default MethodHandle handle(MethodHandles.Lookup lookup) throws NoSuchMethodException, IllegalAccessException {
return lookup.findVirtual(IGLFWCursorEnterFun.class, "ninvoke", MTYPE);
default MemorySegment stub(Arena arena) {
return TYPE.of(arena, this);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -16,31 +16,28 @@

package overrungl.glfw;

import overrungl.Callback;
import overrun.marshal.Upcall;

import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
* This is the function pointer type for cursor position callbacks. A cursor
* position callback function has the following signature:
* {@snippet :
* @Invoker(IGLFWCursorPosFun::invoke)
* void functionName(MemorySegment window, double xpos, double ypos);
* void functionName(MemorySegment window, double xpos, double ypos); // @link regex="functionName" target="#invoke"
* }
*
* @author squid233
* @see GLFW#setCursorPosCallback
* @since 0.1.0
*/
@FunctionalInterface
public interface IGLFWCursorPosFun extends Callback {
FunctionDescriptor DESC = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_DOUBLE, ValueLayout.JAVA_DOUBLE);
MethodType MTYPE = DESC.toMethodType();
public interface GLFWCursorPosFun extends Upcall {
/**
* The type.
*/
Type<GLFWCursorPosFun> TYPE = Upcall.type();

/**
* The function pointer type for cursor position callbacks.
Expand All @@ -49,15 +46,11 @@ public interface IGLFWCursorPosFun extends Callback {
* @param xpos The new cursor x-coordinate, relative to the left edge of the content area.
* @param ypos The new cursor y-coordinate, relative to the top edge of the content area.
*/
@Stub
void invoke(MemorySegment window, double xpos, double ypos);

@Override
default FunctionDescriptor descriptor() {
return DESC;
}

@Override
default MethodHandle handle(MethodHandles.Lookup lookup) throws NoSuchMethodException, IllegalAccessException {
return lookup.findVirtual(IGLFWCursorPosFun.class, "invoke", MTYPE);
default MemorySegment stub(Arena arena) {
return TYPE.of(arena, this);
}
}
Loading

0 comments on commit b668b71

Please sign in to comment.