2019-03-13 13:51:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* java_godot_io_wrapper.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#include "java_godot_io_wrapper.h"
|
2022-02-16 13:56:32 +01:00
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/error/error_list.h"
|
2022-04-24 21:12:22 +02:00
|
|
|
#include "core/math/rect2.h"
|
|
|
|
#include "core/variant/variant.h"
|
2019-03-13 13:51:55 +01:00
|
|
|
|
|
|
|
// JNIEnv is only valid within the thread it belongs to, in a multi threading environment
|
|
|
|
// we can't cache it.
|
|
|
|
// For GodotIO we call all access methods from our thread and we thus get a valid JNIEnv
|
2021-01-19 13:29:41 +01:00
|
|
|
// from get_jni_env().
|
2019-03-13 13:51:55 +01:00
|
|
|
|
|
|
|
GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instance) {
|
|
|
|
godot_io_instance = p_env->NewGlobalRef(p_godot_io_instance);
|
|
|
|
if (godot_io_instance) {
|
|
|
|
cls = p_env->GetObjectClass(godot_io_instance);
|
|
|
|
if (cls) {
|
|
|
|
cls = (jclass)p_env->NewGlobalRef(cls);
|
|
|
|
} else {
|
|
|
|
// this is a pretty serious fail.. bail... pointers will stay 0
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_open_URI = p_env->GetMethodID(cls, "openURI", "(Ljava/lang/String;)I");
|
2021-07-11 03:39:31 +02:00
|
|
|
_get_cache_dir = p_env->GetMethodID(cls, "getCacheDir", "()Ljava/lang/String;");
|
2019-03-13 13:51:55 +01:00
|
|
|
_get_data_dir = p_env->GetMethodID(cls, "getDataDir", "()Ljava/lang/String;");
|
2022-04-24 21:12:22 +02:00
|
|
|
_get_display_cutouts = p_env->GetMethodID(cls, "getDisplayCutouts", "()[I"),
|
2022-04-26 11:40:57 +02:00
|
|
|
_get_display_safe_area = p_env->GetMethodID(cls, "getDisplaySafeArea", "()[I"),
|
2019-03-13 13:51:55 +01:00
|
|
|
_get_locale = p_env->GetMethodID(cls, "getLocale", "()Ljava/lang/String;");
|
|
|
|
_get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;");
|
|
|
|
_get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I");
|
2022-04-04 11:22:51 +02:00
|
|
|
_get_scaled_density = p_env->GetMethodID(cls, "getScaledDensity", "()F");
|
2022-01-27 20:46:57 +01:00
|
|
|
_get_screen_refresh_rate = p_env->GetMethodID(cls, "getScreenRefreshRate", "(D)D");
|
2019-05-09 11:21:49 +02:00
|
|
|
_get_unique_id = p_env->GetMethodID(cls, "getUniqueID", "()Ljava/lang/String;");
|
2022-07-07 20:20:10 +02:00
|
|
|
_show_keyboard = p_env->GetMethodID(cls, "showKeyboard", "(Ljava/lang/String;IIII)V");
|
2019-03-13 13:51:55 +01:00
|
|
|
_hide_keyboard = p_env->GetMethodID(cls, "hideKeyboard", "()V");
|
|
|
|
_set_screen_orientation = p_env->GetMethodID(cls, "setScreenOrientation", "(I)V");
|
2020-03-27 17:30:18 +01:00
|
|
|
_get_screen_orientation = p_env->GetMethodID(cls, "getScreenOrientation", "()I");
|
2021-07-11 03:39:31 +02:00
|
|
|
_get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(IZ)Ljava/lang/String;");
|
2019-03-13 13:51:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GodotIOJavaWrapper::~GodotIOJavaWrapper() {
|
2024-04-15 19:30:18 +02:00
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_NULL(env);
|
|
|
|
|
|
|
|
env->DeleteGlobalRef(cls);
|
|
|
|
env->DeleteGlobalRef(godot_io_instance);
|
2019-03-13 13:51:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
jobject GodotIOJavaWrapper::get_instance() {
|
|
|
|
return godot_io_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error GodotIOJavaWrapper::open_uri(const String &p_uri) {
|
|
|
|
if (_open_URI) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(env, ERR_UNAVAILABLE);
|
2019-03-13 13:51:55 +01:00
|
|
|
jstring jStr = env->NewStringUTF(p_uri.utf8().get_data());
|
2024-04-15 19:30:18 +02:00
|
|
|
Error result = env->CallIntMethod(godot_io_instance, _open_URI, jStr) ? ERR_CANT_OPEN : OK;
|
|
|
|
env->DeleteLocalRef(jStr);
|
|
|
|
return result;
|
2019-03-13 13:51:55 +01:00
|
|
|
} else {
|
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 03:39:31 +02:00
|
|
|
String GodotIOJavaWrapper::get_cache_dir() {
|
|
|
|
if (_get_cache_dir) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(env, String());
|
2021-07-11 03:39:31 +02:00
|
|
|
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_cache_dir);
|
2019-03-13 13:51:55 +01:00
|
|
|
return jstring_to_string(s, env);
|
|
|
|
} else {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 03:39:31 +02:00
|
|
|
String GodotIOJavaWrapper::get_user_data_dir() {
|
|
|
|
if (_get_data_dir) {
|
2021-06-08 18:43:10 +02:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(env, String());
|
2021-07-11 03:39:31 +02:00
|
|
|
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_data_dir);
|
2021-06-08 18:43:10 +02:00
|
|
|
return jstring_to_string(s, env);
|
|
|
|
} else {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 13:51:55 +01:00
|
|
|
String GodotIOJavaWrapper::get_locale() {
|
|
|
|
if (_get_locale) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(env, String());
|
2019-03-13 13:51:55 +01:00
|
|
|
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_locale);
|
|
|
|
return jstring_to_string(s, env);
|
|
|
|
} else {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String GodotIOJavaWrapper::get_model() {
|
|
|
|
if (_get_model) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(env, String());
|
2019-03-13 13:51:55 +01:00
|
|
|
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_model);
|
|
|
|
return jstring_to_string(s, env);
|
|
|
|
} else {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int GodotIOJavaWrapper::get_screen_dpi() {
|
|
|
|
if (_get_screen_DPI) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(env, 160);
|
2019-03-13 13:51:55 +01:00
|
|
|
return env->CallIntMethod(godot_io_instance, _get_screen_DPI);
|
|
|
|
} else {
|
|
|
|
return 160;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 11:22:51 +02:00
|
|
|
float GodotIOJavaWrapper::get_scaled_density() {
|
|
|
|
if (_get_scaled_density) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(env, 1.0f);
|
2022-04-04 11:22:51 +02:00
|
|
|
return env->CallFloatMethod(godot_io_instance, _get_scaled_density);
|
|
|
|
} else {
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 20:46:57 +01:00
|
|
|
float GodotIOJavaWrapper::get_screen_refresh_rate(float fallback) {
|
|
|
|
if (_get_screen_refresh_rate) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
if (env == nullptr) {
|
2022-02-10 12:00:11 +01:00
|
|
|
ERR_PRINT("An error occurred while trying to get screen refresh rate.");
|
2022-01-27 20:46:57 +01:00
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
return (float)env->CallDoubleMethod(godot_io_instance, _get_screen_refresh_rate, (double)fallback);
|
|
|
|
}
|
2022-02-10 12:00:11 +01:00
|
|
|
ERR_PRINT("An error occurred while trying to get the screen refresh rate.");
|
2022-01-27 20:46:57 +01:00
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2022-08-05 03:41:48 +02:00
|
|
|
TypedArray<Rect2> GodotIOJavaWrapper::get_display_cutouts() {
|
|
|
|
TypedArray<Rect2> result;
|
2022-04-24 21:12:22 +02:00
|
|
|
ERR_FAIL_NULL_V(_get_display_cutouts, result);
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_NULL_V(env, result);
|
|
|
|
jintArray returnArray = (jintArray)env->CallObjectMethod(godot_io_instance, _get_display_cutouts);
|
|
|
|
jint arrayLength = env->GetArrayLength(returnArray);
|
|
|
|
jint *arrayBody = env->GetIntArrayElements(returnArray, JNI_FALSE);
|
|
|
|
int cutouts = arrayLength / 4;
|
|
|
|
for (int i = 0; i < cutouts; i++) {
|
|
|
|
int x = arrayBody[i * 4];
|
|
|
|
int y = arrayBody[i * 4 + 1];
|
|
|
|
int width = arrayBody[i * 4 + 2];
|
|
|
|
int height = arrayBody[i * 4 + 3];
|
|
|
|
Rect2 cutout(x, y, width, height);
|
|
|
|
result.append(cutout);
|
|
|
|
}
|
|
|
|
env->ReleaseIntArrayElements(returnArray, arrayBody, 0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-26 11:40:57 +02:00
|
|
|
Rect2i GodotIOJavaWrapper::get_display_safe_area() {
|
|
|
|
Rect2i result;
|
|
|
|
ERR_FAIL_NULL_V(_get_display_safe_area, result);
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_NULL_V(env, result);
|
|
|
|
jintArray returnArray = (jintArray)env->CallObjectMethod(godot_io_instance, _get_display_safe_area);
|
|
|
|
ERR_FAIL_COND_V(env->GetArrayLength(returnArray) != 4, result);
|
|
|
|
jint *arrayBody = env->GetIntArrayElements(returnArray, JNI_FALSE);
|
|
|
|
result = Rect2i(arrayBody[0], arrayBody[1], arrayBody[2], arrayBody[3]);
|
|
|
|
env->ReleaseIntArrayElements(returnArray, arrayBody, 0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-13 13:51:55 +01:00
|
|
|
String GodotIOJavaWrapper::get_unique_id() {
|
2019-05-09 11:21:49 +02:00
|
|
|
if (_get_unique_id) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(env, String());
|
2019-05-09 11:21:49 +02:00
|
|
|
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_unique_id);
|
2019-03-13 13:51:55 +01:00
|
|
|
return jstring_to_string(s, env);
|
|
|
|
} else {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GodotIOJavaWrapper::has_vk() {
|
2022-05-30 23:13:49 +02:00
|
|
|
return (_show_keyboard != nullptr) && (_hide_keyboard != nullptr);
|
2019-03-13 13:51:55 +01:00
|
|
|
}
|
|
|
|
|
2022-07-07 20:20:10 +02:00
|
|
|
void GodotIOJavaWrapper::show_vk(const String &p_existing, int p_type, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
|
2019-03-13 13:51:55 +01:00
|
|
|
if (_show_keyboard) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL(env);
|
2019-03-13 13:51:55 +01:00
|
|
|
jstring jStr = env->NewStringUTF(p_existing.utf8().get_data());
|
2022-07-07 20:20:10 +02:00
|
|
|
env->CallVoidMethod(godot_io_instance, _show_keyboard, jStr, p_type, p_max_input_length, p_cursor_start, p_cursor_end);
|
2024-04-15 19:30:18 +02:00
|
|
|
env->DeleteLocalRef(jStr);
|
2019-03-13 13:51:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GodotIOJavaWrapper::hide_vk() {
|
|
|
|
if (_hide_keyboard) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL(env);
|
2019-03-13 13:51:55 +01:00
|
|
|
env->CallVoidMethod(godot_io_instance, _hide_keyboard);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GodotIOJavaWrapper::set_screen_orientation(int p_orient) {
|
|
|
|
if (_set_screen_orientation) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL(env);
|
2019-03-13 13:51:55 +01:00
|
|
|
env->CallVoidMethod(godot_io_instance, _set_screen_orientation, p_orient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 17:30:18 +01:00
|
|
|
int GodotIOJavaWrapper::get_screen_orientation() {
|
|
|
|
if (_get_screen_orientation) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(env, 0);
|
2020-03-27 17:30:18 +01:00
|
|
|
return env->CallIntMethod(godot_io_instance, _get_screen_orientation);
|
2019-03-13 13:51:55 +01:00
|
|
|
} else {
|
2020-03-27 17:30:18 +01:00
|
|
|
return 0;
|
2019-03-13 13:51:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 03:39:31 +02:00
|
|
|
String GodotIOJavaWrapper::get_system_dir(int p_dir, bool p_shared_storage) {
|
2020-03-27 17:30:18 +01:00
|
|
|
if (_get_system_dir) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2022-05-30 23:13:49 +02:00
|
|
|
ERR_FAIL_NULL_V(env, String("."));
|
2021-07-11 03:39:31 +02:00
|
|
|
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_system_dir, p_dir, p_shared_storage);
|
2020-03-27 17:30:18 +01:00
|
|
|
return jstring_to_string(s, env);
|
2019-03-13 13:51:55 +01:00
|
|
|
} else {
|
2020-03-27 17:30:18 +01:00
|
|
|
return String(".");
|
2019-03-13 13:51:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 19:22:13 +01:00
|
|
|
// SafeNumeric because it can be changed from non-main thread and we need to
|
2019-03-13 13:51:55 +01:00
|
|
|
// ensure the change is immediately visible to other threads.
|
2021-02-10 19:22:13 +01:00
|
|
|
static SafeNumeric<int> virtual_keyboard_height;
|
2019-03-13 13:51:55 +01:00
|
|
|
|
|
|
|
int GodotIOJavaWrapper::get_vk_height() {
|
2021-02-10 19:22:13 +01:00
|
|
|
return virtual_keyboard_height.get();
|
2019-03-13 13:51:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GodotIOJavaWrapper::set_vk_height(int p_height) {
|
2021-02-10 19:22:13 +01:00
|
|
|
virtual_keyboard_height.set(p_height);
|
2019-03-13 13:51:55 +01:00
|
|
|
}
|