From 22773f644a9f39a4656f1ca2f21675ab4c7a90c8 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Wed, 14 Feb 2024 14:11:58 -0700 Subject: [PATCH 1/2] Store initial_text data in SoftwareKeyboard --- ctru-rs/src/applets/swkbd.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index 41d40cd2..b1c4a287 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -22,6 +22,7 @@ pub struct SoftwareKeyboard { state: Box, callback: Option>, error_message: Option, + initial_text: Option, } /// Configuration structure to setup the Parental Lock applet. @@ -233,6 +234,7 @@ impl SoftwareKeyboard { state, callback: None, error_message: None, + initial_text: None, } } } @@ -503,8 +505,12 @@ impl SoftwareKeyboard { #[doc(alias = "swkbdSetInitialText")] pub fn set_initial_text(&mut self, text: &str) { unsafe { - let nul_terminated: String = text.chars().chain(once('\0')).collect(); - ctru_sys::swkbdSetInitialText(self.state.as_mut(), nul_terminated.as_ptr()); + self.initial_text = Some(CString::new(text).unwrap()); + + ctru_sys::swkbdSetInitialText( + self.state.as_mut(), + self.initial_text.as_ref().unwrap().as_ptr(), + ); } } From 9eea3f0d1f92fed4e473540347aff627fa4389e3 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Thu, 15 Feb 2024 13:17:27 -0700 Subject: [PATCH 2/2] Make initial text argument optional This allows the user to clear the initial_text field by supplying a `None` --- ctru-rs/src/applets/swkbd.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index b1c4a287..9dd75d11 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -499,18 +499,23 @@ impl SoftwareKeyboard { /// use ctru::applets::swkbd::SoftwareKeyboard; /// let mut keyboard = SoftwareKeyboard::default(); /// - /// keyboard.set_initial_text("Write here what you like!"); + /// keyboard.set_initial_text(Some("Write here what you like!")); /// # /// # } #[doc(alias = "swkbdSetInitialText")] - pub fn set_initial_text(&mut self, text: &str) { - unsafe { - self.initial_text = Some(CString::new(text).unwrap()); + pub fn set_initial_text(&mut self, text: Option<&str>) { + if let Some(text) = text { + let initial_text = CString::new(text).unwrap(); - ctru_sys::swkbdSetInitialText( - self.state.as_mut(), - self.initial_text.as_ref().unwrap().as_ptr(), - ); + unsafe { + ctru_sys::swkbdSetInitialText(self.state.as_mut(), initial_text.as_ptr()); + } + + self.initial_text = Some(initial_text); + } else { + unsafe { ctru_sys::swkbdSetInitialText(self.state.as_mut(), std::ptr::null()) }; + + self.initial_text = None; } }