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