Skip to content

Commit

Permalink
Add basic error applet support
Browse files Browse the repository at this point in the history
  • Loading branch information
FenrirWolf committed Feb 19, 2024
1 parent 4dbe83e commit df88c27
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
56 changes: 56 additions & 0 deletions ctru-rs/src/applets/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! Error applet
//!
//! This applet displays error text as a pop-up message on the lower screen.
#![doc(alias = "Error")]

use crate::services::{apt::Apt, gfx::Gfx};

use ctru_sys::errorConf;

/// Configuration struct to set up the Error applet.
#[doc(alias = "errorConf")]
pub struct ErrorApplet {
state: Box<errorConf>,
}

/// The kind of error applet to display.
#[doc(alias = "errorType")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
pub enum Kind {
/// Error text is centered in the error applet window.
Center = ctru_sys::ERROR_TEXT,
/// Error text starts at the top of the error applet window.
Top = ctru_sys::ERROR_TEXT_WORD_WRAP,
}

impl ErrorApplet {
/// Initialize the error applet with the provided text kind.
#[doc(alias = "errorInit")]
pub fn new(kind: Kind) -> Self {
let mut state = Box::new(errorConf::default());

Check warning on line 31 in ctru-rs/src/applets/error.rs

View workflow job for this annotation

GitHub Actions / lint (nightly-2023-06-01)

`Box::new(_)` of default value

unsafe { ctru_sys::errorInit(state.as_mut(), kind as _, 0) };

Self { state }
}

/// Sets the error text to display.
#[doc(alias = "errorText")]
pub fn set_text(&mut self, text: &str) {
for (idx, code_unit) in text
.encode_utf16()
.chain(std::iter::once(0))
.take(self.state.Text.len() - 1)
.enumerate()
{
self.state.Text[idx] = code_unit;
}
}

/// Launches the error applet.
#[doc(alias = "errorDisp")]
pub fn launch(&mut self, _apt: &Apt, _gfx: &Gfx) {
unsafe { ctru_sys::errorDisp(self.state.as_mut()) }
}
}
1 change: 1 addition & 0 deletions ctru-rs/src/applets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
//!
//! Applets block execution of the thread that launches them as long as the user doesn't close the applet.

pub mod error;
pub mod mii_selector;
pub mod swkbd;
2 changes: 2 additions & 0 deletions ctru-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ fn main() {
.blocklist_type("u(8|16|32|64)")
.blocklist_type("__builtin_va_list")
.blocklist_type("__va_list")
.blocklist_type("errorReturnCode")
.blocklist_type("errorScreenFlag")
.opaque_type("MiiData")
.derive_default(true)
.wrap_static_fns(true)
Expand Down
16 changes: 16 additions & 0 deletions ctru-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@
pub mod result;
pub use result::*;

// Fun fact: bindgen can and will generate enum values of the wrong size, and if a generated struct contains fields
// using those values, then that struct will have the wrong size and field offsets too. To fix that problem,
// you have to blocklist the enum type in bindgen and manually define it with the proper data type.
pub const ERROR_UNKNOWN: errorReturnCode = -1;
pub const ERROR_NONE: errorReturnCode = 0;
pub const ERROR_SUCCESS: errorReturnCode = 1;
pub const ERROR_NOT_SUPPORTED: errorReturnCode = 2;
pub const ERROR_HOME_BUTTON: errorReturnCode = 10;
pub const ERROR_SOFTWARE_RESET: errorReturnCode = 11;
pub const ERROR_POWER_BUTTON: errorReturnCode = 12;
pub type errorReturnCode = libc::c_schar;

pub const ERROR_NORMAL: errorScreenFlag = 0;
pub const ERROR_STEREO: errorScreenFlag = 1;
pub type errorScreenFlag = libc::c_char;

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

/// In lieu of a proper errno function exposed by libc
Expand Down

0 comments on commit df88c27

Please sign in to comment.