android: make code 're-entrant' - i.e. NativeActivity can restart the program without...
[freeglut] / src / android / fg_runtime_android.c
1 /*
2  * fg_runtime_android.c
3  *
4  * Android runtime
5  *
6  * Copyright (C) 2012  Sylvain Beucler
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 /* Parts taken from Android NDK's 'native-activity' sample: */
27 /*
28  * Copyright (C) 2010 The Android Open Source Project
29  * 
30  * Licensed under the Apache License, Version 2.0 (the "License");
31  * you may not use this file except in compliance with the License.
32  * You may obtain a copy of the License at
33  *
34  *      http://www.apache.org/licenses/LICENSE-2.0
35  *
36  * Unless required by applicable law or agreed to in writing, software
37  * distributed under the License is distributed on an "AS IS" BASIS,
38  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39  * See the License for the specific language governing permissions and
40  * limitations under the License.
41  *
42  */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <jni.h>
48 #include <android/log.h>
49 #include <android/asset_manager.h>
50 #include <android/native_window.h>
51 #include "android/native_app_glue/android_native_app_glue.h"
52
53 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "FreeGLUT", __VA_ARGS__))
54 #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "FreeGLUT", __VA_ARGS__))
55
56 /* Cf. freeglut_main_android.c */
57 extern int32_t handle_input(struct android_app* app, AInputEvent* event);
58 extern void handle_cmd(struct android_app* app, int32_t cmd);
59
60 extern int main(int argc, char* argv[]);
61
62 /** NativeActivity Callbacks **/
63 /* Caution: they are called in the native_activity thread, not the
64    FreeGLUT thread. Use android_app_write_cmd. */
65
66 /* Could be used instead of onNativeWindowRedrawNeeded */
67 /* Deals with status bar presence */
68 static void onContentRectChanged(ANativeActivity* activity, const ARect* rect) {
69   LOGI("onContentRectChanged: l=%d,t=%d,r=%d,b=%d", rect->left, rect->top, rect->right, rect->bottom);
70 }
71
72 /* Bug: not called during a resize in android-9, only once on startup :/ */
73 static void onNativeWindowResized(ANativeActivity* activity, ANativeWindow* window) {
74   LOGI("onNativeWindowResized: %p\n", (void*)activity);
75 }
76
77 /* Called after a resize, compensate broken onNativeWindowResized */
78 static void onNativeWindowRedrawNeeded(ANativeActivity* activity, ANativeWindow* window) {
79   LOGI("onNativeWindowRedrawNeeded: %p\n", (void*)activity);
80   struct android_app* app = (struct android_app*)activity->instance;
81   android_app_write_cmd(app, APP_CMD_WINDOW_RESIZED);
82 }
83
84 /**
85  * Extract all .apk assets to the application directory so they can be
86  * accessed using accessed.
87  * TODO: parse directories recursively
88  */
89 static void extract_assets(struct android_app* app) {
90   /* Get usable JNI context */
91   JNIEnv* env = app->activity->env;
92   JavaVM* vm = app->activity->vm;
93   (*vm)->AttachCurrentThread(vm, &env, NULL);
94   
95   {
96     /* Get a handle on our calling NativeActivity class */
97     jclass activityClass = (*env)->GetObjectClass(env, app->activity->clazz);
98     
99     /* Get path to cache dir (/data/data/org.myapp/cache) */
100     jmethodID getCacheDir = (*env)->GetMethodID(env, activityClass, "getCacheDir", "()Ljava/io/File;");
101     jobject file = (*env)->CallObjectMethod(env, app->activity->clazz, getCacheDir);
102     jclass fileClass = (*env)->FindClass(env, "java/io/File");
103     jmethodID getAbsolutePath = (*env)->GetMethodID(env, fileClass, "getAbsolutePath", "()Ljava/lang/String;");
104     jstring jpath = (jstring)(*env)->CallObjectMethod(env, file, getAbsolutePath);
105     const char* app_dir = (*env)->GetStringUTFChars(env, jpath, NULL);
106     
107     /* chdir in the application cache directory */
108     LOGI("app_dir: %s", app_dir);
109     chdir(app_dir);
110     (*env)->ReleaseStringUTFChars(env, jpath, app_dir);
111     
112     /* Pre-extract assets, to avoid Android-specific file opening */
113     {
114       AAssetManager* mgr = app->activity->assetManager;
115       AAssetDir* assetDir = AAssetManager_openDir(mgr, "");
116       const char* filename = (const char*)NULL;
117       while ((filename = AAssetDir_getNextFileName(assetDir)) != NULL) {
118         AAsset* asset = AAssetManager_open(mgr, filename, AASSET_MODE_STREAMING);
119         char buf[BUFSIZ];
120         int nb_read = 0;
121         FILE* out = fopen(filename, "w");
122         while ((nb_read = AAsset_read(asset, buf, BUFSIZ)) > 0)
123           fwrite(buf, nb_read, 1, out);
124         fclose(out);
125         AAsset_close(asset);
126       }
127       AAssetDir_close(assetDir);
128     }
129   }
130
131   (*vm)->DetachCurrentThread(vm);
132 }
133
134 /**
135  * This is the main entry point of a native application that is using
136  * android_native_app_glue.  It runs in its own thread, with its own
137  * event loop for receiving input events and doing other things.
138  */
139 void android_main(struct android_app* app) {
140   LOGI("android_main");
141
142   /* Register window resize callback */
143   app->activity->callbacks->onNativeWindowResized = onNativeWindowResized;
144   app->activity->callbacks->onContentRectChanged = onContentRectChanged;
145   app->activity->callbacks->onNativeWindowRedrawNeeded = onNativeWindowRedrawNeeded;
146   
147   app->onAppCmd = handle_cmd;
148   app->onInputEvent = handle_input;
149
150   extract_assets(app);
151
152   /* Call user's main */
153   {
154     char progname[5] = "self";
155     char* argv[] = {progname, NULL};
156     main(1, argv);
157     /* FreeGLUT will exit() by itself if
158        GLUT_ACTION_ON_WINDOW_CLOSE == GLUT_ACTION_EXIT */
159   }
160
161   LOGI("android_main: end");
162
163   /* Finish processing all events (namely APP_CMD_DESTROY) before
164      exiting thread */
165   while (!app->destroyRequested)
166       fgPlatformProcessSingleEvent();
167
168   /* Let NativeActivity restart us */
169   /* Users may want to forcibly exit() in their main() anyway because
170      NativeActivity doesn't dlclose() us, so all statically-assigned
171      variables keep their old values on restart.. */
172 }