Skip to content

Commit 3f6d67c

Browse files
authored
[rcore_android] implement GetCurrentMonitor() (#5204)
1 parent 07033cf commit 3f6d67c

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/platforms/rcore_android.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
#include <android_native_app_glue.h> // Required for: android_app struct and activity management
5050
#include <android/window.h> // Required for: AWINDOW_FLAG_FULLSCREEN definition and others
5151
//#include <android/sensor.h> // Required for: Android sensors functions (accelerometer, gyroscope, light...)
52-
#include <jni.h> // Required for: JNIEnv and JavaVM [Used in OpenURL()]
52+
#include <jni.h> // Required for: JNIEnv and JavaVM [Used in OpenURL() and GetCurrentMonitor()]
5353

5454
#include <EGL/egl.h> // Native platform windowing system interface
5555

@@ -446,8 +446,32 @@ int GetMonitorCount(void)
446446
// Get current monitor where window is placed
447447
int GetCurrentMonitor(void)
448448
{
449-
TRACELOG(LOG_WARNING, "GetCurrentMonitor() not implemented on target platform");
450-
return 0;
449+
int displayId = -1;
450+
JNIEnv* env = NULL;
451+
JavaVM* vm = platform.app->activity->vm;
452+
(*vm)->AttachCurrentThread(vm, &env, NULL);
453+
454+
jobject activity = platform.app->activity->clazz;
455+
jclass activityClass = (*env)->GetObjectClass(env, activity);
456+
457+
jmethodID getDisplayMethod = (*env)->GetMethodID(env, activityClass, "getDisplay", "()Landroid/view/Display;");
458+
459+
jobject display = (*env)->CallObjectMethod(env, activity, getDisplayMethod);
460+
461+
if (display == NULL) {
462+
TRACELOG(LOG_ERROR, "GetCurrentMonitor() couldn't get the display object");
463+
} else {
464+
jclass displayClass = (*env)->FindClass(env, "android/view/Display");
465+
jmethodID getDisplayIdMethod = (*env)->GetMethodID(env, displayClass, "getDisplayId", "()I");
466+
displayId = (int) (*env)->CallIntMethod(env, display, getDisplayIdMethod);
467+
(*env)->DeleteLocalRef(env, displayClass);
468+
}
469+
470+
(*env)->DeleteLocalRef(env, activityClass);
471+
(*env)->DeleteLocalRef(env, display);
472+
473+
(*vm)->DetachCurrentThread(vm);
474+
return displayId;
451475
}
452476

453477
// Get selected monitor position

0 commit comments

Comments
 (0)