Browse Source

Fix todo and start working on swkbd

pull/149/head
Andrea Ciliberti 1 year ago
parent
commit
7a86507182
  1. 12
      ctru-rs/examples/software-keyboard.rs
  2. 9
      ctru-rs/src/applets/mii_selector.rs
  3. 15
      ctru-rs/src/applets/swkbd.rs

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

@ -11,6 +11,11 @@ fn main() { @@ -11,6 +11,11 @@ fn main() {
let gfx = Gfx::new().unwrap();
let _console = Console::new(gfx.top_screen.borrow_mut());
// Prepares a software keyboard with two buttons: one to cancel input and one
// to accept it. You can also use `SoftwareKeyboard::new()` to launch the keyboard
// with different configurations.
let mut keyboard = SoftwareKeyboard::default();
println!("Press A to enter some text or press Start to exit.");
while apt.main_loop() {
@ -22,14 +27,9 @@ fn main() { @@ -22,14 +27,9 @@ fn main() {
// Check if the user request to write some input.
if hid.keys_down().contains(KeyPad::A) {
// Prepares a software keyboard with two buttons: One to cancel input and one
// to accept it. You can also use `SoftwareKeyboard::new()` to launch the keyboard in different
// configurations.
let mut keyboard = SoftwareKeyboard::default();
// Raise the software keyboard. You can perform different actions depending on which
// software button the user pressed.
match keyboard.get_string(2048) {
match keyboard.get_string(2048, &apt, &gfx) {
Ok((text, Button::Right)) => println!("You entered: {text}"),
Ok((_, Button::Left)) => println!("Cancelled"),
Ok((_, Button::Middle)) => println!("How did you even press this?"),

9
ctru-rs/src/applets/mii_selector.rs

@ -4,6 +4,8 @@ @@ -4,6 +4,8 @@
//! The selected Mii is readable as a [`Mii`].
use crate::mii::Mii;
use crate::services::{apt::Apt, gfx::Gfx};
use bitflags::bitflags;
use std::{ffi::CString, fmt};
@ -252,9 +254,8 @@ impl MiiSelector { @@ -252,9 +254,8 @@ impl MiiSelector {
/// Launch the Mii Selector.
///
/// Depending on the configuration, the Mii Selector window will appear either on the bottom screen (default behaviour) or the top screen (see [`Options::USE_TOP_SCREEN`]).
///
/// TODO: UNSAFE OPERATION, LAUNCHING APPLETS REQUIRES GRAPHICS, WITHOUT AN ACTIVE GFX THIS WILL CAUSE A SEGMENTATION FAULT.
/// Depending on the configuration, the Mii Selector window will appear either
/// on the bottom screen (default behaviour) or the top screen (see [`Options::USE_TOP_SCREEN`]).
///
/// # Example
///
@ -276,7 +277,7 @@ impl MiiSelector { @@ -276,7 +277,7 @@ impl MiiSelector {
/// # }
/// ```
#[doc(alias = "miiSelectorLaunch")]
pub fn launch(&mut self) -> Result<Selection, Error> {
pub fn launch(&mut self, apt: &Apt, gfx: &Gfx) -> Result<Selection, Error> {
let mut return_val = Box::<ctru_sys::MiiSelectorReturn>::default();
unsafe { ctru_sys::miiSelectorLaunch(self.config.as_mut(), return_val.as_mut()) }

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

@ -5,11 +5,13 @@ @@ -5,11 +5,13 @@
// TODO: Split the Parental PIN lock operations into a different type.
#![doc(alias = "keyboard")]
use bitflags::bitflags;
use ctru_sys::{
self, swkbdInit, swkbdInputText, swkbdSetButton, swkbdSetFeatures, swkbdSetHintText,
swkbdSetInitialText, SwkbdState,
};
use crate::services::{apt::Apt, gfx::Gfx};
use bitflags::bitflags;
use libc;
use std::fmt::Display;
use std::iter::once;
@ -188,8 +190,7 @@ impl SoftwareKeyboard { @@ -188,8 +190,7 @@ impl SoftwareKeyboard {
/// # Notes
///
/// The text received from the keyboard will be truncated if it is longer than `max_bytes`.
///
/// TODO: UNSAFE OPERATION, LAUNCHING APPLETS REQUIRES GRAPHICS, WITHOUT AN ACTIVE GFX THIS WILL CAUSE A SEGMENTATION FAULT.
/// Use [`SoftwareKeyboard::set_max_text_len()`] to make sure the buffer can contain the input text.
///
/// # Example
///
@ -207,13 +208,13 @@ impl SoftwareKeyboard { @@ -207,13 +208,13 @@ impl SoftwareKeyboard {
/// # }
/// ```
#[doc(alias = "swkbdInputText")]
pub fn get_string(&mut self, max_bytes: usize) -> Result<(String, Button), Error> {
pub fn get_string(&mut self, max_bytes: usize, apt: &Apt, gfx: &Gfx) -> Result<(String, Button), Error> {
// 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
// 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.
let mut tmp = vec![0u8; max_bytes];
let button = self.write_exact(&mut tmp)?;
let button = self.write_exact(&mut tmp, apt, gfx)?;
// 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 these operations
@ -234,8 +235,6 @@ impl SoftwareKeyboard { @@ -234,8 +235,6 @@ impl SoftwareKeyboard {
/// If the buffer is too small to contain the entire sequence received from the keyboard,
/// the output will be truncated.
///
/// TODO: UNSAFE OPERATION, LAUNCHING APPLETS REQUIRES GRAPHICS, WITHOUT AN ACTIVE GFX THIS WILL CAUSE A SEGMENTATION FAULT.
///
/// # Example
///
/// ```
@ -254,7 +253,7 @@ impl SoftwareKeyboard { @@ -254,7 +253,7 @@ impl SoftwareKeyboard {
/// # }
/// ```
#[doc(alias = "swkbdInputText")]
pub fn write_exact(&mut self, buf: &mut [u8]) -> Result<Button, Error> {
pub fn write_exact(&mut self, buf: &mut [u8], apt: &Apt, gfx: &Gfx) -> Result<Button, Error> {
unsafe {
match swkbdInputText(self.state.as_mut(), buf.as_mut_ptr(), buf.len()) {
ctru_sys::SWKBD_BUTTON_NONE => Err(self.parse_swkbd_error()),

Loading…
Cancel
Save