1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! Windows-specific console control handler.
//!
//! On Windows, pressing Ctrl-C generates a `CTRL_C_EVENT` that by default
//! terminates the process immediately — before any destructors (and therefore
//! terminal cleanup) can run. This module installs a custom handler via
//! `SetConsoleCtrlHandler` that performs terminal cleanup (disable mouse
//! capture, bracketed paste, alternate screen, raw mode) and then exits
//! with code 130 (the conventional "interrupted" exit code).
use cleanup_terminal;
// Windows console control event constants.
const CTRL_C_EVENT: u32 = 0;
const CTRL_BREAK_EVENT: u32 = 1;
const CTRL_CLOSE_EVENT: u32 = 2;
unsafe extern "system"
/// Console ctrl handler callback invoked by Windows on `CTRL_C_EVENT` (and
/// other control events like `CTRL_BREAK_EVENT`, `CTRL_CLOSE_EVENT`, etc.).
///
/// The handler cleans up the terminal state so that mouse capture, raw mode,
/// and the alternate screen are properly disabled, then exits with code 130.
///
/// Returning `TRUE` (1) tells Windows that the event has been handled and the
/// default handler (which would call `ExitProcess` without cleanup) should
/// **not** run.
unsafe extern "system"
/// Install our console ctrl handler so that `CTRL_C_EVENT` triggers terminal
/// cleanup instead of an abrupt process termination.
pub
/// Remove our console ctrl handler, restoring default behaviour.
pub