java_godot_lib_jni.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "java_godot_lib_jni.h"
  31. #include "java_godot_io_wrapper.h"
  32. #include "java_godot_wrapper.h"
  33. #include "android/asset_manager_jni.h"
  34. #include "api/java_class_wrapper.h"
  35. #include "api/jni_singleton.h"
  36. #include "core/config/engine.h"
  37. #include "core/config/project_settings.h"
  38. #include "core/input/input.h"
  39. #include "dir_access_jandroid.h"
  40. #include "display_server_android.h"
  41. #include "file_access_android.h"
  42. #include "jni_utils.h"
  43. #include "main/main.h"
  44. #include "net_socket_android.h"
  45. #include "os_android.h"
  46. #include "string_android.h"
  47. #include "thread_jandroid.h"
  48. #include <android/input.h>
  49. #include <unistd.h>
  50. #include <android/native_window_jni.h>
  51. static JavaClassWrapper *java_class_wrapper = nullptr;
  52. static OS_Android *os_android = nullptr;
  53. static GodotJavaWrapper *godot_java = nullptr;
  54. static GodotIOJavaWrapper *godot_io_java = nullptr;
  55. static bool initialized = false;
  56. static int step = 0;
  57. static Size2 new_size;
  58. static Vector3 accelerometer;
  59. static Vector3 gravity;
  60. static Vector3 magnetometer;
  61. static Vector3 gyroscope;
  62. extern "C" {
  63. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHeight(JNIEnv *env, jclass clazz, jint p_height) {
  64. if (godot_io_java) {
  65. godot_io_java->set_vk_height(p_height);
  66. }
  67. }
  68. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *env, jclass clazz, jobject activity, jobject godot_instance, jobject p_asset_manager, jboolean p_use_apk_expansion) {
  69. initialized = true;
  70. JavaVM *jvm;
  71. env->GetJavaVM(&jvm);
  72. // create our wrapper classes
  73. godot_java = new GodotJavaWrapper(env, activity, godot_instance);
  74. godot_io_java = new GodotIOJavaWrapper(env, godot_java->get_member_object("io", "Lorg/godotengine/godot/GodotIO;", env));
  75. init_thread_jandroid(jvm, env);
  76. jobject amgr = env->NewGlobalRef(p_asset_manager);
  77. FileAccessAndroid::asset_manager = AAssetManager_fromJava(env, amgr);
  78. DirAccessJAndroid::setup(godot_io_java->get_instance());
  79. NetSocketAndroid::setup(godot_java->get_member_object("netUtils", "Lorg/godotengine/godot/utils/GodotNetUtils;", env));
  80. os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion);
  81. char wd[500];
  82. getcwd(wd, 500);
  83. godot_java->on_video_init(env);
  84. }
  85. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ondestroy(JNIEnv *env, jclass clazz) {
  86. // lets cleanup
  87. if (godot_io_java) {
  88. delete godot_io_java;
  89. }
  90. if (godot_java) {
  91. delete godot_java;
  92. }
  93. if (os_android) {
  94. delete os_android;
  95. }
  96. }
  97. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jclass clazz, jobjectArray p_cmdline) {
  98. setup_android_thread();
  99. const char **cmdline = nullptr;
  100. jstring *j_cmdline = nullptr;
  101. int cmdlen = 0;
  102. if (p_cmdline) {
  103. cmdlen = env->GetArrayLength(p_cmdline);
  104. if (cmdlen) {
  105. cmdline = (const char **)memalloc((cmdlen + 1) * sizeof(const char *));
  106. ERR_FAIL_NULL_MSG(cmdline, "Out of memory.");
  107. cmdline[cmdlen] = nullptr;
  108. j_cmdline = (jstring *)memalloc(cmdlen * sizeof(jstring));
  109. ERR_FAIL_NULL_MSG(j_cmdline, "Out of memory.");
  110. for (int i = 0; i < cmdlen; i++) {
  111. jstring string = (jstring)env->GetObjectArrayElement(p_cmdline, i);
  112. const char *rawString = env->GetStringUTFChars(string, 0);
  113. cmdline[i] = rawString;
  114. j_cmdline[i] = string;
  115. }
  116. }
  117. }
  118. Error err = Main::setup("apk", cmdlen, (char **)cmdline, false);
  119. if (cmdline) {
  120. if (j_cmdline) {
  121. for (int i = 0; i < cmdlen; ++i) {
  122. env->ReleaseStringUTFChars(j_cmdline[i], cmdline[i]);
  123. }
  124. memfree(j_cmdline);
  125. }
  126. memfree(cmdline);
  127. }
  128. if (err != OK) {
  129. return; // should exit instead and print the error
  130. }
  131. java_class_wrapper = memnew(JavaClassWrapper(godot_java->get_activity()));
  132. ClassDB::register_class<JNISingleton>();
  133. }
  134. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_resize(JNIEnv *env, jclass clazz, jobject p_surface, jint p_width, jint p_height) {
  135. if (os_android) {
  136. os_android->set_display_size(Size2i(p_width, p_height));
  137. // No need to reset the surface during startup
  138. if (step > 0) {
  139. if (p_surface) {
  140. ANativeWindow *native_window = ANativeWindow_fromSurface(env, p_surface);
  141. os_android->set_native_window(native_window);
  142. DisplayServerAndroid::get_singleton()->reset_window();
  143. DisplayServerAndroid::get_singleton()->notify_surface_changed(p_width, p_height);
  144. }
  145. }
  146. }
  147. }
  148. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_newcontext(JNIEnv *env, jclass clazz, jobject p_surface, jboolean p_32_bits) {
  149. if (os_android) {
  150. if (step == 0) {
  151. // During startup
  152. os_android->set_context_is_16_bits(!p_32_bits);
  153. if (p_surface) {
  154. ANativeWindow *native_window = ANativeWindow_fromSurface(env, p_surface);
  155. os_android->set_native_window(native_window);
  156. }
  157. } else {
  158. // Rendering context recreated because it was lost; restart app to let it reload everything
  159. os_android->main_loop_end();
  160. godot_java->restart(env);
  161. step = -1; // Ensure no further steps are attempted
  162. }
  163. }
  164. }
  165. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_back(JNIEnv *env, jclass clazz) {
  166. if (step == 0)
  167. return;
  168. os_android->main_loop_request_go_back();
  169. }
  170. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, jclass clazz) {
  171. if (step == -1)
  172. return;
  173. if (step == 0) {
  174. // Since Godot is initialized on the UI thread, main_thread_id was set to that thread's id,
  175. // but for Godot purposes, the main thread is the one running the game loop
  176. Main::setup2(Thread::get_caller_id());
  177. ++step;
  178. return;
  179. }
  180. if (step == 1) {
  181. if (!Main::start()) {
  182. return; // should exit instead and print the error
  183. }
  184. godot_java->on_godot_setup_completed(env);
  185. os_android->main_loop_begin();
  186. godot_java->on_godot_main_loop_started(env);
  187. ++step;
  188. }
  189. DisplayServerAndroid::get_singleton()->process_accelerometer(accelerometer);
  190. DisplayServerAndroid::get_singleton()->process_gravity(gravity);
  191. DisplayServerAndroid::get_singleton()->process_magnetometer(magnetometer);
  192. DisplayServerAndroid::get_singleton()->process_gyroscope(gyroscope);
  193. if (os_android->main_loop_iterate()) {
  194. godot_java->force_quit(env);
  195. }
  196. }
  197. void touch_preprocessing(JNIEnv *env, jclass clazz, jint input_device, jint ev, jint pointer, jint pointer_count, jfloatArray positions, jint buttons_mask, jfloat vertical_factor, jfloat horizontal_factor) {
  198. if (step == 0)
  199. return;
  200. Vector<DisplayServerAndroid::TouchPos> points;
  201. for (int i = 0; i < pointer_count; i++) {
  202. jfloat p[3];
  203. env->GetFloatArrayRegion(positions, i * 3, 3, p);
  204. DisplayServerAndroid::TouchPos tp;
  205. tp.pos = Point2(p[1], p[2]);
  206. tp.id = (int)p[0];
  207. points.push_back(tp);
  208. }
  209. if ((input_device & AINPUT_SOURCE_MOUSE) == AINPUT_SOURCE_MOUSE || (input_device & AINPUT_SOURCE_MOUSE_RELATIVE) == AINPUT_SOURCE_MOUSE_RELATIVE) {
  210. DisplayServerAndroid::get_singleton()->process_mouse_event(input_device, ev, buttons_mask, points[0].pos, vertical_factor, horizontal_factor);
  211. } else {
  212. DisplayServerAndroid::get_singleton()->process_touch(ev, pointer, points);
  213. }
  214. }
  215. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_touch__IIII_3F(JNIEnv *env, jclass clazz, jint input_device, jint ev, jint pointer, jint pointer_count, jfloatArray position) {
  216. touch_preprocessing(env, clazz, input_device, ev, pointer, pointer_count, position);
  217. }
  218. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_touch__IIII_3FI(JNIEnv *env, jclass clazz, jint input_device, jint ev, jint pointer, jint pointer_count, jfloatArray position, jint buttons_mask) {
  219. touch_preprocessing(env, clazz, input_device, ev, pointer, pointer_count, position, buttons_mask);
  220. }
  221. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_touch__IIII_3FIFF(JNIEnv *env, jclass clazz, jint input_device, jint ev, jint pointer, jint pointer_count, jfloatArray position, jint buttons_mask, jfloat vertical_factor, jfloat horizontal_factor) {
  222. touch_preprocessing(env, clazz, input_device, ev, pointer, pointer_count, position, buttons_mask, vertical_factor, horizontal_factor);
  223. }
  224. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_hover(JNIEnv *env, jclass clazz, jint p_type, jfloat p_x, jfloat p_y) {
  225. if (step == 0)
  226. return;
  227. DisplayServerAndroid::get_singleton()->process_hover(p_type, Point2(p_x, p_y));
  228. }
  229. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_doubleTap(JNIEnv *env, jclass clazz, jint p_button_mask, jint p_x, jint p_y) {
  230. if (step == 0)
  231. return;
  232. DisplayServerAndroid::get_singleton()->process_double_tap(p_button_mask, Point2(p_x, p_y));
  233. }
  234. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_scroll(JNIEnv *env, jclass clazz, jint p_x, jint p_y) {
  235. if (step == 0)
  236. return;
  237. DisplayServerAndroid::get_singleton()->process_scroll(Point2(p_x, p_y));
  238. }
  239. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env, jclass clazz, jint p_device, jint p_button, jboolean p_pressed) {
  240. if (step == 0)
  241. return;
  242. DisplayServerAndroid::JoypadEvent jevent;
  243. jevent.device = p_device;
  244. jevent.type = DisplayServerAndroid::JOY_EVENT_BUTTON;
  245. jevent.index = p_button;
  246. jevent.pressed = p_pressed;
  247. DisplayServerAndroid::get_singleton()->process_joy_event(jevent);
  248. }
  249. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyaxis(JNIEnv *env, jclass clazz, jint p_device, jint p_axis, jfloat p_value) {
  250. if (step == 0)
  251. return;
  252. DisplayServerAndroid::JoypadEvent jevent;
  253. jevent.device = p_device;
  254. jevent.type = DisplayServerAndroid::JOY_EVENT_AXIS;
  255. jevent.index = p_axis;
  256. jevent.value = p_value;
  257. DisplayServerAndroid::get_singleton()->process_joy_event(jevent);
  258. }
  259. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, jclass clazz, jint p_device, jint p_hat_x, jint p_hat_y) {
  260. if (step == 0)
  261. return;
  262. DisplayServerAndroid::JoypadEvent jevent;
  263. jevent.device = p_device;
  264. jevent.type = DisplayServerAndroid::JOY_EVENT_HAT;
  265. int hat = 0;
  266. if (p_hat_x != 0) {
  267. if (p_hat_x < 0)
  268. hat |= HatMask::HAT_MASK_LEFT;
  269. else
  270. hat |= HatMask::HAT_MASK_RIGHT;
  271. }
  272. if (p_hat_y != 0) {
  273. if (p_hat_y < 0)
  274. hat |= HatMask::HAT_MASK_UP;
  275. else
  276. hat |= HatMask::HAT_MASK_DOWN;
  277. }
  278. jevent.hat = hat;
  279. DisplayServerAndroid::get_singleton()->process_joy_event(jevent);
  280. }
  281. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jclass clazz, jint p_device, jboolean p_connected, jstring p_name) {
  282. if (os_android) {
  283. String name = jstring_to_string(p_name, env);
  284. Input::get_singleton()->joy_connection_changed(p_device, p_connected, name);
  285. }
  286. }
  287. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_key(JNIEnv *env, jclass clazz, jint p_keycode, jint p_scancode, jint p_unicode_char, jboolean p_pressed) {
  288. if (step == 0)
  289. return;
  290. DisplayServerAndroid::get_singleton()->process_key_event(p_keycode, p_scancode, p_unicode_char, p_pressed);
  291. }
  292. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  293. accelerometer = Vector3(x, y, z);
  294. }
  295. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gravity(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  296. gravity = Vector3(x, y, z);
  297. }
  298. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  299. magnetometer = Vector3(x, y, z);
  300. }
  301. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gyroscope(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  302. gyroscope = Vector3(x, y, z);
  303. }
  304. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jclass clazz) {
  305. if (step == 0)
  306. return;
  307. os_android->main_loop_focusin();
  308. }
  309. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusout(JNIEnv *env, jclass clazz) {
  310. if (step == 0)
  311. return;
  312. os_android->main_loop_focusout();
  313. }
  314. JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getGlobal(JNIEnv *env, jclass clazz, jstring path) {
  315. String js = jstring_to_string(path, env);
  316. return env->NewStringUTF(ProjectSettings::get_singleton()->get(js).operator String().utf8().get_data());
  317. }
  318. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_callobject(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
  319. Object *obj = ObjectDB::get_instance(ObjectID(ID));
  320. ERR_FAIL_COND(!obj);
  321. int res = env->PushLocalFrame(16);
  322. ERR_FAIL_COND(res != 0);
  323. String str_method = jstring_to_string(method, env);
  324. int count = env->GetArrayLength(params);
  325. Variant *vlist = (Variant *)alloca(sizeof(Variant) * count);
  326. Variant **vptr = (Variant **)alloca(sizeof(Variant *) * count);
  327. for (int i = 0; i < count; i++) {
  328. jobject obj = env->GetObjectArrayElement(params, i);
  329. Variant v;
  330. if (obj)
  331. v = _jobject_to_variant(env, obj);
  332. memnew_placement(&vlist[i], Variant);
  333. vlist[i] = v;
  334. vptr[i] = &vlist[i];
  335. env->DeleteLocalRef(obj);
  336. };
  337. Callable::CallError err;
  338. obj->call(str_method, (const Variant **)vptr, count, err);
  339. // something
  340. env->PopLocalFrame(nullptr);
  341. }
  342. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_calldeferred(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
  343. Object *obj = ObjectDB::get_instance(ObjectID(ID));
  344. ERR_FAIL_COND(!obj);
  345. int res = env->PushLocalFrame(16);
  346. ERR_FAIL_COND(res != 0);
  347. String str_method = jstring_to_string(method, env);
  348. int count = env->GetArrayLength(params);
  349. Variant args[VARIANT_ARG_MAX];
  350. for (int i = 0; i < MIN(count, VARIANT_ARG_MAX); i++) {
  351. jobject obj = env->GetObjectArrayElement(params, i);
  352. if (obj)
  353. args[i] = _jobject_to_variant(env, obj);
  354. env->DeleteLocalRef(obj);
  355. };
  356. static_assert(VARIANT_ARG_MAX == 5, "This code needs to be updated if VARIANT_ARG_MAX != 5");
  357. obj->call_deferred(str_method, args[0], args[1], args[2], args[3], args[4]);
  358. // something
  359. env->PopLocalFrame(nullptr);
  360. }
  361. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_requestPermissionResult(JNIEnv *env, jclass clazz, jstring p_permission, jboolean p_result) {
  362. String permission = jstring_to_string(p_permission, env);
  363. if (permission == "android.permission.RECORD_AUDIO" && p_result) {
  364. AudioDriver::get_singleton()->capture_start();
  365. }
  366. if (os_android->get_main_loop()) {
  367. os_android->get_main_loop()->emit_signal("on_request_permissions_result", permission, p_result == JNI_TRUE);
  368. }
  369. }
  370. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNIEnv *env, jclass clazz) {
  371. if (step == 0)
  372. return;
  373. if (os_android->get_main_loop()) {
  374. os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
  375. }
  376. }
  377. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIEnv *env, jclass clazz) {
  378. if (step == 0)
  379. return;
  380. if (os_android->get_main_loop()) {
  381. os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
  382. }
  383. }
  384. }