From 0b9ab04c11649ee0cfb5744d5c409f16c74b4124 Mon Sep 17 00:00:00 2001 From: Cykyrios Date: Wed, 27 Oct 2021 19:55:16 +0200 Subject: [PATCH] Add screen_get_inactive_rect() to DisplayServer --- doc/classes/DisplayServer.xml | 8 ++++++++ platform/android/display_server_android.cpp | 8 ++++++++ platform/android/display_server_android.h | 1 + .../lib/src/org/godotengine/godot/GodotIO.java | 17 +++++++++++++++++ platform/android/java_godot_io_wrapper.cpp | 15 +++++++++++++++ platform/android/java_godot_io_wrapper.h | 2 ++ platform/iphone/display_server_iphone.h | 1 + platform/iphone/display_server_iphone.mm | 4 ++++ .../javascript/display_server_javascript.cpp | 4 ++++ platform/javascript/display_server_javascript.h | 1 + platform/linuxbsd/display_server_x11.cpp | 4 ++++ platform/linuxbsd/display_server_x11.h | 1 + platform/osx/display_server_osx.h | 1 + platform/osx/display_server_osx.mm | 4 ++++ platform/windows/display_server_windows.cpp | 4 ++++ platform/windows/display_server_windows.h | 1 + servers/display_server.cpp | 1 + servers/display_server.h | 1 + servers/display_server_headless.h | 1 + 19 files changed, 79 insertions(+) diff --git a/doc/classes/DisplayServer.xml b/doc/classes/DisplayServer.xml index 92d6a220d2..5a39208d56 100644 --- a/doc/classes/DisplayServer.xml +++ b/doc/classes/DisplayServer.xml @@ -389,6 +389,14 @@ [b]Note:[/b] This method is implemented on Android, Linux, macOS and Windows. Returns [code]72[/code] on unsupported platforms. + + + + + Returns the bounding rectangle of the inactive part of a device's display, such as a notch, or an empty [Rect2i] if none is found. + [b]Note:[/b] This method is implemented on Android. Returns an empty [Rect2i] on unsupported platforms. + + diff --git a/platform/android/display_server_android.cpp b/platform/android/display_server_android.cpp index 720752d28f..d139c22baf 100644 --- a/platform/android/display_server_android.cpp +++ b/platform/android/display_server_android.cpp @@ -142,6 +142,14 @@ Rect2i DisplayServerAndroid::screen_get_usable_rect(int p_screen) const { return Rect2i(xywh[0], xywh[1], xywh[2], xywh[3]); } +Rect2i DisplayServerAndroid::screen_get_inactive_rect(int p_screen) const { + GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java(); + ERR_FAIL_COND_V(!godot_io_java, Rect2i()); + int xywh[4]; + godot_io_java->screen_get_inactive_rect(xywh); + return Rect2i(xywh[0], xywh[1], xywh[2], xywh[3]); +} + int DisplayServerAndroid::screen_get_dpi(int p_screen) const { GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java(); ERR_FAIL_COND_V(!godot_io_java, 0); diff --git a/platform/android/display_server_android.h b/platform/android/display_server_android.h index 669a1c80e4..608a959e08 100644 --- a/platform/android/display_server_android.h +++ b/platform/android/display_server_android.h @@ -104,6 +104,7 @@ public: virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; + virtual Rect2i screen_get_inactive_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java b/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java index d85d88ec6c..dfbfa7a87d 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java +++ b/platform/android/java/lib/src/org/godotengine/godot/GodotIO.java @@ -38,6 +38,7 @@ import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.res.AssetManager; import android.graphics.Point; +import android.graphics.Rect; import android.net.Uri; import android.os.Build; import android.os.Environment; @@ -248,6 +249,22 @@ public class GodotIO { return result; } + public int[] screenGetInactiveRect() { + int[] result = { 0, 0, 0, 0 }; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + WindowInsets insets = activity.getWindow().getDecorView().getRootWindowInsets(); + DisplayCutout cutout = insets.getDisplayCutout(); + if (cutout != null) { + Rect boundingRect = cutout.getBoundingRects().get(0); + result[0] = boundingRect.left; + result[1] = boundingRect.top; + result[2] = boundingRect.width(); + result[3] = boundingRect.height(); + } + } + return result; + } + public void showKeyboard(String p_existing_text, boolean p_multiline, int p_max_input_length, int p_cursor_start, int p_cursor_end) { if (edit != null) edit.showKeyboard(p_existing_text, p_multiline, p_max_input_length, p_cursor_start, p_cursor_end); diff --git a/platform/android/java_godot_io_wrapper.cpp b/platform/android/java_godot_io_wrapper.cpp index 5cd2c382d2..47477e2320 100644 --- a/platform/android/java_godot_io_wrapper.cpp +++ b/platform/android/java_godot_io_wrapper.cpp @@ -54,6 +54,7 @@ GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instanc _get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;"); _get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I"); _screen_get_usable_rect = p_env->GetMethodID(cls, "screenGetUsableRect", "()[I"), + _screen_get_inactive_rect = p_env->GetMethodID(cls, "screenGetInactiveRect", "()[I"); _get_unique_id = p_env->GetMethodID(cls, "getUniqueID", "()Ljava/lang/String;"); _show_keyboard = p_env->GetMethodID(cls, "showKeyboard", "(Ljava/lang/String;ZIII)V"); _hide_keyboard = p_env->GetMethodID(cls, "hideKeyboard", "()V"); @@ -150,6 +151,20 @@ void GodotIOJavaWrapper::screen_get_usable_rect(int (&p_rect_xywh)[4]) { } } +void GodotIOJavaWrapper::screen_get_inactive_rect(int (&p_rect_xywh)[4]) { + if (_screen_get_inactive_rect) { + JNIEnv *env = get_jni_env(); + ERR_FAIL_COND(env == nullptr); + jintArray returnArray = (jintArray)env->CallObjectMethod(godot_io_instance, _screen_get_inactive_rect); + ERR_FAIL_COND(env->GetArrayLength(returnArray) != 4); + jint *arrayBody = env->GetIntArrayElements(returnArray, JNI_FALSE); + for (int i = 0; i < 4; i++) { + p_rect_xywh[i] = arrayBody[i]; + } + env->ReleaseIntArrayElements(returnArray, arrayBody, 0); + } +} + String GodotIOJavaWrapper::get_unique_id() { if (_get_unique_id) { JNIEnv *env = get_jni_env(); diff --git a/platform/android/java_godot_io_wrapper.h b/platform/android/java_godot_io_wrapper.h index 8f6d7f813f..917c6de3bb 100644 --- a/platform/android/java_godot_io_wrapper.h +++ b/platform/android/java_godot_io_wrapper.h @@ -52,6 +52,7 @@ private: jmethodID _get_model = 0; jmethodID _get_screen_DPI = 0; jmethodID _screen_get_usable_rect = 0; + jmethodID _screen_get_inactive_rect = 0; jmethodID _get_unique_id = 0; jmethodID _show_keyboard = 0; jmethodID _hide_keyboard = 0; @@ -72,6 +73,7 @@ public: String get_model(); int get_screen_dpi(); void screen_get_usable_rect(int (&p_rect_xywh)[4]); + void screen_get_inactive_rect(int (&p_rect_xywh)[4]); String get_unique_id(); bool has_vk(); void show_vk(const String &p_existing, bool p_multiline, int p_max_input_length, int p_cursor_start, int p_cursor_end); diff --git a/platform/iphone/display_server_iphone.h b/platform/iphone/display_server_iphone.h index 5cfcc1765c..a352d05d02 100644 --- a/platform/iphone/display_server_iphone.h +++ b/platform/iphone/display_server_iphone.h @@ -127,6 +127,7 @@ public: virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; + virtual Rect2i screen_get_inactive_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; diff --git a/platform/iphone/display_server_iphone.mm b/platform/iphone/display_server_iphone.mm index e18448fb6d..9ec9d5d393 100644 --- a/platform/iphone/display_server_iphone.mm +++ b/platform/iphone/display_server_iphone.mm @@ -357,6 +357,10 @@ Rect2i DisplayServerIPhone::screen_get_usable_rect(int p_screen) const { } } +Rect2i DisplayServerIPhone::screen_get_inactive_rect(int p_screen) const { + return Rect2i(); +} + int DisplayServerIPhone::screen_get_dpi(int p_screen) const { struct utsname systemInfo; uname(&systemInfo); diff --git a/platform/javascript/display_server_javascript.cpp b/platform/javascript/display_server_javascript.cpp index c2eb826db9..cd10d9cf24 100644 --- a/platform/javascript/display_server_javascript.cpp +++ b/platform/javascript/display_server_javascript.cpp @@ -795,6 +795,10 @@ Rect2i DisplayServerJavaScript::screen_get_usable_rect(int p_screen) const { return Rect2i(0, 0, size[0], size[1]); } +Rect2i DisplayServerJavaScript::screen_get_inactive_rect(int p_screen) const { + return Rect2i(); +} + int DisplayServerJavaScript::screen_get_dpi(int p_screen) const { return godot_js_display_screen_dpi_get(); } diff --git a/platform/javascript/display_server_javascript.h b/platform/javascript/display_server_javascript.h index 1aa600c421..f500b40ab3 100644 --- a/platform/javascript/display_server_javascript.h +++ b/platform/javascript/display_server_javascript.h @@ -134,6 +134,7 @@ public: virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; + virtual Rect2i screen_get_inactive_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; diff --git a/platform/linuxbsd/display_server_x11.cpp b/platform/linuxbsd/display_server_x11.cpp index 212b6762e1..bac1bd9f32 100644 --- a/platform/linuxbsd/display_server_x11.cpp +++ b/platform/linuxbsd/display_server_x11.cpp @@ -739,6 +739,10 @@ Rect2i DisplayServerX11::screen_get_usable_rect(int p_screen) const { return rect; } +Rect2i DisplayServerX11::screen_get_inactive_rect(int p_screen) const { + return Rect2i(0, 0, 0, 0); +} + int DisplayServerX11::screen_get_dpi(int p_screen) const { _THREAD_SAFE_METHOD_ diff --git a/platform/linuxbsd/display_server_x11.h b/platform/linuxbsd/display_server_x11.h index ded481f613..7440df41f5 100644 --- a/platform/linuxbsd/display_server_x11.h +++ b/platform/linuxbsd/display_server_x11.h @@ -298,6 +298,7 @@ public: virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; + virtual Rect2i screen_get_inactive_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; diff --git a/platform/osx/display_server_osx.h b/platform/osx/display_server_osx.h index e8f2858489..e193a118ec 100644 --- a/platform/osx/display_server_osx.h +++ b/platform/osx/display_server_osx.h @@ -231,6 +231,7 @@ public: virtual float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual float screen_get_max_scale() const override; virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; + virtual Rect2i screen_get_inactive_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual Vector get_window_list() const override; diff --git a/platform/osx/display_server_osx.mm b/platform/osx/display_server_osx.mm index 3fe055a511..7b977905de 100644 --- a/platform/osx/display_server_osx.mm +++ b/platform/osx/display_server_osx.mm @@ -2224,6 +2224,10 @@ Rect2i DisplayServerOSX::screen_get_usable_rect(int p_screen) const { return Rect2i(); } +Rect2i DisplayServerOSX::screen_get_inactive_rect(int p_screen) const { + return Rect2i(); +} + Vector DisplayServerOSX::get_window_list() const { _THREAD_SAFE_METHOD_ diff --git a/platform/windows/display_server_windows.cpp b/platform/windows/display_server_windows.cpp index 905c4142a8..a15a1b05c8 100644 --- a/platform/windows/display_server_windows.cpp +++ b/platform/windows/display_server_windows.cpp @@ -351,6 +351,10 @@ Rect2i DisplayServerWindows::screen_get_usable_rect(int p_screen) const { return data.rect; } +Rect2i DisplayServerWindows::screen_get_inactive_rect(int p_screen) const { + return Rect2i(); +} + typedef struct { int count; int screen; diff --git a/platform/windows/display_server_windows.h b/platform/windows/display_server_windows.h index 22c4f96a90..de3261424e 100644 --- a/platform/windows/display_server_windows.h +++ b/platform/windows/display_server_windows.h @@ -458,6 +458,7 @@ public: virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; + virtual Rect2i screen_get_inactive_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; diff --git a/servers/display_server.cpp b/servers/display_server.cpp index 3fb47e8f05..39aa11b1e4 100644 --- a/servers/display_server.cpp +++ b/servers/display_server.cpp @@ -375,6 +375,7 @@ void DisplayServer::_bind_methods() { ClassDB::bind_method(D_METHOD("screen_get_position", "screen"), &DisplayServer::screen_get_position, DEFVAL(SCREEN_OF_MAIN_WINDOW)); ClassDB::bind_method(D_METHOD("screen_get_size", "screen"), &DisplayServer::screen_get_size, DEFVAL(SCREEN_OF_MAIN_WINDOW)); ClassDB::bind_method(D_METHOD("screen_get_usable_rect", "screen"), &DisplayServer::screen_get_usable_rect, DEFVAL(SCREEN_OF_MAIN_WINDOW)); + ClassDB::bind_method(D_METHOD("screen_get_inactive_rect", "screen"), &DisplayServer::screen_get_inactive_rect, DEFVAL(SCREEN_OF_MAIN_WINDOW)); ClassDB::bind_method(D_METHOD("screen_get_dpi", "screen"), &DisplayServer::screen_get_dpi, DEFVAL(SCREEN_OF_MAIN_WINDOW)); ClassDB::bind_method(D_METHOD("screen_get_scale", "screen"), &DisplayServer::screen_get_scale, DEFVAL(SCREEN_OF_MAIN_WINDOW)); ClassDB::bind_method(D_METHOD("screen_is_touchscreen", "screen"), &DisplayServer::screen_is_touchscreen, DEFVAL(SCREEN_OF_MAIN_WINDOW)); diff --git a/servers/display_server.h b/servers/display_server.h index 2595cf2eb8..a5d99312e9 100644 --- a/servers/display_server.h +++ b/servers/display_server.h @@ -173,6 +173,7 @@ public: virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const = 0; virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const = 0; virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const = 0; + virtual Rect2i screen_get_inactive_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const = 0; virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const = 0; virtual float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const; virtual float screen_get_max_scale() const { diff --git a/servers/display_server_headless.h b/servers/display_server_headless.h index d9ee91084f..2f5370d0cc 100644 --- a/servers/display_server_headless.h +++ b/servers/display_server_headless.h @@ -59,6 +59,7 @@ public: Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override { return Point2i(); } Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override { return Size2i(); } Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override { return Rect2i(); } + Rect2i screen_get_inactive_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override { return Rect2i(); } int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override { return 96; /* 0 might cause issues */ } float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const override { return 1; } float screen_get_max_scale() const override { return 1; }