java_godot_wrapper.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*************************************************************************/
  2. /* java_godot_wrapper.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_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", "()V");
  56. _restart = p_env->GetMethodID(godot_class, "restart", "()V");
  57. _finish = p_env->GetMethodID(godot_class, "forceQuit", "()V");
  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. _init_input_devices = p_env->GetMethodID(godot_class, "initInputDevices", "()V");
  68. _get_surface = p_env->GetMethodID(godot_class, "getSurface", "()Landroid/view/Surface;");
  69. _is_activity_resumed = p_env->GetMethodID(godot_class, "isActivityResumed", "()Z");
  70. _vibrate = p_env->GetMethodID(godot_class, "vibrate", "(I)V");
  71. _get_input_fallback_mapping = p_env->GetMethodID(godot_class, "getInputFallbackMapping", "()Ljava/lang/String;");
  72. _on_godot_setup_completed = p_env->GetMethodID(godot_class, "onGodotSetupCompleted", "()V");
  73. _on_godot_main_loop_started = p_env->GetMethodID(godot_class, "onGodotMainLoopStarted", "()V");
  74. // get some Activity method pointers...
  75. _get_class_loader = p_env->GetMethodID(activity_class, "getClassLoader", "()Ljava/lang/ClassLoader;");
  76. }
  77. GodotJavaWrapper::~GodotJavaWrapper() {
  78. // nothing to do here for now
  79. }
  80. jobject GodotJavaWrapper::get_activity() {
  81. return activity;
  82. }
  83. jobject GodotJavaWrapper::get_member_object(const char *p_name, const char *p_class, JNIEnv *p_env) {
  84. if (godot_class) {
  85. if (p_env == nullptr)
  86. p_env = get_jni_env();
  87. ERR_FAIL_COND_V(p_env == nullptr, nullptr);
  88. jfieldID fid = p_env->GetStaticFieldID(godot_class, p_name, p_class);
  89. return p_env->GetStaticObjectField(godot_class, fid);
  90. } else {
  91. return nullptr;
  92. }
  93. }
  94. jobject GodotJavaWrapper::get_class_loader() {
  95. if (_get_class_loader) {
  96. JNIEnv *env = get_jni_env();
  97. ERR_FAIL_COND_V(env == nullptr, nullptr);
  98. return env->CallObjectMethod(activity, _get_class_loader);
  99. } else {
  100. return nullptr;
  101. }
  102. }
  103. GodotJavaViewWrapper *GodotJavaWrapper::get_godot_view() {
  104. if (_godot_view != nullptr) {
  105. return _godot_view;
  106. }
  107. JNIEnv *env = get_jni_env();
  108. ERR_FAIL_COND_V(env == nullptr, nullptr);
  109. jmethodID godot_view_getter = env->GetMethodID(godot_class, "getRenderView", "()Lorg/godotengine/godot/GodotRenderView;");
  110. _godot_view = new GodotJavaViewWrapper(env->CallObjectMethod(godot_instance, godot_view_getter));
  111. return _godot_view;
  112. }
  113. void GodotJavaWrapper::on_video_init(JNIEnv *p_env) {
  114. if (_on_video_init) {
  115. if (p_env == nullptr)
  116. p_env = get_jni_env();
  117. ERR_FAIL_COND(p_env == nullptr);
  118. p_env->CallVoidMethod(godot_instance, _on_video_init);
  119. }
  120. }
  121. void GodotJavaWrapper::on_godot_setup_completed(JNIEnv *p_env) {
  122. if (_on_godot_setup_completed) {
  123. if (p_env == nullptr) {
  124. p_env = get_jni_env();
  125. }
  126. p_env->CallVoidMethod(godot_instance, _on_godot_setup_completed);
  127. }
  128. }
  129. void GodotJavaWrapper::on_godot_main_loop_started(JNIEnv *p_env) {
  130. if (_on_godot_main_loop_started) {
  131. if (p_env == nullptr) {
  132. p_env = get_jni_env();
  133. }
  134. ERR_FAIL_COND(p_env == nullptr);
  135. p_env->CallVoidMethod(godot_instance, _on_godot_main_loop_started);
  136. }
  137. }
  138. void GodotJavaWrapper::restart(JNIEnv *p_env) {
  139. if (_restart) {
  140. if (p_env == nullptr)
  141. p_env = get_jni_env();
  142. ERR_FAIL_COND(p_env == nullptr);
  143. p_env->CallVoidMethod(godot_instance, _restart);
  144. }
  145. }
  146. void GodotJavaWrapper::force_quit(JNIEnv *p_env) {
  147. if (_finish) {
  148. if (p_env == nullptr)
  149. p_env = get_jni_env();
  150. ERR_FAIL_COND(p_env == nullptr);
  151. p_env->CallVoidMethod(godot_instance, _finish);
  152. }
  153. }
  154. void GodotJavaWrapper::set_keep_screen_on(bool p_enabled) {
  155. if (_set_keep_screen_on) {
  156. JNIEnv *env = get_jni_env();
  157. ERR_FAIL_COND(env == nullptr);
  158. env->CallVoidMethod(godot_instance, _set_keep_screen_on, p_enabled);
  159. }
  160. }
  161. void GodotJavaWrapper::alert(const String &p_message, const String &p_title) {
  162. if (_alert) {
  163. JNIEnv *env = get_jni_env();
  164. ERR_FAIL_COND(env == nullptr);
  165. jstring jStrMessage = env->NewStringUTF(p_message.utf8().get_data());
  166. jstring jStrTitle = env->NewStringUTF(p_title.utf8().get_data());
  167. env->CallVoidMethod(godot_instance, _alert, jStrMessage, jStrTitle);
  168. }
  169. }
  170. int GodotJavaWrapper::get_gles_version_code() {
  171. JNIEnv *env = get_jni_env();
  172. ERR_FAIL_COND_V(env == nullptr, 0);
  173. if (_get_GLES_version_code) {
  174. return env->CallIntMethod(godot_instance, _get_GLES_version_code);
  175. }
  176. return 0;
  177. }
  178. bool GodotJavaWrapper::has_get_clipboard() {
  179. return _get_clipboard != 0;
  180. }
  181. String GodotJavaWrapper::get_clipboard() {
  182. if (_get_clipboard) {
  183. JNIEnv *env = get_jni_env();
  184. ERR_FAIL_COND_V(env == nullptr, String());
  185. jstring s = (jstring)env->CallObjectMethod(godot_instance, _get_clipboard);
  186. return jstring_to_string(s, env);
  187. } else {
  188. return String();
  189. }
  190. }
  191. String GodotJavaWrapper::get_input_fallback_mapping() {
  192. if (_get_input_fallback_mapping) {
  193. JNIEnv *env = get_jni_env();
  194. ERR_FAIL_COND_V(env == nullptr, String());
  195. jstring fallback_mapping = (jstring)env->CallObjectMethod(godot_instance, _get_input_fallback_mapping);
  196. return jstring_to_string(fallback_mapping, env);
  197. } else {
  198. return String();
  199. }
  200. }
  201. bool GodotJavaWrapper::has_set_clipboard() {
  202. return _set_clipboard != 0;
  203. }
  204. void GodotJavaWrapper::set_clipboard(const String &p_text) {
  205. if (_set_clipboard) {
  206. JNIEnv *env = get_jni_env();
  207. ERR_FAIL_COND(env == nullptr);
  208. jstring jStr = env->NewStringUTF(p_text.utf8().get_data());
  209. env->CallVoidMethod(godot_instance, _set_clipboard, jStr);
  210. }
  211. }
  212. bool GodotJavaWrapper::has_has_clipboard() {
  213. return _has_clipboard != 0;
  214. }
  215. bool GodotJavaWrapper::has_clipboard() {
  216. if (_has_clipboard) {
  217. JNIEnv *env = get_jni_env();
  218. ERR_FAIL_COND_V(env == nullptr, false);
  219. return env->CallBooleanMethod(godot_instance, _has_clipboard);
  220. } else {
  221. return false;
  222. }
  223. }
  224. bool GodotJavaWrapper::request_permission(const String &p_name) {
  225. if (_request_permission) {
  226. JNIEnv *env = get_jni_env();
  227. ERR_FAIL_COND_V(env == nullptr, false);
  228. jstring jStrName = env->NewStringUTF(p_name.utf8().get_data());
  229. return env->CallBooleanMethod(godot_instance, _request_permission, jStrName);
  230. } else {
  231. return false;
  232. }
  233. }
  234. bool GodotJavaWrapper::request_permissions() {
  235. if (_request_permissions) {
  236. JNIEnv *env = get_jni_env();
  237. ERR_FAIL_COND_V(env == nullptr, false);
  238. return env->CallBooleanMethod(godot_instance, _request_permissions);
  239. } else {
  240. return false;
  241. }
  242. }
  243. Vector<String> GodotJavaWrapper::get_granted_permissions() const {
  244. Vector<String> permissions_list;
  245. if (_get_granted_permissions) {
  246. JNIEnv *env = get_jni_env();
  247. ERR_FAIL_COND_V(env == nullptr, permissions_list);
  248. jobject permissions_object = env->CallObjectMethod(godot_instance, _get_granted_permissions);
  249. jobjectArray *arr = reinterpret_cast<jobjectArray *>(&permissions_object);
  250. int i = 0;
  251. jsize len = env->GetArrayLength(*arr);
  252. for (i = 0; i < len; i++) {
  253. jstring jstr = (jstring)env->GetObjectArrayElement(*arr, i);
  254. String str = jstring_to_string(jstr, env);
  255. permissions_list.push_back(str);
  256. env->DeleteLocalRef(jstr);
  257. }
  258. }
  259. return permissions_list;
  260. }
  261. void GodotJavaWrapper::init_input_devices() {
  262. if (_init_input_devices) {
  263. JNIEnv *env = get_jni_env();
  264. ERR_FAIL_COND(env == nullptr);
  265. env->CallVoidMethod(godot_instance, _init_input_devices);
  266. }
  267. }
  268. jobject GodotJavaWrapper::get_surface() {
  269. if (_get_surface) {
  270. JNIEnv *env = get_jni_env();
  271. ERR_FAIL_COND_V(env == nullptr, nullptr);
  272. return env->CallObjectMethod(godot_instance, _get_surface);
  273. } else {
  274. return nullptr;
  275. }
  276. }
  277. bool GodotJavaWrapper::is_activity_resumed() {
  278. if (_is_activity_resumed) {
  279. JNIEnv *env = get_jni_env();
  280. ERR_FAIL_COND_V(env == nullptr, false);
  281. return env->CallBooleanMethod(godot_instance, _is_activity_resumed);
  282. } else {
  283. return false;
  284. }
  285. }
  286. void GodotJavaWrapper::vibrate(int p_duration_ms) {
  287. if (_vibrate) {
  288. JNIEnv *env = get_jni_env();
  289. ERR_FAIL_COND(env == nullptr);
  290. env->CallVoidMethod(godot_instance, _vibrate, p_duration_ms);
  291. }
  292. }