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