From 4fd6c66edb9eac295315a950ac8e734ddac91008 Mon Sep 17 00:00:00 2001 From: ns6089 <61738816+ns6089@users.noreply.github.com> Date: Sun, 4 Aug 2024 00:21:10 +0300 Subject: [PATCH] Handle CTRL_CLOSE_EVENT on Windows (#2952) This allows us to terminate gracefully when console window is closed. Mostly relevant for portable builds. --- src/main.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index ed584459..1a1d5ef2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -71,6 +71,15 @@ SessionMonitorWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return DefWindowProc(hwnd, uMsg, wParam, lParam); } } + +WINAPI BOOL +ConsoleCtrlHandler(DWORD type) { + if (type == CTRL_CLOSE_EVENT) { + BOOST_LOG(info) << "Console closed handler called"; + lifetime::exit_sunshine(0, false); + } + return FALSE; +} #endif int @@ -244,6 +253,11 @@ main(int argc, char *argv[]) { shutdown_event->raise(true); }); +#ifdef _WIN32 + // Terminate gracefully on Windows when console window is closed + SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); +#endif + proc::refresh(config::stream.file_apps); // If any of the following fail, we log an error and continue event though sunshine will not function correctly.