2
0

java_godot_io_wrapper.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*************************************************************************/
  2. /* java_godot_io_wrapper.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_io_wrapper.h"
  31. #include "core/error_list.h"
  32. // JNIEnv is only valid within the thread it belongs to, in a multi threading environment
  33. // we can't cache it.
  34. // For GodotIO we call all access methods from our thread and we thus get a valid JNIEnv
  35. // from ThreadAndroid.
  36. GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instance) {
  37. godot_io_instance = p_env->NewGlobalRef(p_godot_io_instance);
  38. if (godot_io_instance) {
  39. cls = p_env->GetObjectClass(godot_io_instance);
  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. _open_URI = p_env->GetMethodID(cls, "openURI", "(Ljava/lang/String;)I");
  47. _get_cache_dir = p_env->GetMethodID(cls, "getCacheDir", "()Ljava/lang/String;");
  48. _get_data_dir = p_env->GetMethodID(cls, "getDataDir", "()Ljava/lang/String;");
  49. _get_locale = p_env->GetMethodID(cls, "getLocale", "()Ljava/lang/String;");
  50. _get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;");
  51. _get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I");
  52. _get_window_safe_area = p_env->GetMethodID(cls, "getWindowSafeArea", "()[I"),
  53. _get_unique_id = p_env->GetMethodID(cls, "getUniqueID", "()Ljava/lang/String;");
  54. _show_keyboard = p_env->GetMethodID(cls, "showKeyboard", "(Ljava/lang/String;ZIII)V");
  55. _hide_keyboard = p_env->GetMethodID(cls, "hideKeyboard", "()V");
  56. _set_screen_orientation = p_env->GetMethodID(cls, "setScreenOrientation", "(I)V");
  57. _get_screen_orientation = p_env->GetMethodID(cls, "getScreenOrientation", "()I");
  58. _get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(IZ)Ljava/lang/String;");
  59. _play_video = p_env->GetMethodID(cls, "playVideo", "(Ljava/lang/String;)V");
  60. _is_video_playing = p_env->GetMethodID(cls, "isVideoPlaying", "()Z");
  61. _pause_video = p_env->GetMethodID(cls, "pauseVideo", "()V");
  62. _stop_video = p_env->GetMethodID(cls, "stopVideo", "()V");
  63. }
  64. }
  65. GodotIOJavaWrapper::~GodotIOJavaWrapper() {
  66. // nothing to do here for now
  67. }
  68. jobject GodotIOJavaWrapper::get_instance() {
  69. return godot_io_instance;
  70. }
  71. Error GodotIOJavaWrapper::open_uri(const String &p_uri) {
  72. if (_open_URI) {
  73. JNIEnv *env = get_jni_env();
  74. ERR_FAIL_COND_V(env == nullptr, ERR_UNAVAILABLE);
  75. jstring jStr = env->NewStringUTF(p_uri.utf8().get_data());
  76. return env->CallIntMethod(godot_io_instance, _open_URI, jStr) ? ERR_CANT_OPEN : OK;
  77. } else {
  78. return ERR_UNAVAILABLE;
  79. }
  80. }
  81. String GodotIOJavaWrapper::get_cache_dir() {
  82. if (_get_cache_dir) {
  83. JNIEnv *env = get_jni_env();
  84. ERR_FAIL_COND_V(env == nullptr, String());
  85. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_cache_dir);
  86. return jstring_to_string(s, env);
  87. } else {
  88. return String();
  89. }
  90. }
  91. String GodotIOJavaWrapper::get_user_data_dir() {
  92. if (_get_data_dir) {
  93. JNIEnv *env = get_jni_env();
  94. ERR_FAIL_COND_V(env == nullptr, String());
  95. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_data_dir);
  96. return jstring_to_string(s, env);
  97. } else {
  98. return String();
  99. }
  100. }
  101. String GodotIOJavaWrapper::get_locale() {
  102. if (_get_locale) {
  103. JNIEnv *env = get_jni_env();
  104. ERR_FAIL_COND_V(env == nullptr, String());
  105. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_locale);
  106. return jstring_to_string(s, env);
  107. } else {
  108. return String();
  109. }
  110. }
  111. String GodotIOJavaWrapper::get_model() {
  112. if (_get_model) {
  113. JNIEnv *env = get_jni_env();
  114. ERR_FAIL_COND_V(env == nullptr, String());
  115. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_model);
  116. return jstring_to_string(s, env);
  117. } else {
  118. return String();
  119. }
  120. }
  121. int GodotIOJavaWrapper::get_screen_dpi() {
  122. if (_get_screen_DPI) {
  123. JNIEnv *env = get_jni_env();
  124. ERR_FAIL_COND_V(env == nullptr, 160);
  125. return env->CallIntMethod(godot_io_instance, _get_screen_DPI);
  126. } else {
  127. return 160;
  128. }
  129. }
  130. void GodotIOJavaWrapper::get_window_safe_area(int (&p_rect_xywh)[4]) {
  131. if (_get_window_safe_area) {
  132. JNIEnv *env = get_jni_env();
  133. ERR_FAIL_COND(env == nullptr);
  134. jintArray returnArray = (jintArray)env->CallObjectMethod(godot_io_instance, _get_window_safe_area);
  135. ERR_FAIL_COND(env->GetArrayLength(returnArray) != 4);
  136. jint *arrayBody = env->GetIntArrayElements(returnArray, JNI_FALSE);
  137. for (int i = 0; i < 4; i++) {
  138. p_rect_xywh[i] = arrayBody[i];
  139. }
  140. env->ReleaseIntArrayElements(returnArray, arrayBody, 0);
  141. }
  142. }
  143. String GodotIOJavaWrapper::get_unique_id() {
  144. if (_get_unique_id) {
  145. JNIEnv *env = get_jni_env();
  146. ERR_FAIL_COND_V(env == nullptr, String());
  147. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_unique_id);
  148. return jstring_to_string(s, env);
  149. } else {
  150. return String();
  151. }
  152. }
  153. bool GodotIOJavaWrapper::has_vk() {
  154. return (_show_keyboard != 0) && (_hide_keyboard != 0);
  155. }
  156. void GodotIOJavaWrapper::show_vk(const String &p_existing, bool p_multiline, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
  157. if (_show_keyboard) {
  158. JNIEnv *env = get_jni_env();
  159. ERR_FAIL_COND(env == nullptr);
  160. jstring jStr = env->NewStringUTF(p_existing.utf8().get_data());
  161. env->CallVoidMethod(godot_io_instance, _show_keyboard, jStr, p_multiline, p_max_input_length, p_cursor_start, p_cursor_end);
  162. }
  163. }
  164. void GodotIOJavaWrapper::hide_vk() {
  165. if (_hide_keyboard) {
  166. JNIEnv *env = get_jni_env();
  167. ERR_FAIL_COND(env == nullptr);
  168. env->CallVoidMethod(godot_io_instance, _hide_keyboard);
  169. }
  170. }
  171. void GodotIOJavaWrapper::set_screen_orientation(int p_orient) {
  172. if (_set_screen_orientation) {
  173. JNIEnv *env = get_jni_env();
  174. ERR_FAIL_COND(env == nullptr);
  175. env->CallVoidMethod(godot_io_instance, _set_screen_orientation, p_orient);
  176. }
  177. }
  178. int GodotIOJavaWrapper::get_screen_orientation() const {
  179. if (_get_screen_orientation) {
  180. JNIEnv *env = get_jni_env();
  181. ERR_FAIL_COND_V(env == nullptr, 0);
  182. return env->CallIntMethod(godot_io_instance, _get_screen_orientation);
  183. } else {
  184. return 0;
  185. }
  186. }
  187. String GodotIOJavaWrapper::get_system_dir(int p_dir, bool p_shared_storage) {
  188. if (_get_system_dir) {
  189. JNIEnv *env = get_jni_env();
  190. ERR_FAIL_COND_V(env == nullptr, String("."));
  191. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_system_dir, p_dir, p_shared_storage);
  192. return jstring_to_string(s, env);
  193. } else {
  194. return String(".");
  195. }
  196. }
  197. void GodotIOJavaWrapper::play_video(const String &p_path) {
  198. // Why is this not here?!?!
  199. }
  200. bool GodotIOJavaWrapper::is_video_playing() {
  201. if (_is_video_playing) {
  202. JNIEnv *env = get_jni_env();
  203. return env->CallBooleanMethod(godot_io_instance, _is_video_playing);
  204. } else {
  205. return false;
  206. }
  207. }
  208. void GodotIOJavaWrapper::pause_video() {
  209. if (_pause_video) {
  210. JNIEnv *env = get_jni_env();
  211. env->CallVoidMethod(godot_io_instance, _pause_video);
  212. }
  213. }
  214. void GodotIOJavaWrapper::stop_video() {
  215. if (_stop_video) {
  216. JNIEnv *env = get_jni_env();
  217. env->CallVoidMethod(godot_io_instance, _stop_video);
  218. }
  219. }
  220. // SafeNumeric because it can be changed from non-main thread and we need to
  221. // ensure the change is immediately visible to other threads.
  222. static SafeNumeric<int> virtual_keyboard_height;
  223. int GodotIOJavaWrapper::get_vk_height() {
  224. return virtual_keyboard_height.get();
  225. }
  226. void GodotIOJavaWrapper::set_vk_height(int p_height) {
  227. virtual_keyboard_height.set(p_height);
  228. }