Translated using Weblate (Japanese)
[oweals/minetest.git] / src / porting_android.cpp
1 /*
2 Minetest
3 Copyright (C) 2014 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef __ANDROID__
21 #error This file may only be compiled for android!
22 #endif
23
24 #include "porting.h"
25 #include "porting_android.h"
26 #include "threading/thread.h"
27 #include "config.h"
28 #include "filesys.h"
29 #include "log.h"
30 #include <sstream>
31
32 #ifdef GPROF
33 #include "prof.h"
34 #endif
35
36 extern int main(int argc, char *argv[]);
37
38 void android_main(android_app *app)
39 {
40         int retval = 0;
41         porting::app_global = app;
42
43         Thread::setName("MainThread");
44
45         try {
46                 app_dummy();
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;
55                 retval = -1;
56         } catch (...) {
57                 __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME,
58                                 "An unknown exception occured!");
59                 errorstream << "Uncaught exception in main thread!" << std::endl;
60                 retval = -1;
61         }
62
63         porting::cleanupAndroid();
64         errorstream << "Shutting down." << std::endl;
65         exit(retval);
66 }
67
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 */
72 extern "C" {
73         JNIEXPORT void JNICALL Java_net_minetest_MtNativeActivity_putMessageBoxResult(
74                         JNIEnv * env, jclass thiz, jstring text)
75         {
76                 errorstream << "Java_net_minetest_MtNativeActivity_putMessageBoxResult got: "
77                                 << std::string((const char*)env->GetStringChars(text,0))
78                                 << std::endl;
79         }
80 }
81
82 namespace porting {
83
84 std::string path_storage = DIR_DELIM "sdcard" DIR_DELIM;
85
86 android_app* app_global;
87 JNIEnv*      jnienv;
88 jclass       nativeActivity;
89
90 jclass findClass(std::string classname)
91 {
92         if (jnienv == 0) {
93                 return 0;
94         }
95
96         jclass nativeactivity = jnienv->FindClass("android/app/NativeActivity");
97         jmethodID getClassLoader =
98                         jnienv->GetMethodID(nativeactivity,"getClassLoader",
99                                         "()Ljava/lang/ClassLoader;");
100         jobject cls =
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);
109 }
110
111 void copyAssets()
112 {
113         jmethodID assetcopy = jnienv->GetMethodID(nativeActivity,"copyAssets","()V");
114
115         if (assetcopy == 0) {
116                 assert("porting::copyAssets unable to find copy assets method" == 0);
117         }
118
119         jnienv->CallVoidMethod(app_global->activity->clazz, assetcopy);
120 }
121
122 void initAndroid()
123 {
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;
130 #ifdef NDEBUG
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;
134 #endif
135         if ( jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
136                 errorstream << "Failed to attach native thread to jvm" << std::endl;
137                 exit(-1);
138         }
139
140         nativeActivity = findClass("net/minetest/minetest/MtNativeActivity");
141         if (nativeActivity == 0) {
142                 errorstream <<
143                         "porting::initAndroid unable to find java native activity class" <<
144                         std::endl;
145         }
146
147 #ifdef GPROF
148         /* in the start-up code */
149         __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME,
150                         "Initializing GPROF profiler");
151         monstartup("libminetest.so");
152 #endif
153 }
154
155 void cleanupAndroid()
156 {
157
158 #ifdef GPROF
159         errorstream << "Shutting down GPROF profiler" << std::endl;
160         setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1);
161         moncleanup();
162 #endif
163
164         JavaVM *jvm = app_global->activity->vm;
165         jvm->DetachCurrentThread();
166 }
167
168 void setExternalStorageDir(JNIEnv* lJNIEnv)
169 {
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");
177
178         jmethodID MethodPath =
179                         lJNIEnv->GetMethodID(ClassFile, "getAbsolutePath",
180                                         "()Ljava/lang/String;");
181         jstring StringPath   =
182                         (jstring) lJNIEnv->CallObjectMethod(ObjectFile, MethodPath);
183
184         const char *externalPath = lJNIEnv->GetStringUTFChars(StringPath, NULL);
185         std::string userPath(externalPath);
186         lJNIEnv->ReleaseStringUTFChars(StringPath, externalPath);
187
188         path_storage             = userPath;
189         path_user                = userPath + DIR_DELIM + PROJECT_NAME;
190         path_share               = userPath + DIR_DELIM + PROJECT_NAME;
191 }
192
193 void showInputDialog(const std::string& acceptButton, const  std::string& hint,
194                 const std::string& current, int editType)
195 {
196         jmethodID showdialog = jnienv->GetMethodID(nativeActivity,"showDialog",
197                 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
198
199         if (showdialog == 0) {
200                 assert("porting::showInputDialog unable to find java show dialog method" == 0);
201         }
202
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;
207
208         jnienv->CallVoidMethod(app_global->activity->clazz, showdialog,
209                         jacceptButton, jhint, jcurrent, jeditType);
210 }
211
212 int getInputDialogState()
213 {
214         jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,
215                         "getDialogState", "()I");
216
217         if (dialogstate == 0) {
218                 assert("porting::getInputDialogState unable to find java dialog state method" == 0);
219         }
220
221         return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate);
222 }
223
224 std::string getInputDialogValue()
225 {
226         jmethodID dialogvalue = jnienv->GetMethodID(nativeActivity,
227                         "getDialogValue", "()Ljava/lang/String;");
228
229         if (dialogvalue == 0) {
230                 assert("porting::getInputDialogValue unable to find java dialog value method" == 0);
231         }
232
233         jobject result = jnienv->CallObjectMethod(app_global->activity->clazz,
234                         dialogvalue);
235
236         const char* javachars = jnienv->GetStringUTFChars((jstring) result,0);
237         std::string text(javachars);
238         jnienv->ReleaseStringUTFChars((jstring) result, javachars);
239
240         return text;
241 }
242
243 #if not defined(SERVER)
244 float getDisplayDensity()
245 {
246         static bool firstrun = true;
247         static float value = 0;
248
249         if (firstrun) {
250                 jmethodID getDensity = jnienv->GetMethodID(nativeActivity, "getDensity",
251                                         "()F");
252
253                 if (getDensity == 0) {
254                         assert("porting::getDisplayDensity unable to find java getDensity method" == 0);
255                 }
256
257                 value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity);
258                 firstrun = false;
259         }
260         return value;
261 }
262
263 v2u32 getDisplaySize()
264 {
265         static bool firstrun = true;
266         static v2u32 retval;
267
268         if (firstrun) {
269                 jmethodID getDisplayWidth = jnienv->GetMethodID(nativeActivity,
270                                 "getDisplayWidth", "()I");
271
272                 if (getDisplayWidth == 0) {
273                         assert("porting::getDisplayWidth unable to find java getDisplayWidth method" == 0);
274                 }
275
276                 retval.X = jnienv->CallIntMethod(app_global->activity->clazz,
277                                 getDisplayWidth);
278
279                 jmethodID getDisplayHeight = jnienv->GetMethodID(nativeActivity,
280                                 "getDisplayHeight", "()I");
281
282                 if (getDisplayHeight == 0) {
283                         assert("porting::getDisplayHeight unable to find java getDisplayHeight method" == 0);
284                 }
285
286                 retval.Y = jnienv->CallIntMethod(app_global->activity->clazz,
287                                 getDisplayHeight);
288
289                 firstrun = false;
290         }
291         return retval;
292 }
293 #endif //SERVER
294 }