Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/android/java_godot_lib_jni.cpp
10277 views
1
/**************************************************************************/
2
/* java_godot_lib_jni.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_lib_jni.h"
32
33
#include "android_input_handler.h"
34
#include "api/java_class_wrapper.h"
35
#include "dir_access_jandroid.h"
36
#include "display_server_android.h"
37
#include "file_access_android.h"
38
#include "file_access_filesystem_jandroid.h"
39
#include "java_godot_io_wrapper.h"
40
#include "java_godot_wrapper.h"
41
#include "jni_utils.h"
42
#include "net_socket_android.h"
43
#include "os_android.h"
44
#include "plugin/godot_plugin_jni.h"
45
#include "thread_jandroid.h"
46
#include "tts_android.h"
47
48
#include "core/config/engine.h"
49
#include "core/config/project_settings.h"
50
#include "core/input/input.h"
51
#include "main/main.h"
52
#include "servers/rendering_server.h"
53
54
#ifndef XR_DISABLED
55
#include "servers/xr_server.h"
56
#endif // XR_DISABLED
57
58
#ifdef TOOLS_ENABLED
59
#include "editor/settings/editor_settings.h"
60
#endif
61
62
#include <android/asset_manager_jni.h>
63
#include <android/input.h>
64
#include <android/native_window_jni.h>
65
#include <unistd.h>
66
67
static JavaClassWrapper *java_class_wrapper = nullptr;
68
static OS_Android *os_android = nullptr;
69
static AndroidInputHandler *input_handler = nullptr;
70
static GodotJavaWrapper *godot_java = nullptr;
71
static GodotIOJavaWrapper *godot_io_java = nullptr;
72
73
enum StartupStep {
74
STEP_TERMINATED = -1,
75
STEP_SETUP,
76
STEP_SHOW_LOGO,
77
STEP_STARTED
78
};
79
80
static SafeNumeric<int> step; // Shared between UI and render threads
81
82
static Vector3 accelerometer;
83
static Vector3 gravity;
84
static Vector3 magnetometer;
85
static Vector3 gyroscope;
86
87
static void _terminate(JNIEnv *env, bool p_restart = false) {
88
if (step.get() == STEP_TERMINATED) {
89
return;
90
}
91
92
step.set(STEP_TERMINATED); // Ensure no further steps are attempted and no further events are sent
93
94
// lets cleanup
95
// Unregister android plugins
96
unregister_plugins_singletons();
97
98
if (java_class_wrapper) {
99
memdelete(java_class_wrapper);
100
}
101
if (input_handler) {
102
delete input_handler;
103
}
104
// Whether restarting is handled by 'Main::cleanup()'
105
bool restart_on_cleanup = false;
106
if (os_android) {
107
restart_on_cleanup = os_android->is_restart_on_exit_set();
108
os_android->main_loop_end();
109
Main::cleanup();
110
delete os_android;
111
}
112
if (godot_io_java) {
113
delete godot_io_java;
114
}
115
116
TTS_Android::terminate();
117
FileAccessAndroid::terminate();
118
DirAccessJAndroid::terminate();
119
FileAccessFilesystemJAndroid::terminate();
120
NetSocketAndroid::terminate();
121
122
cleanup_android_class_loader();
123
124
if (godot_java) {
125
godot_java->on_godot_terminating(env);
126
if (!restart_on_cleanup) {
127
if (p_restart) {
128
godot_java->restart(env);
129
} else {
130
godot_java->force_quit(env);
131
}
132
}
133
delete godot_java;
134
}
135
}
136
137
extern "C" {
138
139
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHeight(JNIEnv *env, jclass clazz, jint p_height) {
140
if (godot_io_java) {
141
godot_io_java->set_vk_height(p_height);
142
}
143
}
144
145
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *env, jclass clazz, jobject p_godot_instance, jobject p_asset_manager, jobject p_godot_io, jobject p_net_utils, jobject p_directory_access_handler, jobject p_file_access_handler, jboolean p_use_apk_expansion) {
146
JavaVM *jvm;
147
env->GetJavaVM(&jvm);
148
149
init_thread_jandroid(jvm, env);
150
setup_android_class_loader();
151
152
// create our wrapper classes
153
godot_java = new GodotJavaWrapper(env, p_godot_instance);
154
godot_io_java = new GodotIOJavaWrapper(env, p_godot_io);
155
156
FileAccessAndroid::setup(p_asset_manager);
157
DirAccessJAndroid::setup(p_directory_access_handler);
158
FileAccessFilesystemJAndroid::setup(p_file_access_handler);
159
NetSocketAndroid::setup(p_net_utils);
160
161
os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion);
162
163
return true;
164
}
165
166
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ondestroy(JNIEnv *env, jclass clazz) {
167
_terminate(env, false);
168
}
169
170
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jclass clazz, jobjectArray p_cmdline, jobject p_godot_tts) {
171
setup_android_thread();
172
173
const char **cmdline = nullptr;
174
jstring *j_cmdline = nullptr;
175
int cmdlen = 0;
176
if (p_cmdline) {
177
cmdlen = env->GetArrayLength(p_cmdline);
178
if (cmdlen) {
179
cmdline = (const char **)memalloc((cmdlen + 1) * sizeof(const char *));
180
ERR_FAIL_NULL_V_MSG(cmdline, false, "Out of memory.");
181
cmdline[cmdlen] = nullptr;
182
j_cmdline = (jstring *)memalloc(cmdlen * sizeof(jstring));
183
ERR_FAIL_NULL_V_MSG(j_cmdline, false, "Out of memory.");
184
185
for (int i = 0; i < cmdlen; i++) {
186
jstring string = (jstring)env->GetObjectArrayElement(p_cmdline, i);
187
const char *rawString = env->GetStringUTFChars(string, nullptr);
188
189
cmdline[i] = rawString;
190
j_cmdline[i] = string;
191
}
192
}
193
}
194
195
Error err = Main::setup(OS_Android::ANDROID_EXEC_PATH, cmdlen, (char **)cmdline, false);
196
if (cmdline) {
197
if (j_cmdline) {
198
for (int i = 0; i < cmdlen; ++i) {
199
env->ReleaseStringUTFChars(j_cmdline[i], cmdline[i]);
200
}
201
memfree(j_cmdline);
202
}
203
memfree(cmdline);
204
}
205
206
// Note: --help and --version return ERR_HELP, but this should be translated to 0 if exit codes are propagated.
207
if (err != OK) {
208
return false;
209
}
210
211
TTS_Android::setup(p_godot_tts);
212
213
java_class_wrapper = memnew(JavaClassWrapper);
214
return true;
215
}
216
217
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_resize(JNIEnv *env, jclass clazz, jobject p_surface, jint p_width, jint p_height) {
218
if (os_android) {
219
os_android->set_display_size(Size2i(p_width, p_height));
220
221
// No need to reset the surface during startup
222
if (step.get() > STEP_SETUP) {
223
if (p_surface) {
224
ANativeWindow *native_window = ANativeWindow_fromSurface(env, p_surface);
225
os_android->set_native_window(native_window);
226
}
227
DisplayServerAndroid::get_singleton()->reset_window();
228
DisplayServerAndroid::get_singleton()->notify_surface_changed(p_width, p_height);
229
}
230
}
231
}
232
233
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_newcontext(JNIEnv *env, jclass clazz, jobject p_surface) {
234
if (os_android) {
235
if (step.get() == STEP_SETUP) {
236
// During startup
237
if (p_surface) {
238
ANativeWindow *native_window = ANativeWindow_fromSurface(env, p_surface);
239
os_android->set_native_window(native_window);
240
}
241
} else {
242
// Rendering context recreated because it was lost; restart app to let it reload everything
243
_terminate(env, true);
244
}
245
}
246
}
247
248
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_back(JNIEnv *env, jclass clazz) {
249
if (step.get() <= STEP_SETUP) {
250
return;
251
}
252
253
if (DisplayServerAndroid *dsa = Object::cast_to<DisplayServerAndroid>(DisplayServer::get_singleton())) {
254
dsa->send_window_event(DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST);
255
}
256
}
257
258
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ttsCallback(JNIEnv *env, jclass clazz, jint event, jint id, jint pos) {
259
TTS_Android::_java_utterance_callback(event, id, pos);
260
}
261
262
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, jclass clazz) {
263
if (step.get() == STEP_TERMINATED) {
264
return true;
265
}
266
267
if (step.get() == STEP_SETUP) {
268
// Since Godot is initialized on the UI thread, main_thread_id was set to that thread's id,
269
// but for Godot purposes, the main thread is the one running the game loop
270
Main::setup2(false); // The logo is shown in the next frame otherwise we run into rendering issues
271
input_handler = new AndroidInputHandler();
272
step.increment();
273
return true;
274
}
275
276
if (step.get() == STEP_SHOW_LOGO) {
277
bool xr_enabled = false;
278
#ifndef XR_DISABLED
279
// Unlike PCVR, there's no additional 2D screen onto which to render the boot logo,
280
// so we skip this step if xr is enabled.
281
if (XRServer::get_xr_mode() == XRServer::XRMODE_DEFAULT) {
282
xr_enabled = GLOBAL_GET_CACHED(bool, "xr/shaders/enabled");
283
} else {
284
xr_enabled = XRServer::get_xr_mode() == XRServer::XRMODE_ON;
285
}
286
#endif // XR_DISABLED
287
if (!xr_enabled) {
288
Main::setup_boot_logo();
289
}
290
291
step.increment();
292
return true;
293
}
294
295
if (step.get() == STEP_STARTED) {
296
if (Main::start() != EXIT_SUCCESS) {
297
return true; // should exit instead and print the error
298
}
299
300
godot_java->on_godot_setup_completed(env);
301
os_android->main_loop_begin();
302
godot_java->on_godot_main_loop_started(env);
303
step.increment();
304
}
305
306
DisplayServerAndroid::get_singleton()->process_accelerometer(accelerometer);
307
DisplayServerAndroid::get_singleton()->process_gravity(gravity);
308
DisplayServerAndroid::get_singleton()->process_magnetometer(magnetometer);
309
DisplayServerAndroid::get_singleton()->process_gyroscope(gyroscope);
310
311
bool should_swap_buffers = false;
312
if (os_android->main_loop_iterate(&should_swap_buffers)) {
313
_terminate(env, false);
314
}
315
316
return should_swap_buffers;
317
}
318
319
// Called on the UI thread
320
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_dispatchMouseEvent(JNIEnv *env, jclass clazz, jint p_event_type, jint p_button_mask, jfloat p_x, jfloat p_y, jfloat p_delta_x, jfloat p_delta_y, jboolean p_double_click, jboolean p_source_mouse_relative, jfloat p_pressure, jfloat p_tilt_x, jfloat p_tilt_y) {
321
if (step.get() <= STEP_SETUP) {
322
return;
323
}
324
325
input_handler->process_mouse_event(p_event_type, p_button_mask, Point2(p_x, p_y), Vector2(p_delta_x, p_delta_y), p_double_click, p_source_mouse_relative, p_pressure, Vector2(p_tilt_x, p_tilt_y));
326
}
327
328
// Called on the UI thread
329
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_dispatchTouchEvent(JNIEnv *env, jclass clazz, jint ev, jint pointer, jint pointer_count, jfloatArray position, jboolean p_double_tap) {
330
if (step.get() <= STEP_SETUP) {
331
return;
332
}
333
334
Vector<AndroidInputHandler::TouchPos> points;
335
for (int i = 0; i < pointer_count; i++) {
336
jfloat p[6];
337
env->GetFloatArrayRegion(position, i * 6, 6, p);
338
AndroidInputHandler::TouchPos tp;
339
tp.id = (int)p[0];
340
tp.pos = Point2(p[1], p[2]);
341
tp.pressure = p[3];
342
tp.tilt = Vector2(p[4], p[5]);
343
points.push_back(tp);
344
}
345
346
input_handler->process_touch_event(ev, pointer, points, p_double_tap);
347
}
348
349
// Called on the UI thread
350
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnify(JNIEnv *env, jclass clazz, jfloat p_x, jfloat p_y, jfloat p_factor) {
351
if (step.get() <= STEP_SETUP) {
352
return;
353
}
354
input_handler->process_magnify(Point2(p_x, p_y), p_factor);
355
}
356
357
// Called on the UI thread
358
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_pan(JNIEnv *env, jclass clazz, jfloat p_x, jfloat p_y, jfloat p_delta_x, jfloat p_delta_y) {
359
if (step.get() <= STEP_SETUP) {
360
return;
361
}
362
input_handler->process_pan(Point2(p_x, p_y), Vector2(p_delta_x, p_delta_y));
363
}
364
365
// Called on the UI thread
366
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env, jclass clazz, jint p_device, jint p_button, jboolean p_pressed) {
367
if (step.get() <= STEP_SETUP) {
368
return;
369
}
370
371
AndroidInputHandler::JoypadEvent jevent;
372
jevent.device = p_device;
373
jevent.type = AndroidInputHandler::JOY_EVENT_BUTTON;
374
jevent.index = p_button;
375
jevent.pressed = p_pressed;
376
377
input_handler->process_joy_event(jevent);
378
}
379
380
// Called on the UI thread
381
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyaxis(JNIEnv *env, jclass clazz, jint p_device, jint p_axis, jfloat p_value) {
382
if (step.get() <= STEP_SETUP) {
383
return;
384
}
385
386
AndroidInputHandler::JoypadEvent jevent;
387
jevent.device = p_device;
388
jevent.type = AndroidInputHandler::JOY_EVENT_AXIS;
389
jevent.index = p_axis;
390
jevent.value = p_value;
391
392
input_handler->process_joy_event(jevent);
393
}
394
395
// Called on the UI thread
396
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, jclass clazz, jint p_device, jint p_hat_x, jint p_hat_y) {
397
if (step.get() <= STEP_SETUP) {
398
return;
399
}
400
401
AndroidInputHandler::JoypadEvent jevent;
402
jevent.device = p_device;
403
jevent.type = AndroidInputHandler::JOY_EVENT_HAT;
404
BitField<HatMask> hat = HatMask::CENTER;
405
if (p_hat_x != 0) {
406
if (p_hat_x < 0) {
407
hat.set_flag(HatMask::LEFT);
408
} else {
409
hat.set_flag(HatMask::RIGHT);
410
}
411
}
412
if (p_hat_y != 0) {
413
if (p_hat_y < 0) {
414
hat.set_flag(HatMask::UP);
415
} else {
416
hat.set_flag(HatMask::DOWN);
417
}
418
}
419
jevent.hat = hat;
420
421
input_handler->process_joy_event(jevent);
422
}
423
424
// Called on the UI thread
425
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jclass clazz, jint p_device, jboolean p_connected, jstring p_name) {
426
if (os_android) {
427
String name = jstring_to_string(p_name, env);
428
Input::get_singleton()->joy_connection_changed(p_device, p_connected, name);
429
}
430
}
431
432
// Called on the UI thread
433
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_key(JNIEnv *env, jclass clazz, jint p_physical_keycode, jint p_unicode, jint p_key_label, jboolean p_pressed, jboolean p_echo) {
434
if (step.get() <= STEP_SETUP) {
435
return;
436
}
437
input_handler->process_key_event(p_physical_keycode, p_unicode, p_key_label, p_pressed, p_echo);
438
}
439
440
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
441
accelerometer = Vector3(x, y, z);
442
}
443
444
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gravity(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
445
gravity = Vector3(x, y, z);
446
}
447
448
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
449
magnetometer = Vector3(x, y, z);
450
}
451
452
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gyroscope(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
453
gyroscope = Vector3(x, y, z);
454
}
455
456
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jclass clazz) {
457
if (step.get() <= STEP_SETUP) {
458
return;
459
}
460
461
os_android->main_loop_focusin();
462
}
463
464
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusout(JNIEnv *env, jclass clazz) {
465
if (step.get() <= STEP_SETUP) {
466
return;
467
}
468
469
os_android->main_loop_focusout();
470
}
471
472
JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getGlobal(JNIEnv *env, jclass clazz, jstring path) {
473
String js = jstring_to_string(path, env);
474
475
Variant setting_with_override = GLOBAL_GET(js);
476
String setting_value = (setting_with_override.get_type() == Variant::NIL) ? "" : setting_with_override;
477
return env->NewStringUTF(setting_value.utf8().get_data());
478
}
479
480
JNIEXPORT jobjectArray JNICALL Java_org_godotengine_godot_GodotLib_getRendererInfo(JNIEnv *env, jclass clazz) {
481
String rendering_driver = RenderingServer::get_singleton()->get_current_rendering_driver_name();
482
String rendering_method = RenderingServer::get_singleton()->get_current_rendering_method();
483
484
jobjectArray result = env->NewObjectArray(2, jni_find_class(env, "java/lang/String"), nullptr);
485
env->SetObjectArrayElement(result, 0, env->NewStringUTF(rendering_driver.utf8().get_data()));
486
env->SetObjectArrayElement(result, 1, env->NewStringUTF(rendering_method.utf8().get_data()));
487
488
return result;
489
}
490
491
JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getEditorSetting(JNIEnv *env, jclass clazz, jstring p_setting_key) {
492
String editor_setting_value = "";
493
#ifdef TOOLS_ENABLED
494
String godot_setting_key = jstring_to_string(p_setting_key, env);
495
Variant editor_setting = EDITOR_GET(godot_setting_key);
496
editor_setting_value = (editor_setting.get_type() == Variant::NIL) ? "" : editor_setting;
497
#else
498
WARN_PRINT("Access to the Editor Settings in only available on Editor builds");
499
#endif
500
501
return env->NewStringUTF(editor_setting_value.utf8().get_data());
502
}
503
504
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setEditorSetting(JNIEnv *env, jclass clazz, jstring p_key, jobject p_data) {
505
#ifdef TOOLS_ENABLED
506
if (EditorSettings::get_singleton() != nullptr) {
507
String key = jstring_to_string(p_key, env);
508
Variant data = _jobject_to_variant(env, p_data);
509
EditorSettings::get_singleton()->set(key, data);
510
}
511
#else
512
WARN_PRINT("Access to the Editor Settings in only available on Editor builds");
513
#endif
514
}
515
516
JNIEXPORT jobject JNICALL Java_org_godotengine_godot_GodotLib_getEditorProjectMetadata(JNIEnv *env, jclass clazz, jstring p_section, jstring p_key, jobject p_default_value) {
517
jvalret result;
518
519
#ifdef TOOLS_ENABLED
520
if (EditorSettings::get_singleton() != nullptr) {
521
String section = jstring_to_string(p_section, env);
522
String key = jstring_to_string(p_key, env);
523
Variant default_value = _jobject_to_variant(env, p_default_value);
524
Variant data = EditorSettings::get_singleton()->get_project_metadata(section, key, default_value);
525
result = _variant_to_jvalue(env, data.get_type(), &data, true);
526
}
527
#else
528
WARN_PRINT("Access to the Editor Settings Project Metadata is only available on Editor builds");
529
#endif
530
531
return result.obj;
532
}
533
534
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setEditorProjectMetadata(JNIEnv *env, jclass clazz, jstring p_section, jstring p_key, jobject p_data) {
535
#ifdef TOOLS_ENABLED
536
if (EditorSettings::get_singleton() != nullptr) {
537
String section = jstring_to_string(p_section, env);
538
String key = jstring_to_string(p_key, env);
539
Variant data = _jobject_to_variant(env, p_data);
540
EditorSettings::get_singleton()->set_project_metadata(section, key, data);
541
}
542
#else
543
WARN_PRINT("Access to the Editor Settings Project Metadata is only available on Editor builds");
544
#endif
545
}
546
547
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onNightModeChanged(JNIEnv *env, jclass clazz) {
548
DisplayServerAndroid *ds = (DisplayServerAndroid *)DisplayServer::get_singleton();
549
if (ds) {
550
ds->emit_system_theme_changed();
551
}
552
}
553
554
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_hardwareKeyboardConnected(JNIEnv *env, jclass clazz, jboolean p_connected) {
555
DisplayServerAndroid *ds = (DisplayServerAndroid *)DisplayServer::get_singleton();
556
if (ds) {
557
ds->emit_hardware_keyboard_connection_changed(p_connected);
558
}
559
}
560
561
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_filePickerCallback(JNIEnv *env, jclass clazz, jboolean p_ok, jobjectArray p_selected_paths) {
562
DisplayServerAndroid *ds = (DisplayServerAndroid *)DisplayServer::get_singleton();
563
if (ds) {
564
Vector<String> selected_paths;
565
566
jint length = env->GetArrayLength(p_selected_paths);
567
for (jint i = 0; i < length; ++i) {
568
jstring java_string = (jstring)env->GetObjectArrayElement(p_selected_paths, i);
569
String path = jstring_to_string(java_string, env);
570
selected_paths.push_back(path);
571
env->DeleteLocalRef(java_string);
572
}
573
ds->emit_file_picker_callback(p_ok, selected_paths);
574
}
575
}
576
577
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_requestPermissionResult(JNIEnv *env, jclass clazz, jstring p_permission, jboolean p_result) {
578
String permission = jstring_to_string(p_permission, env);
579
if (permission == "android.permission.RECORD_AUDIO" && p_result) {
580
AudioDriver::get_singleton()->input_start();
581
}
582
583
if (os_android->get_main_loop()) {
584
os_android->get_main_loop()->emit_signal(SNAME("on_request_permissions_result"), permission, p_result == JNI_TRUE);
585
}
586
}
587
588
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNIEnv *env, jclass clazz) {
589
if (step.get() <= STEP_SETUP) {
590
return;
591
}
592
593
// We force redraw to ensure we render at least once when resuming the app.
594
Main::force_redraw();
595
if (os_android->get_main_loop()) {
596
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
597
}
598
}
599
600
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIEnv *env, jclass clazz) {
601
if (step.get() <= STEP_SETUP) {
602
return;
603
}
604
605
if (os_android->get_main_loop()) {
606
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
607
}
608
}
609
610
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_shouldDispatchInputToRenderThread(JNIEnv *env, jclass clazz) {
611
Input *input = Input::get_singleton();
612
if (input) {
613
return !input->is_agile_input_event_flushing();
614
}
615
return false;
616
}
617
618
JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getProjectResourceDir(JNIEnv *env, jclass clazz) {
619
const String resource_dir = OS::get_singleton()->get_resource_dir();
620
return env->NewStringUTF(resource_dir.utf8().get_data());
621
}
622
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_isEditorHint(JNIEnv *env, jclass clazz) {
623
Engine *engine = Engine::get_singleton();
624
if (engine) {
625
return engine->is_editor_hint();
626
}
627
return false;
628
}
629
630
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_isProjectManagerHint(JNIEnv *env, jclass clazz) {
631
Engine *engine = Engine::get_singleton();
632
if (engine) {
633
return engine->is_project_manager_hint();
634
}
635
return false;
636
}
637
}
638
639