java_godot_lib_jni.cpp 16 KB

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