java_godot_wrapper.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_godot_instance) {
  37. godot_instance = p_env->NewGlobalRef(p_godot_instance);
  38. // get info about our Godot class so we can get pointers and stuff...
  39. cls = p_env->FindClass("org/godotengine/godot/Godot");
  40. if (cls) {
  41. cls = (jclass)p_env->NewGlobalRef(cls);
  42. } else {
  43. // this is a pretty serious fail.. bail... pointers will stay 0
  44. return;
  45. }
  46. // get some method pointers...
  47. _on_video_init = p_env->GetMethodID(cls, "onVideoInit", "()V");
  48. _restart = p_env->GetMethodID(cls, "restart", "()V");
  49. _finish = p_env->GetMethodID(cls, "forceQuit", "()V");
  50. _set_keep_screen_on = p_env->GetMethodID(cls, "setKeepScreenOn", "(Z)V");
  51. _alert = p_env->GetMethodID(cls, "alert", "(Ljava/lang/String;Ljava/lang/String;)V");
  52. _get_GLES_version_code = p_env->GetMethodID(cls, "getGLESVersionCode", "()I");
  53. _get_clipboard = p_env->GetMethodID(cls, "getClipboard", "()Ljava/lang/String;");
  54. _set_clipboard = p_env->GetMethodID(cls, "setClipboard", "(Ljava/lang/String;)V");
  55. _request_permission = p_env->GetMethodID(cls, "requestPermission", "(Ljava/lang/String;)Z");
  56. _request_permissions = p_env->GetMethodID(cls, "requestPermissions", "()Z");
  57. _get_granted_permissions = p_env->GetMethodID(cls, "getGrantedPermissions", "()[Ljava/lang/String;");
  58. _init_input_devices = p_env->GetMethodID(cls, "initInputDevices", "()V");
  59. _get_surface = p_env->GetMethodID(cls, "getSurface", "()Landroid/view/Surface;");
  60. _is_activity_resumed = p_env->GetMethodID(cls, "isActivityResumed", "()Z");
  61. _vibrate = p_env->GetMethodID(cls, "vibrate", "(I)V");
  62. _get_input_fallback_mapping = p_env->GetMethodID(cls, "getInputFallbackMapping", "()Ljava/lang/String;");
  63. _on_gl_godot_main_loop_started = p_env->GetMethodID(cls, "onGLGodotMainLoopStarted", "()V");
  64. }
  65. GodotJavaWrapper::~GodotJavaWrapper() {
  66. // nothing to do here for now
  67. }
  68. jobject GodotJavaWrapper::get_activity() {
  69. // our godot instance is our activity
  70. return godot_instance;
  71. }
  72. jobject GodotJavaWrapper::get_member_object(const char *p_name, const char *p_class, JNIEnv *p_env) {
  73. if (cls) {
  74. if (p_env == NULL)
  75. p_env = ThreadAndroid::get_env();
  76. jfieldID fid = p_env->GetStaticFieldID(cls, p_name, p_class);
  77. return p_env->GetStaticObjectField(cls, fid);
  78. } else {
  79. return NULL;
  80. }
  81. }
  82. jobject GodotJavaWrapper::get_class_loader() {
  83. if (cls) {
  84. JNIEnv *env = ThreadAndroid::get_env();
  85. jmethodID getClassLoader = env->GetMethodID(cls, "getClassLoader", "()Ljava/lang/ClassLoader;");
  86. return env->CallObjectMethod(godot_instance, getClassLoader);
  87. } else {
  88. return NULL;
  89. }
  90. }
  91. void GodotJavaWrapper::gfx_init(bool gl2) {
  92. // beats me what this once did, there was no code,
  93. // but we're getting false if our GLES3 driver is initialised
  94. // and true for our GLES2 driver
  95. // Maybe we're supposed to communicate this back or store it?
  96. }
  97. void GodotJavaWrapper::on_video_init(JNIEnv *p_env) {
  98. if (_on_video_init)
  99. if (p_env == NULL)
  100. p_env = ThreadAndroid::get_env();
  101. p_env->CallVoidMethod(godot_instance, _on_video_init);
  102. }
  103. void GodotJavaWrapper::on_gl_godot_main_loop_started(JNIEnv *p_env) {
  104. if (_on_gl_godot_main_loop_started) {
  105. if (p_env == NULL) {
  106. p_env = ThreadAndroid::get_env();
  107. }
  108. }
  109. p_env->CallVoidMethod(godot_instance, _on_gl_godot_main_loop_started);
  110. }
  111. void GodotJavaWrapper::restart(JNIEnv *p_env) {
  112. if (_restart)
  113. if (p_env == NULL)
  114. p_env = ThreadAndroid::get_env();
  115. p_env->CallVoidMethod(godot_instance, _restart);
  116. }
  117. void GodotJavaWrapper::force_quit(JNIEnv *p_env) {
  118. if (_finish)
  119. if (p_env == NULL)
  120. p_env = ThreadAndroid::get_env();
  121. p_env->CallVoidMethod(godot_instance, _finish);
  122. }
  123. void GodotJavaWrapper::set_keep_screen_on(bool p_enabled) {
  124. if (_set_keep_screen_on) {
  125. JNIEnv *env = ThreadAndroid::get_env();
  126. env->CallVoidMethod(godot_instance, _set_keep_screen_on, p_enabled);
  127. }
  128. }
  129. void GodotJavaWrapper::alert(const String &p_message, const String &p_title) {
  130. if (_alert) {
  131. JNIEnv *env = ThreadAndroid::get_env();
  132. jstring jStrMessage = env->NewStringUTF(p_message.utf8().get_data());
  133. jstring jStrTitle = env->NewStringUTF(p_title.utf8().get_data());
  134. env->CallVoidMethod(godot_instance, _alert, jStrMessage, jStrTitle);
  135. }
  136. }
  137. int GodotJavaWrapper::get_gles_version_code() {
  138. JNIEnv *env = ThreadAndroid::get_env();
  139. if (_get_GLES_version_code) {
  140. return env->CallIntMethod(godot_instance, _get_GLES_version_code);
  141. }
  142. return 0;
  143. }
  144. bool GodotJavaWrapper::has_get_clipboard() {
  145. return _get_clipboard != 0;
  146. }
  147. String GodotJavaWrapper::get_clipboard() {
  148. if (_get_clipboard) {
  149. JNIEnv *env = ThreadAndroid::get_env();
  150. jstring s = (jstring)env->CallObjectMethod(godot_instance, _get_clipboard);
  151. return jstring_to_string(s, env);
  152. } else {
  153. return String();
  154. }
  155. }
  156. String GodotJavaWrapper::get_input_fallback_mapping() {
  157. if (_get_input_fallback_mapping) {
  158. JNIEnv *env = ThreadAndroid::get_env();
  159. jstring fallback_mapping = (jstring)env->CallObjectMethod(godot_instance, _get_input_fallback_mapping);
  160. return jstring_to_string(fallback_mapping, env);
  161. } else {
  162. return String();
  163. }
  164. }
  165. bool GodotJavaWrapper::has_set_clipboard() {
  166. return _set_clipboard != 0;
  167. }
  168. void GodotJavaWrapper::set_clipboard(const String &p_text) {
  169. if (_set_clipboard) {
  170. JNIEnv *env = ThreadAndroid::get_env();
  171. jstring jStr = env->NewStringUTF(p_text.utf8().get_data());
  172. env->CallVoidMethod(godot_instance, _set_clipboard, jStr);
  173. }
  174. }
  175. bool GodotJavaWrapper::request_permission(const String &p_name) {
  176. if (_request_permission) {
  177. JNIEnv *env = ThreadAndroid::get_env();
  178. jstring jStrName = env->NewStringUTF(p_name.utf8().get_data());
  179. return env->CallBooleanMethod(godot_instance, _request_permission, jStrName);
  180. } else {
  181. return false;
  182. }
  183. }
  184. bool GodotJavaWrapper::request_permissions() {
  185. if (_request_permissions) {
  186. JNIEnv *env = ThreadAndroid::get_env();
  187. return env->CallBooleanMethod(godot_instance, _request_permissions);
  188. } else {
  189. return false;
  190. }
  191. }
  192. Vector<String> GodotJavaWrapper::get_granted_permissions() const {
  193. Vector<String> permissions_list;
  194. if (_get_granted_permissions) {
  195. JNIEnv *env = ThreadAndroid::get_env();
  196. jobject permissions_object = env->CallObjectMethod(godot_instance, _get_granted_permissions);
  197. jobjectArray *arr = reinterpret_cast<jobjectArray *>(&permissions_object);
  198. int i = 0;
  199. jsize len = env->GetArrayLength(*arr);
  200. for (i = 0; i < len; i++) {
  201. jstring jstr = (jstring)env->GetObjectArrayElement(*arr, i);
  202. String str = jstring_to_string(jstr, env);
  203. permissions_list.push_back(str);
  204. env->DeleteLocalRef(jstr);
  205. }
  206. }
  207. return permissions_list;
  208. }
  209. void GodotJavaWrapper::init_input_devices() {
  210. if (_init_input_devices) {
  211. JNIEnv *env = ThreadAndroid::get_env();
  212. env->CallVoidMethod(godot_instance, _init_input_devices);
  213. }
  214. }
  215. jobject GodotJavaWrapper::get_surface() {
  216. if (_get_surface) {
  217. JNIEnv *env = ThreadAndroid::get_env();
  218. return env->CallObjectMethod(godot_instance, _get_surface);
  219. } else {
  220. return NULL;
  221. }
  222. }
  223. bool GodotJavaWrapper::is_activity_resumed() {
  224. if (_is_activity_resumed) {
  225. JNIEnv *env = ThreadAndroid::get_env();
  226. return env->CallBooleanMethod(godot_instance, _is_activity_resumed);
  227. } else {
  228. return false;
  229. }
  230. }
  231. void GodotJavaWrapper::vibrate(int p_duration_ms) {
  232. if (_vibrate) {
  233. JNIEnv *env = ThreadAndroid::get_env();
  234. env->CallVoidMethod(godot_instance, _vibrate, p_duration_ms);
  235. }
  236. }