Skip to content

Commit

Permalink
Simplify string conversion code
Browse files Browse the repository at this point in the history
  • Loading branch information
FenrirWolf committed Feb 14, 2024
1 parent 07bdd5b commit ab8d86d
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions ctru-rs/src/applets/swkbd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -279,7 +279,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)),
Expand Down Expand Up @@ -620,7 +620,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<u8>) -> 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,
Expand Down Expand Up @@ -704,11 +704,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::<u16>();

Expand Down Expand Up @@ -792,17 +792,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::<u16>(),
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 {
Expand Down Expand Up @@ -849,20 +848,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::<u16>(),
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();

Expand All @@ -873,6 +865,15 @@ impl SoftwareKeyboard {
text8.len(),
) as _;

extra.callback.map(|cb| {

Check warning on line 868 in ctru-rs/src/applets/swkbd.rs

View workflow job for this annotation

GitHub Actions / lint (nightly-2023-06-01)

called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
swkbd.callback_result = cb(
extra.callback_user,
&mut retmsg,
text8.as_ptr(),
text8.len(),
) as _
});

let retmsg = if !retmsg.is_null() {
unsafe {
let len = libc::strlen(retmsg);
Expand Down

0 comments on commit ab8d86d

Please sign in to comment.