From 3f6d67c61c59affa4561de2ddf988e487093bc80 Mon Sep 17 00:00:00 2001 From: Keks137 Date: Wed, 24 Sep 2025 20:45:55 +0200 Subject: [PATCH] [rcore_android] implement GetCurrentMonitor() (#5204) --- src/platforms/rcore_android.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/platforms/rcore_android.c b/src/platforms/rcore_android.c index bfbdd30b3..6c67dc893 100644 --- a/src/platforms/rcore_android.c +++ b/src/platforms/rcore_android.c @@ -49,7 +49,7 @@ #include // Required for: android_app struct and activity management #include // Required for: AWINDOW_FLAG_FULLSCREEN definition and others //#include // Required for: Android sensors functions (accelerometer, gyroscope, light...) -#include // Required for: JNIEnv and JavaVM [Used in OpenURL()] +#include // Required for: JNIEnv and JavaVM [Used in OpenURL() and GetCurrentMonitor()] #include // Native platform windowing system interface @@ -446,8 +446,32 @@ int GetMonitorCount(void) // Get current monitor where window is placed int GetCurrentMonitor(void) { - TRACELOG(LOG_WARNING, "GetCurrentMonitor() not implemented on target platform"); - return 0; + int displayId = -1; + JNIEnv* env = NULL; + JavaVM* vm = platform.app->activity->vm; + (*vm)->AttachCurrentThread(vm, &env, NULL); + + jobject activity = platform.app->activity->clazz; + jclass activityClass = (*env)->GetObjectClass(env, activity); + + jmethodID getDisplayMethod = (*env)->GetMethodID(env, activityClass, "getDisplay", "()Landroid/view/Display;"); + + jobject display = (*env)->CallObjectMethod(env, activity, getDisplayMethod); + + if (display == NULL) { + TRACELOG(LOG_ERROR, "GetCurrentMonitor() couldn't get the display object"); + } else { + jclass displayClass = (*env)->FindClass(env, "android/view/Display"); + jmethodID getDisplayIdMethod = (*env)->GetMethodID(env, displayClass, "getDisplayId", "()I"); + displayId = (int) (*env)->CallIntMethod(env, display, getDisplayIdMethod); + (*env)->DeleteLocalRef(env, displayClass); + } + + (*env)->DeleteLocalRef(env, activityClass); + (*env)->DeleteLocalRef(env, display); + + (*vm)->DetachCurrentThread(vm); + return displayId; } // Get selected monitor position