More work on the native input

This commit is contained in:
artdeell
2023-03-02 23:01:25 +03:00
parent 21cfe0515a
commit de6c03049a
5 changed files with 71 additions and 93 deletions

View File

@@ -1 +1 @@
1677612008557
1677787112613

View File

@@ -43,14 +43,12 @@ typedef void GLFW_invoke_Scroll_func(void* window, double xoffset, double yoffse
typedef void GLFW_invoke_WindowSize_func(void* window, int width, int height);
static float grabCursorX, grabCursorY, lastCursorX, lastCursorY;
static double cursorX, cursorY, cLastX, cLastY;
jclass inputBridgeClass_ANDROID, inputBridgeClass_JRE;
jmethodID inputBridgeMethod_ANDROID, inputBridgeMethod_JRE;
jmethodID method_accessAndroidClipboard;
jmethodID method_onGrabStateChanged;
jmethodID method_glftSetWindowAttrib;
jmethodID method_internalWindowSizeChanged;
jmethodID method_fallbackWriteKey = NULL;
jclass bridgeClazz;
jclass vmGlfwClass;
jboolean isGrabbing;
@@ -74,7 +72,7 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {
jfieldID field_keyDownBuffer = (*runtimeJNIEnvPtr_JRE)->GetStaticFieldID(runtimeJNIEnvPtr_JRE, vmGlfwClass, "keyDownBuffer", "Ljava/nio/ByteBuffer;");
jobject keyDownBufferJ = (*runtimeJNIEnvPtr_JRE)->GetStaticObjectField(runtimeJNIEnvPtr_JRE, vmGlfwClass, field_keyDownBuffer);
keyDownBuffer = (*runtimeJNIEnvPtr_JRE)->GetDirectBufferAddress(runtimeJNIEnvPtr_JRE, keyDownBufferJ);
hookExec();
hookExec();
}
isGrabbing = JNI_FALSE;
@@ -150,7 +148,6 @@ void handleFramebufferSizeJava(long window, int w, int h) {
void pojavPumpEvents(void* window) {
//__android_log_print(ANDROID_LOG_INFO, "input_bridge_v3", "pojavPumpEvents %d", eventCounter);
for(size_t i = 0; i < eventCounter; i++) {
GLFWInputEvent event = events[i];
switch(event.type) {
@@ -162,6 +159,7 @@ void pojavPumpEvents(void* window) {
break;
case EVENT_TYPE_KEY:
if(GLFW_invoke_Key) GLFW_invoke_Key(window, event.i1, event.i2, event.i3, event.i4);
break;
case EVENT_TYPE_MOUSE_BUTTON:
if(GLFW_invoke_MouseButton) GLFW_invoke_MouseButton(window, event.i1, event.i2, event.i3);
break;
@@ -170,19 +168,51 @@ void pojavPumpEvents(void* window) {
break;
case EVENT_TYPE_FRAMEBUFFER_SIZE:
handleFramebufferSizeJava(showingWindow, event.i1, event.i2);
__android_log_print(ANDROID_LOG_INFO, "NativeInput", "Pumped size event: %i %i",event.i1, event.i2);
if(GLFW_invoke_FramebufferSize) GLFW_invoke_FramebufferSize(window, event.i1, event.i2);
break;
case EVENT_TYPE_WINDOW_SIZE:
handleFramebufferSizeJava(showingWindow, event.i1, event.i2);
__android_log_print(ANDROID_LOG_INFO, "NativeInput", "Pumped size event: %i %i",event.i1, event.i2);
if(GLFW_invoke_WindowSize) GLFW_invoke_WindowSize(window, event.i1, event.i2);
break;
}
}
if((cLastX != cursorX || cLastY != cursorY) && GLFW_invoke_CursorPos) {
cLastX = cursorX;
cLastY = cursorY;
GLFW_invoke_CursorPos(window, cursorX, cursorY);
}
}
void pojavRewindEvents() {
eventCounter = 0;
}
JNIEXPORT void JNICALL
Java_org_lwjgl_glfw_GLFW_nglfwGetCursorPos(JNIEnv *env, jclass clazz, jlong window, jobject xpos,
jobject ypos) {
*(double*)(*env)->GetDirectBufferAddress(env, xpos) = cursorX;
*(double*)(*env)->GetDirectBufferAddress(env, ypos) = cursorY;
// TODO: implement glfwGetCursorPos()
}
JNIEXPORT void JNICALL
Java_org_lwjgl_glfw_GLFW_nglfwGetCursorPosA(JNIEnv *env, jclass clazz, jlong window,
jdoubleArray xpos, jdoubleArray ypos) {
(*env)->SetDoubleArrayRegion(env, xpos, 0,1, &cursorX);
(*env)->SetDoubleArrayRegion(env, ypos, 0,1, &cursorY);
// TODO: implement nglfwGetCursorPosA()
}
JNIEXPORT void JNICALL
Java_org_lwjgl_glfw_GLFW_glfwSetCursorPos(JNIEnv *env, jclass clazz, jlong window, jdouble xpos,
jdouble ypos) {
cLastX = cursorX = xpos;
cLastY = cursorY = ypos;
// TODO: implement glfwSetCursorPos()
}
void sendData(int type, int i1, int i2, int i3, int i4) {
#ifdef DEBUG
@@ -192,15 +222,9 @@ void sendData(int type, int i1, int i2, int i3, int i4) {
LOGE("BUG: Input is ready but thread is not attached yet.");
return;
}
if(inputBridgeClass_ANDROID == NULL) return;
if(type == EVENT_TYPE_CURSOR_POS) {
(*runtimeJNIEnvPtr_ANDROID)->CallStaticVoidMethod(
runtimeJNIEnvPtr_ANDROID,
inputBridgeClass_ANDROID,
inputBridgeMethod_ANDROID,
type,
i1, i2, i3, i4
);
cursorX = i1;
cursorY = i2;
}else {
if (eventCounter < 499) {
GLFWInputEvent *event = &events[eventCounter++];
@@ -324,16 +348,12 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSetInputRead
#ifdef DEBUG
LOGD("Debug: Changing input state, isReady=%d, isUseStackQueueCall=%d\n", inputReady, isUseStackQueueCall);
#endif
__android_log_print(ANDROID_LOG_INFO, "NativeInput", "Input ready: %i", inputReady);
isInputReady = inputReady;
return isUseStackQueueCall;
}
JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSetGrabbing(JNIEnv* env, jclass clazz, jboolean grabbing, jint xset, jint yset) {
JNIEnv *dalvikEnv;
(*dalvikJavaVMPtr)->AttachCurrentThread(dalvikJavaVMPtr, &dalvikEnv, NULL);
(*dalvikEnv)->CallStaticVoidMethod(dalvikEnv, bridgeClazz, method_onGrabStateChanged, grabbing);
(*dalvikJavaVMPtr)->DetachCurrentThread(dalvikJavaVMPtr);
isGrabbing = grabbing;
static void updateGrabCursor(jint xset, jint yset) {
if (isGrabbing == JNI_TRUE) {
grabCursorX = xset; // savedWidth / 2;
grabCursorY = yset; // savedHeight / 2;
@@ -341,6 +361,15 @@ JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSetGrabbing(JNIE
}
}
JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSetGrabbing(JNIEnv* env, jclass clazz, jboolean grabbing) {
JNIEnv *dalvikEnv;
(*dalvikJavaVMPtr)->AttachCurrentThread(dalvikJavaVMPtr, &dalvikEnv, NULL);
(*dalvikEnv)->CallStaticVoidMethod(dalvikEnv, bridgeClazz, method_onGrabStateChanged, grabbing);
(*dalvikJavaVMPtr)->DetachCurrentThread(dalvikJavaVMPtr);
isGrabbing = grabbing;
updateGrabCursor((jint)cursorX, (jint)cursorY);
}
JNIEXPORT jboolean JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeIsGrabbing(JNIEnv* env, jclass clazz) {
return isGrabbing;
}
@@ -430,8 +459,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSendCursorPos(JN
_a > _b ? _a : _b; })
JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSendKey(JNIEnv* env, jclass clazz, jint key, jint scancode, jint action, jint mods) {
if (GLFW_invoke_Key && isInputReady) {
keyDownBuffer[max(0, key-31)]=(jbyte)action;
if (isUseStackQueueCall) {
keyDownBuffer[max(0, key-31)]=(jbyte)action;
sendData(EVENT_TYPE_KEY, key, scancode, action, mods);
} else {
GLFW_invoke_Key((void*) showingWindow, key, scancode, action, mods);
@@ -459,9 +488,10 @@ JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSendMouseButton(
JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSendScreenSize(JNIEnv* env, jclass clazz, jint width, jint height) {
savedWidth = width;
savedHeight = height;
__android_log_print(ANDROID_LOG_INFO, "NativeInput","Updated screen size: %i %i", width, height);
if (isInputReady) {
if (GLFW_invoke_FramebufferSize) {
__android_log_print(ANDROID_LOG_INFO, "NativeInput","Framebuffer submitted");
if (isUseStackQueueCall) {
sendData(EVENT_TYPE_FRAMEBUFFER_SIZE, width, height, 0, 0);
} else {
@@ -470,12 +500,15 @@ JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSendScreenSize(J
}
if (GLFW_invoke_WindowSize) {
__android_log_print(ANDROID_LOG_INFO, "NativeInput","Window submitted");
if (isUseStackQueueCall) {
sendData(EVENT_TYPE_WINDOW_SIZE, width, height, 0, 0);
} else {
GLFW_invoke_WindowSize((void*) showingWindow, width, height);
}
}
}else{
__android_log_print(ANDROID_LOG_INFO, "NativeInput","INR");
}
// return (isInputReady && (GLFW_invoke_FramebufferSize || GLFW_invoke_WindowSize));
@@ -507,10 +540,4 @@ JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSetWindowAttrib(
vmGlfwClass, method_glftSetWindowAttrib,
(jlong) showingWindow, attrib, value
);
}
JNIEXPORT void JNICALL
Java_org_lwjgl_glfw_CallbackBridge_setClass(JNIEnv *env, jclass clazz) {
inputBridgeMethod_ANDROID = (*env)->GetStaticMethodID(env, clazz, "receiveCallback", "(IIIII)V");
inputBridgeClass_ANDROID = (*env)->NewGlobalRef(env, clazz);
}
}

View File

@@ -16,10 +16,6 @@ public class CallbackBridge {
public static final int EVENT_TYPE_WINDOW_SIZE = 1008;
public static final int ANDROID_TYPE_GRAB_STATE = 0;
// Should pending events be limited?
volatile public static List<Integer[]> PENDING_EVENT_LIST = new ArrayList<>();
volatile public static boolean PENDING_EVENT_READY = false;
public static final boolean INPUT_DEBUG_ENABLED;
@@ -42,30 +38,8 @@ public class CallbackBridge {
//Quick and dirty: debul all key inputs to System.out
*/
}
public static void sendGrabbing(boolean grab, int xset, int yset) {
// sendData(ANDROID_TYPE_GRAB_STATE, Boolean.toString(grab));
GLFW.mGLFWIsGrabbing = grab;
nativeSetGrabbing(grab, xset, yset);
}
// Called from Android side
public static void receiveCallback(int type, int i1, int i2, int i3, int i4) {
/*
if (INPUT_DEBUG_ENABLED) {
System.out.println("LWJGL GLFW Callback received type=" + Integer.toString(type) + ", data=" + i1 + ", " + i2 + ", " + i3 + ", " + i4);
}
*/
if (PENDING_EVENT_READY) {
if (type == EVENT_TYPE_CURSOR_POS) {
GLFW.mGLFWCursorX = (double) i1;
GLFW.mGLFWCursorY = (double) i2;
} else {
PENDING_EVENT_LIST.add(new Integer[]{type, (int) i1, (int)i2, i3, i4});
}
} // else System.out.println("Event input is not ready yet!");
}
public static void sendData(int type, String data) {
nativeSendData(false, type, data);
}
@@ -73,7 +47,6 @@ public class CallbackBridge {
public static native boolean nativeSetInputReady(boolean ready);
public static native String nativeClipboard(int action, byte[] copy);
public static native void nativeAttachThreadToOther(boolean isAndroid, boolean isUseStackQueueBool);
private static native void nativeSetGrabbing(boolean grab, int xset, int yset);
public static native void setClass();
public static native void nativeSetGrabbing(boolean grab);
}

View File

@@ -487,7 +487,6 @@ public class GLFW
/* volatile */ public static GLFWWindowSizeCallback mGLFWWindowSizeCallback;
volatile public static int mGLFWWindowWidth, mGLFWWindowHeight;
volatile public static double mGLFWCursorX, mGLFWCursorY, mGLFWCursorLastX, mGLFWCursorLastY;
private static GLFWGammaRamp mGLFWGammaRamp;
private static Map<Integer, String> mGLFWKeyCodes;
@@ -497,8 +496,7 @@ public class GLFW
private static double mGLFWInitialTime;
private static ArrayMap<Long, GLFWWindowProperties> mGLFWWindowMap;
public static boolean mGLFWIsGrabbing, mGLFWIsInputReady, mGLFWIsUseStackQueue = false;
public static boolean mGLFWIsInputReady;
public static final ByteBuffer keyDownBuffer = ByteBuffer.allocateDirect(317);
private static final String PROP_WINDOW_WIDTH = "glfwstub.windowWidth";
private static final String PROP_WINDOW_HEIGHT= "glfwstub.windowHeight";
@@ -526,7 +524,6 @@ public class GLFW
} catch (UnsatisfiedLinkError e) {
e.printStackTrace();
}
CallbackBridge.setClass();
mGLFWErrorCallback = GLFWErrorCallback.createPrint();
mGLFWKeyCodes = new ArrayMap<>();
@@ -1056,30 +1053,11 @@ public class GLFW
public static void glfwPollEvents() {
if (!mGLFWIsInputReady) {
mGLFWIsInputReady = true;
mGLFWIsUseStackQueue = CallbackBridge.nativeSetInputReady(true);
}
if(!CallbackBridge.PENDING_EVENT_READY) {
CallbackBridge.PENDING_EVENT_READY = true;
CallbackBridge.nativeSetInputReady(true);
}
for (Long ptr : mGLFWWindowMap.keySet()) callJV(ptr, Functions.PumpEvents);
callV(Functions.RewindEvents);
if ((mGLFWCursorX != mGLFWCursorLastX || mGLFWCursorY != mGLFWCursorLastY) && mGLFWCursorPosCallback != null) {
mGLFWCursorLastX = mGLFWCursorX;
mGLFWCursorLastY = mGLFWCursorY;
for (Long ptr : mGLFWWindowMap.keySet()) {
if (!mGLFWIsGrabbing && mGLFWWindowSizeCallback != null) {
try {
mGLFWWindowSizeCallback.invoke(ptr, mGLFWWindowWidth, mGLFWWindowHeight);
}catch (Throwable throwable){
throwable.printStackTrace();
}
}
mGLFWCursorPosCallback.invoke(ptr, mGLFWCursorX, mGLFWCursorY);
}
// System.out.println("CursorPos updated to x=" + mGLFWCursorX + ",y=" + mGLFWCursorY);
}
}
public static void internalWindowSizeChanged(long window, int w, int h) {
@@ -1116,9 +1094,9 @@ public class GLFW
if (mode == GLFW_CURSOR) {
switch (value) {
case GLFW_CURSOR_DISABLED:
CallbackBridge.sendGrabbing(true, (int) mGLFWCursorX, (int) mGLFWCursorY);
CallbackBridge.nativeSetGrabbing(true);
break;
default: CallbackBridge.sendGrabbing(false, (int) mGLFWCursorX, (int) mGLFWCursorY);
default: CallbackBridge.nativeSetGrabbing(false);
}
}
@@ -1140,23 +1118,24 @@ public class GLFW
public static int glfwGetMouseButton(@NativeType("GLFWwindow *") long window, int button) {
return 0;
}
public static void glfwGetCursorPos(@NativeType("GLFWwindow *") long window, @Nullable @NativeType("double *") DoubleBuffer xpos, @Nullable @NativeType("double *") DoubleBuffer ypos) {
if (CHECKS) {
checkSafe(xpos, 1);
checkSafe(ypos, 1);
}
xpos.put(mGLFWCursorX);
ypos.put(mGLFWCursorY);
nglfwGetCursorPos(window, xpos, ypos);
}
public static void glfwSetCursorPos(@NativeType("GLFWwindow *") long window, double xpos, double ypos) {
public static native void nglfwGetCursorPos(@NativeType("GLFWwindow *") long window, @Nullable @NativeType("double *") DoubleBuffer xpos, @Nullable @NativeType("double *") DoubleBuffer ypos);
public static native void nglfwGetCursorPosA(@NativeType("GLFWwindow *") long window, @Nullable @NativeType("double *") double[] xpos, @Nullable @NativeType("double *") double[] ypos);
public static native void glfwSetCursorPos(@NativeType("GLFWwindow *") long window, double xpos, double ypos); /*{
mGLFWCursorX = mGLFWCursorLastX = xpos;
mGLFWCursorY = mGLFWCursorLastY = ypos;
CallbackBridge.sendGrabbing(mGLFWIsGrabbing, (int) xpos, (int) ypos);
}
}*/
public static long glfwCreateCursor(@NativeType("const GLFWimage *") GLFWImage image, int xhot, int yhot) {
return 4L;
@@ -1368,8 +1347,7 @@ public class GLFW
checkSafe(xpos, 1);
checkSafe(ypos, 1);
}
xpos[0] = mGLFWCursorX;
ypos[0] = mGLFWCursorY;
nglfwGetCursorPosA(window, xpos, ypos);
}
public static boolean glfwExtensionSupported(String ext) {