3 Copyright (C) 2014 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #error This file may only be compiled for android!
24 #include "util/numeric.h"
26 #include "porting_android.h"
27 #include "threading/thread.h"
40 extern int main(int argc, char *argv[]);
42 void android_main(android_app *app)
45 porting::app_global = app;
47 Thread::setName("Main");
51 char *argv[] = {strdup(PROJECT_NAME), NULL};
52 main(ARRLEN(argv) - 1, argv);
54 } catch (std::exception &e) {
55 errorstream << "Uncaught exception in main thread: " << e.what() << std::endl;
58 errorstream << "Uncaught exception in main thread!" << std::endl;
62 porting::cleanupAndroid();
63 infostream << "Shutting down." << std::endl;
67 /* handler for finished message box input */
68 /* Intentionally NOT in namespace porting */
69 /* TODO this doesn't work as expected, no idea why but there's a workaround */
70 /* for it right now */
72 JNIEXPORT void JNICALL Java_net_minetest_MtNativeActivity_putMessageBoxResult(
73 JNIEnv * env, jclass thiz, jstring text)
75 errorstream << "Java_net_minetest_MtNativeActivity_putMessageBoxResult got: "
76 << std::string((const char*)env->GetStringChars(text,0))
83 std::string path_storage = DIR_DELIM "sdcard" DIR_DELIM;
85 android_app* app_global;
87 jclass nativeActivity;
89 jclass findClass(std::string classname)
95 jclass nativeactivity = jnienv->FindClass("android/app/NativeActivity");
96 jmethodID getClassLoader =
97 jnienv->GetMethodID(nativeactivity,"getClassLoader",
98 "()Ljava/lang/ClassLoader;");
100 jnienv->CallObjectMethod(app_global->activity->clazz, getClassLoader);
101 jclass classLoader = jnienv->FindClass("java/lang/ClassLoader");
102 jmethodID findClass =
103 jnienv->GetMethodID(classLoader, "loadClass",
104 "(Ljava/lang/String;)Ljava/lang/Class;");
105 jstring strClassName =
106 jnienv->NewStringUTF(classname.c_str());
107 return (jclass) jnienv->CallObjectMethod(cls, findClass, strClassName);
112 jmethodID assetcopy = jnienv->GetMethodID(nativeActivity,"copyAssets","()V");
114 if (assetcopy == 0) {
115 assert("porting::copyAssets unable to find copy assets method" == 0);
118 jnienv->CallVoidMethod(app_global->activity->clazz, assetcopy);
123 porting::jnienv = NULL;
124 JavaVM *jvm = app_global->activity->vm;
125 JavaVMAttachArgs lJavaVMAttachArgs;
126 lJavaVMAttachArgs.version = JNI_VERSION_1_6;
127 lJavaVMAttachArgs.name = PROJECT_NAME_C "NativeThread";
128 lJavaVMAttachArgs.group = NULL;
130 // This is a ugly hack as arm v7a non debuggable builds crash without this
131 // printf ... if someone finds out why please fix it!
132 infostream << "Attaching native thread. " << std::endl;
134 if ( jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
135 errorstream << "Failed to attach native thread to jvm" << std::endl;
139 nativeActivity = findClass("net/minetest/minetest/MtNativeActivity");
140 if (nativeActivity == 0) {
142 "porting::initAndroid unable to find java native activity class" <<
147 /* in the start-up code */
148 __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME_C,
149 "Initializing GPROF profiler");
150 monstartup("libminetest.so");
154 void cleanupAndroid()
158 errorstream << "Shutting down GPROF profiler" << std::endl;
159 setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1);
163 JavaVM *jvm = app_global->activity->vm;
164 jvm->DetachCurrentThread();
167 static std::string javaStringToUTF8(jstring js)
170 // Get string as a UTF-8 c-string
171 const char *c_str = jnienv->GetStringUTFChars(js, NULL);
174 // And free the c-string
175 jnienv->ReleaseStringUTFChars(js, c_str);
179 // Calls static method if obj is NULL
180 static std::string getAndroidPath(jclass cls, jobject obj, jclass cls_File,
181 jmethodID mt_getAbsPath, const char *getter)
186 mt_getter = jnienv->GetMethodID(cls, getter,
189 mt_getter = jnienv->GetStaticMethodID(cls, getter,
195 ob_file = jnienv->CallObjectMethod(obj, mt_getter);
197 ob_file = jnienv->CallStaticObjectMethod(cls, mt_getter);
199 // Call getAbsolutePath
200 jstring js_path = (jstring) jnienv->CallObjectMethod(ob_file,
203 return javaStringToUTF8(js_path);
206 void initializePathsAndroid()
208 // Get Environment class
209 jclass cls_Env = jnienv->FindClass("android/os/Environment");
211 jclass cls_File = jnienv->FindClass("java/io/File");
212 // Get getAbsolutePath method
213 jmethodID mt_getAbsPath = jnienv->GetMethodID(cls_File,
214 "getAbsolutePath", "()Ljava/lang/String;");
216 path_cache = getAndroidPath(nativeActivity, app_global->activity->clazz,
217 cls_File, mt_getAbsPath, "getCacheDir");
218 path_storage = getAndroidPath(cls_Env, NULL, cls_File, mt_getAbsPath,
219 "getExternalStorageDirectory");
220 path_user = path_storage + DIR_DELIM + PROJECT_NAME_C;
221 path_share = path_storage + DIR_DELIM + PROJECT_NAME_C;
226 void showInputDialog(const std::string& acceptButton, const std::string& hint,
227 const std::string& current, int editType)
229 jmethodID showdialog = jnienv->GetMethodID(nativeActivity,"showDialog",
230 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
232 if (showdialog == 0) {
233 assert("porting::showInputDialog unable to find java show dialog method" == 0);
236 jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str());
237 jstring jhint = jnienv->NewStringUTF(hint.c_str());
238 jstring jcurrent = jnienv->NewStringUTF(current.c_str());
239 jint jeditType = editType;
241 jnienv->CallVoidMethod(app_global->activity->clazz, showdialog,
242 jacceptButton, jhint, jcurrent, jeditType);
245 int getInputDialogState()
247 jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,
248 "getDialogState", "()I");
250 if (dialogstate == 0) {
251 assert("porting::getInputDialogState unable to find java dialog state method" == 0);
254 return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate);
257 std::string getInputDialogValue()
259 jmethodID dialogvalue = jnienv->GetMethodID(nativeActivity,
260 "getDialogValue", "()Ljava/lang/String;");
262 if (dialogvalue == 0) {
263 assert("porting::getInputDialogValue unable to find java dialog value method" == 0);
266 jobject result = jnienv->CallObjectMethod(app_global->activity->clazz,
269 const char* javachars = jnienv->GetStringUTFChars((jstring) result,0);
270 std::string text(javachars);
271 jnienv->ReleaseStringUTFChars((jstring) result, javachars);
277 float getDisplayDensity()
279 static bool firstrun = true;
280 static float value = 0;
283 jmethodID getDensity = jnienv->GetMethodID(nativeActivity, "getDensity",
286 if (getDensity == 0) {
287 assert("porting::getDisplayDensity unable to find java getDensity method" == 0);
290 value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity);
296 v2u32 getDisplaySize()
298 static bool firstrun = true;
302 jmethodID getDisplayWidth = jnienv->GetMethodID(nativeActivity,
303 "getDisplayWidth", "()I");
305 if (getDisplayWidth == 0) {
306 assert("porting::getDisplayWidth unable to find java getDisplayWidth method" == 0);
309 retval.X = jnienv->CallIntMethod(app_global->activity->clazz,
312 jmethodID getDisplayHeight = jnienv->GetMethodID(nativeActivity,
313 "getDisplayHeight", "()I");
315 if (getDisplayHeight == 0) {
316 assert("porting::getDisplayHeight unable to find java getDisplayHeight method" == 0);
319 retval.Y = jnienv->CallIntMethod(app_global->activity->clazz,
326 #endif // ndef SERVER