#![deny(unsafe_code)]
use crate::sys::win_abi as abi;
use crate::sys::Fd;
use core::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering};
#[path = "windows/ffi.rs"]
mod ffi;
#[path = "windows/io.rs"]
mod io;
pub use io::{read_input, write_all};
#[derive(Clone, Copy)]
pub struct SavedTermios {
in_h: usize,
out_h: usize,
in_mode: u32,
out_mode: Option<u32>,
in_cp: u32,
out_cp: u32,
}
static EMERG_ACTIVE: AtomicBool = AtomicBool::new(false);
static EMERG_IN_H: AtomicUsize = AtomicUsize::new(0);
static EMERG_OUT_H: AtomicUsize = AtomicUsize::new(0);
static EMERG_IN_MODE: AtomicU32 = AtomicU32::new(0);
static EMERG_OUT_MODE: AtomicU32 = AtomicU32::new(u32::MAX);
static EMERG_IN_CP: AtomicU32 = AtomicU32::new(0);
static EMERG_OUT_CP: AtomicU32 = AtomicU32::new(0);
static HANDLERS_INSTALLED: AtomicBool = AtomicBool::new(false);
static VT_OUTPUT: AtomicBool = AtomicBool::new(false);
fn arm_emergency(s: &SavedTermios) {
EMERG_IN_H.store(s.in_h, Ordering::SeqCst);
EMERG_OUT_H.store(s.out_h, Ordering::SeqCst);
EMERG_IN_MODE.store(s.in_mode, Ordering::SeqCst);
EMERG_OUT_MODE.store(s.out_mode.unwrap_or(u32::MAX), Ordering::SeqCst);
EMERG_IN_CP.store(s.in_cp, Ordering::SeqCst);
EMERG_OUT_CP.store(s.out_cp, Ordering::SeqCst);
EMERG_ACTIVE.store(true, Ordering::SeqCst);
}
fn disarm_emergency() {
if !EMERG_ACTIVE.swap(false, Ordering::SeqCst) {
return;
}
let in_h = EMERG_IN_H.load(Ordering::SeqCst) as ffi::Handle;
let out_h = EMERG_OUT_H.load(Ordering::SeqCst) as ffi::Handle;
let out_mode = EMERG_OUT_MODE.load(Ordering::SeqCst);
if out_mode != u32::MAX {
ffi::write_file(out_h, abi::LEAVE_SCREEN);
}
ffi::set_console_mode(in_h, EMERG_IN_MODE.load(Ordering::SeqCst));
if out_mode != u32::MAX {
ffi::set_console_mode(out_h, out_mode);
}
ffi::set_code_pages(
EMERG_IN_CP.load(Ordering::SeqCst),
EMERG_OUT_CP.load(Ordering::SeqCst),
);
}
extern "system" fn ctrl_handler(event: u32) -> i32 {
match abi::ctrl_action(event) {
abi::CtrlAction::Interrupt => {
crate::sys::INTR.store(true, Ordering::SeqCst);
1
}
abi::CtrlAction::Terminate => {
crate::sys::TERM.store(true, Ordering::SeqCst);
disarm_emergency();
1
}
abi::CtrlAction::Ignore => 0,
}
}
pub fn install_signal_handlers() {
if HANDLERS_INSTALLED.swap(true, Ordering::SeqCst) {
return;
}
ffi::add_ctrl_handler(ctrl_handler);
}
pub fn is_tty(fd: Fd) -> bool {
ffi::console_mode(ffi::read_handle(fd)).is_some()
}
pub fn open_tty() -> Option<Fd> {
let access = abi::GENERIC_READ | abi::GENERIC_WRITE;
let read = ffi::open_console_file(&abi::CONIN, access)?;
let write = match ffi::open_console_file(&abi::CONOUT, access) {
Some(w) => w,
None => {
ffi::close_handle(read);
return None;
}
};
match ffi::alloc_tty(read, write) {
Some(fd) => Some(fd),
None => {
ffi::close_handle(read);
ffi::close_handle(write);
None
}
}
}
pub fn tty_fd() -> Option<(Fd, bool)> {
if is_tty(crate::sys::STDIN) {
Some((crate::sys::STDIN, false))
} else {
open_tty().map(|fd| (fd, true))
}
}
pub fn close_fd(fd: Fd) {
if fd > 2 {
if let Some((r, w)) = ffi::take_tty(fd) {
ffi::close_handle(r);
ffi::close_handle(w);
}
}
}
pub fn winsize() -> Option<(u16, u16)> {
winsize_of(crate::sys::STDOUT)
.or_else(|| winsize_of(crate::sys::STDIN))
.or_else(|| {
let fd = open_tty()?;
let r = winsize_of(fd);
close_fd(fd);
r
})
}
pub fn winsize_of(fd: Fd) -> Option<(u16, u16)> {
let info = ffi::screen_info(ffi::write_handle(fd));
let dims = info.and_then(|i| i.window_size());
let out = abi::winsize_result(info.is_some(), dims);
io::note_dims(out);
out
}
enum OutSetup {
Configured(u32),
NotAConsole,
VtUnsupported,
}
fn configure_output(h: ffi::Handle) -> OutSetup {
match ffi::console_mode(h) {
None => OutSetup::NotAConsole,
Some(m) => {
if ffi::set_console_mode(h, abi::raw_output_mode(m)) {
OutSetup::Configured(m)
} else {
OutSetup::VtUnsupported
}
}
}
}
pub fn set_raw(fd: Fd) -> Option<SavedTermios> {
let hin = ffi::read_handle(fd);
let hout = ffi::write_handle(fd);
let in_mode = ffi::console_mode(hin)?;
if !ffi::set_console_mode(hin, abi::raw_input_mode(in_mode)) {
return None;
}
let (in_cp, out_cp) = ffi::code_pages();
ffi::set_code_pages(abi::CP_UTF8, abi::CP_UTF8);
let out_mode = match configure_output(hout) {
OutSetup::Configured(m) => {
VT_OUTPUT.store(true, Ordering::SeqCst);
Some(m)
}
OutSetup::NotAConsole => {
VT_OUTPUT.store(false, Ordering::SeqCst);
None
}
OutSetup::VtUnsupported => {
VT_OUTPUT.store(false, Ordering::SeqCst);
ffi::set_console_mode(hin, in_mode);
ffi::set_code_pages(in_cp, out_cp);
return None;
}
};
let saved = SavedTermios {
in_h: hin as usize,
out_h: hout as usize,
in_mode,
out_mode,
in_cp,
out_cp,
};
arm_emergency(&saved);
io::note_dims(ffi::screen_info(hout).and_then(|i| i.window_size()));
Some(saved)
}
pub fn restore(_fd: Fd, saved: &SavedTermios) -> bool {
let ok_in = ffi::set_console_mode(saved.in_h as ffi::Handle, saved.in_mode);
let ok_out = match saved.out_mode {
Some(m) => ffi::set_console_mode(saved.out_h as ffi::Handle, m),
None => true,
};
ffi::set_code_pages(saved.in_cp, saved.out_cp);
EMERG_ACTIVE.store(false, Ordering::SeqCst);
ok_in && ok_out
}
#[allow(dead_code)]
pub fn vt_output_supported() -> bool {
VT_OUTPUT.load(Ordering::SeqCst)
}