From 3f2fb22641fe3c80a2af11204f2bb948bdf82908 Mon Sep 17 00:00:00 2001 From: Valtteri Koskivuori Date: Sat, 28 Oct 2023 17:07:01 +0300 Subject: [PATCH] Move SIGINT handling out of SDL ui.c It's now living in renderer.c for the time being. That file is super ugly too, though, so I'll have to do something about that. At least sigint handling isn't coupled to SDL glue code for no reason anymore :^) --- src/renderer/renderer.c | 20 ++++++++++++++++++++ src/utils/ui.c | 26 -------------------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/renderer/renderer.c b/src/renderer/renderer.c index 9574484f..2918b2a2 100644 --- a/src/renderer/renderer.c +++ b/src/renderer/renderer.c @@ -34,12 +34,27 @@ #define paused_msec 100 #define active_msec 16 +static bool g_aborted = false; + +void sigHandler(int sig) { + if (sig == 2) { //SIGINT + logr(plain, "\n"); + logr(info, "Received ^C, aborting render without saving\n"); + g_aborted = true; + } +} + + void *renderThread(void *arg); void *renderThreadInteractive(void *arg); /// @todo Use defaultSettings state struct for this. /// @todo Clean this up, it's ugly. struct texture *renderFrame(struct renderer *r) { + //Check for CTRL-C + if (registerHandler(sigint, sigHandler)) { + logr(warning, "Unable to catch SIGINT\n"); + } struct camera camera = r->scene->cameras[r->prefs.selected_camera]; struct texture *output = newTexture(char_p, camera.width, camera.height, 3); @@ -124,6 +139,11 @@ struct texture *renderFrame(struct renderer *r) { //where a worker node disconnects during a render, so maybe fix that next. while (r->state.isRendering) { getKeyboardInput(r); + + if (g_aborted) { + r->state.saveImage = false; + r->state.renderAborted = true; + } //Gather and maintain this average constantly. if (!r->state.threadStates[0].paused) { diff --git a/src/utils/ui.c b/src/utils/ui.c index a05a1f07..1f3bacb6 100644 --- a/src/utils/ui.c +++ b/src/utils/ui.c @@ -37,19 +37,6 @@ struct window { unsigned height; }; -// FIXME: Move the sig handling elsewhere, no reason for it to be tied to this SDL stuff -static bool g_aborted = false; - -//FIXME: This won't work on linux, it'll just abort the execution. -//Take a look at the docs for sigaction() and implement that. -void sigHandler(int sig) { - if (sig == 2) { //SIGINT - logr(plain, "\n"); - logr(info, "Received ^C, aborting render without saving\n"); - g_aborted = true; - } -} - #ifdef CRAY_SDL_ENABLED static void setWindowIcon(SDL_Window *window) { @@ -197,18 +184,6 @@ void printDuration(uint64_t ms) { } void getKeyboardInput(struct renderer *r) { - if (g_aborted) { - r->state.saveImage = false; - r->state.renderAborted = true; - } - static bool sigRegistered = false; - //Check for CTRL-C - if (!sigRegistered) { - if (registerHandler(sigint, sigHandler)) { - logr(warning, "Unable to catch SIGINT\n"); - } - sigRegistered = true; - } #ifdef CRAY_SDL_ENABLED SDL_Event event; while (SDL_PollEvent(&event)) { @@ -327,7 +302,6 @@ static void updateFrames(struct renderer *r) { #endif void win_update(struct window *w, struct renderer *r, struct texture *t) { - if (g_aborted) r->state.renderAborted = true; #ifdef CRAY_SDL_ENABLED if (!w) return; //Render frames