java_godot_lib_jni.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "android_keys_utils.h"
  35. #include "api/java_class_wrapper.h"
  36. #include "api/jni_singleton.h"
  37. #include "audio_driver_jandroid.h"
  38. #include "core/engine.h"
  39. #include "core/project_settings.h"
  40. #include "dir_access_jandroid.h"
  41. #include "file_access_android.h"
  42. #include "file_access_jandroid.h"
  43. #include "jni_utils.h"
  44. #include "main/input_default.h"
  45. #include "main/main.h"
  46. #include "net_socket_android.h"
  47. #include "os_android.h"
  48. #include "string_android.h"
  49. #include "thread_jandroid.h"
  50. #include <android/input.h>
  51. #include <unistd.h>
  52. static JavaClassWrapper *java_class_wrapper = NULL;
  53. static OS_Android *os_android = NULL;
  54. static GodotJavaWrapper *godot_java = NULL;
  55. static GodotIOJavaWrapper *godot_io_java = NULL;
  56. static bool initialized = false;
  57. static int step = 0;
  58. static Size2 new_size;
  59. static Vector3 accelerometer;
  60. static Vector3 gravity;
  61. static Vector3 magnetometer;
  62. static Vector3 gyroscope;
  63. static void _initialize_java_modules() {
  64. if (!ProjectSettings::get_singleton()->has_setting("android/modules")) {
  65. return;
  66. }
  67. String modules = ProjectSettings::get_singleton()->get("android/modules");
  68. modules = modules.strip_edges();
  69. if (modules == String()) {
  70. return;
  71. }
  72. Vector<String> mods = modules.split(",", false);
  73. if (mods.size()) {
  74. jobject cls = godot_java->get_class_loader();
  75. // TODO create wrapper for class loader
  76. JNIEnv *env = ThreadAndroid::get_env();
  77. jclass classLoader = env->FindClass("java/lang/ClassLoader");
  78. jmethodID findClass = env->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
  79. for (int i = 0; i < mods.size(); i++) {
  80. String m = mods[i];
  81. // Deprecated in Godot 3.2.2, it's now a plugin to enable in export preset.
  82. if (m == "org/godotengine/godot/GodotPaymentV3") {
  83. WARN_PRINT("GodotPaymentV3 is deprecated and is replaced by the 'GodotPayment' plugin, which should be enabled in the Android export preset.");
  84. print_line("Skipping Android module: " + m);
  85. continue;
  86. }
  87. print_line("Loading Android module: " + m);
  88. jstring strClassName = env->NewStringUTF(m.utf8().get_data());
  89. jclass singletonClass = (jclass)env->CallObjectMethod(cls, findClass, strClassName);
  90. ERR_CONTINUE_MSG(!singletonClass, "Couldn't find singleton for class: " + m + ".");
  91. jmethodID initialize = env->GetStaticMethodID(singletonClass, "initialize", "(Landroid/app/Activity;)Lorg/godotengine/godot/Godot$SingletonBase;");
  92. ERR_CONTINUE_MSG(!initialize, "Couldn't find proper initialize function 'public static Godot.SingletonBase Class::initialize(Activity p_activity)' initializer for singleton class: " + m + ".");
  93. jobject obj = env->CallStaticObjectMethod(singletonClass, initialize, godot_java->get_activity());
  94. env->NewGlobalRef(obj);
  95. }
  96. }
  97. }
  98. extern "C" {
  99. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHeight(JNIEnv *env, jclass clazz, jint p_height) {
  100. if (godot_io_java) {
  101. godot_io_java->set_vk_height(p_height);
  102. }
  103. }
  104. 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) {
  105. initialized = true;
  106. JavaVM *jvm;
  107. env->GetJavaVM(&jvm);
  108. // create our wrapper classes
  109. godot_java = new GodotJavaWrapper(env, activity, godot_instance);
  110. godot_io_java = new GodotIOJavaWrapper(env, godot_java->get_member_object("io", "Lorg/godotengine/godot/GodotIO;", env));
  111. ThreadAndroid::make_default(jvm);
  112. #ifdef USE_JAVA_FILE_ACCESS
  113. FileAccessJAndroid::setup(godot_io_java->get_instance());
  114. #else
  115. jobject amgr = env->NewGlobalRef(p_asset_manager);
  116. FileAccessAndroid::asset_manager = AAssetManager_fromJava(env, amgr);
  117. #endif
  118. DirAccessJAndroid::setup(godot_io_java->get_instance());
  119. AudioDriverAndroid::setup(godot_io_java->get_instance());
  120. NetSocketAndroid::setup(godot_java->get_member_object("netUtils", "Lorg/godotengine/godot/utils/GodotNetUtils;", env));
  121. os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion);
  122. char wd[500];
  123. getcwd(wd, 500);
  124. godot_java->on_video_init(env);
  125. }
  126. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ondestroy(JNIEnv *env, jclass clazz) {
  127. // lets cleanup
  128. if (godot_io_java) {
  129. delete godot_io_java;
  130. }
  131. if (godot_java) {
  132. delete godot_java;
  133. }
  134. if (os_android) {
  135. delete os_android;
  136. }
  137. }
  138. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jclass clazz, jobjectArray p_cmdline) {
  139. ThreadAndroid::setup_thread();
  140. const char **cmdline = NULL;
  141. jstring *j_cmdline = NULL;
  142. int cmdlen = 0;
  143. if (p_cmdline) {
  144. cmdlen = env->GetArrayLength(p_cmdline);
  145. if (cmdlen) {
  146. cmdline = (const char **)malloc((cmdlen + 1) * sizeof(const char *));
  147. cmdline[cmdlen] = NULL;
  148. j_cmdline = (jstring *)malloc(cmdlen * sizeof(jstring));
  149. for (int i = 0; i < cmdlen; i++) {
  150. jstring string = (jstring)env->GetObjectArrayElement(p_cmdline, i);
  151. const char *rawString = env->GetStringUTFChars(string, 0);
  152. cmdline[i] = rawString;
  153. j_cmdline[i] = string;
  154. }
  155. }
  156. }
  157. Error err = Main::setup("apk", cmdlen, (char **)cmdline, false);
  158. if (cmdline) {
  159. if (j_cmdline) {
  160. for (int i = 0; i < cmdlen; ++i) {
  161. env->ReleaseStringUTFChars(j_cmdline[i], cmdline[i]);
  162. }
  163. free(j_cmdline);
  164. }
  165. free(cmdline);
  166. }
  167. if (err != OK) {
  168. return; //should exit instead and print the error
  169. }
  170. java_class_wrapper = memnew(JavaClassWrapper(godot_java->get_activity()));
  171. ClassDB::register_class<JNISingleton>();
  172. _initialize_java_modules();
  173. }
  174. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_resize(JNIEnv *env, jclass clazz, jint width, jint height) {
  175. if (os_android)
  176. os_android->set_display_size(Size2(width, height));
  177. }
  178. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_newcontext(JNIEnv *env, jclass clazz, jboolean p_32_bits) {
  179. if (os_android) {
  180. if (step == 0) {
  181. // During startup
  182. os_android->set_context_is_16_bits(!p_32_bits);
  183. } else {
  184. // GL context recreated because it was lost; restart app to let it reload everything
  185. os_android->main_loop_end();
  186. godot_java->restart(env);
  187. step = -1; // Ensure no further steps are attempted
  188. }
  189. }
  190. }
  191. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_back(JNIEnv *env, jclass clazz) {
  192. if (step == 0)
  193. return;
  194. os_android->main_loop_request_go_back();
  195. }
  196. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, jclass clazz) {
  197. if (step == -1)
  198. return;
  199. if (step == 0) {
  200. // Since Godot is initialized on the UI thread, _main_thread_id was set to that thread's id,
  201. // but for Godot purposes, the main thread is the one running the game loop
  202. Main::setup2(Thread::get_caller_id());
  203. ++step;
  204. return;
  205. }
  206. if (step == 1) {
  207. if (!Main::start()) {
  208. return; //should exit instead and print the error
  209. }
  210. os_android->main_loop_begin();
  211. godot_java->on_godot_main_loop_started(env);
  212. ++step;
  213. }
  214. os_android->process_accelerometer(accelerometer);
  215. os_android->process_gravity(gravity);
  216. os_android->process_magnetometer(magnetometer);
  217. os_android->process_gyroscope(gyroscope);
  218. if (os_android->main_loop_iterate()) {
  219. godot_java->force_quit(env);
  220. }
  221. }
  222. 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) {
  223. if (step == 0)
  224. return;
  225. Vector<OS_Android::TouchPos> points;
  226. for (int i = 0; i < pointer_count; i++) {
  227. jfloat p[3];
  228. env->GetFloatArrayRegion(positions, i * 3, 3, p);
  229. OS_Android::TouchPos tp;
  230. tp.pos = Point2(p[1], p[2]);
  231. tp.id = (int)p[0];
  232. points.push_back(tp);
  233. }
  234. if ((input_device & AINPUT_SOURCE_MOUSE) == AINPUT_SOURCE_MOUSE) {
  235. os_android->process_mouse_event(ev, buttons_mask, points[0].pos, vertical_factor, horizontal_factor);
  236. } else {
  237. os_android->process_touch(ev, pointer, points);
  238. }
  239. }
  240. 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) {
  241. touch_preprocessing(env, clazz, input_device, ev, pointer, pointer_count, position);
  242. }
  243. 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) {
  244. touch_preprocessing(env, clazz, input_device, ev, pointer, pointer_count, position, buttons_mask);
  245. }
  246. 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) {
  247. touch_preprocessing(env, clazz, input_device, ev, pointer, pointer_count, position, buttons_mask, vertical_factor, horizontal_factor);
  248. }
  249. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_hover(JNIEnv *env, jclass clazz, jint p_type, jfloat p_x, jfloat p_y) {
  250. if (step == 0)
  251. return;
  252. os_android->process_hover(p_type, Point2(p_x, p_y));
  253. }
  254. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_doubleTap(JNIEnv *env, jclass clazz, jint p_button_mask, jint p_x, jint p_y) {
  255. if (step == 0)
  256. return;
  257. os_android->process_double_tap(p_button_mask, Point2(p_x, p_y));
  258. }
  259. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_scroll(JNIEnv *env, jclass clazz, jint p_x, jint p_y) {
  260. if (step == 0)
  261. return;
  262. os_android->process_scroll(Point2(p_x, p_y));
  263. }
  264. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env, jclass clazz, jint p_device, jint p_button, jboolean p_pressed) {
  265. if (step == 0)
  266. return;
  267. OS_Android::JoypadEvent jevent;
  268. jevent.device = p_device;
  269. jevent.type = OS_Android::JOY_EVENT_BUTTON;
  270. jevent.index = p_button;
  271. jevent.pressed = p_pressed;
  272. os_android->process_joy_event(jevent);
  273. }
  274. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyaxis(JNIEnv *env, jclass clazz, jint p_device, jint p_axis, jfloat p_value) {
  275. if (step == 0)
  276. return;
  277. OS_Android::JoypadEvent jevent;
  278. jevent.device = p_device;
  279. jevent.type = OS_Android::JOY_EVENT_AXIS;
  280. jevent.index = p_axis;
  281. jevent.value = p_value;
  282. os_android->process_joy_event(jevent);
  283. }
  284. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, jclass clazz, jint p_device, jint p_hat_x, jint p_hat_y) {
  285. if (step == 0)
  286. return;
  287. OS_Android::JoypadEvent jevent;
  288. jevent.device = p_device;
  289. jevent.type = OS_Android::JOY_EVENT_HAT;
  290. int hat = 0;
  291. if (p_hat_x != 0) {
  292. if (p_hat_x < 0)
  293. hat |= InputDefault::HAT_MASK_LEFT;
  294. else
  295. hat |= InputDefault::HAT_MASK_RIGHT;
  296. }
  297. if (p_hat_y != 0) {
  298. if (p_hat_y < 0)
  299. hat |= InputDefault::HAT_MASK_UP;
  300. else
  301. hat |= InputDefault::HAT_MASK_DOWN;
  302. }
  303. jevent.hat = hat;
  304. os_android->process_joy_event(jevent);
  305. }
  306. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jclass clazz, jint p_device, jboolean p_connected, jstring p_name) {
  307. if (os_android) {
  308. String name = jstring_to_string(p_name, env);
  309. os_android->joy_connection_changed(p_device, p_connected, name);
  310. }
  311. }
  312. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_key(JNIEnv *env, jclass clazz, jint p_scancode, jint p_unicode_char, jboolean p_pressed) {
  313. if (step == 0)
  314. return;
  315. os_android->process_key_event(p_scancode, p_unicode_char, p_pressed);
  316. }
  317. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  318. accelerometer = Vector3(x, y, z);
  319. }
  320. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gravity(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  321. gravity = Vector3(x, y, z);
  322. }
  323. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  324. magnetometer = Vector3(x, y, z);
  325. }
  326. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gyroscope(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  327. gyroscope = Vector3(x, y, z);
  328. }
  329. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jclass clazz) {
  330. if (step == 0)
  331. return;
  332. os_android->main_loop_focusin();
  333. }
  334. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusout(JNIEnv *env, jclass clazz) {
  335. if (step == 0)
  336. return;
  337. os_android->main_loop_focusout();
  338. }
  339. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_audio(JNIEnv *env, jclass clazz) {
  340. ThreadAndroid::setup_thread();
  341. AudioDriverAndroid::thread_func(env);
  342. }
  343. JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getGlobal(JNIEnv *env, jclass clazz, jstring path) {
  344. String js = jstring_to_string(path, env);
  345. return env->NewStringUTF(ProjectSettings::get_singleton()->get(js).operator String().utf8().get_data());
  346. }
  347. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_callobject(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
  348. Object *obj = ObjectDB::get_instance(ID);
  349. ERR_FAIL_COND(!obj);
  350. int res = env->PushLocalFrame(16);
  351. ERR_FAIL_COND(res != 0);
  352. String str_method = jstring_to_string(method, env);
  353. int count = env->GetArrayLength(params);
  354. Variant *vlist = (Variant *)alloca(sizeof(Variant) * count);
  355. Variant **vptr = (Variant **)alloca(sizeof(Variant *) * count);
  356. for (int i = 0; i < count; i++) {
  357. jobject obj = env->GetObjectArrayElement(params, i);
  358. Variant v;
  359. if (obj)
  360. v = _jobject_to_variant(env, obj);
  361. memnew_placement(&vlist[i], Variant);
  362. vlist[i] = v;
  363. vptr[i] = &vlist[i];
  364. env->DeleteLocalRef(obj);
  365. };
  366. Variant::CallError err;
  367. obj->call(str_method, (const Variant **)vptr, count, err);
  368. // something
  369. env->PopLocalFrame(NULL);
  370. }
  371. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_calldeferred(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
  372. Object *obj = ObjectDB::get_instance(ID);
  373. ERR_FAIL_COND(!obj);
  374. int res = env->PushLocalFrame(16);
  375. ERR_FAIL_COND(res != 0);
  376. String str_method = jstring_to_string(method, env);
  377. int count = env->GetArrayLength(params);
  378. Variant args[VARIANT_ARG_MAX];
  379. for (int i = 0; i < MIN(count, VARIANT_ARG_MAX); i++) {
  380. jobject obj = env->GetObjectArrayElement(params, i);
  381. if (obj)
  382. args[i] = _jobject_to_variant(env, obj);
  383. env->DeleteLocalRef(obj);
  384. };
  385. obj->call_deferred(str_method, args[0], args[1], args[2], args[3], args[4]);
  386. // something
  387. env->PopLocalFrame(NULL);
  388. }
  389. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_requestPermissionResult(JNIEnv *env, jclass clazz, jstring p_permission, jboolean p_result) {
  390. String permission = jstring_to_string(p_permission, env);
  391. if (permission == "android.permission.RECORD_AUDIO" && p_result) {
  392. AudioDriver::get_singleton()->capture_start();
  393. }
  394. if (os_android->get_main_loop()) {
  395. os_android->get_main_loop()->emit_signal("on_request_permissions_result", permission, p_result == JNI_TRUE);
  396. }
  397. }
  398. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNIEnv *env, jclass clazz) {
  399. if (step == 0)
  400. return;
  401. if (os_android->get_main_loop()) {
  402. os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APP_RESUMED);
  403. }
  404. }
  405. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIEnv *env, jclass clazz) {
  406. if (step == 0)
  407. return;
  408. if (os_android->get_main_loop()) {
  409. os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APP_PAUSED);
  410. }
  411. }
  412. }