1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 15:04:36 +03:00

remove Mux::config in favor of config::configuration()

The idea is to centralize accessing the config to that
function so that we can implement config reloading.
This commit is contained in:
Wez Furlong 2019-11-24 08:55:55 -08:00
parent 72b55d3982
commit 8996f897b9
14 changed files with 94 additions and 141 deletions

View File

@ -28,14 +28,13 @@ pub mod fontloader;
#[cfg(any(target_os = "macos", windows))] #[cfg(any(target_os = "macos", windows))]
pub mod fontloader_and_freetype; pub mod fontloader_and_freetype;
use super::config::{Config, TextStyle}; use super::config::{configuration, Config, TextStyle};
use term::CellAttributes; use term::CellAttributes;
type FontPtr = Rc<RefCell<Box<dyn NamedFont>>>; type FontPtr = Rc<RefCell<Box<dyn NamedFont>>>;
/// Matches and loads fonts for a given input style /// Matches and loads fonts for a given input style
pub struct FontConfiguration { pub struct FontConfiguration {
config: Arc<Config>,
fonts: RefCell<HashMap<TextStyle, FontPtr>>, fonts: RefCell<HashMap<TextStyle, FontPtr>>,
system: Rc<dyn FontSystem>, system: Rc<dyn FontSystem>,
metrics: RefCell<Option<FontMetrics>>, metrics: RefCell<Option<FontMetrics>>,
@ -129,9 +128,8 @@ impl std::str::FromStr for FontSystemSelection {
impl FontConfiguration { impl FontConfiguration {
/// Create a new empty configuration /// Create a new empty configuration
pub fn new(config: Arc<Config>, system: FontSystemSelection) -> Self { pub fn new(system: FontSystemSelection) -> Self {
Self { Self {
config,
fonts: RefCell::new(HashMap::new()), fonts: RefCell::new(HashMap::new()),
system: system.new_font_system(), system: system.new_font_system(),
metrics: RefCell::new(None), metrics: RefCell::new(None),
@ -151,7 +149,7 @@ impl FontConfiguration {
let scale = *self.dpi_scale.borrow() * *self.font_scale.borrow(); let scale = *self.dpi_scale.borrow() * *self.font_scale.borrow();
let font = Rc::new(RefCell::new(self.system.load_font( let font = Rc::new(RefCell::new(self.system.load_font(
&self.config, &configuration(),
style, style,
scale, scale,
)?)); )?));
@ -168,7 +166,7 @@ impl FontConfiguration {
/// Returns the baseline font specified in the configuration /// Returns the baseline font specified in the configuration
pub fn default_font(&self) -> Result<Rc<RefCell<Box<dyn NamedFont>>>, Error> { pub fn default_font(&self) -> Result<Rc<RefCell<Box<dyn NamedFont>>>, Error> {
self.cached_font(&self.config.font) self.cached_font(&configuration().font)
} }
pub fn get_font_scale(&self) -> f64 { pub fn get_font_scale(&self) -> f64 {
@ -194,7 +192,11 @@ impl FontConfiguration {
/// Apply the defined font_rules from the user configuration to /// Apply the defined font_rules from the user configuration to
/// produce the text style that best matches the supplied input /// produce the text style that best matches the supplied input
/// cell attributes. /// cell attributes.
pub fn match_style(&self, attrs: &CellAttributes) -> &TextStyle { pub fn match_style<'a>(
&self,
config: &'a Arc<Config>,
attrs: &CellAttributes,
) -> &'a TextStyle {
// a little macro to avoid boilerplate for matching the rules. // a little macro to avoid boilerplate for matching the rules.
// If the rule doesn't specify a value for an attribute then // If the rule doesn't specify a value for an attribute then
// it will implicitly match. If it specifies an attribute // it will implicitly match. If it specifies an attribute
@ -211,7 +213,7 @@ impl FontConfiguration {
}; };
}; };
for rule in &self.config.font_rules { for rule in &config.font_rules {
attr_match!(intensity, &rule); attr_match!(intensity, &rule);
attr_match!(underline, &rule); attr_match!(underline, &rule);
attr_match!(italic, &rule); attr_match!(italic, &rule);
@ -224,7 +226,7 @@ impl FontConfiguration {
// so we therefore assume that it did match overall. // so we therefore assume that it did match overall.
return &rule.font; return &rule.font;
} }
&self.config.font &config.font
} }
} }

View File

@ -1,4 +1,4 @@
use crate::config::Config; use crate::config::configuration;
use crate::font::FontConfiguration; use crate::font::FontConfiguration;
use crate::frontend::FrontEnd; use crate::frontend::FrontEnd;
use crate::mux::tab::Tab; use crate::mux::tab::Tab;
@ -121,11 +121,10 @@ impl FrontEnd for GuiFrontEnd {
fn spawn_new_window( fn spawn_new_window(
&self, &self,
config: &Arc<Config>,
fontconfig: &Rc<FontConfiguration>, fontconfig: &Rc<FontConfiguration>,
tab: &Rc<dyn Tab>, tab: &Rc<dyn Tab>,
window_id: MuxWindowId, window_id: MuxWindowId,
) -> Fallible<()> { ) -> Fallible<()> {
termwindow::TermWindow::new_window(config, fontconfig, tab, window_id) termwindow::TermWindow::new_window(&configuration(), fontconfig, tab, window_id)
} }
} }

View File

@ -2,7 +2,7 @@ use super::quad::*;
use super::renderstate::*; use super::renderstate::*;
use super::utilsprites::RenderMetrics; use super::utilsprites::RenderMetrics;
use crate::clipboard::SystemClipboard; use crate::clipboard::SystemClipboard;
use crate::config::Config; use crate::config::{configuration, Config};
use crate::font::{FontConfiguration, FontSystemSelection}; use crate::font::{FontConfiguration, FontSystemSelection};
use crate::frontend::gui::tabbar::{TabBarItem, TabBarState}; use crate::frontend::gui::tabbar::{TabBarItem, TabBarState};
use crate::frontend::{executor, front_end}; use crate::frontend::{executor, front_end};
@ -27,7 +27,6 @@ use termwiz::color::RgbColor;
pub struct TermWindow { pub struct TermWindow {
window: Option<Window>, window: Option<Window>,
fonts: Rc<FontConfiguration>, fonts: Rc<FontConfiguration>,
config: Arc<Config>,
dimensions: Dimensions, dimensions: Dimensions,
mux_window_id: MuxWindowId, mux_window_id: MuxWindowId,
render_metrics: RenderMetrics, render_metrics: RenderMetrics,
@ -256,7 +255,7 @@ impl WindowCallbacks for TermWindow {
return true; return true;
} }
if !self.config.send_composed_key_when_alt_is_pressed if !configuration().send_composed_key_when_alt_is_pressed
&& modifiers.contains(::termwiz::input::Modifiers::ALT) && modifiers.contains(::termwiz::input::Modifiers::ALT)
&& tab.key_down(key, modifiers).is_ok() && tab.key_down(key, modifiers).is_ok()
{ {
@ -388,7 +387,6 @@ impl TermWindow {
Box::new(Self { Box::new(Self {
window: None, window: None,
mux_window_id, mux_window_id,
config: Arc::clone(config),
fonts: Rc::clone(fontconfig), fonts: Rc::clone(fontconfig),
render_metrics, render_metrics,
dimensions: Dimensions { dimensions: Dimensions {
@ -475,14 +473,14 @@ impl TermWindow {
WK::Char('\r') => KC::Enter, WK::Char('\r') => KC::Enter,
WK::Char('\t') => KC::Tab, WK::Char('\t') => KC::Tab,
WK::Char('\u{08}') => { WK::Char('\u{08}') => {
if self.config.swap_backspace_and_delete { if configuration().swap_backspace_and_delete {
KC::Delete KC::Delete
} else { } else {
KC::Backspace KC::Backspace
} }
} }
WK::Char('\u{7f}') => { WK::Char('\u{7f}') => {
if self.config.swap_backspace_and_delete { if configuration().swap_backspace_and_delete {
KC::Backspace KC::Backspace
} else { } else {
KC::Delete KC::Delete
@ -596,7 +594,10 @@ impl TermWindow {
None None
}, },
&window, &window,
self.config.colors.as_ref().and_then(|c| c.tab_bar.as_ref()), configuration()
.colors
.as_ref()
.and_then(|c| c.tab_bar.as_ref()),
); );
if new_tab_bar != self.tab_bar { if new_tab_bar != self.tab_bar {
self.tab_bar = new_tab_bar; self.tab_bar = new_tab_bar;
@ -778,16 +779,13 @@ impl TermWindow {
pub fn spawn_new_window(&mut self) { pub fn spawn_new_window(&mut self) {
promise::Future::with_executor(executor(), move || { promise::Future::with_executor(executor(), move || {
let mux = Mux::get().unwrap(); let mux = Mux::get().unwrap();
let fonts = Rc::new(FontConfiguration::new( let fonts = Rc::new(FontConfiguration::new(FontSystemSelection::get_default()));
Arc::clone(mux.config()),
FontSystemSelection::get_default(),
));
let window_id = mux.new_empty_window(); let window_id = mux.new_empty_window();
let tab = let tab =
mux.default_domain() mux.default_domain()
.spawn(portable_pty::PtySize::default(), None, window_id)?; .spawn(portable_pty::PtySize::default(), None, window_id)?;
let front_end = front_end().expect("to be called on gui thread"); let front_end = front_end().expect("to be called on gui thread");
front_end.spawn_new_window(mux.config(), &fonts, &tab, window_id)?; front_end.spawn_new_window(&fonts, &tab, window_id)?;
Ok(()) Ok(())
}); });
} }
@ -1049,13 +1047,14 @@ impl TermWindow {
// Break the line into clusters of cells with the same attributes // Break the line into clusters of cells with the same attributes
let cell_clusters = line.cluster(); let cell_clusters = line.cluster();
let mut last_cell_idx = 0; let mut last_cell_idx = 0;
let config = configuration();
for cluster in cell_clusters { for cluster in cell_clusters {
let attrs = &cluster.attrs; let attrs = &cluster.attrs;
let is_highlited_hyperlink = match (&attrs.hyperlink, &current_highlight) { let is_highlited_hyperlink = match (&attrs.hyperlink, &current_highlight) {
(&Some(ref this), &Some(ref highlight)) => this == highlight, (&Some(ref this), &Some(ref highlight)) => this == highlight,
_ => false, _ => false,
}; };
let style = self.fonts.match_style(attrs); let style = self.fonts.match_style(&config, attrs);
let bg_color = palette.resolve_bg(attrs.background); let bg_color = palette.resolve_bg(attrs.background);
let fg_color = match attrs.foreground { let fg_color = match attrs.foreground {
@ -1269,6 +1268,8 @@ impl TermWindow {
terminal: &dyn Renderable, terminal: &dyn Renderable,
palette: &ColorPalette, palette: &ColorPalette,
) -> Fallible<()> { ) -> Fallible<()> {
let config = configuration();
let (_num_rows, num_cols) = terminal.physical_dimensions(); let (_num_rows, num_cols) = terminal.physical_dimensions();
let current_highlight = terminal.current_highlight(); let current_highlight = terminal.current_highlight();
@ -1281,7 +1282,7 @@ impl TermWindow {
(&Some(ref this), &Some(ref highlight)) => this == highlight, (&Some(ref this), &Some(ref highlight)) => this == highlight,
_ => false, _ => false,
}; };
let style = self.fonts.match_style(attrs); let style = self.fonts.match_style(&config, attrs);
let bg_color = palette.resolve_bg(attrs.background); let bg_color = palette.resolve_bg(attrs.background);
let fg_color = match attrs.foreground { let fg_color = match attrs.foreground {

View File

@ -1,4 +1,3 @@
use crate::config::Config;
use crate::font::FontConfiguration; use crate::font::FontConfiguration;
use crate::mux::tab::Tab; use crate::mux::tab::Tab;
use crate::mux::window::WindowId; use crate::mux::window::WindowId;
@ -9,7 +8,7 @@ use promise::Executor;
use serde_derive::*; use serde_derive::*;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use std::sync::{Arc, Mutex}; use std::sync::Mutex;
pub mod gui; pub mod gui;
pub mod muxserver; pub mod muxserver;
@ -111,7 +110,6 @@ pub trait FrontEnd: Downcast {
fn spawn_new_window( fn spawn_new_window(
&self, &self,
config: &Arc<Config>,
fontconfig: &Rc<FontConfiguration>, fontconfig: &Rc<FontConfiguration>,
tab: &Rc<dyn Tab>, tab: &Rc<dyn Tab>,
window_id: WindowId, window_id: WindowId,

View File

@ -1,5 +1,4 @@
//! Implements the multiplexer server frontend //! Implements the multiplexer server frontend
use crate::config::Config;
use crate::font::FontConfiguration; use crate::font::FontConfiguration;
use crate::frontend::FrontEnd; use crate::frontend::FrontEnd;
use crate::mux::tab::Tab; use crate::mux::tab::Tab;
@ -11,7 +10,6 @@ use log::info;
use promise::*; use promise::*;
use std::rc::Rc; use std::rc::Rc;
use std::sync::mpsc::{self, Receiver, Sender}; use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::Arc;
#[derive(Clone)] #[derive(Clone)]
struct MuxExecutor { struct MuxExecutor {
@ -43,8 +41,7 @@ impl MuxServerFrontEnd {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
if start_listener { if start_listener {
let mux = Mux::get().unwrap(); spawn_listener()?;
spawn_listener(mux.config())?;
} }
Ok(Rc::new(Self { tx, rx })) Ok(Rc::new(Self { tx, rx }))
} }
@ -85,7 +82,6 @@ impl FrontEnd for MuxServerFrontEnd {
fn spawn_new_window( fn spawn_new_window(
&self, &self,
_config: &Arc<Config>,
_fontconfig: &Rc<FontConfiguration>, _fontconfig: &Rc<FontConfiguration>,
_tab: &Rc<dyn Tab>, _tab: &Rc<dyn Tab>,
_window_id: WindowId, _window_id: WindowId,

View File

@ -1,5 +1,5 @@
use crate::config::configuration;
use crate::mux::domain::DomainId; use crate::mux::domain::DomainId;
use crate::mux::Mux;
use std::collections::HashMap; use std::collections::HashMap;
use term::{KeyCode, KeyModifiers}; use term::{KeyCode, KeyModifiers};
@ -40,9 +40,7 @@ pub struct KeyMap(HashMap<(KeyCode, KeyModifiers), KeyAssignment>);
impl KeyMap { impl KeyMap {
pub fn new() -> Self { pub fn new() -> Self {
let mux = Mux::get().unwrap(); let mut map = configuration()
let mut map = mux
.config()
.key_bindings() .key_bindings()
.expect("keys section of config to be valid"); .expect("keys section of config to be valid");

View File

@ -352,7 +352,7 @@ fn run_ssh(config: Arc<config::Config>, opts: &SshCommand) -> Fallible<()> {
// Set up the mux with no default domain; there's a good chance that // Set up the mux with no default domain; there's a good chance that
// we'll need to show authentication UI and we don't want its domain // we'll need to show authentication UI and we don't want its domain
// to become the default domain. // to become the default domain.
let mux = Rc::new(mux::Mux::new(&config, None)); let mux = Rc::new(mux::Mux::new(None));
Mux::set_mux(&mux); Mux::set_mux(&mux);
// Initiate an ssh connection; since that is a blocking process with // Initiate an ssh connection; since that is a blocking process with
@ -379,7 +379,7 @@ fn run_ssh(config: Arc<config::Config>, opts: &SshCommand) -> Fallible<()> {
let font_system = opts.font_system.unwrap_or(config.font_system); let font_system = opts.font_system.unwrap_or(config.font_system);
font_system.set_default(); font_system.set_default();
let fontconfig = Rc::new(FontConfiguration::new(Arc::clone(&config), font_system)); let fontconfig = Rc::new(FontConfiguration::new(font_system));
let cmd = if !opts.prog.is_empty() { let cmd = if !opts.prog.is_empty() {
let argv: Vec<&std::ffi::OsStr> = opts.prog.iter().map(|x| x.as_os_str()).collect(); let argv: Vec<&std::ffi::OsStr> = opts.prog.iter().map(|x| x.as_os_str()).collect();
let mut builder = CommandBuilder::new(&argv[0]); let mut builder = CommandBuilder::new(&argv[0]);
@ -392,7 +392,6 @@ fn run_ssh(config: Arc<config::Config>, opts: &SshCommand) -> Fallible<()> {
let pty_system = Box::new(portable_pty::ssh::SshSession::new(sess, &config.term)); let pty_system = Box::new(portable_pty::ssh::SshSession::new(sess, &config.term));
let domain: Arc<dyn Domain> = Arc::new(ssh::RemoteSshDomain::with_pty_system( let domain: Arc<dyn Domain> = Arc::new(ssh::RemoteSshDomain::with_pty_system(
&opts.user_at_host_and_port, &opts.user_at_host_and_port,
&config,
pty_system, pty_system,
)); ));
@ -403,7 +402,7 @@ fn run_ssh(config: Arc<config::Config>, opts: &SshCommand) -> Fallible<()> {
let window_id = mux.new_empty_window(); let window_id = mux.new_empty_window();
let tab = domain.spawn(PtySize::default(), cmd, window_id)?; let tab = domain.spawn(PtySize::default(), cmd, window_id)?;
gui.spawn_new_window(mux.config(), &fontconfig, &tab, window_id)?; gui.spawn_new_window(&fontconfig, &tab, window_id)?;
Ok(()) Ok(())
}); });
}); });
@ -415,7 +414,7 @@ fn run_serial(config: Arc<config::Config>, opts: &SerialCommand) -> Fallible<()>
let font_system = opts.font_system.unwrap_or(config.font_system); let font_system = opts.font_system.unwrap_or(config.font_system);
font_system.set_default(); font_system.set_default();
let fontconfig = Rc::new(FontConfiguration::new(Arc::clone(&config), font_system)); let fontconfig = Rc::new(FontConfiguration::new(font_system));
let mut serial = portable_pty::serial::SerialTty::new(&opts.port); let mut serial = portable_pty::serial::SerialTty::new(&opts.port);
if let Some(baud) = opts.baud { if let Some(baud) = opts.baud {
@ -423,9 +422,8 @@ fn run_serial(config: Arc<config::Config>, opts: &SerialCommand) -> Fallible<()>
} }
let pty_system = Box::new(portable_pty::serial::SerialTty::new(&opts.port)); let pty_system = Box::new(portable_pty::serial::SerialTty::new(&opts.port));
let domain: Arc<dyn Domain> = let domain: Arc<dyn Domain> = Arc::new(LocalDomain::with_pty_system("local", pty_system));
Arc::new(LocalDomain::with_pty_system("local", &config, pty_system)); let mux = Rc::new(mux::Mux::new(Some(domain.clone())));
let mux = Rc::new(mux::Mux::new(&config, Some(domain.clone())));
Mux::set_mux(&mux); Mux::set_mux(&mux);
let front_end = opts.front_end.unwrap_or(config.front_end); let front_end = opts.front_end.unwrap_or(config.front_end);
@ -434,7 +432,7 @@ fn run_serial(config: Arc<config::Config>, opts: &SerialCommand) -> Fallible<()>
let window_id = mux.new_empty_window(); let window_id = mux.new_empty_window();
let tab = domain.spawn(PtySize::default(), None, window_id)?; let tab = domain.spawn(PtySize::default(), None, window_id)?;
gui.spawn_new_window(mux.config(), &fontconfig, &tab, window_id)?; gui.spawn_new_window(&fontconfig, &tab, window_id)?;
gui.run_forever() gui.run_forever()
} }
@ -469,10 +467,10 @@ fn run_mux_client(config: Arc<config::Config>, opts: &ConnectCommand) -> Fallibl
let font_system = opts.font_system.unwrap_or(config.font_system); let font_system = opts.font_system.unwrap_or(config.font_system);
font_system.set_default(); font_system.set_default();
let fontconfig = Rc::new(FontConfiguration::new(Arc::clone(&config), font_system)); let fontconfig = Rc::new(FontConfiguration::new(font_system));
let domain: Arc<dyn Domain> = Arc::new(ClientDomain::new(client_config)); let domain: Arc<dyn Domain> = Arc::new(ClientDomain::new(client_config));
let mux = Rc::new(mux::Mux::new(&config, Some(domain.clone()))); let mux = Rc::new(mux::Mux::new(Some(domain.clone())));
Mux::set_mux(&mux); Mux::set_mux(&mux);
let front_end = opts.front_end.unwrap_or(config.front_end); let front_end = opts.front_end.unwrap_or(config.front_end);
@ -492,7 +490,7 @@ fn run_mux_client(config: Arc<config::Config>, opts: &ConnectCommand) -> Fallibl
let tab = mux let tab = mux
.default_domain() .default_domain()
.spawn(PtySize::default(), cmd, window_id)?; .spawn(PtySize::default(), cmd, window_id)?;
gui.spawn_new_window(mux.config(), &fontconfig, &tab, window_id)?; gui.spawn_new_window(&fontconfig, &tab, window_id)?;
} }
for dom in mux.iter_domains() { for dom in mux.iter_domains() {
@ -543,7 +541,7 @@ fn run_terminal_gui(config: Arc<config::Config>, opts: &StartCommand) -> Fallibl
let font_system = opts.font_system.unwrap_or(config.font_system); let font_system = opts.font_system.unwrap_or(config.font_system);
font_system.set_default(); font_system.set_default();
let fontconfig = Rc::new(FontConfiguration::new(Arc::clone(&config), font_system)); let fontconfig = Rc::new(FontConfiguration::new(font_system));
let cmd = if !opts.prog.is_empty() { let cmd = if !opts.prog.is_empty() {
let argv: Vec<&std::ffi::OsStr> = opts.prog.iter().map(|x| x.as_os_str()).collect(); let argv: Vec<&std::ffi::OsStr> = opts.prog.iter().map(|x| x.as_os_str()).collect();
@ -554,8 +552,8 @@ fn run_terminal_gui(config: Arc<config::Config>, opts: &StartCommand) -> Fallibl
None None
}; };
let domain: Arc<dyn Domain> = Arc::new(LocalDomain::new("local", &config)?); let domain: Arc<dyn Domain> = Arc::new(LocalDomain::new("local")?);
let mux = Rc::new(mux::Mux::new(&config, Some(domain.clone()))); let mux = Rc::new(mux::Mux::new(Some(domain.clone())));
Mux::set_mux(&mux); Mux::set_mux(&mux);
let front_end = opts.front_end.unwrap_or(config.front_end); let front_end = opts.front_end.unwrap_or(config.front_end);
@ -583,7 +581,7 @@ fn run_terminal_gui(config: Arc<config::Config>, opts: &StartCommand) -> Fallibl
let tab = mux let tab = mux
.default_domain() .default_domain()
.spawn(PtySize::default(), cmd, window_id)?; .spawn(PtySize::default(), cmd, window_id)?;
gui.spawn_new_window(mux.config(), &fontconfig, &tab, window_id)?; gui.spawn_new_window(&fontconfig, &tab, window_id)?;
} }
for dom in mux.iter_domains() { for dom in mux.iter_domains() {
@ -658,7 +656,7 @@ fn run() -> Result<(), Error> {
SubCommand::ImageCat(cmd) => cmd.run(), SubCommand::ImageCat(cmd) => cmd.run(),
SubCommand::Cli(cli) => { SubCommand::Cli(cli) => {
let initial = true; let initial = true;
let client = Client::new_default_unix_domain(&config, initial)?; let client = Client::new_default_unix_domain(initial)?;
match cli.sub { match cli.sub {
CliSubCommand::List => { CliSubCommand::List => {
let cols = vec![ let cols = vec![

View File

@ -5,7 +5,7 @@
//! container or actually remote, running on the other end //! container or actually remote, running on the other end
//! of an ssh session somewhere. //! of an ssh session somewhere.
use crate::config::Config; use crate::config::configuration;
use crate::localtab::LocalTab; use crate::localtab::LocalTab;
use crate::mux::tab::Tab; use crate::mux::tab::Tab;
use crate::mux::window::WindowId; use crate::mux::window::WindowId;
@ -16,7 +16,6 @@ use log::info;
use portable_pty::cmdbuilder::CommandBuilder; use portable_pty::cmdbuilder::CommandBuilder;
use portable_pty::{PtySize, PtySystem}; use portable_pty::{PtySize, PtySystem};
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc;
static DOMAIN_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0); static DOMAIN_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
pub type DomainId = usize; pub type DomainId = usize;
@ -60,27 +59,20 @@ impl_downcast!(Domain);
pub struct LocalDomain { pub struct LocalDomain {
pty_system: Box<dyn PtySystem>, pty_system: Box<dyn PtySystem>,
config: Arc<Config>,
id: DomainId, id: DomainId,
name: String, name: String,
} }
impl LocalDomain { impl LocalDomain {
pub fn new(name: &str, config: &Arc<Config>) -> Result<Self, Error> { pub fn new(name: &str) -> Result<Self, Error> {
let pty_system = config.pty.get()?; let pty_system = configuration().pty.get()?;
Ok(Self::with_pty_system(name, config, pty_system)) Ok(Self::with_pty_system(name, pty_system))
} }
pub fn with_pty_system( pub fn with_pty_system(name: &str, pty_system: Box<dyn PtySystem>) -> Self {
name: &str,
config: &Arc<Config>,
pty_system: Box<dyn PtySystem>,
) -> Self {
let config = Arc::clone(config);
let id = alloc_domain_id(); let id = alloc_domain_id();
Self { Self {
pty_system, pty_system,
config,
id, id,
name: name.to_string(), name: name.to_string(),
} }
@ -94,9 +86,10 @@ impl Domain for LocalDomain {
command: Option<CommandBuilder>, command: Option<CommandBuilder>,
window: WindowId, window: WindowId,
) -> Result<Rc<dyn Tab>, Error> { ) -> Result<Rc<dyn Tab>, Error> {
let config = configuration();
let cmd = match command { let cmd = match command {
Some(c) => c, Some(c) => c,
None => self.config.build_prog(None)?, None => config.build_prog(None)?,
}; };
let pair = self.pty_system.openpty(size)?; let pair = self.pty_system.openpty(size)?;
let child = pair.slave.spawn_command(cmd)?; let child = pair.slave.spawn_command(cmd)?;
@ -107,13 +100,13 @@ impl Domain for LocalDomain {
size.cols as usize, size.cols as usize,
size.pixel_width as usize, size.pixel_width as usize,
size.pixel_height as usize, size.pixel_height as usize,
self.config.scrollback_lines.unwrap_or(3500), config.scrollback_lines.unwrap_or(3500),
self.config.hyperlink_rules.clone(), config.hyperlink_rules.clone(),
); );
let mux = Mux::get().unwrap(); let mux = Mux::get().unwrap();
if let Some(palette) = mux.config().colors.as_ref() { if let Some(palette) = config.colors.as_ref() {
*terminal.palette_mut() = palette.clone().into(); *terminal.palette_mut() = palette.clone().into();
} }

View File

@ -1,4 +1,4 @@
use crate::config::Config; use crate::config::configuration;
use crate::frontend::{executor, low_pri_executor}; use crate::frontend::{executor, low_pri_executor};
use crate::mux::tab::{Tab, TabId}; use crate::mux::tab::{Tab, TabId};
use crate::mux::window::{Window, WindowId}; use crate::mux::window::{Window, WindowId};
@ -38,18 +38,17 @@ pub type MuxSubscriber = PollableReceiver<MuxNotification>;
pub struct Mux { pub struct Mux {
tabs: RefCell<HashMap<TabId, Rc<dyn Tab>>>, tabs: RefCell<HashMap<TabId, Rc<dyn Tab>>>,
windows: RefCell<HashMap<WindowId, Window>>, windows: RefCell<HashMap<WindowId, Window>>,
config: Arc<Config>,
default_domain: RefCell<Option<Arc<dyn Domain>>>, default_domain: RefCell<Option<Arc<dyn Domain>>>,
domains: RefCell<HashMap<DomainId, Arc<dyn Domain>>>, domains: RefCell<HashMap<DomainId, Arc<dyn Domain>>>,
domains_by_name: RefCell<HashMap<String, Arc<dyn Domain>>>, domains_by_name: RefCell<HashMap<String, Arc<dyn Domain>>>,
subscribers: RefCell<HashMap<usize, PollableSender<MuxNotification>>>, subscribers: RefCell<HashMap<usize, PollableSender<MuxNotification>>>,
} }
fn read_from_tab_pty(config: Arc<Config>, tab_id: TabId, mut reader: Box<dyn std::io::Read>) { fn read_from_tab_pty(tab_id: TabId, mut reader: Box<dyn std::io::Read>) {
const BUFSIZE: usize = 32 * 1024; const BUFSIZE: usize = 32 * 1024;
let mut buf = [0; BUFSIZE]; let mut buf = [0; BUFSIZE];
let mut lim = RateLimiter::new(config.ratelimit_output_bytes_per_second); let mut lim = RateLimiter::new(configuration().ratelimit_output_bytes_per_second);
loop { loop {
match reader.read(&mut buf) { match reader.read(&mut buf) {
@ -133,7 +132,7 @@ thread_local! {
} }
impl Mux { impl Mux {
pub fn new(config: &Arc<Config>, default_domain: Option<Arc<dyn Domain>>) -> Self { pub fn new(default_domain: Option<Arc<dyn Domain>>) -> Self {
let mut domains = HashMap::new(); let mut domains = HashMap::new();
let mut domains_by_name = HashMap::new(); let mut domains_by_name = HashMap::new();
if let Some(default_domain) = default_domain.as_ref() { if let Some(default_domain) = default_domain.as_ref() {
@ -148,7 +147,6 @@ impl Mux {
Self { Self {
tabs: RefCell::new(HashMap::new()), tabs: RefCell::new(HashMap::new()),
windows: RefCell::new(HashMap::new()), windows: RefCell::new(HashMap::new()),
config: Arc::clone(config),
default_domain: RefCell::new(default_domain), default_domain: RefCell::new(default_domain),
domains_by_name: RefCell::new(domains_by_name), domains_by_name: RefCell::new(domains_by_name),
domains: RefCell::new(domains), domains: RefCell::new(domains),
@ -200,10 +198,6 @@ impl Mux {
.insert(domain.domain_name().to_string(), Arc::clone(domain)); .insert(domain.domain_name().to_string(), Arc::clone(domain));
} }
pub fn config(&self) -> &Arc<Config> {
&self.config
}
pub fn set_mux(mux: &Rc<Mux>) { pub fn set_mux(mux: &Rc<Mux>) {
MUX.with(|m| { MUX.with(|m| {
*m.borrow_mut() = Some(Rc::clone(mux)); *m.borrow_mut() = Some(Rc::clone(mux));
@ -229,8 +223,7 @@ impl Mux {
let reader = tab.reader()?; let reader = tab.reader()?;
let tab_id = tab.tab_id(); let tab_id = tab.tab_id();
let config = Arc::clone(&self.config); thread::spawn(move || read_from_tab_pty(tab_id, reader));
thread::spawn(move || read_from_tab_pty(config, tab_id, reader));
Ok(()) Ok(())
} }

View File

@ -1,4 +1,4 @@
use crate::config::{Config, SshDomain, TlsDomainClient, UnixDomain}; use crate::config::{configuration, SshDomain, TlsDomainClient, UnixDomain};
use crate::frontend::executor; use crate::frontend::executor;
use crate::mux::domain::alloc_domain_id; use crate::mux::domain::alloc_domain_id;
use crate::mux::domain::DomainId; use crate::mux::domain::DomainId;
@ -18,7 +18,6 @@ use std::collections::HashMap;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::net::TcpStream; use std::net::TcpStream;
use std::path::Path; use std::path::Path;
use std::sync::Arc;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
@ -567,17 +566,17 @@ impl Client {
self.local_domain_id self.local_domain_id
} }
pub fn new_default_unix_domain(config: &Arc<Config>, initial: bool) -> Fallible<Self> { pub fn new_default_unix_domain(initial: bool) -> Fallible<Self> {
let config = configuration();
let unix_dom = config let unix_dom = config
.unix_domains .unix_domains
.first() .first()
.ok_or_else(|| err_msg("no default unix domain is configured"))?; .ok_or_else(|| err_msg("no default unix domain is configured"))?;
Self::new_unix_domain(alloc_domain_id(), config, unix_dom, initial) Self::new_unix_domain(alloc_domain_id(), unix_dom, initial)
} }
pub fn new_unix_domain( pub fn new_unix_domain(
local_domain_id: DomainId, local_domain_id: DomainId,
_config: &Arc<Config>,
unix_dom: &UnixDomain, unix_dom: &UnixDomain,
initial: bool, initial: bool,
) -> Fallible<Self> { ) -> Fallible<Self> {
@ -587,22 +586,14 @@ impl Client {
Ok(Self::new(local_domain_id, reconnectable)) Ok(Self::new(local_domain_id, reconnectable))
} }
pub fn new_tls( pub fn new_tls(local_domain_id: DomainId, tls_client: &TlsDomainClient) -> Fallible<Self> {
local_domain_id: DomainId,
_config: &Arc<Config>,
tls_client: &TlsDomainClient,
) -> Fallible<Self> {
let mut reconnectable = let mut reconnectable =
Reconnectable::new(ClientDomainConfig::Tls(tls_client.clone()), None); Reconnectable::new(ClientDomainConfig::Tls(tls_client.clone()), None);
reconnectable.connect(true)?; reconnectable.connect(true)?;
Ok(Self::new(local_domain_id, reconnectable)) Ok(Self::new(local_domain_id, reconnectable))
} }
pub fn new_ssh( pub fn new_ssh(local_domain_id: DomainId, ssh_dom: &SshDomain) -> Fallible<Self> {
local_domain_id: DomainId,
_config: &Arc<Config>,
ssh_dom: &SshDomain,
) -> Fallible<Self> {
let mut reconnectable = Reconnectable::new(ClientDomainConfig::Ssh(ssh_dom.clone()), None); let mut reconnectable = Reconnectable::new(ClientDomainConfig::Ssh(ssh_dom.clone()), None);
reconnectable.connect(true)?; reconnectable.connect(true)?;
Ok(Self::new(local_domain_id, reconnectable)) Ok(Self::new(local_domain_id, reconnectable))

View File

@ -192,14 +192,10 @@ impl Domain for ClientDomain {
let client = match &self.config { let client = match &self.config {
ClientDomainConfig::Unix(unix) => { ClientDomainConfig::Unix(unix) => {
let initial = true; let initial = true;
Client::new_unix_domain(self.local_domain_id, mux.config(), unix, initial)? Client::new_unix_domain(self.local_domain_id, unix, initial)?
}
ClientDomainConfig::Tls(tls) => {
Client::new_tls(self.local_domain_id, mux.config(), tls)?
}
ClientDomainConfig::Ssh(ssh) => {
Client::new_ssh(self.local_domain_id, mux.config(), ssh)?
} }
ClientDomainConfig::Tls(tls) => Client::new_tls(self.local_domain_id, tls)?,
ClientDomainConfig::Ssh(ssh) => Client::new_ssh(self.local_domain_id, ssh)?,
}; };
let inner = Arc::new(ClientInner::new(self.local_domain_id, client)); let inner = Arc::new(ClientInner::new(self.local_domain_id, client));
@ -226,17 +222,14 @@ impl Domain for ClientDomain {
window.push(&tab); window.push(&tab);
} else { } else {
log::error!("spawn new local window"); log::error!("spawn new local window");
let fonts = Rc::new(FontConfiguration::new( let fonts = Rc::new(FontConfiguration::new(FontSystemSelection::get_default()));
Arc::clone(mux.config()),
FontSystemSelection::get_default(),
));
let local_window_id = mux.new_empty_window(); let local_window_id = mux.new_empty_window();
inner.record_remote_to_local_window_mapping(entry.window_id, local_window_id); inner.record_remote_to_local_window_mapping(entry.window_id, local_window_id);
mux.add_tab_to_window(&tab, local_window_id)?; mux.add_tab_to_window(&tab, local_window_id)?;
front_end() front_end()
.unwrap() .unwrap()
.spawn_new_window(mux.config(), &fonts, &tab, local_window_id) .spawn_new_window(&fonts, &tab, local_window_id)
.unwrap(); .unwrap();
} }
} }

View File

@ -1,4 +1,4 @@
use crate::config::{Config, TlsDomainServer, UnixDomain}; use crate::config::{configuration, TlsDomainServer, UnixDomain};
use crate::create_user_owned_dirs; use crate::create_user_owned_dirs;
use crate::frontend::executor; use crate::frontend::executor;
use crate::mux::tab::{Tab, TabId}; use crate::mux::tab::{Tab, TabId};
@ -200,7 +200,7 @@ mod not_ossl {
} }
} }
pub fn spawn_tls_listener(_config: &Arc<Config>, tls_server: &TlsDomainServer) -> Fallible<()> { pub fn spawn_tls_listener(tls_server: &TlsDomainServer) -> Fallible<()> {
let identity = IdentitySource::PemFiles { let identity = IdentitySource::PemFiles {
key: tls_server key: tls_server
.pem_private_key .pem_private_key
@ -328,10 +328,7 @@ mod ossl {
} }
} }
pub fn spawn_tls_listener( pub fn spawn_tls_listener(tls_server: &TlsDomainServer) -> Result<(), Error> {
_config: &Arc<Config>,
tls_server: &TlsDomainServer,
) -> Result<(), Error> {
openssl::init(); openssl::init();
let mut acceptor = SslAcceptor::mozilla_modern(SslMethod::tls())?; let mut acceptor = SslAcceptor::mozilla_modern(SslMethod::tls())?;
@ -428,9 +425,9 @@ struct ClientSurfaceState {
impl ClientSurfaceState { impl ClientSurfaceState {
fn new(cols: usize, rows: usize) -> Self { fn new(cols: usize, rows: usize) -> Self {
let mux = Mux::get().expect("to be running on gui thread"); let push_limiter = RateLimiter::new(configuration().ratelimit_mux_output_pushes_per_second);
let push_limiter = RateLimiter::new(mux.config().ratelimit_mux_output_pushes_per_second); let update_limiter =
let update_limiter = RateLimiter::new(mux.config().ratelimit_mux_output_scans_per_second); RateLimiter::new(configuration().ratelimit_mux_output_scans_per_second);
let surface = Surface::new(cols, rows); let surface = Surface::new(cols, rows);
Self { Self {
surface, surface,
@ -890,16 +887,17 @@ fn safely_create_sock_path(unix_dom: &UnixDomain) -> Result<UnixListener, Error>
} }
#[cfg(any(feature = "openssl", unix))] #[cfg(any(feature = "openssl", unix))]
fn spawn_tls_listener(config: &Arc<Config>, tls_server: &TlsDomainServer) -> Fallible<()> { fn spawn_tls_listener(tls_server: &TlsDomainServer) -> Fallible<()> {
ossl::spawn_tls_listener(config, tls_server) ossl::spawn_tls_listener(tls_server)
} }
#[cfg(not(any(feature = "openssl", unix)))] #[cfg(not(any(feature = "openssl", unix)))]
fn spawn_tls_listener(config: &Arc<Config>, tls_server: &TlsDomainServer) -> Fallible<()> { fn spawn_tls_listener(tls_server: &TlsDomainServer) -> Fallible<()> {
not_ossl::spawn_tls_listener(config, tls_server) not_ossl::spawn_tls_listener(tls_server)
} }
pub fn spawn_listener(config: &Arc<Config>) -> Fallible<()> { pub fn spawn_listener() -> Fallible<()> {
let config = configuration();
for unix_dom in &config.unix_domains { for unix_dom in &config.unix_domains {
let mut listener = LocalListener::new(safely_create_sock_path(unix_dom)?); let mut listener = LocalListener::new(safely_create_sock_path(unix_dom)?);
thread::spawn(move || { thread::spawn(move || {
@ -908,7 +906,7 @@ pub fn spawn_listener(config: &Arc<Config>) -> Fallible<()> {
} }
for tls_server in &config.tls_servers { for tls_server in &config.tls_servers {
spawn_tls_listener(config, tls_server)?; spawn_tls_listener(tls_server)?;
} }
Ok(()) Ok(())
} }

View File

@ -1,4 +1,4 @@
use crate::config::Config; use crate::config::configuration;
use crate::localtab::LocalTab; use crate::localtab::LocalTab;
use crate::mux::domain::{alloc_domain_id, Domain, DomainId, DomainState}; use crate::mux::domain::{alloc_domain_id, Domain, DomainId, DomainState};
use crate::mux::tab::Tab; use crate::mux::tab::Tab;
@ -14,7 +14,6 @@ use std::io::Write;
use std::net::TcpStream; use std::net::TcpStream;
use std::path::Path; use std::path::Path;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc;
use termwiz::cell::{unicode_column_width, AttributeChange, Intensity}; use termwiz::cell::{unicode_column_width, AttributeChange, Intensity};
use termwiz::lineedit::*; use termwiz::lineedit::*;
use termwiz::surface::Change; use termwiz::surface::Change;
@ -316,22 +315,15 @@ pub fn ssh_connect(remote_address: &str, username: &str) -> Fallible<ssh2::Sessi
pub struct RemoteSshDomain { pub struct RemoteSshDomain {
pty_system: Box<dyn PtySystem>, pty_system: Box<dyn PtySystem>,
config: Arc<Config>,
id: DomainId, id: DomainId,
name: String, name: String,
} }
impl RemoteSshDomain { impl RemoteSshDomain {
pub fn with_pty_system( pub fn with_pty_system(name: &str, pty_system: Box<dyn PtySystem>) -> Self {
name: &str,
config: &Arc<Config>,
pty_system: Box<dyn PtySystem>,
) -> Self {
let config = Arc::clone(config);
let id = alloc_domain_id(); let id = alloc_domain_id();
Self { Self {
pty_system, pty_system,
config,
id, id,
name: name.to_string(), name: name.to_string(),
} }
@ -353,18 +345,20 @@ impl Domain for RemoteSshDomain {
let child = pair.slave.spawn_command(cmd)?; let child = pair.slave.spawn_command(cmd)?;
log::info!("spawned: {:?}", child); log::info!("spawned: {:?}", child);
let config = configuration();
let mut terminal = term::Terminal::new( let mut terminal = term::Terminal::new(
size.rows as usize, size.rows as usize,
size.cols as usize, size.cols as usize,
size.pixel_width as usize, size.pixel_width as usize,
size.pixel_height as usize, size.pixel_height as usize,
self.config.scrollback_lines.unwrap_or(3500), config.scrollback_lines.unwrap_or(3500),
self.config.hyperlink_rules.clone(), config.hyperlink_rules.clone(),
); );
let mux = Mux::get().unwrap(); let mux = Mux::get().unwrap();
if let Some(palette) = mux.config().colors.as_ref() { if let Some(palette) = configuration().colors.as_ref() {
*terminal.palette_mut() = palette.clone().into(); *terminal.palette_mut() = palette.clone().into();
} }

View File

@ -432,12 +432,11 @@ pub fn run<T: Send + 'static, F: Send + 'static + Fn(TermWizTerminal) -> Fallibl
mux.add_tab_to_window(&tab, window_id)?; mux.add_tab_to_window(&tab, window_id)?;
let fontconfig = Rc::new(FontConfiguration::new( let fontconfig = Rc::new(FontConfiguration::new(
Arc::clone(mux.config()),
crate::font::FontSystemSelection::get_default(), crate::font::FontSystemSelection::get_default(),
)); ));
let gui = front_end().unwrap(); let gui = front_end().unwrap();
gui.spawn_new_window(mux.config(), &fontconfig, &tab, window_id)?; gui.spawn_new_window(&fontconfig, &tab, window_id)?;
Ok(()) Ok(())
}); });