make Looper, Handler, and MessageQueue work properly

this for example makes Unity apps not steal the main thread,
hanging Gtk.
This commit is contained in:
Mis012
2023-07-25 14:26:29 +02:00
parent 7ac5587fca
commit 08998b0076
15 changed files with 997 additions and 158 deletions

View File

@@ -0,0 +1,62 @@
#include "defines.h"
#include "util.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/* TODO put these in a header */
typedef void ALooper;
ALooper * ALooper_prepare(void);
void ALooper_wake(ALooper *looper);
bool ALooper_isPolling(ALooper *looper);
int ALooper_pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData);
struct native_message_queue {
ALooper *looper;
bool in_callback;
};
JNIEXPORT jlong JNICALL Java_android_os_MessageQueue_nativeInit(JNIEnv *env, jclass this)
{
struct native_message_queue *message_queue = malloc(sizeof(struct native_message_queue));
message_queue->in_callback = false;
message_queue->looper = ALooper_prepare();
return _INTPTR(message_queue);
}
JNIEXPORT void JNICALL Java_android_os_MessageQueue_nativeDestroy(JNIEnv *env, jclass this, jlong ptr)
{
struct native_message_queue *message_queue = _PTR(ptr);
free(message_queue);
}
JNIEXPORT void JNICALL Java_android_os_MessageQueue_nativePollOnce(JNIEnv *env, jclass this, jlong ptr, jint timeout_millis)
{
struct native_message_queue *message_queue = _PTR(ptr);
// printf("Java_android_os_MessageQueue_nativePollOnce: entry (timeout: %d)\n", timeout_millis);
message_queue->in_callback = true;
ALooper_pollOnce(timeout_millis, NULL, NULL, NULL);
message_queue->in_callback = false;
// printf("Java_android_os_MessageQueue_nativePollOnce: exit\n");
/* TODO: what's with the exception stuff */
}
JNIEXPORT void JNICALL Java_android_os_MessageQueue_nativeWake(JNIEnv *env, jclass this, jlong ptr)
{
struct native_message_queue *message_queue = _PTR(ptr);
ALooper_wake(message_queue->looper);
}
JNIEXPORT jboolean JNICALL Java_android_os_MessageQueue_nativeIsIdling(JNIEnv *env, jclass this, jlong ptr)
{
struct native_message_queue *message_queue = _PTR(ptr);
return ALooper_isPolling(message_queue->looper);
}

View File

@@ -1,3 +1,6 @@
#include <time.h>
#include <math.h>
#include "generated_headers/android_os_SystemClock.h"
JNIEXPORT jlong JNICALL Java_android_os_SystemClock_elapsedRealtime(JNIEnv *env, jclass this)
@@ -5,3 +8,10 @@ JNIEXPORT jlong JNICALL Java_android_os_SystemClock_elapsedRealtime(JNIEnv *env,
printf("FIXME: Java_android_os_SystemClock_elapsedRealtime: returning 0\n");
return 0; // FIXME
}
JNIEXPORT jlong JNICALL Java_android_os_SystemClock_uptimeMillis(JNIEnv *env, jclass this)
{
struct timespec t;
clock_gettime(CLOCK_REALTIME, &t);
return t.tv_sec * 1000 + lround(t.tv_nsec / 1e6);
}

View File

@@ -10,42 +10,42 @@ extern "C" {
/*
* Class: android_os_MessageQueue
* Method: nativeInit
* Signature: ()I
* Signature: ()J
*/
JNIEXPORT jint JNICALL Java_android_os_MessageQueue_nativeInit
JNIEXPORT jlong JNICALL Java_android_os_MessageQueue_nativeInit
(JNIEnv *, jclass);
/*
* Class: android_os_MessageQueue
* Method: nativeDestroy
* Signature: (I)V
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_android_os_MessageQueue_nativeDestroy
(JNIEnv *, jclass, jint);
(JNIEnv *, jclass, jlong);
/*
* Class: android_os_MessageQueue
* Method: nativePollOnce
* Signature: (II)V
* Signature: (JI)V
*/
JNIEXPORT void JNICALL Java_android_os_MessageQueue_nativePollOnce
(JNIEnv *, jclass, jint, jint);
(JNIEnv *, jclass, jlong, jint);
/*
* Class: android_os_MessageQueue
* Method: nativeWake
* Signature: (I)V
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_android_os_MessageQueue_nativeWake
(JNIEnv *, jclass, jint);
(JNIEnv *, jclass, jlong);
/*
* Class: android_os_MessageQueue
* Method: nativeIsIdling
* Signature: (I)Z
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_android_os_MessageQueue_nativeIsIdling
(JNIEnv *, jclass, jint);
(JNIEnv *, jclass, jlong);
#ifdef __cplusplus
}

View File

@@ -15,6 +15,14 @@ extern "C" {
JNIEXPORT jboolean JNICALL Java_android_os_SystemClock_setCurrentTimeMillis
(JNIEnv *, jclass, jlong);
/*
* Class: android_os_SystemClock
* Method: uptimeMillis
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_android_os_SystemClock_uptimeMillis
(JNIEnv *, jclass);
/*
* Class: android_os_SystemClock
* Method: elapsedRealtime