java_godot_io_wrapper.cpp 10 KB

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