|
| 1 | +//! Error applet. |
| 2 | +//! |
| 3 | +//! This applet displays error text as a pop-up message on the lower screen. |
| 4 | +
|
| 5 | +use crate::services::{apt::Apt, gfx::Gfx}; |
| 6 | + |
| 7 | +use ctru_sys::errorConf; |
| 8 | + |
| 9 | +/// Configuration struct to set up the Error applet. |
| 10 | +#[doc(alias = "errorConf")] |
| 11 | +pub struct PopUp { |
| 12 | + state: Box<errorConf>, |
| 13 | +} |
| 14 | + |
| 15 | +/// Determines whether the Error applet will use word wrapping when displaying a message. |
| 16 | +#[doc(alias = "errorType")] |
| 17 | +#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 18 | +#[repr(u32)] |
| 19 | +pub enum WordWrap { |
| 20 | + /// Error text is centered in the error applet window and does not use word wrapping. |
| 21 | + Disabled = ctru_sys::ERROR_TEXT, |
| 22 | + /// Error text starts at the top of the error applet window and uses word wrapping. |
| 23 | + Enabled = ctru_sys::ERROR_TEXT_WORD_WRAP, |
| 24 | +} |
| 25 | + |
| 26 | +/// Error returned by an unsuccessful [`PopUp::launch()`]. |
| 27 | +#[doc(alias = "errorReturnCode")] |
| 28 | +#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 29 | +#[repr(i8)] |
| 30 | +pub enum Error { |
| 31 | + /// Unknown error occurred. |
| 32 | + Unknown = ctru_sys::ERROR_UNKNOWN, |
| 33 | + /// Operation not supported. |
| 34 | + NotSupported = ctru_sys::ERROR_NOT_SUPPORTED, |
| 35 | + /// Home button pressed while [`PopUp`] was running. |
| 36 | + HomePressed = ctru_sys::ERROR_HOME_BUTTON, |
| 37 | + /// Power button pressed while [`PopUp`] was running. |
| 38 | + PowerPressed = ctru_sys::ERROR_POWER_BUTTON, |
| 39 | + /// Reset button pressed while [`PopUp`] was running. |
| 40 | + ResetPressed = ctru_sys::ERROR_SOFTWARE_RESET, |
| 41 | +} |
| 42 | + |
| 43 | +impl PopUp { |
| 44 | + /// Initializes the error applet with the provided word wrap setting. |
| 45 | + #[doc(alias = "errorInit")] |
| 46 | + pub fn new(word_wrap: WordWrap) -> Self { |
| 47 | + let mut state = Box::<errorConf>::default(); |
| 48 | + |
| 49 | + unsafe { ctru_sys::errorInit(state.as_mut(), word_wrap as _, 0) }; |
| 50 | + |
| 51 | + Self { state } |
| 52 | + } |
| 53 | + |
| 54 | + /// Sets the error text to display. |
| 55 | + /// |
| 56 | + /// # Notes |
| 57 | + /// |
| 58 | + /// The text will be converted to UTF-16 for display with the applet, and the message will be truncated if it exceeds |
| 59 | + /// 1900 UTF-16 code units in length after conversion. |
| 60 | + #[doc(alias = "errorText")] |
| 61 | + pub fn set_text(&mut self, text: &str) { |
| 62 | + for (idx, code_unit) in text |
| 63 | + .encode_utf16() |
| 64 | + .take(self.state.Text.len() - 1) |
| 65 | + .chain(std::iter::once(0)) |
| 66 | + .enumerate() |
| 67 | + { |
| 68 | + self.state.Text[idx] = code_unit; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// Launches the error applet. |
| 73 | + #[doc(alias = "errorDisp")] |
| 74 | + pub fn launch(&mut self, _apt: &Apt, _gfx: &Gfx) -> Result<(), Error> { |
| 75 | + unsafe { self.launch_unchecked() } |
| 76 | + } |
| 77 | + |
| 78 | + /// Launches the error applet without requiring an [`Apt`] or [`Gfx`] handle. |
| 79 | + /// |
| 80 | + /// # Safety |
| 81 | + /// |
| 82 | + /// Potentially leads to undefined behavior if the aforementioned services are not actually active when the applet launches. |
| 83 | + unsafe fn launch_unchecked(&mut self) -> Result<(), Error> { |
| 84 | + unsafe { ctru_sys::errorDisp(self.state.as_mut()) }; |
| 85 | + |
| 86 | + match self.state.returnCode { |
| 87 | + ctru_sys::ERROR_NONE | ctru_sys::ERROR_SUCCESS => Ok(()), |
| 88 | + ctru_sys::ERROR_NOT_SUPPORTED => Err(Error::NotSupported), |
| 89 | + ctru_sys::ERROR_HOME_BUTTON => Err(Error::HomePressed), |
| 90 | + ctru_sys::ERROR_POWER_BUTTON => Err(Error::PowerPressed), |
| 91 | + ctru_sys::ERROR_SOFTWARE_RESET => Err(Error::ResetPressed), |
| 92 | + _ => Err(Error::Unknown), |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// Sets a custom [panic hook](https://doc.rust-lang.org/std/panic/fn.set_hook.html) that uses the error applet to display panic messages. |
| 98 | +/// |
| 99 | +/// You can also choose to have the previously registered panic hook called along with the error applet popup, which can be useful |
| 100 | +/// if you want to use output redirection to display panic messages over `3dslink` or `GDB`. |
| 101 | +/// |
| 102 | +/// You can use [`std::panic::take_hook`](https://doc.rust-lang.org/std/panic/fn.take_hook.html) to unregister the panic hook |
| 103 | +/// set by this function. |
| 104 | +/// |
| 105 | +/// # Notes |
| 106 | +/// |
| 107 | +/// * If the [`Gfx`] service is not initialized during a panic, the error applet will not be displayed and the old panic hook will be called. |
| 108 | +pub fn set_panic_hook(call_old_hook: bool) { |
| 109 | + use crate::services::gfx::GFX_ACTIVE; |
| 110 | + use std::sync::TryLockError; |
| 111 | + |
| 112 | + let old_hook = std::panic::take_hook(); |
| 113 | + |
| 114 | + std::panic::set_hook(Box::new(move |panic_info| { |
| 115 | + // If we get a `WouldBlock` error, we know that the `Gfx` service has been initialized. |
| 116 | + // Otherwise fallback to using the old panic hook. |
| 117 | + if let (Err(TryLockError::WouldBlock), Ok(_apt)) = (GFX_ACTIVE.try_lock(), Apt::new()) { |
| 118 | + if call_old_hook { |
| 119 | + old_hook(panic_info); |
| 120 | + } |
| 121 | + |
| 122 | + let thread = std::thread::current(); |
| 123 | + |
| 124 | + let name = thread.name().unwrap_or("<unnamed>"); |
| 125 | + |
| 126 | + let message = format!("thread '{name}' {panic_info}"); |
| 127 | + |
| 128 | + let mut popup = PopUp::new(WordWrap::Enabled); |
| 129 | + |
| 130 | + popup.set_text(&message); |
| 131 | + |
| 132 | + unsafe { |
| 133 | + let _ = popup.launch_unchecked(); |
| 134 | + } |
| 135 | + } else { |
| 136 | + old_hook(panic_info); |
| 137 | + } |
| 138 | + })); |
| 139 | +} |
| 140 | + |
| 141 | +impl std::fmt::Display for Error { |
| 142 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 143 | + match self { |
| 144 | + Self::NotSupported => write!(f, "operation not supported"), |
| 145 | + Self::HomePressed => write!(f, "home button pressed while error applet was running"), |
| 146 | + Self::PowerPressed => write!(f, "power button pressed while error applet was running"), |
| 147 | + Self::ResetPressed => write!(f, "reset button pressed while error applet was running"), |
| 148 | + Self::Unknown => write!(f, "an unknown error occurred"), |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +impl std::error::Error for Error {} |
0 commit comments