From 294284ef653b556716b47aed6f9a5d77de3d5de8 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 31 Mar 2023 23:54:49 +0200 Subject: [PATCH] Better swkbd names --- ctru-rs/examples/file-explorer.rs | 2 +- ctru-rs/examples/software-keyboard.rs | 2 +- ctru-rs/src/applets/swkbd.rs | 12 +++++------- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/ctru-rs/examples/file-explorer.rs b/ctru-rs/examples/file-explorer.rs index 2e4e254..527595c 100644 --- a/ctru-rs/examples/file-explorer.rs +++ b/ctru-rs/examples/file-explorer.rs @@ -163,7 +163,7 @@ impl<'a> FileExplorer<'a> { fn get_input_and_run(&mut self, action: impl FnOnce(&mut Self, String)) { let mut keyboard = Swkbd::default(); - match keyboard.write_to_string() { + match keyboard.get_string(2048) { Ok((path, Button::Right)) => { // Clicked "OK" action(self, path); diff --git a/ctru-rs/examples/software-keyboard.rs b/ctru-rs/examples/software-keyboard.rs index 51bb29e..5f65be4 100644 --- a/ctru-rs/examples/software-keyboard.rs +++ b/ctru-rs/examples/software-keyboard.rs @@ -26,7 +26,7 @@ fn main() { // Raise the software keyboard. You can perform different actions depending on which // software button the user pressed - match keyboard.write_to_string() { + match keyboard.get_string(2048) { 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/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index b5aaaf1..0af7dee 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -99,16 +99,14 @@ impl Swkbd { /// Gets input from this keyboard and appends it to the provided string. /// - /// 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) -> Result<(String, Button), Error> { + /// The text received from the keyboard will be truncated if it is longer than `max_bytes`. + pub fn get_string(&mut self, max_bytes: usize) -> 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 = vec![0u8; MAX_BYTES]; - let button = self.write_bytes(&mut tmp)?; + let mut tmp = vec![0u8; max_bytes]; + let button = self.write_exact(&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 @@ -126,7 +124,7 @@ impl Swkbd { /// /// If the buffer is too small to contain the entire sequence received from the keyboard, /// the output will be truncated but should still be well-formed UTF-8. - pub fn write_bytes(&mut self, buf: &mut [u8]) -> Result { + pub fn write_exact(&mut self, buf: &mut [u8]) -> Result { unsafe { match swkbdInputText(self.state.as_mut(), buf.as_mut_ptr(), buf.len()) { ctru_sys::SWKBD_BUTTON_NONE => Err(self.parse_swkbd_error()),