From 757afe9fe6f195ffa8b9032a1ce1ee368839046a Mon Sep 17 00:00:00 2001 From: Fenrir Date: Fri, 16 Feb 2024 11:27:04 -0700 Subject: [PATCH] Make swkbd_input_text a method for SoftwareKeyboard This lets us mark the function as safe since it can only be called with an initialized `SwkbdState` --- ctru-rs/src/applets/swkbd.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index a47a7bc..c7a2be1 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -281,7 +281,7 @@ impl SoftwareKeyboard { (self as *mut Self).cast(), ); - match Self::swkbd_input_text(&mut self.state, &mut output) { + match self.swkbd_input_text(&mut output) { ctru_sys::SWKBD_BUTTON_NONE => Err(self.state.result.into()), ctru_sys::SWKBD_BUTTON_LEFT => Ok((output, Button::Left)), ctru_sys::SWKBD_BUTTON_MIDDLE => Ok((output, Button::Middle)), @@ -631,10 +631,7 @@ impl SoftwareKeyboard { // A reimplementation of `swkbdInputText` from `libctru/source/applets/swkbd.c`. Allows us to // get text from the software keyboard and put it directly into a `String` without requiring // an intermediate fixed-size buffer - // - // SAFETY: `swkbd` must be initialized by `swkbdInit` before calling this function. - #[deny(unsafe_op_in_unsafe_fn)] - unsafe fn swkbd_input_text(swkbd: &mut SwkbdState, output: &mut String) -> SwkbdButton { + fn swkbd_input_text(&mut self, output: &mut String) -> SwkbdButton { use ctru_sys::{ MEMPERM_READ, MEMPERM_WRITE, R_FAILED, SWKBD_BUTTON_LEFT, SWKBD_BUTTON_MIDDLE, SWKBD_BUTTON_NONE, SWKBD_BUTTON_RIGHT, SWKBD_D0_CLICK, SWKBD_D1_CLICK0, @@ -642,6 +639,7 @@ impl SoftwareKeyboard { SWKBD_FILTER_CALLBACK, SWKBD_OUTOFMEM, }; + let swkbd = self.state.as_mut(); let mut extra = unsafe { swkbd.__bindgen_anon_1.extra }; // Calculate shared mem size @@ -862,7 +860,7 @@ impl SoftwareKeyboard { let text16 = unsafe { widestring::Utf16Str::from_slice_unchecked(std::slice::from_raw_parts( SWKBD_SHARED_MEM.add(swkbd.text_offset as _).cast(), - swkbd.text_length as _, + swkbd.text_length as usize + 1, )) };