java_godot_wrapper.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*************************************************************************/
  2. /* java_godot_wrapper.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_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 ThreadAndroid. 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_godot_instance) {
  37. godot_instance = p_env->NewGlobalRef(p_godot_instance);
  38. // get info about our Godot class so we can get pointers and stuff...
  39. cls = p_env->FindClass("org/godotengine/godot/Godot");
  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. // get some method pointers...
  47. _on_video_init = p_env->GetMethodID(cls, "onVideoInit", "()V");
  48. _restart = p_env->GetMethodID(cls, "restart", "()V");
  49. _finish = p_env->GetMethodID(cls, "forceQuit", "()V");
  50. _set_keep_screen_on = p_env->GetMethodID(cls, "setKeepScreenOn", "(Z)V");
  51. _alert = p_env->GetMethodID(cls, "alert", "(Ljava/lang/String;Ljava/lang/String;)V");
  52. _get_GLES_version_code = p_env->GetMethodID(cls, "getGLESVersionCode", "()I");
  53. _get_clipboard = p_env->GetMethodID(cls, "getClipboard", "()Ljava/lang/String;");
  54. _set_clipboard = p_env->GetMethodID(cls, "setClipboard", "(Ljava/lang/String;)V");
  55. _request_permission = p_env->GetMethodID(cls, "requestPermission", "(Ljava/lang/String;)Z");
  56. _init_input_devices = p_env->GetMethodID(cls, "initInputDevices", "()V");
  57. }
  58. GodotJavaWrapper::~GodotJavaWrapper() {
  59. // nothing to do here for now
  60. }
  61. jobject GodotJavaWrapper::get_activity() {
  62. // our godot instance is our activity
  63. return godot_instance;
  64. }
  65. jobject GodotJavaWrapper::get_member_object(const char *p_name, const char *p_class, JNIEnv *p_env) {
  66. if (cls) {
  67. if (p_env == NULL)
  68. p_env = ThreadAndroid::get_env();
  69. jfieldID fid = p_env->GetStaticFieldID(cls, p_name, p_class);
  70. return p_env->GetStaticObjectField(cls, fid);
  71. } else {
  72. return NULL;
  73. }
  74. }
  75. jobject GodotJavaWrapper::get_class_loader() {
  76. if (cls) {
  77. JNIEnv *env = ThreadAndroid::get_env();
  78. jmethodID getClassLoader = env->GetMethodID(cls, "getClassLoader", "()Ljava/lang/ClassLoader;");
  79. return env->CallObjectMethod(godot_instance, getClassLoader);
  80. } else {
  81. return NULL;
  82. }
  83. }
  84. void GodotJavaWrapper::gfx_init(bool gl2) {
  85. // beats me what this once did, there was no code,
  86. // but we're getting false if our GLES3 driver is initialised
  87. // and true for our GLES2 driver
  88. // Maybe we're supposed to communicate this back or store it?
  89. }
  90. void GodotJavaWrapper::on_video_init(JNIEnv *p_env) {
  91. if (_on_video_init)
  92. if (p_env == NULL)
  93. p_env = ThreadAndroid::get_env();
  94. p_env->CallVoidMethod(godot_instance, _on_video_init);
  95. }
  96. void GodotJavaWrapper::restart(JNIEnv *p_env) {
  97. if (_restart)
  98. if (p_env == NULL)
  99. p_env = ThreadAndroid::get_env();
  100. p_env->CallVoidMethod(godot_instance, _restart);
  101. }
  102. void GodotJavaWrapper::force_quit(JNIEnv *p_env) {
  103. if (_finish)
  104. if (p_env == NULL)
  105. p_env = ThreadAndroid::get_env();
  106. p_env->CallVoidMethod(godot_instance, _finish);
  107. }
  108. void GodotJavaWrapper::set_keep_screen_on(bool p_enabled) {
  109. if (_set_keep_screen_on) {
  110. JNIEnv *env = ThreadAndroid::get_env();
  111. env->CallVoidMethod(godot_instance, _set_keep_screen_on, p_enabled);
  112. }
  113. }
  114. void GodotJavaWrapper::alert(const String &p_message, const String &p_title) {
  115. if (_alert) {
  116. JNIEnv *env = ThreadAndroid::get_env();
  117. jstring jStrMessage = env->NewStringUTF(p_message.utf8().get_data());
  118. jstring jStrTitle = env->NewStringUTF(p_title.utf8().get_data());
  119. env->CallVoidMethod(godot_instance, _alert, jStrMessage, jStrTitle);
  120. }
  121. }
  122. int GodotJavaWrapper::get_gles_version_code() {
  123. JNIEnv *env = ThreadAndroid::get_env();
  124. if (_get_GLES_version_code) {
  125. return env->CallIntMethod(godot_instance, _get_GLES_version_code);
  126. }
  127. return 0;
  128. }
  129. bool GodotJavaWrapper::has_get_clipboard() {
  130. return _get_clipboard != 0;
  131. }
  132. String GodotJavaWrapper::get_clipboard() {
  133. if (_get_clipboard) {
  134. JNIEnv *env = ThreadAndroid::get_env();
  135. jstring s = (jstring)env->CallObjectMethod(godot_instance, _get_clipboard);
  136. return jstring_to_string(s, env);
  137. } else {
  138. return String();
  139. }
  140. }
  141. bool GodotJavaWrapper::has_set_clipboard() {
  142. return _set_clipboard != 0;
  143. }
  144. void GodotJavaWrapper::set_clipboard(const String &p_text) {
  145. if (_set_clipboard) {
  146. JNIEnv *env = ThreadAndroid::get_env();
  147. jstring jStr = env->NewStringUTF(p_text.utf8().get_data());
  148. env->CallVoidMethod(godot_instance, _set_clipboard, jStr);
  149. }
  150. }
  151. bool GodotJavaWrapper::request_permission(const String &p_name) {
  152. if (_request_permission) {
  153. JNIEnv *env = ThreadAndroid::get_env();
  154. jstring jStrName = env->NewStringUTF(p_name.utf8().get_data());
  155. return env->CallBooleanMethod(godot_instance, _request_permission, jStrName);
  156. } else {
  157. return false;
  158. }
  159. }
  160. void GodotJavaWrapper::init_input_devices() {
  161. if (_init_input_devices) {
  162. JNIEnv *env = ThreadAndroid::get_env();
  163. env->CallVoidMethod(godot_instance, _init_input_devices);
  164. }
  165. }