From b63b72ed9064c9982d813ea98945c0faf549ce85 Mon Sep 17 00:00:00 2001 From: grdddj Date: Thu, 4 May 2023 14:59:23 +0200 Subject: [PATCH] chore(core/rust): flag or delete unused code --- core/embed/rust/build.rs | 18 +- core/embed/rust/src/micropython/iter.rs | 9 +- core/embed/rust/src/micropython/util.rs | 1 + core/embed/rust/src/storage/mod.rs | 2 + core/embed/rust/src/trezorhal/dma2d.rs | 1 + core/embed/rust/src/trezorhal/io.rs | 3 - core/embed/rust/src/trezorhal/rgb_led.rs | 2 + core/embed/rust/src/trezorhal/storage.rs | 2 + core/embed/rust/src/ui/component/text/iter.rs | 288 ------------------ core/embed/rust/src/ui/display/mod.rs | 2 - .../model_tt/component/homescreen/render.rs | 2 - .../ui/model_tt/component/keyboard/common.rs | 4 - .../src/ui/model_tt/component/keyboard/pin.rs | 1 - .../ui/model_tt/component/welcome_screen.rs | 5 +- core/embed/rust/src/ui/model_tt/event.rs | 28 -- core/embed/rust/src/ui/model_tt/layout.rs | 33 -- core/embed/rust/src/ui/model_tt/mod.rs | 1 - 17 files changed, 14 insertions(+), 388 deletions(-) delete mode 100644 core/embed/rust/src/ui/component/text/iter.rs delete mode 100644 core/embed/rust/src/ui/model_tt/event.rs diff --git a/core/embed/rust/build.rs b/core/embed/rust/build.rs index 89e95336e2..fd8d400e35 100644 --- a/core/embed/rust/build.rs +++ b/core/embed/rust/build.rs @@ -160,13 +160,11 @@ fn generate_micropython_bindings() { .allowlist_function("mp_obj_is_true") .allowlist_function("mp_call_function_n_kw") .allowlist_function("trezor_obj_get_ll_checked") - .allowlist_function("trezor_obj_get_ull_checked") .allowlist_function("trezor_obj_str_from_rom_text") // buffer .allowlist_function("mp_get_buffer") .allowlist_var("MP_BUFFER_READ") .allowlist_var("MP_BUFFER_WRITE") - .allowlist_var("MP_BUFFER_RW") .allowlist_var("mp_type_str") .allowlist_var("mp_type_bytes") .allowlist_var("mp_type_bytearray") @@ -174,7 +172,6 @@ fn generate_micropython_bindings() { // dict .allowlist_type("mp_obj_dict_t") .allowlist_function("mp_obj_new_dict") - .allowlist_function("mp_obj_dict_store") .allowlist_var("mp_type_dict") // fun .allowlist_type("mp_obj_fun_builtin_fixed_t") @@ -199,7 +196,6 @@ fn generate_micropython_bindings() { .allowlist_var("mp_type_list") // map .allowlist_type("mp_map_elem_t") - .allowlist_type("mp_map_lookup_kind_t") .allowlist_function("mp_map_init") .allowlist_function("mp_map_init_fixed_table") .allowlist_function("mp_map_lookup") @@ -259,10 +255,6 @@ fn generate_trezorhal_bindings() { .allowlist_function("flash_init") // storage .allowlist_var("EXTERNAL_SALT_SIZE") - .allowlist_var("FLAG_PUBLIC") - .allowlist_var("FLAGS_WRITE") - .allowlist_var("MAX_APPID") - .allowlist_type("PIN_UI_WAIT_CALLBACK") .allowlist_function("storage_init") .allowlist_function("storage_wipe") .allowlist_function("storage_is_unlocked") @@ -279,7 +271,6 @@ fn generate_trezorhal_bindings() { .allowlist_function("storage_set_counter") .allowlist_function("storage_next_counter") // display - .allowlist_function("display_init") .allowlist_function("display_offset") .allowlist_function("display_refresh") .allowlist_function("display_backlight") @@ -293,7 +284,6 @@ fn generate_trezorhal_bindings() { .allowlist_function("display_pixeldata_dirty") .allowlist_function("display_set_window") .allowlist_function("display_sync") - .allowlist_var("DISPLAY_CMD_ADDRESS") .allowlist_var("DISPLAY_DATA_ADDRESS") .allowlist_type("toif_format_t") // fonts @@ -347,18 +337,12 @@ fn generate_trezorhal_bindings() { .no_copy("buffer_jpeg_t") .no_copy("buffer_jpeg_work_t") .no_copy("buffer_blurring_t") - .allowlist_var("text_buffer_height") - .allowlist_var("buffer_width") //usb .allowlist_function("usb_configured") // touch .allowlist_function("touch_read") // button - .allowlist_function("button_read") - .allowlist_var("BTN_EVT_DOWN") - .allowlist_var("BTN_EVT_UP") - .allowlist_var("BTN_RIGHT") - .allowlist_var("BTN_LEFT"); + .allowlist_function("button_read"); // Write the bindings to a file in the OUR_DIR. bindings diff --git a/core/embed/rust/src/micropython/iter.rs b/core/embed/rust/src/micropython/iter.rs index 51dc8ff10e..696b2dd878 100644 --- a/core/embed/rust/src/micropython/iter.rs +++ b/core/embed/rust/src/micropython/iter.rs @@ -21,6 +21,7 @@ impl IterBuf { } } +#[allow(dead_code)] // iter_buf is not really used but needs to be there for SAFETY reasons pub struct Iter<'a> { iter: Obj, iter_buf: &'a mut IterBuf, @@ -45,14 +46,6 @@ impl<'a> Iter<'a> { caught_exception: Obj::const_null(), }) } - - pub fn error(&self) -> Option { - if self.caught_exception.is_null() { - None - } else { - Some(Error::CaughtException(self.caught_exception)) - } - } } impl<'a> Iterator for Iter<'a> { diff --git a/core/embed/rust/src/micropython/util.rs b/core/embed/rust/src/micropython/util.rs index caa2ae2d3d..7a836d05b8 100644 --- a/core/embed/rust/src/micropython/util.rs +++ b/core/embed/rust/src/micropython/util.rs @@ -21,6 +21,7 @@ pub unsafe fn try_or_raise(func: impl FnOnce() -> Result) -> T { /// Extract kwargs from a C call and pass them into Rust. Raise exception if an /// error occurs. Should only called when returning from Rust to C. See /// `raise_exception` for details. +#[allow(dead_code)] pub unsafe fn try_with_kwargs( kwargs: *const Map, func: impl FnOnce(&Map) -> Result, diff --git a/core/embed/rust/src/storage/mod.rs b/core/embed/rust/src/storage/mod.rs index 29d4777ff6..a9a123ddc3 100644 --- a/core/embed/rust/src/storage/mod.rs +++ b/core/embed/rust/src/storage/mod.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] + use crate::trezorhal::storage::{get, get_length}; pub const HOMESCREEN_MAX_SIZE: usize = 16384; diff --git a/core/embed/rust/src/trezorhal/dma2d.rs b/core/embed/rust/src/trezorhal/dma2d.rs index 8615bc93bd..0c32cda9ae 100644 --- a/core/embed/rust/src/trezorhal/dma2d.rs +++ b/core/embed/rust/src/trezorhal/dma2d.rs @@ -1,5 +1,6 @@ use super::ffi; +#[allow(dead_code)] pub fn dma2d_setup_4bpp(fg_color: u16, bg_color: u16) { unsafe { ffi::dma2d_setup_4bpp(fg_color, bg_color) } } diff --git a/core/embed/rust/src/trezorhal/io.rs b/core/embed/rust/src/trezorhal/io.rs index 90f9988f52..7f9af446fb 100644 --- a/core/embed/rust/src/trezorhal/io.rs +++ b/core/embed/rust/src/trezorhal/io.rs @@ -1,8 +1,5 @@ use super::ffi; -#[cfg(feature = "button")] -pub use super::ffi::{BTN_EVT_DOWN, BTN_EVT_UP, BTN_LEFT, BTN_RIGHT}; - #[cfg(feature = "touch")] pub fn io_touch_read() -> u32 { unsafe { ffi::touch_read() } diff --git a/core/embed/rust/src/trezorhal/rgb_led.rs b/core/embed/rust/src/trezorhal/rgb_led.rs index f1a46b9f5a..379797e51d 100644 --- a/core/embed/rust/src/trezorhal/rgb_led.rs +++ b/core/embed/rust/src/trezorhal/rgb_led.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] + use super::ffi; pub fn set_color(color: u32) { diff --git a/core/embed/rust/src/trezorhal/storage.rs b/core/embed/rust/src/trezorhal/storage.rs index 0c728b006d..c05621c785 100644 --- a/core/embed/rust/src/trezorhal/storage.rs +++ b/core/embed/rust/src/trezorhal/storage.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] + use super::ffi; use crate::error::Error; use core::ptr; diff --git a/core/embed/rust/src/ui/component/text/iter.rs b/core/embed/rust/src/ui/component/text/iter.rs deleted file mode 100644 index 6834b11f16..0000000000 --- a/core/embed/rust/src/ui/component/text/iter.rs +++ /dev/null @@ -1,288 +0,0 @@ -use crate::ui::{component::LineBreaking, display::Font}; -use core::iter; - -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -struct LineBreak { - /// Index of character **after** the line-break. - offset: usize, - /// Distance from the last line-break of the sequence, in pixels. - width: i16, - style: BreakStyle, -} - -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -enum BreakStyle { - Hard, - AtWhitespaceOrWordBoundary, - InsideWord, -} - -fn limit_line_breaks( - breaks: impl Iterator, - line_height: i16, - available_height: i16, -) -> impl Iterator { - breaks.take(available_height as usize / line_height as usize) -} - -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -enum Appendix { - None, - Hyphen, -} - -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -struct Span<'a> { - text: &'a str, - append: Appendix, -} - -fn break_text_to_spans( - text: &str, - text_font: impl GlyphMetrics, - hyphen_font: impl GlyphMetrics, - breaking: LineBreaking, - available_width: i16, -) -> impl Iterator { - let mut finished = false; - let mut last_break = LineBreak { - offset: 0, - width: 0, - style: BreakStyle::AtWhitespaceOrWordBoundary, - }; - let mut breaks = select_line_breaks( - text.char_indices(), - text_font, - hyphen_font, - breaking, - available_width, - ); - iter::from_fn(move || { - if finished { - None - } else if let Some(lb) = breaks.next() { - let start_of_line = last_break.offset; - let end_of_line = lb.offset; // Not inclusive. - last_break = lb; - if let BreakStyle::AtWhitespaceOrWordBoundary = lb.style { - last_break.offset += 1; - } - Some(Span { - text: &text[start_of_line..end_of_line], - append: match lb.style { - BreakStyle::Hard | BreakStyle::AtWhitespaceOrWordBoundary => Appendix::None, - BreakStyle::InsideWord => Appendix::Hyphen, - }, - }) - } else { - finished = true; - Some(Span { - text: &text[last_break.offset..], - append: Appendix::None, - }) - } - }) -} - -fn select_line_breaks( - chars: impl Iterator, - text_font: impl GlyphMetrics, - hyphen_font: impl GlyphMetrics, - breaking: LineBreaking, - available_width: i16, -) -> impl Iterator { - let hyphen_width = hyphen_font.char_width('-'); - - let mut proposed = None; - let mut line_width = 0; - let mut found_any_whitespace = false; - - chars.filter_map(move |(offset, ch)| { - let char_width = text_font.char_width(ch); - let exceeds_available_width = line_width + char_width > available_width; - let have_space_for_break = line_width + char_width + hyphen_width <= available_width; - let can_break_word = - matches!(breaking, LineBreaking::BreakWordsAndInsertHyphen) || !found_any_whitespace; - - let break_line = match ch { - '\n' | '\r' => { - // Immediate hard break. - Some(LineBreak { - offset, - width: line_width, - style: BreakStyle::Hard, - }) - } - ' ' | '\t' => { - // Whitespace, propose a line-break before this character. - proposed = Some(LineBreak { - offset, - width: line_width, - style: BreakStyle::AtWhitespaceOrWordBoundary, - }); - found_any_whitespace = true; - None - } - _ if have_space_for_break && can_break_word => { - // Propose a word-break after this character. In case the next character is - // whitespace, the proposed word break is replaced by a whitespace break. - proposed = Some(LineBreak { - offset: offset + 1, - width: line_width + char_width + hyphen_width, - style: BreakStyle::InsideWord, - }); - None - } - _ if exceeds_available_width => { - // Consume the last proposed line-break. In case we don't have anything - // proposed, we hard-break immediately before this character. This only happens - // if the first character of the line doesn't fit. - Some(proposed.unwrap_or(LineBreak { - offset, - width: line_width, - style: BreakStyle::Hard, - })) - } - _ => None, - }; - if break_line.is_some() { - // Reset the state. - proposed = None; - line_width = 0; - found_any_whitespace = false; - } else { - // Shift cursor. - line_width += char_width; - } - break_line - }) -} - -pub trait GlyphMetrics { - fn char_width(&self, ch: char) -> i16; - fn text_width(&self, text: &str) -> i16; - fn line_height(&self) -> i16; -} - -impl GlyphMetrics for Font { - fn char_width(&self, ch: char) -> i16 { - Font::char_width(*self, ch) - } - - fn text_width(&self, text: &str) -> i16 { - Font::text_width(*self, text) - } - - fn line_height(&self) -> i16 { - Font::line_height(*self) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_selected_line_breaks() { - assert_eq!(line_breaks("abcd ef", 34), vec![inside_word(2, 25)]); - } - - #[test] - fn test_break_text() { - assert_eq!( - break_text("abcd ef", 24), - vec![ - Span { - text: "a", - append: Appendix::Hyphen - }, - Span { - text: "bcd", - append: Appendix::None - }, - Span { - text: "ef", - append: Appendix::None - } - ] - ) - } - - #[derive(Copy, Clone)] - struct Fixed { - width: i16, - height: i16, - } - - impl GlyphMetrics for Fixed { - fn char_width(&self, _ch: char) -> i16 { - self.width - } - - fn line_height(&self) -> i16 { - self.height - } - - fn text_width(&self, text: &str) -> i16 { - self.width * text.len() as i16 - } - } - - fn break_text(s: &str, w: i16) -> Vec { - break_text_to_spans( - s, - Fixed { - width: 10, - height: 10, - }, - Fixed { - width: 5, - height: 10, - }, - LineBreaking::BreakWordsAndInsertHyphen, - w, - ) - .collect::>() - } - - fn line_breaks(s: &str, w: i16) -> Vec { - select_line_breaks( - s.char_indices(), - Fixed { - width: 10, - height: 10, - }, - Fixed { - width: 5, - height: 10, - }, - LineBreaking::BreakWordsAndInsertHyphen, - w, - ) - .collect::>() - } - - fn hard(offset: usize, width: i16) -> LineBreak { - LineBreak { - offset, - width, - style: BreakStyle::Hard, - } - } - - fn whitespace(offset: usize, width: i16) -> LineBreak { - LineBreak { - offset, - width, - style: BreakStyle::AtWhitespaceOrWordBoundary, - } - } - - fn inside_word(offset: usize, width: i16) -> LineBreak { - LineBreak { - offset, - width, - style: BreakStyle::InsideWord, - } - } -} diff --git a/core/embed/rust/src/ui/display/mod.rs b/core/embed/rust/src/ui/display/mod.rs index 44a1df1afe..a0ef90e5d4 100644 --- a/core/embed/rust/src/ui/display/mod.rs +++ b/core/embed/rust/src/ui/display/mod.rs @@ -632,8 +632,6 @@ pub fn icon_over_icon( bg_buffer_used = &mut *bg2; } - const BUFFER_BPP: usize = 4; - let using_fg = process_buffer( y, r_fg, diff --git a/core/embed/rust/src/ui/model_tt/component/homescreen/render.rs b/core/embed/rust/src/ui/model_tt/component/homescreen/render.rs index fb85e46d23..4a16eb71cf 100644 --- a/core/embed/rust/src/ui/model_tt/component/homescreen/render.rs +++ b/core/embed/rust/src/ui/model_tt/component/homescreen/render.rs @@ -52,8 +52,6 @@ pub const HOMESCREEN_TOIF_X_OFFSET: usize = ((WIDTH - HOMESCREEN_TOIF_SIZE) / 2) const HOMESCREEN_MAX_ICON_SIZE: i16 = 20; const NOTIFICATION_HEIGHT: i16 = 36; const NOTIFICATION_BORDER: i16 = 6; -const NOTIFICATION_ICON_SPACE: i16 = 8; -const NOTIFICATION_TEXT_OFFSET: Offset = Offset::new(1, -2); const TEXT_ICON_SPACE: i16 = 2; const HOMESCREEN_DIM_HEIGHT: i16 = 35; diff --git a/core/embed/rust/src/ui/model_tt/component/keyboard/common.rs b/core/embed/rust/src/ui/model_tt/component/keyboard/common.rs index 5c59332a3e..8b1ecee709 100644 --- a/core/embed/rust/src/ui/model_tt/component/keyboard/common.rs +++ b/core/embed/rust/src/ui/model_tt/component/keyboard/common.rs @@ -10,10 +10,6 @@ use crate::{ }, }; -pub const HEADER_HEIGHT: i16 = 25; -pub const HEADER_PADDING_SIDE: i16 = 5; -pub const HEADER_PADDING_BOTTOM: i16 = 12; - /// Contains state commonly used in implementations multi-tap keyboards. pub struct MultiTapKeyboard { /// Configured timeout after which we cancel currently pending key. diff --git a/core/embed/rust/src/ui/model_tt/component/keyboard/pin.rs b/core/embed/rust/src/ui/model_tt/component/keyboard/pin.rs index 632856dcac..fe4c918eed 100644 --- a/core/embed/rust/src/ui/model_tt/component/keyboard/pin.rs +++ b/core/embed/rust/src/ui/model_tt/component/keyboard/pin.rs @@ -29,7 +29,6 @@ const MAX_VISIBLE_DOTS: usize = 14; const MAX_VISIBLE_DIGITS: usize = 16; const DIGIT_COUNT: usize = 10; // 0..10 -const HEADER_HEIGHT: i16 = 25; const HEADER_PADDING_SIDE: i16 = 5; const HEADER_PADDING_BOTTOM: i16 = 12; diff --git a/core/embed/rust/src/ui/model_tt/component/welcome_screen.rs b/core/embed/rust/src/ui/model_tt/component/welcome_screen.rs index 7db76a14b0..ea320ea8b2 100644 --- a/core/embed/rust/src/ui/model_tt/component/welcome_screen.rs +++ b/core/embed/rust/src/ui/model_tt/component/welcome_screen.rs @@ -1,8 +1,10 @@ +#[cfg(not(feature = "bootloader"))] +use crate::ui::display; #[cfg(feature = "bootloader")] use crate::ui::model_tt::bootloader::theme::DEVICE_NAME; use crate::ui::{ component::{Component, Event, EventCtx, Never}, - display::{self, Icon}, + display::Icon, geometry::{self, Offset, Rect}, model_tt::theme, }; @@ -10,6 +12,7 @@ use crate::ui::{ const TEXT_BOTTOM_MARGIN: i16 = 24; // matching the homescreen label margin const ICON_TOP_MARGIN: i16 = 48; const MODEL_NAME: &str = "Trezor Model T"; +#[cfg(not(feature = "bootloader"))] const MODEL_NAME_FONT: display::Font = display::Font::DEMIBOLD; pub struct WelcomeScreen { diff --git a/core/embed/rust/src/ui/model_tt/event.rs b/core/embed/rust/src/ui/model_tt/event.rs deleted file mode 100644 index 884821470b..0000000000 --- a/core/embed/rust/src/ui/model_tt/event.rs +++ /dev/null @@ -1,28 +0,0 @@ -use core::convert::TryInto; - -use crate::{error, ui::geometry::Point}; - -#[derive(Copy, Clone, PartialEq, Eq)] -pub enum TouchEvent { - /// A person has started touching the screen at given absolute coordinates. - /// `TouchMove` will usually follow, and `TouchEnd` should finish the - /// interaction. - TouchStart(Point), - /// Touch has moved into a different point on the screen. - TouchMove(Point), - /// Touch has ended at a point on the screen. - TouchEnd(Point), -} - -impl TouchEvent { - pub fn new(event: u32, x: u32, y: u32) -> Result { - let point = Point::new(x.try_into()?, y.try_into()?); - let result = match event { - 1 => Self::TouchStart(point), - 2 => Self::TouchMove(point), - 4 => Self::TouchEnd(point), - _ => return Err(error::Error::OutOfRange), - }; - Ok(result) - } -} diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index f6fa91b3db..b91ffa2f97 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -685,29 +685,6 @@ extern "C" fn new_confirm_reset_device(n_args: usize, args: *const Obj, kwargs: unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -extern "C" fn new_show_qr(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { - let block = move |_args: &[Obj], kwargs: &Map| { - let title: StrBuffer = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?; - let address: StrBuffer = kwargs.get(Qstr::MP_QSTR_address)?.try_into()?; - let verb_cancel: StrBuffer = kwargs.get(Qstr::MP_QSTR_verb_cancel)?.try_into()?; - let case_sensitive: bool = kwargs.get(Qstr::MP_QSTR_case_sensitive)?.try_into()?; - - let buttons = Button::cancel_confirm( - Button::with_text(verb_cancel), - Button::with_text("CONFIRM".into()).styled(theme::button_confirm()), - false, - ); - - let obj = LayoutObj::new(Frame::left_aligned( - theme::label_title(), - title, - Dialog::new(Qr::new(address, case_sensitive)?.with_border(4), buttons), - ))?; - Ok(obj.into()) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - extern "C" fn new_show_address_details(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { let block = move |_args: &[Obj], kwargs: &Map| { let address: StrBuffer = kwargs.get(Qstr::MP_QSTR_address)?.try_into()?; @@ -1671,16 +1648,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// """Confirm TOS before device setup.""" Qstr::MP_QSTR_confirm_reset_device => obj_fn_kw!(0, new_confirm_reset_device).as_obj(), - /// def show_qr( - /// *, - /// title: str, - /// address: str, - /// verb_cancel: str, - /// case_sensitive: bool, - /// ) -> object: - /// """Show QR code.""" - Qstr::MP_QSTR_show_qr => obj_fn_kw!(0, new_show_qr).as_obj(), - /// def show_address_details( /// *, /// address: str, diff --git a/core/embed/rust/src/ui/model_tt/mod.rs b/core/embed/rust/src/ui/model_tt/mod.rs index 8308f1b738..529781d27e 100644 --- a/core/embed/rust/src/ui/model_tt/mod.rs +++ b/core/embed/rust/src/ui/model_tt/mod.rs @@ -2,7 +2,6 @@ pub mod bootloader; pub mod component; pub mod constant; -pub mod event; pub mod theme; #[cfg(feature = "micropython")]