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