java_godot_io_wrapper.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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) 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_io_wrapper.h"
  31. #include "core/array.h"
  32. #include "core/error_list.h"
  33. #include "core/math/rect2.h"
  34. #include "core/variant.h"
  35. // JNIEnv is only valid within the thread it belongs to, in a multi threading environment
  36. // we can't cache it.
  37. // For GodotIO we call all access methods from our thread and we thus get a valid JNIEnv
  38. // from ThreadAndroid.
  39. GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instance) {
  40. godot_io_instance = p_env->NewGlobalRef(p_godot_io_instance);
  41. if (godot_io_instance) {
  42. cls = p_env->GetObjectClass(godot_io_instance);
  43. if (cls) {
  44. cls = (jclass)p_env->NewGlobalRef(cls);
  45. } else {
  46. // this is a pretty serious fail.. bail... pointers will stay 0
  47. return;
  48. }
  49. _open_URI = p_env->GetMethodID(cls, "openURI", "(Ljava/lang/String;)I");
  50. _get_cache_dir = p_env->GetMethodID(cls, "getCacheDir", "()Ljava/lang/String;");
  51. _get_data_dir = p_env->GetMethodID(cls, "getDataDir", "()Ljava/lang/String;");
  52. _get_display_cutouts = p_env->GetMethodID(cls, "getDisplayCutouts", "()[I"),
  53. _get_locale = p_env->GetMethodID(cls, "getLocale", "()Ljava/lang/String;");
  54. _get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;");
  55. _get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I");
  56. _get_scaled_density = p_env->GetMethodID(cls, "getScaledDensity", "()F");
  57. _get_screen_refresh_rate = p_env->GetMethodID(cls, "getScreenRefreshRate", "(D)D");
  58. _get_window_safe_area = p_env->GetMethodID(cls, "getWindowSafeArea", "()[I"),
  59. _get_unique_id = p_env->GetMethodID(cls, "getUniqueID", "()Ljava/lang/String;");
  60. _show_keyboard = p_env->GetMethodID(cls, "showKeyboard", "(Ljava/lang/String;IIII)V");
  61. _hide_keyboard = p_env->GetMethodID(cls, "hideKeyboard", "()V");
  62. _set_screen_orientation = p_env->GetMethodID(cls, "setScreenOrientation", "(I)V");
  63. _get_screen_orientation = p_env->GetMethodID(cls, "getScreenOrientation", "()I");
  64. _get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(IZ)Ljava/lang/String;");
  65. }
  66. }
  67. GodotIOJavaWrapper::~GodotIOJavaWrapper() {
  68. // nothing to do here for now
  69. }
  70. jobject GodotIOJavaWrapper::get_instance() {
  71. return godot_io_instance;
  72. }
  73. Error GodotIOJavaWrapper::open_uri(const String &p_uri) {
  74. if (_open_URI) {
  75. JNIEnv *env = get_jni_env();
  76. ERR_FAIL_NULL_V(env, ERR_UNAVAILABLE);
  77. jstring jStr = env->NewStringUTF(p_uri.utf8().get_data());
  78. return env->CallIntMethod(godot_io_instance, _open_URI, jStr) ? ERR_CANT_OPEN : OK;
  79. } else {
  80. return ERR_UNAVAILABLE;
  81. }
  82. }
  83. String GodotIOJavaWrapper::get_cache_dir() {
  84. if (_get_cache_dir) {
  85. JNIEnv *env = get_jni_env();
  86. ERR_FAIL_NULL_V(env, String());
  87. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_cache_dir);
  88. return jstring_to_string(s, env);
  89. } else {
  90. return String();
  91. }
  92. }
  93. String GodotIOJavaWrapper::get_user_data_dir() {
  94. if (_get_data_dir) {
  95. JNIEnv *env = get_jni_env();
  96. ERR_FAIL_NULL_V(env, String());
  97. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_data_dir);
  98. return jstring_to_string(s, env);
  99. } else {
  100. return String();
  101. }
  102. }
  103. String GodotIOJavaWrapper::get_locale() {
  104. if (_get_locale) {
  105. JNIEnv *env = get_jni_env();
  106. ERR_FAIL_NULL_V(env, String());
  107. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_locale);
  108. return jstring_to_string(s, env);
  109. } else {
  110. return String();
  111. }
  112. }
  113. String GodotIOJavaWrapper::get_model() {
  114. if (_get_model) {
  115. JNIEnv *env = get_jni_env();
  116. ERR_FAIL_NULL_V(env, String());
  117. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_model);
  118. return jstring_to_string(s, env);
  119. } else {
  120. return String();
  121. }
  122. }
  123. int GodotIOJavaWrapper::get_screen_dpi() {
  124. if (_get_screen_DPI) {
  125. JNIEnv *env = get_jni_env();
  126. ERR_FAIL_NULL_V(env, 160);
  127. return env->CallIntMethod(godot_io_instance, _get_screen_DPI);
  128. } else {
  129. return 160;
  130. }
  131. }
  132. float GodotIOJavaWrapper::get_scaled_density() {
  133. if (_get_scaled_density) {
  134. JNIEnv *env = get_jni_env();
  135. ERR_FAIL_NULL_V(env, 1.0f);
  136. return env->CallFloatMethod(godot_io_instance, _get_scaled_density);
  137. } else {
  138. return 1.0f;
  139. }
  140. }
  141. float GodotIOJavaWrapper::get_screen_refresh_rate(float p_fallback) {
  142. if (_get_screen_refresh_rate) {
  143. JNIEnv *env = get_jni_env();
  144. if (env == nullptr) {
  145. ERR_PRINT("An error occurred while trying to get screen refresh rate.");
  146. return p_fallback;
  147. }
  148. return (float)env->CallDoubleMethod(godot_io_instance, _get_screen_refresh_rate, (double)p_fallback);
  149. }
  150. ERR_PRINT("An error occurred while trying to get the screen refresh rate.");
  151. return p_fallback;
  152. }
  153. void GodotIOJavaWrapper::get_window_safe_area(int (&p_rect_xywh)[4]) {
  154. if (_get_window_safe_area) {
  155. JNIEnv *env = get_jni_env();
  156. ERR_FAIL_NULL(env);
  157. jintArray returnArray = (jintArray)env->CallObjectMethod(godot_io_instance, _get_window_safe_area);
  158. ERR_FAIL_COND(env->GetArrayLength(returnArray) != 4);
  159. jint *arrayBody = env->GetIntArrayElements(returnArray, JNI_FALSE);
  160. for (int i = 0; i < 4; i++) {
  161. p_rect_xywh[i] = arrayBody[i];
  162. }
  163. env->ReleaseIntArrayElements(returnArray, arrayBody, 0);
  164. }
  165. }
  166. Array GodotIOJavaWrapper::get_display_cutouts() {
  167. Array result;
  168. ERR_FAIL_NULL_V(_get_display_cutouts, result);
  169. JNIEnv *env = get_jni_env();
  170. ERR_FAIL_NULL_V(env, result);
  171. jintArray returnArray = (jintArray)env->CallObjectMethod(godot_io_instance, _get_display_cutouts);
  172. jint arrayLength = env->GetArrayLength(returnArray);
  173. jint *arrayBody = env->GetIntArrayElements(returnArray, JNI_FALSE);
  174. int cutouts = arrayLength / 4;
  175. for (int i = 0; i < cutouts; i++) {
  176. int x = arrayBody[i * 4];
  177. int y = arrayBody[i * 4 + 1];
  178. int width = arrayBody[i * 4 + 2];
  179. int height = arrayBody[i * 4 + 3];
  180. Rect2 cutout(x, y, width, height);
  181. result.append(cutout);
  182. }
  183. env->ReleaseIntArrayElements(returnArray, arrayBody, 0);
  184. return result;
  185. }
  186. String GodotIOJavaWrapper::get_unique_id() {
  187. if (_get_unique_id) {
  188. JNIEnv *env = get_jni_env();
  189. ERR_FAIL_NULL_V(env, String());
  190. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_unique_id);
  191. return jstring_to_string(s, env);
  192. } else {
  193. return String();
  194. }
  195. }
  196. bool GodotIOJavaWrapper::has_vk() {
  197. return (_show_keyboard != nullptr) && (_hide_keyboard != nullptr);
  198. }
  199. void GodotIOJavaWrapper::show_vk(const String &p_existing, int p_type, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
  200. if (_show_keyboard) {
  201. JNIEnv *env = get_jni_env();
  202. ERR_FAIL_NULL(env);
  203. jstring jStr = env->NewStringUTF(p_existing.utf8().get_data());
  204. env->CallVoidMethod(godot_io_instance, _show_keyboard, jStr, p_type, p_max_input_length, p_cursor_start, p_cursor_end);
  205. }
  206. }
  207. void GodotIOJavaWrapper::hide_vk() {
  208. if (_hide_keyboard) {
  209. JNIEnv *env = get_jni_env();
  210. ERR_FAIL_NULL(env);
  211. env->CallVoidMethod(godot_io_instance, _hide_keyboard);
  212. }
  213. }
  214. void GodotIOJavaWrapper::set_screen_orientation(int p_orient) {
  215. if (_set_screen_orientation) {
  216. JNIEnv *env = get_jni_env();
  217. ERR_FAIL_NULL(env);
  218. env->CallVoidMethod(godot_io_instance, _set_screen_orientation, p_orient);
  219. }
  220. }
  221. int GodotIOJavaWrapper::get_screen_orientation() const {
  222. if (_get_screen_orientation) {
  223. JNIEnv *env = get_jni_env();
  224. ERR_FAIL_NULL_V(env, 0);
  225. return env->CallIntMethod(godot_io_instance, _get_screen_orientation);
  226. } else {
  227. return 0;
  228. }
  229. }
  230. String GodotIOJavaWrapper::get_system_dir(int p_dir, bool p_shared_storage) {
  231. if (_get_system_dir) {
  232. JNIEnv *env = get_jni_env();
  233. ERR_FAIL_NULL_V(env, String("."));
  234. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_system_dir, p_dir, p_shared_storage);
  235. return jstring_to_string(s, env);
  236. } else {
  237. return String(".");
  238. }
  239. }
  240. // SafeNumeric because it can be changed from non-main thread and we need to
  241. // ensure the change is immediately visible to other threads.
  242. static SafeNumeric<int> virtual_keyboard_height;
  243. int GodotIOJavaWrapper::get_vk_height() {
  244. return virtual_keyboard_height.get();
  245. }
  246. void GodotIOJavaWrapper::set_vk_height(int p_height) {
  247. virtual_keyboard_height.set(p_height);
  248. }