From 7a86507182fc9b68b31c6901c4b2c02df8c9f33b Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Mon, 27 Nov 2023 17:34:07 +0100 Subject: [PATCH] Fix todo and start working on swkbd --- ctru-rs/examples/software-keyboard.rs | 12 ++++++------ ctru-rs/src/applets/mii_selector.rs | 9 +++++---- ctru-rs/src/applets/swkbd.rs | 15 +++++++-------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/ctru-rs/examples/software-keyboard.rs b/ctru-rs/examples/software-keyboard.rs index 61c32db..cc8d81c 100644 --- a/ctru-rs/examples/software-keyboard.rs +++ b/ctru-rs/examples/software-keyboard.rs @@ -11,6 +11,11 @@ fn main() { let gfx = Gfx::new().unwrap(); let _console = Console::new(gfx.top_screen.borrow_mut()); + // Prepares a software keyboard with two buttons: one to cancel input and one + // to accept it. You can also use `SoftwareKeyboard::new()` to launch the keyboard + // with different configurations. + let mut keyboard = SoftwareKeyboard::default(); + println!("Press A to enter some text or press Start to exit."); while apt.main_loop() { @@ -22,14 +27,9 @@ fn main() { // Check if the user request to write some input. if hid.keys_down().contains(KeyPad::A) { - // Prepares a software keyboard with two buttons: One to cancel input and one - // to accept it. You can also use `SoftwareKeyboard::new()` to launch the keyboard in different - // configurations. - let mut keyboard = SoftwareKeyboard::default(); - // Raise the software keyboard. You can perform different actions depending on which // software button the user pressed. - match keyboard.get_string(2048) { + match keyboard.get_string(2048, &apt, &gfx) { Ok((text, Button::Right)) => println!("You entered: {text}"), Ok((_, Button::Left)) => println!("Cancelled"), Ok((_, Button::Middle)) => println!("How did you even press this?"), diff --git a/ctru-rs/src/applets/mii_selector.rs b/ctru-rs/src/applets/mii_selector.rs index 2353d7d..81ab2ae 100644 --- a/ctru-rs/src/applets/mii_selector.rs +++ b/ctru-rs/src/applets/mii_selector.rs @@ -4,6 +4,8 @@ //! The selected Mii is readable as a [`Mii`]. use crate::mii::Mii; +use crate::services::{apt::Apt, gfx::Gfx}; + use bitflags::bitflags; use std::{ffi::CString, fmt}; @@ -252,9 +254,8 @@ impl MiiSelector { /// Launch the Mii Selector. /// - /// Depending on the configuration, the Mii Selector window will appear either on the bottom screen (default behaviour) or the top screen (see [`Options::USE_TOP_SCREEN`]). - /// - /// TODO: UNSAFE OPERATION, LAUNCHING APPLETS REQUIRES GRAPHICS, WITHOUT AN ACTIVE GFX THIS WILL CAUSE A SEGMENTATION FAULT. + /// Depending on the configuration, the Mii Selector window will appear either + /// on the bottom screen (default behaviour) or the top screen (see [`Options::USE_TOP_SCREEN`]). /// /// # Example /// @@ -276,7 +277,7 @@ impl MiiSelector { /// # } /// ``` #[doc(alias = "miiSelectorLaunch")] - pub fn launch(&mut self) -> Result { + pub fn launch(&mut self, apt: &Apt, gfx: &Gfx) -> Result { let mut return_val = Box::::default(); unsafe { ctru_sys::miiSelectorLaunch(self.config.as_mut(), return_val.as_mut()) } diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index 69d4329..05ca659 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -5,11 +5,13 @@ // TODO: Split the Parental PIN lock operations into a different type. #![doc(alias = "keyboard")] -use bitflags::bitflags; use ctru_sys::{ self, swkbdInit, swkbdInputText, swkbdSetButton, swkbdSetFeatures, swkbdSetHintText, swkbdSetInitialText, SwkbdState, }; +use crate::services::{apt::Apt, gfx::Gfx}; + +use bitflags::bitflags; use libc; use std::fmt::Display; use std::iter::once; @@ -188,8 +190,7 @@ impl SoftwareKeyboard { /// # Notes /// /// The text received from the keyboard will be truncated if it is longer than `max_bytes`. - /// - /// TODO: UNSAFE OPERATION, LAUNCHING APPLETS REQUIRES GRAPHICS, WITHOUT AN ACTIVE GFX THIS WILL CAUSE A SEGMENTATION FAULT. + /// Use [`SoftwareKeyboard::set_max_text_len()`] to make sure the buffer can contain the input text. /// /// # Example /// @@ -207,13 +208,13 @@ impl SoftwareKeyboard { /// # } /// ``` #[doc(alias = "swkbdInputText")] - pub fn get_string(&mut self, max_bytes: usize) -> Result<(String, Button), Error> { + pub fn get_string(&mut self, max_bytes: usize, apt: &Apt, gfx: &Gfx) -> Result<(String, Button), Error> { // Unfortunately the libctru API doesn't really provide a way to get the exact length // of the string that it receieves from the software keyboard. Instead it expects you // to pass in a buffer and hope that it's big enough to fit the entire string, so // you have to set some upper limit on the potential size of the user's input. let mut tmp = vec![0u8; max_bytes]; - let button = self.write_exact(&mut tmp)?; + let button = self.write_exact(&mut tmp, apt, gfx)?; // libctru does, however, seem to ensure that the buffer will always contain a properly // terminated UTF-8 sequence even if the input has to be truncated, so these operations @@ -234,8 +235,6 @@ impl SoftwareKeyboard { /// If the buffer is too small to contain the entire sequence received from the keyboard, /// the output will be truncated. /// - /// TODO: UNSAFE OPERATION, LAUNCHING APPLETS REQUIRES GRAPHICS, WITHOUT AN ACTIVE GFX THIS WILL CAUSE A SEGMENTATION FAULT. - /// /// # Example /// /// ``` @@ -254,7 +253,7 @@ impl SoftwareKeyboard { /// # } /// ``` #[doc(alias = "swkbdInputText")] - pub fn write_exact(&mut self, buf: &mut [u8]) -> Result { + pub fn write_exact(&mut self, buf: &mut [u8], apt: &Apt, gfx: &Gfx) -> Result { unsafe { match swkbdInputText(self.state.as_mut(), buf.as_mut_ptr(), buf.len()) { ctru_sys::SWKBD_BUTTON_NONE => Err(self.parse_swkbd_error()),