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