java_godot_wrapper.cpp 12 KB

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