java_godot_wrapper.cpp 13 KB

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