make pty reading slightly more portable to win

This commit is contained in:
Wez Furlong committed 2019-02-16 15:03:34 -08:00
1 parent 66a6b4b912
commit f42463b601
3 files changed
+34 -43

No files matched your search

+3 -4
View File
@@ -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> {
+4 -39
View File
@@ -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<usize, io::Error> {
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<Self, Error> {
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 {
+27
View File
@@ -79,6 +79,24 @@ fn dup(fd: RawFd) -> Result<RawFd, Error> {
}
}
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<Self, Error> {
let fd = dup(self.fd)?;
Ok(Self { fd })
}
}
impl AsRawFd for MasterPty {