diff --git a/LICENSE b/LICENSE index 0f77ba544..69d008be6 100644 --- a/LICENSE +++ b/LICENSE @@ -16,6 +16,9 @@ A PARTICULAR PURPOSE. See the licenses for more details. This covers everything in the repository but QEMU, the submodule at vendor/qemu, which keeps QEMU's own licenses (its LICENSE and COPYING -files). The crates the build takes, from crates.io and from the metal2vulkan +files), and the 8 by 8 font's glyphs in +crates/reims-vgpu-window/src/native/sdl_thread/question.rs, copied from SDL +3.4.18's src/render/SDL_render_debug_font.h: Marcel Sondaar's font8_8, of +IBM's VGA font, which that file marks public domain. The crates the build takes, from crates.io and from the metal2vulkan repository, are not part of the repository, and each keeps its own license. diff --git a/crates/reims-vgpu-window/features.md b/crates/reims-vgpu-window/features.md index 89d227e71..28d82756b 100644 --- a/crates/reims-vgpu-window/features.md +++ b/crates/reims-vgpu-window/features.md @@ -652,31 +652,49 @@ runtime's own device. The switches above apply, and `resolution=` too. `manual` (a VM with two displays) - Leaving: closing a window (its close button, Alt+F4 or the window system's close, the task bar's on Windows) lets every key and button go - and ends capture, the key-ups reaching the guest first, then asks, in - SDL's message box, with macOS's words and buttons (Leaving, above), - Cancel the default for Return and Escape: on Linux zenity's question - where zenity is installed, a window of its own, else SDL's own X11 - dialog, which the window owns on X11 and under XWayland, Cancel left of - Turn Off; on Windows a task dialog the window owns, Turn Off left of - Cancel. Turn Off asks the host to end the VM; Cancel, or the question - dismissed, leaves the window and the VM running, nothing held in the - guest and the input released. From the close to the answer nothing of - the window's input reaches the guest: not a key that goes down after the - close let the keys go (F4 of Alt+F4 on Windows, which SDL hands over - after the close), nor the keys and clicks the question takes, nor their - ups. While the question is up, the library's UI thread waits for it: the - windows' frames keep drawing, and their events wait. When to ask is as on - macOS; where SDL shows no question (no zenity and no X server), the close - goes ahead as before, and the reason is logged. The window closes at the - display's release, once its swapchain is gone. + and ends capture, the key-ups reaching the guest first, then asks, with + macOS's words and buttons (Leaving, above) in ASCII, Cancel the default + for Return and Escape. On Linux the library asks in the window itself, + with nothing installed on the host and the same on Wayland and X11 and + every desktop: a dark panel centred over the frame, Cancel left of Turn + Off, in an 8 by 8 pixel font each of whose pixels is a whole square of + the screen's, a line about 12 points high at every scale, fractional + ones and 4K included; Tab and the arrows move the focus, Space answers + the focused button, a click answers the button it went down and up on, + the host's arrow shows over the window, and a minimized window is + restored and raised. Where the window draws no more (its GPU lost, or no + swapchain), the question shows in SDL's message box instead: zenity's + where zenity is installed, else SDL's own X11 dialog. On Windows a task + dialog the window owns, Turn Off left of Cancel. Turn Off asks the host + to end the VM; Cancel, or the question dismissed, leaves the window and + the VM running, nothing held in the guest and the input released. From + the close to the answer nothing of the window's input reaches the guest: + not a key that goes down after the close let the keys go (F4 of Alt+F4 + on Windows, which SDL hands over after the close), nor the keys and + clicks the question takes, nor their ups. Where no question can show, + the close keeps the VM running and logs why; the VM is then turned off + by the guest's own shutdown or the host's stop (reims-ctl stop). The one + case known to reach it is a Linux host whose window draws no more (its + GPU lost, or no swapchain) in a Wayland session without zenity, or with + no X server. While a dialog is up, the library's UI thread waits for it: + the windows' frames keep drawing, and their events wait. When to ask is + as on macOS. The window closes at the display's release, once its + swapchain is gone. `test: close::a_close_asks_only_where_the_host_would_hear_of_it`, `test: close::only_turn_off_tells_the_host`, `test: sdl::shell::a_close_asks_where_the_host_hears_and_tells_it_once_turned_off`, `test: native::sdl_thread::input::a_cancelled_close_leaves_nothing_held_and_the_input_goes_on`, `test: native::sdl_thread::input::a_key_down_after_the_close_s_lift_never_reaches_the_guest`, + `test: native::sdl_thread::question::the_question_s_words_are_macos_s_in_the_font`, + `test: native::sdl_thread::question::the_font_scales_with_the_density`, + `test: native::sdl_thread::question::return_and_escape_cancel_and_space_answers_the_focus`, + `test: native::sdl_thread::question::a_click_answers_the_button_it_went_down_and_up_on`, + `test: native::sdl_thread::render::an_overlay_is_drawn_and_told_unshown_once`, + `test: reims-vgpu::graphics::sink::the_window_s_overlay_is_drawn_over_everything_in_a_pass_of_its_own`, `test: native::sdl_thread::sdl::the_question_s_default_is_cancel`, `test: native::sdl_thread::sdl::an_answer_is_turn_off_only_by_its_button`, - `manual` (the question on Wayland, X11 and Windows; Cancel, then Turn Off) + `manual` (the question on Wayland without zenity, X11 and Windows; + Cancel, then Turn Off) - A GPU the driver reports lost, which the library makes no new device for: the window stays open and keeps the last frame it showed, and draws no more. Its swapchain makes no Vulkan call on the lost device from then on, diff --git a/crates/reims-vgpu-window/src/close.rs b/crates/reims-vgpu-window/src/close.rs index 781572f1e..b20f698ad 100644 --- a/crates/reims-vgpu-window/src/close.rs +++ b/crates/reims-vgpu-window/src/close.rs @@ -9,9 +9,9 @@ //! window lives (the display's release takes the host's callbacks back) and the host gave a close //! callback (without one a close does nothing). One question is open at a time in the process: a //! close asked for while one is open adds none. Once the person has chosen Turn Off, a later close -//! tells the host at once, as the VM is ending. Where the OS cannot show the question -//! ([`Answer::Unshown`]), the close goes ahead as it did before the question, and the OS's side -//! logs why. +//! tells the host at once, as the VM is ending. Where no question can show ([`Answer::Unshown`]), +//! the close does nothing: a close that cannot ask never turns the VM off. The VM runs on, the +//! next close asks again, and the OS's side logs why. //! //! Before the question shows, the window's input lets go as a close's does: every key and button //! held goes up and capture ends, the keys going to the guest first. A cancelled close leaves it @@ -53,7 +53,7 @@ pub(crate) enum Answer { TurnOff, /// Cancel, or the question dismissed: nothing happens. Cancel, - /// The OS could not show the question: the host is told, as before the question. + /// No question could show: nothing happens, as for Cancel, and the OS's side logs why. Unshown, } @@ -62,7 +62,7 @@ pub(crate) enum Answer { pub(crate) struct Asks { /// A question is open. open: bool, - /// The person chose Turn Off, or no question could be shown: the VM is ending. + /// The person chose Turn Off: the VM is ending. ending: bool, } @@ -85,10 +85,8 @@ impl Asks { /// The person answered the open question: whether the host is told. pub(crate) fn answered(&mut self, answer: Answer) -> bool { self.open = false; - if answer != Answer::Cancel { - self.ending = true; - } - answer != Answer::Cancel + self.ending |= answer == Answer::TurnOff; + answer == Answer::TurnOff } } @@ -109,7 +107,7 @@ pub(crate) fn answered(answer: Answer) -> bool { let said = match answer { Answer::TurnOff => "the person chose Turn Off: the host turns the VM off", Answer::Cancel => "the close is cancelled, or its window ended: the host is not told", - Answer::Unshown => "no question showed: the host turns the VM off", + Answer::Unshown => "no question showed: the host is not told, and the VM runs on", }; log::info!(target: "reims_vgpu_window", "{said}"); tell @@ -130,8 +128,8 @@ mod tests { assert_eq!(asks.request(false), Step::Nothing); } - /// Cancel tells the host nothing, and the next close asks again; Turn Off, or a question the - /// OS could not show, tells the host, and a later close tells it at once + /// Cancel, or a question that could not show, tells the host nothing, and the next close asks + /// again; Turn Off tells the host, and a later close tells it at once #[test] fn only_turn_off_tells_the_host() { let mut asks = Asks::default(); @@ -144,7 +142,7 @@ mod tests { let mut unshown = Asks::default(); assert_eq!(unshown.request(true), Step::Ask); - assert!(unshown.answered(Answer::Unshown)); - assert_eq!(unshown.request(true), Step::Tell); + assert!(!unshown.answered(Answer::Unshown)); + assert_eq!(unshown.request(true), Step::Ask); } } diff --git a/crates/reims-vgpu-window/src/handle.rs b/crates/reims-vgpu-window/src/handle.rs index 4d487188c..2901993c6 100644 --- a/crates/reims-vgpu-window/src/handle.rs +++ b/crates/reims-vgpu-window/src/handle.rs @@ -194,6 +194,17 @@ pub struct DrawnCursor { pub at: (u16, u16), } +/// An image the window draws over its frame, a pixel of it to a pixel of the drawable, clipped to +/// the drawable, wherever the frame comes from: on Linux the question a close asks +/// (`native::sdl_thread::question`). +#[derive(Clone, Debug)] +pub struct DrawnOverlay { + /// Its pixels, opaque or blended over the frame by their alpha; the hot spot is not used. + pub image: Arc, + /// Its top left, in the drawable's pixels. + pub at: (u64, u64), +} + /// What a window posts to its UI thread: `native`'s posts, or a test's. pub(crate) struct UiPosts { /// Starts the window's render thread and posts its opening. diff --git a/crates/reims-vgpu-window/src/lib.rs b/crates/reims-vgpu-window/src/lib.rs index 85e4b661c..315aa8183 100644 --- a/crates/reims-vgpu-window/src/lib.rs +++ b/crates/reims-vgpu-window/src/lib.rs @@ -26,8 +26,8 @@ //! other displays), [`scroll`] (wheel and trackpad deltas to notches or smooth scrolling) //! and [`env`](mod@env) (the `REIMS_VGPU_WINDOW_*` variables); //! - a close the person asks for, which ends the VM, and the question asked before it: [`close`] -//! (when the window asks, its words, and what an answer does), with each OS's dialog in its -//! `native` side; +//! (when the window asks, its words, and what an answer does), with each OS's question in its +//! `native` side: on Linux one the library draws in the window (`native/sdl_thread/question`); //! - the host's screens ([`screens`]), as the OS lists them; //! - the window itself: [`handle`] (the [`Window`] the display holds), and its OS side //! (`native`, one directory per OS behind one file). On macOS (`native/macos/`) that is the @@ -71,7 +71,7 @@ pub use config::{ MomentumPhase, PointerBeyond, PointerFn, PointerMotion, ScrollEvent, ScrollFn, ScrollPhase, WheelFn, WindowCallbacks, WindowConfig, }; -pub use handle::{DrawnCursor, FrameSink, Glyph, PixelRect, Window}; +pub use handle::{DrawnCursor, DrawnOverlay, FrameSink, Glyph, PixelRect, Window}; pub use present::{Drawn, Presenter, VulkanTarget}; pub use screens::HostScreen; diff --git a/crates/reims-vgpu-window/src/native/macos/alert.rs b/crates/reims-vgpu-window/src/native/macos/alert.rs index fc0c7d91e..d4c8bc436 100644 --- a/crates/reims-vgpu-window/src/native/macos/alert.rs +++ b/crates/reims-vgpu-window/src/native/macos/alert.rs @@ -29,7 +29,7 @@ pub(crate) type Answered = fn(&[u64], Answer, MainThreadMarker); /// Asks the person whether to turn the VM off for windows `ids`, in an alert as a sheet on /// `window` (the module doc): the answer is posted to the main thread, to `answered`. Where -/// AppKit refuses the sheet, that is logged, and the answer is Unshown: the close goes ahead. +/// AppKit refuses the sheet, that is logged, and the answer is Unshown: the VM runs on. pub(crate) fn ask(window: &NSWindow, ids: Vec, answered: Answered, mtm: MainThreadMarker) { let alert = msg::get("new", || NSAlert::new(mtm)); msg::get("setAlertStyle:", || { @@ -77,7 +77,7 @@ pub(crate) fn ask(window: &NSWindow, ids: Vec, answered: Answered, mtm: Mai alert.beginSheetModalForWindow_completionHandler(window, Some(&handler)); }) { log::warn!( - "the question before turning the VM off does not show ({}: {}): the close goes ahead", + "the question before turning the VM off does not show ({}: {}): the VM runs on", caught.name, caught.reason ); diff --git a/crates/reims-vgpu-window/src/native/macos/render.rs b/crates/reims-vgpu-window/src/native/macos/render.rs index f47dc7e86..e615d74c4 100644 --- a/crates/reims-vgpu-window/src/native/macos/render.rs +++ b/crates/reims-vgpu-window/src/native/macos/render.rs @@ -299,6 +299,7 @@ fn run(window: &Window) { work.layout.scale, work.layout.pixels, cursor.as_ref(), + None, ) { retry = Some(work.layout); } diff --git a/crates/reims-vgpu-window/src/native/macos/sdl.rs b/crates/reims-vgpu-window/src/native/macos/sdl.rs index 6e7889029..c8f543535 100644 --- a/crates/reims-vgpu-window/src/native/macos/sdl.rs +++ b/crates/reims-vgpu-window/src/native/macos/sdl.rs @@ -287,6 +287,15 @@ pub(crate) fn screens_changed(shell: &mut Shell) { } } +/// The window was laid out again: nothing more, as its question is a sheet ([`ask`]). +pub(crate) fn laid_out(_shell: &mut Shell) {} + +/// Whether a question is open in the window, over which the host's arrow shows: none, as its +/// question is a sheet ([`ask`]). +pub(crate) fn asks(_shell: &Shell) -> bool { + false +} + /// SDL's input events are not taken: AppKit's events are, before the application dispatches /// them (`input`). pub(crate) fn input_event(_shell: &mut Shell, _kind: u32, _event: &SDL_Event) -> bool { diff --git a/crates/reims-vgpu-window/src/native/macos/shell.rs b/crates/reims-vgpu-window/src/native/macos/shell.rs index 1a2a6a73f..42ccfad74 100644 --- a/crates/reims-vgpu-window/src/native/macos/shell.rs +++ b/crates/reims-vgpu-window/src/native/macos/shell.rs @@ -325,8 +325,8 @@ fn view_of(id: u64, mtm: MainThreadMarker) -> Option> { open.iter().find(|s| s.id == id).map(|s| s.view.clone()) } -/// The person answered the question a close asked (`super::alert`) for windows `ids`: Turn Off, -/// or a question AppKit could not show, tells each one's host, which ends the VM; Cancel tells +/// The person answered the question a close asked (`super::alert`) for windows `ids`: Turn Off +/// tells each one's host, which ends the VM; Cancel, or a question AppKit could not show, tells /// none, and the VM runs on (`crate::close::answered`). A window closed meanwhile tells nothing. pub(crate) fn answered(ids: &[u64], answer: Answer, mtm: MainThreadMarker) { if close::answered(answer) { diff --git a/crates/reims-vgpu-window/src/native/sdl_thread.rs b/crates/reims-vgpu-window/src/native/sdl_thread.rs index a0f4bfdf3..a6658625e 100644 --- a/crates/reims-vgpu-window/src/native/sdl_thread.rs +++ b/crates/reims-vgpu-window/src/native/sdl_thread.rs @@ -3,7 +3,8 @@ //! (`crate::sdl`), and what the SDL thread does its own way is here: SDL runs on the library's UI //! thread (`ui`), where the window opens, changes and ends, as `sdl` places, sizes and ends it, //! and its events become the host's input (`input`, `keymap`); each window's render thread draws -//! into its swapchain (`render`). What the OS under it adds is its own file (`super::linux`: the +//! into its swapchain (`render`), on Linux with the question a close asks over the frame +//! (`question`). What the OS under it adds is its own file (`super::linux`: the //! GPU a Wayland compositor composites on; `super::windows`). This file is what the shared window //! code asks of it: the render thread (`render`, through the display's //! [`FrameSink`](crate::FrameSink)), the window's opening, changes and end, posted to the UI @@ -11,6 +12,8 @@ mod input; mod keymap; +#[cfg(target_os = "linux")] +mod question; mod render; pub(crate) mod sdl; mod ui; diff --git a/crates/reims-vgpu-window/src/native/sdl_thread/question.rs b/crates/reims-vgpu-window/src/native/sdl_thread/question.rs new file mode 100644 index 000000000..c2e21e8df --- /dev/null +++ b/crates/reims-vgpu-window/src/native/sdl_thread/question.rs @@ -0,0 +1,735 @@ +//! The question a close asks on Linux, drawn by the library in the VM's own window +//! (`crate::close`): it needs nothing installed on the host and shows the same on every desktop, +//! Wayland's and X11's. [`Question`] lays it out for the window's drawable and pixel density +//! ([`shape`]), draws it into an image ([`draw`]) that the window's render thread draws over the +//! frame ([`DrawnOverlay`], `super::render`), and takes the window's keys and clicks while it is +//! open (`super::sdl`). +//! +//! **What it shows**: macOS's words and buttons ([`close::QUESTION`], [`close::DETAIL`], +//! [`close::TURN_OFF`], [`close::CANCEL`]), in ASCII: the title over the detail, wrapped at a +//! word to at most [`MAX_COLUMNS`] characters a line, then the buttons at the right, Cancel left +//! of Turn Off as GNOME's and KDE's dialogs place them, on a dark panel centred in the drawable. +//! Cancel, the default, is filled with the accent colour, and the button with the focus has a +//! ring, Cancel's at first. +//! +//! **The font** ([`FONT`]) is the 8 by 8 one SDL draws its debug text with, its glyphs `!` to `~` +//! copied from SDL 3.4.18's `src/render/SDL_render_debug_font.h`: Marcel Sondaar's `font8_8`, of +//! IBM's VGA font, which that file marks public domain. Each of its pixels is a square of a whole +//! number of the drawable's pixels ([`scale_for`]): about 1.5 a point of the window's, so a line is +//! about 12 points high at every scale, fractional ones included; less where the question would +//! not fit the drawable otherwise. +//! +//! **Its keys** ([`Question::key`]), by their USB usages: Return, the keypad's Enter and Escape +//! answer Cancel, the default; Space answers the button with the focus; Tab and the left and right +//! arrows move the focus. A click answers the button it went down and up on +//! ([`Question::click`]). + +use std::sync::Arc; + +use crate::close::{self, Answer}; +use crate::handle::{DrawnOverlay, Glyph}; + +/// The font's glyphs for `!` (0x21) to `~` (0x7e), 8 rows each from the top, a row's lowest bit +/// its leftmost pixel: copied from SDL 3.4.18's `src/render/SDL_render_debug_font.h`, whose font, +/// Marcel Sondaar's `font8_8` of IBM's VGA font, is in the public domain (the module doc). +#[rustfmt::skip] +const FONT: [[u8; 8]; 94] = [ + [0x18, 0x3c, 0x3c, 0x18, 0x18, 0x00, 0x18, 0x00], // '!' + [0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // '"' + [0x36, 0x36, 0x7f, 0x36, 0x7f, 0x36, 0x36, 0x00], // '#' + [0x0c, 0x3e, 0x03, 0x1e, 0x30, 0x1f, 0x0c, 0x00], // '$' + [0x00, 0x63, 0x33, 0x18, 0x0c, 0x66, 0x63, 0x00], // '%' + [0x1c, 0x36, 0x1c, 0x6e, 0x3b, 0x33, 0x6e, 0x00], // '&' + [0x06, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00], // '\'' + [0x18, 0x0c, 0x06, 0x06, 0x06, 0x0c, 0x18, 0x00], // '(' + [0x06, 0x0c, 0x18, 0x18, 0x18, 0x0c, 0x06, 0x00], // ')' + [0x00, 0x66, 0x3c, 0xff, 0x3c, 0x66, 0x00, 0x00], // '*' + [0x00, 0x0c, 0x0c, 0x3f, 0x0c, 0x0c, 0x00, 0x00], // '+' + [0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x06], // ',' + [0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00], // '-' + [0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x00], // '.' + [0x60, 0x30, 0x18, 0x0c, 0x06, 0x03, 0x01, 0x00], // '/' + [0x3e, 0x63, 0x73, 0x7b, 0x6f, 0x67, 0x3e, 0x00], // '0' + [0x0c, 0x0e, 0x0c, 0x0c, 0x0c, 0x0c, 0x3f, 0x00], // '1' + [0x1e, 0x33, 0x30, 0x1c, 0x06, 0x33, 0x3f, 0x00], // '2' + [0x1e, 0x33, 0x30, 0x1c, 0x30, 0x33, 0x1e, 0x00], // '3' + [0x38, 0x3c, 0x36, 0x33, 0x7f, 0x30, 0x78, 0x00], // '4' + [0x3f, 0x03, 0x1f, 0x30, 0x30, 0x33, 0x1e, 0x00], // '5' + [0x1c, 0x06, 0x03, 0x1f, 0x33, 0x33, 0x1e, 0x00], // '6' + [0x3f, 0x33, 0x30, 0x18, 0x0c, 0x0c, 0x0c, 0x00], // '7' + [0x1e, 0x33, 0x33, 0x1e, 0x33, 0x33, 0x1e, 0x00], // '8' + [0x1e, 0x33, 0x33, 0x3e, 0x30, 0x18, 0x0e, 0x00], // '9' + [0x00, 0x0c, 0x0c, 0x00, 0x00, 0x0c, 0x0c, 0x00], // ':' + [0x00, 0x0c, 0x0c, 0x00, 0x00, 0x0c, 0x0c, 0x06], // ';' + [0x18, 0x0c, 0x06, 0x03, 0x06, 0x0c, 0x18, 0x00], // '<' + [0x00, 0x00, 0x3f, 0x00, 0x00, 0x3f, 0x00, 0x00], // '=' + [0x06, 0x0c, 0x18, 0x30, 0x18, 0x0c, 0x06, 0x00], // '>' + [0x1e, 0x33, 0x30, 0x18, 0x0c, 0x00, 0x0c, 0x00], // '?' + [0x3e, 0x63, 0x7b, 0x7b, 0x7b, 0x03, 0x1e, 0x00], // '@' + [0x0c, 0x1e, 0x33, 0x33, 0x3f, 0x33, 0x33, 0x00], // 'A' + [0x3f, 0x66, 0x66, 0x3e, 0x66, 0x66, 0x3f, 0x00], // 'B' + [0x3c, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3c, 0x00], // 'C' + [0x1f, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1f, 0x00], // 'D' + [0x7f, 0x46, 0x16, 0x1e, 0x16, 0x46, 0x7f, 0x00], // 'E' + [0x7f, 0x46, 0x16, 0x1e, 0x16, 0x06, 0x0f, 0x00], // 'F' + [0x3c, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7c, 0x00], // 'G' + [0x33, 0x33, 0x33, 0x3f, 0x33, 0x33, 0x33, 0x00], // 'H' + [0x1e, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x00], // 'I' + [0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1e, 0x00], // 'J' + [0x67, 0x66, 0x36, 0x1e, 0x36, 0x66, 0x67, 0x00], // 'K' + [0x0f, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7f, 0x00], // 'L' + [0x63, 0x77, 0x7f, 0x7f, 0x6b, 0x63, 0x63, 0x00], // 'M' + [0x63, 0x67, 0x6f, 0x7b, 0x73, 0x63, 0x63, 0x00], // 'N' + [0x1c, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1c, 0x00], // 'O' + [0x3f, 0x66, 0x66, 0x3e, 0x06, 0x06, 0x0f, 0x00], // 'P' + [0x1e, 0x33, 0x33, 0x33, 0x3b, 0x1e, 0x38, 0x00], // 'Q' + [0x3f, 0x66, 0x66, 0x3e, 0x36, 0x66, 0x67, 0x00], // 'R' + [0x1e, 0x33, 0x07, 0x0e, 0x38, 0x33, 0x1e, 0x00], // 'S' + [0x3f, 0x2d, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x00], // 'T' + [0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3f, 0x00], // 'U' + [0x33, 0x33, 0x33, 0x33, 0x33, 0x1e, 0x0c, 0x00], // 'V' + [0x63, 0x63, 0x63, 0x6b, 0x7f, 0x77, 0x63, 0x00], // 'W' + [0x63, 0x63, 0x36, 0x1c, 0x1c, 0x36, 0x63, 0x00], // 'X' + [0x33, 0x33, 0x33, 0x1e, 0x0c, 0x0c, 0x1e, 0x00], // 'Y' + [0x7f, 0x63, 0x31, 0x18, 0x4c, 0x66, 0x7f, 0x00], // 'Z' + [0x1e, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1e, 0x00], // '[' + [0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0x40, 0x00], // '\\' + [0x1e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1e, 0x00], // ']' + [0x08, 0x1c, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00], // '^' + [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff], // '_' + [0x0c, 0x0c, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00], // '`' + [0x00, 0x00, 0x1e, 0x30, 0x3e, 0x33, 0x6e, 0x00], // 'a' + [0x07, 0x06, 0x06, 0x3e, 0x66, 0x66, 0x3b, 0x00], // 'b' + [0x00, 0x00, 0x1e, 0x33, 0x03, 0x33, 0x1e, 0x00], // 'c' + [0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6e, 0x00], // 'd' + [0x00, 0x00, 0x1e, 0x33, 0x3f, 0x03, 0x1e, 0x00], // 'e' + [0x1c, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0f, 0x00], // 'f' + [0x00, 0x00, 0x6e, 0x33, 0x33, 0x3e, 0x30, 0x1f], // 'g' + [0x07, 0x06, 0x36, 0x6e, 0x66, 0x66, 0x67, 0x00], // 'h' + [0x0c, 0x00, 0x0e, 0x0c, 0x0c, 0x0c, 0x1e, 0x00], // 'i' + [0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1e], // 'j' + [0x07, 0x06, 0x66, 0x36, 0x1e, 0x36, 0x67, 0x00], // 'k' + [0x0e, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x00], // 'l' + [0x00, 0x00, 0x33, 0x7f, 0x7f, 0x6b, 0x63, 0x00], // 'm' + [0x00, 0x00, 0x1f, 0x33, 0x33, 0x33, 0x33, 0x00], // 'n' + [0x00, 0x00, 0x1e, 0x33, 0x33, 0x33, 0x1e, 0x00], // 'o' + [0x00, 0x00, 0x3b, 0x66, 0x66, 0x3e, 0x06, 0x0f], // 'p' + [0x00, 0x00, 0x6e, 0x33, 0x33, 0x3e, 0x30, 0x78], // 'q' + [0x00, 0x00, 0x3b, 0x6e, 0x66, 0x06, 0x0f, 0x00], // 'r' + [0x00, 0x00, 0x3e, 0x03, 0x1e, 0x30, 0x1f, 0x00], // 's' + [0x08, 0x0c, 0x3e, 0x0c, 0x0c, 0x2c, 0x18, 0x00], // 't' + [0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6e, 0x00], // 'u' + [0x00, 0x00, 0x33, 0x33, 0x33, 0x1e, 0x0c, 0x00], // 'v' + [0x00, 0x00, 0x63, 0x6b, 0x7f, 0x7f, 0x36, 0x00], // 'w' + [0x00, 0x00, 0x63, 0x36, 0x1c, 0x36, 0x63, 0x00], // 'x' + [0x00, 0x00, 0x33, 0x33, 0x33, 0x3e, 0x30, 0x1f], // 'y' + [0x00, 0x00, 0x3f, 0x19, 0x0c, 0x26, 0x3f, 0x00], // 'z' + [0x38, 0x0c, 0x0c, 0x07, 0x0c, 0x0c, 0x38, 0x00], // '{' + [0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00], // '|' + [0x07, 0x0c, 0x0c, 0x38, 0x0c, 0x0c, 0x07, 0x00], // '}' + [0x6e, 0x3b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // '~' +]; + +/// The longest a line of the detail is, in characters, before it wraps at a word. +const MAX_COLUMNS: u32 = 40; +/// A glyph's side, in the font's pixels. +const CELL: u32 = 8; +/// The panel's margin around what it shows, in the font's pixels. +const PAD: u32 = 16; +/// The room between the title and the detail, in the font's pixels. +const TITLE_GAP: u32 = 12; +/// The room between the detail's lines, in the font's pixels. +const LEADING: u32 = 4; +/// The room between the detail and the buttons, in the font's pixels. +const BUTTONS_GAP: u32 = 16; +/// A button's height, then its width, in the font's pixels: room around its label, the longer +/// one (Turn Off) setting both buttons' width. +const BUTTON: (u32, u32) = (24, 96); +/// The room between the buttons, in the font's pixels. +const BUTTON_GAP: u32 = 8; +/// The fewest characters a line of the detail holds: the buttons' width. +const MIN_COLUMNS: u32 = (2 * BUTTON.1 + BUTTON_GAP) / CELL; +/// The most drawable pixels a font pixel takes. +const MAX_SCALE: u32 = 16; + +/// The panel's colour, RGBA. +const PANEL: [u8; 4] = [0x2b, 0x2b, 0x2b, 0xff]; +/// The panel's edge. +const EDGE: [u8; 4] = [0x6b, 0x6b, 0x6b, 0xff]; +/// The title's and the buttons' labels. +const BRIGHT: [u8; 4] = [0xff, 0xff, 0xff, 0xff]; +/// The detail's. +const TEXT: [u8; 4] = [0xd4, 0xd4, 0xd4, 0xff]; +/// The default button, Cancel. +const ACCENT: [u8; 4] = [0x35, 0x74, 0xf0, 0xff]; +/// The other button. +const PLAIN: [u8; 4] = [0x4a, 0x4a, 0x4a, 0xff]; +/// The ring around the button with the focus. +const RING: [u8; 4] = [0xa8, 0xc8, 0xff, 0xff]; + +/// Return, the keypad's Enter, Escape, Tab, Space, and the right and left arrows: USB usages. +const RETURN: u16 = 0x28; +/// The keypad's Enter. +const KEYPAD_ENTER: u16 = 0x58; +/// Escape. +const ESCAPE: u16 = 0x29; +/// Tab. +const TAB: u16 = 0x2B; +/// Space. +const SPACE: u16 = 0x2C; +/// The right arrow. +const RIGHT: u16 = 0x4F; +/// The left arrow. +const LEFT: u16 = 0x50; + +/// One of the question's buttons. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum Button { + /// Cancel, the default. + Cancel, + /// Turn Off. + TurnOff, +} + +/// The buttons, left to right. +const BUTTONS: [Button; 2] = [Button::Cancel, Button::TurnOff]; + +impl Button { + /// What choosing it answers. + fn answer(self) -> Answer { + match self { + Button::Cancel => Answer::Cancel, + Button::TurnOff => Answer::TurnOff, + } + } + + /// Its label. + fn label(self) -> &'static str { + match self { + Button::Cancel => close::CANCEL, + Button::TurnOff => close::TURN_OFF, + } + } +} + +/// The question laid out for a drawable ([`shape`]): its sizes in the font's pixels, each `scale` +/// drawable pixels on a side. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(super) struct Shape { + /// The drawable's pixels on a side of a font pixel. + pub(super) scale: u32, + /// The detail's lines. + lines: Vec, + /// The panel's width and height, in font pixels. + size: (u32, u32), + /// The panel's top left in the drawable, in its pixels. + at: (u64, u64), +} + +impl Shape { + /// The panel's width and height in the drawable's pixels. + fn pixels(&self) -> (u32, u32) { + (self.size.0 * self.scale, self.size.1 * self.scale) + } + + /// Where `button` is in the panel, in font pixels: its left, top, width and height. + fn button(&self, button: Button) -> (u32, u32, u32, u32) { + let top = self.size.1 - PAD - BUTTON.0; + let right = self.size.0 - PAD; + let left = match button { + Button::TurnOff => right - BUTTON.1, + Button::Cancel => right - 2 * BUTTON.1 - BUTTON_GAP, + }; + (left, top, BUTTON.1, BUTTON.0) + } + + /// The top of the detail's first line, in font pixels. + fn detail_top() -> u32 { + PAD + CELL + TITLE_GAP + } + + /// The button under `point`, in the drawable's pixels. + fn button_at(&self, point: (f64, f64)) -> Option