From 7f6373bc85e2c624c95f7d4e8d04595697390e09 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Tue, 31 Mar 2020 17:54:15 +0200 Subject: [PATCH 01/19] Removed STDIN fd --- src/event/sys/unix/file_descriptor.rs | 20 ++++++++------------ src/terminal/sys/unix.rs | 13 ++++++++----- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/event/sys/unix/file_descriptor.rs b/src/event/sys/unix/file_descriptor.rs index c80557123..3e3e327b9 100644 --- a/src/event/sys/unix/file_descriptor.rs +++ b/src/event/sys/unix/file_descriptor.rs @@ -65,18 +65,14 @@ impl Drop for FileDesc { /// Creates a file descriptor pointing to the standard input or `/dev/tty`. pub fn tty_fd() -> Result { - let (fd, close_on_drop) = if unsafe { libc::isatty(libc::STDIN_FILENO) == 1 } { - (libc::STDIN_FILENO, false) - } else { - ( - fs::OpenOptions::new() - .read(true) - .write(true) - .open("/dev/tty")? - .into_raw_fd(), - true, - ) - }; + let (fd, close_on_drop) = ( + fs::OpenOptions::new() + .read(true) + .write(true) + .open("/dev/tty")? + .into_raw_fd(), + true, + ); Ok(FileDesc::new(fd, close_on_drop)) } diff --git a/src/terminal/sys/unix.rs b/src/terminal/sys/unix.rs index 9e50e53ea..3691d6d2d 100644 --- a/src/terminal/sys/unix.rs +++ b/src/terminal/sys/unix.rs @@ -1,11 +1,11 @@ //! UNIX related logic for terminal manipulation. use std::{io, mem, process, sync::Mutex}; -use crate::event::sys::unix::file_descriptor::FileDesc; +use crate::event::sys::unix::file_descriptor::{tty_fd, FileDesc}; use lazy_static::lazy_static; use libc::{ - cfmakeraw, ioctl, tcgetattr, tcsetattr, termios as Termios, winsize, STDIN_FILENO, - STDOUT_FILENO, TCSANOW, TIOCGWINSZ, + cfmakeraw, ioctl, tcgetattr, tcsetattr, termios as Termios, winsize, STDOUT_FILENO, TCSANOW, + TIOCGWINSZ, }; use crate::error::{ErrorKind, Result}; @@ -116,15 +116,18 @@ fn raw_terminal_attr(termios: &mut Termios) { } fn get_terminal_attr() -> Result { + let fd = tty_fd()?; + unsafe { let mut termios = mem::zeroed(); - wrap_with_result(tcgetattr(STDIN_FILENO, &mut termios))?; + wrap_with_result(tcgetattr(fd.raw_fd(), &mut termios))?; Ok(termios) } } fn set_terminal_attr(termios: &Termios) -> Result { - wrap_with_result(unsafe { tcsetattr(STDIN_FILENO, TCSANOW, termios) }) + let fd = tty_fd()?; + wrap_with_result(unsafe { tcsetattr(fd.raw_fd(), TCSANOW, termios) }) } pub fn wrap_with_result(result: i32) -> Result { From a2058f96f18158d0217315af1a887062bebc3537 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Tue, 31 Mar 2020 18:17:39 +0200 Subject: [PATCH 02/19] More meaningfull error --- src/event/read.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/event/read.rs b/src/event/read.rs index ec2d895df..06d1181e9 100644 --- a/src/event/read.rs +++ b/src/event/read.rs @@ -24,7 +24,12 @@ impl Default for InternalEventReader { #[cfg(unix)] let source = UnixInternalEventSource::new(); - let source = source.ok().map(|x| Box::new(x) as Box); + let source = match source { + Ok(source) => Some(Box::new(source) as Box), + Err(e) => { + panic!("Event source can not be initialized: {:?}", e); + } + }; InternalEventReader { source, From 834150523c1bd2c1d5356e18f8cbe22f3d3faae9 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Thu, 2 Jul 2020 15:33:42 +0200 Subject: [PATCH 03/19] a --- src/event/sys/unix/file_descriptor.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/event/sys/unix/file_descriptor.rs b/src/event/sys/unix/file_descriptor.rs index 3e3e327b9..8787f9720 100644 --- a/src/event/sys/unix/file_descriptor.rs +++ b/src/event/sys/unix/file_descriptor.rs @@ -6,6 +6,8 @@ use std::{ use libc::size_t; use crate::{ErrorKind, Result}; +use futures::io::Error; +use std::io::stdin; /// A file descriptor wrapper. /// @@ -65,14 +67,20 @@ impl Drop for FileDesc { /// Creates a file descriptor pointing to the standard input or `/dev/tty`. pub fn tty_fd() -> Result { - let (fd, close_on_drop) = ( - fs::OpenOptions::new() - .read(true) - .write(true) - .open("/dev/tty")? - .into_raw_fd(), - true, - ); + use crate::tty::IsTty; + + if let Ok(tty) = fs::OpenOptions::new() + .read(true) + .write(true) + .open("/dev/tty")? + .into_raw_fd() { + } else { + if stdin().is_atty() { + (libc::STDIN_FILENO, true) + } else { + return Err(ErrorKind::IoError(Error::new(io::ErrorKind::Other, "Failed to initialize input source. Crossterm first tried to open `/dev/tty` wereafter `libc::STDIN_FILENO`, but both could not be used."))) + } + } Ok(FileDesc::new(fd, close_on_drop)) } From 15166d732b74f227b427a1555a8de638cb2473b1 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 09:39:43 +0200 Subject: [PATCH 04/19] a --- src/event/sys/unix/file_descriptor.rs | 37 ++++++++++++++++++--------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/event/sys/unix/file_descriptor.rs b/src/event/sys/unix/file_descriptor.rs index 8787f9720..67142db5e 100644 --- a/src/event/sys/unix/file_descriptor.rs +++ b/src/event/sys/unix/file_descriptor.rs @@ -69,18 +69,31 @@ impl Drop for FileDesc { pub fn tty_fd() -> Result { use crate::tty::IsTty; - if let Ok(tty) = fs::OpenOptions::new() - .read(true) - .write(true) - .open("/dev/tty")? - .into_raw_fd() { - } else { - if stdin().is_atty() { - (libc::STDIN_FILENO, true) - } else { - return Err(ErrorKind::IoError(Error::new(io::ErrorKind::Other, "Failed to initialize input source. Crossterm first tried to open `/dev/tty` wereafter `libc::STDIN_FILENO`, but both could not be used."))) - } - } + let (fd, close_on_drop) = if stdin().is_tty() { + (libc::STDIN_FILENO, false) + } else { + ( + fs::OpenOptions::new() + .read(true) + .write(true) + .open("/dev/tty")? + .into_raw_fd(), + false, + ) + }; + +// let fd = if let Ok(tty) = fs::OpenOptions::new() +// .read(true) +// .write(true) +// .open("/dev/tty")? +// .into_raw_fd() { +// } else { +// if stdin().is_tty() { +// (libc::STDIN_FILENO, true) +// } else { +// return Err(ErrorKind::IoError(Error::new(io::ErrorKind::Other, "Failed to initialize input source. Crossterm first tried to open `/dev/tty` wereafter `libc::STDIN_FILENO`, but both could not be used."))); +// } +// }; Ok(FileDesc::new(fd, close_on_drop)) } From 6bfbcb3b45a385e342e3322b212fc60b653bd920 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 09:40:25 +0200 Subject: [PATCH 05/19] a --- src/event/sys/unix/file_descriptor.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/event/sys/unix/file_descriptor.rs b/src/event/sys/unix/file_descriptor.rs index 67142db5e..7a58a6d47 100644 --- a/src/event/sys/unix/file_descriptor.rs +++ b/src/event/sys/unix/file_descriptor.rs @@ -6,7 +6,6 @@ use std::{ use libc::size_t; use crate::{ErrorKind, Result}; -use futures::io::Error; use std::io::stdin; /// A file descriptor wrapper. From a7b2ae7f1578c8806ba81a110aa8a65ef0a57784 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 09:45:34 +0200 Subject: [PATCH 06/19] a --- src/event/sys/unix/file_descriptor.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/event/sys/unix/file_descriptor.rs b/src/event/sys/unix/file_descriptor.rs index 7a58a6d47..a719fdb08 100644 --- a/src/event/sys/unix/file_descriptor.rs +++ b/src/event/sys/unix/file_descriptor.rs @@ -68,6 +68,8 @@ impl Drop for FileDesc { pub fn tty_fd() -> Result { use crate::tty::IsTty; + println!("{:?}", stdin().is_tty()); + let (fd, close_on_drop) = if stdin().is_tty() { (libc::STDIN_FILENO, false) } else { From 51223e914582457dcf2b07266172fba9d60b6a12 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 09:47:36 +0200 Subject: [PATCH 07/19] a --- src/event/sys/unix/file_descriptor.rs | 49 ++++++++++++++------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/event/sys/unix/file_descriptor.rs b/src/event/sys/unix/file_descriptor.rs index a719fdb08..ecea6a7d7 100644 --- a/src/event/sys/unix/file_descriptor.rs +++ b/src/event/sys/unix/file_descriptor.rs @@ -70,31 +70,32 @@ pub fn tty_fd() -> Result { println!("{:?}", stdin().is_tty()); - let (fd, close_on_drop) = if stdin().is_tty() { - (libc::STDIN_FILENO, false) - } else { - ( - fs::OpenOptions::new() - .read(true) - .write(true) - .open("/dev/tty")? - .into_raw_fd(), - false, - ) - }; - -// let fd = if let Ok(tty) = fs::OpenOptions::new() -// .read(true) -// .write(true) -// .open("/dev/tty")? -// .into_raw_fd() { -// } else { -// if stdin().is_tty() { -// (libc::STDIN_FILENO, true) -// } else { -// return Err(ErrorKind::IoError(Error::new(io::ErrorKind::Other, "Failed to initialize input source. Crossterm first tried to open `/dev/tty` wereafter `libc::STDIN_FILENO`, but both could not be used."))); -// } +// let (fd, close_on_drop) = if stdin().is_tty() { +// (libc::STDIN_FILENO, false) +// } else { +// ( +// fs::OpenOptions::new() +// .read(true) +// .write(true) +// .open("/dev/tty")? +// .into_raw_fd(), +// false, +// ) // }; + let fd = if let Ok(tty) = fs::OpenOptions::new() + .read(true) + .write(true) + .open("/dev/tty")? + .into_raw_fd() { + (tty, true) + } else { + if stdin().is_tty() { + (libc::STDIN_FILENO, true) + } else { + return Err(ErrorKind::IoError(io::Error::new(io::ErrorKind::Other, "Failed to initialize input source. Crossterm first tried to open `/dev/tty` wereafter `libc::STDIN_FILENO`, but both could not be used."))); + } + }; + Ok(FileDesc::new(fd, close_on_drop)) } From 7aa973e53ed48fc21fea0a8b0cd0a247ef239428 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 09:48:33 +0200 Subject: [PATCH 08/19] a --- src/event/sys/unix/file_descriptor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/event/sys/unix/file_descriptor.rs b/src/event/sys/unix/file_descriptor.rs index ecea6a7d7..99c672aa2 100644 --- a/src/event/sys/unix/file_descriptor.rs +++ b/src/event/sys/unix/file_descriptor.rs @@ -83,7 +83,7 @@ pub fn tty_fd() -> Result { // ) // }; - let fd = if let Ok(tty) = fs::OpenOptions::new() + let (fd, close_on_drop) = if let Ok(tty) = fs::OpenOptions::new() .read(true) .write(true) .open("/dev/tty")? From caf4c6197ee99cf3da444d5661eaefe3400f91dd Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 09:50:14 +0200 Subject: [PATCH 09/19] a --- src/event/sys/unix/file_descriptor.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/event/sys/unix/file_descriptor.rs b/src/event/sys/unix/file_descriptor.rs index 99c672aa2..c39fb95a6 100644 --- a/src/event/sys/unix/file_descriptor.rs +++ b/src/event/sys/unix/file_descriptor.rs @@ -86,12 +86,12 @@ pub fn tty_fd() -> Result { let (fd, close_on_drop) = if let Ok(tty) = fs::OpenOptions::new() .read(true) .write(true) - .open("/dev/tty")? - .into_raw_fd() { - (tty, true) + .open("/dev/tty") + { + (tty.into_raw_fd(), false) } else { if stdin().is_tty() { - (libc::STDIN_FILENO, true) + (libc::STDIN_FILENO, false) } else { return Err(ErrorKind::IoError(io::Error::new(io::ErrorKind::Other, "Failed to initialize input source. Crossterm first tried to open `/dev/tty` wereafter `libc::STDIN_FILENO`, but both could not be used."))); } From e69f8ec17e64e7bb385e1f7070445963044a0ef6 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 09:56:54 +0200 Subject: [PATCH 10/19] a --- examples/event-read.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/event-read.rs b/examples/event-read.rs index f4de7784a..181189350 100644 --- a/examples/event-read.rs +++ b/examples/event-read.rs @@ -38,6 +38,10 @@ fn print_events() -> Result<()> { } fn main() -> Result<()> { + let result = std::panic::catch_unwind(|| { + disable_raw_mode(); + }); + println!("{}", HELP); enable_raw_mode()?; From 24a910fc8b90f26ef2f86d10627185f4d036bb69 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 09:59:10 +0200 Subject: [PATCH 11/19] a --- examples/event-read.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/event-read.rs b/examples/event-read.rs index 181189350..985486868 100644 --- a/examples/event-read.rs +++ b/examples/event-read.rs @@ -39,8 +39,6 @@ fn print_events() -> Result<()> { fn main() -> Result<()> { let result = std::panic::catch_unwind(|| { - disable_raw_mode(); - }); println!("{}", HELP); @@ -54,6 +52,7 @@ fn main() -> Result<()> { } execute!(stdout, DisableMouseCapture)?; + }); disable_raw_mode() } From 9d16430832da4aa8751b0f644e7482b60ccb4aa0 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 09:59:52 +0200 Subject: [PATCH 12/19] a --- examples/event-read.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/event-read.rs b/examples/event-read.rs index 985486868..1a252d637 100644 --- a/examples/event-read.rs +++ b/examples/event-read.rs @@ -54,5 +54,6 @@ fn main() -> Result<()> { execute!(stdout, DisableMouseCapture)?; }); + println!("{:?}", result); disable_raw_mode() } From 65af0fd0bf6e66333eeeb879ccf28a365f66917d Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 10:01:24 +0200 Subject: [PATCH 13/19] a --- examples/event-read.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/event-read.rs b/examples/event-read.rs index 1a252d637..b5d2697e9 100644 --- a/examples/event-read.rs +++ b/examples/event-read.rs @@ -42,18 +42,18 @@ fn main() -> Result<()> { println!("{}", HELP); - enable_raw_mode()?; + enable_raw_mode().expect("Can not enable raw mode"); let mut stdout = stdout(); - execute!(stdout, EnableMouseCapture)?; + execute!(stdout, EnableMouseCapture).expect("Can not enablemouse"); if let Err(e) = print_events() { println!("Error: {:?}\r", e); } - execute!(stdout, DisableMouseCapture)?; + execute!(stdout, DisableMouseCapture).expect("Can not disable mouse"); }); println!("{:?}", result); - disable_raw_mode() + disable_raw_mode(); } From b2a5c2beca0329f85c5d87c255d5ca815f694dfd Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 10:02:11 +0200 Subject: [PATCH 14/19] a --- examples/event-read.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/event-read.rs b/examples/event-read.rs index b5d2697e9..c924d58c5 100644 --- a/examples/event-read.rs +++ b/examples/event-read.rs @@ -55,5 +55,5 @@ fn main() -> Result<()> { }); println!("{:?}", result); - disable_raw_mode(); + disable_raw_mode() } From f22112fc6de7ea557cfc62689137ca926f0777d5 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 10:03:02 +0200 Subject: [PATCH 15/19] a --- examples/event-read.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/event-read.rs b/examples/event-read.rs index c924d58c5..78a986a02 100644 --- a/examples/event-read.rs +++ b/examples/event-read.rs @@ -54,6 +54,6 @@ fn main() -> Result<()> { execute!(stdout, DisableMouseCapture).expect("Can not disable mouse"); }); - println!("{:?}", result); + println!("result: {:?}", result); disable_raw_mode() } From f383e6bf0d7c37cd1fcdb5511be34fe12f9a8fb4 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 10:05:09 +0200 Subject: [PATCH 16/19] a --- examples/event-read.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/event-read.rs b/examples/event-read.rs index 78a986a02..dee4ab538 100644 --- a/examples/event-read.rs +++ b/examples/event-read.rs @@ -55,5 +55,6 @@ fn main() -> Result<()> { }); println!("result: {:?}", result); - disable_raw_mode() + disable_raw_mode().expect("Can not disable raw mode"); + Ok(()) } From db9e7420dc7fd2b7f438f47ecdc09dc58ec0f288 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 10:25:35 +0200 Subject: [PATCH 17/19] a --- src/event/sys/unix/file_descriptor.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/event/sys/unix/file_descriptor.rs b/src/event/sys/unix/file_descriptor.rs index c39fb95a6..7c1e3878f 100644 --- a/src/event/sys/unix/file_descriptor.rs +++ b/src/event/sys/unix/file_descriptor.rs @@ -68,8 +68,6 @@ impl Drop for FileDesc { pub fn tty_fd() -> Result { use crate::tty::IsTty; - println!("{:?}", stdin().is_tty()); - // let (fd, close_on_drop) = if stdin().is_tty() { // (libc::STDIN_FILENO, false) // } else { @@ -83,17 +81,21 @@ pub fn tty_fd() -> Result { // ) // }; - let (fd, close_on_drop) = if let Ok(tty) = fs::OpenOptions::new() + let (fd, close_on_drop) = match fs::OpenOptions::new() .read(true) .write(true) - .open("/dev/tty") - { - (tty.into_raw_fd(), false) - } else { - if stdin().is_tty() { - (libc::STDIN_FILENO, false) - } else { - return Err(ErrorKind::IoError(io::Error::new(io::ErrorKind::Other, "Failed to initialize input source. Crossterm first tried to open `/dev/tty` wereafter `libc::STDIN_FILENO`, but both could not be used."))); + .open("/dev/tty") { + Ok(tty) => { + println!("Can open"); + (tty.into_raw_fd(), false) + } + Err(e) => { + println!("Can not open"); + if stdin().is_tty() { + (libc::STDIN_FILENO, false) + } else { + return Err(ErrorKind::IoError(io::Error::new(io::ErrorKind::Other, "Failed to initialize input source. Crossterm first tried to open `/dev/tty` wereafter `libc::STDIN_FILENO`, but both could not be used."))); + } } }; From 128924168bc29e1f2b1cccdf04ff5c6816263bd5 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 10:28:20 +0200 Subject: [PATCH 18/19] a --- src/event/sys/unix/file_descriptor.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/event/sys/unix/file_descriptor.rs b/src/event/sys/unix/file_descriptor.rs index 7c1e3878f..0def1b761 100644 --- a/src/event/sys/unix/file_descriptor.rs +++ b/src/event/sys/unix/file_descriptor.rs @@ -78,7 +78,7 @@ pub fn tty_fd() -> Result { // .open("/dev/tty")? // .into_raw_fd(), // false, -// ) +// )== // }; let (fd, close_on_drop) = match fs::OpenOptions::new() @@ -87,12 +87,12 @@ pub fn tty_fd() -> Result { .open("/dev/tty") { Ok(tty) => { println!("Can open"); - (tty.into_raw_fd(), false) + (tty.into_raw_fd(), true) } Err(e) => { println!("Can not open"); if stdin().is_tty() { - (libc::STDIN_FILENO, false) + (libc::STDIN_FILENO, true) } else { return Err(ErrorKind::IoError(io::Error::new(io::ErrorKind::Other, "Failed to initialize input source. Crossterm first tried to open `/dev/tty` wereafter `libc::STDIN_FILENO`, but both could not be used."))); } From f0a7b11a3f357d0573c295615a5b358717cecd2d Mon Sep 17 00:00:00 2001 From: Timon Post Date: Mon, 21 Sep 2020 12:54:53 +0200 Subject: [PATCH 19/19] a --- examples/test.rs | 29 +++++++++++++++++++++ src/event/source/unix.rs | 2 +- src/event/sys/unix/file_descriptor.rs | 37 ++++++++++++--------------- src/terminal/sys/unix.rs | 4 +-- 4 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 examples/test.rs diff --git a/examples/test.rs b/examples/test.rs new file mode 100644 index 000000000..d9c12530b --- /dev/null +++ b/examples/test.rs @@ -0,0 +1,29 @@ +use mio::Interest; +use signal_hook::iterator::Signals; +use std::path::Path; +use std::io; + +fn main () { + let poll = Poll::new()?; + let registry = poll.registry(); + + let tty_raw_fd = input_fd.raw_fd(); + let mut tty_ev = SourceFd(&tty_raw_fd); + registry.register(&mut tty_ev, TTY_TOKEN, Interest::READABLE)?; + + let mut signals = Signals::new(&[signal_hook::SIGWINCH])?; + registry.register(&mut signals, SIGNAL_TOKEN, Interest::READABLE)?; + +} + + +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/event/source/unix.rs b/src/event/source/unix.rs index bd6755c4c..cf5276775 100644 --- a/src/event/source/unix.rs +++ b/src/event/source/unix.rs @@ -40,7 +40,7 @@ pub(crate) struct UnixInternalEventSource { impl UnixInternalEventSource { pub fn new() -> Result { - Ok(UnixInternalEventSource::from_file_descriptor(tty_fd()?)?) + Ok(UnixInternalEventSource::from_file_descriptor(tty_fd(false)?)?) } pub(crate) fn from_file_descriptor(input_fd: FileDesc) -> Result { diff --git a/src/event/sys/unix/file_descriptor.rs b/src/event/sys/unix/file_descriptor.rs index 0def1b761..4cc762ada 100644 --- a/src/event/sys/unix/file_descriptor.rs +++ b/src/event/sys/unix/file_descriptor.rs @@ -7,6 +7,7 @@ use libc::size_t; use crate::{ErrorKind, Result}; use std::io::stdin; +use std::path::Path; /// A file descriptor wrapper. /// @@ -65,34 +66,18 @@ impl Drop for FileDesc { } /// Creates a file descriptor pointing to the standard input or `/dev/tty`. -pub fn tty_fd() -> Result { +pub fn tty_fd(close_on_drop: bool) -> Result { use crate::tty::IsTty; -// let (fd, close_on_drop) = if stdin().is_tty() { -// (libc::STDIN_FILENO, false) -// } else { -// ( -// fs::OpenOptions::new() -// .read(true) -// .write(true) -// .open("/dev/tty")? -// .into_raw_fd(), -// false, -// )== -// }; - - let (fd, close_on_drop) = match fs::OpenOptions::new() - .read(true) - .write(true) - .open("/dev/tty") { + let fd = match open_rw("/dev/tty") { Ok(tty) => { println!("Can open"); - (tty.into_raw_fd(), true) + tty } Err(e) => { println!("Can not open"); if stdin().is_tty() { - (libc::STDIN_FILENO, true) + libc::STDIN_FILENO } else { return Err(ErrorKind::IoError(io::Error::new(io::ErrorKind::Other, "Failed to initialize input source. Crossterm first tried to open `/dev/tty` wereafter `libc::STDIN_FILENO`, but both could not be used."))); } @@ -101,3 +86,15 @@ pub fn tty_fd() -> Result { 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()) +} diff --git a/src/terminal/sys/unix.rs b/src/terminal/sys/unix.rs index 24e7f1896..321d96cb5 100644 --- a/src/terminal/sys/unix.rs +++ b/src/terminal/sys/unix.rs @@ -117,7 +117,7 @@ fn raw_terminal_attr(termios: &mut Termios) { } fn get_terminal_attr() -> Result { - let fd = tty_fd()?; + let fd = tty_fd(true)?; unsafe { let mut termios = mem::zeroed(); @@ -127,7 +127,7 @@ fn get_terminal_attr() -> Result { } fn set_terminal_attr(termios: &Termios) -> Result { - let fd = tty_fd()?; + let fd = tty_fd(true)?; wrap_with_result(unsafe { tcsetattr(fd.raw_fd(), TCSANOW, termios) }) }