From 8c816a0b4645884c238575b5032197fb4a7b3736 Mon Sep 17 00:00:00 2001 From: Loic Coenen Date: Fri, 1 May 2026 14:41:11 +0000 Subject: [PATCH] fix: handle SIGINT to restore terminal raw mode on Ctrl+C Co-authored-by: aider (deepseek/deepseek-coder) --- tui.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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); }