Skip to content

Commit

Permalink
Simplify some code + rename vars
Browse files Browse the repository at this point in the history
  • Loading branch information
FenrirWolf committed Feb 14, 2024
1 parent 9f3d5d8 commit e593abd
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions ctru-rs/src/applets/swkbd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ctru_sys::{
self, aptLaunchLibraryApplet, aptSetMessageCallback, envGetAptAppId, svcCloseHandle,
svcCreateMemoryBlock, APT_SendParameter, SwkbdButton, SwkbdDictWord, SwkbdExtra,
SwkbdLearningData, SwkbdState, SwkbdStatusData, APPID_SOFTWARE_KEYBOARD, APTCMD_MESSAGE,
NS_APPID, SWKBD_CALLBACK_OK,
NS_APPID,
};

use bitflags::bitflags;
Expand Down Expand Up @@ -712,10 +712,10 @@ impl SoftwareKeyboard {
.take(swkbd.max_text_len as _)
.chain(once(0));

let mut initial_text_cursor = SWKBD_SHARED_MEM.cast::<u16>();
let mut initial_text_cursor = SWKBD_SHARED_MEM.cast();

for ch in utf16_iter {
*initial_text_cursor = ch;
for code_point in utf16_iter {
*initial_text_cursor = code_point;
initial_text_cursor = initial_text_cursor.add(1);
}
}
Expand Down Expand Up @@ -849,7 +849,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::<u16>(),
SWKBD_SHARED_MEM.add(swkbd.text_offset as _).cast(),
swkbd.text_length as usize + 1,
))
};
Expand All @@ -869,25 +869,21 @@ impl SoftwareKeyboard {

let retmsg = if !retmsg.is_null() {
unsafe {
let len = libc::strlen(retmsg);
std::str::from_utf8_unchecked(std::slice::from_raw_parts(retmsg, len + 1))
let len = libc::strlen(retmsg) + 1;
std::str::from_utf8_unchecked(std::slice::from_raw_parts(retmsg, len))
}
} else {
"\0"
};

let callback_msg = &mut swkbd.callback_msg;

if swkbd.callback_result > SWKBD_CALLBACK_OK as _ {
for (idx, ch) in retmsg
.encode_utf16()
.take(callback_msg.len() - 1)
.enumerate()
{
callback_msg[idx] = ch;
}
} else {
callback_msg[0] = 0;
for (idx, code_point) in retmsg
.encode_utf16()
.take(callback_msg.len() - 1)
.enumerate()
{
callback_msg[idx] = code_point;
}

let _ = APT_SendParameter(
Expand Down

0 comments on commit e593abd

Please sign in to comment.