java_godot_io_wrapper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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_temp_dir = p_env->GetMethodID(cls, "getTempDir", "()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_display_safe_area = p_env->GetMethodID(cls, "getDisplaySafeArea", "()[I"),
  54. _get_locale = p_env->GetMethodID(cls, "getLocale", "()Ljava/lang/String;");
  55. _get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;");
  56. _get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I");
  57. _get_scaled_density = p_env->GetMethodID(cls, "getScaledDensity", "()F");
  58. _get_screen_refresh_rate = p_env->GetMethodID(cls, "getScreenRefreshRate", "(D)D");
  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. _has_hardware_keyboard = p_env->GetMethodID(cls, "hasHardwareKeyboard", "()Z");
  63. _set_screen_orientation = p_env->GetMethodID(cls, "setScreenOrientation", "(I)V");
  64. _get_screen_orientation = p_env->GetMethodID(cls, "getScreenOrientation", "()I");
  65. _get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(IZ)Ljava/lang/String;");
  66. }
  67. }
  68. GodotIOJavaWrapper::~GodotIOJavaWrapper() {
  69. JNIEnv *env = get_jni_env();
  70. ERR_FAIL_NULL(env);
  71. env->DeleteGlobalRef(cls);
  72. env->DeleteGlobalRef(godot_io_instance);
  73. }
  74. jobject GodotIOJavaWrapper::get_instance() {
  75. return godot_io_instance;
  76. }
  77. Error GodotIOJavaWrapper::open_uri(const String &p_uri) {
  78. if (_open_URI) {
  79. JNIEnv *env = get_jni_env();
  80. ERR_FAIL_NULL_V(env, ERR_UNAVAILABLE);
  81. jstring jStr = env->NewStringUTF(p_uri.utf8().get_data());
  82. Error result = env->CallIntMethod(godot_io_instance, _open_URI, jStr) ? ERR_CANT_OPEN : OK;
  83. env->DeleteLocalRef(jStr);
  84. return result;
  85. } else {
  86. return ERR_UNAVAILABLE;
  87. }
  88. }
  89. String GodotIOJavaWrapper::get_cache_dir() {
  90. if (_get_cache_dir) {
  91. JNIEnv *env = get_jni_env();
  92. ERR_FAIL_NULL_V(env, String());
  93. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_cache_dir);
  94. return jstring_to_string(s, env);
  95. } else {
  96. return String();
  97. }
  98. }
  99. String GodotIOJavaWrapper::get_temp_dir() {
  100. if (_get_temp_dir) {
  101. JNIEnv *env = get_jni_env();
  102. ERR_FAIL_NULL_V(env, String());
  103. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_temp_dir);
  104. return jstring_to_string(s, env);
  105. } else {
  106. return String();
  107. }
  108. }
  109. String GodotIOJavaWrapper::get_user_data_dir(const String &p_user_dir) {
  110. if (_get_data_dir) {
  111. JNIEnv *env = get_jni_env();
  112. ERR_FAIL_NULL_V(env, String());
  113. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_data_dir);
  114. return jstring_to_string(s, env);
  115. } else {
  116. return String();
  117. }
  118. }
  119. String GodotIOJavaWrapper::get_locale() {
  120. if (_get_locale) {
  121. JNIEnv *env = get_jni_env();
  122. ERR_FAIL_NULL_V(env, String());
  123. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_locale);
  124. return jstring_to_string(s, env);
  125. } else {
  126. return String();
  127. }
  128. }
  129. String GodotIOJavaWrapper::get_model() {
  130. if (_get_model) {
  131. JNIEnv *env = get_jni_env();
  132. ERR_FAIL_NULL_V(env, String());
  133. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_model);
  134. return jstring_to_string(s, env);
  135. } else {
  136. return String();
  137. }
  138. }
  139. int GodotIOJavaWrapper::get_screen_dpi() {
  140. if (_get_screen_DPI) {
  141. JNIEnv *env = get_jni_env();
  142. ERR_FAIL_NULL_V(env, 160);
  143. return env->CallIntMethod(godot_io_instance, _get_screen_DPI);
  144. } else {
  145. return 160;
  146. }
  147. }
  148. float GodotIOJavaWrapper::get_scaled_density() {
  149. if (_get_scaled_density) {
  150. JNIEnv *env = get_jni_env();
  151. ERR_FAIL_NULL_V(env, 1.0f);
  152. return env->CallFloatMethod(godot_io_instance, _get_scaled_density);
  153. } else {
  154. return 1.0f;
  155. }
  156. }
  157. float GodotIOJavaWrapper::get_screen_refresh_rate(float fallback) {
  158. if (_get_screen_refresh_rate) {
  159. JNIEnv *env = get_jni_env();
  160. if (env == nullptr) {
  161. ERR_PRINT("An error occurred while trying to get screen refresh rate.");
  162. return fallback;
  163. }
  164. return (float)env->CallDoubleMethod(godot_io_instance, _get_screen_refresh_rate, (double)fallback);
  165. }
  166. ERR_PRINT("An error occurred while trying to get the screen refresh rate.");
  167. return fallback;
  168. }
  169. TypedArray<Rect2> GodotIOJavaWrapper::get_display_cutouts() {
  170. TypedArray<Rect2> result;
  171. ERR_FAIL_NULL_V(_get_display_cutouts, result);
  172. JNIEnv *env = get_jni_env();
  173. ERR_FAIL_NULL_V(env, result);
  174. jintArray returnArray = (jintArray)env->CallObjectMethod(godot_io_instance, _get_display_cutouts);
  175. jint arrayLength = env->GetArrayLength(returnArray);
  176. jint *arrayBody = env->GetIntArrayElements(returnArray, JNI_FALSE);
  177. int cutouts = arrayLength / 4;
  178. for (int i = 0; i < cutouts; i++) {
  179. int x = arrayBody[i * 4];
  180. int y = arrayBody[i * 4 + 1];
  181. int width = arrayBody[i * 4 + 2];
  182. int height = arrayBody[i * 4 + 3];
  183. Rect2 cutout(x, y, width, height);
  184. result.append(cutout);
  185. }
  186. env->ReleaseIntArrayElements(returnArray, arrayBody, 0);
  187. return result;
  188. }
  189. Rect2i GodotIOJavaWrapper::get_display_safe_area() {
  190. Rect2i result;
  191. ERR_FAIL_NULL_V(_get_display_safe_area, result);
  192. JNIEnv *env = get_jni_env();
  193. ERR_FAIL_NULL_V(env, result);
  194. jintArray returnArray = (jintArray)env->CallObjectMethod(godot_io_instance, _get_display_safe_area);
  195. ERR_FAIL_COND_V(env->GetArrayLength(returnArray) != 4, result);
  196. jint *arrayBody = env->GetIntArrayElements(returnArray, JNI_FALSE);
  197. result = Rect2i(arrayBody[0], arrayBody[1], arrayBody[2], arrayBody[3]);
  198. env->ReleaseIntArrayElements(returnArray, arrayBody, 0);
  199. return result;
  200. }
  201. String GodotIOJavaWrapper::get_unique_id() {
  202. if (_get_unique_id) {
  203. JNIEnv *env = get_jni_env();
  204. ERR_FAIL_NULL_V(env, String());
  205. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_unique_id);
  206. return jstring_to_string(s, env);
  207. } else {
  208. return String();
  209. }
  210. }
  211. bool GodotIOJavaWrapper::has_vk() {
  212. return (_show_keyboard != nullptr) && (_hide_keyboard != nullptr);
  213. }
  214. bool GodotIOJavaWrapper::has_hardware_keyboard() {
  215. if (_has_hardware_keyboard) {
  216. JNIEnv *env = get_jni_env();
  217. ERR_FAIL_NULL_V(env, false);
  218. return env->CallBooleanMethod(godot_io_instance, _has_hardware_keyboard);
  219. } else {
  220. return false;
  221. }
  222. }
  223. void GodotIOJavaWrapper::show_vk(const String &p_existing, int p_type, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
  224. if (_show_keyboard) {
  225. JNIEnv *env = get_jni_env();
  226. ERR_FAIL_NULL(env);
  227. jstring jStr = env->NewStringUTF(p_existing.utf8().get_data());
  228. env->CallVoidMethod(godot_io_instance, _show_keyboard, jStr, p_type, p_max_input_length, p_cursor_start, p_cursor_end);
  229. env->DeleteLocalRef(jStr);
  230. }
  231. }
  232. void GodotIOJavaWrapper::hide_vk() {
  233. if (_hide_keyboard) {
  234. JNIEnv *env = get_jni_env();
  235. ERR_FAIL_NULL(env);
  236. env->CallVoidMethod(godot_io_instance, _hide_keyboard);
  237. }
  238. }
  239. void GodotIOJavaWrapper::set_screen_orientation(int p_orient) {
  240. if (_set_screen_orientation) {
  241. JNIEnv *env = get_jni_env();
  242. ERR_FAIL_NULL(env);
  243. env->CallVoidMethod(godot_io_instance, _set_screen_orientation, p_orient);
  244. }
  245. }
  246. int GodotIOJavaWrapper::get_screen_orientation() {
  247. if (_get_screen_orientation) {
  248. JNIEnv *env = get_jni_env();
  249. ERR_FAIL_NULL_V(env, 0);
  250. return env->CallIntMethod(godot_io_instance, _get_screen_orientation);
  251. } else {
  252. return 0;
  253. }
  254. }
  255. String GodotIOJavaWrapper::get_system_dir(int p_dir, bool p_shared_storage) {
  256. if (_get_system_dir) {
  257. JNIEnv *env = get_jni_env();
  258. ERR_FAIL_NULL_V(env, String("."));
  259. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_system_dir, p_dir, p_shared_storage);
  260. return jstring_to_string(s, env);
  261. } else {
  262. return String(".");
  263. }
  264. }
  265. // SafeNumeric because it can be changed from non-main thread and we need to
  266. // ensure the change is immediately visible to other threads.
  267. static SafeNumeric<int> virtual_keyboard_height;
  268. int GodotIOJavaWrapper::get_vk_height() {
  269. return virtual_keyboard_height.get();
  270. }
  271. void GodotIOJavaWrapper::set_vk_height(int p_height) {
  272. virtual_keyboard_height.set(p_height);
  273. }