From f42463b601687eb5a91a0320e52d9fc7a6c2e283 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 16 Feb 2019 15:03:34 -0800 Subject: [PATCH] make pty reading slightly more portable to win --- src/gliumwindows.rs | 7 +++---- src/guiloop/glutinloop.rs | 43 ++++----------------------------------- src/pty.rs | 27 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 43 deletions(-) diff --git a/src/gliumwindows.rs b/src/gliumwindows.rs index 9012eccff..7686771fa 100644 --- a/src/gliumwindows.rs +++ b/src/gliumwindows.rs @@ -1,5 +1,6 @@ //! Generic system dependent windows via glium+glutin +use super::MasterPty; use clipboard::{Clipboard, ClipboardImpl, Paste}; use config::Config; use failure::Error; @@ -9,9 +10,7 @@ use glium::glutin::{self, ElementState, MouseCursor}; use guiloop::{GuiEventLoop, SessionTerminated}; use opengl::render::Renderer; use opengl::textureatlas::OutOfTextureSpace; -use pty::MasterPty; use std::io::Write; -use std::os::unix::io::{AsRawFd, RawFd}; use std::process::{Child, Command}; use std::rc::Rc; use term::KeyCode; @@ -194,8 +193,8 @@ impl TerminalWindow { self.host.display.gl_window().id() } - pub fn pty_fd(&self) -> RawFd { - self.host.pty.as_raw_fd() + pub fn pty(&self) -> &MasterPty { + &self.host.pty } pub fn paint(&mut self) -> Result<(), Error> { diff --git a/src/guiloop/glutinloop.rs b/src/guiloop/glutinloop.rs index acd069e24..932434564 100644 --- a/src/guiloop/glutinloop.rs +++ b/src/guiloop/glutinloop.rs @@ -4,9 +4,7 @@ use glium; use glium::glutin::EventsLoopProxy; use std::cell::RefCell; use std::collections::HashMap; -use std::io::{self, Read}; -#[cfg(unix)] -use std::os::unix::io::RawFd; +use std::io::Read; use std::rc::Rc; use std::sync::mpsc::{self, Receiver, Sender, TryRecvError}; use std::thread; @@ -47,20 +45,6 @@ enum IOEvent { Terminated { window_id: WindowId }, } -struct ReadWrap { - fd: RawFd, -} -impl io::Read for ReadWrap { - fn read(&mut self, buf: &mut [u8]) -> Result { - let size = unsafe { libc::read(self.fd, buf.as_mut_ptr() as *mut _, buf.len()) }; - if size == -1 { - Err(io::Error::last_os_error()) - } else { - Ok(size as usize) - } - } -} - /// This struct holds references to Windows. /// The primary mapping is from `WindowId` -> `TerminalWindow`. #[derive(Default)] @@ -84,24 +68,6 @@ pub struct GuiEventLoop { tick_rx: Receiver<()>, } -fn clear_nonblocking(fd: RawFd) -> Result<(), Error> { - let flags = unsafe { libc::fcntl(fd, libc::F_GETFL, 0) }; - if flags == -1 { - bail!( - "fcntl to read flags failed: {:?}", - io::Error::last_os_error() - ); - } - let result = unsafe { libc::fcntl(fd, libc::F_SETFL, flags & !libc::O_NONBLOCK) }; - if result == -1 { - bail!( - "fcntl to set NONBLOCK failed: {:?}", - io::Error::last_os_error() - ); - } - Ok(()) -} - impl GuiEventLoop { pub fn new() -> Result { let event_loop = glium::glutin::EventsLoop::new(); @@ -144,19 +110,18 @@ impl GuiEventLoop { /// Add a window to the event loop and run it. pub fn add_window(&self, window: gliumwindows::TerminalWindow) -> Result<(), Error> { let window_id = window.window_id(); - let fd = window.pty_fd(); + let mut pty = window.pty().try_clone()?; + pty.clear_nonblocking()?; let mut windows = self.windows.borrow_mut(); windows.by_id.insert(window_id, window); let tx = self.poll_tx.clone(); thread::spawn(move || { - clear_nonblocking(fd).unwrap(); - let mut fd = ReadWrap { fd }; const BUFSIZE: usize = 8192; let mut buf = [0; BUFSIZE]; loop { - match fd.read(&mut buf) { + match pty.read(&mut buf) { Ok(size) => { if tx .send(IOEvent::Data { diff --git a/src/pty.rs b/src/pty.rs index 1fd2f5820..54356f9b2 100644 --- a/src/pty.rs +++ b/src/pty.rs @@ -79,6 +79,24 @@ fn dup(fd: RawFd) -> Result { } } +fn clear_nonblocking(fd: RawFd) -> Result<(), Error> { + let flags = unsafe { libc::fcntl(fd, libc::F_GETFL, 0) }; + if flags == -1 { + bail!( + "fcntl to read flags failed: {:?}", + io::Error::last_os_error() + ); + } + let result = unsafe { libc::fcntl(fd, libc::F_SETFL, flags & !libc::O_NONBLOCK) }; + if result == -1 { + bail!( + "fcntl to set NONBLOCK failed: {:?}", + io::Error::last_os_error() + ); + } + Ok(()) +} + fn set_nonblocking(fd: RawFd) -> Result<(), Error> { let flags = unsafe { libc::fcntl(fd, libc::F_GETFL, 0) }; if flags == -1 { @@ -249,6 +267,15 @@ impl MasterPty { } Ok(size) } + + pub fn clear_nonblocking(&self) -> Result<(), Error> { + clear_nonblocking(self.fd) + } + + pub fn try_clone(&self) -> Result { + let fd = dup(self.fd)?; + Ok(Self { fd }) + } } impl AsRawFd for MasterPty {