]> git.lizzy.rs Git - minetest.git/blob - src/porting_android.cpp
Re-enable verbose logging on Android
[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 <cstdlib>
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         char *argv[] = {strdup(PROJECT_NAME), strdup("--verbose"), nullptr};
50         try {
51                 main(ARRLEN(argv) - 1, argv);
52         } catch (std::exception &e) {
53                 errorstream << "Uncaught exception in main thread: " << e.what() << std::endl;
54                 retval = -1;
55         } catch (...) {
56                 errorstream << "Uncaught exception in main thread!" << std::endl;
57                 retval = -1;
58         }
59         free(argv[0]);
60         free(argv[1]);
61
62         porting::cleanupAndroid();
63         infostream << "Shutting down." << std::endl;
64         exit(retval);
65 }
66
67 /**
68  * Handler for finished message box input
69  * Intentionally NOT in namespace porting
70  * ToDo: this doesn't work as expected, there's a workaround for it right now
71  */
72 extern "C" {
73         JNIEXPORT void JNICALL Java_net_minetest_minetest_GameActivity_putMessageBoxResult(
74                         JNIEnv *env, jclass thiz, jstring text)
75         {
76                 errorstream <<
77                         "Java_net_minetest_minetest_GameActivity_putMessageBoxResult got: " <<
78                         std::string((const char*) env->GetStringChars(text, nullptr)) << std::endl;
79         }
80 }
81
82 namespace porting {
83 android_app *app_global;
84 JNIEnv      *jnienv;
85 jclass       nativeActivity;
86
87 jclass findClass(const std::string &classname)
88 {
89         if (jnienv == nullptr)
90                 return nullptr;
91
92         jclass nativeactivity = jnienv->FindClass("android/app/NativeActivity");
93         jmethodID getClassLoader = jnienv->GetMethodID(
94                         nativeactivity, "getClassLoader", "()Ljava/lang/ClassLoader;");
95         jobject cls = jnienv->CallObjectMethod(
96                                                 app_global->activity->clazz, getClassLoader);
97         jclass classLoader = jnienv->FindClass("java/lang/ClassLoader");
98         jmethodID findClass = jnienv->GetMethodID(classLoader, "loadClass",
99                                         "(Ljava/lang/String;)Ljava/lang/Class;");
100         jstring strClassName = jnienv->NewStringUTF(classname.c_str());
101         return (jclass) jnienv->CallObjectMethod(cls, findClass, strClassName);
102 }
103
104 void initAndroid()
105 {
106         porting::jnienv = nullptr;
107         JavaVM *jvm = app_global->activity->vm;
108         JavaVMAttachArgs lJavaVMAttachArgs;
109         lJavaVMAttachArgs.version = JNI_VERSION_1_6;
110         lJavaVMAttachArgs.name = PROJECT_NAME_C "NativeThread";
111         lJavaVMAttachArgs.group = nullptr;
112
113         if (jvm->AttachCurrentThread(&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
114                 errorstream << "Failed to attach native thread to jvm" << std::endl;
115                 exit(-1);
116         }
117
118         nativeActivity = findClass("net/minetest/minetest/GameActivity");
119         if (nativeActivity == nullptr)
120                 errorstream <<
121                         "porting::initAndroid unable to find java native activity class" <<
122                         std::endl;
123
124 #ifdef GPROF
125         // in the start-up code
126         __android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME_C,
127                         "Initializing GPROF profiler");
128         monstartup("libMinetest.so");
129 #endif
130 }
131
132 void cleanupAndroid()
133 {
134 #ifdef GPROF
135         errorstream << "Shutting down GPROF profiler" << std::endl;
136         setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1);
137         moncleanup();
138 #endif
139
140         JavaVM *jvm = app_global->activity->vm;
141         jvm->DetachCurrentThread();
142 }
143
144 static std::string javaStringToUTF8(jstring js)
145 {
146         std::string str;
147         // Get string as a UTF-8 c-string
148         const char *c_str = jnienv->GetStringUTFChars(js, nullptr);
149         // Save it
150         str = c_str;
151         // And free the c-string
152         jnienv->ReleaseStringUTFChars(js, c_str);
153         return str;
154 }
155
156 void initializePathsAndroid()
157 {
158         // Set user and share paths
159         {
160                 jmethodID getUserDataPath = jnienv->GetMethodID(nativeActivity,
161                                 "getUserDataPath", "()Ljava/lang/String;");
162                 FATAL_ERROR_IF(getUserDataPath==nullptr,
163                                 "porting::initializePathsAndroid unable to find Java getUserDataPath method");
164                 jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, getUserDataPath);
165                 const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
166                 path_user = javachars;
167                 path_share = javachars;
168                 path_locale  = path_share + DIR_DELIM + "locale";
169                 jnienv->ReleaseStringUTFChars((jstring) result, javachars);
170         }
171
172         // Set cache path
173         {
174                 jmethodID getCachePath = jnienv->GetMethodID(nativeActivity,
175                                 "getCachePath", "()Ljava/lang/String;");
176                 FATAL_ERROR_IF(getCachePath==nullptr,
177                                 "porting::initializePathsAndroid unable to find Java getCachePath method");
178                 jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, getCachePath);
179                 const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
180                 path_cache = javachars;
181                 jnienv->ReleaseStringUTFChars((jstring) result, javachars);
182
183                 migrateCachePath();
184         }
185 }
186
187 void showInputDialog(const std::string &acceptButton, const std::string &hint,
188                 const std::string &current, int editType)
189 {
190         jmethodID showdialog = jnienv->GetMethodID(nativeActivity, "showDialog",
191                 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
192
193         FATAL_ERROR_IF(showdialog == nullptr,
194                 "porting::showInputDialog unable to find java show dialog method");
195
196         jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str());
197         jstring jhint         = jnienv->NewStringUTF(hint.c_str());
198         jstring jcurrent      = jnienv->NewStringUTF(current.c_str());
199         jint    jeditType     = editType;
200
201         jnienv->CallVoidMethod(app_global->activity->clazz, showdialog,
202                         jacceptButton, jhint, jcurrent, jeditType);
203 }
204
205 void openURIAndroid(const std::string &url)
206 {
207         jmethodID url_open = jnienv->GetMethodID(nativeActivity, "openURI",
208                 "(Ljava/lang/String;)V");
209
210         FATAL_ERROR_IF(url_open == nullptr,
211                 "porting::openURIAndroid unable to find java openURI method");
212
213         jstring jurl = jnienv->NewStringUTF(url.c_str());
214         jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl);
215 }
216
217 void shareFileAndroid(const std::string &path)
218 {
219         jmethodID url_open = jnienv->GetMethodID(nativeActivity, "shareFile",
220                         "(Ljava/lang/String;)V");
221
222         FATAL_ERROR_IF(url_open == nullptr,
223                         "porting::shareFileAndroid unable to find java openURI method");
224
225         jstring jurl = jnienv->NewStringUTF(path.c_str());
226         jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl);
227 }
228
229 int getInputDialogState()
230 {
231         jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,
232                         "getDialogState", "()I");
233
234         FATAL_ERROR_IF(dialogstate == nullptr,
235                 "porting::getInputDialogState unable to find java dialog state method");
236
237         return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate);
238 }
239
240 std::string getInputDialogValue()
241 {
242         jmethodID dialogvalue = jnienv->GetMethodID(nativeActivity,
243                         "getDialogValue", "()Ljava/lang/String;");
244
245         FATAL_ERROR_IF(dialogvalue == nullptr,
246                 "porting::getInputDialogValue unable to find java dialog value method");
247
248         jobject result = jnienv->CallObjectMethod(app_global->activity->clazz,
249                         dialogvalue);
250
251         const char *javachars = jnienv->GetStringUTFChars((jstring) result, nullptr);
252         std::string text(javachars);
253         jnienv->ReleaseStringUTFChars((jstring) result, javachars);
254
255         return text;
256 }
257
258 #ifndef SERVER
259 float getDisplayDensity()
260 {
261         static bool firstrun = true;
262         static float value = 0;
263
264         if (firstrun) {
265                 jmethodID getDensity = jnienv->GetMethodID(nativeActivity,
266                                 "getDensity", "()F");
267
268                 FATAL_ERROR_IF(getDensity == nullptr,
269                         "porting::getDisplayDensity unable to find java getDensity method");
270
271                 value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity);
272                 firstrun = false;
273         }
274         return value;
275 }
276
277 v2u32 getDisplaySize()
278 {
279         static bool firstrun = true;
280         static v2u32 retval;
281
282         if (firstrun) {
283                 jmethodID getDisplayWidth = jnienv->GetMethodID(nativeActivity,
284                                 "getDisplayWidth", "()I");
285
286                 FATAL_ERROR_IF(getDisplayWidth == nullptr,
287                         "porting::getDisplayWidth unable to find java getDisplayWidth method");
288
289                 retval.X = jnienv->CallIntMethod(app_global->activity->clazz,
290                                 getDisplayWidth);
291
292                 jmethodID getDisplayHeight = jnienv->GetMethodID(nativeActivity,
293                                 "getDisplayHeight", "()I");
294
295                 FATAL_ERROR_IF(getDisplayHeight == nullptr,
296                         "porting::getDisplayHeight unable to find java getDisplayHeight method");
297
298                 retval.Y = jnienv->CallIntMethod(app_global->activity->clazz,
299                                 getDisplayHeight);
300
301                 firstrun = false;
302         }
303         return retval;
304 }
305 #endif // ndef SERVER
306 }