2016-06-30 03:56:12 +00:00
|
|
|
// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
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(inclusive_range_syntax)]
|
2016-06-30 03:14:37 +00:00
|
|
|
#![feature(drop_types_in_const)]
|
2016-10-14 23:38:15 +00:00
|
|
|
#![allow(stable_features)] // lying about question_mark because 1.14.0 isn't released!
|
|
|
|
|
|
|
|
#![feature(proc_macro)]
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
2016-05-25 03:55:51 +00:00
|
|
|
|
2016-11-20 00:16:20 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate alacritty;
|
2016-06-30 16:04:06 +00:00
|
|
|
extern crate cgmath;
|
2016-10-09 01:42:33 +00:00
|
|
|
extern crate copypasta;
|
2016-06-30 16:04:06 +00:00
|
|
|
extern crate errno;
|
2016-06-10 03:39:40 +00:00
|
|
|
extern crate font;
|
2016-02-24 04:42:58 +00:00
|
|
|
extern crate glutin;
|
2016-06-30 16:04:06 +00:00
|
|
|
extern crate libc;
|
2016-09-24 23:11:50 +00:00
|
|
|
extern crate mio;
|
2016-06-04 17:54:33 +00:00
|
|
|
extern crate notify;
|
2016-06-28 02:46:24 +00:00
|
|
|
extern crate parking_lot;
|
2016-06-30 16:04:06 +00:00
|
|
|
extern crate serde;
|
2016-11-20 00:16:20 +00:00
|
|
|
extern crate serde_json;
|
2016-06-30 16:04:06 +00:00
|
|
|
extern crate serde_yaml;
|
2016-09-18 18:17:44 +00:00
|
|
|
extern crate vte;
|
2016-02-21 23:20:03 +00:00
|
|
|
|
2016-06-06 23:54:15 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate bitflags;
|
|
|
|
|
2016-12-11 06:44:13 +00:00
|
|
|
use std::error::Error;
|
2016-06-28 02:46:24 +00:00
|
|
|
use std::sync::{mpsc, Arc};
|
2016-12-11 06:44:13 +00:00
|
|
|
use std::rc::Rc;
|
2016-06-28 02:46:24 +00:00
|
|
|
|
2016-12-11 06:44:13 +00:00
|
|
|
use alacritty::cli;
|
2016-11-20 00:16:20 +00:00
|
|
|
use alacritty::config::{self, Config};
|
2016-12-11 06:44:13 +00:00
|
|
|
use alacritty::display::{self, Display};
|
2016-11-20 00:16:20 +00:00
|
|
|
use alacritty::event;
|
2016-12-10 05:10:49 +00:00
|
|
|
use alacritty::event_loop::EventLoop;
|
2016-11-20 00:16:20 +00:00
|
|
|
use alacritty::input;
|
|
|
|
use alacritty::sync::FairMutex;
|
2016-12-11 06:44:13 +00:00
|
|
|
use alacritty::term::{Term};
|
|
|
|
use alacritty::tty::{self, process_should_exit};
|
2016-06-30 03:14:37 +00:00
|
|
|
|
2016-12-05 00:27:20 +00:00
|
|
|
|
2016-06-10 03:39:40 +00:00
|
|
|
fn main() {
|
2016-06-30 16:04:06 +00:00
|
|
|
// Load configuration
|
2016-12-05 19:18:02 +00:00
|
|
|
let config = match Config::load() {
|
2016-12-05 00:27:20 +00:00
|
|
|
// Error loading config
|
2016-06-30 16:04:06 +00:00
|
|
|
Err(err) => match err {
|
|
|
|
// Use default config when not found
|
2016-12-05 00:27:20 +00:00
|
|
|
config::Error::NotFound => {
|
|
|
|
err_println!("Config file not found; using defaults");
|
2016-12-05 19:18:02 +00:00
|
|
|
Config::default()
|
2016-12-05 00:27:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// If there's a problem with the config file, print an error
|
|
|
|
// and exit.
|
2016-06-30 16:04:06 +00:00
|
|
|
_ => die!("{}", err),
|
|
|
|
},
|
2016-12-05 00:27:20 +00:00
|
|
|
|
|
|
|
// Successfully loaded config from file
|
2016-12-05 19:18:02 +00:00
|
|
|
Ok(config) => config
|
2016-06-30 16:04:06 +00:00
|
|
|
};
|
|
|
|
|
2016-12-05 19:18:02 +00:00
|
|
|
// Load command line options
|
2016-12-05 00:27:20 +00:00
|
|
|
let options = cli::Options::load();
|
2016-11-20 00:16:20 +00:00
|
|
|
|
2016-12-10 19:27:53 +00:00
|
|
|
// Run alacritty
|
2016-12-11 06:44:13 +00:00
|
|
|
if let Err(err) = run(config, options) {
|
|
|
|
die!("{}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("Goodbye");
|
2016-12-10 19:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Run Alacritty
|
|
|
|
///
|
|
|
|
/// Currently, these operations take place in this order.
|
|
|
|
/// 1. create a window
|
|
|
|
/// 2. create font rasterizer
|
|
|
|
/// 3. create quad renderer
|
|
|
|
/// 4. create glyph cache
|
|
|
|
/// 5. resize window/renderer using info from glyph cache / rasterizer
|
|
|
|
/// 6. create a pty
|
|
|
|
/// 7. create the terminal
|
|
|
|
/// 8. set resize callback on the window
|
|
|
|
/// 9. create event loop
|
|
|
|
/// 10. create display
|
|
|
|
/// 11. create input processor
|
|
|
|
/// 12. create config reloader
|
|
|
|
/// 13. enter main loop
|
|
|
|
///
|
|
|
|
/// Observations:
|
|
|
|
/// * The window + quad renderer + glyph cache and display are closely
|
|
|
|
/// related Actually, probably include the input processor as well.
|
|
|
|
/// The resize callback can be lumped in there and that resize step.
|
|
|
|
/// Rasterizer as well. Maybe we can lump *all* of this into the
|
|
|
|
/// `Display`.
|
|
|
|
/// * the pty and event loop closely related
|
|
|
|
/// * The term bridges the display and pty
|
|
|
|
/// * Main loop currently manages input, config reload events, drawing, and
|
|
|
|
/// exiting
|
|
|
|
///
|
|
|
|
/// It would be *really* great if this could read more like
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// let display = Display::new(args..);
|
|
|
|
/// let pty = Pty::new(display.size());
|
|
|
|
/// let term = Arc::new(Term::new(display.size());
|
|
|
|
/// let io_loop = Loop::new(Pty::new(display.size()), term.clone());
|
|
|
|
/// let config_reloader = config::Monitor::new(&config);
|
|
|
|
///
|
|
|
|
/// loop {
|
|
|
|
/// force_draw = false;
|
|
|
|
/// // Wait for something to happen
|
|
|
|
/// processor.process_events(&display);
|
|
|
|
///
|
|
|
|
/// // Handle config reloads
|
|
|
|
/// if let Ok(config) = config_rx.try_recv() {
|
|
|
|
/// force_draw = true;
|
|
|
|
/// display.update_config(&config);
|
|
|
|
/// processor.update_config(&config);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Maybe draw the terminal
|
|
|
|
/// let terminal = terminal.lock();
|
|
|
|
/// signal_flag.set(false);
|
|
|
|
/// if force_draw || terminal.dirty {
|
|
|
|
/// display.draw(terminal, &config);
|
|
|
|
/// drop(terminal);
|
|
|
|
/// display.swap_buffers();
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Begin shutdown if the flag was raised.
|
|
|
|
/// if process_should_exit() {
|
|
|
|
/// break;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// instead of the 200 line monster it currently is.
|
|
|
|
///
|
2016-12-11 06:44:13 +00:00
|
|
|
fn run(config: Config, options: cli::Options) -> Result<(), Box<Error>> {
|
|
|
|
// Create a display.
|
|
|
|
//
|
|
|
|
// The display manages a window and can draw the terminal
|
|
|
|
let mut display = Display::new(&config, &options)?;
|
|
|
|
|
|
|
|
// Create the terminal
|
|
|
|
//
|
|
|
|
// This object contains all of the state about what's being displayed. It's
|
|
|
|
// wrapped in a clonable mutex since both the I/O loop and display need to
|
|
|
|
// access it.
|
|
|
|
let terminal = Arc::new(FairMutex::new(Term::new(display.size().to_owned())));
|
|
|
|
|
|
|
|
// Create the pty
|
|
|
|
//
|
|
|
|
// The pty forks a process to run the shell on the slave side of the
|
|
|
|
// pseudoterminal. A file descriptor for the master side is retained for
|
|
|
|
// reading/writing to the shell.
|
|
|
|
let pty = Rc::new(tty::new(display.size()));
|
|
|
|
|
|
|
|
// When the display is resized, inform the kernel of changes to pty
|
|
|
|
// dimensions.
|
|
|
|
//
|
|
|
|
// TODO: The Rc on pty is needed due to a borrowck error here. The borrow
|
|
|
|
// checker says that `pty` is still borrowed when it is dropped at the end
|
|
|
|
// of the `run` function.
|
|
|
|
let pty_ref = pty.clone();
|
|
|
|
display.set_resize_callback(move |size| {
|
|
|
|
pty_ref.resize(size);
|
2016-12-10 05:10:49 +00:00
|
|
|
});
|
2016-11-12 01:40:12 +00:00
|
|
|
|
2016-12-11 06:44:13 +00:00
|
|
|
// Create the pseudoterminal I/O loop
|
|
|
|
//
|
|
|
|
// pty I/O is ran on another thread as to not occupy cycles used by the
|
|
|
|
// renderer and input processing. Note that access to the terminal state is
|
|
|
|
// synchronized since the I/O loop updates the state, and the display
|
|
|
|
// consumes it periodically.
|
2016-09-24 23:11:50 +00:00
|
|
|
let event_loop = EventLoop::new(
|
2016-09-18 18:17:44 +00:00
|
|
|
terminal.clone(),
|
2016-12-11 06:44:13 +00:00
|
|
|
display.notifier(),
|
|
|
|
pty.reader(),
|
2016-12-05 00:27:20 +00:00
|
|
|
options.ref_test,
|
2016-09-18 18:17:44 +00:00
|
|
|
);
|
2016-09-01 17:24:20 +00:00
|
|
|
|
2016-09-24 23:11:50 +00:00
|
|
|
let loop_tx = event_loop.channel();
|
2016-09-26 15:28:38 +00:00
|
|
|
let event_loop_handle = event_loop.spawn(None);
|
2016-09-24 23:11:50 +00:00
|
|
|
|
2016-09-01 17:24:20 +00:00
|
|
|
// Event processor
|
2016-12-11 06:44:13 +00:00
|
|
|
let resize_tx = display.resize_channel();
|
2016-09-24 23:11:50 +00:00
|
|
|
let mut processor = event::Processor::new(
|
|
|
|
input::LoopNotifier(loop_tx),
|
|
|
|
terminal.clone(),
|
2016-12-11 06:44:13 +00:00
|
|
|
resize_tx,
|
2016-11-20 00:16:20 +00:00
|
|
|
&config,
|
2016-12-05 00:27:20 +00:00
|
|
|
options.ref_test,
|
2016-09-24 23:11:50 +00:00
|
|
|
);
|
2016-09-01 17:24:20 +00:00
|
|
|
|
2016-10-27 17:05:04 +00:00
|
|
|
let (config_tx, config_rx) = mpsc::channel();
|
|
|
|
|
|
|
|
// create a config watcher when config is loaded from disk
|
2016-12-05 19:18:02 +00:00
|
|
|
let _config_reloader = config.path().map(|path| {
|
|
|
|
config::Watcher::new(path, ConfigHandler {
|
2016-10-27 17:05:04 +00:00
|
|
|
tx: config_tx,
|
2016-12-11 06:44:13 +00:00
|
|
|
loop_kicker: display.notifier(),
|
2016-10-27 17:05:04 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2016-09-01 17:24:20 +00:00
|
|
|
// Main loop
|
2016-12-11 06:44:13 +00:00
|
|
|
let mut config_updated = false;
|
2016-09-01 17:24:20 +00:00
|
|
|
loop {
|
|
|
|
// Wait for something to happen
|
2016-12-11 06:44:13 +00:00
|
|
|
processor.process_events(display.window());
|
2016-09-01 17:24:20 +00:00
|
|
|
|
2016-12-04 23:48:12 +00:00
|
|
|
// Handle config reloads
|
2016-10-27 17:05:04 +00:00
|
|
|
if let Ok(config) = config_rx.try_recv() {
|
2016-12-11 06:44:13 +00:00
|
|
|
config_updated = true;
|
2016-10-27 17:05:04 +00:00
|
|
|
display.update_config(&config);
|
2016-11-15 17:38:50 +00:00
|
|
|
processor.update_config(&config);
|
2016-10-27 17:05:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 17:24:20 +00:00
|
|
|
// Maybe draw the terminal
|
2016-09-23 17:12:11 +00:00
|
|
|
let terminal = terminal.lock();
|
2016-12-11 06:44:13 +00:00
|
|
|
if terminal.dirty || config_updated {
|
2016-12-04 21:13:44 +00:00
|
|
|
display.draw(terminal, &config);
|
2016-09-01 17:24:20 +00:00
|
|
|
}
|
2016-08-30 02:23:04 +00:00
|
|
|
|
2016-12-04 23:48:12 +00:00
|
|
|
// Begin shutdown if the flag was raised.
|
2016-09-01 17:24:20 +00:00
|
|
|
if process_should_exit() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-01 04:16:17 +00:00
|
|
|
|
2016-10-27 17:05:04 +00:00
|
|
|
// FIXME need file watcher to work with custom delegates before
|
|
|
|
// joining config reloader is possible
|
2016-12-11 06:44:13 +00:00
|
|
|
//
|
|
|
|
// HELP I don't know what I meant in the above fixme
|
2016-10-27 17:05:04 +00:00
|
|
|
// config_reloader.join().ok();
|
|
|
|
|
2016-09-01 17:24:20 +00:00
|
|
|
// shutdown
|
2016-09-24 23:11:50 +00:00
|
|
|
event_loop_handle.join().ok();
|
2016-12-11 06:44:13 +00:00
|
|
|
|
|
|
|
Ok(())
|
2016-08-30 02:23:04 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 17:05:04 +00:00
|
|
|
struct ConfigHandler {
|
|
|
|
tx: mpsc::Sender<config::Config>,
|
2016-12-11 06:44:13 +00:00
|
|
|
loop_kicker: display::Notifier,
|
2016-10-27 17:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl config::OnConfigReload for ConfigHandler {
|
|
|
|
fn on_config_reload(&mut self, config: Config) {
|
|
|
|
if let Err(..) = self.tx.send(config) {
|
|
|
|
err_println!("Failed to notify of new config");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-11 06:44:13 +00:00
|
|
|
self.loop_kicker.notify();
|
2016-10-27 17:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|