Nacre/src/main.rs

157 lines
5.8 KiB
Rust

//! Nacre: a web browser on the Servo engine.
mod actions;
mod adblock;
mod app;
mod discord;
mod hub;
mod importer;
mod inject;
mod instance;
mod keys;
mod media;
mod net;
mod paths;
mod pdf;
mod perf;
mod resources;
mod selftest;
mod spell;
mod store;
mod tabs;
mod theme;
mod ui;
mod updates;
mod util;
mod window;
use winit::event_loop::EventLoop;
use crate::app::{App, AppEvent, Launch};
use crate::paths::Paths;
const HELP: &str = "Nacre, a web browser
Usage: nacre [options] [address...]
--profile NAME use (or create) the profile NAME
--incognito open an incognito window: nothing is saved
--new-window open the addresses in a new window
--app ID open an installed web app
--version print the version
--help print this help
Environment: NACRE_HOME=DIR keeps every file under DIR; NACRE_NO_BRIDGE=1 turns the
network bridge (VPN, secure DNS) off; LIBGL_ALWAYS_SOFTWARE=1 forces software rendering.";
fn parse_args() -> Result<Launch, String> {
let mut launch = Launch::default();
let mut args = std::env::args().skip(1);
while let Some(arg) = args.next() {
match arg.as_str() {
"--help" | "-h" => {
println!("{HELP}");
std::process::exit(0);
},
"--version" | "-V" => {
println!("Nacre {}", env!("CARGO_PKG_VERSION"));
std::process::exit(0);
},
"--incognito" | "--private" | "--private-window" => launch.incognito = true,
"--new-window" => launch.new_window = true,
"--profile" => launch.profile = Some(args.next().ok_or("--profile needs a name")?),
"--app" => launch.app = Some(args.next().ok_or("--app needs an app id")?),
other if other.starts_with("--profile=") => launch.profile = Some(other["--profile=".len()..].to_owned()),
other if other.starts_with("--app=") => launch.app = Some(other["--app=".len()..].to_owned()),
other if other.starts_with("--") => return Err(format!("unknown option {other} (see --help)")),
other => {
// Paths given on the command line open as files.
let path = std::path::Path::new(other);
let url = if !other.contains("://") && path.exists() {
std::fs::canonicalize(path).ok().and_then(|p| url::Url::from_file_path(p).ok()).map(|u| u.to_string()).unwrap_or_else(|| other.to_owned())
} else {
other.to_owned()
};
launch.urls.push(url);
},
}
}
Ok(launch)
}
/// Software OpenGL when hardware acceleration is off, or in a virtual machine on "Auto".
fn choose_gl(settings: &store::settings::Settings) {
use store::settings::AutoOnOff;
if std::env::var_os("LIBGL_ALWAYS_SOFTWARE").is_some() {
return;
}
let software = match settings.web.gpu {
AutoOnOff::On => false,
AutoOnOff::Off => true,
AutoOnOff::Auto => {
let product = std::fs::read_to_string("/sys/class/dmi/id/product_name").unwrap_or_default().to_lowercase();
["virtualbox", "vmware", "qemu", "kvm", "bochs"].iter().any(|vm| product.contains(vm))
},
};
if software {
// SAFETY: called on the main thread before any other thread exists.
unsafe { std::env::set_var("LIBGL_ALWAYS_SOFTWARE", "1") };
}
}
/// Points GStreamer at Nacre's patched MSE plugin (`msesrc`, docs/engine/gst-mse-patches.md) ahead of the
/// system one. The matching library, libgstmse-1.0.so.0, is found through the binary's RUNPATH (build.rs).
/// Release layout: `<exe dir>/lib/gstreamer-1.0`; a dev build: `target/gst-mse/gstreamer-1.0` (tools/gst-mse.sh).
fn use_own_gst_mse() {
let Some(dir) = std::env::current_exe().ok().and_then(|exe| exe.parent().map(|dir| dir.to_path_buf())) else {
return;
};
let Some(plugins) = [dir.join("lib/gstreamer-1.0"), dir.join("../gst-mse/gstreamer-1.0")]
.into_iter()
.find(|plugins| plugins.join(if cfg!(windows) { "gstmse.dll" } else { "libgstmse.so" }).is_file())
else {
eprintln!("nacre: gst-mse: no patched mse plugin next to the binary, using the system one");
return;
};
let mut paths = vec![plugins.clone()];
paths.extend(std::env::var_os("GST_PLUGIN_PATH").iter().flat_map(std::env::split_paths));
if let Ok(value) = std::env::join_paths(paths) {
// SAFETY: called on the main thread before any other thread exists.
unsafe { std::env::set_var("GST_PLUGIN_PATH", value) };
}
let library = std::fs::read_to_string("/proc/self/maps")
.unwrap_or_default()
.lines()
.find_map(|line| line.split_whitespace().nth(5).filter(|path| path.contains("libgstmse-1.0")).map(str::to_owned))
.unwrap_or_else(|| "not loaded".into());
eprintln!("nacre: gst-mse: plugin {}, library {library}", plugins.display());
}
fn main() {
let launch = match parse_args() {
Ok(launch) => launch,
Err(error) => {
eprintln!("nacre: {error}");
std::process::exit(2);
},
};
let base = Paths::for_profile(launch.profile.as_deref());
let paths = if launch.incognito { Paths::incognito(&base) } else { base };
if !paths.incognito && instance::hand_off(&paths, &launch) {
return;
}
choose_gl(&store::settings::Settings::load(&paths.settings()));
use_own_gst_mse();
let event_loop = match EventLoop::<AppEvent>::with_user_event().build() {
Ok(event_loop) => event_loop,
Err(error) => {
eprintln!("nacre: can't connect to the display: {error}");
std::process::exit(1);
},
};
let mut app = App::new(event_loop.create_proxy(), paths, launch);
if let Err(error) = event_loop.run_app(&mut app) {
eprintln!("nacre: {error}");
}
app.quit();
}