sector-f-linux

Sector F Linux
git clone https://git.sectorflabs.com/sector-f-linux.git
Log | Files | Refs | README | LICENSE

main.rs (4700B)


      1 //! patina-greeter: Sector F Linux's login screen, with the first-run setup in it (ADR-0012). greetd
      2 //! runs it as the greeter user inside a kiosk compositor (`cage`); it shows the login, or, on a
      3 //! machine with no account, the setup that makes the first one.
      4 
      5 mod app;
      6 mod backend;
      7 mod field;
      8 mod model;
      9 
     10 use std::sync::Arc;
     11 
     12 use backend::{Backend, Mock};
     13 mod system;
     14 
     15 const USAGE: &str = "usage: patina-greeter
     16 
     17 The login screen. Run by greetd inside a kiosk compositor: `cage -s -- patina-greeter`.
     18 
     19 For trying the screens without greetd, on a mock whose password is `sectorf`:
     20   PATINA_GREETER_MOCK=login   a machine with accounts (alice and bob)
     21   PATINA_GREETER_MOCK=setup   a machine fresh from the install, with none
     22   PATINA_GREETER_SIZE=1280x800  a window of that size, not full screen
     23   PATINA_GREETER_DEMO=...       fills a screen in: login: typing, wrong, problem; setup: wifi, wifi-problem, clock, name, name-problem, filled, problem
     24   PATINA_GREETER_SCREENSHOT=file.png  saves the screen to a file and quits
     25   PATINA_GREETER_PROBE=status|zone:Z|name:N|wifi:SSID:PW  no window: asks the real system (for trying it)
     26   PATINA_SKIN=name              another skin";
     27 
     28 fn main() -> eframe::Result {
     29     match std::env::args().nth(1).as_deref() {
     30         None => {}
     31         Some("-h" | "--help") => {
     32             println!("{USAGE}");
     33             return Ok(());
     34         }
     35         Some("-V" | "--version") => {
     36             println!("patina-greeter {}", env!("CARGO_PKG_VERSION"));
     37             return Ok(());
     38         }
     39         Some(_) => {
     40             eprintln!("{USAGE}");
     41             std::process::exit(2);
     42         }
     43     }
     44 
     45     // No window: what the real system answers, for trying the setup's calls on a machine or a VM.
     46     if let Ok(what) = std::env::var("PATINA_GREETER_PROBE") {
     47         return probe(&system::System::new(), &what);
     48     }
     49 
     50     let backend: Arc<dyn Backend> = match std::env::var("PATINA_GREETER_MOCK").as_deref() {
     51         Ok("setup") => Arc::new(Mock::first_run()),
     52         Ok(_) => Arc::new(Mock::with_users()),
     53         Err(_) => Arc::new(system::System::new()),
     54     };
     55 
     56     let mut viewport = egui::ViewportBuilder::default()
     57         .with_title("Sector F Linux")
     58         .with_app_id("com.sectorflabs.Greeter")
     59         .with_decorations(false);
     60     viewport = match window_size() {
     61         Some(size) => viewport.with_inner_size(size),
     62         None => viewport.with_fullscreen(true),
     63     };
     64     let options = eframe::NativeOptions {
     65         viewport,
     66         ..Default::default()
     67     };
     68     eframe::run_native(
     69         "patina-greeter",
     70         options,
     71         Box::new(move |cc| {
     72             let mut app = app::App::new(cc, backend);
     73             if let Ok(what) = std::env::var("PATINA_GREETER_DEMO") {
     74                 app.demo(&what);
     75             }
     76             if let Ok(path) = std::env::var("PATINA_GREETER_SCREENSHOT") {
     77                 app.screenshot_to(path.into());
     78             }
     79             Ok(Box::new(app))
     80         }),
     81     )
     82 }
     83 
     84 /// `PATINA_GREETER_SIZE=1280x800`, for a window on a desktop while developing.
     85 /// `PATINA_GREETER_PROBE=...`: `status` prints what the real system reports; `zone:Europe/Stockholm`,
     86 /// `name:alices-laptop` and `wifi:SSID:PASSWORD` make the change the setup would make, as whoever runs
     87 /// this (so polkit's rules for the greeter user can be tried with `runuser -u greetd`).
     88 fn probe(system: &system::System, what: &str) -> eframe::Result {
     89     match what.split_once(':') {
     90         None => {
     91             println!("wifi available: {}", system.wifi_available());
     92             println!("networks: {:?}", system.wifi_networks());
     93             let zones = system.timezones();
     94             println!(
     95                 "time zones: {} (current {})",
     96                 zones.len(),
     97                 system.timezone()
     98             );
     99             println!("host name: {}", system.hostname());
    100             println!("first run: {}", system.first_run());
    101         }
    102         Some(("zone", zone)) => println!("set zone: {:?}", system.set_timezone(zone)),
    103         Some(("name", name)) => println!("set name: {:?}", system.set_hostname(name)),
    104         Some(("wifi", rest)) => {
    105             let (ssid, password) = rest.split_once(':').unwrap_or((rest, ""));
    106             println!("join {ssid}: {:?}", system.wifi_connect(ssid, password));
    107         }
    108         Some((other, _)) => eprintln!("unknown probe {other}"),
    109     }
    110     Ok(())
    111 }
    112 
    113 fn window_size() -> Option<[f32; 2]> {
    114     let text = std::env::var("PATINA_GREETER_SIZE").ok()?;
    115     let (w, h) = text.split_once('x')?;
    116     Some([w.parse().ok()?, h.parse().ok()?])
    117 }
    118 
    119 impl eframe::App for app::App {
    120     fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
    121         self.draw(ui);
    122     }
    123 }