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