From 76f29d0865d092cdba77451c31a10954e9342256 Mon Sep 17 00:00:00 2001 From: Aaron Culliney Date: Sun, 17 Apr 2016 12:08:11 -0700 Subject: [PATCH] Allow emulator shutdown state to be queried as needed --- Android/jni/jnihooks.c | 2 +- src/display.c | 4 ++-- src/display.h | 2 +- src/interface.c | 2 +- src/misc.c | 10 ++++++++-- src/misc.h | 11 +++++++++-- src/prefs.c | 4 ++-- src/prefs.h | 2 +- src/video/glnode.c | 6 +++--- src/video/video.h | 2 +- 10 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Android/jni/jnihooks.c b/Android/jni/jnihooks.c index 12d243b4..4c666485 100644 --- a/Android/jni/jnihooks.c +++ b/Android/jni/jnihooks.c @@ -200,7 +200,7 @@ void Java_org_deadc0de_apple2ix_Apple2Activity_nativeOnCreate(JNIEnv *env, jclas void Java_org_deadc0de_apple2ix_Apple2View_nativeGraphicsInitialized(JNIEnv *env, jclass cls) { LOG("..."); _video_setRenderThread(pthread_self()); // by definition, this method is called on the render thread ... - video_shutdown(false); + video_shutdown(); video_init(); } diff --git a/src/display.c b/src/display.c index 073ca892..ff9db923 100644 --- a/src/display.c +++ b/src/display.c @@ -1216,7 +1216,7 @@ bool video_isRenderThread(void) { return (pthread_self() == render_thread_id); } -void video_shutdown(bool emulatorShuttingDown) { +void video_shutdown(void) { #if MOBILE_DEVICE // WARNING : shutdown should occur on the render thread. Platform code (iOS, Android) should ensure this is called @@ -1224,7 +1224,7 @@ void video_shutdown(bool emulatorShuttingDown) { assert(!render_thread_id || pthread_self() == render_thread_id); #endif - video_backend->shutdown(emulatorShuttingDown); + video_backend->shutdown(); if (pthread_self() == render_thread_id) { FREE(video__fb); diff --git a/src/display.h b/src/display.h index 12a8aa6f..90d96db7 100644 --- a/src/display.h +++ b/src/display.h @@ -64,7 +64,7 @@ void video_main_loop(void); * Shutdown video system. Should only be called on the render thread (unless render thread is in emulator-managed main * video loop). */ -void video_shutdown(bool emulatorShuttingDown); +void video_shutdown(void); /* * Begin a render pass (only for non-emulator-managed main video). This should only be called on the render thread. diff --git a/src/interface.c b/src/interface.c index 5715377b..b69dc8a8 100644 --- a/src/interface.c +++ b/src/interface.c @@ -1308,7 +1308,7 @@ void c_interface_parameters() } if (shutdown) { - video_shutdown(/*emulatorShuttingDown:*/false); // soft quit video_main_loop() + video_shutdown(); // soft quit video_main_loop() } else { prefs_sync(NULL); } diff --git a/src/misc.c b/src/misc.c index c47f2fbb..3487ca20 100644 --- a/src/misc.c +++ b/src/misc.c @@ -25,6 +25,7 @@ typedef struct module_ctor_node_s { } module_ctor_node_s; static module_ctor_node_s *head = NULL; +static bool emulatorShuttingDown = false; bool do_logging = true; // also controlled by NDEBUG FILE *error_log = NULL; @@ -292,14 +293,19 @@ void emulator_start(void) { } void emulator_shutdown(void) { + emulatorShuttingDown = true; disk6_eject(0); disk6_eject(1); - video_shutdown(/*emulatorShuttingDown:*/true); - prefs_shutdown(/*emulatorShuttingDown:*/true); + video_shutdown(); + prefs_shutdown(); timing_stopCPU(); _shutdown_threads(); } +bool emulator_isShuttingDown(void) { + return emulatorShuttingDown; +} + #if !defined(__APPLE__) && !defined(ANDROID) int main(int _argc, char **_argv) { argc = _argc; diff --git a/src/misc.h b/src/misc.h index fb482314..a2b9dc37 100644 --- a/src/misc.h +++ b/src/misc.h @@ -22,8 +22,6 @@ enum { CTOR_PRIORITY_LATE = 201, }; -typedef void (*startup_callback_f)(void); - // top installation directory extern const char *data_dir; @@ -31,6 +29,12 @@ extern const char *data_dir; extern char **argv; extern int argc; +// +// Emulator lifecycle +// + +typedef void (*startup_callback_f)(void); + // register a startup function void emulator_registerStartupCallback(long order, startup_callback_f callback); @@ -40,6 +44,9 @@ void emulator_start(void); // shutdown emulator in preparation for app exit void emulator_shutdown(void); +// is emulator shutting down? +bool emulator_isShuttingDown(void); + // // Emulator state save/restore // diff --git a/src/prefs.c b/src/prefs.c index cc62c0f1..2814026b 100644 --- a/src/prefs.c +++ b/src/prefs.c @@ -454,8 +454,8 @@ void prefs_sync(const char *domain) { FREE(alreadySynced); } -void prefs_shutdown(bool emulatorShuttingDown) { - if (!emulatorShuttingDown) { +void prefs_shutdown(void) { + if (!emulator_isShuttingDown()) { return; } diff --git a/src/prefs.h b/src/prefs.h index 8f7023d8..b1c635b5 100644 --- a/src/prefs.h +++ b/src/prefs.h @@ -131,7 +131,7 @@ extern void prefs_registerListener(const char * _NONNULL domain, _NONNULL prefs_ extern void prefs_sync(const char * _NULLABLE domain); // cleans up and removes listener in preparation for app shutdown -extern void prefs_shutdown(bool emulatorShuttingDown); +extern void prefs_shutdown(void); #endif diff --git a/src/video/glnode.c b/src/video/glnode.c index 50535355..c5cabd3d 100644 --- a/src/video/glnode.c +++ b/src/video/glnode.c @@ -161,12 +161,12 @@ static void glnode_setupNodes(void *ctx) { LOG("END glnode_setupNodes ..."); } -static void glnode_shutdownNodes(bool emulatorShuttingDown) { +static void glnode_shutdownNodes(void) { LOG("BEGIN glnode_shutdownNodes ..."); #if USE_GLUT if (glut_in_main_loop) { - assert(!emulatorShuttingDown); + assert(!emulator_isShuttingDown()); glutLeaveMainLoop(); return; } @@ -178,7 +178,7 @@ static void glnode_shutdownNodes(bool emulatorShuttingDown) { p = p->last; } - if (emulatorShuttingDown) { + if (emulator_isShuttingDown()) { // clean up to make Valgrind happy ... p = head; while (p) { diff --git a/src/video/video.h b/src/video/video.h index 3e9b986c..93782ac8 100644 --- a/src/video/video.h +++ b/src/video/video.h @@ -21,7 +21,7 @@ typedef struct video_backend_s { void (*main_loop)(void); void (*reshape)(int width, int height, bool landscape); void (*render)(void); - void (*shutdown)(bool emulatorShuttingDown); + void (*shutdown)(void); } video_backend_s; /*