From df46811d2d5538d33d9994a7ad8c79a935e5669e Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Wed, 27 Jul 2011 03:46:29 +1200 Subject: [PATCH] win32: catch Ctrl+C on console. --- util.c | 34 +++++++++++++++++++++++++++++++++- util.h | 2 ++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/util.c b/util.c index 315dee1..f13b63f 100644 --- a/util.c +++ b/util.c @@ -35,6 +35,37 @@ static volatile int ctrlc_flag; +#ifdef WIN32 +static HANDLE ctrlc_event; + +static WINAPI BOOL ctrlc_handler(DWORD event) +{ + if (event == CTRL_C_EVENT) { + ctrlc_flag = 1; + SetEvent(ctrlc_event); + return TRUE; + } + + return FALSE; +} + +void ctrlc_init(void) +{ + ctrlc_event = CreateEvent(0, TRUE, FALSE, NULL); + SetConsoleCtrlHandler(ctrlc_handler, TRUE); +} + +void ctrlc_reset(void) +{ + ctrlc_flag = 0; + ResetEvent(ctrlc_event); +} + +HANDLE ctrlc_win32_event(void) +{ + return ctrlc_event; +} +#else /* WIN32 */ static void sigint_handler(int signum) { ctrlc_flag = 1; @@ -42,7 +73,7 @@ static void sigint_handler(int signum) void ctrlc_init(void) { -#if defined(WIN32) || defined(__CYGWIN__) +#if defined(__CYGWIN__) signal(SIGINT, sigint_handler); #else const static struct sigaction siga = { @@ -58,6 +89,7 @@ void ctrlc_reset(void) { ctrlc_flag = 0; } +#endif int ctrlc_check(void) { diff --git a/util.h b/util.h index 1ebc431..6f8a838 100644 --- a/util.h +++ b/util.h @@ -57,6 +57,8 @@ int hexval(int c); #ifdef WIN32 char *strsep(char **strp, const char *delim); + +HANDLE ctrlc_win32_event(void); #endif #endif