diff --git a/tui.c b/tui.c index f0868b5..1ca60af 100644 --- a/tui.c +++ b/tui.c @@ -2,6 +2,7 @@ #include #include #include +#include #define GRID_ROWS 8 #define GRID_COLS 8 @@ -32,6 +33,9 @@ static int state_to_color(ClipState state) { case CLIP_STOPPED: return COLOR_STOPPED; default: return COLOR_EMPTY; } + + // Install signal handler for Ctrl+C + signal(SIGINT, handle_sigint); } // Get clip index from grid position @@ -138,6 +142,12 @@ static void draw_grid(void) { refresh(); } +static void handle_sigint(int sig) { + (void)sig; + tui_cleanup(); + _exit(1); +} + void tui_init(Engine *engine) { g_engine = engine; @@ -171,6 +181,10 @@ void tui_run(Engine *engine) { while (1) { int ch = getch(); + if (ch == ERR) { + // getch returned error (e.g., signal interrupted) + break; + } switch (ch) { case 'h': @@ -261,4 +275,7 @@ void tui_cleanup(void) { // Restore terminal settings curs_set(1); endwin(); + + // Reset signal handler to default + signal(SIGINT, SIG_DFL); }