refactor: merge the console into the live desk and shrink the desk trait to what screens differ in
This commit is contained in:
1 parent
734988247d
commit
f8f1b54d75
6 files changed
+298
-344
No files matched your search
@@ -3,23 +3,23 @@ use ratatui::{
|
||||
style::{Color, Modifier},
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
use crate::screen_settings::font_glyphs::Glyphs;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum ConsolePalette {
|
||||
#[default]
|
||||
Terminal,
|
||||
SixteenColorVt,
|
||||
EightColorVt,
|
||||
}
|
||||
|
||||
impl ConsolePalette {
|
||||
#[must_use]
|
||||
pub fn of_virtual_terminal(font_uses_high_glyphs: bool) -> Self {
|
||||
if font_uses_high_glyphs {
|
||||
Self::EightColorVt
|
||||
} else {
|
||||
Self::SixteenColorVt
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct ConsoleLook {
|
||||
pub glyphs: Glyphs,
|
||||
pub palette: ConsolePalette,
|
||||
}
|
||||
|
||||
impl ConsolePalette {
|
||||
pub fn fit_frame(self, frame: &mut Buffer) {
|
||||
if self == Self::Terminal {
|
||||
return;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::{collections::HashSet, fs, ptr};
|
||||
use std::{collections::HashSet, fs, mem, ptr};
|
||||
|
||||
use rustix::{
|
||||
io::Errno,
|
||||
@@ -8,7 +8,10 @@ use rustix::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
console_session::{ask_password, console_palette::ConsolePalette},
|
||||
console_session::{
|
||||
ask_password,
|
||||
console_palette::{ConsoleLook, ConsolePalette},
|
||||
},
|
||||
screen_settings::font_glyphs::Glyphs,
|
||||
};
|
||||
|
||||
@@ -19,89 +22,8 @@ const VIRTUAL_TERMINAL_PREFIX: &str = "/dev/tty";
|
||||
const KERNEL_PRINTK: &str = "/proc/sys/kernel/printk";
|
||||
const ALERTS_ONLY: &str = "1";
|
||||
const HIGHEST_LOW_GLYPH: u16 = 255;
|
||||
pub struct Console {
|
||||
quiet_kernel: Option<QuietKernelConsole>,
|
||||
foreground: Option<ForegroundVt>,
|
||||
for_system: bool,
|
||||
own_vt: Option<u16>,
|
||||
pub glyphs: Glyphs,
|
||||
pub palette: ConsolePalette,
|
||||
}
|
||||
|
||||
impl Console {
|
||||
pub fn take(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();
|
||||
let font = loaded_font();
|
||||
Self {
|
||||
quiet_kernel: for_system.then(QuietKernelConsole::take).flatten(),
|
||||
foreground: own_vt
|
||||
.filter(|_| !prompt_elsewhere)
|
||||
.and_then(ForegroundVt::take),
|
||||
for_system,
|
||||
own_vt,
|
||||
glyphs: font_glyphs(font.as_ref()),
|
||||
palette: console_palette(own_vt, font.as_ref()),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn answers_password_prompts(&self) -> bool {
|
||||
self.for_system && self.foreground.is_some()
|
||||
}
|
||||
|
||||
pub 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()
|
||||
}
|
||||
|
||||
pub fn keep_up(&mut self) -> bool {
|
||||
let kernel_got_loud = self
|
||||
.quiet_kernel
|
||||
.as_mut()
|
||||
.is_some_and(QuietKernelConsole::keep_quiet);
|
||||
let terminal_was_taken = self.foreground.as_ref().is_some_and(ForegroundVt::insist);
|
||||
let font = loaded_font();
|
||||
let glyphs = font_glyphs(font.as_ref());
|
||||
let palette = console_palette(self.own_vt, font.as_ref());
|
||||
let font_changed = glyphs != self.glyphs || palette != self.palette;
|
||||
self.glyphs = glyphs;
|
||||
self.palette = palette;
|
||||
kernel_got_loud || terminal_was_taken || font_changed
|
||||
}
|
||||
|
||||
pub fn begin_handover(&mut self) {
|
||||
self.set_grip(TerminalGrip::Yielding);
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn console_was_taken(&self) -> bool {
|
||||
self.foreground
|
||||
.as_ref()
|
||||
.is_some_and(ForegroundVt::taken_away)
|
||||
}
|
||||
|
||||
pub fn leave_console(&mut self) {
|
||||
self.set_grip(TerminalGrip::Surrendered);
|
||||
}
|
||||
|
||||
fn set_grip(&mut self, grip: TerminalGrip) {
|
||||
if let Some(foreground) = &mut self.foreground {
|
||||
foreground.grip = grip;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct QuietKernelConsole {
|
||||
pub(super) struct QuietKernelConsole {
|
||||
previous_level: String,
|
||||
}
|
||||
|
||||
@@ -111,13 +33,13 @@ fn console_level() -> Option<String> {
|
||||
}
|
||||
|
||||
impl QuietKernelConsole {
|
||||
fn take() -> Option<Self> {
|
||||
pub(super) fn take() -> Option<Self> {
|
||||
let previous_level = console_level()?;
|
||||
fs::write(KERNEL_PRINTK, ALERTS_ONLY).ok()?;
|
||||
Some(Self { previous_level })
|
||||
}
|
||||
|
||||
fn keep_quiet(&mut self) -> bool {
|
||||
pub(super) fn keep_quiet(&mut self) -> bool {
|
||||
let Some(level) = console_level() else {
|
||||
return false;
|
||||
};
|
||||
@@ -134,17 +56,19 @@ struct LoadedFont {
|
||||
uses_high_glyphs: bool,
|
||||
}
|
||||
|
||||
fn font_glyphs(font: Option<&LoadedFont>) -> Glyphs {
|
||||
font.map_or_else(Glyphs::default, |font| {
|
||||
pub(super) fn console_look(own_vt: Option<u16>) -> ConsoleLook {
|
||||
let font = loaded_font();
|
||||
let glyphs = font.as_ref().map_or_else(Glyphs::default, |font| {
|
||||
Glyphs::fitting(|symbol| symbol.is_ascii() || font.characters.contains(&symbol))
|
||||
})
|
||||
}
|
||||
|
||||
fn console_palette(own_vt: Option<u16>, font: Option<&LoadedFont>) -> ConsolePalette {
|
||||
if own_vt.is_none() {
|
||||
return ConsolePalette::Terminal;
|
||||
}
|
||||
ConsolePalette::of_virtual_terminal(font.is_some_and(|font| font.uses_high_glyphs))
|
||||
});
|
||||
let palette = match own_vt {
|
||||
None => ConsolePalette::Terminal,
|
||||
Some(_) if font.as_ref().is_some_and(|font| font.uses_high_glyphs) => {
|
||||
ConsolePalette::EightColorVt
|
||||
}
|
||||
Some(_) => ConsolePalette::SixteenColorVt,
|
||||
};
|
||||
ConsoleLook { glyphs, palette }
|
||||
}
|
||||
|
||||
impl Drop for QuietKernelConsole {
|
||||
@@ -204,17 +128,9 @@ struct VtState {
|
||||
_state: u16,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum TerminalGrip {
|
||||
Insisting,
|
||||
Yielding,
|
||||
Surrendered,
|
||||
}
|
||||
|
||||
struct ForegroundVt {
|
||||
pub(super) struct ForegroundVt {
|
||||
own: u16,
|
||||
previous: u16,
|
||||
grip: TerminalGrip,
|
||||
}
|
||||
|
||||
fn active_vt() -> Option<u16> {
|
||||
@@ -222,7 +138,7 @@ fn active_vt() -> Option<u16> {
|
||||
Some(state.active)
|
||||
}
|
||||
|
||||
fn vt_of_stdin() -> Option<u16> {
|
||||
pub(super) fn vt_of_stdin() -> Option<u16> {
|
||||
let name = ttyname(stdin(), Vec::new()).ok()?;
|
||||
name.to_str()
|
||||
.ok()?
|
||||
@@ -233,25 +149,25 @@ fn vt_of_stdin() -> Option<u16> {
|
||||
}
|
||||
|
||||
impl ForegroundVt {
|
||||
fn take(own: u16) -> Option<Self> {
|
||||
pub(super) fn take(own: u16) -> Option<Self> {
|
||||
let previous = active_vt()?;
|
||||
if previous != own {
|
||||
activate(own).ok()?;
|
||||
}
|
||||
Some(Self {
|
||||
own,
|
||||
previous,
|
||||
grip: TerminalGrip::Insisting,
|
||||
})
|
||||
Some(Self { own, previous })
|
||||
}
|
||||
|
||||
fn insist(&self) -> bool {
|
||||
self.grip == TerminalGrip::Insisting && self.taken_away() && activate(self.own).is_ok()
|
||||
pub(super) fn insist(&self) -> bool {
|
||||
self.taken_away() && activate(self.own).is_ok()
|
||||
}
|
||||
|
||||
fn taken_away(&self) -> bool {
|
||||
pub(super) fn taken_away(&self) -> bool {
|
||||
active_vt().is_some_and(|active| active != self.own)
|
||||
}
|
||||
|
||||
pub(super) fn stay_switched(self) {
|
||||
mem::forget(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ForegroundVt {
|
||||
@@ -260,9 +176,6 @@ impl Drop for ForegroundVt {
|
||||
eprintln!("bootvine: leaving a password prompt to the console agent on this terminal");
|
||||
return;
|
||||
}
|
||||
if self.grip == TerminalGrip::Surrendered {
|
||||
return;
|
||||
}
|
||||
let _ = activate(self.previous);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,62 +1,81 @@
|
||||
use std::{future::ready, io, mem, ops::ControlFlow, time::Duration};
|
||||
use std::{future::ready, io, ops::ControlFlow, time::Duration};
|
||||
|
||||
use crossterm::event::Event;
|
||||
use futures::{Stream, StreamExt};
|
||||
|
||||
use crate::{
|
||||
boot_model::{BootState, boot_origin::BootOrigin},
|
||||
boot_model::BootChange,
|
||||
boot_view::PasswordPrompt,
|
||||
console_session::{
|
||||
ask_password::{PasswordEntry, RequestWatch},
|
||||
console_palette::ConsolePalette,
|
||||
console_takeover::Console,
|
||||
ask_password::{self, PasswordEntry, RequestWatch},
|
||||
console_palette::ConsoleLook,
|
||||
console_takeover::{ForegroundVt, QuietKernelConsole, console_look, vt_of_stdin},
|
||||
passphrase_input::asks_to_quit,
|
||||
stream_sources::{BACKGROUND_CHECK_INTERVAL, items_when_readable, periodic_inputs},
|
||||
watch_screen::{ConsoleDesk, ScreenInput, WatchEnding},
|
||||
stream_sources::items_when_readable,
|
||||
watch_screen::{ConsoleDesk, Refresh, ScreenInput, WatchEnding},
|
||||
},
|
||||
screen_settings::font_glyphs::Glyphs,
|
||||
};
|
||||
|
||||
const FULL_REPAINT_INTERVAL: Duration = Duration::from_secs(5);
|
||||
|
||||
pub struct LiveDesk {
|
||||
console: Console,
|
||||
quiet_kernel: Option<QuietKernelConsole>,
|
||||
foreground: Option<ForegroundVt>,
|
||||
for_system: bool,
|
||||
own_vt: Option<u16>,
|
||||
look: ConsoleLook,
|
||||
entry: Option<PasswordEntry>,
|
||||
requests_watched: bool,
|
||||
clear_requested: bool,
|
||||
handover_deadline: Option<Duration>,
|
||||
}
|
||||
|
||||
impl LiveDesk {
|
||||
#[must_use]
|
||||
pub fn new(console: Console, requests_watched: bool) -> Self {
|
||||
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 {
|
||||
console,
|
||||
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,
|
||||
requests_watched,
|
||||
clear_requested: false,
|
||||
handover_deadline: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn follow_requests(&mut self, requests_changed: bool) {
|
||||
self.clear_requested |=
|
||||
requests_changed && self.console.come_forward_once_prompts_are_gone();
|
||||
self.entry = if self.console.answers_password_prompts() {
|
||||
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 glyphs(&self) -> &Glyphs {
|
||||
&self.console.glyphs
|
||||
}
|
||||
|
||||
fn palette(&self) -> ConsolePalette {
|
||||
self.console.palette
|
||||
fn look(&self) -> ConsoleLook {
|
||||
self.look
|
||||
}
|
||||
|
||||
fn prompt(&self, _now: Duration) -> Option<PasswordPrompt<'_>> {
|
||||
@@ -82,57 +101,55 @@ impl ConsoleDesk for LiveDesk {
|
||||
return;
|
||||
}
|
||||
eprintln!("bootvine: {reached} reached, handing the console over");
|
||||
self.console.begin_handover();
|
||||
self.handover_deadline = Some(deadline);
|
||||
}
|
||||
|
||||
fn frame_passed(&mut self, _state: &mut BootState, now: Duration) -> ControlFlow<WatchEnding> {
|
||||
fn frame_passed(&mut self, now: Duration) -> ControlFlow<WatchEnding, Vec<BootChange>> {
|
||||
let Some(deadline) = self.handover_deadline else {
|
||||
return ControlFlow::Continue(());
|
||||
return ControlFlow::Continue(Vec::new());
|
||||
};
|
||||
if self.console.console_was_taken() {
|
||||
if self
|
||||
.foreground
|
||||
.as_ref()
|
||||
.is_some_and(ForegroundVt::taken_away)
|
||||
{
|
||||
eprintln!("bootvine: another program took the console, leaving it there");
|
||||
self.console.leave_console();
|
||||
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(())
|
||||
ControlFlow::Continue(Vec::new())
|
||||
}
|
||||
}
|
||||
|
||||
fn password_requests_changed(&mut self) {
|
||||
self.follow_requests(true);
|
||||
fn password_requests_changed(&mut self) -> Refresh {
|
||||
self.follow_requests(true)
|
||||
}
|
||||
|
||||
fn check_prompts(&mut self) {
|
||||
self.follow_requests(!self.requests_watched);
|
||||
self.follow_requests(false);
|
||||
}
|
||||
|
||||
fn keep_console_up(&mut self) -> bool {
|
||||
self.console.keep_up()
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
fn take_clear_request(&mut self) -> bool {
|
||||
mem::take(&mut self.clear_requested)
|
||||
}
|
||||
|
||||
fn refreshed_origin(&self) -> Option<BootOrigin> {
|
||||
Some(BootOrigin::read())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prompt_check_inputs() -> impl Stream<Item = ScreenInput> {
|
||||
periodic_inputs(BACKGROUND_CHECK_INTERVAL, || ScreenInput::PromptCheck)
|
||||
}
|
||||
|
||||
pub fn console_upkeep_inputs() -> impl Stream<Item = ScreenInput> {
|
||||
periodic_inputs(BACKGROUND_CHECK_INTERVAL, || ScreenInput::ConsoleUpkeep)
|
||||
}
|
||||
|
||||
pub fn full_repaint_inputs() -> impl Stream<Item = ScreenInput> {
|
||||
periodic_inputs(FULL_REPAINT_INTERVAL, || ScreenInput::FullRepaint)
|
||||
}
|
||||
|
||||
pub fn password_request_inputs(watch: RequestWatch) -> io::Result<impl Stream<Item = ScreenInput>> {
|
||||
|
||||
@@ -7,13 +7,12 @@ use crate::{
|
||||
boot_model::{BootState, Phase, boot_origin::BootOrigin},
|
||||
console_session::{
|
||||
ask_password::RequestWatch,
|
||||
console_takeover::Console,
|
||||
live_desk::{
|
||||
LiveDesk, console_upkeep_inputs, full_repaint_inputs, password_request_inputs,
|
||||
prompt_check_inputs,
|
||||
live_desk::{LiveDesk, password_request_inputs},
|
||||
stream_sources::{
|
||||
BACKGROUND_CHECK_INTERVAL, frame_inputs, key_inputs, periodic_inputs,
|
||||
termination_inputs,
|
||||
},
|
||||
stream_sources::{frame_inputs, key_inputs, termination_inputs},
|
||||
watch_screen::{WatchEnding, WatchScreen, watch_until_ending},
|
||||
watch_screen::{ScreenInput, WatchEnding, WatchScreen, watch_until_ending},
|
||||
},
|
||||
screen_settings::{color_scheme::Colors, config_file::Timing},
|
||||
systemd_link::{
|
||||
@@ -32,15 +31,13 @@ pub struct Settings {
|
||||
pub colors: Colors,
|
||||
}
|
||||
|
||||
struct ConfiguredStatusOnExit {
|
||||
scope: Scope,
|
||||
}
|
||||
const FULL_REPAINT_INTERVAL: Duration = Duration::from_secs(5);
|
||||
|
||||
struct ConfiguredStatusOnExit;
|
||||
|
||||
impl Drop for ConfiguredStatusOnExit {
|
||||
fn drop(&mut self) {
|
||||
if self.scope == Scope::System
|
||||
&& let Ok(session) = Session::open(Scope::System)
|
||||
{
|
||||
if let Ok(session) = Session::open(Scope::System) {
|
||||
session.set_show_status(StatusOutput::Configured).ok();
|
||||
}
|
||||
}
|
||||
@@ -76,14 +73,15 @@ pub fn run(settings: &Settings) -> Result<(), Error> {
|
||||
}
|
||||
|
||||
async fn watch_live_boot(settings: &Settings, phase: Phase) -> Result<(), Error> {
|
||||
let _restores_status = ConfiguredStatusOnExit {
|
||||
scope: settings.scope,
|
||||
};
|
||||
let for_system = settings.scope == Scope::System;
|
||||
let _restores_status = for_system.then_some(ConfiguredStatusOnExit);
|
||||
let requests = RequestWatch::open();
|
||||
let desk = LiveDesk::new(
|
||||
Console::take(settings.scope == Scope::System),
|
||||
requests.is_some(),
|
||||
);
|
||||
let prompt_check: fn() -> ScreenInput = if requests.is_some() {
|
||||
|| ScreenInput::PromptCheck
|
||||
} else {
|
||||
|| ScreenInput::PasswordRequests
|
||||
};
|
||||
let desk = LiveDesk::take_console(for_system);
|
||||
let password_inputs = requests.map(password_request_inputs).transpose()?;
|
||||
let event_inputs = stream::select(
|
||||
stream::select(
|
||||
@@ -96,14 +94,20 @@ async fn watch_live_boot(settings: &Settings, phase: Phase) -> Result<(), Error>
|
||||
),
|
||||
);
|
||||
let timer_inputs = stream::select(
|
||||
stream::select(frame_inputs(), prompt_check_inputs()),
|
||||
stream::select(console_upkeep_inputs(), full_repaint_inputs()),
|
||||
stream::select(
|
||||
frame_inputs(),
|
||||
periodic_inputs(BACKGROUND_CHECK_INTERVAL, prompt_check),
|
||||
),
|
||||
stream::select(
|
||||
periodic_inputs(BACKGROUND_CHECK_INTERVAL, || ScreenInput::ConsoleUpkeep),
|
||||
periodic_inputs(FULL_REPAINT_INTERVAL, || ScreenInput::FullRepaint),
|
||||
),
|
||||
);
|
||||
let inputs = stream::select(event_inputs, timer_inputs);
|
||||
let mut screen = WatchScreen::new(
|
||||
BootState::new(phase),
|
||||
desk,
|
||||
BootOrigin::read(),
|
||||
BootOrigin::read,
|
||||
settings.colors,
|
||||
settings.timing,
|
||||
Duration::ZERO,
|
||||
|
||||
@@ -11,8 +11,8 @@ use ratatui::{
|
||||
use crate::{
|
||||
boot_model::{BootChange, BootProjection, BootState, boot_origin::BootOrigin},
|
||||
boot_view::{BootView, PasswordPrompt, project_boot},
|
||||
console_session::console_palette::ConsolePalette,
|
||||
screen_settings::{color_scheme::Colors, config_file::Timing, font_glyphs::Glyphs},
|
||||
console_session::console_palette::ConsoleLook,
|
||||
screen_settings::{color_scheme::Colors, config_file::Timing},
|
||||
systemd_link::{link_error::Error, monotonic_clock::monotonic_now},
|
||||
};
|
||||
|
||||
@@ -37,27 +37,30 @@ pub enum WatchEnding {
|
||||
Failed(Error),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum Refresh {
|
||||
Steady,
|
||||
Repaint,
|
||||
Clear,
|
||||
}
|
||||
|
||||
pub trait ConsoleDesk {
|
||||
fn glyphs(&self) -> &Glyphs;
|
||||
fn palette(&self) -> ConsolePalette;
|
||||
fn prompt(&self, now: Duration) -> Option<PasswordPrompt<'_>>;
|
||||
fn press_key(&mut self, event: &Event, now: Duration) -> ControlFlow<WatchEnding>;
|
||||
fn frame_passed(&mut self, state: &mut BootState, now: Duration) -> ControlFlow<WatchEnding>;
|
||||
fn frame_passed(&mut self, now: Duration) -> ControlFlow<WatchEnding, Vec<BootChange>>;
|
||||
|
||||
fn password_requests_changed(&mut self) {}
|
||||
fn look(&self) -> ConsoleLook {
|
||||
ConsoleLook::default()
|
||||
}
|
||||
|
||||
fn password_requests_changed(&mut self) -> Refresh {
|
||||
Refresh::Steady
|
||||
}
|
||||
|
||||
fn check_prompts(&mut self) {}
|
||||
|
||||
fn keep_console_up(&mut self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn take_clear_request(&mut self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn refreshed_origin(&self) -> Option<BootOrigin> {
|
||||
None
|
||||
fn keep_console_up(&mut self) -> Refresh {
|
||||
Refresh::Steady
|
||||
}
|
||||
|
||||
fn begin_handover(&mut self, _reached: &str, _deadline: Duration) {}
|
||||
@@ -71,8 +74,9 @@ struct CachedProjection {
|
||||
pub struct WatchScreen<Desk> {
|
||||
pub state: BootState,
|
||||
pub desk: Desk,
|
||||
read_origin: fn() -> BootOrigin,
|
||||
origin: BootOrigin,
|
||||
full_repaint_due: bool,
|
||||
refresh_due: Refresh,
|
||||
colors: Colors,
|
||||
timing: Timing,
|
||||
epoch: Duration,
|
||||
@@ -83,7 +87,7 @@ impl<Desk: ConsoleDesk> WatchScreen<Desk> {
|
||||
pub fn new(
|
||||
state: BootState,
|
||||
desk: Desk,
|
||||
origin: BootOrigin,
|
||||
read_origin: fn() -> BootOrigin,
|
||||
colors: Colors,
|
||||
timing: Timing,
|
||||
epoch: Duration,
|
||||
@@ -91,8 +95,9 @@ impl<Desk: ConsoleDesk> WatchScreen<Desk> {
|
||||
Self {
|
||||
state,
|
||||
desk,
|
||||
origin,
|
||||
full_repaint_due: false,
|
||||
read_origin,
|
||||
origin: read_origin(),
|
||||
refresh_due: Refresh::Steady,
|
||||
colors,
|
||||
timing,
|
||||
epoch,
|
||||
@@ -100,6 +105,10 @@ impl<Desk: ConsoleDesk> WatchScreen<Desk> {
|
||||
}
|
||||
}
|
||||
|
||||
fn request_refresh(&mut self, refresh: Refresh) {
|
||||
self.refresh_due = self.refresh_due.max(refresh);
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn now(&self) -> Duration {
|
||||
monotonic_now().saturating_sub(self.epoch)
|
||||
@@ -110,21 +119,29 @@ impl<Desk: ConsoleDesk> WatchScreen<Desk> {
|
||||
match input {
|
||||
ScreenInput::Boot(changes) => self.apply_boot_changes(changes, now),
|
||||
ScreenInput::Key(event) => self.desk.press_key(&event, now),
|
||||
ScreenInput::Frame => self.desk.frame_passed(&mut self.state, now),
|
||||
ScreenInput::Frame => {
|
||||
let changes = self.desk.frame_passed(now)?;
|
||||
if changes.is_empty() {
|
||||
return ControlFlow::Continue(());
|
||||
}
|
||||
self.apply_boot_changes(changes, now)
|
||||
}
|
||||
ScreenInput::PromptCheck => {
|
||||
self.desk.check_prompts();
|
||||
ControlFlow::Continue(())
|
||||
}
|
||||
ScreenInput::ConsoleUpkeep => {
|
||||
self.full_repaint_due |= self.desk.keep_console_up();
|
||||
let refresh = self.desk.keep_console_up();
|
||||
self.request_refresh(refresh);
|
||||
ControlFlow::Continue(())
|
||||
}
|
||||
ScreenInput::FullRepaint => {
|
||||
self.full_repaint_due = true;
|
||||
self.request_refresh(Refresh::Repaint);
|
||||
ControlFlow::Continue(())
|
||||
}
|
||||
ScreenInput::PasswordRequests => {
|
||||
self.desk.password_requests_changed();
|
||||
let refresh = self.desk.password_requests_changed();
|
||||
self.request_refresh(refresh);
|
||||
ControlFlow::Continue(())
|
||||
}
|
||||
ScreenInput::Terminate => ControlFlow::Break(WatchEnding::ViewerLeft),
|
||||
@@ -142,10 +159,8 @@ impl<Desk: ConsoleDesk> WatchScreen<Desk> {
|
||||
.into_iter()
|
||||
.for_each(|change| self.state.apply(change));
|
||||
self.projection = None;
|
||||
if self.state.phase != phase_before
|
||||
&& let Some(origin) = self.desk.refreshed_origin()
|
||||
{
|
||||
self.origin = origin;
|
||||
if self.state.phase != phase_before {
|
||||
self.origin = (self.read_origin)();
|
||||
}
|
||||
if self.state.display_manager_started() {
|
||||
let display_manager = self
|
||||
@@ -183,10 +198,11 @@ impl<Desk: ConsoleDesk> WatchScreen<Desk> {
|
||||
where
|
||||
Error: From<B::Error>,
|
||||
{
|
||||
if self.desk.take_clear_request() {
|
||||
if self.refresh_due == Refresh::Clear {
|
||||
terminal.clear()?;
|
||||
}
|
||||
let now = self.now();
|
||||
let look = self.desk.look();
|
||||
if self
|
||||
.projection
|
||||
.as_ref()
|
||||
@@ -208,20 +224,20 @@ impl<Desk: ConsoleDesk> WatchScreen<Desk> {
|
||||
projection,
|
||||
colors: &self.colors,
|
||||
timing: &self.timing,
|
||||
glyphs: self.desk.glyphs(),
|
||||
glyphs: &look.glyphs,
|
||||
now,
|
||||
origin: &self.origin,
|
||||
password_prompt: self.desk.prompt(now),
|
||||
},
|
||||
canvas.area(),
|
||||
);
|
||||
self.desk.palette().fit_frame(canvas.buffer_mut());
|
||||
look.palette.fit_frame(canvas.buffer_mut());
|
||||
})?;
|
||||
if self.full_repaint_due {
|
||||
if self.refresh_due == Refresh::Repaint {
|
||||
let painted = completed.buffer.clone();
|
||||
overwrite_every_cell(terminal.backend_mut(), &painted)?;
|
||||
self.full_repaint_due = false;
|
||||
}
|
||||
self.refresh_due = Refresh::Steady;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -267,19 +283,10 @@ mod tests {
|
||||
|
||||
#[derive(Default)]
|
||||
struct HandoverRecorder {
|
||||
glyphs: Glyphs,
|
||||
handovers: Vec<String>,
|
||||
}
|
||||
|
||||
impl ConsoleDesk for HandoverRecorder {
|
||||
fn glyphs(&self) -> &Glyphs {
|
||||
&self.glyphs
|
||||
}
|
||||
|
||||
fn palette(&self) -> ConsolePalette {
|
||||
ConsolePalette::Terminal
|
||||
}
|
||||
|
||||
fn prompt(&self, _now: Duration) -> Option<PasswordPrompt<'_>> {
|
||||
None
|
||||
}
|
||||
@@ -288,12 +295,8 @@ mod tests {
|
||||
ControlFlow::Continue(())
|
||||
}
|
||||
|
||||
fn frame_passed(
|
||||
&mut self,
|
||||
_state: &mut BootState,
|
||||
_now: Duration,
|
||||
) -> ControlFlow<WatchEnding> {
|
||||
ControlFlow::Continue(())
|
||||
fn frame_passed(&mut self, _now: Duration) -> ControlFlow<WatchEnding, Vec<BootChange>> {
|
||||
ControlFlow::Continue(Vec::new())
|
||||
}
|
||||
|
||||
fn begin_handover(&mut self, reached: &str, _deadline: Duration) {
|
||||
@@ -301,17 +304,20 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn screen_watching(state: BootState) -> WatchScreen<HandoverRecorder> {
|
||||
let origin = BootOrigin {
|
||||
fn test_origin() -> BootOrigin {
|
||||
BootOrigin {
|
||||
host: "test".to_owned(),
|
||||
system: None,
|
||||
generation: None,
|
||||
kernel: "Linux".to_owned(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn screen_watching(state: BootState) -> WatchScreen<HandoverRecorder> {
|
||||
WatchScreen::new(
|
||||
state,
|
||||
HandoverRecorder::default(),
|
||||
origin,
|
||||
test_origin,
|
||||
Colors::default(),
|
||||
Timing::default(),
|
||||
Duration::ZERO,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
io,
|
||||
iter::once,
|
||||
mem,
|
||||
ops::ControlFlow,
|
||||
time::Duration,
|
||||
};
|
||||
@@ -12,17 +14,16 @@ use zeroize::Zeroizing;
|
||||
|
||||
use crate::{
|
||||
boot_model::{
|
||||
ActiveState, Activity, BootState, JobKind, JobResult, Phase, UnitFacts, UnitTimeouts,
|
||||
boot_origin::BootOrigin,
|
||||
ActiveState, Activity, BootChange, BootState, JobKind, JobResult, Phase, UnitFacts,
|
||||
UnitTimeouts, boot_origin::BootOrigin,
|
||||
},
|
||||
boot_view::PasswordPrompt,
|
||||
console_session::{
|
||||
console_palette::ConsolePalette,
|
||||
passphrase_input,
|
||||
stream_sources::{frame_inputs, key_inputs},
|
||||
watch_screen::{ConsoleDesk, WatchEnding, WatchScreen, watch_until_ending},
|
||||
},
|
||||
screen_settings::{color_scheme::Colors, config_file::Timing, font_glyphs::Glyphs},
|
||||
screen_settings::{color_scheme::Colors, config_file::Timing},
|
||||
systemd_link::monotonic_clock::monotonic_now,
|
||||
};
|
||||
|
||||
@@ -178,6 +179,7 @@ impl ScriptedJob {
|
||||
}
|
||||
|
||||
struct Script {
|
||||
steps: &'static [Step],
|
||||
jobs: Vec<ScriptedJob>,
|
||||
started: HashMap<&'static str, Duration>,
|
||||
finished: HashSet<&'static str>,
|
||||
@@ -191,32 +193,39 @@ struct Answer {
|
||||
}
|
||||
|
||||
impl Script {
|
||||
fn begin(
|
||||
steps: &'static [Step],
|
||||
describe: fn(&str) -> Option<&'static str>,
|
||||
first_job: u32,
|
||||
state: &mut BootState,
|
||||
now: Duration,
|
||||
) -> Self {
|
||||
let jobs: Vec<ScriptedJob> = steps
|
||||
.iter()
|
||||
.zip(first_job..)
|
||||
.map(|(step, id)| ScriptedJob { id, step })
|
||||
.collect();
|
||||
state.set_root(steps[0].unit.to_owned());
|
||||
jobs.iter().for_each(|job| {
|
||||
state.track_unit(job.facts(steps, describe), job.initial_activity());
|
||||
});
|
||||
jobs.iter()
|
||||
.for_each(|job| state.queue_job(job.id, job.step.unit, job.kind(), now));
|
||||
fn begin(steps: &'static [Step], first_job: u32) -> Self {
|
||||
Self {
|
||||
jobs,
|
||||
steps,
|
||||
jobs: steps
|
||||
.iter()
|
||||
.zip(first_job..)
|
||||
.map(|(step, id)| ScriptedJob { id, step })
|
||||
.collect(),
|
||||
started: HashMap::new(),
|
||||
finished: HashSet::new(),
|
||||
answer: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn opening_changes(
|
||||
&self,
|
||||
describe: fn(&str) -> Option<&'static str>,
|
||||
now: Duration,
|
||||
) -> Vec<BootChange> {
|
||||
once(BootChange::SetRoot(self.steps[0].unit.to_owned()))
|
||||
.chain(self.jobs.iter().map(|job| BootChange::TrackUnit {
|
||||
facts: job.facts(self.steps, describe),
|
||||
activity: job.initial_activity(),
|
||||
}))
|
||||
.chain(self.jobs.iter().map(|job| BootChange::QueueJob {
|
||||
id: job.id,
|
||||
unit: job.step.unit.to_owned(),
|
||||
kind: job.kind(),
|
||||
at: now,
|
||||
}))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn asking(&self) -> Option<&'static Step> {
|
||||
self.jobs.iter().map(|job| job.step).find(|step| {
|
||||
step.asks.is_some()
|
||||
@@ -281,74 +290,92 @@ impl Script {
|
||||
!self.finished.contains(job.step.unit) && done_at.is_some_and(|done_at| now >= done_at)
|
||||
}
|
||||
|
||||
fn advance(&mut self, state: &mut BootState, now: Duration) {
|
||||
fn advance(&mut self, now: Duration) -> Vec<BootChange> {
|
||||
let starting: HashSet<&'static str> = self
|
||||
.jobs
|
||||
.iter()
|
||||
.filter(|job| self.is_ready(job))
|
||||
.map(|job| job.step.unit)
|
||||
.collect();
|
||||
self.jobs
|
||||
.iter()
|
||||
.filter(|job| starting.contains(job.step.unit))
|
||||
.for_each(|job| {
|
||||
self.started.insert(job.step.unit, now);
|
||||
state.update_activity(job.step.unit, job.started_activity(now));
|
||||
});
|
||||
self.started
|
||||
.extend(starting.iter().map(|unit| (*unit, now)));
|
||||
let due: HashSet<&'static str> = self
|
||||
.jobs
|
||||
.iter()
|
||||
.filter(|job| self.is_due(job, now))
|
||||
.map(|job| job.step.unit)
|
||||
.collect();
|
||||
self.jobs
|
||||
let changes = self
|
||||
.jobs
|
||||
.iter()
|
||||
.filter(|job| due.contains(job.step.unit))
|
||||
.for_each(|job| {
|
||||
state.update_activity(job.step.unit, job.settled_activity(now));
|
||||
state.finish_job(job.id, job.outcome(), now);
|
||||
});
|
||||
.filter(|job| starting.contains(job.step.unit))
|
||||
.map(|job| BootChange::UpdateActivity {
|
||||
unit: job.step.unit.to_owned(),
|
||||
activity: job.started_activity(now),
|
||||
})
|
||||
.chain(
|
||||
self.jobs
|
||||
.iter()
|
||||
.filter(|job| due.contains(job.step.unit))
|
||||
.flat_map(|job| {
|
||||
[
|
||||
BootChange::UpdateActivity {
|
||||
unit: job.step.unit.to_owned(),
|
||||
activity: job.settled_activity(now),
|
||||
},
|
||||
BootChange::FinishJob {
|
||||
id: job.id,
|
||||
result: job.outcome(),
|
||||
at: now,
|
||||
},
|
||||
]
|
||||
}),
|
||||
)
|
||||
.collect();
|
||||
self.finished.extend(due);
|
||||
changes
|
||||
}
|
||||
}
|
||||
|
||||
struct DemoDesk {
|
||||
script: Script,
|
||||
after_switch_root: Option<&'static [Step]>,
|
||||
describe: fn(&str) -> Option<&'static str>,
|
||||
next_stage: &'static [Step],
|
||||
settled_at: Option<Duration>,
|
||||
typed: Zeroizing<String>,
|
||||
glyphs: Glyphs,
|
||||
}
|
||||
|
||||
impl DemoDesk {
|
||||
fn enter_next_stage(&mut self, now: Duration) -> Vec<BootChange> {
|
||||
if !self.script.is_done() || self.next_stage.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
self.script = Script::begin(mem::take(&mut self.next_stage), 1000);
|
||||
once(BootChange::SwitchRoot { at: now })
|
||||
.chain(self.script.opening_changes(self.describe, now))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl ConsoleDesk for DemoDesk {
|
||||
fn frame_passed(&mut self, state: &mut BootState, now: Duration) -> ControlFlow<WatchEnding> {
|
||||
self.script.advance(state, now);
|
||||
if self.script.is_done()
|
||||
&& let Some(steps) = self.after_switch_root.take()
|
||||
{
|
||||
state.switch_root(now);
|
||||
self.script = Script::begin(steps, self.describe, 1000, state, now);
|
||||
}
|
||||
fn frame_passed(&mut self, now: Duration) -> ControlFlow<WatchEnding, Vec<BootChange>> {
|
||||
let changes: Vec<BootChange> = self
|
||||
.script
|
||||
.advance(now)
|
||||
.into_iter()
|
||||
.chain(self.enter_next_stage(now))
|
||||
.collect();
|
||||
if !self.script.is_done() {
|
||||
return ControlFlow::Continue(());
|
||||
return ControlFlow::Continue(changes);
|
||||
}
|
||||
let settled_at = *self.settled_at.get_or_insert(now);
|
||||
if now.saturating_sub(settled_at) >= FINAL_FRAME {
|
||||
ControlFlow::Break(WatchEnding::BootCompleted)
|
||||
} else {
|
||||
ControlFlow::Continue(())
|
||||
ControlFlow::Continue(changes)
|
||||
}
|
||||
}
|
||||
|
||||
fn glyphs(&self) -> &Glyphs {
|
||||
&self.glyphs
|
||||
}
|
||||
|
||||
fn palette(&self) -> ConsolePalette {
|
||||
ConsolePalette::Terminal
|
||||
}
|
||||
|
||||
fn prompt(&self, now: Duration) -> Option<PasswordPrompt<'_>> {
|
||||
self.script.prompt(&self.typed, now)
|
||||
}
|
||||
@@ -378,27 +405,23 @@ fn demo_origin() -> BootOrigin {
|
||||
}
|
||||
|
||||
async fn play_scenario(terminal: &mut DefaultTerminal, scenario: &Scenario) -> io::Result<()> {
|
||||
let script = Script::begin(scenario.steps, 1);
|
||||
let mut state = BootState::new(scenario.phase);
|
||||
let script = Script::begin(
|
||||
scenario.steps,
|
||||
scenario.describe,
|
||||
1,
|
||||
&mut state,
|
||||
Duration::ZERO,
|
||||
);
|
||||
script
|
||||
.opening_changes(scenario.describe, Duration::ZERO)
|
||||
.into_iter()
|
||||
.for_each(|change| state.apply(change));
|
||||
let desk = DemoDesk {
|
||||
script,
|
||||
after_switch_root: (!scenario.after_switch_root.is_empty())
|
||||
.then_some(scenario.after_switch_root),
|
||||
describe: scenario.describe,
|
||||
next_stage: scenario.after_switch_root,
|
||||
settled_at: None,
|
||||
typed: passphrase_input::empty(),
|
||||
glyphs: Glyphs::default(),
|
||||
};
|
||||
let mut screen = WatchScreen::new(
|
||||
state,
|
||||
desk,
|
||||
demo_origin(),
|
||||
demo_origin,
|
||||
Colors::default(),
|
||||
Timing::default(),
|
||||
monotonic_now(),
|
||||
@@ -430,16 +453,10 @@ mod tests {
|
||||
KEY_DERIVATION,
|
||||
)];
|
||||
|
||||
struct AskingDemo {
|
||||
script: Script,
|
||||
state: BootState,
|
||||
}
|
||||
|
||||
fn asking_demo() -> AskingDemo {
|
||||
let mut state = BootState::new(Phase::Initrd);
|
||||
let mut script = Script::begin(UNLOCK_ONLY, |_| None, 1, &mut state, Duration::ZERO);
|
||||
script.advance(&mut state, Duration::ZERO);
|
||||
AskingDemo { script, state }
|
||||
fn asking_demo() -> Script {
|
||||
let mut script = Script::begin(UNLOCK_ONLY, 1);
|
||||
script.advance(Duration::ZERO);
|
||||
script
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -449,7 +466,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn a_wrong_answer_is_checked_then_called_out() {
|
||||
let AskingDemo { mut script, .. } = asking_demo();
|
||||
let mut script = asking_demo();
|
||||
let asked = script.prompt("", Duration::ZERO).expect("asking");
|
||||
assert_eq!(asked.hint, Some(DEMO_PASSPHRASE_HINT));
|
||||
assert!(!asked.rejected);
|
||||
@@ -462,12 +479,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn the_right_answer_unlocks_and_closes_the_prompt() {
|
||||
let AskingDemo {
|
||||
mut script,
|
||||
mut state,
|
||||
} = asking_demo();
|
||||
let mut script = asking_demo();
|
||||
script.answer(DEMO_PASSPHRASE, Duration::ZERO);
|
||||
script.advance(&mut state, KEY_DERIVATION);
|
||||
script.advance(KEY_DERIVATION);
|
||||
assert!(script.is_done());
|
||||
assert!(script.prompt("", KEY_DERIVATION).is_none());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user