2016-06-29 23:56:12 -04: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-24 23:55:51 -04:00
|
|
|
//! Alacritty - The GPU Enhanced Terminal
|
2017-01-06 23:44:51 -05:00
|
|
|
#![cfg_attr(feature = "clippy", feature(plugin))]
|
2018-01-05 20:42:55 -05:00
|
|
|
#![cfg_attr(feature = "clippy", plugin(clippy))]
|
|
|
|
#![cfg_attr(feature = "clippy", deny(clippy))]
|
|
|
|
#![cfg_attr(feature = "clippy", deny(enum_glob_use))]
|
|
|
|
#![cfg_attr(feature = "clippy", deny(if_not_else))]
|
|
|
|
#![cfg_attr(feature = "clippy", deny(wrong_pub_self_convention))]
|
|
|
|
#![cfg_attr(feature = "nightly", feature(core_intrinsics))]
|
|
|
|
#![cfg_attr(all(test, feature = "bench"), feature(test))]
|
2016-10-14 19:38:15 -04:00
|
|
|
|
2016-11-19 19:16:20 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate alacritty;
|
2016-06-06 19:54:15 -04:00
|
|
|
|
2017-01-13 02:15:06 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
2016-12-11 01:44:13 -05:00
|
|
|
use std::error::Error;
|
2016-12-12 01:02:03 -05:00
|
|
|
use std::sync::Arc;
|
2018-01-07 23:46:11 -05:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
use std::env;
|
2016-06-27 22:46:24 -04:00
|
|
|
|
2016-12-11 01:44:13 -05:00
|
|
|
use alacritty::cli;
|
2016-11-19 19:16:20 -05:00
|
|
|
use alacritty::config::{self, Config};
|
2016-12-11 02:32:12 -05:00
|
|
|
use alacritty::display::Display;
|
2016-11-19 19:16:20 -05:00
|
|
|
use alacritty::event;
|
2017-05-20 00:35:43 -04:00
|
|
|
use alacritty::event_loop::{self, EventLoop, Msg};
|
2017-12-13 12:52:36 -05:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
use alacritty::locale;
|
2017-01-13 02:15:06 -05:00
|
|
|
use alacritty::logging;
|
2016-11-19 19:16:20 -05:00
|
|
|
use alacritty::sync::FairMutex;
|
2016-12-11 01:44:13 -05:00
|
|
|
use alacritty::term::{Term};
|
|
|
|
use alacritty::tty::{self, process_should_exit};
|
2017-01-01 22:09:27 -05:00
|
|
|
use alacritty::util::fmt::Red;
|
2016-06-29 23:14:37 -04:00
|
|
|
|
2016-06-09 23:39:40 -04:00
|
|
|
fn main() {
|
2017-05-28 20:42:36 -04:00
|
|
|
// Load command line options and config
|
2016-12-04 19:27:20 -05:00
|
|
|
let options = cli::Options::load();
|
2017-05-28 20:42:36 -04:00
|
|
|
let config = load_config(&options);
|
2016-11-19 19:16:20 -05:00
|
|
|
|
2018-01-07 23:46:11 -05:00
|
|
|
// Switch to home directory
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
env::set_current_dir(env::home_dir().unwrap()).unwrap();
|
|
|
|
// Set locale
|
2017-12-13 12:52:36 -05:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
locale::set_locale_environment();
|
|
|
|
|
2016-12-10 14:27:53 -05:00
|
|
|
// Run alacritty
|
2017-11-29 18:38:29 -05:00
|
|
|
if let Err(err) = run(config, &options) {
|
2016-12-31 22:49:51 -05:00
|
|
|
die!("Alacritty encountered an unrecoverable error:\n\n\t{}\n", Red(err));
|
2016-12-11 01:44:13 -05:00
|
|
|
}
|
|
|
|
|
2017-01-13 02:15:06 -05:00
|
|
|
info!("Goodbye.");
|
2016-12-10 14:27:53 -05:00
|
|
|
}
|
|
|
|
|
2017-05-28 20:42:36 -04:00
|
|
|
/// Load configuration
|
|
|
|
///
|
|
|
|
/// If a configuration file is given as a command line argument we don't
|
|
|
|
/// generate a default file. If an empty configuration file is given, i.e.
|
|
|
|
/// /dev/null, we load the compiled-in defaults.
|
|
|
|
fn load_config(options: &cli::Options) -> Config {
|
|
|
|
let config_path = options.config_path()
|
2018-01-05 20:42:55 -05:00
|
|
|
.or_else(Config::installed_config)
|
2017-05-28 20:42:36 -04:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
Config::write_defaults()
|
|
|
|
.unwrap_or_else(|err| die!("Write defaults config failure: {}", err))
|
|
|
|
});
|
|
|
|
|
|
|
|
Config::load_from(&*config_path).unwrap_or_else(|err| {
|
2018-01-05 19:50:12 -05:00
|
|
|
eprintln!("Error: {}; Loading default config", err);
|
|
|
|
Config::default()
|
2017-05-28 20:42:36 -04:00
|
|
|
})
|
|
|
|
}
|
2016-12-31 22:49:51 -05:00
|
|
|
|
2016-12-10 14:27:53 -05:00
|
|
|
/// Run Alacritty
|
|
|
|
///
|
2016-12-11 02:32:12 -05:00
|
|
|
/// Creates a window, the terminal state, pty, I/O event loop, input processor,
|
|
|
|
/// config change monitor, and runs the main display loop.
|
2017-11-29 18:38:29 -05:00
|
|
|
fn run(mut config: Config, options: &cli::Options) -> Result<(), Box<Error>> {
|
2017-01-23 11:45:40 -05:00
|
|
|
// Initialize the logger first as to capture output from other subsystems
|
2017-11-29 18:38:29 -05:00
|
|
|
logging::initialize(options)?;
|
2017-01-23 11:45:40 -05:00
|
|
|
|
2017-01-13 02:15:06 -05:00
|
|
|
info!("Welcome to Alacritty.");
|
2017-09-11 14:12:25 -04:00
|
|
|
config.path().map(|config_path| {
|
|
|
|
info!("Configuration loaded from {}", config_path.display());
|
|
|
|
});
|
2017-01-13 02:15:06 -05:00
|
|
|
|
2016-12-11 01:44:13 -05:00
|
|
|
// Create a display.
|
|
|
|
//
|
|
|
|
// The display manages a window and can draw the terminal
|
2017-11-29 18:38:29 -05:00
|
|
|
let mut display = Display::new(&config, options)?;
|
2016-12-11 01:44:13 -05:00
|
|
|
|
2017-01-13 02:15:06 -05:00
|
|
|
info!(
|
2016-12-29 20:39:30 -05:00
|
|
|
"PTY Dimensions: {:?} x {:?}",
|
|
|
|
display.size().lines(),
|
|
|
|
display.size().cols()
|
|
|
|
);
|
|
|
|
|
2016-12-11 01:44:13 -05:00
|
|
|
// 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.
|
2017-01-08 22:18:39 -05:00
|
|
|
let terminal = Term::new(&config, display.size().to_owned());
|
2016-12-29 21:38:22 -05:00
|
|
|
let terminal = Arc::new(FairMutex::new(terminal));
|
2016-12-11 01:44:13 -05:00
|
|
|
|
2017-05-27 23:08:28 -04:00
|
|
|
// Find the window ID for setting $WINDOWID
|
|
|
|
let window_id = display.get_window_id();
|
|
|
|
|
2016-12-11 01:44:13 -05:00
|
|
|
// 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.
|
2018-01-05 20:42:55 -05:00
|
|
|
let mut pty = tty::new(&config, options, &display.size(), window_id);
|
2016-11-11 20:40:12 -05:00
|
|
|
|
2016-12-11 01:44:13 -05: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 19:11:50 -04:00
|
|
|
let event_loop = EventLoop::new(
|
2017-09-27 20:29:44 -04:00
|
|
|
Arc::clone(&terminal),
|
2016-12-11 01:44:13 -05:00
|
|
|
display.notifier(),
|
|
|
|
pty.reader(),
|
2016-12-04 19:27:20 -05:00
|
|
|
options.ref_test,
|
2016-09-18 14:17:44 -04:00
|
|
|
);
|
2016-09-01 13:24:20 -04:00
|
|
|
|
2016-12-11 02:32:12 -05:00
|
|
|
// The event loop channel allows write requests from the event processor
|
|
|
|
// to be sent to the loop and ultimately written to the pty.
|
2016-09-24 19:11:50 -04:00
|
|
|
let loop_tx = event_loop.channel();
|
|
|
|
|
2016-09-01 13:24:20 -04:00
|
|
|
// Event processor
|
2016-12-12 01:02:03 -05:00
|
|
|
//
|
|
|
|
// Need the Rc<RefCell<_>> here since a ref is shared in the resize callback
|
2016-12-12 12:31:48 -05:00
|
|
|
let mut processor = event::Processor::new(
|
2017-05-20 00:35:43 -04:00
|
|
|
event_loop::Notifier(event_loop.channel()),
|
2016-12-11 02:32:12 -05:00
|
|
|
display.resize_channel(),
|
2017-11-29 18:38:29 -05:00
|
|
|
options,
|
2016-11-19 19:16:20 -05:00
|
|
|
&config,
|
2016-12-04 19:27:20 -05:00
|
|
|
options.ref_test,
|
2016-12-29 15:30:30 -05:00
|
|
|
display.size().to_owned(),
|
2016-12-12 12:31:48 -05:00
|
|
|
);
|
2016-09-01 13:24:20 -04:00
|
|
|
|
2016-12-11 02:32:12 -05:00
|
|
|
// Create a config monitor when config was loaded from path
|
|
|
|
//
|
|
|
|
// The monitor watches the config file for changes and reloads it. Pending
|
|
|
|
// config changes are processed in the main loop.
|
2017-08-29 12:32:08 -04:00
|
|
|
let config_monitor = match (options.live_config_reload, config.live_config_reload()) {
|
|
|
|
// Start monitor if CLI flag says yes
|
|
|
|
(Some(true), _) |
|
|
|
|
// Or if no CLI flag was passed and the config says yes
|
|
|
|
(None, true) => config.path()
|
|
|
|
.map(|path| config::Monitor::new(path, display.notifier())),
|
|
|
|
// Otherwise, don't start the monitor
|
|
|
|
_ => None,
|
|
|
|
};
|
2016-10-27 13:05:04 -04:00
|
|
|
|
2016-12-11 02:32:12 -05:00
|
|
|
// Kick off the I/O thread
|
|
|
|
let io_thread = event_loop.spawn(None);
|
2016-10-27 13:05:04 -04:00
|
|
|
|
2016-12-11 02:32:12 -05:00
|
|
|
// Main display loop
|
2016-09-01 13:24:20 -04:00
|
|
|
loop {
|
2016-12-11 02:32:12 -05:00
|
|
|
// Process input and window events
|
2016-12-29 21:38:22 -05:00
|
|
|
let mut terminal = processor.process_events(&terminal, display.window());
|
2016-09-01 13:24:20 -04:00
|
|
|
|
2016-12-04 18:48:12 -05:00
|
|
|
// Handle config reloads
|
2016-12-29 21:38:22 -05:00
|
|
|
config_monitor.as_ref()
|
2016-12-11 02:32:12 -05:00
|
|
|
.and_then(|monitor| monitor.pending_config())
|
2016-12-17 02:12:44 -05:00
|
|
|
.map(|new_config| {
|
2016-12-27 22:46:57 -05:00
|
|
|
config = new_config;
|
2016-12-11 02:32:12 -05:00
|
|
|
display.update_config(&config);
|
2016-12-12 12:31:48 -05:00
|
|
|
processor.update_config(&config);
|
2017-01-08 22:18:39 -05:00
|
|
|
terminal.update_config(&config);
|
2016-12-29 21:38:22 -05:00
|
|
|
terminal.dirty = true;
|
|
|
|
});
|
2016-10-27 13:05:04 -04:00
|
|
|
|
2016-09-01 13:24:20 -04:00
|
|
|
// Maybe draw the terminal
|
2017-10-06 14:32:53 -04:00
|
|
|
if terminal.needs_draw() {
|
2017-07-24 20:54:06 -04:00
|
|
|
// Try to update the position of the input method editor
|
|
|
|
display.update_ime_position(&terminal);
|
2016-12-12 12:31:48 -05:00
|
|
|
// Handle pending resize events
|
|
|
|
//
|
|
|
|
// The second argument is a list of types that want to be notified
|
|
|
|
// of display size changes.
|
2017-10-14 13:35:56 -04:00
|
|
|
display.handle_resize(&mut terminal, &config, &mut [&mut pty, &mut processor]);
|
2016-12-12 12:31:48 -05:00
|
|
|
|
|
|
|
// Draw the current state of the terminal
|
2017-06-16 00:43:28 -04:00
|
|
|
display.draw(terminal, &config, processor.selection.as_ref());
|
2016-09-01 13:24:20 -04:00
|
|
|
}
|
2016-08-29 22:23:04 -04:00
|
|
|
|
2016-12-04 18:48:12 -05:00
|
|
|
// Begin shutdown if the flag was raised.
|
2016-09-01 13:24:20 -04:00
|
|
|
if process_should_exit() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-01 00:16:17 -04:00
|
|
|
|
2017-05-20 00:35:43 -04:00
|
|
|
loop_tx.send(Msg::Shutdown).expect("Error sending shutdown to event loop");
|
|
|
|
|
2016-12-11 02:32:12 -05:00
|
|
|
// FIXME patch notify library to have a shutdown method
|
2016-10-27 13:05:04 -04:00
|
|
|
// config_reloader.join().ok();
|
|
|
|
|
2016-12-11 02:32:12 -05:00
|
|
|
// Wait for the I/O thread thread to finish
|
|
|
|
let _ = io_thread.join();
|
2016-12-11 01:44:13 -05:00
|
|
|
|
|
|
|
Ok(())
|
2016-08-29 22:23:04 -04:00
|
|
|
}
|