From a48cf31502ba47eb8fc4d2bcccf0b2a357adf1d8 Mon Sep 17 00:00:00 2001 From: Timon Date: Wed, 11 Jan 2023 21:19:18 +0100 Subject: [PATCH] Use dev/tty everywere --- src/cursor/sys/unix.rs | 2 +- src/event/sys/unix/file_descriptor.rs | 35 +++++++++++++++++++++++++++ src/terminal/sys/unix.rs | 11 +++------ 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/cursor/sys/unix.rs b/src/cursor/sys/unix.rs index f610be104..6867b1ba7 100644 --- a/src/cursor/sys/unix.rs +++ b/src/cursor/sys/unix.rs @@ -32,7 +32,7 @@ fn read_position() -> Result<(u16, u16)> { fn read_position_raw() -> Result<(u16, u16)> { // Use `ESC [ 6 n` to and retrieve the cursor position. - let mut stdout = io::stdout(); + let mut stdout = tty_fd()?; stdout.write_all(b"\x1B[6n")?; stdout.flush()?; diff --git a/src/event/sys/unix/file_descriptor.rs b/src/event/sys/unix/file_descriptor.rs index 268d57ebf..c8b535ba5 100644 --- a/src/event/sys/unix/file_descriptor.rs +++ b/src/event/sys/unix/file_descriptor.rs @@ -73,6 +73,7 @@ impl AsRawFd for FileDesc { } /// Creates a file descriptor pointing to the standard input or `/dev/tty`. +#[cfg(not(feature = "use-dev-tty"))] pub fn tty_fd() -> Result { let (fd, close_on_drop) = if unsafe { libc::isatty(libc::STDIN_FILENO) == 1 } { (libc::STDIN_FILENO, false) @@ -89,3 +90,37 @@ pub fn tty_fd() -> Result { Ok(FileDesc::new(fd, close_on_drop)) } + +/// Creates a file descriptor pointing to the standard input or `/dev/tty`. +#[cfg(feature = "use-dev-tty")] +pub fn tty_fd(close_on_drop: bool) -> Result { + use crate::tty::IsTty; + + let fd = match open_rw("/dev/tty") { + Ok(tty) => { + println!("Can open"); + tty + } + Err(e) => { + println!("Can not open"); + if stdin().is_tty() { + libc::STDIN_FILENO + } else { + return Err(ErrorKind::IoError(io::Error::new(io::ErrorKind::Other, "Failed to initialize file descriptor. Crossterm first tried to open `/dev/tty` and then `libc::STDIN_FILENO` but both could not be used."))); + } + } + }; + + Ok(FileDesc::new(fd, close_on_drop)) +} + +fn open_rw>(path: P) -> io::Result { + use std::fs::OpenOptions; + + let file = OpenOptions::new() + .read(true) + .write(true) + .open(path)?; + + Ok(file.into_raw_fd()) +} \ No newline at end of file diff --git a/src/terminal/sys/unix.rs b/src/terminal/sys/unix.rs index 184a8088c..bb87157e3 100644 --- a/src/terminal/sys/unix.rs +++ b/src/terminal/sys/unix.rs @@ -123,14 +123,9 @@ fn read_supports_keyboard_enhancement_raw() -> Result { // ESC [ c Query primary device attributes. const QUERY: &[u8] = b"\x1B[?u\x1B[c"; - if let Err(_) = File::open("/dev/tty").and_then(|mut file| { - file.write_all(QUERY)?; - file.flush() - }) { - let mut stdout = io::stdout(); - stdout.write_all(QUERY)?; - stdout.flush()?; - } + let fd = tty_fd()?; + fd.write_all(QUERY)?; + fd.flush(); loop { match poll_internal(