2016-05-25 03:55:51 +00:00
|
|
|
//! Alacritty - The GPU Enhanced Terminal
|
|
|
|
#![feature(question_mark)]
|
Initial ANSI parser implementation
This is the initial terminal stream parsing implementation for
Alacritty. There are currently several TODOs, FIXMEs, and unimplemented!
things scattered about still, but what's here is good enough to
correctly parse my zsh startup.
The `Parser` implementation is largely based on the suck-less _simple
terminal_ parser. Because this is Rust and Rust has a fantastic type
system, some improvements are possible. First, `Parser` is a struct, and
its data is stored internally instead of statically. Second, there's no
terminal updates hard-coded into the parser. Instead, `Parser` is
generic over a `Handler` type which has methods for all of the actions
supported by the parser. Because Parser is generic, it should be
possible (with proper inlining) to have equivalent performance to the
hard-coded version.
In addition to using _simple terminal_ as a reference, there's a doc in
Alacritty's repository `docs/ansicode.txt`, a summary of the ANSI
terminal protocol, which has been referenced extensively.
There's probably a large number escapes we don't handle, and that's ok.
There's a lot that aren't necessary for everyday terminal usage. If you
feel like something that's not supported should be, feel free to add it.
Please try not to become overzealous and adding support for sequences
only used by folks trapped in 1988.
2016-05-29 05:09:25 +00:00
|
|
|
#![feature(range_contains)]
|
|
|
|
#![feature(inclusive_range_syntax)]
|
|
|
|
#![feature(io)]
|
2016-05-31 03:44:37 +00:00
|
|
|
#![feature(unicode)]
|
2016-05-25 03:55:51 +00:00
|
|
|
|
2016-02-21 23:20:03 +00:00
|
|
|
extern crate fontconfig;
|
|
|
|
extern crate freetype;
|
|
|
|
extern crate libc;
|
2016-02-24 04:42:58 +00:00
|
|
|
extern crate glutin;
|
|
|
|
extern crate cgmath;
|
2016-02-25 03:08:57 +00:00
|
|
|
extern crate euclid;
|
2016-02-21 23:20:03 +00:00
|
|
|
|
Initial ANSI parser implementation
This is the initial terminal stream parsing implementation for
Alacritty. There are currently several TODOs, FIXMEs, and unimplemented!
things scattered about still, but what's here is good enough to
correctly parse my zsh startup.
The `Parser` implementation is largely based on the suck-less _simple
terminal_ parser. Because this is Rust and Rust has a fantastic type
system, some improvements are possible. First, `Parser` is a struct, and
its data is stored internally instead of statically. Second, there's no
terminal updates hard-coded into the parser. Instead, `Parser` is
generic over a `Handler` type which has methods for all of the actions
supported by the parser. Because Parser is generic, it should be
possible (with proper inlining) to have equivalent performance to the
hard-coded version.
In addition to using _simple terminal_ as a reference, there's a doc in
Alacritty's repository `docs/ansicode.txt`, a summary of the ANSI
terminal protocol, which has been referenced extensively.
There's probably a large number escapes we don't handle, and that's ok.
There's a lot that aren't necessary for everyday terminal usage. If you
feel like something that's not supported should be, feel free to add it.
Please try not to become overzealous and adding support for sequences
only used by folks trapped in 1988.
2016-05-29 05:09:25 +00:00
|
|
|
#[macro_use]
|
|
|
|
mod macros;
|
|
|
|
|
2016-02-21 23:20:03 +00:00
|
|
|
mod list_fonts;
|
2016-02-22 03:44:54 +00:00
|
|
|
mod text;
|
2016-02-26 04:59:21 +00:00
|
|
|
mod renderer;
|
2016-04-10 23:19:39 +00:00
|
|
|
mod grid;
|
2016-05-21 18:08:50 +00:00
|
|
|
mod meter;
|
2016-05-25 03:55:51 +00:00
|
|
|
mod tty;
|
Initial ANSI parser implementation
This is the initial terminal stream parsing implementation for
Alacritty. There are currently several TODOs, FIXMEs, and unimplemented!
things scattered about still, but what's here is good enough to
correctly parse my zsh startup.
The `Parser` implementation is largely based on the suck-less _simple
terminal_ parser. Because this is Rust and Rust has a fantastic type
system, some improvements are possible. First, `Parser` is a struct, and
its data is stored internally instead of statically. Second, there's no
terminal updates hard-coded into the parser. Instead, `Parser` is
generic over a `Handler` type which has methods for all of the actions
supported by the parser. Because Parser is generic, it should be
possible (with proper inlining) to have equivalent performance to the
hard-coded version.
In addition to using _simple terminal_ as a reference, there's a doc in
Alacritty's repository `docs/ansicode.txt`, a summary of the ANSI
terminal protocol, which has been referenced extensively.
There's probably a large number escapes we don't handle, and that's ok.
There's a lot that aren't necessary for everyday terminal usage. If you
feel like something that's not supported should be, feel free to add it.
Please try not to become overzealous and adding support for sequences
only used by folks trapped in 1988.
2016-05-29 05:09:25 +00:00
|
|
|
mod ansi;
|
2016-05-31 03:44:37 +00:00
|
|
|
mod term;
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::io::{BufReader, Read, BufRead, Write, BufWriter};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::fs::File;
|
|
|
|
|
|
|
|
use std::os::unix::io::{FromRawFd, AsRawFd};
|
2016-02-21 23:20:03 +00:00
|
|
|
|
2016-02-26 04:59:21 +00:00
|
|
|
use renderer::{Glyph, QuadRenderer};
|
2016-02-27 22:45:38 +00:00
|
|
|
use text::FontDesc;
|
2016-04-10 23:19:39 +00:00
|
|
|
use grid::Grid;
|
2016-05-31 03:44:37 +00:00
|
|
|
use term::Term;
|
|
|
|
use meter::Meter;
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, Copy, Clone, Default)]
|
|
|
|
pub struct Rgb {
|
|
|
|
r: u8,
|
|
|
|
g: u8,
|
|
|
|
b: u8,
|
|
|
|
}
|
2016-02-24 04:42:58 +00:00
|
|
|
|
2016-05-21 04:36:28 +00:00
|
|
|
mod gl {
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs"));
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:19:39 +00:00
|
|
|
static INIT_LIST: &'static str = "abcdefghijklmnopqrstuvwxyz\
|
|
|
|
ABCDEFGHIJKLMNOPQRSTUVWXYZ\
|
|
|
|
01234567890\
|
2016-05-31 03:44:37 +00:00
|
|
|
~`!@#$%^&*()[]{}-_=+\\|\"'/?.,<>;:█└│├─➜";
|
2016-02-24 04:42:58 +00:00
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
type GlyphCache = HashMap<char, renderer::Glyph>;
|
2016-05-21 18:08:50 +00:00
|
|
|
|
2016-06-03 03:27:07 +00:00
|
|
|
struct TermProps {
|
|
|
|
cell_width: f32,
|
|
|
|
sep_x: f32,
|
|
|
|
cell_height: f32,
|
|
|
|
sep_y: f32,
|
|
|
|
height: f32,
|
2016-05-21 18:08:50 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 16:15:41 +00:00
|
|
|
fn main() {
|
2016-05-31 03:44:37 +00:00
|
|
|
let window = glutin::WindowBuilder::new()
|
|
|
|
.with_title("alacritty".into())
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
2016-02-24 04:42:58 +00:00
|
|
|
let (width, height) = window.get_inner_size_pixels().unwrap();
|
|
|
|
unsafe {
|
2016-02-26 05:02:18 +00:00
|
|
|
window.make_current().unwrap();
|
|
|
|
}
|
2016-02-24 04:42:58 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);
|
|
|
|
gl::Viewport(0, 0, width as i32, height as i32);
|
|
|
|
}
|
|
|
|
|
2016-02-28 04:26:31 +00:00
|
|
|
let (dpi_x, dpi_y) = window.get_dpi().unwrap();
|
|
|
|
let dpr = window.hidpi_factor();
|
|
|
|
|
2016-05-21 04:36:28 +00:00
|
|
|
let font_size = 11.;
|
2016-04-10 23:19:39 +00:00
|
|
|
|
|
|
|
let sep_x = 2;
|
2016-04-11 15:05:19 +00:00
|
|
|
let sep_y = 5;
|
2016-04-10 23:19:39 +00:00
|
|
|
|
2016-04-11 15:05:19 +00:00
|
|
|
let desc = FontDesc::new("DejaVu Sans Mono", "Book");
|
2016-02-28 04:26:31 +00:00
|
|
|
let mut rasterizer = text::Rasterizer::new(dpi_x, dpi_y, dpr);
|
2016-02-26 04:59:21 +00:00
|
|
|
|
2016-04-10 23:19:39 +00:00
|
|
|
let (cell_width, cell_height) = rasterizer.box_size_for_font(&desc, font_size);
|
|
|
|
|
|
|
|
let num_cols = grid::num_cells_axis(cell_width, sep_x, width);
|
|
|
|
let num_rows = grid::num_cells_axis(cell_height, sep_y, height);
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
let tty = tty::new(num_rows as u8, num_cols as u8);
|
|
|
|
tty.resize(num_rows as usize, num_cols as usize, width as usize, height as usize);
|
|
|
|
let mut reader = tty.reader();
|
|
|
|
let mut writer = tty.writer();
|
2016-05-25 03:55:51 +00:00
|
|
|
|
2016-04-10 23:19:39 +00:00
|
|
|
println!("num_cols, num_rows = {}, {}", num_cols, num_rows);
|
|
|
|
|
|
|
|
let mut grid = Grid::new(num_rows as usize, num_cols as usize);
|
|
|
|
|
|
|
|
let mut glyph_cache = HashMap::new();
|
|
|
|
for c in INIT_LIST.chars() {
|
|
|
|
let glyph = Glyph::new(&rasterizer.get_glyph(&desc, font_size, c));
|
2016-05-31 03:44:37 +00:00
|
|
|
glyph_cache.insert(c, glyph);
|
2016-04-10 23:19:39 +00:00
|
|
|
}
|
2016-02-24 04:42:58 +00:00
|
|
|
|
|
|
|
unsafe {
|
2016-05-21 04:36:28 +00:00
|
|
|
gl::Enable(gl::BLEND);
|
|
|
|
gl::BlendFunc(gl::SRC1_COLOR, gl::ONE_MINUS_SRC1_COLOR);
|
2016-02-25 05:02:21 +00:00
|
|
|
gl::Enable(gl::MULTISAMPLE);
|
2016-02-24 04:42:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
let (chars_tx, chars_rx) = ::std::sync::mpsc::channel();
|
|
|
|
::std::thread::spawn(move || {
|
|
|
|
for c in reader.chars() {
|
|
|
|
let c = c.unwrap();
|
|
|
|
chars_tx.send(c);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-03 03:27:07 +00:00
|
|
|
let mut renderer = QuadRenderer::new(width, height);
|
2016-05-31 03:44:37 +00:00
|
|
|
let mut terminal = Term::new(tty, grid);
|
|
|
|
let mut meter = Meter::new();
|
|
|
|
|
|
|
|
let mut pty_parser = ansi::Parser::new();
|
2016-02-24 04:42:58 +00:00
|
|
|
|
2016-05-21 18:08:50 +00:00
|
|
|
'main_loop: loop {
|
2016-05-31 03:44:37 +00:00
|
|
|
// Handle keyboard/mouse input and other window events
|
|
|
|
{
|
|
|
|
let mut writer = BufWriter::new(&writer);
|
|
|
|
for event in window.poll_events() {
|
|
|
|
match event {
|
|
|
|
glutin::Event::Closed => break 'main_loop,
|
|
|
|
glutin::Event::ReceivedCharacter(c) => {
|
|
|
|
let encoded = c.encode_utf8();
|
|
|
|
writer.write(encoded.as_slice());
|
|
|
|
},
|
|
|
|
glutin::Event::KeyboardInput(state, _code, key) => {
|
|
|
|
match state {
|
|
|
|
glutin::ElementState::Pressed => {
|
|
|
|
match key {
|
|
|
|
Some(glutin::VirtualKeyCode::Up) => {
|
|
|
|
writer.write("\x1b[A".as_bytes());
|
|
|
|
},
|
|
|
|
Some(glutin::VirtualKeyCode::Down) => {
|
|
|
|
writer.write("\x1b[B".as_bytes());
|
|
|
|
},
|
|
|
|
Some(glutin::VirtualKeyCode::Left) => {
|
|
|
|
writer.write("\x1b[D".as_bytes());
|
|
|
|
},
|
|
|
|
Some(glutin::VirtualKeyCode::Right) => {
|
|
|
|
writer.write("\x1b[C".as_bytes());
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
2016-05-21 18:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
while let Ok(c) = chars_rx.try_recv() {
|
|
|
|
pty_parser.advance(&mut terminal, c);
|
|
|
|
}
|
|
|
|
|
2016-02-24 04:42:58 +00:00
|
|
|
unsafe {
|
2016-05-21 04:36:28 +00:00
|
|
|
gl::ClearColor(0.0, 0.0, 0.00, 1.0);
|
2016-02-24 04:42:58 +00:00
|
|
|
gl::Clear(gl::COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
2016-05-21 18:08:50 +00:00
|
|
|
{
|
|
|
|
let _sampler = meter.sampler();
|
|
|
|
|
2016-06-03 03:27:07 +00:00
|
|
|
let props = TermProps {
|
|
|
|
cell_width: cell_width as f32,
|
|
|
|
sep_x: sep_x as f32,
|
|
|
|
cell_height: cell_height as f32,
|
|
|
|
sep_y: sep_y as f32,
|
|
|
|
height: height as f32,
|
|
|
|
};
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
// Draw the grid
|
2016-06-03 03:27:07 +00:00
|
|
|
renderer.render_grid(terminal.grid(), &glyph_cache, &props);
|
2016-05-31 03:44:37 +00:00
|
|
|
|
|
|
|
// Also draw the cursor
|
2016-06-03 03:27:07 +00:00
|
|
|
renderer.render_cursor(terminal.cursor(), &glyph_cache, &props);
|
2016-04-10 23:19:39 +00:00
|
|
|
}
|
2016-02-24 04:42:58 +00:00
|
|
|
|
2016-06-03 03:27:07 +00:00
|
|
|
// Draw render timer
|
2016-05-21 18:08:50 +00:00
|
|
|
let timing = format!("{:.3} usec", meter.average());
|
2016-05-31 03:44:37 +00:00
|
|
|
let color = Rgb { r: 0xd5, g: 0x4e, b: 0x53 };
|
2016-06-03 03:27:07 +00:00
|
|
|
renderer.render_string(&timing[..], &glyph_cache, cell_width, &color);
|
2016-05-21 18:08:50 +00:00
|
|
|
|
2016-02-26 05:02:18 +00:00
|
|
|
window.swap_buffers().unwrap();
|
2016-02-24 04:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|