diff --git a/engine/src/looper.c b/engine/src/looper.c index 2e95215..82ddb8e 100644 --- a/engine/src/looper.c +++ b/engine/src/looper.c @@ -79,15 +79,19 @@ static void looper_cleanup(jack_client_t *client) { } } -/* Signal handler: deactivate and cleanup before exit */ +void looper_shutdown(jack_client_t *client) { + jack_deactivate(client); + looper_cleanup(client); + jack_client_close(client); + log_close(); +} + +volatile int looper_quit = 0; + +/* Signal handler: set quit flag only */ static void signal_handler(int sig) { (void)sig; - if (global_client) { - looper_cleanup(global_client); - jack_client_close(global_client); - } - log_close(); - exit(0); + looper_quit = 1; } static void looper_write_status(void) { diff --git a/engine/src/looper.h b/engine/src/looper.h index 9e064a5..514b5eb 100644 --- a/engine/src/looper.h +++ b/engine/src/looper.h @@ -16,4 +16,10 @@ void jack_shutdown_cb(void *arg); /* Main‑loop command processing (add/remove channels) */ void looper_process_commands(jack_client_t *client); +/* Shutdown (must be called from the main thread after looper_quit is set) */ +void looper_shutdown(jack_client_t *client); + +/* Flag set by signal handler – main loop should check this */ +extern volatile int looper_quit; + #endif diff --git a/engine/src/main.c b/engine/src/main.c index b08a4cb..2928c62 100644 --- a/engine/src/main.c +++ b/engine/src/main.c @@ -49,7 +49,7 @@ int main(int argc, char *argv[]) { log_msg("looper running (client name '%s')", client_name); - while (1) { + while (!looper_quit) { looper_process_commands(client); { struct timespec ts = {.tv_sec = 0, .tv_nsec = 10000000}; @@ -57,7 +57,6 @@ int main(int argc, char *argv[]) { } } - jack_client_close(client); - log_close(); + looper_shutdown(client); return 0; }