stub for a glutin based window

It can render the terminal but doesn't handle input of any kind.
This commit is contained in:
Wez Furlong committed 2018-02-21 00:16:03 -08:00
1 parent 5a7a543050
commit d876d23a28
2 files changed
+242 -33

No files matched your search

+118
View File
@@ -0,0 +1,118 @@
//! Generic system dependent windows via glium+glutin
#![allow(dead_code)]
use failure::Error;
use font::FontConfiguration;
use glium::{self, glutin};
use opengl::render::Renderer;
use pty::MasterPty;
use std::io::{Read, Write};
use std::process::Child;
use std::rc::Rc;
use term::{self, Terminal};
use term::hyperlink::Hyperlink;
struct Host {
display: glium::Display,
pty: MasterPty,
clipboard: Option<String>,
}
impl term::TerminalHost for Host {
fn writer(&mut self) -> &mut Write {
&mut self.pty
}
fn click_link(&mut self, _link: &Rc<Hyperlink>) {}
fn get_clipboard(&mut self) -> Result<String, Error> {
bail!("no clipboard");
}
fn set_clipboard(&mut self, _clip: Option<String>) -> Result<(), Error> {
bail!("no clipboard");
}
fn set_title(&mut self, title: &str) {
self.display.gl_window().set_title(title);
}
}
pub struct TerminalWindow {
host: Host,
renderer: Renderer,
width: u16,
height: u16,
cell_height: usize,
cell_width: usize,
terminal: Terminal,
process: Child,
}
impl TerminalWindow {
pub fn new(
event_loop: &glutin::EventsLoop,
width: u16,
height: u16,
terminal: Terminal,
pty: MasterPty,
process: Child,
fonts: FontConfiguration,
palette: term::color::ColorPalette,
) -> Result<TerminalWindow, Error> {
let (cell_height, cell_width) = {
// Urgh, this is a bit repeaty, but we need to satisfy the borrow checker
let font = fonts.default_font()?;
let metrics = font.borrow_mut().get_fallback(0)?.metrics();
(metrics.cell_height, metrics.cell_width)
};
let window = glutin::WindowBuilder::new()
.with_dimensions(width.into(), height.into())
.with_title("wezterm");
let context = glutin::ContextBuilder::new()
.with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGlEs, (2, 0)))
.with_vsync(true)
.with_pixel_format(24, 8)
.with_srgb(true);
let display =
glium::Display::new(window, context, &event_loop).map_err(|e| format_err!("{:?}", e))?;
let host = Host {
display,
pty,
clipboard: None,
};
let renderer = Renderer::new(&host.display, width, height, fonts, palette)?;
let cell_height = cell_height.ceil() as usize;
let cell_width = cell_width.ceil() as usize;
Ok(TerminalWindow {
host,
renderer,
width,
height,
cell_height,
cell_width,
terminal,
process,
})
}
pub fn paint(&mut self) -> Result<(), Error> {
let mut target = self.host.display.draw();
let res = self.renderer.paint(&mut target, &mut self.terminal);
// Ensure that we finish() the target before we let the
// error bubble up, otherwise we lose the context.
target.finish().unwrap();
res?;
Ok(())
}
pub fn handle_pty_readable_event(&mut self) {
const BUFSIZE: usize = 8192;
let mut buf = [0; BUFSIZE];
match self.host.pty.read(&mut buf) {
Ok(size) => self.terminal.advance_bytes(&buf[0..size], &mut self.host),
Err(err) => eprintln!("error reading from pty: {:?}", err),
}
}
}
+124 -33
View File
@@ -50,6 +50,7 @@ mod opengl;
mod xwindows;
#[cfg(all(unix, not(target_os = "macos")))]
use xwindows::xwin::TerminalWindow;
mod gliumwindows;
mod font;
use font::FontConfiguration;
@@ -76,41 +77,88 @@ fn get_shell() -> Result<String, Error> {
})
}
fn run() -> Result<(), Error> {
/// Drives the gui on macos. It is included in the linux build to
/// make it less likely to get broken by accident.
#[allow(dead_code)]
fn run_glium(
master: pty::MasterPty,
child: std::process::Child,
config: config::Config,
fontconfig: FontConfiguration,
terminal: term::Terminal,
initial_pixel_width: u16,
initial_pixel_height: u16,
) -> Result<(), Error> {
use glium::glutin;
let poll = Poll::new()?;
let conn = xwindows::Connection::new()?;
poll.register(&master, Token(0), Ready::readable(), PollOpt::edge())?;
let waiter = sigchld::ChildWaiter::new()?;
let mut events_loop = glium::glutin::EventsLoop::new();
let config = config::Config::load()?;
println!("Using configuration: {:#?}", config);
// First step is to figure out the font metrics so that we know how
// big things are going to be.
let fontconfig = FontConfiguration::new(config.clone());
let font = fontconfig.default_font()?;
// we always load the cell_height for font 0,
// regardless of which font we are shaping here,
// so that we can scale glyphs appropriately
let metrics = font.borrow_mut().get_fallback(0)?.metrics();
let initial_cols = 80u16;
let initial_rows = 24u16;
let initial_pixel_width = initial_cols * metrics.cell_width.ceil() as u16;
let initial_pixel_height = initial_rows * metrics.cell_height.ceil() as u16;
let (master, slave) = pty::openpty(
initial_rows,
initial_cols,
let mut window = gliumwindows::TerminalWindow::new(
&events_loop,
initial_pixel_width,
initial_pixel_height,
terminal,
master,
child,
fontconfig,
config
.colors
.map(|p| p.into())
.unwrap_or_else(term::color::ColorPalette::default),
)?;
let cmd = Command::new(get_shell()?);
let child = slave.spawn_command(cmd)?;
eprintln!("spawned: {:?}", child);
let mut events = Events::with_capacity(8);
let mut done = false;
loop {
if done {
break;
}
if poll.poll(&mut events, Some(Duration::new(0, 2000)))? != 0 {
for event in &events {
if event.token() == Token(0) && event.readiness().is_readable() {
window.handle_pty_readable_event();
window.paint().unwrap();
}
}
}
events_loop.poll_events(|event| {
match event {
glutin::Event::WindowEvent { event, .. } => match event {
// Break from the main loop when the window is closed.
glutin::WindowEvent::Closed => {
done = true;
return;
}
// glutin::ControlFlow::Break, Redraw the triangle when the
// window is resized.
glutin::WindowEvent::Resized(..) => window.paint().unwrap(),
glutin::WindowEvent::Refresh => window.paint().unwrap(),
_ => (),
},
_ => (),
}
});
}
Ok(())
}
#[cfg(all(unix, not(target_os = "macos")))]
fn run_xwindows(
master: pty::MasterPty,
child: std::process::Child,
config: config::Config,
fontconfig: FontConfiguration,
terminal: term::Terminal,
initial_pixel_width: u16,
initial_pixel_height: u16,
) -> Result<(), Error> {
let conn = xwindows::Connection::new()?;
let poll = Poll::new()?;
let waiter = sigchld::ChildWaiter::new()?;
// Ask mio to watch the pty for input from the child process
poll.register(&master, Token(0), Ready::readable(), PollOpt::edge())?;
@@ -124,11 +172,6 @@ fn run() -> Result<(), Error> {
poll.register(&waiter, Token(2), Ready::readable(), PollOpt::edge())?;
let terminal = term::Terminal::new(
initial_rows as usize,
initial_cols as usize,
config.scrollback_lines.unwrap_or(3500),
);
// let message = "; ❤ 😍🤢\n\x1b[91;mw00t\n\x1b[37;104;m bleet\x1b[0;m.";
// terminal.advance_bytes(message);
// !=
@@ -206,6 +249,54 @@ fn run() -> Result<(), Error> {
}
}
fn run() -> Result<(), Error> {
let config = config::Config::load()?;
println!("Using configuration: {:#?}", config);
// First step is to figure out the font metrics so that we know how
// big things are going to be.
let fontconfig = FontConfiguration::new(config.clone());
let font = fontconfig.default_font()?;
// we always load the cell_height for font 0,
// regardless of which font we are shaping here,
// so that we can scale glyphs appropriately
let metrics = font.borrow_mut().get_fallback(0)?.metrics();
let initial_cols = 80u16;
let initial_rows = 24u16;
let initial_pixel_width = initial_cols * metrics.cell_width.ceil() as u16;
let initial_pixel_height = initial_rows * metrics.cell_height.ceil() as u16;
let (master, slave) = pty::openpty(
initial_rows,
initial_cols,
initial_pixel_width,
initial_pixel_height,
)?;
let cmd = Command::new(get_shell()?);
let child = slave.spawn_command(cmd)?;
eprintln!("spawned: {:?}", child);
let terminal = term::Terminal::new(
initial_rows as usize,
initial_cols as usize,
config.scrollback_lines.unwrap_or(3500),
);
run_xwindows(
master,
child,
config,
fontconfig,
terminal,
initial_pixel_width,
initial_pixel_height,
)
}
fn main() {
run().unwrap();
}