Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
TimonPost committed Sep 21, 2020
1 parent 1289241 commit f0a7b11
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
29 changes: 29 additions & 0 deletions examples/test.rs
Original file line number Diff line number Diff line change
@@ -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<P: AsRef<Path>>(path: P) -> io::Result<RawFd> {
use std::fs::OpenOptions;

let file = OpenOptions::new()
.read(true)
.write(true)
.open(path)?;

Ok(file.into_raw_fd())
}
2 changes: 1 addition & 1 deletion src/event/source/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(crate) struct UnixInternalEventSource {

impl UnixInternalEventSource {
pub fn new() -> Result<Self> {
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<Self> {
Expand Down
37 changes: 17 additions & 20 deletions src/event/sys/unix/file_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use libc::size_t;

use crate::{ErrorKind, Result};
use std::io::stdin;
use std::path::Path;

/// A file descriptor wrapper.
///
Expand Down Expand Up @@ -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<FileDesc> {
pub fn tty_fd(close_on_drop: bool) -> Result<FileDesc> {
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.")));
}
Expand All @@ -101,3 +86,15 @@ pub fn tty_fd() -> Result<FileDesc> {

Ok(FileDesc::new(fd, close_on_drop))
}


fn open_rw<P: AsRef<Path>>(path: P) -> io::Result<RawFd> {
use std::fs::OpenOptions;

let file = OpenOptions::new()
.read(true)
.write(true)
.open(path)?;

Ok(file.into_raw_fd())
}
4 changes: 2 additions & 2 deletions src/terminal/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn raw_terminal_attr(termios: &mut Termios) {
}

fn get_terminal_attr() -> Result<Termios> {
let fd = tty_fd()?;
let fd = tty_fd(true)?;

unsafe {
let mut termios = mem::zeroed();
Expand All @@ -127,7 +127,7 @@ fn get_terminal_attr() -> Result<Termios> {
}

fn set_terminal_attr(termios: &Termios) -> Result<bool> {
let fd = tty_fd()?;
let fd = tty_fd(true)?;
wrap_with_result(unsafe { tcsetattr(fd.raw_fd(), TCSANOW, termios) })
}

Expand Down

0 comments on commit f0a7b11

Please sign in to comment.