java_godot_lib_jni.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 "audio_driver_jandroid.h"
  37. #include "core/engine.h"
  38. #include "core/input/input.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/main.h"
  45. #include "net_socket_android.h"
  46. #include "os_android.h"
  47. #include "string_android.h"
  48. #include "thread_jandroid.h"
  49. #include <unistd.h>
  50. static JavaClassWrapper *java_class_wrapper = NULL;
  51. static OS_Android *os_android = NULL;
  52. static GodotJavaWrapper *godot_java = NULL;
  53. static GodotIOJavaWrapper *godot_io_java = NULL;
  54. static bool initialized = false;
  55. static int step = 0;
  56. static Size2 new_size;
  57. static Vector3 accelerometer;
  58. static Vector3 gravity;
  59. static Vector3 magnetometer;
  60. static Vector3 gyroscope;
  61. extern "C" {
  62. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHeight(JNIEnv *env, jclass clazz, jint p_height) {
  63. if (godot_io_java) {
  64. godot_io_java->set_vk_height(p_height);
  65. }
  66. }
  67. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *env, jclass clazz, jobject activity, jobject p_asset_manager, jboolean p_use_apk_expansion) {
  68. initialized = true;
  69. JavaVM *jvm;
  70. env->GetJavaVM(&jvm);
  71. // create our wrapper classes
  72. godot_java = new GodotJavaWrapper(env, activity); // our activity is our godot instance is our activity..
  73. godot_io_java = new GodotIOJavaWrapper(env, godot_java->get_member_object("io", "Lorg/godotengine/godot/GodotIO;", env));
  74. ThreadAndroid::make_default(jvm);
  75. #ifdef USE_JAVA_FILE_ACCESS
  76. FileAccessJAndroid::setup(godot_io_java->get_instance());
  77. #else
  78. jobject amgr = env->NewGlobalRef(p_asset_manager);
  79. FileAccessAndroid::asset_manager = AAssetManager_fromJava(env, amgr);
  80. #endif
  81. DirAccessJAndroid::setup(godot_io_java->get_instance());
  82. AudioDriverAndroid::setup(godot_io_java->get_instance());
  83. NetSocketAndroid::setup(godot_java->get_member_object("netUtils", "Lorg/godotengine/godot/utils/GodotNetUtils;", env));
  84. os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion);
  85. char wd[500];
  86. getcwd(wd, 500);
  87. godot_java->on_video_init(env);
  88. }
  89. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ondestroy(JNIEnv *env, jclass clazz, jobject activity) {
  90. // lets cleanup
  91. if (godot_io_java) {
  92. delete godot_io_java;
  93. }
  94. if (godot_java) {
  95. delete godot_java;
  96. }
  97. if (os_android) {
  98. delete os_android;
  99. }
  100. }
  101. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jclass clazz, jobjectArray p_cmdline) {
  102. ThreadAndroid::setup_thread();
  103. const char **cmdline = NULL;
  104. jstring *j_cmdline = NULL;
  105. int cmdlen = 0;
  106. if (p_cmdline) {
  107. cmdlen = env->GetArrayLength(p_cmdline);
  108. if (cmdlen) {
  109. cmdline = (const char **)malloc((cmdlen + 1) * sizeof(const char *));
  110. cmdline[cmdlen] = NULL;
  111. j_cmdline = (jstring *)malloc(cmdlen * sizeof(jstring));
  112. for (int i = 0; i < cmdlen; i++) {
  113. jstring string = (jstring)env->GetObjectArrayElement(p_cmdline, i);
  114. const char *rawString = env->GetStringUTFChars(string, 0);
  115. cmdline[i] = rawString;
  116. j_cmdline[i] = string;
  117. }
  118. }
  119. }
  120. Error err = Main::setup("apk", cmdlen, (char **)cmdline, false);
  121. if (cmdline) {
  122. if (j_cmdline) {
  123. for (int i = 0; i < cmdlen; ++i) {
  124. env->ReleaseStringUTFChars(j_cmdline[i], cmdline[i]);
  125. }
  126. free(j_cmdline);
  127. }
  128. free(cmdline);
  129. }
  130. if (err != OK) {
  131. return; //should exit instead and print the error
  132. }
  133. java_class_wrapper = memnew(JavaClassWrapper(godot_java->get_activity()));
  134. }
  135. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_resize(JNIEnv *env, jclass clazz, jint width, jint height) {
  136. if (os_android)
  137. os_android->set_display_size(Size2(width, height));
  138. }
  139. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_newcontext(JNIEnv *env, jclass clazz, jboolean p_32_bits) {
  140. if (os_android) {
  141. if (step == 0) {
  142. // During startup
  143. os_android->set_context_is_16_bits(!p_32_bits);
  144. } else {
  145. // GL context recreated because it was lost; restart app to let it reload everything
  146. os_android->main_loop_end();
  147. godot_java->restart(env);
  148. step = -1; // Ensure no further steps are attempted
  149. }
  150. }
  151. }
  152. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_back(JNIEnv *env, jclass clazz) {
  153. if (step == 0)
  154. return;
  155. os_android->main_loop_request_go_back();
  156. }
  157. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, jclass clazz) {
  158. if (step == -1)
  159. return;
  160. if (step == 0) {
  161. // Since Godot is initialized on the UI thread, _main_thread_id was set to that thread's id,
  162. // but for Godot purposes, the main thread is the one running the game loop
  163. Main::setup2(Thread::get_caller_id());
  164. ++step;
  165. return;
  166. }
  167. if (step == 1) {
  168. if (!Main::start()) {
  169. return; //should exit instead and print the error
  170. }
  171. os_android->main_loop_begin();
  172. godot_java->on_godot_main_loop_started(env);
  173. ++step;
  174. }
  175. os_android->process_accelerometer(accelerometer);
  176. os_android->process_gravity(gravity);
  177. os_android->process_magnetometer(magnetometer);
  178. os_android->process_gyroscope(gyroscope);
  179. if (os_android->main_loop_iterate()) {
  180. godot_java->force_quit(env);
  181. }
  182. }
  183. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_touch(JNIEnv *env, jclass clazz, jint ev, jint pointer, jint count, jintArray positions) {
  184. if (step == 0)
  185. return;
  186. Vector<OS_Android::TouchPos> points;
  187. for (int i = 0; i < count; i++) {
  188. jint p[3];
  189. env->GetIntArrayRegion(positions, i * 3, 3, p);
  190. OS_Android::TouchPos tp;
  191. tp.pos = Point2(p[1], p[2]);
  192. tp.id = p[0];
  193. points.push_back(tp);
  194. }
  195. os_android->process_touch(ev, pointer, points);
  196. /*
  197. if (os_android)
  198. os_android->process_touch(ev,pointer,points);
  199. */
  200. }
  201. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_hover(JNIEnv *env, jclass clazz, jint p_type, jint p_x, jint p_y) {
  202. if (step == 0)
  203. return;
  204. os_android->process_hover(p_type, Point2(p_x, p_y));
  205. }
  206. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_doubletap(JNIEnv *env, jclass clazz, jint p_x, jint p_y) {
  207. if (step == 0)
  208. return;
  209. os_android->process_double_tap(Point2(p_x, p_y));
  210. }
  211. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_scroll(JNIEnv *env, jclass clazz, jint p_x, jint p_y) {
  212. if (step == 0)
  213. return;
  214. os_android->process_scroll(Point2(p_x, p_y));
  215. }
  216. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env, jclass clazz, jint p_device, jint p_button, jboolean p_pressed) {
  217. if (step == 0)
  218. return;
  219. OS_Android::JoypadEvent jevent;
  220. jevent.device = p_device;
  221. jevent.type = OS_Android::JOY_EVENT_BUTTON;
  222. jevent.index = p_button;
  223. jevent.pressed = p_pressed;
  224. os_android->process_joy_event(jevent);
  225. }
  226. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyaxis(JNIEnv *env, jclass clazz, jint p_device, jint p_axis, jfloat p_value) {
  227. if (step == 0)
  228. return;
  229. OS_Android::JoypadEvent jevent;
  230. jevent.device = p_device;
  231. jevent.type = OS_Android::JOY_EVENT_AXIS;
  232. jevent.index = p_axis;
  233. jevent.value = p_value;
  234. os_android->process_joy_event(jevent);
  235. }
  236. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, jclass clazz, jint p_device, jint p_hat_x, jint p_hat_y) {
  237. if (step == 0)
  238. return;
  239. OS_Android::JoypadEvent jevent;
  240. jevent.device = p_device;
  241. jevent.type = OS_Android::JOY_EVENT_HAT;
  242. int hat = 0;
  243. if (p_hat_x != 0) {
  244. if (p_hat_x < 0)
  245. hat |= InputDefault::HAT_MASK_LEFT;
  246. else
  247. hat |= InputDefault::HAT_MASK_RIGHT;
  248. }
  249. if (p_hat_y != 0) {
  250. if (p_hat_y < 0)
  251. hat |= InputDefault::HAT_MASK_UP;
  252. else
  253. hat |= InputDefault::HAT_MASK_DOWN;
  254. }
  255. jevent.hat = hat;
  256. os_android->process_joy_event(jevent);
  257. }
  258. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jclass clazz, jint p_device, jboolean p_connected, jstring p_name) {
  259. if (os_android) {
  260. String name = jstring_to_string(p_name, env);
  261. os_android->joy_connection_changed(p_device, p_connected, name);
  262. }
  263. }
  264. 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) {
  265. if (step == 0)
  266. return;
  267. Ref<InputEventKey> ievent;
  268. ievent.instance();
  269. int val = p_unicode_char;
  270. int keycode = android_get_keysym(p_keycode);
  271. int phy_keycode = android_get_keysym(p_scancode);
  272. ievent->set_keycode(keycode);
  273. ievent->set_physical_keycode(phy_keycode);
  274. ievent->set_unicode(val);
  275. ievent->set_pressed(p_pressed);
  276. if (val == '\n') {
  277. ievent->set_keycode(KEY_ENTER);
  278. } else if (val == 61448) {
  279. ievent->set_keycode(KEY_BACKSPACE);
  280. ievent->set_unicode(KEY_BACKSPACE);
  281. } else if (val == 61453) {
  282. ievent->set_keycode(KEY_ENTER);
  283. ievent->set_unicode(KEY_ENTER);
  284. } else if (p_keycode == 4) {
  285. os_android->main_loop_request_go_back();
  286. }
  287. os_android->process_event(ievent);
  288. }
  289. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  290. accelerometer = Vector3(x, y, z);
  291. }
  292. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gravity(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  293. gravity = Vector3(x, y, z);
  294. }
  295. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  296. magnetometer = Vector3(x, y, z);
  297. }
  298. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gyroscope(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  299. gyroscope = Vector3(x, y, z);
  300. }
  301. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jclass clazz) {
  302. if (step == 0)
  303. return;
  304. os_android->main_loop_focusin();
  305. }
  306. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusout(JNIEnv *env, jclass clazz) {
  307. if (step == 0)
  308. return;
  309. os_android->main_loop_focusout();
  310. }
  311. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_audio(JNIEnv *env, jclass clazz) {
  312. ThreadAndroid::setup_thread();
  313. AudioDriverAndroid::thread_func(env);
  314. }
  315. JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getGlobal(JNIEnv *env, jclass clazz, jstring path) {
  316. String js = jstring_to_string(path, env);
  317. return env->NewStringUTF(ProjectSettings::get_singleton()->get(js).operator String().utf8().get_data());
  318. }
  319. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_callobject(JNIEnv *env, jclass clazz, jint ID, jstring method, jobjectArray params) {
  320. Object *obj = ObjectDB::get_instance(ObjectID((uint64_t)ID));
  321. ERR_FAIL_COND(!obj);
  322. int res = env->PushLocalFrame(16);
  323. ERR_FAIL_COND(res != 0);
  324. String str_method = jstring_to_string(method, env);
  325. int count = env->GetArrayLength(params);
  326. Variant *vlist = (Variant *)alloca(sizeof(Variant) * count);
  327. Variant **vptr = (Variant **)alloca(sizeof(Variant *) * count);
  328. for (int i = 0; i < count; i++) {
  329. jobject obj = env->GetObjectArrayElement(params, i);
  330. Variant v;
  331. if (obj)
  332. v = _jobject_to_variant(env, obj);
  333. memnew_placement(&vlist[i], Variant);
  334. vlist[i] = v;
  335. vptr[i] = &vlist[i];
  336. env->DeleteLocalRef(obj);
  337. };
  338. Callable::CallError err;
  339. obj->call(str_method, (const Variant **)vptr, count, err);
  340. // something
  341. env->PopLocalFrame(NULL);
  342. }
  343. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_calldeferred(JNIEnv *env, jclass clazz, jint ID, jstring method, jobjectArray params) {
  344. Object *obj = ObjectDB::get_instance(ObjectID((uint64_t)ID));
  345. ERR_FAIL_COND(!obj);
  346. int res = env->PushLocalFrame(16);
  347. ERR_FAIL_COND(res != 0);
  348. String str_method = jstring_to_string(method, env);
  349. int count = env->GetArrayLength(params);
  350. Variant args[VARIANT_ARG_MAX];
  351. for (int i = 0; i < MIN(count, VARIANT_ARG_MAX); i++) {
  352. jobject obj = env->GetObjectArrayElement(params, i);
  353. if (obj)
  354. args[i] = _jobject_to_variant(env, obj);
  355. env->DeleteLocalRef(obj);
  356. };
  357. obj->call_deferred(str_method, args[0], args[1], args[2], args[3], args[4]);
  358. // something
  359. env->PopLocalFrame(NULL);
  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_APP_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_APP_PAUSED);
  382. }
  383. }
  384. }