java_godot_wrapper.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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-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_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. _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. _request_permission = p_env->GetMethodID(godot_class, "requestPermission", "(Ljava/lang/String;)Z");
  64. _request_permissions = p_env->GetMethodID(godot_class, "requestPermissions", "()Z");
  65. _get_granted_permissions = p_env->GetMethodID(godot_class, "getGrantedPermissions", "()[Ljava/lang/String;");
  66. _init_input_devices = p_env->GetMethodID(godot_class, "initInputDevices", "()V");
  67. _get_surface = p_env->GetMethodID(godot_class, "getSurface", "()Landroid/view/Surface;");
  68. _is_activity_resumed = p_env->GetMethodID(godot_class, "isActivityResumed", "()Z");
  69. _vibrate = p_env->GetMethodID(godot_class, "vibrate", "(I)V");
  70. _get_input_fallback_mapping = p_env->GetMethodID(godot_class, "getInputFallbackMapping", "()Ljava/lang/String;");
  71. _on_godot_main_loop_started = p_env->GetMethodID(godot_class, "onGodotMainLoopStarted", "()V");
  72. // get some Activity method pointers...
  73. _get_class_loader = p_env->GetMethodID(activity_class, "getClassLoader", "()Ljava/lang/ClassLoader;");
  74. }
  75. GodotJavaWrapper::~GodotJavaWrapper() {
  76. // nothing to do here for now
  77. }
  78. jobject GodotJavaWrapper::get_activity() {
  79. return activity;
  80. }
  81. jobject GodotJavaWrapper::get_member_object(const char *p_name, const char *p_class, JNIEnv *p_env) {
  82. if (godot_class) {
  83. if (p_env == nullptr)
  84. p_env = ThreadAndroid::get_env();
  85. jfieldID fid = p_env->GetStaticFieldID(godot_class, p_name, p_class);
  86. return p_env->GetStaticObjectField(godot_class, fid);
  87. } else {
  88. return nullptr;
  89. }
  90. }
  91. jobject GodotJavaWrapper::get_class_loader() {
  92. if (_get_class_loader) {
  93. JNIEnv *env = ThreadAndroid::get_env();
  94. return env->CallObjectMethod(activity, _get_class_loader);
  95. } else {
  96. return nullptr;
  97. }
  98. }
  99. GodotJavaViewWrapper *GodotJavaWrapper::get_godot_view() {
  100. if (_godot_view != nullptr) {
  101. return _godot_view;
  102. }
  103. JNIEnv *env = ThreadAndroid::get_env();
  104. jmethodID godot_view_getter = env->GetMethodID(godot_class, "getRenderView", "()Lorg/godotengine/godot/GodotRenderView;");
  105. _godot_view = new GodotJavaViewWrapper(env->CallObjectMethod(godot_instance, godot_view_getter));
  106. return _godot_view;
  107. }
  108. void GodotJavaWrapper::on_video_init(JNIEnv *p_env) {
  109. if (_on_video_init)
  110. if (p_env == nullptr)
  111. p_env = ThreadAndroid::get_env();
  112. p_env->CallVoidMethod(godot_instance, _on_video_init);
  113. }
  114. void GodotJavaWrapper::on_godot_main_loop_started(JNIEnv *p_env) {
  115. if (_on_godot_main_loop_started) {
  116. if (p_env == nullptr) {
  117. p_env = ThreadAndroid::get_env();
  118. }
  119. }
  120. p_env->CallVoidMethod(godot_instance, _on_godot_main_loop_started);
  121. }
  122. void GodotJavaWrapper::restart(JNIEnv *p_env) {
  123. if (_restart)
  124. if (p_env == nullptr)
  125. p_env = ThreadAndroid::get_env();
  126. p_env->CallVoidMethod(godot_instance, _restart);
  127. }
  128. void GodotJavaWrapper::force_quit(JNIEnv *p_env) {
  129. if (_finish)
  130. if (p_env == nullptr)
  131. p_env = ThreadAndroid::get_env();
  132. p_env->CallVoidMethod(godot_instance, _finish);
  133. }
  134. void GodotJavaWrapper::set_keep_screen_on(bool p_enabled) {
  135. if (_set_keep_screen_on) {
  136. JNIEnv *env = ThreadAndroid::get_env();
  137. env->CallVoidMethod(godot_instance, _set_keep_screen_on, p_enabled);
  138. }
  139. }
  140. void GodotJavaWrapper::alert(const String &p_message, const String &p_title) {
  141. if (_alert) {
  142. JNIEnv *env = ThreadAndroid::get_env();
  143. jstring jStrMessage = env->NewStringUTF(p_message.utf8().get_data());
  144. jstring jStrTitle = env->NewStringUTF(p_title.utf8().get_data());
  145. env->CallVoidMethod(godot_instance, _alert, jStrMessage, jStrTitle);
  146. }
  147. }
  148. int GodotJavaWrapper::get_gles_version_code() {
  149. JNIEnv *env = ThreadAndroid::get_env();
  150. if (_get_GLES_version_code) {
  151. return env->CallIntMethod(godot_instance, _get_GLES_version_code);
  152. }
  153. return 0;
  154. }
  155. bool GodotJavaWrapper::has_get_clipboard() {
  156. return _get_clipboard != 0;
  157. }
  158. String GodotJavaWrapper::get_clipboard() {
  159. if (_get_clipboard) {
  160. JNIEnv *env = ThreadAndroid::get_env();
  161. jstring s = (jstring)env->CallObjectMethod(godot_instance, _get_clipboard);
  162. return jstring_to_string(s, env);
  163. } else {
  164. return String();
  165. }
  166. }
  167. String GodotJavaWrapper::get_input_fallback_mapping() {
  168. if (_get_input_fallback_mapping) {
  169. JNIEnv *env = ThreadAndroid::get_env();
  170. jstring fallback_mapping = (jstring)env->CallObjectMethod(godot_instance, _get_input_fallback_mapping);
  171. return jstring_to_string(fallback_mapping, env);
  172. } else {
  173. return String();
  174. }
  175. }
  176. bool GodotJavaWrapper::has_set_clipboard() {
  177. return _set_clipboard != 0;
  178. }
  179. void GodotJavaWrapper::set_clipboard(const String &p_text) {
  180. if (_set_clipboard) {
  181. JNIEnv *env = ThreadAndroid::get_env();
  182. jstring jStr = env->NewStringUTF(p_text.utf8().get_data());
  183. env->CallVoidMethod(godot_instance, _set_clipboard, jStr);
  184. }
  185. }
  186. bool GodotJavaWrapper::request_permission(const String &p_name) {
  187. if (_request_permission) {
  188. JNIEnv *env = ThreadAndroid::get_env();
  189. jstring jStrName = env->NewStringUTF(p_name.utf8().get_data());
  190. return env->CallBooleanMethod(godot_instance, _request_permission, jStrName);
  191. } else {
  192. return false;
  193. }
  194. }
  195. bool GodotJavaWrapper::request_permissions() {
  196. if (_request_permissions) {
  197. JNIEnv *env = ThreadAndroid::get_env();
  198. return env->CallBooleanMethod(godot_instance, _request_permissions);
  199. } else {
  200. return false;
  201. }
  202. }
  203. Vector<String> GodotJavaWrapper::get_granted_permissions() const {
  204. Vector<String> permissions_list;
  205. if (_get_granted_permissions) {
  206. JNIEnv *env = ThreadAndroid::get_env();
  207. jobject permissions_object = env->CallObjectMethod(godot_instance, _get_granted_permissions);
  208. jobjectArray *arr = reinterpret_cast<jobjectArray *>(&permissions_object);
  209. int i = 0;
  210. jsize len = env->GetArrayLength(*arr);
  211. for (i = 0; i < len; i++) {
  212. jstring jstr = (jstring)env->GetObjectArrayElement(*arr, i);
  213. String str = jstring_to_string(jstr, env);
  214. permissions_list.push_back(str);
  215. env->DeleteLocalRef(jstr);
  216. }
  217. }
  218. return permissions_list;
  219. }
  220. void GodotJavaWrapper::init_input_devices() {
  221. if (_init_input_devices) {
  222. JNIEnv *env = ThreadAndroid::get_env();
  223. env->CallVoidMethod(godot_instance, _init_input_devices);
  224. }
  225. }
  226. jobject GodotJavaWrapper::get_surface() {
  227. if (_get_surface) {
  228. JNIEnv *env = ThreadAndroid::get_env();
  229. return env->CallObjectMethod(godot_instance, _get_surface);
  230. } else {
  231. return nullptr;
  232. }
  233. }
  234. bool GodotJavaWrapper::is_activity_resumed() {
  235. if (_is_activity_resumed) {
  236. JNIEnv *env = ThreadAndroid::get_env();
  237. return env->CallBooleanMethod(godot_instance, _is_activity_resumed);
  238. } else {
  239. return false;
  240. }
  241. }
  242. void GodotJavaWrapper::vibrate(int p_duration_ms) {
  243. if (_vibrate) {
  244. JNIEnv *env = ThreadAndroid::get_env();
  245. env->CallVoidMethod(godot_instance, _vibrate, p_duration_ms);
  246. }
  247. }