Implement signal handling to gracefully stop the application

This commit is contained in:
2026-01-03 22:59:38 +00:00
parent 3a4fc2ea8b
commit 265302fec3
3 changed files with 31 additions and 0 deletions

View File

@@ -5,6 +5,8 @@
#include <rapidjson/writer.h>
#include <rapidjson/prettywriter.h>
#include <atomic>
#include <csignal>
#include <cstdlib>
#include <cstdint>
#include <exception>
@@ -20,8 +22,23 @@
#include "app/sdl3_app.hpp"
#include <SDL3/SDL_main.h>
namespace sdl3cpp::app {
std::atomic<bool> g_signalReceived{false};
}
namespace {
void SignalHandler(int signal) {
if (signal == SIGINT || signal == SIGTERM) {
sdl3cpp::app::g_signalReceived.store(true);
}
}
void SetupSignalHandlers() {
std::signal(SIGINT, SignalHandler);
std::signal(SIGTERM, SignalHandler);
}
std::filesystem::path FindScriptPath(const char* argv0) {
std::filesystem::path executable;
if (argv0 && *argv0 != '\0') {
@@ -294,6 +311,7 @@ void WriteRuntimeConfigJson(const RuntimeConfig& runtimeConfig,
int main(int argc, char** argv) {
SDL_SetMainReady();
SetupSignalHandlers();
try {
AppOptions options = ParseCommandLine(argc, argv);
sdl3cpp::app::TraceLogger::SetEnabled(options.traceEnabled);