From fd7afb6e3c119002b21cdcfeb6891441702cc883 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 31 Mar 2023 14:03:42 +0200 Subject: [PATCH] Fixed enums in SWKBD --- ctru-rs/examples/camera-image.rs | 2 +- ctru-rs/src/applets/swkbd.rs | 106 +++++++++++++++++++------------ 2 files changed, 68 insertions(+), 40 deletions(-) diff --git a/ctru-rs/examples/camera-image.rs b/ctru-rs/examples/camera-image.rs index a852830..41aa9f3 100644 --- a/ctru-rs/examples/camera-image.rs +++ b/ctru-rs/examples/camera-image.rs @@ -1,5 +1,5 @@ use ctru::prelude::*; -use ctru::services::cam::{Cam, OutputFormat, ShutterSound, ViewSize, Camera}; +use ctru::services::cam::{Cam, Camera, OutputFormat, ShutterSound, ViewSize}; use ctru::services::gfx::Screen; use ctru::services::gspgpu::FramebufferFormat; diff --git a/ctru-rs/src/applets/swkbd.rs b/ctru-rs/src/applets/swkbd.rs index 976bdec..19db774 100644 --- a/ctru-rs/src/applets/swkbd.rs +++ b/ctru-rs/src/applets/swkbd.rs @@ -19,68 +19,72 @@ pub struct Swkbd { /// Western is a text keyboard without japanese symbols (only applies to JPN systems). For other /// systems it's the same as a Normal keyboard. #[derive(Copy, Clone, Debug)] +#[repr(u32)] pub enum Kind { - Normal, - Qwerty, - Numpad, - Western, + Normal = ctru_sys::SWKBD_TYPE_NORMAL, + Qwerty = ctru_sys::SWKBD_TYPE_QWERTY, + Numpad = ctru_sys::SWKBD_TYPE_NUMPAD, + Western = ctru_sys::SWKBD_TYPE_WESTERN, } /// Represents which button the user pressed to close the software keyboard. #[derive(Copy, Clone, Debug)] +#[repr(u32)] pub enum Button { - Left, - Middle, - Right, + Left = ctru_sys::SWKBD_BUTTON_LEFT, + Middle = ctru_sys::SWKBD_BUTTON_MIDDLE, + Right = ctru_sys::SWKBD_BUTTON_RIGHT, } /// Error type for the software keyboard. #[derive(Copy, Clone, Debug)] +#[repr(i32)] pub enum Error { - InvalidInput, - OutOfMem, - HomePressed, - ResetPressed, - PowerPressed, - ParentalOk, - ParentalFail, - BannedInput, + InvalidInput = ctru_sys::SWKBD_INVALID_INPUT, + OutOfMem = ctru_sys::SWKBD_OUTOFMEM, + HomePressed = ctru_sys::SWKBD_HOMEPRESSED, + ResetPressed = ctru_sys::SWKBD_RESETPRESSED, + PowerPressed = ctru_sys::SWKBD_POWERPRESSED, + ParentalOk = ctru_sys::SWKBD_PARENTAL_OK, + ParentalFail = ctru_sys::SWKBD_PARENTAL_FAIL, + BannedInput = ctru_sys::SWKBD_BANNED_INPUT, } /// Restrictions on keyboard input #[derive(Copy, Clone, Debug)] +#[repr(u32)] pub enum ValidInput { - Anything, - NotEmpty, - NotEmptyNotBlank, - NotBlank, - FixedLen, + Anything = ctru_sys::SWKBD_ANYTHING, + NotEmpty = ctru_sys::SWKBD_NOTEMPTY, + NotEmptyNotBlank = ctru_sys::SWKBD_NOTEMPTY_NOTBLANK, + NotBlank = ctru_sys::SWKBD_NOTBLANK, + FixedLen = ctru_sys::SWKBD_FIXEDLEN, } bitflags! { /// Keyboard feature flags pub struct Features: u32 { - const PARENTAL_PIN = 1 << 0; - const DARKEN_TOP_SCREEN = 1 << 1; - const PREDICTIVE_INPUT = 1 << 2; - const MULTILINE = 1 << 3; - const FIXED_WIDTH = 1 << 4; - const ALLOW_HOME = 1 << 5; - const ALLOW_RESET = 1 << 6; - const ALLOW_POWER = 1 << 7; - const DEFAULT_QWERTY = 1 << 8; + const ParentalPin = ctru_sys::SWKBD_PARENTAL; + const DarkenTopScreen = ctru_sys::SWKBD_DARKEN_TOP_SCREEN; + const PredictiveInput = ctru_sys::SWKBD_PREDICTIVE_INPUT; + const Multiline = ctru_sys::SWKBD_MULTILINE; + const FixedWidth = ctru_sys::SWKBD_FIXED_WIDTH; + const AllowHome = ctru_sys::SWKBD_ALLOW_HOME; + const AllowReset = ctru_sys::SWKBD_ALLOW_RESET; + const AllowPower = ctru_sys::SWKBD_ALLOW_POWER; + const DefaultQwerty = ctru_sys::SWKBD_DEFAULT_QWERTY; } } bitflags! { /// Keyboard input filtering flags pub struct Filters: u32 { - const DIGITS = 1 << 0; - const AT = 1 << 1; - const PERCENT = 1 << 2; - const BACKSLASH = 1 << 3; - const PROFANITY = 1 << 4; - const CALLBACK = 1 << 5; + const DIGITS = ctru_sys::SWKBD_FILTER_DIGITS; + const AT = ctru_sys::SWKBD_FILTER_AT; + const PERCENT = ctru_sys::SWKBD_FILTER_PERCENT; + const BACKSLASH = ctru_sys::SWKBD_FILTER_BACKSLASH; + const PROFANITY = ctru_sys::SWKBD_FILTER_PROFANITY; + const CALLBACK = ctru_sys::SWKBD_FILTER_CALLBACK; } } @@ -90,7 +94,7 @@ impl Swkbd { pub fn init(keyboard_type: Kind, num_buttons: i32) -> Self { unsafe { let mut state = Box::::default(); - swkbdInit(state.as_mut(), keyboard_type as u32, num_buttons, -1); + swkbdInit(state.as_mut(), keyboard_type.into(), num_buttons, -1); Swkbd { state } } } @@ -123,7 +127,7 @@ impl Swkbd { /// this software keyboard. /// /// If the buffer is too small to contain the entire sequence received from the keyboard, - /// the output will be truncated but should still be well-formed UTF-8 + /// the output will be truncated but should still be well-formed UTF-8. pub fn get_bytes(&mut self, buf: &mut [u8]) -> Result { unsafe { match swkbdInputText(self.state.as_mut(), buf.as_mut_ptr(), buf.len()) { @@ -143,7 +147,7 @@ impl Swkbd { /// Configures input validation for this keyboard pub fn set_validation(&mut self, validation: ValidInput, filters: Filters) { - self.state.valid_input = validation as i32; + self.state.valid_input = validation.into(); self.state.filter_flags = filters.bits; } @@ -173,7 +177,7 @@ impl Swkbd { let nul_terminated: String = text.chars().chain(once('\0')).collect(); swkbdSetButton( self.state.as_mut(), - button as u32, + button.into(), nul_terminated.as_ptr(), submit, ); @@ -210,3 +214,27 @@ impl Default for Swkbd { Swkbd::init(Kind::Normal, 2) } } + +impl From for u32 { + fn from(value: Kind) -> Self { + value as u32 + } +} + +impl From