Nacre/src/main.rs

141 lines
4.9 KiB
Rust

//! Nacre: a web browser on WebKitGTK, with its interface drawn by egui.
mod actions;
mod adblock;
mod app;
mod discord;
mod egui_area;
mod engine;
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 gtk::prelude::*;
use crate::app::{App, 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") };
}
}
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()));
// Nacre has its own single-instance handling (instance.rs), so GTK's is off.
let gtk_app = gtk::Application::builder().application_id("dev.nacre.Nacre").flags(gtk::gio::ApplicationFlags::NON_UNIQUE).build();
let state = std::cell::RefCell::new(Some((paths, launch)));
let keep: std::rc::Rc<std::cell::RefCell<Option<std::rc::Rc<std::cell::RefCell<App>>>>> = Default::default();
let held = keep.clone();
gtk_app.connect_activate(move |gtk_app| {
let Some((paths, launch)) = state.borrow_mut().take() else { return };
let app = App::new(gtk_app.clone(), paths, launch);
app.borrow_mut().open_initial();
// Ticks: timers, session saves, self-test steps.
glib_tick();
*held.borrow_mut() = Some(app);
});
gtk_app.run_with_args::<&str>(&[]);
if let Some(app) = keep.borrow_mut().take() {
app.borrow_mut().quit();
}
}
fn glib_tick() {
gtk::glib::timeout_add_local(std::time::Duration::from_millis(100), || {
app::pump();
gtk::glib::ControlFlow::Continue
});
}