better signal handling

This commit is contained in:
Roland Hollós 2025-03-27 15:32:50 +01:00
parent fa58550954
commit 04073fd407

21
main.c
View File

@ -9,9 +9,19 @@
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <cairo.h> #include <cairo.h>
#include <cairo-xlib.h> #include <cairo-xlib.h>
#include <signal.h>
#include "config.h" #include "config.h"
#define VERSION "0.100" #define VERSION "0.100"
// --- Globals for Signal Handling ---
volatile sig_atomic_t running = 1; // <--- Flag to control main loop
// --- Signal Handler Function ---
void handle_signal(int signum) { // <--- Simple handler to stop the loop
(void) signum;
running = 0;
}
typedef struct _rectangle { typedef struct _rectangle {
int pos_x; int pos_x;
int pos_y; int pos_y;
@ -183,7 +193,13 @@ int main(int argc, char** argv) {
DefaultVisual(d, s), DefaultVisual(d, s),
width, height); width, height);
cairo_t *cr = cairo_create(surf); cairo_t *cr = cairo_create(surf);
while(1){
// --- Register Signal Handlers ---
signal(SIGINT, handle_signal); // <--- Catch Ctrl+C
signal(SIGTERM, handle_signal); // <--- Catch standard termination signal
// --- Main Loop ---
while(running){ // <--- Changed loop condition
drawFibbTime(cr, width, height); drawFibbTime(cr, width, height);
XChangeProperty(d, w, prop_root, XA_PIXMAP, 32, PropModeReplace, XChangeProperty(d, w, prop_root, XA_PIXMAP, 32, PropModeReplace,
(unsigned char*) &pix, 1); (unsigned char*) &pix, 1);
@ -193,6 +209,9 @@ int main(int argc, char** argv) {
XFlush(d); XFlush(d);
sleep(REFRESH_TIME); sleep(REFRESH_TIME);
} }
// --- Cleanup (Now reachable) ---
printf("\nFibonacci Clock: Shutting down gracefully...\n"); // Optional message
cairo_destroy(cr); cairo_destroy(cr);
cairo_surface_destroy(surf); cairo_surface_destroy(surf);
XFreePixmap(d, pix); XFreePixmap(d, pix);