Skip to content

Commit

Permalink
Still thinking about the filter callbacks...
Browse files Browse the repository at this point in the history
  • Loading branch information
Meziu committed Dec 28, 2023
1 parent fa613c2 commit 815aa61
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 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, SwkbdState};

use bitflags::bitflags;
use libc;
use std::ffi::CStr;
use std::ffi::{CStr, CString};
use std::fmt::Display;
use std::iter::once;
use std::str;
Expand Down Expand Up @@ -199,10 +199,6 @@ bitflags! {
const BACKSLASH = ctru_sys::SWKBD_FILTER_BACKSLASH;
/// Disallow the use of profanity via Nintendo's profanity filter.
const PROFANITY = ctru_sys::SWKBD_FILTER_PROFANITY;
/// Use a custom callback in order to filter the input.
///
/// TODO: It's currently impossible to setup a custom filter callback.
const CALLBACK = ctru_sys::SWKBD_FILTER_CALLBACK;
}
}

Expand Down Expand Up @@ -378,26 +374,52 @@ impl SoftwareKeyboard {
///
/// # Notes

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

View workflow job for this annotation

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

Diff in /__w/ctru-rs/ctru-rs/ctru-rs/src/applets/swkbd.rs
///
/// This function will overwrite any currently set filter configuration.
/// The filter callback will work only for the next time the keyboard is used. After using it once, it must be set again.
///
/// # Example
///
/// ```
/// # let _runner = test_runner::GdbRunner::default();
/// # fn main() {
/// #

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

View workflow job for this annotation

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

Diff in /__w/ctru-rs/ctru-rs/ctru-rs/src/applets/swkbd.rs
/// use ctru::applets::swkbd::{SoftwareKeyboard, CallbackInput};
/// let mut keyboard = SoftwareKeyboard::default();
///
/// keyboard.set_filter_callback(|text| {
/// if text.contains("boo") {
/// println!("Ah, you scared me!");
/// }

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

View workflow job for this annotation

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

Diff in /__w/ctru-rs/ctru-rs/ctru-rs/src/applets/swkbd.rs
///
/// (CallbackResult::Ok, None)
/// });
/// #
/// # }
pub fn set_filter_callback<F>(&mut self, callback: F)
where
F: FnOnce(&str) -> CallbackResult,
F: FnOnce(&str) -> (CallbackResult, Option<CString>),
{
unsafe extern "C" fn internal_callback<F>(
user: *mut libc::c_void,
_pp_message: *mut *const libc::c_char,
pp_message: *mut *const libc::c_char,
text: *const libc::c_char,
_text_size: libc::size_t,
) -> ctru_sys::SwkbdCallbackResult
where
F: FnOnce(&str) -> CallbackResult,
F: FnOnce(&str) -> (CallbackResult, Option<CString>),
{
let closure = Box::from_raw(user as *mut Box<F>);

let text = CStr::from_ptr(text);
let text_slice: &str = text.to_str().unwrap();

closure(text_slice).into()
let result = closure(text_slice);

if let Some(cstr) = result.1 {
*pp_message = cstr.as_ptr();
Box::leak(Box::new(cstr)); // Definitely SHOULD NOT do this, but as far as design goes, it's clean.
}

result.0.into()
}

let boxed_callback = Box::new(Box::new(callback));
Expand Down Expand Up @@ -511,10 +533,10 @@ impl SoftwareKeyboard {
/// Set the 2 custom characters to add to the keyboard while using [`Kind::Numpad`].

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

View workflow job for this annotation

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

Diff in /__w/ctru-rs/ctru-rs/ctru-rs/src/applets/swkbd.rs
///
/// These characters will appear in their own buttons right next to the `0` key.
///
///
/// # Notes
///
/// You can set one or both of these keys to `NUL` (value 0) to avoid showing the additional buttons to the user.
///
/// If `None` is passed as either key, that button will not be shown to the user.
///
/// # Example
///
Expand All @@ -525,13 +547,26 @@ impl SoftwareKeyboard {
/// use ctru::applets::swkbd::{SoftwareKeyboard, Kind, ButtonConfig};
/// let mut keyboard = SoftwareKeyboard::new(Kind::Numpad, ButtonConfig::LeftRight);

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

View workflow job for this annotation

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

Diff in /__w/ctru-rs/ctru-rs/ctru-rs/src/applets/swkbd.rs
///
/// keyboard.set_numpad_keys(('#', '.'));
/// keyboard.set_numpad_keys(Some('#'), Some('.'));
///
/// // The right numpad key will not be shown.
/// keyboard.set_numpad_keys(Some('!'), None);
/// #
/// # }
#[doc(alias = "swkbdSetNumpadKeys")]
pub fn set_numpad_keys(&mut self, keys: (char, char)) {
pub fn set_numpad_keys(&mut self, left_key: Option<char>, right_key: Option<char>) {
let mut keys = (0, 0);

if let Some(k) = left_key {
keys.0 = k as i32;
}

if let Some(k) = right_key {
keys.1 = k as i32;
}

unsafe {
ctru_sys::swkbdSetNumpadKeys(self.state.as_mut(), keys.0 as i32, keys.1 as i32);
ctru_sys::swkbdSetNumpadKeys(self.state.as_mut(), keys.0, keys.1);
}
}

Expand Down

0 comments on commit 815aa61

Please sign in to comment.