use std::{future::ready, io, ops::ControlFlow, time::Duration}; use crossterm::event::Event; use futures::{Stream, StreamExt}; use crate::{ boot_model::BootChange, boot_view::PasswordPrompt, console_session::{ ask_password::{self, PasswordEntry, RequestWatch}, console_palette::ConsoleLook, console_takeover::{ForegroundVt, QuietKernelConsole, console_look, vt_of_stdin}, passphrase_input::asks_to_quit, stream_sources::items_when_readable, watch_screen::{ConsoleDesk, Refresh, ScreenInput, WatchEnding}, }, }; pub struct LiveDesk { quiet_kernel: Option, foreground: Option, for_system: bool, own_vt: Option, look: ConsoleLook, entry: Option, handover_deadline: Option, } impl LiveDesk { #[must_use] pub fn take_console(for_system: bool) -> Self { let prompt_elsewhere = for_system && ask_password::requested(); if prompt_elsewhere { eprintln!("bootvine: a password prompt is already showing, staying behind it"); } let own_vt = vt_of_stdin(); Self { quiet_kernel: for_system.then(QuietKernelConsole::take).flatten(), foreground: own_vt .filter(|_| !prompt_elsewhere) .and_then(ForegroundVt::take), for_system, own_vt, look: console_look(own_vt), entry: None, handover_deadline: None, } } fn come_forward_once_prompts_are_gone(&mut self) -> bool { if !self.for_system || self.foreground.is_some() || ask_password::requested() { return false; } self.foreground = self.own_vt.and_then(ForegroundVt::take); if self.foreground.is_some() { eprintln!("bootvine: password prompt answered, taking the console back"); } self.foreground.is_some() } fn follow_requests(&mut self, requests_changed: bool) -> Refresh { let came_forward = requests_changed && self.come_forward_once_prompts_are_gone(); self.entry = if self.for_system && self.foreground.is_some() { PasswordEntry::follow(self.entry.take(), requests_changed) } else { None }; if came_forward { Refresh::Clear } else { Refresh::Steady } } } impl ConsoleDesk for LiveDesk { fn look(&self) -> ConsoleLook { self.look } fn prompt(&self, _now: Duration) -> Option> { self.entry.as_ref().map(PasswordEntry::prompt) } fn press_key(&mut self, event: &Event, _now: Duration) -> ControlFlow { let Some(entry) = &mut self.entry else { return if asks_to_quit(event, false) { ControlFlow::Break(WatchEnding::ViewerLeft) } else { ControlFlow::Continue(()) }; }; if let Err(error) = entry.type_key(event) { eprintln!("bootvine: cannot answer the password prompt: {error}"); } ControlFlow::Continue(()) } fn begin_handover(&mut self, reached: &str, deadline: Duration) { if self.handover_deadline.is_some() { return; } eprintln!("bootvine: {reached} reached, handing the console over"); self.handover_deadline = Some(deadline); } fn frame_passed(&mut self, now: Duration) -> ControlFlow> { let Some(deadline) = self.handover_deadline else { return ControlFlow::Continue(Vec::new()); }; if self .foreground .as_ref() .is_some_and(ForegroundVt::taken_away) { eprintln!("bootvine: another program took the console, leaving it there"); if let Some(foreground) = self.foreground.take() { foreground.stay_switched(); } ControlFlow::Break(WatchEnding::BootCompleted) } else if now >= deadline { eprintln!("bootvine: nothing took the console, switching back"); ControlFlow::Break(WatchEnding::BootCompleted) } else { ControlFlow::Continue(Vec::new()) } } fn password_requests_changed(&mut self) -> Refresh { self.follow_requests(true) } fn check_prompts(&mut self) { self.follow_requests(false); } fn keep_console_up(&mut self) -> Refresh { let kernel_got_loud = self .quiet_kernel .as_mut() .is_some_and(QuietKernelConsole::keep_quiet); let terminal_was_taken = self.handover_deadline.is_none() && self.foreground.as_ref().is_some_and(ForegroundVt::insist); let look = console_look(self.own_vt); let restyled = look != self.look; self.look = look; if kernel_got_loud || terminal_was_taken || restyled { Refresh::Repaint } else { Refresh::Steady } } } pub fn password_request_inputs(watch: RequestWatch) -> io::Result> { Ok(items_when_readable(watch, RequestWatch::changed)? .filter(|changed| ready(*changed)) .map(|_| ScreenInput::PasswordRequests)) }