From 28abd98f7fef5161ede934a41287b2c0ec1d7668 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Fri, 8 May 2020 14:54:33 +0200 Subject: [PATCH] Properly handle Ctrl+C on Windows By default, Ctrl+C just kills the process on Windows. This caused corrupted video files on recording. Handle Ctrl+C properly to clean up properly. Fixes #818 --- README.md | 1 - app/src/scrcpy.c | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 09cb27b8..0d0c4c5e 100644 --- a/README.md +++ b/README.md @@ -210,7 +210,6 @@ To disable mirroring while recording: scrcpy --no-display --record file.mp4 scrcpy -Nr file.mkv # interrupt recording with Ctrl+C -# Ctrl+C does not terminate properly on Windows, so disconnect the device ``` "Skipped frames" are recorded, even if they are not displayed in real time (for diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 096b0794..2a4702e6 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -7,6 +7,10 @@ #include #include +#ifdef _WIN32 +# include +#endif + #include "config.h" #include "command.h" #include "common.h" @@ -45,6 +49,18 @@ static struct input_manager input_manager = { .prefer_text = false, // initialized later }; +#ifdef _WIN32 +BOOL windows_ctrl_handler(DWORD ctrl_type) { + if (ctrl_type == CTRL_C_EVENT) { + SDL_Event event; + event.type = SDL_QUIT; + SDL_PushEvent(&event); + return TRUE; + } + return FALSE; +} +#endif // _WIN32 + // init SDL and set appropriate hints static bool sdl_init_and_configure(bool display, const char *render_driver) { @@ -56,6 +72,14 @@ sdl_init_and_configure(bool display, const char *render_driver) { atexit(SDL_Quit); +#ifdef _WIN32 + // Clean up properly on Ctrl+C on Windows + bool ok = SetConsoleCtrlHandler(windows_ctrl_handler, TRUE); + if (!ok) { + LOGW("Could not set Ctrl+C handler"); + } +#endif // _WIN32 + if (!display) { return true; }