diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index 4e41915..98291e0 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -5,10 +5,10 @@ use crate::services::{apt::Apt, gfx::Gfx}; use ctru_sys::{ - self, aptLaunchLibraryApplet, aptSetMessageCallback, envGetAptAppId, svcBreak, svcCloseHandle, + self, aptLaunchLibraryApplet, aptSetMessageCallback, envGetAptAppId, svcCloseHandle, svcCreateMemoryBlock, APT_SendParameter, SwkbdButton, SwkbdDictWord, SwkbdExtra, SwkbdLearningData, SwkbdState, SwkbdStatusData, APPID_SOFTWARE_KEYBOARD, APTCMD_MESSAGE, - NS_APPID, SWKBD_CALLBACK_OK, USERBREAK_PANIC, + NS_APPID, SWKBD_CALLBACK_OK, }; use bitflags::bitflags; @@ -281,7 +281,7 @@ impl SoftwareKeyboard { (self as *mut Self).cast(), ); - match Self::swkbd_input_text(self.state.as_mut(), output.as_mut_vec()) { + match Self::swkbd_input_text(self.state.as_mut(), &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,7 +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 - fn swkbd_input_text(swkbd: &mut SwkbdState, buf: &mut Vec) -> SwkbdButton { + fn swkbd_input_text(swkbd: &mut SwkbdState, 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, @@ -715,11 +715,11 @@ impl SoftwareKeyboard { swkbd.initial_text_offset = 0; unsafe { - let utf16_iter = CStr::from_ptr(extra.initial_text) - .to_str() - .unwrap() - .encode_utf16() - .chain(once(0)); + let utf16_iter = + str::from_utf8_unchecked(CStr::from_ptr(extra.initial_text).to_bytes()) + .encode_utf16() + .take(swkbd.max_text_len as _) + .chain(once(0)); let mut initial_text_cursor = SWKBD_SHARED_MEM.cast::(); @@ -803,17 +803,16 @@ impl SoftwareKeyboard { let text16 = if swkbd.text_length > 0 { unsafe { - widestring::Utf16Str::from_slice(std::slice::from_raw_parts( + widestring::Utf16Str::from_slice_unchecked(std::slice::from_raw_parts( SWKBD_SHARED_MEM.add(swkbd.text_offset as _).cast::(), swkbd.text_length as usize, )) - .unwrap() } } else { widestring::utf16str!("") }; - buf.extend(text16.encode_utf8()); + *output = text16.to_string(); if swkbd.save_state_flags & (1 << 0) != 0 { unsafe { @@ -860,20 +859,13 @@ impl SoftwareKeyboard { } let text16 = unsafe { - widestring::Utf16Str::from_slice(std::slice::from_raw_parts( + widestring::Utf16Str::from_slice_unchecked(std::slice::from_raw_parts( SWKBD_SHARED_MEM.add(swkbd.text_offset as _).cast::(), swkbd.text_length as usize + 1, )) - .unwrap() }; - let text8 = match String::from_utf8(text16.encode_utf8().collect()) { - Ok(s) => s, - Err(_) => { - svcBreak(USERBREAK_PANIC); - unreachable!(); - } - }; + let text8 = text16.to_string(); let mut retmsg = std::ptr::null();