Fix mouse events sent to wrong GUI elements when dragging
[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 "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                 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 static std::string javaStringToUTF8(jstring js)
168 {
169         std::string str;
170         // Get string as a UTF-8 c-string
171         const char *c_str = jnienv->GetStringUTFChars(js, NULL);
172         // Save it
173         str = c_str;
174         // And free the c-string
175         jnienv->ReleaseStringUTFChars(js, c_str);
176         return str;
177 }
178
179 // Calls static method if obj is NULL
180 static std::string getAndroidPath(jclass cls, jobject obj, jclass cls_File,
181                 jmethodID mt_getAbsPath, const char *getter)
182 {
183         // Get getter method
184         jmethodID mt_getter;
185         if (obj)
186                 mt_getter = jnienv->GetMethodID(cls, getter,
187                                 "()Ljava/io/File;");
188         else
189                 mt_getter = jnienv->GetStaticMethodID(cls, getter,
190                                 "()Ljava/io/File;");
191
192         // Call getter
193         jobject ob_file;
194         if (obj)
195                 ob_file = jnienv->CallObjectMethod(obj, mt_getter);
196         else
197                 ob_file = jnienv->CallStaticObjectMethod(cls, mt_getter);
198
199         // Call getAbsolutePath
200         jstring js_path = (jstring) jnienv->CallObjectMethod(ob_file,
201                         mt_getAbsPath);
202
203         return javaStringToUTF8(js_path);
204 }
205
206 void initializePathsAndroid()
207 {
208         // Get Environment class
209         jclass cls_Env = jnienv->FindClass("android/os/Environment");
210         // Get File class
211         jclass cls_File = jnienv->FindClass("java/io/File");
212         // Get getAbsolutePath method
213         jmethodID mt_getAbsPath = jnienv->GetMethodID(cls_File,
214                                 "getAbsolutePath", "()Ljava/lang/String;");
215
216         path_cache   = getAndroidPath(nativeActivity, app_global->activity->clazz,
217                         cls_File, mt_getAbsPath, "getCacheDir");
218         path_storage = getAndroidPath(cls_Env, NULL, cls_File, mt_getAbsPath,
219                         "getExternalStorageDirectory");
220         path_user    = path_storage + DIR_DELIM + PROJECT_NAME_C;
221         path_share   = path_storage + DIR_DELIM + PROJECT_NAME_C;
222
223         migrateCachePath();
224 }
225
226 void showInputDialog(const std::string& acceptButton, const  std::string& hint,
227                 const std::string& current, int editType)
228 {
229         jmethodID showdialog = jnienv->GetMethodID(nativeActivity,"showDialog",
230                 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
231
232         if (showdialog == 0) {
233                 assert("porting::showInputDialog unable to find java show dialog method" == 0);
234         }
235
236         jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str());
237         jstring jhint         = jnienv->NewStringUTF(hint.c_str());
238         jstring jcurrent      = jnienv->NewStringUTF(current.c_str());
239         jint    jeditType     = editType;
240
241         jnienv->CallVoidMethod(app_global->activity->clazz, showdialog,
242                         jacceptButton, jhint, jcurrent, jeditType);
243 }
244
245 int getInputDialogState()
246 {
247         jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,
248                         "getDialogState", "()I");
249
250         if (dialogstate == 0) {
251                 assert("porting::getInputDialogState unable to find java dialog state method" == 0);
252         }
253
254         return jnienv->CallIntMethod(app_global->activity->clazz, dialogstate);
255 }
256
257 std::string getInputDialogValue()
258 {
259         jmethodID dialogvalue = jnienv->GetMethodID(nativeActivity,
260                         "getDialogValue", "()Ljava/lang/String;");
261
262         if (dialogvalue == 0) {
263                 assert("porting::getInputDialogValue unable to find java dialog value method" == 0);
264         }
265
266         jobject result = jnienv->CallObjectMethod(app_global->activity->clazz,
267                         dialogvalue);
268
269         const char* javachars = jnienv->GetStringUTFChars((jstring) result,0);
270         std::string text(javachars);
271         jnienv->ReleaseStringUTFChars((jstring) result, javachars);
272
273         return text;
274 }
275
276 #ifndef SERVER
277 float getDisplayDensity()
278 {
279         static bool firstrun = true;
280         static float value = 0;
281
282         if (firstrun) {
283                 jmethodID getDensity = jnienv->GetMethodID(nativeActivity, "getDensity",
284                                         "()F");
285
286                 if (getDensity == 0) {
287                         assert("porting::getDisplayDensity unable to find java getDensity method" == 0);
288                 }
289
290                 value = jnienv->CallFloatMethod(app_global->activity->clazz, getDensity);
291                 firstrun = false;
292         }
293         return value;
294 }
295
296 v2u32 getDisplaySize()
297 {
298         static bool firstrun = true;
299         static v2u32 retval;
300
301         if (firstrun) {
302                 jmethodID getDisplayWidth = jnienv->GetMethodID(nativeActivity,
303                                 "getDisplayWidth", "()I");
304
305                 if (getDisplayWidth == 0) {
306                         assert("porting::getDisplayWidth unable to find java getDisplayWidth method" == 0);
307                 }
308
309                 retval.X = jnienv->CallIntMethod(app_global->activity->clazz,
310                                 getDisplayWidth);
311
312                 jmethodID getDisplayHeight = jnienv->GetMethodID(nativeActivity,
313                                 "getDisplayHeight", "()I");
314
315                 if (getDisplayHeight == 0) {
316                         assert("porting::getDisplayHeight unable to find java getDisplayHeight method" == 0);
317                 }
318
319                 retval.Y = jnienv->CallIntMethod(app_global->activity->clazz,
320                                 getDisplayHeight);
321
322                 firstrun = false;
323         }
324         return retval;
325 }
326 #endif // ndef SERVER
327 }