porting_android.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. Minetest
  3. Copyright (C) 2014 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef __ANDROID__
  17. #error This file may only be compiled for android!
  18. #endif
  19. #include "util/numeric.h"
  20. #include "porting.h"
  21. #include "porting_android.h"
  22. #include "threading/thread.h"
  23. #include "config.h"
  24. #include "filesys.h"
  25. #include "log.h"
  26. #include <sstream>
  27. #include <exception>
  28. #include <cstdlib>
  29. #ifdef GPROF
  30. #include "prof.h"
  31. #endif
  32. extern int main(int argc, char *argv[]);
  33. void android_main(android_app *app)
  34. {
  35. int retval = 0;
  36. porting::app_global = app;
  37. Thread::setName("Main");
  38. try {
  39. char *argv[] = {strdup(PROJECT_NAME), nullptr};
  40. main(ARRLEN(argv) - 1, argv);
  41. free(argv[0]);
  42. } catch (std::exception &e) {
  43. errorstream << "Uncaught exception in main thread: " << e.what() << std::endl;
  44. retval = -1;
  45. } catch (...) {
  46. errorstream << "Uncaught exception in main thread!" << std::endl;
  47. retval = -1;
  48. }
  49. porting::cleanupAndroid();
  50. infostream << "Shutting down." << std::endl;
  51. exit(retval);
  52. }
  53. /**
  54. * Handler for finished message box input
  55. * Intentionally NOT in namespace porting
  56. * ToDo: this doesn't work as expected, there's a workaround for it right now
  57. */
  58. extern "C" {
  59. JNIEXPORT void JNICALL Java_net_minetest_minetest_GameActivity_putMessageBoxResult(
  60. JNIEnv *env, jclass thiz, jstring text)
  61. {
  62. errorstream <<
  63. "Java_net_minetest_minetest_GameActivity_putMessageBoxResult got: " <<
  64. std::string((const char*) env->GetStringChars(text, nullptr)) << std::endl;
  65. }
  66. }
  67. namespace porting {
  68. android_app *app_global;
  69. JNIEnv *jnienv;
  70. jclass nativeActivity;
  71. jclass findClass(const std::string &classname)
  72. {
  73. if (jnienv == nullptr)
  74. return nullptr;
  75. jclass nativeactivity = jnienv->FindClass("android/app/NativeActivity");
  76. jmethodID getClassLoader = jnienv->GetMethodID(
  77. nativeactivity, "getClassLoader", "()Ljava/lang/ClassLoader;");
  78. jobject cls = jnienv->CallObjectMethod(
  79. app_global->activity->clazz, getClassLoader);
  80. jclass classLoader = jnienv->FindClass("java/lang/ClassLoader");
  81. jmethodID findClass = jnienv->GetMethodID(classLoader, "loadClass",
  82. "(Ljava/lang/String;)Ljava/lang/Class;");
  83. jstring strClassName = jnienv->NewStringUTF(classname.c_str());
  84. return (jclass) jnienv->CallObjectMethod(cls, findClass, strClassName);
  85. }
  86. void initAndroid()
  87. {
  88. porting::jnienv = nullptr;
  89. JavaVM *jvm = app_global->activity->vm;
  90. JavaVMAttachArgs lJavaVMAttachArgs;
  91. lJavaVMAttachArgs.version = JNI_VERSION_1_6;
  92. lJavaVMAttachArgs.name = PROJECT_NAME_C "NativeThread";
  93. lJavaVMAttachArgs.group = nullptr;
  94. if (jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
  95. errorstream << "Failed to attach native thread to jvm" << std::endl;
  96. exit(-1);
  97. }
  98. nativeActivity = findClass("net/minetest/minetest/GameActivity");
  99. if (nativeActivity == nullptr)
  100. errorstream <<
  101. "porting::initAndroid unable to find java native activity class" <<
  102. std::endl;
  103. #ifdef GPROF
  104. // in the start-up code
  105. __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME_C,
  106. "Initializing GPROF profiler");
  107. monstartup("libMinetest.so");
  108. #endif
  109. }
  110. void cleanupAndroid()
  111. {
  112. #ifdef GPROF
  113. errorstream << "Shutting down GPROF profiler" << std::endl;
  114. setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1);
  115. moncleanup();
  116. #endif
  117. JavaVM *jvm = app_global->activity->vm;
  118. jvm->DetachCurrentThread();
  119. }
  120. static std::string javaStringToUTF8(jstring js)
  121. {
  122. std::string str;
  123. // Get string as a UTF-8 c-string
  124. const char *c_str = jnienv->GetStringUTFChars(js, nullptr);
  125. // Save it
  126. str = c_str;
  127. // And free the c-string
  128. jnienv->ReleaseStringUTFChars(js, c_str);
  129. return str;
  130. }
  131. void initializePathsAndroid()
  132. {
  133. // Set user and share paths
  134. {
  135. jmethodID getUserDataPath = jnienv->GetMethodID(nativeActivity,
  136. "getUserDataPath", "()Ljava/lang/String;");
  137. FATAL_ERROR_IF(getUserDataPath==nullptr,
  138. "porting::initializePathsAndroid unable to find Java getUserDataPath method");
  139. jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, getUserDataPath);
  140. const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
  141. path_user = javachars;
  142. path_share = javachars;
  143. path_locale = path_share + DIR_DELIM + "locale";
  144. jnienv->ReleaseStringUTFChars((jstring) result, javachars);
  145. }
  146. // Set cache path
  147. {
  148. jmethodID getCachePath = jnienv->GetMethodID(nativeActivity,
  149. "getCachePath", "()Ljava/lang/String;");
  150. FATAL_ERROR_IF(getCachePath==nullptr,
  151. "porting::initializePathsAndroid unable to find Java getCachePath method");
  152. jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, getCachePath);
  153. const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
  154. path_cache = javachars;
  155. jnienv->ReleaseStringUTFChars((jstring) result, javachars);
  156. migrateCachePath();
  157. }
  158. }
  159. void showInputDialog(const std::string &acceptButton, const std::string &hint,
  160. const std::string &current, int editType)
  161. {
  162. jmethodID showdialog = jnienv->GetMethodID(nativeActivity, "showDialog",
  163. "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
  164. FATAL_ERROR_IF(showdialog == nullptr,
  165. "porting::showInputDialog unable to find java show dialog method");
  166. jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str());
  167. jstring jhint = jnienv->NewStringUTF(hint.c_str());
  168. jstring jcurrent = jnienv->NewStringUTF(current.c_str());
  169. jint jeditType = editType;
  170. jnienv->CallVoidMethod(app_global->activity->clazz, showdialog,
  171. jacceptButton, jhint, jcurrent, jeditType);
  172. }
  173. void openURIAndroid(const std::string &url)
  174. {
  175. jmethodID url_open = jnienv->GetMethodID(nativeActivity, "openURI",
  176. "(Ljava/lang/String;)V");
  177. FATAL_ERROR_IF(url_open == nullptr,
  178. "porting::openURIAndroid unable to find java openURI method");
  179. jstring jurl = jnienv->NewStringUTF(url.c_str());
  180. jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl);
  181. }
  182. int getInputDialogState()
  183. {
  184. jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,
  185. "getDialogState", "()I");
  186. FATAL_ERROR_IF(dialogstate == nullptr,
  187. "porting::getInputDialogState unable to find java dialog state method");
  188. return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate);
  189. }
  190. std::string getInputDialogValue()
  191. {
  192. jmethodID dialogvalue = jnienv->GetMethodID(nativeActivity,
  193. "getDialogValue", "()Ljava/lang/String;");
  194. FATAL_ERROR_IF(dialogvalue == nullptr,
  195. "porting::getInputDialogValue unable to find java dialog value method");
  196. jobject result = jnienv->CallObjectMethod(app_global->activity->clazz,
  197. dialogvalue);
  198. const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
  199. std::string text(javachars);
  200. jnienv->ReleaseStringUTFChars((jstring) result, javachars);
  201. return text;
  202. }
  203. #ifndef SERVER
  204. float getDisplayDensity()
  205. {
  206. static bool firstrun = true;
  207. static float value = 0;
  208. if (firstrun) {
  209. jmethodID getDensity = jnienv->GetMethodID(nativeActivity,
  210. "getDensity", "()F");
  211. FATAL_ERROR_IF(getDensity == nullptr,
  212. "porting::getDisplayDensity unable to find java getDensity method");
  213. value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity);
  214. firstrun = false;
  215. }
  216. return value;
  217. }
  218. v2u32 getDisplaySize()
  219. {
  220. static bool firstrun = true;
  221. static v2u32 retval;
  222. if (firstrun) {
  223. jmethodID getDisplayWidth = jnienv->GetMethodID(nativeActivity,
  224. "getDisplayWidth", "()I");
  225. FATAL_ERROR_IF(getDisplayWidth == nullptr,
  226. "porting::getDisplayWidth unable to find java getDisplayWidth method");
  227. retval.X = jnienv->CallIntMethod(app_global->activity->clazz,
  228. getDisplayWidth);
  229. jmethodID getDisplayHeight = jnienv->GetMethodID(nativeActivity,
  230. "getDisplayHeight", "()I");
  231. FATAL_ERROR_IF(getDisplayHeight == nullptr,
  232. "porting::getDisplayHeight unable to find java getDisplayHeight method");
  233. retval.Y = jnienv->CallIntMethod(app_global->activity->clazz,
  234. getDisplayHeight);
  235. firstrun = false;
  236. }
  237. return retval;
  238. }
  239. #endif // ndef SERVER
  240. }