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!
25 #include "porting_android.h"
26 #include "threading/thread.h"
36 extern int main(int argc, char *argv[]);
38 void android_main(android_app *app)
41 porting::app_global = app;
43 Thread::setName("MainThread");
47 char *argv[] = {(char*) "minetest"};
48 main(sizeof(argv) / sizeof(argv[0]), argv);
49 } catch (BaseException &e) {
50 std::stringstream msg;
51 msg << "Exception handled by main: " << e.what();
52 const char *message = msg.str().c_str();
53 __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME, "%s", message);
54 errorstream << msg << std::endl;
57 __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME,
58 "An unknown exception occured!");
59 errorstream << "Uncaught exception in main thread!" << std::endl;
63 porting::cleanupAndroid();
64 errorstream << "Shutting down." << std::endl;
68 /* handler for finished message box input */
69 /* Intentionally NOT in namespace porting */
70 /* TODO this doesn't work as expected, no idea why but there's a workaround */
71 /* for it right now */
73 JNIEXPORT void JNICALL Java_net_minetest_MtNativeActivity_putMessageBoxResult(
74 JNIEnv * env, jclass thiz, jstring text)
76 errorstream << "Java_net_minetest_MtNativeActivity_putMessageBoxResult got: "
77 << std::string((const char*)env->GetStringChars(text,0))
84 std::string path_storage = DIR_DELIM "sdcard" DIR_DELIM;
86 android_app* app_global;
88 jclass nativeActivity;
90 jclass findClass(std::string classname)
96 jclass nativeactivity = jnienv->FindClass("android/app/NativeActivity");
97 jmethodID getClassLoader =
98 jnienv->GetMethodID(nativeactivity,"getClassLoader",
99 "()Ljava/lang/ClassLoader;");
101 jnienv->CallObjectMethod(app_global->activity->clazz, getClassLoader);
102 jclass classLoader = jnienv->FindClass("java/lang/ClassLoader");
103 jmethodID findClass =
104 jnienv->GetMethodID(classLoader, "loadClass",
105 "(Ljava/lang/String;)Ljava/lang/Class;");
106 jstring strClassName =
107 jnienv->NewStringUTF(classname.c_str());
108 return (jclass) jnienv->CallObjectMethod(cls, findClass, strClassName);
113 jmethodID assetcopy = jnienv->GetMethodID(nativeActivity,"copyAssets","()V");
115 if (assetcopy == 0) {
116 assert("porting::copyAssets unable to find copy assets method" == 0);
119 jnienv->CallVoidMethod(app_global->activity->clazz, assetcopy);
124 porting::jnienv = NULL;
125 JavaVM *jvm = app_global->activity->vm;
126 JavaVMAttachArgs lJavaVMAttachArgs;
127 lJavaVMAttachArgs.version = JNI_VERSION_1_6;
128 lJavaVMAttachArgs.name = "MinetestNativeThread";
129 lJavaVMAttachArgs.group = NULL;
131 // This is a ugly hack as arm v7a non debuggable builds crash without this
132 // printf ... if someone finds out why please fix it!
133 infostream << "Attaching native thread. " << std::endl;
135 if ( jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
136 errorstream << "Failed to attach native thread to jvm" << std::endl;
140 nativeActivity = findClass("net/minetest/minetest/MtNativeActivity");
141 if (nativeActivity == 0) {
143 "porting::initAndroid unable to find java native activity class" <<
148 /* in the start-up code */
149 __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME,
150 "Initializing GPROF profiler");
151 monstartup("libminetest.so");
155 void cleanupAndroid()
159 errorstream << "Shutting down GPROF profiler" << std::endl;
160 setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1);
164 JavaVM *jvm = app_global->activity->vm;
165 jvm->DetachCurrentThread();
168 void setExternalStorageDir(JNIEnv* lJNIEnv)
170 // Android: Retrieve ablsolute path to external storage device (sdcard)
171 jclass ClassEnv = lJNIEnv->FindClass("android/os/Environment");
172 jmethodID MethodDir =
173 lJNIEnv->GetStaticMethodID(ClassEnv,
174 "getExternalStorageDirectory","()Ljava/io/File;");
175 jobject ObjectFile = lJNIEnv->CallStaticObjectMethod(ClassEnv, MethodDir);
176 jclass ClassFile = lJNIEnv->FindClass("java/io/File");
178 jmethodID MethodPath =
179 lJNIEnv->GetMethodID(ClassFile, "getAbsolutePath",
180 "()Ljava/lang/String;");
182 (jstring) lJNIEnv->CallObjectMethod(ObjectFile, MethodPath);
184 const char *externalPath = lJNIEnv->GetStringUTFChars(StringPath, NULL);
185 std::string userPath(externalPath);
186 lJNIEnv->ReleaseStringUTFChars(StringPath, externalPath);
188 path_storage = userPath;
189 path_user = userPath + DIR_DELIM + PROJECT_NAME;
190 path_share = userPath + DIR_DELIM + PROJECT_NAME;
193 void showInputDialog(const std::string& acceptButton, const std::string& hint,
194 const std::string& current, int editType)
196 jmethodID showdialog = jnienv->GetMethodID(nativeActivity,"showDialog",
197 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
199 if (showdialog == 0) {
200 assert("porting::showInputDialog unable to find java show dialog method" == 0);
203 jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str());
204 jstring jhint = jnienv->NewStringUTF(hint.c_str());
205 jstring jcurrent = jnienv->NewStringUTF(current.c_str());
206 jint jeditType = editType;
208 jnienv->CallVoidMethod(app_global->activity->clazz, showdialog,
209 jacceptButton, jhint, jcurrent, jeditType);
212 int getInputDialogState()
214 jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,
215 "getDialogState", "()I");
217 if (dialogstate == 0) {
218 assert("porting::getInputDialogState unable to find java dialog state method" == 0);
221 return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate);
224 std::string getInputDialogValue()
226 jmethodID dialogvalue = jnienv->GetMethodID(nativeActivity,
227 "getDialogValue", "()Ljava/lang/String;");
229 if (dialogvalue == 0) {
230 assert("porting::getInputDialogValue unable to find java dialog value method" == 0);
233 jobject result = jnienv->CallObjectMethod(app_global->activity->clazz,
236 const char* javachars = jnienv->GetStringUTFChars((jstring) result,0);
237 std::string text(javachars);
238 jnienv->ReleaseStringUTFChars((jstring) result, javachars);
243 #if not defined(SERVER)
244 float getDisplayDensity()
246 static bool firstrun = true;
247 static float value = 0;
250 jmethodID getDensity = jnienv->GetMethodID(nativeActivity, "getDensity",
253 if (getDensity == 0) {
254 assert("porting::getDisplayDensity unable to find java getDensity method" == 0);
257 value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity);
263 v2u32 getDisplaySize()
265 static bool firstrun = true;
269 jmethodID getDisplayWidth = jnienv->GetMethodID(nativeActivity,
270 "getDisplayWidth", "()I");
272 if (getDisplayWidth == 0) {
273 assert("porting::getDisplayWidth unable to find java getDisplayWidth method" == 0);
276 retval.X = jnienv->CallIntMethod(app_global->activity->clazz,
279 jmethodID getDisplayHeight = jnienv->GetMethodID(nativeActivity,
280 "getDisplayHeight", "()I");
282 if (getDisplayHeight == 0) {
283 assert("porting::getDisplayHeight unable to find java getDisplayHeight method" == 0);
286 retval.Y = jnienv->CallIntMethod(app_global->activity->clazz,