Document android app lifecycle; kill app when window is closed, until pausing/restori...
[freeglut] / src / android / fg_runtime_android.c
index e83c2d3..b5d6cad 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * freeglut_runtime_android.c
+ * fg_runtime_android.c
  *
  * Android runtime
  *
@@ -23,7 +23,7 @@
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
-/* Parts taken from Android NDK's 'native-activity' sample : */
+/* Parts taken from Android NDK's 'native-activity' sample: */
 /*
  * Copyright (C) 2010 The Android Open Source Project
  * 
@@ -49,6 +49,7 @@
 #include <android/asset_manager.h>
 #include <android/native_window.h>
 #include "android/native_app_glue/android_native_app_glue.h"
+#include "android/fg_main_android.h"
 
 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "FreeGLUT", __VA_ARGS__))
 #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "FreeGLUT", __VA_ARGS__))
@@ -78,7 +79,6 @@ static void onNativeWindowResized(ANativeActivity* activity, ANativeWindow* wind
 static void onNativeWindowRedrawNeeded(ANativeActivity* activity, ANativeWindow* window) {
   LOGI("onNativeWindowRedrawNeeded: %p\n", (void*)activity);
   struct android_app* app = (struct android_app*)activity->instance;
-  //if (fgDisplay.pDisplay.single_window->Window.pContext.eglSurface != EGL_NO_SURFACE)
   android_app_write_cmd(app, APP_CMD_WINDOW_RESIZED);
 }
 
@@ -128,6 +128,8 @@ static void extract_assets(struct android_app* app) {
       AAssetDir_close(assetDir);
     }
   }
+
+  (*vm)->DetachCurrentThread(vm);
 }
 
 /**
@@ -138,7 +140,7 @@ static void extract_assets(struct android_app* app) {
 void android_main(struct android_app* app) {
   LOGI("android_main");
 
-  // Register window resize callback
+  /* Register window resize callback */
   app->activity->callbacks->onNativeWindowResized = onNativeWindowResized;
   app->activity->callbacks->onContentRectChanged = onContentRectChanged;
   app->activity->callbacks->onNativeWindowRedrawNeeded = onNativeWindowRedrawNeeded;
@@ -153,8 +155,22 @@ void android_main(struct android_app* app) {
     char progname[5] = "self";
     char* argv[] = {progname, NULL};
     main(1, argv);
+    /* FreeGLUT will exit() by itself if
+       GLUT_ACTION_ON_WINDOW_CLOSE == GLUT_ACTION_EXIT */
   }
 
   LOGI("android_main: end");
+
+  /* TODO: Pausing/resuming windows not ready yet, so exiting now */
   exit(0);
+
+  /* Finish processing all events (namely APP_CMD_DESTROY) before
+     exiting thread */
+  while (!app->destroyRequested)
+      fgPlatformProcessSingleEvent();
+
+  /* Let NativeActivity restart us */
+  /* Users may want to forcibly exit() in their main() anyway because
+     NativeActivity doesn't dlclose() us, so all statically-assigned
+     variables keep their old values on restart.. */
 }