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"
35 extern int main(int argc, char *argv[]);
37 void android_main(android_app *app)
40 porting::app_global = app;
42 porting::setThreadName("MainThread");
46 char *argv[] = { (char*) "minetest" };
47 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;
58 __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME,
59 "Some exception occured");
60 errorstream << "Uncaught exception in main thread!" << std::endl;
64 porting::cleanupAndroid();
65 errorstream << "Shutting down minetest." << std::endl;
69 /* handler for finished message box input */
70 /* Intentionally NOT in namespace porting */
71 /* TODO this doesn't work as expected, no idea why but there's a workaround */
72 /* for it right now */
74 JNIEXPORT void JNICALL Java_net_minetest_MtNativeActivity_putMessageBoxResult(
75 JNIEnv * env, jclass thiz, jstring text)
77 errorstream << "Java_net_minetest_MtNativeActivity_putMessageBoxResult got: "
78 << std::string((const char*)env->GetStringChars(text,0))
85 std::string path_storage = DIR_DELIM "sdcard" DIR_DELIM;
87 android_app* app_global;
89 jclass nativeActivity;
91 jclass findClass(std::string classname)
97 jclass nativeactivity = jnienv->FindClass("android/app/NativeActivity");
98 jmethodID getClassLoader =
99 jnienv->GetMethodID(nativeactivity,"getClassLoader",
100 "()Ljava/lang/ClassLoader;");
102 jnienv->CallObjectMethod(app_global->activity->clazz, getClassLoader);
103 jclass classLoader = jnienv->FindClass("java/lang/ClassLoader");
104 jmethodID findClass =
105 jnienv->GetMethodID(classLoader, "loadClass",
106 "(Ljava/lang/String;)Ljava/lang/Class;");
107 jstring strClassName =
108 jnienv->NewStringUTF(classname.c_str());
109 return (jclass) jnienv->CallObjectMethod(cls, findClass, strClassName);
114 jmethodID assetcopy = jnienv->GetMethodID(nativeActivity,"copyAssets","()V");
116 if (assetcopy == 0) {
117 assert("porting::copyAssets unable to find copy assets method" == 0);
120 jnienv->CallVoidMethod(app_global->activity->clazz, assetcopy);
125 porting::jnienv = NULL;
126 JavaVM *jvm = app_global->activity->vm;
127 JavaVMAttachArgs lJavaVMAttachArgs;
128 lJavaVMAttachArgs.version = JNI_VERSION_1_6;
129 lJavaVMAttachArgs.name = "MinetestNativeThread";
130 lJavaVMAttachArgs.group = NULL;
132 // This is a ugly hack as arm v7a non debuggable builds crash without this
133 // printf ... if someone finds out why please fix it!
134 infostream << "Attaching native thread. " << std::endl;
136 if ( jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
137 errorstream << "Failed to attach native thread to jvm" << std::endl;
141 nativeActivity = findClass("net/minetest/minetest/MtNativeActivity");
142 if (nativeActivity == 0) {
144 "porting::initAndroid unable to find java native activity class" <<
149 /* in the start-up code */
150 __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME,
151 "Initializing GPROF profiler");
152 monstartup("libminetest.so");
156 void cleanupAndroid()
160 errorstream << "Shutting down GPROF profiler" << std::endl;
161 setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1);
165 JavaVM *jvm = app_global->activity->vm;
166 jvm->DetachCurrentThread();
169 void setExternalStorageDir(JNIEnv* lJNIEnv)
171 // Android: Retrieve ablsolute path to external storage device (sdcard)
172 jclass ClassEnv = lJNIEnv->FindClass("android/os/Environment");
173 jmethodID MethodDir =
174 lJNIEnv->GetStaticMethodID(ClassEnv,
175 "getExternalStorageDirectory","()Ljava/io/File;");
176 jobject ObjectFile = lJNIEnv->CallStaticObjectMethod(ClassEnv, MethodDir);
177 jclass ClassFile = lJNIEnv->FindClass("java/io/File");
179 jmethodID MethodPath =
180 lJNIEnv->GetMethodID(ClassFile, "getAbsolutePath",
181 "()Ljava/lang/String;");
183 (jstring) lJNIEnv->CallObjectMethod(ObjectFile, MethodPath);
185 const char *externalPath = lJNIEnv->GetStringUTFChars(StringPath, NULL);
186 std::string userPath(externalPath);
187 lJNIEnv->ReleaseStringUTFChars(StringPath, externalPath);
189 path_storage = userPath;
190 path_user = userPath + DIR_DELIM + PROJECT_NAME;
191 path_share = userPath + DIR_DELIM + PROJECT_NAME;
194 void showInputDialog(const std::string& acceptButton, const std::string& hint,
195 const std::string& current, int editType)
197 jmethodID showdialog = jnienv->GetMethodID(nativeActivity,"showDialog",
198 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
200 if (showdialog == 0) {
201 assert("porting::showInputDialog unable to find java show dialog method" == 0);
204 jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str());
205 jstring jhint = jnienv->NewStringUTF(hint.c_str());
206 jstring jcurrent = jnienv->NewStringUTF(current.c_str());
207 jint jeditType = editType;
209 jnienv->CallVoidMethod(app_global->activity->clazz, showdialog,
210 jacceptButton, jhint, jcurrent, jeditType);
213 int getInputDialogState()
215 jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,
216 "getDialogState", "()I");
218 if (dialogstate == 0) {
219 assert("porting::getInputDialogState unable to find java dialog state method" == 0);
222 return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate);
225 std::string getInputDialogValue()
227 jmethodID dialogvalue = jnienv->GetMethodID(nativeActivity,
228 "getDialogValue", "()Ljava/lang/String;");
230 if (dialogvalue == 0) {
231 assert("porting::getInputDialogValue unable to find java dialog value method" == 0);
234 jobject result = jnienv->CallObjectMethod(app_global->activity->clazz,
237 const char* javachars = jnienv->GetStringUTFChars((jstring) result,0);
238 std::string text(javachars);
239 jnienv->ReleaseStringUTFChars((jstring) result, javachars);
244 #if not defined(SERVER)
245 float getDisplayDensity()
247 static bool firstrun = true;
248 static float value = 0;
251 jmethodID getDensity = jnienv->GetMethodID(nativeActivity, "getDensity",
254 if (getDensity == 0) {
255 assert("porting::getDisplayDensity unable to find java getDensity method" == 0);
258 value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity);
264 v2u32 getDisplaySize()
266 static bool firstrun = true;
270 jmethodID getDisplayWidth = jnienv->GetMethodID(nativeActivity,
271 "getDisplayWidth", "()I");
273 if (getDisplayWidth == 0) {
274 assert("porting::getDisplayWidth unable to find java getDisplayWidth method" == 0);
277 retval.X = jnienv->CallIntMethod(app_global->activity->clazz,
280 jmethodID getDisplayHeight = jnienv->GetMethodID(nativeActivity,
281 "getDisplayHeight", "()I");
283 if (getDisplayHeight == 0) {
284 assert("porting::getDisplayHeight unable to find java getDisplayHeight method" == 0);
287 retval.Y = jnienv->CallIntMethod(app_global->activity->clazz,