use std::io;
use windows_sys::Win32::System::Console::{
CTRL_BREAK_EVENT, CTRL_C_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT,
SetConsoleCtrlHandler,
};
use windows_sys::core::BOOL;
use super::handle_signal;
#[expect(
unsafe_code,
reason = "Win32 PHANDLER_ROUTINE callback must be declared as unsafe extern"
)]
unsafe extern "system" fn handler(ctrl_type: u32) -> BOOL {
let exit_code = match ctrl_type {
CTRL_C_EVENT | CTRL_BREAK_EVENT => 130,
CTRL_CLOSE_EVENT | CTRL_LOGOFF_EVENT | CTRL_SHUTDOWN_EVENT => 143,
_ => return 0, };
handle_signal(exit_code);
1 }
#[expect(
unsafe_code,
reason = "FFI to Win32 SetConsoleCtrlHandler; `handler` matches the documented PHANDLER_ROUTINE ABI"
)]
pub fn install() -> io::Result<()> {
let ok: BOOL = unsafe { SetConsoleCtrlHandler(Some(handler), 1) };
if ok == 0 {
return Err(io::Error::last_os_error());
}
Ok(())
}