Browse Source

Add basic error applet support

pull/162/head
Fenrir 10 months ago
parent
commit
dc2959396c
  1. 56
      ctru-rs/src/applets/error.rs
  2. 1
      ctru-rs/src/applets/mod.rs
  3. 2
      ctru-sys/build.rs
  4. 16
      ctru-sys/src/lib.rs

56
ctru-rs/src/applets/error.rs

@ -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::<errorConf>::default();
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
ctru-rs/src/applets/mod.rs

@ -8,5 +8,6 @@
//! //!
//! Applets block execution of the thread that launches them as long as the user doesn't close the applet. //! 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 mii_selector;
pub mod swkbd; pub mod swkbd;

2
ctru-sys/build.rs

@ -79,6 +79,8 @@ fn main() {
.blocklist_type("u(8|16|32|64)") .blocklist_type("u(8|16|32|64)")
.blocklist_type("__builtin_va_list") .blocklist_type("__builtin_va_list")
.blocklist_type("__va_list") .blocklist_type("__va_list")
.blocklist_type("errorReturnCode")
.blocklist_type("errorScreenFlag")
.opaque_type("MiiData") .opaque_type("MiiData")
.derive_default(true) .derive_default(true)
.wrap_static_fns(true) .wrap_static_fns(true)

16
ctru-sys/src/lib.rs

@ -16,6 +16,22 @@
pub mod result; pub mod result;
pub use 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")); include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
/// In lieu of a proper errno function exposed by libc /// In lieu of a proper errno function exposed by libc

Loading…
Cancel
Save