feat: add ASIO signal handling for graceful shutdown

- Add asio dependency for async signal handling
- Register SIGINT and SIGTERM to quit the application
- Poll signals each frame for non-blocking shutdown
- Q key and Ctrl+C both cleanly exit the program
This commit is contained in:
2026-05-05 21:53:51 +02:00
parent 47d01f57c0
commit f18e5e4adc
3 changed files with 92 additions and 2 deletions
+2 -1
View File
@@ -11,9 +11,10 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/deps")
find_package(fmt REQUIRED)
find_package(glfw3 REQUIRED)
find_package(glad REQUIRED)
find_package(asio REQUIRED)
# Target setup
add_executable(cuber "cuber.cpp" "cbt/window.cpp")
target_include_directories(cuber PRIVATE ".")
target_compile_features(cuber PRIVATE cxx_std_23)
target_link_libraries(cuber PRIVATE fmt::fmt glfw::glfw glad::glad)
target_link_libraries(cuber PRIVATE fmt::fmt glfw::glfw glad::glad asio::asio)
+20 -1
View File
@@ -3,6 +3,10 @@
#include "cbt/window.hpp"
#include <csignal>
#include <asio.hpp>
#include "glad/glad.h"
auto main(int, char const*[]) -> int {
@@ -12,8 +16,23 @@ auto main(int, char const*[]) -> int {
return 1;
}
asio::io_context io;
asio::signal_set signals(io, SIGINT, SIGTERM);
bool quit = false;
signals.async_wait([&](auto, auto) {
quit = true;
io.stop();
});
auto process_signals = [&]() -> void {
while (io.poll()) {}
};
while (!w.should_close()) {
if (glfwGetKey(static_cast<GLFWwindow*>(w.raw()), GLFW_KEY_Q) == GLFW_PRESS) {
process_signals();
if (quit || glfwGetKey(static_cast<GLFWwindow*>(w.raw()), GLFW_KEY_Q) == GLFW_PRESS) {
break;
}
+70
View File
@@ -0,0 +1,70 @@
# ==============================================================================
# Find asio
# ==============================================================================
# This module fetches the asio networking library.
#
# Targets provided:
# asio::asio - The asio library target
#
# Variables set:
# asio_FOUND - TRUE if asio is available
# asio_LIBRARIES - The asio library target (asio::asio)
# asio_INCLUDE_DIR - Include directories for asio
# asio_VERSION - Version of asio (if available)
# ==============================================================================
if (DEFINED _FINDASIO_INCLUDED)
return()
endif()
set(_FINDASIO_INCLUDED TRUE)
# Use the version passed to find_package(), or default to 1.32.0
if (DEFINED asio_FIND_VERSION AND NOT asio_FIND_VERSION STREQUAL "")
set(ASIO_VERSION "${asio_FIND_VERSION}")
else()
set(ASIO_VERSION "1.30.2")
endif()
message(STATUS "Fetching asio ${ASIO_VERSION}")
include(FetchContent)
find_program(GIT_EXECUTABLE git)
if (GIT_EXECUTABLE)
set(ASIO_FETCH_METHOD "GIT")
else()
message(FATAL_ERROR "Fetch method ZIP not supported")
endif()
if (ASIO_FETCH_METHOD STREQUAL "GIT")
FetchContent_Declare(
asio
GIT_REPOSITORY https://github.com/mononerv/asio.git
GIT_TAG ${ASIO_VERSION}
)
endif()
FetchContent_MakeAvailable(asio)
if (NOT TARGET asio::asio)
if (TARGET asio)
add_library(asio::asio ALIAS asio)
else()
message(FATAL_ERROR "Could not fetch asio; no target asio or asio::asio available")
endif()
endif()
set(asio_FOUND TRUE)
set(asio_LIBRARIES asio::asio)
set(asio_VERSION "${ASIO_VERSION}")
get_target_property(_asio_inc asio::asio INTERFACE_INCLUDE_DIRECTORIES)
set(asio_INCLUDE_DIR "${_asio_inc}")
# Mark asio includes as SYSTEM to suppress warnings from its headers
if (_asio_inc AND TARGET asio)
set_target_properties(asio PROPERTIES
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${_asio_inc}"
)
endif()
set(ASIO_LICENSE_FILE "${asio_SOURCE_DIR}/LICENSE_1_0.txt" CACHE FILEPATH "Path to Asio license file")