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