java_godot_wrapper.cpp 14 KB

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