From ebb7e8dfae62b052c6848a607ec834aedc6b8983 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Dec 2025 18:58:33 +0800 Subject: [PATCH] refactor QR code notification implementation Previous implementation sent the QR code as draw commands. This is inefficient as it would require hundreds of draw commands to be sent per QR code rendered. This implementation codes the QR code as a bool and sends it in a single memory message, and implements a QR code renderer on the receiver side. The QR code rendering is also cleaned up so that the QR code is always centered and takes the full screen, to give the best conditions for someone scanning the code from another device. --- libs/ux-api/src/minigfx/handlers.rs | 66 +++++++++++++++++++++++++ libs/ux-api/src/service/api.rs | 8 +++ libs/ux-api/src/service/gfx.rs | 14 ++++++ libs/ux-api/src/widgets/notification.rs | 25 ++++++---- services/modals/src/main.rs | 2 +- 5 files changed, 104 insertions(+), 11 deletions(-) diff --git a/libs/ux-api/src/minigfx/handlers.rs b/libs/ux-api/src/minigfx/handlers.rs index 6e22798c0..c8c3d6845 100644 --- a/libs/ux-api/src/minigfx/handlers.rs +++ b/libs/ux-api/src/minigfx/handlers.rs @@ -7,6 +7,7 @@ use xous_ipc::Buffer; use super::FrameBuffer; use super::*; +use crate::service::api::QrRender; use crate::wordwrap::*; pub fn draw_clip_object(display: &mut T, msg: &mut xous::envelope::Envelope) { @@ -400,3 +401,68 @@ pub fn draw_text_view(display: &mut T, msg: &mut xous::envelope: // pack our data back into the buffer to return buffer.replace(tv).unwrap(); } + +pub fn render_qr( + display: &mut T, + screen_clip: Option, + msg: &mut xous::envelope::Envelope, +) { + if msg.body.has_memory() { + let screensize = Rectangle::new( + Point::new(0, 0), + Point::new(crate::platform::WIDTH as _, crate::platform::HEIGHT as _), + ); + let buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; + let qr_render = buffer.to_original::().unwrap(); + log::debug!( + "qr_render width {}: {:?}", + qr_render.width, + &qr_render.modules[..32.min(qr_render.modules.len())] + ); + const QUIET_MODULES: isize = 1; + let window_width = screen_clip.unwrap_or(screensize).width() as isize; + let total_width = qr_render.width as isize + QUIET_MODULES * 2; + let module_width = (window_width / total_width).max(1) as isize; + let left_edge = if module_width * total_width < window_width { + (window_width - (module_width * total_width)) as isize / 2 + } else { + 0 + }; + + const USE_TOP_LEFT: bool = false; + let tl = if USE_TOP_LEFT { qr_render.top_left } else { Point::new(0, 0) }; + + // draw a white background over the rendering area + let r = Rectangle::new_with_style( + Point::from((left_edge, tl.y)), + screen_clip.unwrap_or(screensize).br, + DrawStyle::new(PixelColor::Light, PixelColor::Light, 1), + ); + op::rectangle(display, r, screen_clip.into(), false); + + // now raster in the modules + let mut line_top_left = + Point::new(left_edge + module_width * QUIET_MODULES, tl.y + module_width * QUIET_MODULES); + let dark = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1); + for line in qr_render.modules.chunks(qr_render.width) { + for (i, &module) in line.iter().enumerate() { + if module { + // dark color: render a square + let m = Rectangle::new_coords_with_style( + line_top_left.x + i as isize * module_width, + line_top_left.y, + line_top_left.x + (i + 1) as isize * module_width, + line_top_left.y + module_width, + dark, + ); + op::rectangle(display, m, screen_clip.into(), false); + } else { + // light color: don't render, background is already light + } + } + line_top_left += Point::new(0, module_width); + } + } else { + panic!("Incorrect message type for RenderQr") + } +} diff --git a/libs/ux-api/src/service/api.rs b/libs/ux-api/src/service/api.rs index 209fe6478..ff15d7007 100644 --- a/libs/ux-api/src/service/api.rs +++ b/libs/ux-api/src/service/api.rs @@ -98,6 +98,7 @@ pub enum GfxOpcode { UnclippedObjectList, /// V2 API for getting filtered keyboard events FilteredKeyboardListener, + RenderQr, #[cfg(feature = "board-baosec")] AcquireQr, @@ -137,6 +138,13 @@ pub struct QrAcquisition { pub meta: Option, } +#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub struct QrRender { + pub width: usize, + pub top_left: crate::minigfx::Point, + pub modules: Vec, +} + // this structure is used to register a keyboard listener. #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone)] pub struct KeyboardRegistration { diff --git a/libs/ux-api/src/service/gfx.rs b/libs/ux-api/src/service/gfx.rs index 00c9b7d26..f078565b7 100644 --- a/libs/ux-api/src/service/gfx.rs +++ b/libs/ux-api/src/service/gfx.rs @@ -729,6 +729,20 @@ impl Gfx { buf.lend(self.conn, GfxOpcode::UnclippedObjectList.to_u32().unwrap()).map(|_| ()) } + /// On small screens, `top_left` is ignored and the code takes the whole screen, always + pub fn render_qr( + &self, + qr_stream: &Vec, + qr_width: usize, + top_left: Point, + ) -> Result<(), xous::Error> { + let qr_render = QrRender { width: qr_width, top_left, modules: qr_stream.to_owned() }; + let mut buf = Buffer::into_buf(qr_render).unwrap(); + buf.lend_mut(self.conn, GfxOpcode::RenderQr.to_u32().unwrap())?; + // do nothing with the response - function just blocks until the QR code is done rendering + Ok(()) + } + #[cfg(feature = "board-baosec")] pub fn acquire_qr(&self) -> Result { let acquisition = QrAcquisition { content: None, meta: None }; diff --git a/libs/ux-api/src/widgets/notification.rs b/libs/ux-api/src/widgets/notification.rs index 0452fe2d9..7f65f594f 100644 --- a/libs/ux-api/src/widgets/notification.rs +++ b/libs/ux-api/src/widgets/notification.rs @@ -4,7 +4,7 @@ use locales::t; use qrcode::{Color, QrCode}; use super::*; -use crate::minigfx::op::{HEIGHT, WIDTH}; +use crate::minigfx::op::HEIGHT; use crate::minigfx::*; use crate::service::api::*; use crate::service::gfx::Gfx; @@ -52,14 +52,14 @@ impl Notification { Err(_e) => QrCode::new(t!("notification.qrcode.error", locales::LANG)).unwrap(), }; self.qrwidth = qrcode.width(); - log::info!("qrcode {}x{} : {} bytes ", self.qrwidth, self.qrwidth, setting.len()); - self.qrcode = Vec::new(); - for color in qrcode.to_colors().iter() { - match color { - Color::Dark => self.qrcode.push(true), - Color::Light => self.qrcode.push(false), - } - } + self.qrcode = qrcode.into_colors().into_iter().map(|c| c != Color::Light).collect(); + log::info!( + "qrcode {}x{} : {} bytes, {} modules", + self.qrwidth, + self.qrwidth, + setting.len(), + self.qrcode.len() + ); } None => { self.qrcode = Vec::new(); @@ -99,7 +99,11 @@ impl Notification { self.gfx.draw_textview(&mut tv).expect("couldn't post tv"); } - fn draw_qrcode(&self, at_height: isize, modal: &Modal) { + fn draw_qrcode(&self, at_height: isize, _modal: &Modal) { + self.gfx + .render_qr(&self.qrcode, self.qrwidth, Point { x: 0, y: at_height }) + .expect("couldn't render QR"); + /* // calculate pixel size of each module in the qrcode let qrcode_modules: isize = self.qrwidth.try_into().unwrap(); let modules: isize = qrcode_modules + 2 * QUIET_MODULES; @@ -147,6 +151,7 @@ impl Notification { module.translate(step); } self.gfx.draw_object_list(obj_list).expect("couldn't draw qrcode module"); + */ } } diff --git a/services/modals/src/main.rs b/services/modals/src/main.rs index 3b6f099cb..645361eda 100644 --- a/services/modals/src/main.rs +++ b/services/modals/src/main.rs @@ -520,7 +520,7 @@ fn wrapped_main() -> ! { renderer_modal.modify( Some(ActionType::Notification(notification)), Some(text), - false, + text.len() == 0, None, true, Some(DEFAULT_STYLE),