java_godot_lib_jni.cpp 15 KB

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