2019-02-19 10:26:41 +03:00
|
|
|
use crate::istty::IsTty;
|
2018-07-21 14:39:56 +03:00
|
|
|
use failure::Error;
|
2018-07-27 18:48:34 +03:00
|
|
|
use libc::{self, poll, pollfd, winsize, POLLIN};
|
|
|
|
use signal_hook::{self, SigId};
|
2018-07-24 05:38:35 +03:00
|
|
|
use std::collections::VecDeque;
|
2018-07-22 17:03:11 +03:00
|
|
|
use std::fs::OpenOptions;
|
2018-07-24 15:45:24 +03:00
|
|
|
use std::io::{stdin, stdout, Error as IoError, ErrorKind, Read, Write};
|
2018-07-21 14:39:56 +03:00
|
|
|
use std::mem;
|
2018-07-22 21:30:26 +03:00
|
|
|
use std::ops::Deref;
|
2018-07-21 14:39:56 +03:00
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
2018-07-27 18:48:34 +03:00
|
|
|
use std::os::unix::net::UnixStream;
|
2018-07-21 20:59:41 +03:00
|
|
|
use termios::{
|
2018-07-21 21:18:24 +03:00
|
|
|
cfmakeraw, tcdrain, tcflush, tcsetattr, Termios, TCIFLUSH, TCIOFLUSH, TCOFLUSH, TCSADRAIN,
|
|
|
|
TCSAFLUSH, TCSANOW,
|
2018-07-21 20:59:41 +03:00
|
|
|
};
|
2018-07-21 14:39:56 +03:00
|
|
|
|
2019-02-19 10:26:41 +03:00
|
|
|
use crate::caps::Capabilities;
|
|
|
|
use crate::escape::csi::{DecPrivateMode, DecPrivateModeCode, Mode, CSI};
|
|
|
|
use crate::input::{InputEvent, InputParser};
|
|
|
|
use crate::render::terminfo::TerminfoRenderer;
|
|
|
|
use crate::surface::Change;
|
|
|
|
use crate::terminal::{cast, Blocking, ScreenSize, Terminal};
|
2018-07-22 21:30:26 +03:00
|
|
|
|
|
|
|
const BUF_SIZE: usize = 128;
|
2018-07-21 15:12:56 +03:00
|
|
|
|
2018-07-21 20:30:15 +03:00
|
|
|
/// Helper function to duplicate a file descriptor.
|
|
|
|
/// The duplicated descriptor will have the close-on-exec flag set.
|
|
|
|
fn dup(fd: RawFd) -> Result<RawFd, Error> {
|
|
|
|
let new_fd = unsafe { libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, 0) };
|
|
|
|
if new_fd == -1 {
|
2018-07-24 15:45:24 +03:00
|
|
|
bail!("dup of pty fd failed: {:?}", IoError::last_os_error())
|
2018-07-21 20:30:15 +03:00
|
|
|
}
|
|
|
|
Ok(new_fd)
|
2018-07-21 15:12:56 +03:00
|
|
|
}
|
|
|
|
|
2018-07-21 20:59:41 +03:00
|
|
|
pub enum Purge {
|
|
|
|
InputQueue,
|
|
|
|
OutputQueue,
|
|
|
|
InputAndOutputQueue,
|
|
|
|
}
|
|
|
|
|
2018-07-21 21:18:24 +03:00
|
|
|
pub enum SetAttributeWhen {
|
|
|
|
/// changes are applied immediately
|
|
|
|
Now,
|
|
|
|
/// Apply once the current output queue has drained
|
|
|
|
AfterDrainOutputQueue,
|
|
|
|
/// Wait for the current output queue to drain, then
|
|
|
|
/// discard any unread input
|
|
|
|
AfterDrainOutputQueuePurgeInputQueue,
|
|
|
|
}
|
|
|
|
|
2018-07-21 20:30:15 +03:00
|
|
|
pub trait UnixTty {
|
|
|
|
fn get_size(&mut self) -> Result<winsize, Error>;
|
|
|
|
fn set_size(&mut self, size: winsize) -> Result<(), Error>;
|
|
|
|
fn get_termios(&mut self) -> Result<Termios, Error>;
|
2018-07-21 21:18:24 +03:00
|
|
|
fn set_termios(&mut self, termios: &Termios, when: SetAttributeWhen) -> Result<(), Error>;
|
2018-07-21 20:59:41 +03:00
|
|
|
/// Waits until all written data has been transmitted.
|
|
|
|
fn drain(&mut self) -> Result<(), Error>;
|
|
|
|
fn purge(&mut self, purge: Purge) -> Result<(), Error>;
|
2018-07-21 20:30:15 +03:00
|
|
|
}
|
|
|
|
|
2018-07-22 21:30:26 +03:00
|
|
|
struct Fd {
|
2018-07-21 20:30:15 +03:00
|
|
|
fd: RawFd,
|
|
|
|
}
|
|
|
|
|
2018-07-22 21:30:26 +03:00
|
|
|
impl Drop for Fd {
|
2018-07-21 20:30:15 +03:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
libc::close(self.fd);
|
2018-07-21 15:12:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-22 21:30:26 +03:00
|
|
|
|
|
|
|
impl Fd {
|
|
|
|
pub fn new<S: AsRawFd>(s: &S) -> Result<Self, Error> {
|
|
|
|
ensure!(s.is_tty(), "Can only construct a TtyHandle from a tty");
|
|
|
|
let fd = dup(s.as_raw_fd())?;
|
|
|
|
Ok(Self { fd })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Fd {
|
|
|
|
type Target = RawFd;
|
|
|
|
fn deref(&self) -> &RawFd {
|
|
|
|
&self.fd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TtyReadHandle {
|
|
|
|
fd: Fd,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TtyReadHandle {
|
|
|
|
fn new(fd: Fd) -> Self {
|
|
|
|
Self { fd }
|
|
|
|
}
|
2018-07-24 05:38:35 +03:00
|
|
|
|
|
|
|
fn set_blocking(&mut self, blocking: Blocking) -> Result<(), Error> {
|
|
|
|
let value: libc::c_int = match blocking {
|
2018-07-24 16:25:58 +03:00
|
|
|
Blocking::Wait => 0,
|
|
|
|
Blocking::DoNotWait => 1,
|
2018-07-24 05:38:35 +03:00
|
|
|
};
|
|
|
|
if unsafe { libc::ioctl(*self.fd, libc::FIONBIO, &value as *const _) } != 0 {
|
2018-07-24 15:45:24 +03:00
|
|
|
bail!("failed to ioctl(FIONBIO): {:?}", IoError::last_os_error());
|
2018-07-24 05:38:35 +03:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-07-22 21:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Read for TtyReadHandle {
|
2018-07-24 15:45:24 +03:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, IoError> {
|
2018-07-22 21:30:26 +03:00
|
|
|
let size = unsafe { libc::read(*self.fd, buf.as_mut_ptr() as *mut _, buf.len()) };
|
2018-07-21 20:30:15 +03:00
|
|
|
if size == -1 {
|
2018-07-24 15:45:24 +03:00
|
|
|
Err(IoError::last_os_error())
|
2018-07-21 20:30:15 +03:00
|
|
|
} else {
|
|
|
|
Ok(size as usize)
|
2018-07-21 15:12:56 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-22 21:30:26 +03:00
|
|
|
}
|
2018-07-21 15:12:56 +03:00
|
|
|
|
2018-07-22 21:30:26 +03:00
|
|
|
pub struct TtyWriteHandle {
|
|
|
|
fd: Fd,
|
|
|
|
write_buffer: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TtyWriteHandle {
|
|
|
|
fn new(fd: Fd) -> Self {
|
|
|
|
Self {
|
|
|
|
fd,
|
|
|
|
write_buffer: Vec::with_capacity(BUF_SIZE),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:45:24 +03:00
|
|
|
fn flush_local_buffer(&mut self) -> Result<(), IoError> {
|
2019-02-24 21:33:21 +03:00
|
|
|
if !self.write_buffer.is_empty() {
|
2018-07-22 21:30:26 +03:00
|
|
|
do_write(*self.fd, &self.write_buffer)?;
|
|
|
|
self.write_buffer.clear();
|
|
|
|
}
|
2018-07-21 20:30:15 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:45:24 +03:00
|
|
|
fn do_write(fd: RawFd, buf: &[u8]) -> Result<usize, IoError> {
|
2018-07-22 21:30:26 +03:00
|
|
|
let size = unsafe { libc::write(fd, buf.as_ptr() as *const _, buf.len()) };
|
|
|
|
if size == -1 {
|
2018-07-24 15:45:24 +03:00
|
|
|
Err(IoError::last_os_error())
|
2018-07-22 21:30:26 +03:00
|
|
|
} else {
|
|
|
|
Ok(size as usize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Write for TtyWriteHandle {
|
2018-07-24 15:45:24 +03:00
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize, IoError> {
|
2018-07-22 21:30:26 +03:00
|
|
|
if self.write_buffer.len() + buf.len() > self.write_buffer.capacity() {
|
|
|
|
self.flush()?;
|
|
|
|
}
|
|
|
|
if buf.len() >= self.write_buffer.capacity() {
|
|
|
|
do_write(*self.fd, buf)
|
2018-07-21 20:30:15 +03:00
|
|
|
} else {
|
2018-07-22 21:30:26 +03:00
|
|
|
self.write_buffer.write(buf)
|
2018-07-21 15:12:56 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-21 14:39:56 +03:00
|
|
|
|
2018-07-24 15:45:24 +03:00
|
|
|
fn flush(&mut self) -> Result<(), IoError> {
|
2018-07-22 21:30:26 +03:00
|
|
|
self.flush_local_buffer()?;
|
|
|
|
self.drain()
|
2018-07-24 15:45:24 +03:00
|
|
|
.map_err(|e| IoError::new(ErrorKind::Other, format!("{}", e)))?;
|
2018-07-22 21:30:26 +03:00
|
|
|
Ok(())
|
2018-07-21 20:30:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 21:30:26 +03:00
|
|
|
impl UnixTty for TtyWriteHandle {
|
2018-07-21 20:30:15 +03:00
|
|
|
fn get_size(&mut self) -> Result<winsize, Error> {
|
|
|
|
let mut size: winsize = unsafe { mem::zeroed() };
|
2018-07-22 21:30:26 +03:00
|
|
|
if unsafe { libc::ioctl(*self.fd, libc::TIOCGWINSZ, &mut size) } != 0 {
|
2018-07-24 15:45:24 +03:00
|
|
|
bail!("failed to ioctl(TIOCGWINSZ): {}", IoError::last_os_error());
|
2018-07-21 20:30:15 +03:00
|
|
|
}
|
|
|
|
Ok(size)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_size(&mut self, size: winsize) -> Result<(), Error> {
|
2018-07-22 21:30:26 +03:00
|
|
|
if unsafe { libc::ioctl(*self.fd, libc::TIOCSWINSZ, &size as *const _) } != 0 {
|
2018-07-21 20:30:15 +03:00
|
|
|
bail!(
|
|
|
|
"failed to ioctl(TIOCSWINSZ): {:?}",
|
2018-07-24 15:45:24 +03:00
|
|
|
IoError::last_os_error()
|
2018-07-21 20:30:15 +03:00
|
|
|
);
|
2018-07-21 14:39:56 +03:00
|
|
|
}
|
2018-07-21 20:30:15 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_termios(&mut self) -> Result<Termios, Error> {
|
2018-07-22 21:30:26 +03:00
|
|
|
Termios::from_fd(*self.fd).map_err(|e| format_err!("get_termios failed: {}", e))
|
2018-07-21 20:30:15 +03:00
|
|
|
}
|
|
|
|
|
2018-07-21 21:18:24 +03:00
|
|
|
fn set_termios(&mut self, termios: &Termios, when: SetAttributeWhen) -> Result<(), Error> {
|
|
|
|
let when = match when {
|
|
|
|
SetAttributeWhen::Now => TCSANOW,
|
|
|
|
SetAttributeWhen::AfterDrainOutputQueue => TCSADRAIN,
|
|
|
|
SetAttributeWhen::AfterDrainOutputQueuePurgeInputQueue => TCSAFLUSH,
|
|
|
|
};
|
2018-07-22 21:30:26 +03:00
|
|
|
tcsetattr(*self.fd, when, termios).map_err(|e| format_err!("set_termios failed: {}", e))
|
2018-07-21 14:39:56 +03:00
|
|
|
}
|
2018-07-21 20:59:41 +03:00
|
|
|
|
|
|
|
fn drain(&mut self) -> Result<(), Error> {
|
2018-07-22 21:30:26 +03:00
|
|
|
tcdrain(*self.fd).map_err(|e| format_err!("tcdrain failed: {}", e))
|
2018-07-21 20:59:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn purge(&mut self, purge: Purge) -> Result<(), Error> {
|
|
|
|
let param = match purge {
|
|
|
|
Purge::InputQueue => TCIFLUSH,
|
|
|
|
Purge::OutputQueue => TCOFLUSH,
|
|
|
|
Purge::InputAndOutputQueue => TCIOFLUSH,
|
|
|
|
};
|
2018-07-22 21:30:26 +03:00
|
|
|
tcflush(*self.fd, param).map_err(|e| format_err!("tcflush failed: {}", e))
|
2018-07-21 20:59:41 +03:00
|
|
|
}
|
2018-07-21 14:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A unix style terminal
|
|
|
|
pub struct UnixTerminal {
|
2018-07-22 21:30:26 +03:00
|
|
|
read: TtyReadHandle,
|
|
|
|
write: TtyWriteHandle,
|
2018-07-21 14:39:56 +03:00
|
|
|
saved_termios: Termios,
|
2018-07-21 23:19:38 +03:00
|
|
|
renderer: TerminfoRenderer,
|
2018-07-24 05:38:35 +03:00
|
|
|
input_parser: InputParser,
|
|
|
|
input_queue: Option<VecDeque<InputEvent>>,
|
2018-07-27 18:48:34 +03:00
|
|
|
sigwinch_id: SigId,
|
|
|
|
sigwinch_pipe: UnixStream,
|
2018-07-28 17:47:32 +03:00
|
|
|
caps: Capabilities,
|
2018-07-21 14:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UnixTerminal {
|
|
|
|
/// Attempt to create an instance from the stdin and stdout of the
|
|
|
|
/// process. This will fail unless both are associated with a tty.
|
2018-07-21 20:30:15 +03:00
|
|
|
/// Note that this will duplicate the underlying file descriptors
|
|
|
|
/// and will no longer participate in the stdin/stdout locking
|
|
|
|
/// provided by the rust standard library.
|
2018-07-21 23:19:38 +03:00
|
|
|
pub fn new_from_stdio(caps: Capabilities) -> Result<UnixTerminal, Error> {
|
|
|
|
Self::new_with(caps, &stdin(), &stdout())
|
|
|
|
}
|
2018-07-21 14:39:56 +03:00
|
|
|
|
2018-07-21 23:19:38 +03:00
|
|
|
pub fn new_with<A: AsRawFd, B: AsRawFd>(
|
|
|
|
caps: Capabilities,
|
|
|
|
read: &A,
|
|
|
|
write: &B,
|
|
|
|
) -> Result<UnixTerminal, Error> {
|
2018-07-27 18:48:34 +03:00
|
|
|
let mut read = TtyReadHandle::new(Fd::new(read)?);
|
2018-07-22 21:30:26 +03:00
|
|
|
let mut write = TtyWriteHandle::new(Fd::new(write)?);
|
2018-07-21 20:30:15 +03:00
|
|
|
let saved_termios = write.get_termios()?;
|
2018-07-28 17:47:32 +03:00
|
|
|
let renderer = TerminfoRenderer::new(caps.clone());
|
2018-07-24 05:38:35 +03:00
|
|
|
let input_parser = InputParser::new();
|
|
|
|
let input_queue = None;
|
2018-07-21 14:39:56 +03:00
|
|
|
|
2018-07-27 18:48:34 +03:00
|
|
|
let (sigwinch_pipe, pipe_write) = UnixStream::pair()?;
|
|
|
|
let sigwinch_id = signal_hook::pipe::register(libc::SIGWINCH, pipe_write)?;
|
|
|
|
sigwinch_pipe.set_nonblocking(true)?;
|
|
|
|
|
|
|
|
read.set_blocking(Blocking::DoNotWait)?;
|
|
|
|
|
2018-07-21 14:39:56 +03:00
|
|
|
Ok(UnixTerminal {
|
2018-07-28 17:47:32 +03:00
|
|
|
caps,
|
2018-07-21 20:30:15 +03:00
|
|
|
read,
|
|
|
|
write,
|
2018-07-21 14:39:56 +03:00
|
|
|
saved_termios,
|
2018-07-21 23:19:38 +03:00
|
|
|
renderer,
|
2018-07-24 05:38:35 +03:00
|
|
|
input_parser,
|
|
|
|
input_queue,
|
2018-07-27 18:48:34 +03:00
|
|
|
sigwinch_pipe,
|
|
|
|
sigwinch_id,
|
2018-07-21 14:39:56 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to explicitly open a handle to the terminal device
|
|
|
|
/// (/dev/tty) and build a `UnixTerminal` from there. This will
|
|
|
|
/// yield a terminal even if the stdio streams have been redirected,
|
|
|
|
/// provided that the process has an associated controlling terminal.
|
2018-07-21 23:19:38 +03:00
|
|
|
pub fn new(caps: Capabilities) -> Result<UnixTerminal, Error> {
|
2018-07-21 14:39:56 +03:00
|
|
|
let file = OpenOptions::new().read(true).write(true).open("/dev/tty")?;
|
2018-07-21 23:19:38 +03:00
|
|
|
Self::new_with(caps, &file, &file)
|
2018-07-21 14:39:56 +03:00
|
|
|
}
|
2018-07-27 18:48:34 +03:00
|
|
|
|
|
|
|
/// Test whether we caught delivery of SIGWINCH.
|
|
|
|
/// If so, yield an `InputEvent` with the current size of the tty.
|
|
|
|
fn caught_sigwinch(&mut self) -> Result<Option<InputEvent>, Error> {
|
|
|
|
let mut buf = [0u8; 64];
|
|
|
|
|
|
|
|
match self.sigwinch_pipe.read(&mut buf) {
|
|
|
|
Ok(_) => {
|
|
|
|
let size = self.write.get_size()?;
|
|
|
|
Ok(Some(InputEvent::Resized {
|
|
|
|
rows: cast(size.ws_row)?,
|
|
|
|
cols: cast(size.ws_col)?,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
Err(ref e)
|
|
|
|
if e.kind() == ErrorKind::WouldBlock || e.kind() == ErrorKind::Interrupted =>
|
|
|
|
{
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
Err(e) => Err(format_err!("failed to read sigwinch pipe {}", e)),
|
|
|
|
}
|
|
|
|
}
|
2018-07-21 14:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Terminal for UnixTerminal {
|
|
|
|
fn set_raw_mode(&mut self) -> Result<(), Error> {
|
2018-07-21 20:30:15 +03:00
|
|
|
let mut raw = self.write.get_termios()?;
|
2018-07-21 14:39:56 +03:00
|
|
|
cfmakeraw(&mut raw);
|
2018-07-21 20:30:15 +03:00
|
|
|
self.write
|
2018-07-21 21:18:24 +03:00
|
|
|
.set_termios(&raw, SetAttributeWhen::AfterDrainOutputQueuePurgeInputQueue)
|
2018-07-28 07:30:49 +03:00
|
|
|
.map_err(|e| format_err!("failed to set raw mode: {}", e))?;
|
|
|
|
|
2018-07-28 17:47:32 +03:00
|
|
|
macro_rules! decset {
|
|
|
|
($variant:ident) => {
|
|
|
|
write!(
|
|
|
|
self.write,
|
|
|
|
"{}",
|
|
|
|
CSI::Mode(Mode::SetDecPrivateMode(DecPrivateMode::Code(
|
|
|
|
DecPrivateModeCode::$variant
|
|
|
|
)))
|
|
|
|
)?;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.caps.bracketed_paste() {
|
|
|
|
decset!(BracketedPaste);
|
|
|
|
}
|
|
|
|
if self.caps.mouse_reporting() {
|
|
|
|
decset!(AnyEventMouse);
|
|
|
|
decset!(SGRMouse);
|
|
|
|
}
|
2018-07-28 07:30:49 +03:00
|
|
|
self.write.flush()?;
|
|
|
|
|
|
|
|
Ok(())
|
2018-07-21 14:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_screen_size(&mut self) -> Result<ScreenSize, Error> {
|
2018-07-21 20:30:15 +03:00
|
|
|
let size = self.write.get_size()?;
|
2018-07-21 14:39:56 +03:00
|
|
|
Ok(ScreenSize {
|
|
|
|
rows: cast(size.ws_row)?,
|
|
|
|
cols: cast(size.ws_col)?,
|
|
|
|
xpixel: cast(size.ws_xpixel)?,
|
|
|
|
ypixel: cast(size.ws_ypixel)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_screen_size(&mut self, size: ScreenSize) -> Result<(), Error> {
|
|
|
|
let size = winsize {
|
|
|
|
ws_row: cast(size.rows)?,
|
|
|
|
ws_col: cast(size.cols)?,
|
|
|
|
ws_xpixel: cast(size.xpixel)?,
|
|
|
|
ws_ypixel: cast(size.ypixel)?,
|
|
|
|
};
|
|
|
|
|
2018-07-21 20:30:15 +03:00
|
|
|
self.write.set_size(size)
|
2018-07-21 14:39:56 +03:00
|
|
|
}
|
2018-07-21 23:19:38 +03:00
|
|
|
fn render(&mut self, changes: &[Change]) -> Result<(), Error> {
|
|
|
|
self.renderer
|
|
|
|
.render_to(changes, &mut self.read, &mut self.write)
|
|
|
|
}
|
2018-07-22 21:30:26 +03:00
|
|
|
fn flush(&mut self) -> Result<(), Error> {
|
|
|
|
self.write
|
|
|
|
.flush()
|
|
|
|
.map_err(|e| format_err!("flush failed: {}", e))
|
|
|
|
}
|
2018-07-24 05:38:35 +03:00
|
|
|
|
|
|
|
fn poll_input(&mut self, blocking: Blocking) -> Result<Option<InputEvent>, Error> {
|
|
|
|
if let Some(ref mut queue) = self.input_queue {
|
|
|
|
if let Some(event) = queue.pop_front() {
|
|
|
|
return Ok(Some(event));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 18:48:34 +03:00
|
|
|
// Some unfortunately verbose code here. In order to safely hook and process
|
|
|
|
// SIGWINCH we need to use the self-pipe trick to deliver signals to a pipe
|
|
|
|
// so that we can use poll(2) to wait for events on both the tty input and
|
|
|
|
// the sigwinch pipe at the same time. In theory we could do away with this
|
|
|
|
// and use sigaction to register SIGWINCH without SA_RESTART set; that way
|
|
|
|
// we could do a blocking read and have it get EINTR on a resize.
|
|
|
|
// Doing such a thing may introduce more problems for other components in
|
|
|
|
// the rust crate ecosystem if they're not ready to deal with EINTR, so
|
|
|
|
// we opt to take on the complexity here to make things overall easier to
|
|
|
|
// integrate.
|
|
|
|
|
|
|
|
let mut pfd = [
|
|
|
|
pollfd {
|
|
|
|
fd: self.read.fd.fd,
|
|
|
|
events: POLLIN,
|
|
|
|
revents: 0,
|
|
|
|
},
|
|
|
|
pollfd {
|
|
|
|
fd: self.sigwinch_pipe.as_raw_fd(),
|
|
|
|
events: POLLIN,
|
|
|
|
revents: 0,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let poll_result = unsafe {
|
|
|
|
poll(
|
|
|
|
pfd.as_mut_ptr(),
|
|
|
|
pfd.len() as _,
|
|
|
|
if blocking == Blocking::DoNotWait {
|
|
|
|
0 // Immediate
|
|
|
|
} else {
|
|
|
|
-1 // Infinite
|
|
|
|
},
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if poll_result < 0 {
|
|
|
|
let err = IoError::last_os_error();
|
|
|
|
|
|
|
|
if err.kind() == ErrorKind::Interrupted {
|
|
|
|
// SIGWINCH may have been the source of the interrupt.
|
|
|
|
// Check for that now so that we reduce the latency of
|
|
|
|
// processing the resize
|
|
|
|
if let Some(resize) = self.caught_sigwinch()? {
|
|
|
|
return Ok(Some(resize));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
return Err(format_err!("poll(2) error: {}", err));
|
|
|
|
}
|
2018-07-24 05:38:35 +03:00
|
|
|
|
2018-07-27 18:48:34 +03:00
|
|
|
if pfd[1].revents != 0 {
|
|
|
|
// SIGWINCH received via our pipe?
|
|
|
|
if let Some(resize) = self.caught_sigwinch()? {
|
|
|
|
return Ok(Some(resize));
|
2018-07-24 05:38:35 +03:00
|
|
|
}
|
2018-07-27 18:48:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if pfd[0].revents != 0 {
|
|
|
|
let mut buf = [0u8; 64];
|
|
|
|
match self.read.read(&mut buf) {
|
|
|
|
Ok(n) => {
|
|
|
|
// A little bit of a dance with moving the queue out of self
|
|
|
|
// to appease the borrow checker. We'll need to be sure to
|
|
|
|
// move it back before we return!
|
|
|
|
let mut queue = match self.input_queue.take() {
|
|
|
|
Some(queue) => queue,
|
|
|
|
None => VecDeque::new(),
|
|
|
|
};
|
|
|
|
self.input_parser
|
|
|
|
.parse(&buf[0..n], |evt| queue.push_back(evt), n == buf.len());
|
|
|
|
let result = queue.pop_front();
|
|
|
|
// Move the queue back into self before we leave this scope
|
|
|
|
self.input_queue = Some(queue);
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
Err(ref e)
|
|
|
|
if e.kind() == ErrorKind::WouldBlock || e.kind() == ErrorKind::Interrupted =>
|
|
|
|
{
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
Err(e) => Err(format_err!("failed to read input {}", e)),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
2018-07-24 05:38:35 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-21 14:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for UnixTerminal {
|
|
|
|
fn drop(&mut self) {
|
2018-07-28 17:47:32 +03:00
|
|
|
macro_rules! decreset {
|
|
|
|
($variant:ident) => {
|
|
|
|
write!(
|
|
|
|
self.write,
|
|
|
|
"{}",
|
|
|
|
CSI::Mode(Mode::ResetDecPrivateMode(DecPrivateMode::Code(
|
|
|
|
DecPrivateModeCode::$variant
|
|
|
|
)))
|
2019-02-16 11:08:38 +03:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-07-28 17:47:32 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if self.caps.bracketed_paste() {
|
|
|
|
decreset!(BracketedPaste);
|
|
|
|
}
|
|
|
|
if self.caps.mouse_reporting() {
|
|
|
|
decreset!(SGRMouse);
|
|
|
|
decreset!(AnyEventMouse);
|
|
|
|
}
|
2018-07-28 07:30:49 +03:00
|
|
|
self.write.flush().unwrap();
|
|
|
|
|
2018-07-27 18:48:34 +03:00
|
|
|
signal_hook::unregister(self.sigwinch_id);
|
2018-07-21 20:30:15 +03:00
|
|
|
self.write
|
2018-07-21 21:18:24 +03:00
|
|
|
.set_termios(&self.saved_termios, SetAttributeWhen::Now)
|
2018-07-21 14:39:56 +03:00
|
|
|
.expect("failed to restore original termios state");
|
|
|
|
}
|
|
|
|
}
|