Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/android/java_godot_io_wrapper.cpp
10277 views
1
/**************************************************************************/
2
/* java_godot_io_wrapper.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "java_godot_io_wrapper.h"
32
33
#include "core/math/rect2.h"
34
#include "core/variant/variant.h"
35
36
// JNIEnv is only valid within the thread it belongs to, in a multi threading environment
37
// we can't cache it.
38
// For GodotIO we call all access methods from our thread and we thus get a valid JNIEnv
39
// from get_jni_env().
40
41
GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instance) {
42
godot_io_instance = p_env->NewGlobalRef(p_godot_io_instance);
43
if (godot_io_instance) {
44
cls = p_env->GetObjectClass(godot_io_instance);
45
if (cls) {
46
cls = (jclass)p_env->NewGlobalRef(cls);
47
} else {
48
// this is a pretty serious fail.. bail... pointers will stay 0
49
return;
50
}
51
52
_open_URI = p_env->GetMethodID(cls, "openURI", "(Ljava/lang/String;)I");
53
_get_cache_dir = p_env->GetMethodID(cls, "getCacheDir", "()Ljava/lang/String;");
54
_get_temp_dir = p_env->GetMethodID(cls, "getTempDir", "()Ljava/lang/String;");
55
_get_data_dir = p_env->GetMethodID(cls, "getDataDir", "()Ljava/lang/String;");
56
_get_display_cutouts = p_env->GetMethodID(cls, "getDisplayCutouts", "()[I"),
57
_get_display_safe_area = p_env->GetMethodID(cls, "getDisplaySafeArea", "()[I"),
58
_get_locale = p_env->GetMethodID(cls, "getLocale", "()Ljava/lang/String;");
59
_get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;");
60
_get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I");
61
_get_scaled_density = p_env->GetMethodID(cls, "getScaledDensity", "()F");
62
_get_screen_refresh_rate = p_env->GetMethodID(cls, "getScreenRefreshRate", "(D)D");
63
_get_unique_id = p_env->GetMethodID(cls, "getUniqueID", "()Ljava/lang/String;");
64
_show_keyboard = p_env->GetMethodID(cls, "showKeyboard", "(Ljava/lang/String;IIII)V");
65
_hide_keyboard = p_env->GetMethodID(cls, "hideKeyboard", "()V");
66
_has_hardware_keyboard = p_env->GetMethodID(cls, "hasHardwareKeyboard", "()Z");
67
_set_screen_orientation = p_env->GetMethodID(cls, "setScreenOrientation", "(I)V");
68
_get_screen_orientation = p_env->GetMethodID(cls, "getScreenOrientation", "()I");
69
_get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(IZ)Ljava/lang/String;");
70
_get_display_rotation = p_env->GetMethodID(cls, "getDisplayRotation", "()I");
71
}
72
}
73
74
GodotIOJavaWrapper::~GodotIOJavaWrapper() {
75
JNIEnv *env = get_jni_env();
76
ERR_FAIL_NULL(env);
77
78
env->DeleteGlobalRef(cls);
79
env->DeleteGlobalRef(godot_io_instance);
80
}
81
82
jobject GodotIOJavaWrapper::get_instance() {
83
return godot_io_instance;
84
}
85
86
Error GodotIOJavaWrapper::open_uri(const String &p_uri) {
87
if (_open_URI) {
88
JNIEnv *env = get_jni_env();
89
ERR_FAIL_NULL_V(env, ERR_UNAVAILABLE);
90
jstring jStr = env->NewStringUTF(p_uri.utf8().get_data());
91
Error result = env->CallIntMethod(godot_io_instance, _open_URI, jStr) ? ERR_CANT_OPEN : OK;
92
env->DeleteLocalRef(jStr);
93
return result;
94
} else {
95
return ERR_UNAVAILABLE;
96
}
97
}
98
99
String GodotIOJavaWrapper::get_cache_dir() {
100
if (_get_cache_dir) {
101
JNIEnv *env = get_jni_env();
102
ERR_FAIL_NULL_V(env, String());
103
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_cache_dir);
104
return jstring_to_string(s, env);
105
} else {
106
return String();
107
}
108
}
109
110
String GodotIOJavaWrapper::get_temp_dir() {
111
if (_get_temp_dir) {
112
JNIEnv *env = get_jni_env();
113
ERR_FAIL_NULL_V(env, String());
114
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_temp_dir);
115
return jstring_to_string(s, env);
116
} else {
117
return String();
118
}
119
}
120
121
String GodotIOJavaWrapper::get_user_data_dir(const String &p_user_dir) {
122
if (_get_data_dir) {
123
JNIEnv *env = get_jni_env();
124
ERR_FAIL_NULL_V(env, String());
125
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_data_dir);
126
return jstring_to_string(s, env);
127
} else {
128
return String();
129
}
130
}
131
132
String GodotIOJavaWrapper::get_locale() {
133
if (_get_locale) {
134
JNIEnv *env = get_jni_env();
135
ERR_FAIL_NULL_V(env, String());
136
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_locale);
137
return jstring_to_string(s, env);
138
} else {
139
return String();
140
}
141
}
142
143
String GodotIOJavaWrapper::get_model() {
144
if (_get_model) {
145
JNIEnv *env = get_jni_env();
146
ERR_FAIL_NULL_V(env, String());
147
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_model);
148
return jstring_to_string(s, env);
149
} else {
150
return String();
151
}
152
}
153
154
int GodotIOJavaWrapper::get_screen_dpi() {
155
if (_get_screen_DPI) {
156
JNIEnv *env = get_jni_env();
157
ERR_FAIL_NULL_V(env, 160);
158
return env->CallIntMethod(godot_io_instance, _get_screen_DPI);
159
} else {
160
return 160;
161
}
162
}
163
164
float GodotIOJavaWrapper::get_scaled_density() {
165
if (_get_scaled_density) {
166
JNIEnv *env = get_jni_env();
167
ERR_FAIL_NULL_V(env, 1.0f);
168
return env->CallFloatMethod(godot_io_instance, _get_scaled_density);
169
} else {
170
return 1.0f;
171
}
172
}
173
174
float GodotIOJavaWrapper::get_screen_refresh_rate(float fallback) {
175
if (_get_screen_refresh_rate) {
176
JNIEnv *env = get_jni_env();
177
if (env == nullptr) {
178
ERR_PRINT("An error occurred while trying to get screen refresh rate.");
179
return fallback;
180
}
181
return (float)env->CallDoubleMethod(godot_io_instance, _get_screen_refresh_rate, (double)fallback);
182
}
183
ERR_PRINT("An error occurred while trying to get the screen refresh rate.");
184
return fallback;
185
}
186
187
TypedArray<Rect2> GodotIOJavaWrapper::get_display_cutouts() {
188
TypedArray<Rect2> result;
189
ERR_FAIL_NULL_V(_get_display_cutouts, result);
190
JNIEnv *env = get_jni_env();
191
ERR_FAIL_NULL_V(env, result);
192
jintArray returnArray = (jintArray)env->CallObjectMethod(godot_io_instance, _get_display_cutouts);
193
jint arrayLength = env->GetArrayLength(returnArray);
194
jint *arrayBody = env->GetIntArrayElements(returnArray, JNI_FALSE);
195
int cutouts = arrayLength / 4;
196
for (int i = 0; i < cutouts; i++) {
197
int x = arrayBody[i * 4];
198
int y = arrayBody[i * 4 + 1];
199
int width = arrayBody[i * 4 + 2];
200
int height = arrayBody[i * 4 + 3];
201
Rect2 cutout(x, y, width, height);
202
result.append(cutout);
203
}
204
env->ReleaseIntArrayElements(returnArray, arrayBody, 0);
205
return result;
206
}
207
208
Rect2i GodotIOJavaWrapper::get_display_safe_area() {
209
Rect2i result;
210
ERR_FAIL_NULL_V(_get_display_safe_area, result);
211
JNIEnv *env = get_jni_env();
212
ERR_FAIL_NULL_V(env, result);
213
jintArray returnArray = (jintArray)env->CallObjectMethod(godot_io_instance, _get_display_safe_area);
214
ERR_FAIL_COND_V(env->GetArrayLength(returnArray) != 4, result);
215
jint *arrayBody = env->GetIntArrayElements(returnArray, JNI_FALSE);
216
result = Rect2i(arrayBody[0], arrayBody[1], arrayBody[2], arrayBody[3]);
217
env->ReleaseIntArrayElements(returnArray, arrayBody, 0);
218
return result;
219
}
220
221
String GodotIOJavaWrapper::get_unique_id() {
222
if (_get_unique_id) {
223
JNIEnv *env = get_jni_env();
224
ERR_FAIL_NULL_V(env, String());
225
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_unique_id);
226
return jstring_to_string(s, env);
227
} else {
228
return String();
229
}
230
}
231
232
bool GodotIOJavaWrapper::has_vk() {
233
return (_show_keyboard != nullptr) && (_hide_keyboard != nullptr);
234
}
235
236
bool GodotIOJavaWrapper::has_hardware_keyboard() {
237
if (_has_hardware_keyboard) {
238
JNIEnv *env = get_jni_env();
239
ERR_FAIL_NULL_V(env, false);
240
return env->CallBooleanMethod(godot_io_instance, _has_hardware_keyboard);
241
} else {
242
return false;
243
}
244
}
245
246
void GodotIOJavaWrapper::show_vk(const String &p_existing, int p_type, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
247
if (_show_keyboard) {
248
JNIEnv *env = get_jni_env();
249
ERR_FAIL_NULL(env);
250
jstring jStr = env->NewStringUTF(p_existing.utf8().get_data());
251
env->CallVoidMethod(godot_io_instance, _show_keyboard, jStr, p_type, p_max_input_length, p_cursor_start, p_cursor_end);
252
env->DeleteLocalRef(jStr);
253
}
254
}
255
256
void GodotIOJavaWrapper::hide_vk() {
257
if (_hide_keyboard) {
258
JNIEnv *env = get_jni_env();
259
ERR_FAIL_NULL(env);
260
env->CallVoidMethod(godot_io_instance, _hide_keyboard);
261
}
262
}
263
264
void GodotIOJavaWrapper::set_screen_orientation(int p_orient) {
265
if (_set_screen_orientation) {
266
JNIEnv *env = get_jni_env();
267
ERR_FAIL_NULL(env);
268
env->CallVoidMethod(godot_io_instance, _set_screen_orientation, p_orient);
269
}
270
}
271
272
int GodotIOJavaWrapper::get_screen_orientation() {
273
if (_get_screen_orientation) {
274
JNIEnv *env = get_jni_env();
275
ERR_FAIL_NULL_V(env, 0);
276
return env->CallIntMethod(godot_io_instance, _get_screen_orientation);
277
} else {
278
return 0;
279
}
280
}
281
282
String GodotIOJavaWrapper::get_system_dir(int p_dir, bool p_shared_storage) {
283
if (_get_system_dir) {
284
JNIEnv *env = get_jni_env();
285
ERR_FAIL_NULL_V(env, String("."));
286
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_system_dir, p_dir, p_shared_storage);
287
return jstring_to_string(s, env);
288
} else {
289
return String(".");
290
}
291
}
292
293
int GodotIOJavaWrapper::get_display_rotation() {
294
if (_get_display_rotation) {
295
JNIEnv *env = get_jni_env();
296
ERR_FAIL_NULL_V(env, 0);
297
return env->CallIntMethod(godot_io_instance, _get_display_rotation);
298
} else {
299
return 0;
300
}
301
}
302
303
// SafeNumeric because it can be changed from non-main thread and we need to
304
// ensure the change is immediately visible to other threads.
305
static SafeNumeric<int> virtual_keyboard_height;
306
307
int GodotIOJavaWrapper::get_vk_height() {
308
return virtual_keyboard_height.get();
309
}
310
311
void GodotIOJavaWrapper::set_vk_height(int p_height) {
312
virtual_keyboard_height.set(p_height);
313
}
314
315