2020-05-05 22:50:23 +00:00
|
|
|
//! Process window events.
|
2019-10-05 00:29:26 +00:00
|
|
|
use std::borrow::Cow;
|
2019-11-03 19:02:26 +00:00
|
|
|
use std::cmp::max;
|
2019-10-05 00:29:26 +00:00
|
|
|
use std::env;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use std::fs;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
2020-01-10 01:51:37 +00:00
|
|
|
use std::mem;
|
2020-04-09 01:02:10 +00:00
|
|
|
use std::path::PathBuf;
|
2020-05-03 23:29:11 +00:00
|
|
|
#[cfg(not(any(target_os = "macos", windows)))]
|
|
|
|
use std::sync::atomic::Ordering;
|
2019-10-05 00:29:26 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
use glutin::dpi::PhysicalSize;
|
2020-04-10 18:23:50 +00:00
|
|
|
use glutin::event::{ElementState, Event as GlutinEvent, ModifiersState, WindowEvent};
|
2020-02-07 13:44:11 +00:00
|
|
|
use glutin::event_loop::{ControlFlow, EventLoop, EventLoopProxy, EventLoopWindowTarget};
|
2019-10-05 00:29:26 +00:00
|
|
|
use glutin::platform::desktop::EventLoopExtDesktop;
|
2020-04-09 01:02:10 +00:00
|
|
|
#[cfg(not(any(target_os = "macos", windows)))]
|
|
|
|
use glutin::platform::unix::EventLoopWindowTargetExtUnix;
|
2019-10-05 00:29:26 +00:00
|
|
|
use log::{debug, info, warn};
|
|
|
|
use serde_json as json;
|
|
|
|
|
2020-05-17 21:14:13 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
use font::set_font_smoothing;
|
2020-05-27 14:46:52 +00:00
|
|
|
use font::{self, Size};
|
2019-10-05 00:29:26 +00:00
|
|
|
|
|
|
|
use alacritty_terminal::clipboard::ClipboardType;
|
2019-11-03 19:02:26 +00:00
|
|
|
use alacritty_terminal::config::Font;
|
2019-10-05 00:29:26 +00:00
|
|
|
use alacritty_terminal::config::LOG_TARGET_CONFIG;
|
|
|
|
use alacritty_terminal::event::OnResize;
|
|
|
|
use alacritty_terminal::event::{Event, EventListener, Notify};
|
|
|
|
use alacritty_terminal::grid::Scroll;
|
|
|
|
use alacritty_terminal::index::{Column, Line, Point, Side};
|
|
|
|
use alacritty_terminal::message_bar::{Message, MessageBuffer};
|
2020-03-18 02:35:08 +00:00
|
|
|
use alacritty_terminal::selection::{Selection, SelectionType};
|
2019-10-05 00:29:26 +00:00
|
|
|
use alacritty_terminal::sync::FairMutex;
|
|
|
|
use alacritty_terminal::term::cell::Cell;
|
2020-03-18 02:35:08 +00:00
|
|
|
use alacritty_terminal::term::{SizeInfo, Term, TermMode};
|
2019-11-16 21:11:56 +00:00
|
|
|
#[cfg(not(windows))]
|
2019-10-05 00:29:26 +00:00
|
|
|
use alacritty_terminal::tty;
|
|
|
|
use alacritty_terminal::util::{limit, start_daemon};
|
|
|
|
|
2020-01-16 23:42:26 +00:00
|
|
|
use crate::cli::Options;
|
2019-10-05 00:29:26 +00:00
|
|
|
use crate::config;
|
|
|
|
use crate::config::Config;
|
|
|
|
use crate::display::Display;
|
2019-11-11 21:05:24 +00:00
|
|
|
use crate::input::{self, ActionContext as _, FONT_SIZE_STEP};
|
2020-03-18 02:35:08 +00:00
|
|
|
use crate::url::{Url, Urls};
|
2019-10-05 00:29:26 +00:00
|
|
|
use crate::window::Window;
|
|
|
|
|
2019-11-03 19:02:26 +00:00
|
|
|
#[derive(Default, Clone, Debug, PartialEq)]
|
|
|
|
pub struct DisplayUpdate {
|
2020-01-10 01:51:37 +00:00
|
|
|
pub dimensions: Option<PhysicalSize<u32>>,
|
2020-04-15 03:50:34 +00:00
|
|
|
pub message_buffer: bool,
|
2019-11-03 19:02:26 +00:00
|
|
|
pub font: Option<Font>,
|
2020-04-15 03:50:34 +00:00
|
|
|
pub cursor: bool,
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:02:26 +00:00
|
|
|
impl DisplayUpdate {
|
2019-10-05 00:29:26 +00:00
|
|
|
fn is_empty(&self) -> bool {
|
2020-04-15 03:50:34 +00:00
|
|
|
self.dimensions.is_none() && self.font.is_none() && !self.message_buffer && !self.cursor
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ActionContext<'a, N, T> {
|
|
|
|
pub notifier: &'a mut N,
|
|
|
|
pub terminal: &'a mut Term<T>,
|
|
|
|
pub size_info: &'a mut SizeInfo,
|
|
|
|
pub mouse: &'a mut Mouse,
|
|
|
|
pub received_count: &'a mut usize,
|
|
|
|
pub suppress_chars: &'a mut bool,
|
2019-11-11 21:05:24 +00:00
|
|
|
pub modifiers: &'a mut ModifiersState,
|
2019-10-05 00:29:26 +00:00
|
|
|
pub window: &'a mut Window,
|
|
|
|
pub message_buffer: &'a mut MessageBuffer,
|
2019-11-03 19:02:26 +00:00
|
|
|
pub display_update_pending: &'a mut DisplayUpdate,
|
|
|
|
pub config: &'a mut Config,
|
2020-02-07 13:44:11 +00:00
|
|
|
pub event_loop: &'a EventLoopWindowTarget<Event>,
|
2020-03-18 02:35:08 +00:00
|
|
|
pub urls: &'a Urls,
|
2019-11-03 19:02:26 +00:00
|
|
|
font_size: &'a mut Size,
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, N: Notify + 'a, T: EventListener> input::ActionContext<T> for ActionContext<'a, N, T> {
|
|
|
|
fn write_to_pty<B: Into<Cow<'static, [u8]>>>(&mut self, val: B) {
|
|
|
|
self.notifier.notify(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size_info(&self) -> SizeInfo {
|
|
|
|
*self.size_info
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scroll(&mut self, scroll: Scroll) {
|
|
|
|
self.terminal.scroll_display(scroll);
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Update selection.
|
2020-03-18 02:35:08 +00:00
|
|
|
if self.terminal.mode().contains(TermMode::VI)
|
2020-05-30 20:45:44 +00:00
|
|
|
&& self.terminal.selection.as_ref().map(|s| s.is_empty()) != Some(true)
|
2020-03-18 02:35:08 +00:00
|
|
|
{
|
|
|
|
self.update_selection(self.terminal.vi_mode_cursor.point, Side::Right);
|
|
|
|
} else if ElementState::Pressed == self.mouse().left_button_state {
|
2019-10-05 00:29:26 +00:00
|
|
|
let (x, y) = (self.mouse().x, self.mouse().y);
|
|
|
|
let size_info = self.size_info();
|
|
|
|
let point = size_info.pixels_to_coords(x, y);
|
|
|
|
let cell_side = self.mouse().cell_side;
|
|
|
|
self.update_selection(Point { line: point.line, col: point.col }, cell_side);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn copy_selection(&mut self, ty: ClipboardType) {
|
|
|
|
if let Some(selected) = self.terminal.selection_to_string() {
|
|
|
|
if !selected.is_empty() {
|
|
|
|
self.terminal.clipboard().store(ty, selected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn selection_is_empty(&self) -> bool {
|
2020-05-30 20:45:44 +00:00
|
|
|
self.terminal.selection.as_ref().map(Selection::is_empty).unwrap_or(true)
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_selection(&mut self) {
|
2020-05-30 20:45:44 +00:00
|
|
|
self.terminal.selection = None;
|
2019-10-05 00:29:26 +00:00
|
|
|
self.terminal.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_selection(&mut self, point: Point, side: Side) {
|
|
|
|
let point = self.terminal.visible_to_buffer(point);
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Update selection if one exists.
|
2020-03-18 02:35:08 +00:00
|
|
|
let vi_mode = self.terminal.mode().contains(TermMode::VI);
|
2020-05-30 20:45:44 +00:00
|
|
|
if let Some(selection) = &mut self.terminal.selection {
|
2019-10-05 00:29:26 +00:00
|
|
|
selection.update(point, side);
|
|
|
|
|
2020-03-18 02:35:08 +00:00
|
|
|
if vi_mode {
|
|
|
|
selection.include_all();
|
|
|
|
}
|
2019-10-05 00:29:26 +00:00
|
|
|
|
2020-03-18 02:35:08 +00:00
|
|
|
self.terminal.dirty = true;
|
|
|
|
}
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 02:35:08 +00:00
|
|
|
fn start_selection(&mut self, ty: SelectionType, point: Point, side: Side) {
|
2019-10-05 00:29:26 +00:00
|
|
|
let point = self.terminal.visible_to_buffer(point);
|
2020-05-30 20:45:44 +00:00
|
|
|
self.terminal.selection = Some(Selection::new(ty, point, side));
|
2019-10-05 00:29:26 +00:00
|
|
|
self.terminal.dirty = true;
|
|
|
|
}
|
|
|
|
|
2020-03-18 02:35:08 +00:00
|
|
|
fn toggle_selection(&mut self, ty: SelectionType, point: Point, side: Side) {
|
2020-05-30 20:45:44 +00:00
|
|
|
match &mut self.terminal.selection {
|
2020-03-18 02:35:08 +00:00
|
|
|
Some(selection) if selection.ty == ty && !selection.is_empty() => {
|
|
|
|
self.clear_selection();
|
|
|
|
},
|
|
|
|
Some(selection) if !selection.is_empty() => {
|
|
|
|
selection.ty = ty;
|
|
|
|
self.terminal.dirty = true;
|
|
|
|
},
|
|
|
|
_ => self.start_selection(ty, point, side),
|
|
|
|
}
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn mouse_coords(&self) -> Option<Point> {
|
|
|
|
let x = self.mouse.x as usize;
|
|
|
|
let y = self.mouse.y as usize;
|
|
|
|
|
2019-11-03 20:59:28 +00:00
|
|
|
if self.size_info.contains_point(x, y) {
|
2019-10-05 00:29:26 +00:00
|
|
|
Some(self.size_info.pixels_to_coords(x, y))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 02:35:08 +00:00
|
|
|
#[inline]
|
|
|
|
fn mouse_mode(&self) -> bool {
|
|
|
|
self.terminal.mode().intersects(TermMode::MOUSE_MODE)
|
|
|
|
&& !self.terminal.mode().contains(TermMode::VI)
|
|
|
|
}
|
|
|
|
|
2019-10-05 00:29:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn mouse_mut(&mut self) -> &mut Mouse {
|
|
|
|
self.mouse
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn mouse(&self) -> &Mouse {
|
|
|
|
self.mouse
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn received_count(&mut self) -> &mut usize {
|
|
|
|
&mut self.received_count
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn suppress_chars(&mut self) -> &mut bool {
|
|
|
|
&mut self.suppress_chars
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-11-11 21:05:24 +00:00
|
|
|
fn modifiers(&mut self) -> &mut ModifiersState {
|
2019-10-05 00:29:26 +00:00
|
|
|
&mut self.modifiers
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn window(&self) -> &Window {
|
|
|
|
self.window
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn window_mut(&mut self) -> &mut Window {
|
|
|
|
self.window
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn terminal(&self) -> &Term<T> {
|
|
|
|
self.terminal
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn terminal_mut(&mut self) -> &mut Term<T> {
|
|
|
|
self.terminal
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spawn_new_instance(&mut self) {
|
|
|
|
let alacritty = env::args().next().unwrap();
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
let args = {
|
|
|
|
#[cfg(not(target_os = "freebsd"))]
|
|
|
|
let proc_prefix = "";
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
let proc_prefix = "/compat/linux";
|
|
|
|
let link_path = format!("{}/proc/{}/cwd", proc_prefix, tty::child_pid());
|
|
|
|
if let Ok(path) = fs::read_link(link_path) {
|
|
|
|
vec!["--working-directory".into(), path]
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
let args: Vec<String> = Vec::new();
|
|
|
|
|
|
|
|
match start_daemon(&alacritty, &args) {
|
|
|
|
Ok(_) => debug!("Started new Alacritty process: {} {:?}", alacritty, args),
|
|
|
|
Err(_) => warn!("Unable to start new Alacritty process: {} {:?}", alacritty, args),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn change_font_size(&mut self, delta: f32) {
|
2019-11-03 19:02:26 +00:00
|
|
|
*self.font_size = max(*self.font_size + delta, Size::new(FONT_SIZE_STEP));
|
|
|
|
let font = self.config.font.clone().with_size(*self.font_size);
|
|
|
|
self.display_update_pending.font = Some(font);
|
2019-10-05 00:29:26 +00:00
|
|
|
self.terminal.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reset_font_size(&mut self) {
|
2019-11-03 19:02:26 +00:00
|
|
|
*self.font_size = self.config.font.size;
|
|
|
|
self.display_update_pending.font = Some(self.config.font.clone());
|
2019-10-05 00:29:26 +00:00
|
|
|
self.terminal.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pop_message(&mut self) {
|
2020-04-15 03:50:34 +00:00
|
|
|
self.display_update_pending.message_buffer = true;
|
2019-10-05 00:29:26 +00:00
|
|
|
self.message_buffer.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn message(&self) -> Option<&Message> {
|
|
|
|
self.message_buffer.message()
|
|
|
|
}
|
2019-11-03 19:02:26 +00:00
|
|
|
|
|
|
|
fn config(&self) -> &Config {
|
|
|
|
self.config
|
|
|
|
}
|
2020-02-07 13:44:11 +00:00
|
|
|
|
|
|
|
fn event_loop(&self) -> &EventLoopWindowTarget<Event> {
|
|
|
|
self.event_loop
|
|
|
|
}
|
2020-03-18 02:35:08 +00:00
|
|
|
|
|
|
|
fn urls(&self) -> &Urls {
|
|
|
|
self.urls
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Spawn URL launcher when clicking on URLs.
|
|
|
|
fn launch_url(&self, url: Url) {
|
|
|
|
if self.mouse.block_url_launcher {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref launcher) = self.config.ui_config.mouse.url.launcher {
|
|
|
|
let mut args = launcher.args().to_vec();
|
|
|
|
let start = self.terminal.visible_to_buffer(url.start());
|
|
|
|
let end = self.terminal.visible_to_buffer(url.end());
|
|
|
|
args.push(self.terminal.bounds_to_string(start, end));
|
|
|
|
|
|
|
|
match start_daemon(launcher.program(), &args) {
|
|
|
|
Ok(_) => debug!("Launched {} with args {:?}", launcher.program(), args),
|
|
|
|
Err(_) => warn!("Unable to launch {} with args {:?}", launcher.program(), args),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 18:23:50 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2019-10-05 00:29:26 +00:00
|
|
|
pub enum ClickState {
|
|
|
|
None,
|
|
|
|
Click,
|
|
|
|
DoubleClick,
|
|
|
|
TripleClick,
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// State of the mouse.
|
2020-03-18 02:35:08 +00:00
|
|
|
#[derive(Debug)]
|
2019-10-05 00:29:26 +00:00
|
|
|
pub struct Mouse {
|
|
|
|
pub x: usize,
|
|
|
|
pub y: usize,
|
|
|
|
pub left_button_state: ElementState,
|
|
|
|
pub middle_button_state: ElementState,
|
|
|
|
pub right_button_state: ElementState,
|
|
|
|
pub last_click_timestamp: Instant,
|
|
|
|
pub click_state: ClickState,
|
2020-03-02 05:32:18 +00:00
|
|
|
pub scroll_px: f64,
|
2019-10-05 00:29:26 +00:00
|
|
|
pub line: Line,
|
|
|
|
pub column: Column,
|
|
|
|
pub cell_side: Side,
|
|
|
|
pub lines_scrolled: f32,
|
|
|
|
pub block_url_launcher: bool,
|
2019-11-03 20:59:28 +00:00
|
|
|
pub inside_grid: bool,
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Mouse {
|
|
|
|
fn default() -> Mouse {
|
|
|
|
Mouse {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
last_click_timestamp: Instant::now(),
|
|
|
|
left_button_state: ElementState::Released,
|
|
|
|
middle_button_state: ElementState::Released,
|
|
|
|
right_button_state: ElementState::Released,
|
|
|
|
click_state: ClickState::None,
|
2020-03-02 05:32:18 +00:00
|
|
|
scroll_px: 0.,
|
2019-10-05 00:29:26 +00:00
|
|
|
line: Line(0),
|
|
|
|
column: Column(0),
|
|
|
|
cell_side: Side::Left,
|
2020-03-02 05:32:18 +00:00
|
|
|
lines_scrolled: 0.,
|
2019-10-05 00:29:26 +00:00
|
|
|
block_url_launcher: false,
|
2019-11-03 20:59:28 +00:00
|
|
|
inside_grid: false,
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// The event processor.
|
2019-10-05 00:29:26 +00:00
|
|
|
///
|
|
|
|
/// Stores some state from received events and dispatches actions when they are
|
|
|
|
/// triggered.
|
|
|
|
pub struct Processor<N> {
|
|
|
|
notifier: N,
|
|
|
|
mouse: Mouse,
|
|
|
|
received_count: usize,
|
|
|
|
suppress_chars: bool,
|
2019-11-11 21:05:24 +00:00
|
|
|
modifiers: ModifiersState,
|
2019-10-05 00:29:26 +00:00
|
|
|
config: Config,
|
|
|
|
message_buffer: MessageBuffer,
|
|
|
|
display: Display,
|
2019-11-03 19:02:26 +00:00
|
|
|
font_size: Size,
|
2020-05-03 23:29:11 +00:00
|
|
|
event_queue: Vec<GlutinEvent<'static, alacritty_terminal::event::Event>>,
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 21:32:24 +00:00
|
|
|
impl<N: Notify + OnResize> Processor<N> {
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Create a new event processor.
|
2019-10-05 00:29:26 +00:00
|
|
|
///
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Takes a writer which is expected to be hooked up to the write end of a PTY.
|
2019-10-05 00:29:26 +00:00
|
|
|
pub fn new(
|
|
|
|
notifier: N,
|
|
|
|
message_buffer: MessageBuffer,
|
|
|
|
config: Config,
|
|
|
|
display: Display,
|
|
|
|
) -> Processor<N> {
|
|
|
|
Processor {
|
|
|
|
notifier,
|
|
|
|
mouse: Default::default(),
|
|
|
|
received_count: 0,
|
|
|
|
suppress_chars: false,
|
|
|
|
modifiers: Default::default(),
|
2019-11-03 19:02:26 +00:00
|
|
|
font_size: config.font.size,
|
2019-10-05 00:29:26 +00:00
|
|
|
config,
|
|
|
|
message_buffer,
|
|
|
|
display,
|
2020-05-03 23:29:11 +00:00
|
|
|
event_queue: Vec::new(),
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:29:11 +00:00
|
|
|
/// Return `true` if `event_queue` is empty, `false` otherwise.
|
|
|
|
#[inline]
|
|
|
|
#[cfg(not(any(target_os = "macos", windows)))]
|
|
|
|
fn event_queue_empty(&mut self) -> bool {
|
|
|
|
let wayland_event_queue = match self.display.wayland_event_queue.as_mut() {
|
|
|
|
Some(wayland_event_queue) => wayland_event_queue,
|
|
|
|
// Since frame callbacks do not exist on X11, just check for event queue.
|
|
|
|
None => return self.event_queue.is_empty(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check for pending frame callbacks on Wayland.
|
|
|
|
let events_dispatched = wayland_event_queue
|
|
|
|
.dispatch_pending(&mut (), |_, _, _| {})
|
|
|
|
.expect("failed to dispatch event queue");
|
|
|
|
|
|
|
|
self.event_queue.is_empty() && events_dispatched == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return `true` if `event_queue` is empty, `false` otherwise.
|
|
|
|
#[inline]
|
|
|
|
#[cfg(any(target_os = "macos", windows))]
|
|
|
|
fn event_queue_empty(&mut self) -> bool {
|
|
|
|
self.event_queue.is_empty()
|
|
|
|
}
|
|
|
|
|
2019-10-05 00:29:26 +00:00
|
|
|
/// Run the event loop.
|
|
|
|
pub fn run<T>(&mut self, terminal: Arc<FairMutex<Term<T>>>, mut event_loop: EventLoop<Event>)
|
|
|
|
where
|
|
|
|
T: EventListener,
|
|
|
|
{
|
2020-02-07 13:44:11 +00:00
|
|
|
event_loop.run_return(|event, event_loop, control_flow| {
|
2019-10-05 00:29:26 +00:00
|
|
|
if self.config.debug.print_events {
|
|
|
|
info!("glutin event: {:?}", event);
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:29:11 +00:00
|
|
|
// Ignore all events we do not care about.
|
2020-01-10 01:51:37 +00:00
|
|
|
if Self::skip_event(&event) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match event {
|
2020-05-03 23:29:11 +00:00
|
|
|
// Check for shutdown.
|
2019-11-16 21:11:56 +00:00
|
|
|
GlutinEvent::UserEvent(Event::Exit) => {
|
2019-10-05 00:29:26 +00:00
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
return;
|
|
|
|
},
|
2020-05-03 23:29:11 +00:00
|
|
|
// Process events.
|
2020-01-05 01:00:50 +00:00
|
|
|
GlutinEvent::RedrawEventsCleared => {
|
2019-10-05 00:29:26 +00:00
|
|
|
*control_flow = ControlFlow::Wait;
|
|
|
|
|
2020-05-03 23:29:11 +00:00
|
|
|
if self.event_queue_empty() {
|
2019-10-05 00:29:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2020-05-03 23:29:11 +00:00
|
|
|
// Remap DPR change event to remove lifetime.
|
2020-01-10 01:51:37 +00:00
|
|
|
GlutinEvent::WindowEvent {
|
|
|
|
event: WindowEvent::ScaleFactorChanged { scale_factor, new_inner_size },
|
|
|
|
..
|
|
|
|
} => {
|
2019-10-05 00:29:26 +00:00
|
|
|
*control_flow = ControlFlow::Poll;
|
2020-01-10 01:51:37 +00:00
|
|
|
let size = (new_inner_size.width, new_inner_size.height);
|
|
|
|
let event = GlutinEvent::UserEvent(Event::DPRChanged(scale_factor, size));
|
2020-05-03 23:29:11 +00:00
|
|
|
self.event_queue.push(event);
|
2020-01-10 01:51:37 +00:00
|
|
|
return;
|
|
|
|
},
|
|
|
|
// Transmute to extend lifetime, which exists only for `ScaleFactorChanged` event.
|
|
|
|
// Since we remap that event to remove the lifetime, this is safe.
|
|
|
|
event => unsafe {
|
|
|
|
*control_flow = ControlFlow::Poll;
|
2020-05-03 23:29:11 +00:00
|
|
|
self.event_queue.push(mem::transmute(event));
|
2019-10-05 00:29:26 +00:00
|
|
|
return;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut terminal = terminal.lock();
|
|
|
|
|
2019-11-03 19:02:26 +00:00
|
|
|
let mut display_update_pending = DisplayUpdate::default();
|
2019-10-05 00:29:26 +00:00
|
|
|
|
|
|
|
let context = ActionContext {
|
|
|
|
terminal: &mut terminal,
|
|
|
|
notifier: &mut self.notifier,
|
|
|
|
mouse: &mut self.mouse,
|
|
|
|
size_info: &mut self.display.size_info,
|
|
|
|
received_count: &mut self.received_count,
|
|
|
|
suppress_chars: &mut self.suppress_chars,
|
|
|
|
modifiers: &mut self.modifiers,
|
|
|
|
message_buffer: &mut self.message_buffer,
|
2019-11-03 19:02:26 +00:00
|
|
|
display_update_pending: &mut display_update_pending,
|
2019-10-05 00:29:26 +00:00
|
|
|
window: &mut self.display.window,
|
2019-11-03 19:02:26 +00:00
|
|
|
font_size: &mut self.font_size,
|
|
|
|
config: &mut self.config,
|
2020-03-18 02:35:08 +00:00
|
|
|
urls: &self.display.urls,
|
2020-02-07 13:44:11 +00:00
|
|
|
event_loop,
|
2019-10-05 00:29:26 +00:00
|
|
|
};
|
2020-03-18 02:35:08 +00:00
|
|
|
let mut processor = input::Processor::new(context, &self.display.highlighted_url);
|
2019-10-05 00:29:26 +00:00
|
|
|
|
2020-05-03 23:29:11 +00:00
|
|
|
for event in self.event_queue.drain(..) {
|
2019-10-05 00:29:26 +00:00
|
|
|
Processor::handle_event(event, &mut processor);
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:29:11 +00:00
|
|
|
// Process DisplayUpdate events.
|
2019-11-03 19:02:26 +00:00
|
|
|
if !display_update_pending.is_empty() {
|
|
|
|
self.display.handle_update(
|
2019-10-05 00:29:26 +00:00
|
|
|
&mut terminal,
|
2019-12-14 21:32:24 +00:00
|
|
|
&mut self.notifier,
|
2019-10-05 00:29:26 +00:00
|
|
|
&self.message_buffer,
|
|
|
|
&self.config,
|
2019-11-03 19:02:26 +00:00
|
|
|
display_update_pending,
|
2019-10-05 00:29:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:29:11 +00:00
|
|
|
#[cfg(not(any(target_os = "macos", windows)))]
|
|
|
|
{
|
|
|
|
// Skip rendering on Wayland until we get frame event from compositor.
|
|
|
|
if event_loop.is_wayland()
|
|
|
|
&& !self.display.window.should_draw.load(Ordering::Relaxed)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-05 00:29:26 +00:00
|
|
|
if terminal.dirty {
|
2019-10-24 22:26:33 +00:00
|
|
|
terminal.dirty = false;
|
|
|
|
|
2020-05-03 23:29:11 +00:00
|
|
|
// Request immediate re-draw if visual bell animation is not finished yet.
|
2019-10-24 22:26:33 +00:00
|
|
|
if !terminal.visual_bell.completed() {
|
2020-05-03 23:29:11 +00:00
|
|
|
self.event_queue.push(GlutinEvent::UserEvent(Event::Wakeup));
|
2019-10-24 22:26:33 +00:00
|
|
|
}
|
2019-10-05 00:29:26 +00:00
|
|
|
|
2020-05-03 23:29:11 +00:00
|
|
|
// Redraw screen.
|
2019-11-03 20:59:28 +00:00
|
|
|
self.display.draw(
|
|
|
|
terminal,
|
|
|
|
&self.message_buffer,
|
|
|
|
&self.config,
|
|
|
|
&self.mouse,
|
2019-11-11 21:05:24 +00:00
|
|
|
self.modifiers,
|
2019-11-03 20:59:28 +00:00
|
|
|
);
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Write ref tests to disk.
|
2019-10-05 00:29:26 +00:00
|
|
|
self.write_ref_test_results(&terminal.lock());
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Handle events from glutin.
|
2019-10-05 00:29:26 +00:00
|
|
|
///
|
2019-10-24 22:26:33 +00:00
|
|
|
/// Doesn't take self mutably due to borrow checking.
|
2019-10-05 00:29:26 +00:00
|
|
|
fn handle_event<T>(
|
|
|
|
event: GlutinEvent<Event>,
|
|
|
|
processor: &mut input::Processor<T, ActionContext<N, T>>,
|
|
|
|
) where
|
|
|
|
T: EventListener,
|
|
|
|
{
|
|
|
|
match event {
|
|
|
|
GlutinEvent::UserEvent(event) => match event {
|
2020-01-10 01:51:37 +00:00
|
|
|
Event::DPRChanged(scale_factor, (width, height)) => {
|
|
|
|
let display_update_pending = &mut processor.ctx.display_update_pending;
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Push current font to update its DPR.
|
2020-01-11 04:19:40 +00:00
|
|
|
display_update_pending.font =
|
|
|
|
Some(processor.ctx.config.font.clone().with_size(*processor.ctx.font_size));
|
2020-01-10 01:51:37 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Resize to event's dimensions, since no resize event is emitted on Wayland.
|
2020-01-10 01:51:37 +00:00
|
|
|
display_update_pending.dimensions = Some(PhysicalSize::new(width, height));
|
|
|
|
|
|
|
|
processor.ctx.size_info.dpr = scale_factor;
|
|
|
|
processor.ctx.terminal.dirty = true;
|
|
|
|
},
|
2019-10-05 00:29:26 +00:00
|
|
|
Event::Title(title) => processor.ctx.window.set_title(&title),
|
|
|
|
Event::Wakeup => processor.ctx.terminal.dirty = true,
|
|
|
|
Event::Urgent => {
|
|
|
|
processor.ctx.window.set_urgent(!processor.ctx.terminal.is_focused)
|
|
|
|
},
|
2020-04-09 01:02:10 +00:00
|
|
|
Event::ConfigReload(path) => Self::reload_config(&path, processor),
|
2019-10-05 00:29:26 +00:00
|
|
|
Event::Message(message) => {
|
|
|
|
processor.ctx.message_buffer.push(message);
|
2020-04-15 03:50:34 +00:00
|
|
|
processor.ctx.display_update_pending.message_buffer = true;
|
2019-10-05 00:29:26 +00:00
|
|
|
processor.ctx.terminal.dirty = true;
|
|
|
|
},
|
|
|
|
Event::MouseCursorDirty => processor.reset_mouse_cursor(),
|
|
|
|
Event::Exit => (),
|
|
|
|
},
|
2020-01-05 01:00:50 +00:00
|
|
|
GlutinEvent::RedrawRequested(_) => processor.ctx.terminal.dirty = true,
|
2019-10-05 00:29:26 +00:00
|
|
|
GlutinEvent::WindowEvent { event, window_id, .. } => {
|
|
|
|
match event {
|
2020-03-07 22:17:38 +00:00
|
|
|
WindowEvent::CloseRequested => processor.ctx.terminal.exit(),
|
|
|
|
WindowEvent::Resized(size) => {
|
2019-12-09 17:26:31 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
// Minimizing the window sends a Resize event with zero width and
|
|
|
|
// height. But there's no need to ever actually resize to this.
|
|
|
|
// Both WinPTY & ConPTY have issues when resizing down to zero size
|
|
|
|
// and back.
|
2020-01-10 01:51:37 +00:00
|
|
|
if size.width == 0 && size.height == 0 {
|
2019-12-09 17:26:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 01:51:37 +00:00
|
|
|
processor.ctx.display_update_pending.dimensions = Some(size);
|
2019-10-05 00:29:26 +00:00
|
|
|
processor.ctx.terminal.dirty = true;
|
|
|
|
},
|
2020-03-07 22:17:38 +00:00
|
|
|
WindowEvent::KeyboardInput { input, is_synthetic: false, .. } => {
|
2019-11-11 21:05:24 +00:00
|
|
|
processor.key_input(input);
|
2019-10-05 00:29:26 +00:00
|
|
|
},
|
2020-03-07 22:17:38 +00:00
|
|
|
WindowEvent::ReceivedCharacter(c) => processor.received_char(c),
|
|
|
|
WindowEvent::MouseInput { state, button, .. } => {
|
2020-02-27 23:42:39 +00:00
|
|
|
processor.ctx.window.set_mouse_visible(true);
|
|
|
|
processor.mouse_input(state, button);
|
|
|
|
processor.ctx.terminal.dirty = true;
|
2019-10-05 00:29:26 +00:00
|
|
|
},
|
2020-03-11 22:31:59 +00:00
|
|
|
WindowEvent::ModifiersChanged(modifiers) => {
|
|
|
|
processor.modifiers_input(modifiers)
|
|
|
|
},
|
2020-03-07 22:17:38 +00:00
|
|
|
WindowEvent::CursorMoved { position, .. } => {
|
2020-01-10 01:51:37 +00:00
|
|
|
let (x, y) = position.into();
|
|
|
|
let x = limit(x, 0, processor.ctx.size_info.width as i32);
|
|
|
|
let y = limit(y, 0, processor.ctx.size_info.height as i32);
|
2019-10-05 00:29:26 +00:00
|
|
|
|
|
|
|
processor.ctx.window.set_mouse_visible(true);
|
2020-01-05 01:00:50 +00:00
|
|
|
processor.mouse_moved(x as usize, y as usize);
|
2019-10-05 00:29:26 +00:00
|
|
|
},
|
2020-03-07 22:17:38 +00:00
|
|
|
WindowEvent::MouseWheel { delta, phase, .. } => {
|
2019-10-05 00:29:26 +00:00
|
|
|
processor.ctx.window.set_mouse_visible(true);
|
2020-01-05 01:00:50 +00:00
|
|
|
processor.mouse_wheel_input(delta, phase);
|
2019-10-05 00:29:26 +00:00
|
|
|
},
|
2020-03-07 22:17:38 +00:00
|
|
|
WindowEvent::Focused(is_focused) => {
|
2019-10-05 00:29:26 +00:00
|
|
|
if window_id == processor.ctx.window.window_id() {
|
|
|
|
processor.ctx.terminal.is_focused = is_focused;
|
|
|
|
processor.ctx.terminal.dirty = true;
|
|
|
|
|
|
|
|
if is_focused {
|
|
|
|
processor.ctx.window.set_urgent(false);
|
|
|
|
} else {
|
|
|
|
processor.ctx.window.set_mouse_visible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
processor.on_focus_change(is_focused);
|
|
|
|
}
|
|
|
|
},
|
2020-03-07 22:17:38 +00:00
|
|
|
WindowEvent::DroppedFile(path) => {
|
2019-10-05 00:29:26 +00:00
|
|
|
let path: String = path.to_string_lossy().into();
|
2020-06-03 00:01:26 +00:00
|
|
|
processor.ctx.write_to_pty((path + " ").into_bytes());
|
2019-10-05 00:29:26 +00:00
|
|
|
},
|
2020-03-07 22:17:38 +00:00
|
|
|
WindowEvent::CursorLeft { .. } => {
|
2019-11-03 20:59:28 +00:00
|
|
|
processor.ctx.mouse.inside_grid = false;
|
|
|
|
|
|
|
|
if processor.highlighted_url.is_some() {
|
|
|
|
processor.ctx.terminal.dirty = true;
|
|
|
|
}
|
|
|
|
},
|
2020-03-07 22:17:38 +00:00
|
|
|
WindowEvent::KeyboardInput { is_synthetic: true, .. }
|
|
|
|
| WindowEvent::TouchpadPressure { .. }
|
|
|
|
| WindowEvent::ScaleFactorChanged { .. }
|
|
|
|
| WindowEvent::CursorEntered { .. }
|
|
|
|
| WindowEvent::AxisMotion { .. }
|
|
|
|
| WindowEvent::HoveredFileCancelled
|
|
|
|
| WindowEvent::Destroyed
|
|
|
|
| WindowEvent::ThemeChanged(_)
|
|
|
|
| WindowEvent::HoveredFile(_)
|
|
|
|
| WindowEvent::Touch(_)
|
|
|
|
| WindowEvent::Moved(_) => (),
|
2019-10-05 00:29:26 +00:00
|
|
|
}
|
|
|
|
},
|
2019-11-11 21:05:24 +00:00
|
|
|
GlutinEvent::Suspended { .. }
|
2019-10-05 00:29:26 +00:00
|
|
|
| GlutinEvent::NewEvents { .. }
|
2020-03-11 22:31:59 +00:00
|
|
|
| GlutinEvent::DeviceEvent { .. }
|
2020-01-05 01:00:50 +00:00
|
|
|
| GlutinEvent::MainEventsCleared
|
|
|
|
| GlutinEvent::RedrawEventsCleared
|
2019-10-05 00:29:26 +00:00
|
|
|
| GlutinEvent::Resumed
|
|
|
|
| GlutinEvent::LoopDestroyed => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Check if an event is irrelevant and can be skipped.
|
2019-10-05 00:29:26 +00:00
|
|
|
fn skip_event(event: &GlutinEvent<Event>) -> bool {
|
|
|
|
match event {
|
2020-03-07 22:17:38 +00:00
|
|
|
GlutinEvent::WindowEvent { event, .. } => match event {
|
|
|
|
WindowEvent::KeyboardInput { is_synthetic: true, .. }
|
|
|
|
| WindowEvent::TouchpadPressure { .. }
|
|
|
|
| WindowEvent::CursorEntered { .. }
|
|
|
|
| WindowEvent::AxisMotion { .. }
|
|
|
|
| WindowEvent::HoveredFileCancelled
|
|
|
|
| WindowEvent::Destroyed
|
|
|
|
| WindowEvent::HoveredFile(_)
|
|
|
|
| WindowEvent::Touch(_)
|
|
|
|
| WindowEvent::Moved(_) => true,
|
|
|
|
_ => false,
|
2019-10-05 00:29:26 +00:00
|
|
|
},
|
2019-11-11 21:05:24 +00:00
|
|
|
GlutinEvent::Suspended { .. }
|
2019-10-05 00:29:26 +00:00
|
|
|
| GlutinEvent::NewEvents { .. }
|
2020-01-05 01:00:50 +00:00
|
|
|
| GlutinEvent::MainEventsCleared
|
2019-10-05 00:29:26 +00:00
|
|
|
| GlutinEvent::LoopDestroyed => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 01:02:10 +00:00
|
|
|
pub fn reload_config<T>(
|
|
|
|
path: &PathBuf,
|
|
|
|
processor: &mut input::Processor<T, ActionContext<N, T>>,
|
|
|
|
) where
|
|
|
|
T: EventListener,
|
|
|
|
{
|
|
|
|
processor.ctx.message_buffer.remove_target(LOG_TARGET_CONFIG);
|
2020-04-15 03:50:34 +00:00
|
|
|
processor.ctx.display_update_pending.message_buffer = true;
|
2020-04-09 01:02:10 +00:00
|
|
|
|
|
|
|
let config = match config::reload_from(&path) {
|
|
|
|
Ok(config) => config,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let options = Options::new();
|
|
|
|
let config = options.into_config(config);
|
|
|
|
|
|
|
|
processor.ctx.terminal.update_config(&config);
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Reload cursor if we've changed its thickness.
|
2020-04-15 03:50:34 +00:00
|
|
|
if (processor.ctx.config.cursor.thickness() - config.cursor.thickness()).abs()
|
|
|
|
> std::f64::EPSILON
|
|
|
|
{
|
|
|
|
processor.ctx.display_update_pending.cursor = true;
|
|
|
|
}
|
|
|
|
|
2020-04-09 01:02:10 +00:00
|
|
|
if processor.ctx.config.font != config.font {
|
2020-05-05 22:50:23 +00:00
|
|
|
// Do not update font size if it has been changed at runtime.
|
2020-04-09 01:02:10 +00:00
|
|
|
if *processor.ctx.font_size == processor.ctx.config.font.size {
|
|
|
|
*processor.ctx.font_size = config.font.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
let font = config.font.clone().with_size(*processor.ctx.font_size);
|
|
|
|
processor.ctx.display_update_pending.font = Some(font);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(target_os = "macos", windows)))]
|
|
|
|
{
|
|
|
|
if processor.ctx.event_loop.is_wayland() {
|
|
|
|
processor.ctx.window.set_wayland_theme(&config.colors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 21:14:13 +00:00
|
|
|
// Set subpixel anti-aliasing.
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
set_font_smoothing(config.font.use_thin_strokes());
|
|
|
|
|
2020-04-09 01:02:10 +00:00
|
|
|
*processor.ctx.config = config;
|
|
|
|
|
|
|
|
processor.ctx.terminal.dirty = true;
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Write the ref test results to the disk.
|
2019-10-05 00:29:26 +00:00
|
|
|
pub fn write_ref_test_results<T>(&self, terminal: &Term<T>) {
|
|
|
|
if !self.config.debug.ref_test {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
// Dump grid state.
|
2019-10-05 00:29:26 +00:00
|
|
|
let mut grid = terminal.grid().clone();
|
2020-05-30 20:45:44 +00:00
|
|
|
grid.initialize_all(Cell::default());
|
2019-10-05 00:29:26 +00:00
|
|
|
grid.truncate();
|
|
|
|
|
|
|
|
let serialized_grid = json::to_string(&grid).expect("serialize grid");
|
|
|
|
|
|
|
|
let serialized_size = json::to_string(&self.display.size_info).expect("serialize size");
|
|
|
|
|
|
|
|
let serialized_config = format!("{{\"history_size\":{}}}", grid.history_size());
|
|
|
|
|
|
|
|
File::create("./grid.json")
|
|
|
|
.and_then(|mut f| f.write_all(serialized_grid.as_bytes()))
|
|
|
|
.expect("write grid.json");
|
|
|
|
|
|
|
|
File::create("./size.json")
|
|
|
|
.and_then(|mut f| f.write_all(serialized_size.as_bytes()))
|
|
|
|
.expect("write size.json");
|
|
|
|
|
|
|
|
File::create("./config.json")
|
|
|
|
.and_then(|mut f| f.write_all(serialized_config.as_bytes()))
|
|
|
|
.expect("write config.json");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct EventProxy(EventLoopProxy<Event>);
|
|
|
|
|
|
|
|
impl EventProxy {
|
|
|
|
pub fn new(proxy: EventLoopProxy<Event>) -> Self {
|
|
|
|
EventProxy(proxy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EventListener for EventProxy {
|
|
|
|
fn send_event(&self, event: Event) {
|
|
|
|
let _ = self.0.send_event(event);
|
|
|
|
}
|
|
|
|
}
|