Browse Source

Revert swkbd changes

pull/107/head
Andrea Ciliberti 2 years ago
parent
commit
6a5b01d1ad
  1. 11
      ctru-rs/examples/file-explorer.rs
  2. 11
      ctru-rs/examples/software-keyboard.rs
  3. 16
      ctru-rs/src/applets/swkbd.rs

11
ctru-rs/examples/file-explorer.rs

@ -162,16 +162,17 @@ impl<'a> FileExplorer<'a> {
fn get_input_and_run(&mut self, action: impl FnOnce(&mut Self, String)) { fn get_input_and_run(&mut self, action: impl FnOnce(&mut Self, String)) {
let mut keyboard = Swkbd::default(); let mut keyboard = Swkbd::default();
let mut new_path_str = String::new();
match keyboard.write_to_string() { match keyboard.write_to_string(&mut new_path_str) {
Ok((path, Button::Right)) => { Ok(Button::Right) => {
// Clicked "OK" // Clicked "OK"
action(self, path); action(self, new_path_str);
} }
Ok((_, Button::Left)) => { Ok(Button::Left) => {
// Clicked "Cancel" // Clicked "Cancel"
} }
Ok((_, Button::Middle)) => { Ok(Button::Middle) => {
// This button wasn't shown // This button wasn't shown
unreachable!() unreachable!()
} }

11
ctru-rs/examples/software-keyboard.rs

@ -24,12 +24,15 @@ fn main() {
// configurations. // configurations.
let mut keyboard = Swkbd::default(); let mut keyboard = Swkbd::default();
// String used to store text received from the keyboard
let mut text = String::new();
// Raise the software keyboard. You can perform different actions depending on which // Raise the software keyboard. You can perform different actions depending on which
// software button the user pressed // software button the user pressed
match keyboard.write_to_string() { match keyboard.write_to_string(&mut text) {
Ok((text, Button::Right)) => println!("You entered: {text}"), Ok(Button::Right) => println!("You entered: {text}"),
Ok((_, Button::Left)) => println!("Cancelled"), Ok(Button::Left) => println!("Cancelled"),
Ok((_, Button::Middle)) => println!("How did you even press this?"), Ok(Button::Middle) => println!("How did you even press this?"),
Err(_) => println!("Oh noes, an error happened!"), Err(_) => println!("Oh noes, an error happened!"),
} }
} }

16
ctru-rs/src/applets/swkbd.rs

@ -2,6 +2,7 @@ use bitflags::bitflags;
use ctru_sys::{ use ctru_sys::{
self, swkbdInit, swkbdInputText, swkbdSetButton, swkbdSetFeatures, swkbdSetHintText, SwkbdState, self, swkbdInit, swkbdInputText, swkbdSetButton, swkbdSetFeatures, swkbdSetHintText, SwkbdState,
}; };
use libc;
use std::iter::once; use std::iter::once;
use std::str; use std::str;
@ -100,21 +101,24 @@ impl Swkbd {
/// ///
/// The text received from the keyboard will be truncated if it is greater than 2048 bytes /// The text received from the keyboard will be truncated if it is greater than 2048 bytes
/// in length. /// in length.
pub fn write_to_string(&mut self) -> Result<(String, Button), Error> { pub fn write_to_string(&mut self, buf: &mut String) -> Result<Button, Error> {
// Unfortunately the libctru API doesn't really provide a way to get the exact length // Unfortunately the libctru API doesn't really provide a way to get the exact length
// of the string that it receieves from the software keyboard. Instead it expects you // of the string that it receieves from the software keyboard. Instead it expects you
// to pass in a buffer and hope that it's big enough to fit the entire string, so // to pass in a buffer and hope that it's big enough to fit the entire string, so
// you have to set some upper limit on the potential size of the user's input. // you have to set some upper limit on the potential size of the user's input.
const MAX_BYTES: usize = 2048; const MAX_BYTES: usize = 2048;
let mut buf = vec![0u8; MAX_BYTES]; let mut tmp = [0u8; MAX_BYTES];
let button = self.write_bytes(&mut buf)?; let button = self.write_bytes(&mut tmp)?;
// libctru does, however, seem to ensure that the buffer will always contain a properly // libctru does, however, seem to ensure that the buffer will always contain a properly
// terminated UTF-8 sequence even if the input has to be truncated, so this operation // terminated UTF-8 sequence even if the input has to be truncated, so these operations
// should be safe. // should be safe.
let res = String::from_utf8(buf).unwrap(); let len = unsafe { libc::strlen(tmp.as_ptr()) };
let utf8 = unsafe { str::from_utf8_unchecked(&tmp[..len]) };
Ok((res, button)) // Copy the input into the user's `String`
*buf += utf8;
Ok(button)
} }
/// Fills the provided buffer with a UTF-8 encoded, NUL-terminated sequence of bytes from /// Fills the provided buffer with a UTF-8 encoded, NUL-terminated sequence of bytes from

Loading…
Cancel
Save