Skip to content

Commit

Permalink
Properly handle Ctrl+C on Windows
Browse files Browse the repository at this point in the history
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 <#818>
  • Loading branch information
rom1v committed May 8, 2020
1 parent e2d5f0e commit 28abd98
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <sys/time.h>
#include <SDL2/SDL.h>

#ifdef _WIN32
# include <windows.h>
#endif

#include "config.h"
#include "command.h"
#include "common.h"
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 28abd98

Please sign in to comment.