java_godot_wrapper.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /**************************************************************************/
  2. /* java_godot_wrapper.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_wrapper.h"
  31. // JNIEnv is only valid within the thread it belongs to, in a multi threading environment
  32. // we can't cache it.
  33. // For Godot we call most access methods from our thread and we thus get a valid JNIEnv
  34. // from ThreadAndroid. For one or two we expect to pass the environment
  35. // TODO we could probably create a base class for this...
  36. GodotJavaWrapper::GodotJavaWrapper(JNIEnv *p_env, jobject p_activity, jobject p_godot_instance) {
  37. godot_instance = p_env->NewGlobalRef(p_godot_instance);
  38. activity = p_env->NewGlobalRef(p_activity);
  39. // get info about our Godot class so we can get pointers and stuff...
  40. godot_class = p_env->FindClass("org/godotengine/godot/Godot");
  41. if (godot_class) {
  42. godot_class = (jclass)p_env->NewGlobalRef(godot_class);
  43. } else {
  44. // this is a pretty serious fail.. bail... pointers will stay 0
  45. return;
  46. }
  47. activity_class = p_env->FindClass("android/app/Activity");
  48. if (activity_class) {
  49. activity_class = (jclass)p_env->NewGlobalRef(activity_class);
  50. } else {
  51. // this is a pretty serious fail.. bail... pointers will stay 0
  52. return;
  53. }
  54. // get some Godot method pointers...
  55. _on_video_init = p_env->GetMethodID(godot_class, "onVideoInit", "()V");
  56. _create_offscreen_gl = p_env->GetMethodID(godot_class, "createOffscreenGL", "()Z");
  57. _destroy_offscreen_gl = p_env->GetMethodID(godot_class, "destroyOffscreenGL", "()V");
  58. _set_offscreen_gl_current = p_env->GetMethodID(godot_class, "setOffscreenGLCurrent", "(Z)V");
  59. _restart = p_env->GetMethodID(godot_class, "restart", "()V");
  60. _finish = p_env->GetMethodID(godot_class, "forceQuit", "(I)Z");
  61. _set_keep_screen_on = p_env->GetMethodID(godot_class, "setKeepScreenOn", "(Z)V");
  62. _alert = p_env->GetMethodID(godot_class, "alert", "(Ljava/lang/String;Ljava/lang/String;)V");
  63. _get_GLES_version_code = p_env->GetMethodID(godot_class, "getGLESVersionCode", "()I");
  64. _get_clipboard = p_env->GetMethodID(godot_class, "getClipboard", "()Ljava/lang/String;");
  65. _set_clipboard = p_env->GetMethodID(godot_class, "setClipboard", "(Ljava/lang/String;)V");
  66. _has_clipboard = p_env->GetMethodID(godot_class, "hasClipboard", "()Z");
  67. _request_permission = p_env->GetMethodID(godot_class, "requestPermission", "(Ljava/lang/String;)Z");
  68. _request_permissions = p_env->GetMethodID(godot_class, "requestPermissions", "()Z");
  69. _get_granted_permissions = p_env->GetMethodID(godot_class, "getGrantedPermissions", "()[Ljava/lang/String;");
  70. _init_input_devices = p_env->GetMethodID(godot_class, "initInputDevices", "()V");
  71. _get_surface = p_env->GetMethodID(godot_class, "getSurface", "()Landroid/view/Surface;");
  72. _is_activity_resumed = p_env->GetMethodID(godot_class, "isActivityResumed", "()Z");
  73. _vibrate = p_env->GetMethodID(godot_class, "vibrate", "(I)V");
  74. _get_input_fallback_mapping = p_env->GetMethodID(godot_class, "getInputFallbackMapping", "()Ljava/lang/String;");
  75. _on_godot_setup_completed = p_env->GetMethodID(godot_class, "onGodotSetupCompleted", "()V");
  76. _on_godot_main_loop_started = p_env->GetMethodID(godot_class, "onGodotMainLoopStarted", "()V");
  77. _create_new_godot_instance = p_env->GetMethodID(godot_class, "createNewGodotInstance", "([Ljava/lang/String;)I");
  78. _get_render_view = p_env->GetMethodID(godot_class, "getRenderView", "()Lorg/godotengine/godot/GodotView;");
  79. _begin_benchmark_measure = p_env->GetMethodID(godot_class, "beginBenchmarkMeasure", "(Ljava/lang/String;)V");
  80. _end_benchmark_measure = p_env->GetMethodID(godot_class, "endBenchmarkMeasure", "(Ljava/lang/String;)V");
  81. _dump_benchmark = p_env->GetMethodID(godot_class, "dumpBenchmark", "(Ljava/lang/String;)V");
  82. // get some Activity method pointers...
  83. _get_class_loader = p_env->GetMethodID(activity_class, "getClassLoader", "()Ljava/lang/ClassLoader;");
  84. }
  85. GodotJavaWrapper::~GodotJavaWrapper() {
  86. if (godot_view) {
  87. delete godot_view;
  88. }
  89. JNIEnv *env = get_jni_env();
  90. ERR_FAIL_NULL(env);
  91. env->DeleteGlobalRef(godot_instance);
  92. env->DeleteGlobalRef(godot_class);
  93. env->DeleteGlobalRef(activity);
  94. env->DeleteGlobalRef(activity_class);
  95. }
  96. jobject GodotJavaWrapper::get_activity() {
  97. return activity;
  98. }
  99. jobject GodotJavaWrapper::get_member_object(const char *p_name, const char *p_class, JNIEnv *p_env) {
  100. if (godot_class) {
  101. if (p_env == nullptr) {
  102. p_env = get_jni_env();
  103. }
  104. ERR_FAIL_NULL_V(p_env, nullptr);
  105. jfieldID fid = p_env->GetStaticFieldID(godot_class, p_name, p_class);
  106. return p_env->GetStaticObjectField(godot_class, fid);
  107. } else {
  108. return nullptr;
  109. }
  110. }
  111. jobject GodotJavaWrapper::get_class_loader() {
  112. if (_get_class_loader) {
  113. JNIEnv *env = get_jni_env();
  114. ERR_FAIL_NULL_V(env, nullptr);
  115. return env->CallObjectMethod(activity, _get_class_loader);
  116. } else {
  117. return nullptr;
  118. }
  119. }
  120. GodotJavaViewWrapper *GodotJavaWrapper::get_godot_view() {
  121. if (godot_view != nullptr) {
  122. return godot_view;
  123. }
  124. if (_get_render_view) {
  125. JNIEnv *env = get_jni_env();
  126. ERR_FAIL_NULL_V(env, nullptr);
  127. jobject godot_render_view = env->CallObjectMethod(godot_instance, _get_render_view);
  128. if (!env->IsSameObject(godot_render_view, nullptr)) {
  129. godot_view = new GodotJavaViewWrapper(godot_render_view);
  130. }
  131. }
  132. return godot_view;
  133. }
  134. void GodotJavaWrapper::on_video_init(JNIEnv *p_env) {
  135. if (_on_video_init) {
  136. if (p_env == nullptr) {
  137. p_env = get_jni_env();
  138. }
  139. ERR_FAIL_NULL(p_env);
  140. p_env->CallVoidMethod(godot_instance, _on_video_init);
  141. }
  142. }
  143. bool GodotJavaWrapper::create_offscreen_gl(JNIEnv *p_env) {
  144. if (_create_offscreen_gl) {
  145. return p_env->CallBooleanMethod(godot_instance, _create_offscreen_gl);
  146. } else {
  147. return false;
  148. }
  149. }
  150. void GodotJavaWrapper::destroy_offscreen_gl(JNIEnv *p_env) {
  151. if (_destroy_offscreen_gl) {
  152. p_env->CallVoidMethod(godot_instance, _destroy_offscreen_gl);
  153. }
  154. }
  155. void GodotJavaWrapper::set_offscreen_gl_current(JNIEnv *p_env, bool p_current) {
  156. if (_set_offscreen_gl_current) {
  157. if (p_env == nullptr) {
  158. p_env = get_jni_env();
  159. }
  160. ERR_FAIL_NULL(p_env);
  161. p_env->CallVoidMethod(godot_instance, _set_offscreen_gl_current, p_current);
  162. }
  163. }
  164. void GodotJavaWrapper::on_godot_setup_completed(JNIEnv *p_env) {
  165. if (_on_godot_setup_completed) {
  166. if (p_env == nullptr) {
  167. p_env = get_jni_env();
  168. }
  169. ERR_FAIL_NULL(p_env);
  170. p_env->CallVoidMethod(godot_instance, _on_godot_setup_completed);
  171. }
  172. }
  173. void GodotJavaWrapper::on_godot_main_loop_started(JNIEnv *p_env) {
  174. if (_on_godot_main_loop_started) {
  175. if (p_env == nullptr) {
  176. p_env = get_jni_env();
  177. }
  178. ERR_FAIL_NULL(p_env);
  179. p_env->CallVoidMethod(godot_instance, _on_godot_main_loop_started);
  180. }
  181. }
  182. void GodotJavaWrapper::restart(JNIEnv *p_env) {
  183. if (_restart) {
  184. if (p_env == nullptr) {
  185. p_env = get_jni_env();
  186. }
  187. ERR_FAIL_NULL(p_env);
  188. p_env->CallVoidMethod(godot_instance, _restart);
  189. }
  190. }
  191. bool GodotJavaWrapper::force_quit(JNIEnv *p_env, int p_instance_id) {
  192. if (_finish) {
  193. if (p_env == nullptr) {
  194. p_env = get_jni_env();
  195. }
  196. ERR_FAIL_NULL_V(p_env, false);
  197. return p_env->CallBooleanMethod(godot_instance, _finish, p_instance_id);
  198. }
  199. return false;
  200. }
  201. void GodotJavaWrapper::set_keep_screen_on(bool p_enabled) {
  202. if (_set_keep_screen_on) {
  203. JNIEnv *env = get_jni_env();
  204. ERR_FAIL_NULL(env);
  205. env->CallVoidMethod(godot_instance, _set_keep_screen_on, p_enabled);
  206. }
  207. }
  208. void GodotJavaWrapper::alert(const String &p_message, const String &p_title) {
  209. if (_alert) {
  210. JNIEnv *env = get_jni_env();
  211. ERR_FAIL_NULL(env);
  212. jstring jStrMessage = env->NewStringUTF(p_message.utf8().get_data());
  213. jstring jStrTitle = env->NewStringUTF(p_title.utf8().get_data());
  214. env->CallVoidMethod(godot_instance, _alert, jStrMessage, jStrTitle);
  215. }
  216. }
  217. int GodotJavaWrapper::get_gles_version_code() {
  218. JNIEnv *env = get_jni_env();
  219. ERR_FAIL_NULL_V(env, 0);
  220. if (_get_GLES_version_code) {
  221. return env->CallIntMethod(godot_instance, _get_GLES_version_code);
  222. }
  223. return 0;
  224. }
  225. bool GodotJavaWrapper::has_get_clipboard() {
  226. return _get_clipboard != nullptr;
  227. }
  228. String GodotJavaWrapper::get_clipboard() {
  229. if (_get_clipboard) {
  230. JNIEnv *env = get_jni_env();
  231. ERR_FAIL_NULL_V(env, String());
  232. jstring s = (jstring)env->CallObjectMethod(godot_instance, _get_clipboard);
  233. return jstring_to_string(s, env);
  234. } else {
  235. return String();
  236. }
  237. }
  238. String GodotJavaWrapper::get_input_fallback_mapping() {
  239. if (_get_input_fallback_mapping) {
  240. JNIEnv *env = get_jni_env();
  241. ERR_FAIL_NULL_V(env, String());
  242. jstring fallback_mapping = (jstring)env->CallObjectMethod(godot_instance, _get_input_fallback_mapping);
  243. return jstring_to_string(fallback_mapping, env);
  244. } else {
  245. return String();
  246. }
  247. }
  248. bool GodotJavaWrapper::has_set_clipboard() {
  249. return _set_clipboard != nullptr;
  250. }
  251. void GodotJavaWrapper::set_clipboard(const String &p_text) {
  252. if (_set_clipboard) {
  253. JNIEnv *env = get_jni_env();
  254. ERR_FAIL_NULL(env);
  255. jstring jStr = env->NewStringUTF(p_text.utf8().get_data());
  256. env->CallVoidMethod(godot_instance, _set_clipboard, jStr);
  257. }
  258. }
  259. bool GodotJavaWrapper::has_has_clipboard() {
  260. return _has_clipboard != nullptr;
  261. }
  262. bool GodotJavaWrapper::has_clipboard() {
  263. if (_has_clipboard) {
  264. JNIEnv *env = get_jni_env();
  265. ERR_FAIL_NULL_V(env, false);
  266. return env->CallBooleanMethod(godot_instance, _has_clipboard);
  267. } else {
  268. return false;
  269. }
  270. }
  271. bool GodotJavaWrapper::request_permission(const String &p_name) {
  272. if (_request_permission) {
  273. JNIEnv *env = get_jni_env();
  274. ERR_FAIL_NULL_V(env, false);
  275. jstring jStrName = env->NewStringUTF(p_name.utf8().get_data());
  276. return env->CallBooleanMethod(godot_instance, _request_permission, jStrName);
  277. } else {
  278. return false;
  279. }
  280. }
  281. bool GodotJavaWrapper::request_permissions() {
  282. if (_request_permissions) {
  283. JNIEnv *env = get_jni_env();
  284. ERR_FAIL_NULL_V(env, false);
  285. return env->CallBooleanMethod(godot_instance, _request_permissions);
  286. } else {
  287. return false;
  288. }
  289. }
  290. Vector<String> GodotJavaWrapper::get_granted_permissions() const {
  291. Vector<String> permissions_list;
  292. if (_get_granted_permissions) {
  293. JNIEnv *env = get_jni_env();
  294. ERR_FAIL_NULL_V(env, permissions_list);
  295. jobject permissions_object = env->CallObjectMethod(godot_instance, _get_granted_permissions);
  296. jobjectArray *arr = reinterpret_cast<jobjectArray *>(&permissions_object);
  297. jsize len = env->GetArrayLength(*arr);
  298. for (int i = 0; i < len; i++) {
  299. jstring jstr = (jstring)env->GetObjectArrayElement(*arr, i);
  300. String str = jstring_to_string(jstr, env);
  301. permissions_list.push_back(str);
  302. env->DeleteLocalRef(jstr);
  303. }
  304. }
  305. return permissions_list;
  306. }
  307. void GodotJavaWrapper::init_input_devices() {
  308. if (_init_input_devices) {
  309. JNIEnv *env = get_jni_env();
  310. ERR_FAIL_NULL(env);
  311. env->CallVoidMethod(godot_instance, _init_input_devices);
  312. }
  313. }
  314. jobject GodotJavaWrapper::get_surface() {
  315. if (_get_surface) {
  316. JNIEnv *env = get_jni_env();
  317. ERR_FAIL_NULL_V(env, nullptr);
  318. return env->CallObjectMethod(godot_instance, _get_surface);
  319. } else {
  320. return nullptr;
  321. }
  322. }
  323. bool GodotJavaWrapper::is_activity_resumed() {
  324. if (_is_activity_resumed) {
  325. JNIEnv *env = get_jni_env();
  326. ERR_FAIL_NULL_V(env, false);
  327. return env->CallBooleanMethod(godot_instance, _is_activity_resumed);
  328. } else {
  329. return false;
  330. }
  331. }
  332. void GodotJavaWrapper::vibrate(int p_duration_ms) {
  333. if (_vibrate) {
  334. JNIEnv *env = get_jni_env();
  335. ERR_FAIL_NULL(env);
  336. env->CallVoidMethod(godot_instance, _vibrate, p_duration_ms);
  337. }
  338. }
  339. int GodotJavaWrapper::create_new_godot_instance(List<String> args) {
  340. if (_create_new_godot_instance) {
  341. JNIEnv *env = get_jni_env();
  342. ERR_FAIL_NULL_V(env, 0);
  343. jobjectArray jargs = env->NewObjectArray(args.size(), env->FindClass("java/lang/String"), env->NewStringUTF(""));
  344. for (int i = 0; i < args.size(); i++) {
  345. env->SetObjectArrayElement(jargs, i, env->NewStringUTF(args[i].utf8().get_data()));
  346. }
  347. return env->CallIntMethod(godot_instance, _create_new_godot_instance, jargs);
  348. } else {
  349. return 0;
  350. }
  351. }
  352. void GodotJavaWrapper::begin_benchmark_measure(const String &p_label) {
  353. if (_begin_benchmark_measure) {
  354. JNIEnv *env = get_jni_env();
  355. ERR_FAIL_NULL(env);
  356. jstring j_label = env->NewStringUTF(p_label.utf8().get_data());
  357. env->CallVoidMethod(godot_instance, _begin_benchmark_measure, j_label);
  358. }
  359. }
  360. void GodotJavaWrapper::end_benchmark_measure(const String &p_label) {
  361. if (_end_benchmark_measure) {
  362. JNIEnv *env = get_jni_env();
  363. ERR_FAIL_NULL(env);
  364. jstring j_label = env->NewStringUTF(p_label.utf8().get_data());
  365. env->CallVoidMethod(godot_instance, _end_benchmark_measure, j_label);
  366. }
  367. }
  368. void GodotJavaWrapper::dump_benchmark(const String &benchmark_file) {
  369. if (_dump_benchmark) {
  370. JNIEnv *env = get_jni_env();
  371. ERR_FAIL_NULL(env);
  372. jstring j_benchmark_file = env->NewStringUTF(benchmark_file.utf8().get_data());
  373. env->CallVoidMethod(godot_instance, _dump_benchmark, j_benchmark_file);
  374. }
  375. }