java_godot_wrapper.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 get_jni_env(). 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", "()Z");
  56. _restart = p_env->GetMethodID(godot_class, "restart", "()V");
  57. _finish = p_env->GetMethodID(godot_class, "forceQuit", "(I)Z");
  58. _set_keep_screen_on = p_env->GetMethodID(godot_class, "setKeepScreenOn", "(Z)V");
  59. _alert = p_env->GetMethodID(godot_class, "alert", "(Ljava/lang/String;Ljava/lang/String;)V");
  60. _get_GLES_version_code = p_env->GetMethodID(godot_class, "getGLESVersionCode", "()I");
  61. _get_clipboard = p_env->GetMethodID(godot_class, "getClipboard", "()Ljava/lang/String;");
  62. _set_clipboard = p_env->GetMethodID(godot_class, "setClipboard", "(Ljava/lang/String;)V");
  63. _has_clipboard = p_env->GetMethodID(godot_class, "hasClipboard", "()Z");
  64. _request_permission = p_env->GetMethodID(godot_class, "requestPermission", "(Ljava/lang/String;)Z");
  65. _request_permissions = p_env->GetMethodID(godot_class, "requestPermissions", "()Z");
  66. _get_granted_permissions = p_env->GetMethodID(godot_class, "getGrantedPermissions", "()[Ljava/lang/String;");
  67. _get_ca_certificates = p_env->GetMethodID(godot_class, "getCACertificates", "()Ljava/lang/String;");
  68. _init_input_devices = p_env->GetMethodID(godot_class, "initInputDevices", "()V");
  69. _get_surface = p_env->GetMethodID(godot_class, "getSurface", "()Landroid/view/Surface;");
  70. _is_activity_resumed = p_env->GetMethodID(godot_class, "isActivityResumed", "()Z");
  71. _vibrate = p_env->GetMethodID(godot_class, "vibrate", "(I)V");
  72. _get_input_fallback_mapping = p_env->GetMethodID(godot_class, "getInputFallbackMapping", "()Ljava/lang/String;");
  73. _on_godot_setup_completed = p_env->GetMethodID(godot_class, "onGodotSetupCompleted", "()V");
  74. _on_godot_main_loop_started = p_env->GetMethodID(godot_class, "onGodotMainLoopStarted", "()V");
  75. _create_new_godot_instance = p_env->GetMethodID(godot_class, "createNewGodotInstance", "([Ljava/lang/String;)I");
  76. _get_render_view = p_env->GetMethodID(godot_class, "getRenderView", "()Lorg/godotengine/godot/GodotRenderView;");
  77. // get some Activity method pointers...
  78. _get_class_loader = p_env->GetMethodID(activity_class, "getClassLoader", "()Ljava/lang/ClassLoader;");
  79. }
  80. GodotJavaWrapper::~GodotJavaWrapper() {
  81. if (godot_view) {
  82. delete godot_view;
  83. }
  84. JNIEnv *env = get_jni_env();
  85. ERR_FAIL_NULL(env);
  86. env->DeleteGlobalRef(godot_instance);
  87. env->DeleteGlobalRef(godot_class);
  88. env->DeleteGlobalRef(activity);
  89. env->DeleteGlobalRef(activity_class);
  90. }
  91. jobject GodotJavaWrapper::get_activity() {
  92. return activity;
  93. }
  94. jobject GodotJavaWrapper::get_member_object(const char *p_name, const char *p_class, JNIEnv *p_env) {
  95. if (godot_class) {
  96. if (p_env == nullptr) {
  97. p_env = get_jni_env();
  98. }
  99. ERR_FAIL_NULL_V(p_env, nullptr);
  100. jfieldID fid = p_env->GetStaticFieldID(godot_class, p_name, p_class);
  101. return p_env->GetStaticObjectField(godot_class, fid);
  102. } else {
  103. return nullptr;
  104. }
  105. }
  106. jobject GodotJavaWrapper::get_class_loader() {
  107. if (_get_class_loader) {
  108. JNIEnv *env = get_jni_env();
  109. ERR_FAIL_NULL_V(env, nullptr);
  110. return env->CallObjectMethod(activity, _get_class_loader);
  111. } else {
  112. return nullptr;
  113. }
  114. }
  115. GodotJavaViewWrapper *GodotJavaWrapper::get_godot_view() {
  116. if (godot_view != nullptr) {
  117. return godot_view;
  118. }
  119. if (_get_render_view) {
  120. JNIEnv *env = get_jni_env();
  121. ERR_FAIL_NULL_V(env, nullptr);
  122. jobject godot_render_view = env->CallObjectMethod(godot_instance, _get_render_view);
  123. if (!env->IsSameObject(godot_render_view, nullptr)) {
  124. godot_view = new GodotJavaViewWrapper(godot_render_view);
  125. }
  126. }
  127. return godot_view;
  128. }
  129. bool GodotJavaWrapper::on_video_init(JNIEnv *p_env) {
  130. if (_on_video_init) {
  131. if (p_env == nullptr) {
  132. p_env = get_jni_env();
  133. }
  134. ERR_FAIL_NULL_V(p_env, false);
  135. return p_env->CallBooleanMethod(godot_instance, _on_video_init);
  136. }
  137. return false;
  138. }
  139. void GodotJavaWrapper::on_godot_setup_completed(JNIEnv *p_env) {
  140. if (_on_godot_setup_completed) {
  141. if (p_env == nullptr) {
  142. p_env = get_jni_env();
  143. }
  144. p_env->CallVoidMethod(godot_instance, _on_godot_setup_completed);
  145. }
  146. }
  147. void GodotJavaWrapper::on_godot_main_loop_started(JNIEnv *p_env) {
  148. if (_on_godot_main_loop_started) {
  149. if (p_env == nullptr) {
  150. p_env = get_jni_env();
  151. }
  152. ERR_FAIL_NULL(p_env);
  153. p_env->CallVoidMethod(godot_instance, _on_godot_main_loop_started);
  154. }
  155. }
  156. void GodotJavaWrapper::restart(JNIEnv *p_env) {
  157. if (_restart) {
  158. if (p_env == nullptr) {
  159. p_env = get_jni_env();
  160. }
  161. ERR_FAIL_NULL(p_env);
  162. p_env->CallVoidMethod(godot_instance, _restart);
  163. }
  164. }
  165. bool GodotJavaWrapper::force_quit(JNIEnv *p_env, int p_instance_id) {
  166. if (_finish) {
  167. if (p_env == nullptr) {
  168. p_env = get_jni_env();
  169. }
  170. ERR_FAIL_NULL_V(p_env, false);
  171. return p_env->CallBooleanMethod(godot_instance, _finish, p_instance_id);
  172. }
  173. return false;
  174. }
  175. void GodotJavaWrapper::set_keep_screen_on(bool p_enabled) {
  176. if (_set_keep_screen_on) {
  177. JNIEnv *env = get_jni_env();
  178. ERR_FAIL_NULL(env);
  179. env->CallVoidMethod(godot_instance, _set_keep_screen_on, p_enabled);
  180. }
  181. }
  182. void GodotJavaWrapper::alert(const String &p_message, const String &p_title) {
  183. if (_alert) {
  184. JNIEnv *env = get_jni_env();
  185. ERR_FAIL_NULL(env);
  186. jstring jStrMessage = env->NewStringUTF(p_message.utf8().get_data());
  187. jstring jStrTitle = env->NewStringUTF(p_title.utf8().get_data());
  188. env->CallVoidMethod(godot_instance, _alert, jStrMessage, jStrTitle);
  189. }
  190. }
  191. int GodotJavaWrapper::get_gles_version_code() {
  192. JNIEnv *env = get_jni_env();
  193. ERR_FAIL_NULL_V(env, 0);
  194. if (_get_GLES_version_code) {
  195. return env->CallIntMethod(godot_instance, _get_GLES_version_code);
  196. }
  197. return 0;
  198. }
  199. bool GodotJavaWrapper::has_get_clipboard() {
  200. return _get_clipboard != nullptr;
  201. }
  202. String GodotJavaWrapper::get_clipboard() {
  203. if (_get_clipboard) {
  204. JNIEnv *env = get_jni_env();
  205. ERR_FAIL_NULL_V(env, String());
  206. jstring s = (jstring)env->CallObjectMethod(godot_instance, _get_clipboard);
  207. return jstring_to_string(s, env);
  208. } else {
  209. return String();
  210. }
  211. }
  212. String GodotJavaWrapper::get_input_fallback_mapping() {
  213. if (_get_input_fallback_mapping) {
  214. JNIEnv *env = get_jni_env();
  215. ERR_FAIL_NULL_V(env, String());
  216. jstring fallback_mapping = (jstring)env->CallObjectMethod(godot_instance, _get_input_fallback_mapping);
  217. return jstring_to_string(fallback_mapping, env);
  218. } else {
  219. return String();
  220. }
  221. }
  222. bool GodotJavaWrapper::has_set_clipboard() {
  223. return _set_clipboard != nullptr;
  224. }
  225. void GodotJavaWrapper::set_clipboard(const String &p_text) {
  226. if (_set_clipboard) {
  227. JNIEnv *env = get_jni_env();
  228. ERR_FAIL_NULL(env);
  229. jstring jStr = env->NewStringUTF(p_text.utf8().get_data());
  230. env->CallVoidMethod(godot_instance, _set_clipboard, jStr);
  231. }
  232. }
  233. bool GodotJavaWrapper::has_has_clipboard() {
  234. return _has_clipboard != nullptr;
  235. }
  236. bool GodotJavaWrapper::has_clipboard() {
  237. if (_has_clipboard) {
  238. JNIEnv *env = get_jni_env();
  239. ERR_FAIL_NULL_V(env, false);
  240. return env->CallBooleanMethod(godot_instance, _has_clipboard);
  241. } else {
  242. return false;
  243. }
  244. }
  245. bool GodotJavaWrapper::request_permission(const String &p_name) {
  246. if (_request_permission) {
  247. JNIEnv *env = get_jni_env();
  248. ERR_FAIL_NULL_V(env, false);
  249. jstring jStrName = env->NewStringUTF(p_name.utf8().get_data());
  250. return env->CallBooleanMethod(godot_instance, _request_permission, jStrName);
  251. } else {
  252. return false;
  253. }
  254. }
  255. bool GodotJavaWrapper::request_permissions() {
  256. if (_request_permissions) {
  257. JNIEnv *env = get_jni_env();
  258. ERR_FAIL_NULL_V(env, false);
  259. return env->CallBooleanMethod(godot_instance, _request_permissions);
  260. } else {
  261. return false;
  262. }
  263. }
  264. Vector<String> GodotJavaWrapper::get_granted_permissions() const {
  265. Vector<String> permissions_list;
  266. if (_get_granted_permissions) {
  267. JNIEnv *env = get_jni_env();
  268. ERR_FAIL_NULL_V(env, permissions_list);
  269. jobject permissions_object = env->CallObjectMethod(godot_instance, _get_granted_permissions);
  270. jobjectArray *arr = reinterpret_cast<jobjectArray *>(&permissions_object);
  271. jsize len = env->GetArrayLength(*arr);
  272. for (int i = 0; i < len; i++) {
  273. jstring jstr = (jstring)env->GetObjectArrayElement(*arr, i);
  274. String str = jstring_to_string(jstr, env);
  275. permissions_list.push_back(str);
  276. env->DeleteLocalRef(jstr);
  277. }
  278. }
  279. return permissions_list;
  280. }
  281. String GodotJavaWrapper::get_ca_certificates() const {
  282. if (_get_ca_certificates) {
  283. JNIEnv *env = get_jni_env();
  284. ERR_FAIL_NULL_V(env, String());
  285. jstring s = (jstring)env->CallObjectMethod(godot_instance, _get_ca_certificates);
  286. return jstring_to_string(s, env);
  287. } else {
  288. return String();
  289. }
  290. }
  291. void GodotJavaWrapper::init_input_devices() {
  292. if (_init_input_devices) {
  293. JNIEnv *env = get_jni_env();
  294. ERR_FAIL_NULL(env);
  295. env->CallVoidMethod(godot_instance, _init_input_devices);
  296. }
  297. }
  298. jobject GodotJavaWrapper::get_surface() {
  299. if (_get_surface) {
  300. JNIEnv *env = get_jni_env();
  301. ERR_FAIL_NULL_V(env, nullptr);
  302. return env->CallObjectMethod(godot_instance, _get_surface);
  303. } else {
  304. return nullptr;
  305. }
  306. }
  307. bool GodotJavaWrapper::is_activity_resumed() {
  308. if (_is_activity_resumed) {
  309. JNIEnv *env = get_jni_env();
  310. ERR_FAIL_NULL_V(env, false);
  311. return env->CallBooleanMethod(godot_instance, _is_activity_resumed);
  312. } else {
  313. return false;
  314. }
  315. }
  316. void GodotJavaWrapper::vibrate(int p_duration_ms) {
  317. if (_vibrate) {
  318. JNIEnv *env = get_jni_env();
  319. ERR_FAIL_NULL(env);
  320. env->CallVoidMethod(godot_instance, _vibrate, p_duration_ms);
  321. }
  322. }
  323. int GodotJavaWrapper::create_new_godot_instance(List<String> args) {
  324. if (_create_new_godot_instance) {
  325. JNIEnv *env = get_jni_env();
  326. ERR_FAIL_NULL_V(env, 0);
  327. jobjectArray jargs = env->NewObjectArray(args.size(), env->FindClass("java/lang/String"), env->NewStringUTF(""));
  328. for (int i = 0; i < args.size(); i++) {
  329. env->SetObjectArrayElement(jargs, i, env->NewStringUTF(args[i].utf8().get_data()));
  330. }
  331. return env->CallIntMethod(godot_instance, _create_new_godot_instance, jargs);
  332. } else {
  333. return 0;
  334. }
  335. }