From 9cd9c1eec42d5adbfae95ee962941050cf77ba5f Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 31 Mar 2023 19:02:59 +0200 Subject: [PATCH] Standardise SWKBD to C-NO-OUT --- ctru-rs/examples/file-explorer.rs | 11 +++++------ ctru-rs/examples/software-keyboard.rs | 11 ++++------- ctru-rs/src/applets/swkbd.rs | 12 ++++++------ 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/ctru-rs/examples/file-explorer.rs b/ctru-rs/examples/file-explorer.rs index 78b4968..2e4e254 100644 --- a/ctru-rs/examples/file-explorer.rs +++ b/ctru-rs/examples/file-explorer.rs @@ -162,17 +162,16 @@ impl<'a> FileExplorer<'a> { fn get_input_and_run(&mut self, action: impl FnOnce(&mut Self, String)) { let mut keyboard = Swkbd::default(); - let mut new_path_str = String::new(); - match keyboard.write_to_string(&mut new_path_str) { - Ok(Button::Right) => { + match keyboard.write_to_string() { + Ok((path, Button::Right)) => { // Clicked "OK" - action(self, new_path_str); + action(self, path); } - Ok(Button::Left) => { + Ok((_, Button::Left)) => { // Clicked "Cancel" } - Ok(Button::Middle) => { + Ok((_, Button::Middle)) => { // This button wasn't shown unreachable!() } diff --git a/ctru-rs/examples/software-keyboard.rs b/ctru-rs/examples/software-keyboard.rs index 2f43b6f..51bb29e 100644 --- a/ctru-rs/examples/software-keyboard.rs +++ b/ctru-rs/examples/software-keyboard.rs @@ -24,15 +24,12 @@ fn main() { // configurations. let mut keyboard = Swkbd::default(); - // String used to store text received from the keyboard - let mut text = String::new(); - // Raise the software keyboard. You can perform different actions depending on which // software button the user pressed - match keyboard.write_to_string(&mut text) { - Ok(Button::Right) => println!("You entered: {text}"), - Ok(Button::Left) => println!("Cancelled"), - Ok(Button::Middle) => println!("How did you even press this?"), + match keyboard.write_to_string() { + Ok((text, Button::Right)) => println!("You entered: {text}"), + Ok((_, Button::Left)) => println!("Cancelled"), + Ok((_, Button::Middle)) => println!("How did you even press this?"), Err(_) => println!("Oh noes, an error happened!"), } } diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index 2e58331..5153c25 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -101,24 +101,24 @@ impl Swkbd { /// /// The text received from the keyboard will be truncated if it is greater than 2048 bytes /// in length. - pub fn write_to_string(&mut self, buf: &mut String) -> Result { + pub fn write_to_string(&mut self) -> 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. const MAX_BYTES: usize = 2048; - let mut tmp = [0u8; MAX_BYTES]; + let mut tmp = vec![0u8; MAX_BYTES]; let button = self.write_bytes(&mut tmp)?; // 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 // should be safe. let len = unsafe { libc::strlen(tmp.as_ptr()) }; - let utf8 = unsafe { str::from_utf8_unchecked(&tmp[..len]) }; + tmp.truncate(len); - // Copy the input into the user's `String` - *buf += utf8; - Ok(button) + let res = unsafe { String::from_utf8_unchecked(tmp) }; + + Ok((res, button)) } /// Fills the provided buffer with a UTF-8 encoded, NUL-terminated sequence of bytes from