From b2cb35c182cd370019d5e062e10605b1d74cc310 Mon Sep 17 00:00:00 2001 From: Aaron Culliney Date: Sat, 12 Sep 2015 15:33:22 -0700 Subject: [PATCH] Lifecycle tweaks that get Android building again --- .../org/deadc0de/apple2ix/Apple2Activity.java | 17 ++++++---- Android/jni/jnihooks.c | 34 +++++++++++-------- Android/jni/jniprefs.c | 2 +- src/misc.c | 12 ++++--- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Activity.java b/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Activity.java index 9425d379..919aa873 100644 --- a/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Activity.java +++ b/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Activity.java @@ -560,12 +560,17 @@ public class Apple2Activity extends Activity { public void onClick(DialogInterface dialog, int which) { nativeOnQuit(); Apple2Activity.this.finish(); - try { - Thread.sleep(1000); - } catch (InterruptedException ex) { - // ... - } - System.exit(0); + new Runnable() { + @Override + public void run() { + try { + Thread.sleep(2000); + } catch (InterruptedException ex) { + // ... + } + System.exit(0); + } + }.run(); } }).setNegativeButton(R.string.no, null).create(); /*mQuitDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { diff --git a/Android/jni/jnihooks.c b/Android/jni/jnihooks.c index 60c3dd99..817bf892 100644 --- a/Android/jni/jnihooks.c +++ b/Android/jni/jnihooks.c @@ -30,6 +30,8 @@ enum { ANDROID_ACTION_POINTER_UP = 0x6, }; +static bool shuttingDown = false; + #if TESTING static void _run_tests(void) { char *local_argv[] = { @@ -134,7 +136,7 @@ void Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnCreate(JNIEnv *env, jobje _run_tests(); // CPU thread is started from testsuite (if needed) #else - timing_startCPU(); + emulator_start(); #endif } @@ -145,13 +147,8 @@ void Java_org_deadc0de_apple2ix_Apple2Activity_nativeGraphicsChanged(JNIEnv *env void Java_org_deadc0de_apple2ix_Apple2Activity_nativeGraphicsInitialized(JNIEnv *env, jobject obj, jint width, jint height) { LOG("width:%d height:%d", width, height); - static bool graphicsPreviouslyInitialized = false; - if (graphicsPreviouslyInitialized) { - LOG("shutting down previous context"); - video_shutdown(); - } - graphicsPreviouslyInitialized = true; + video_shutdown(); video_backend->reshape(width, height); video_backend->init((void *)0); } @@ -171,6 +168,9 @@ void Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnResume(JNIEnv *env, jobje } void Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnPause(JNIEnv *env, jobject obj, jboolean isSystemPause) { + if (shuttingDown) { + return; + } if (cpu_isPaused()) { return; } @@ -192,7 +192,7 @@ void Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnPause(JNIEnv *env, jobjec } void Java_org_deadc0de_apple2ix_Apple2Activity_nativeRender(JNIEnv *env, jobject obj) { - if (UNLIKELY(emulator_shutting_down)) { + if (UNLIKELY(shuttingDown)) { return; } @@ -225,17 +225,16 @@ void Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnQuit(JNIEnv *env, jobject #if TESTING // test driver thread is managing CPU #else + shuttingDown = true; + LOG("%s", ""); c_eject_6(0); c_eject_6(1); - emulator_shutting_down = true; - cpu_resume(); - if (pthread_join(cpu_thread_id, NULL)) { - ERRLOG("OOPS: pthread_join of CPU thread ..."); - } + + emulator_shutdown(); #endif } @@ -276,17 +275,22 @@ void Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnUncaughtException(JNIEnv } void Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnKeyDown(JNIEnv *env, jobject obj, jint keyCode, jint metaState) { + if (UNLIKELY(shuttingDown)) { + return; + } android_keycode_to_emulator(keyCode, metaState, true); } void Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnKeyUp(JNIEnv *env, jobject obj, jint keyCode, jint metaState) { + if (UNLIKELY(shuttingDown)) { + return; + } android_keycode_to_emulator(keyCode, metaState, false); } jlong Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnTouch(JNIEnv *env, jobject obj, jint action, jint pointerCount, jint pointerIndex, jfloatArray xCoords, jfloatArray yCoords) { //LOG(": %d/%d/%d :", action, pointerCount, pointerIndex); - if (UNLIKELY(emulator_shutting_down)) { - video_shutdown(); + if (UNLIKELY(shuttingDown)) { return 0x0LL; } diff --git a/Android/jni/jniprefs.c b/Android/jni/jniprefs.c index 03a7e475..6a7ef103 100644 --- a/Android/jni/jniprefs.c +++ b/Android/jni/jniprefs.c @@ -47,7 +47,7 @@ void Java_org_deadc0de_apple2ix_Apple2Preferences_nativeSetSpeakerVolume(JNIEnv assert(goesToTen >= 0); sound_volume = goesToTen; #warning FIXME TODO refactor/remove sound_volume ? - speaker_setVolumeZeroToTen(goesToTen); + vm_reinitializeAudio(); } void Java_org_deadc0de_apple2ix_Apple2Preferences_nativeSetAudioLatency(JNIEnv *env, jclass cls, jfloat latencySecs) { diff --git a/src/misc.c b/src/misc.c index ba25e0ca..7c5d12fd 100644 --- a/src/misc.c +++ b/src/misc.c @@ -38,10 +38,8 @@ static void _init_common() { static void _shutdown_threads(void) { #if !TESTING +# if defined(__linux__) && !defined(ANDROID) LOG("Emulator waiting for other threads to clean up..."); -# if !__linux__ -# error FIXME TODO on Darwin ... -# else do { DIR *dir = opendir("/proc/self/task"); if (!dir) { @@ -71,6 +69,8 @@ static void _shutdown_threads(void) { static struct timespec ts = { .tv_sec=0, .tv_nsec=33333333 }; nanosleep(&ts, NULL); // 30Hz framerate } while (1); +# elif defined(__APPLE__) +# error TODO FIXME ... verify leaks-n-things with instruments on Darwin # endif #endif } @@ -96,9 +96,13 @@ int main(int _argc, char **_argv) { argv = _argv; emulator_start(); - video_main_loop(); + + // main loop ... + emulator_shutdown(); + LOG("Emulator exit ..."); + return 0; } #endif