Skip to content

Commit 0c304b0

Browse files
Try to gracefully close game on first ctrl+c
1 parent e91beba commit 0c304b0

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

ffrunner.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,27 @@ fetch_icon(char *iconUrl, char *outPath)
664664
return false;
665665
}
666666

667+
static BOOL WINAPI
668+
console_ctrl_handler(DWORD ctrlType)
669+
{
670+
switch (ctrlType) {
671+
case CTRL_C_EVENT:
672+
case CTRL_BREAK_EVENT:
673+
case CTRL_CLOSE_EVENT:
674+
case CTRL_LOGOFF_EVENT:
675+
case CTRL_SHUTDOWN_EVENT:
676+
logmsg("Console signal %d received, shutting down\n", ctrlType);
677+
if (hwnd) {
678+
PostMessageW(hwnd, WM_CLOSE, 0, 0);
679+
}
680+
// remove so next ctrl+c will force quit
681+
SetConsoleCtrlHandler(console_ctrl_handler, FALSE);
682+
return TRUE;
683+
default:
684+
return FALSE;
685+
}
686+
}
687+
667688
void
668689
setup_console() {
669690
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
@@ -675,6 +696,8 @@ setup_console() {
675696

676697
// disable output buffering
677698
setvbuf(stdout, NULL, _IONBF, 0);
699+
700+
SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
678701
}
679702
}
680703

0 commit comments

Comments
 (0)