From e371addd66e807a7688904b9da29c7c30b3e9451 Mon Sep 17 00:00:00 2001 From: TechiePi Date: Sun, 10 Jul 2022 19:34:19 +0200 Subject: [PATCH 01/13] Initial CFGU implementation --- ctru-rs/src/services/cfgu.rs | 224 +++++++++++++++++++++++++++++++++++ ctru-rs/src/services/mod.rs | 1 + 2 files changed, 225 insertions(+) create mode 100644 ctru-rs/src/services/cfgu.rs diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs new file mode 100644 index 0000000..0165811 --- /dev/null +++ b/ctru-rs/src/services/cfgu.rs @@ -0,0 +1,224 @@ +//! Configuration service +//! +//! This module contains basic methods to retrieve and change configuration from the console. + +#[repr(u8)] +pub enum Region { + Japan = 0, + Usa = 1, + Europe = 2, + Australia = 3, + China = 4, + Korea = 5, + Taiwan = 6, +} + +#[repr(u8)] +pub enum Language { + Japan = 0, + English = 1, + French = 2, + German = 3, + Italian = 4, + Spanish = 5, + SimpChinese = 6, + Korean = 7, + Dutch = 8, + Portuguese = 9, + Russian = 10, + TradChinese = 11, +} + +#[repr(u8)] +pub enum SystemModel { + Model3DS = 0, + Model3DSXL = 1, + ModelNew3DS = 2, + Model2DS = 3, + ModelNew3DSXL = 4, + ModelNew2DSXL = 5, +} + +/// Represents the configuration service. No actions can be performed +/// until an instance of this struct is created. +/// +/// The service exits when all instances of this struct go out of scope. +pub struct Cfgu(()); + +impl Cfgu { + /// Initializes the CFGU service. + /// + /// # Errors + /// + /// This function will return Err if there was an error initializing the + /// CFGU service. + /// + /// ctrulib services are reference counted, so this function may be called + /// as many times as desired and the service will not exit until all + /// instances of Fs drop out of scope. + pub fn init() -> crate::Result { + unsafe { + let r = ctru_sys::cfguInit(); + if r < 0 { + Err(r.into()) + } else { + Ok(Cfgu(())) + } + } + } + + /// Gets system region from secure info + pub fn get_region(&self) -> crate::Result { + let mut region: u8 = 0; + let region_pointer: *mut u8 = &mut region; + + unsafe { + let r = ctru_sys::CFGU_SecureInfoGetRegion(region_pointer); + if r < 0 { + Err(r.into()) + } else { + // The system shouldn't give an invalid value + Ok(Region::try_from(region).unwrap()) + } + } + } + + /// Gets system's model + pub fn get_system_model(&self) -> crate::Result { + let mut model: u8 = 0; + let model_pointer: *mut u8 = &mut model; + + unsafe { + let r = ctru_sys::CFGU_GetSystemModel(model_pointer); + if r < 0 { + Err(r.into()) + } else { + // The system shouldn't give an invalid value + Ok(SystemModel::try_from(model).unwrap()) + } + } + } + + /// Gets system's language + pub fn get_system_language(&self) -> crate::Result { + let mut language: u8 = 0; + let language_pointer: *mut u8 = &mut language; + + unsafe { + let r = ctru_sys::CFGU_GetSystemLanguage(language_pointer); + if r < 0 { + Err(r.into()) + } else { + // The system shouldn't give an invalid value + Ok(Language::try_from(language).unwrap()) + } + } + } + + /// Checks if NFC is supported by the console + pub fn is_nfc_supported(&self) -> crate::Result { + let mut supported: bool = false; + let supported_pointer: *mut bool = &mut supported; + + unsafe { + let r = ctru_sys::CFGU_IsNFCSupported(supported_pointer); + if r < 0 { + Err(r.into()) + } else { + Ok(supported) + } + } + } + + /// Check if the console is 2DS-like (2DS, New2DS, New2DSXL) + pub fn is_2ds_like(&self) -> crate::Result { + let mut is_2ds_like: u8 = 0; + let is_2ds_like_pointer: *mut u8 = &mut is_2ds_like; + + unsafe { + let r = ctru_sys::CFGU_GetModelNintendo2DS(is_2ds_like_pointer); + if r < 0 { + Err(r.into()) + } else { + Ok(is_2ds_like < 1) + } + } + } +} + +impl Drop for Cfgu { + fn drop(&mut self) { + unsafe { + ctru_sys::cfguExit(); + } + } +} + +macro_rules! from_type_to_u8 { + ($from_type:ty) => { + impl From<$from_type> for u8 { + fn from(v: $from_type) -> Self { + v as u8 + } + } + } +} + +from_type_to_u8!(Region); +from_type_to_u8!(Language); +from_type_to_u8!(SystemModel); + +impl TryFrom for Region { + type Error = (); + + fn try_from(value: u8) -> Result { + match value { + 0 => Ok(Region::Japan), + 1 => Ok(Region::Usa), + 2 => Ok(Region::Europe), + 3 => Ok(Region::Australia), + 4 => Ok(Region::China), + 5 => Ok(Region::Korea), + 6 => Ok(Region::Taiwan), + _ => Err(()) + } + } +} + +impl TryFrom for Language { + type Error = (); + + fn try_from(value: u8) -> Result { + match value { + 0 => Ok(Language::Japan), + 1 => Ok(Language::English), + 2 => Ok(Language::French), + 3 => Ok(Language::German), + 4 => Ok(Language::Italian), + 5 => Ok(Language::Spanish), + 6 => Ok(Language::SimpChinese), + 7 => Ok(Language::Korean), + 8 => Ok(Language::Dutch), + 9 => Ok(Language::Portuguese), + 10 => Ok(Language::Russian), + 11 => Ok(Language::TradChinese), + _ => Err(()) + } + } +} + +impl TryFrom for SystemModel { + type Error = (); + + fn try_from(value: u8) -> Result { + match value { + 0 => Ok(SystemModel::Model3DS), + 1 => Ok(SystemModel::Model3DSXL), + 2 => Ok(SystemModel::ModelNew3DS), + 3 => Ok(SystemModel::Model2DS), + 4 => Ok(SystemModel::ModelNew3DSXL), + 5 => Ok(SystemModel::ModelNew2DSXL), + _ => Err(()) + } + } +} \ No newline at end of file diff --git a/ctru-rs/src/services/mod.rs b/ctru-rs/src/services/mod.rs index 0dc750c..8e14bb1 100644 --- a/ctru-rs/src/services/mod.rs +++ b/ctru-rs/src/services/mod.rs @@ -1,4 +1,5 @@ pub mod apt; +pub mod cfgu; pub mod fs; pub mod gspgpu; pub mod hid; From 650ba42b35f96950fd5761ed31d05c9a5f6f6144 Mon Sep 17 00:00:00 2001 From: TechiePi Date: Sun, 10 Jul 2022 19:43:56 +0200 Subject: [PATCH 02/13] Implement debug and make function names consistent --- ctru-rs/src/services/cfgu.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index 0165811..acdb36d 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -2,6 +2,7 @@ //! //! This module contains basic methods to retrieve and change configuration from the console. +#[derive(Debug)] #[repr(u8)] pub enum Region { Japan = 0, @@ -13,6 +14,7 @@ pub enum Region { Taiwan = 6, } +#[derive(Debug)] #[repr(u8)] pub enum Language { Japan = 0, @@ -29,6 +31,7 @@ pub enum Language { TradChinese = 11, } +#[derive(Debug)] #[repr(u8)] pub enum SystemModel { Model3DS = 0, @@ -84,7 +87,7 @@ impl Cfgu { } /// Gets system's model - pub fn get_system_model(&self) -> crate::Result { + pub fn get_model(&self) -> crate::Result { let mut model: u8 = 0; let model_pointer: *mut u8 = &mut model; @@ -100,7 +103,7 @@ impl Cfgu { } /// Gets system's language - pub fn get_system_language(&self) -> crate::Result { + pub fn get_language(&self) -> crate::Result { let mut language: u8 = 0; let language_pointer: *mut u8 = &mut language; From b8618907e56cd92001033843e10b1e036716661a Mon Sep 17 00:00:00 2001 From: TechiePi Date: Sun, 10 Jul 2022 19:51:24 +0200 Subject: [PATCH 03/13] Add example --- .../examples/system-language-region-model.rs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 ctru-rs/examples/system-language-region-model.rs diff --git a/ctru-rs/examples/system-language-region-model.rs b/ctru-rs/examples/system-language-region-model.rs new file mode 100644 index 0000000..e2a9eae --- /dev/null +++ b/ctru-rs/examples/system-language-region-model.rs @@ -0,0 +1,34 @@ +use ctru::console::Console; +use ctru::gfx::Gfx; +use ctru::services::apt::Apt; +use ctru::services::hid::{Hid, KeyPad}; +use ctru::services::cfgu::Cfgu; + +fn main() { + ctru::init(); + let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); + let hid = Hid::init().expect("Couldn't obtain HID controller"); + let apt = Apt::init().expect("Couldn't obtain APT controller"); + let cfgu = Cfgu::init().expect("Couldn't obtain CFGU controller"); + let _console = Console::init(gfx.top_screen.borrow_mut()); + + println!("\x1b[0;0H{}", format!("Region: {:?}", cfgu.get_region().unwrap())); + println!("\x1b[0;10H{}", format!("Language: {:?}", cfgu.get_language().unwrap())); + println!("\x1b[0;20H{}", format!("Model: {:?}", cfgu.get_model().unwrap())); + + // Main loop + while apt.main_loop() { + //Scan all the inputs. This should be done once for each frame + hid.scan_input(); + + if hid.keys_down().contains(KeyPad::KEY_START) { + break; + } + // Flush and swap framebuffers + gfx.flush_buffers(); + gfx.swap_buffers(); + + //Wait for VBlank + gfx.wait_for_vblank(); + } +} From 53d35dd7d9371849f4f7ecd7eea033d7b9a1d8aa Mon Sep 17 00:00:00 2001 From: TechiePi Date: Sun, 10 Jul 2022 19:56:44 +0200 Subject: [PATCH 04/13] Fix example --- ctru-rs/examples/system-language-region-model.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ctru-rs/examples/system-language-region-model.rs b/ctru-rs/examples/system-language-region-model.rs index e2a9eae..47e70c6 100644 --- a/ctru-rs/examples/system-language-region-model.rs +++ b/ctru-rs/examples/system-language-region-model.rs @@ -13,8 +13,8 @@ fn main() { let _console = Console::init(gfx.top_screen.borrow_mut()); println!("\x1b[0;0H{}", format!("Region: {:?}", cfgu.get_region().unwrap())); - println!("\x1b[0;10H{}", format!("Language: {:?}", cfgu.get_language().unwrap())); - println!("\x1b[0;20H{}", format!("Model: {:?}", cfgu.get_model().unwrap())); + println!("\x1b[10;0H{}", format!("Language: {:?}", cfgu.get_language().unwrap())); + println!("\x1b[20;0H{}", format!("Model: {:?}", cfgu.get_model().unwrap())); // Main loop while apt.main_loop() { From 358e2bca4638202f98560575333e1777a54db916 Mon Sep 17 00:00:00 2001 From: TechiePi Date: Sun, 10 Jul 2022 22:08:38 +0200 Subject: [PATCH 05/13] Apply suggestions --- ctru-rs/src/services/cfgu.rs | 106 +++++++++++++++++------------------ 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index acdb36d..83b78d8 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -5,41 +5,41 @@ #[derive(Debug)] #[repr(u8)] pub enum Region { - Japan = 0, - Usa = 1, - Europe = 2, - Australia = 3, - China = 4, - Korea = 5, - Taiwan = 6, + Japan = ctru_sys::CFG_REGION_JPN as u8, + Usa = ctru_sys::CFG_REGION_USA as u8, + Europe = ctru_sys::CFG_REGION_EUR as u8, + Australia = ctru_sys::CFG_REGION_AUS as u8, + China = ctru_sys::CFG_REGION_CHN as u8, + Korea = ctru_sys::CFG_REGION_KOR as u8, + Taiwan = ctru_sys::CFG_REGION_TWN as u8, } #[derive(Debug)] #[repr(u8)] pub enum Language { - Japan = 0, - English = 1, - French = 2, - German = 3, - Italian = 4, - Spanish = 5, - SimpChinese = 6, - Korean = 7, - Dutch = 8, - Portuguese = 9, - Russian = 10, - TradChinese = 11, + Japan = ctru_sys::CFG_LANGUAGE_JP as u8, + English = ctru_sys::CFG_LANGUAGE_EN as u8, + French = ctru_sys::CFG_LANGUAGE_FR as u8, + German = ctru_sys::CFG_LANGUAGE_DE as u8, + Italian = ctru_sys::CFG_LANGUAGE_IT as u8, + Spanish = ctru_sys::CFG_LANGUAGE_ES as u8, + SimpChinese = ctru_sys::CFG_LANGUAGE_ZH as u8, + Korean = ctru_sys::CFG_LANGUAGE_KO as u8, + Dutch = ctru_sys::CFG_LANGUAGE_NL as u8, + Portuguese = ctru_sys::CFG_LANGUAGE_PT as u8, + Russian = ctru_sys::CFG_LANGUAGE_RU as u8, + TradChinese = ctru_sys::CFG_LANGUAGE_TW as u8, } #[derive(Debug)] #[repr(u8)] pub enum SystemModel { - Model3DS = 0, - Model3DSXL = 1, - ModelNew3DS = 2, - Model2DS = 3, - ModelNew3DSXL = 4, - ModelNew2DSXL = 5, + Model3DS = ctru_sys::CFG_MODEL_3DS as u8, + Model3DSXL = ctru_sys::CFG_MODEL_3DSXL as u8, + ModelNew3DS = ctru_sys::CFG_MODEL_N3DS as u8, + Model2DS = ctru_sys::CFG_MODEL_2DS as u8, + ModelNew3DSXL = ctru_sys::CFG_MODEL_N3DSXL as u8, + ModelNew2DSXL = ctru_sys::CFG_MODEL_N2DSXL as u8, } /// Represents the configuration service. No actions can be performed @@ -175,14 +175,14 @@ impl TryFrom for Region { type Error = (); fn try_from(value: u8) -> Result { - match value { - 0 => Ok(Region::Japan), - 1 => Ok(Region::Usa), - 2 => Ok(Region::Europe), - 3 => Ok(Region::Australia), - 4 => Ok(Region::China), - 5 => Ok(Region::Korea), - 6 => Ok(Region::Taiwan), + match value as u32 { + ctru_sys::CFG_REGION_JPN => Ok(Region::Japan), + ctru_sys::CFG_REGION_USA => Ok(Region::Usa), + ctru_sys::CFG_REGION_EUR => Ok(Region::Europe), + ctru_sys::CFG_REGION_AUS => Ok(Region::Australia), + ctru_sys::CFG_REGION_CHN => Ok(Region::China), + ctru_sys::CFG_REGION_KOR => Ok(Region::Korea), + ctru_sys::CFG_REGION_TWN => Ok(Region::Taiwan), _ => Err(()) } } @@ -192,19 +192,19 @@ impl TryFrom for Language { type Error = (); fn try_from(value: u8) -> Result { - match value { - 0 => Ok(Language::Japan), - 1 => Ok(Language::English), - 2 => Ok(Language::French), - 3 => Ok(Language::German), - 4 => Ok(Language::Italian), - 5 => Ok(Language::Spanish), - 6 => Ok(Language::SimpChinese), - 7 => Ok(Language::Korean), - 8 => Ok(Language::Dutch), - 9 => Ok(Language::Portuguese), - 10 => Ok(Language::Russian), - 11 => Ok(Language::TradChinese), + match value as u32 { + ctru_sys::CFG_LANGUAGE_JP => Ok(Language::Japan), + ctru_sys::CFG_LANGUAGE_EN => Ok(Language::English), + ctru_sys::CFG_LANGUAGE_FR => Ok(Language::French), + ctru_sys::CFG_LANGUAGE_DE => Ok(Language::German), + ctru_sys::CFG_LANGUAGE_IT => Ok(Language::Italian), + ctru_sys::CFG_LANGUAGE_ES => Ok(Language::Spanish), + ctru_sys::CFG_LANGUAGE_ZH => Ok(Language::SimpChinese), + ctru_sys::CFG_LANGUAGE_KO => Ok(Language::Korean), + ctru_sys::CFG_LANGUAGE_NL => Ok(Language::Dutch), + ctru_sys::CFG_LANGUAGE_PT => Ok(Language::Portuguese), + ctru_sys::CFG_LANGUAGE_RU => Ok(Language::Russian), + ctru_sys::CFG_LANGUAGE_TW => Ok(Language::TradChinese), _ => Err(()) } } @@ -214,13 +214,13 @@ impl TryFrom for SystemModel { type Error = (); fn try_from(value: u8) -> Result { - match value { - 0 => Ok(SystemModel::Model3DS), - 1 => Ok(SystemModel::Model3DSXL), - 2 => Ok(SystemModel::ModelNew3DS), - 3 => Ok(SystemModel::Model2DS), - 4 => Ok(SystemModel::ModelNew3DSXL), - 5 => Ok(SystemModel::ModelNew2DSXL), + match value as u32 { + ctru_sys::CFG_MODEL_3DS => Ok(SystemModel::Model3DS), + ctru_sys::CFG_MODEL_3DSXL => Ok(SystemModel::Model3DSXL), + ctru_sys::CFG_MODEL_N3DS => Ok(SystemModel::ModelNew3DS), + ctru_sys::CFG_MODEL_2DS => Ok(SystemModel::Model2DS), + ctru_sys::CFG_MODEL_N3DSXL => Ok(SystemModel::ModelNew3DSXL), + ctru_sys::CFG_MODEL_N2DSXL => Ok(SystemModel::ModelNew2DSXL), _ => Err(()) } } From 149ab391d56e7edb462ff9404c87e49c6ee68253 Mon Sep 17 00:00:00 2001 From: TechiePi Date: Sun, 10 Jul 2022 22:11:38 +0200 Subject: [PATCH 06/13] Apply suggestions (II) --- ctru-rs/src/services/cfgu.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index 83b78d8..9d794cb 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -133,17 +133,17 @@ impl Cfgu { } } - /// Check if the console is 2DS-like (2DS, New2DS, New2DSXL) - pub fn is_2ds_like(&self) -> crate::Result { - let mut is_2ds_like: u8 = 0; - let is_2ds_like_pointer: *mut u8 = &mut is_2ds_like; + /// Check if the console is from the 2DS family (2DS, New2DS, New2DSXL) + pub fn is_2ds_family(&self) -> crate::Result { + let mut is_2ds_family: u8 = 0; + let is_2ds_family_pointer: *mut u8 = &mut is_2ds_family; unsafe { - let r = ctru_sys::CFGU_GetModelNintendo2DS(is_2ds_like_pointer); + let r = ctru_sys::CFGU_GetModelNintendo2DS(is_2ds_family_pointer); if r < 0 { Err(r.into()) } else { - Ok(is_2ds_like < 1) + Ok(is_2ds_family < 1) } } } From 509e9308f513ee0f9edbc2bc21019a8258639693 Mon Sep 17 00:00:00 2001 From: TechiePi Date: Sun, 10 Jul 2022 22:14:22 +0200 Subject: [PATCH 07/13] Change ``repr(u8)`` to ``repr(u32)`` to match ctru-sys --- ctru-rs/src/services/cfgu.rs | 56 ++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index 9d794cb..11610ae 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -3,43 +3,43 @@ //! This module contains basic methods to retrieve and change configuration from the console. #[derive(Debug)] -#[repr(u8)] +#[repr(u32)] pub enum Region { - Japan = ctru_sys::CFG_REGION_JPN as u8, - Usa = ctru_sys::CFG_REGION_USA as u8, - Europe = ctru_sys::CFG_REGION_EUR as u8, - Australia = ctru_sys::CFG_REGION_AUS as u8, - China = ctru_sys::CFG_REGION_CHN as u8, - Korea = ctru_sys::CFG_REGION_KOR as u8, - Taiwan = ctru_sys::CFG_REGION_TWN as u8, + Japan = ctru_sys::CFG_REGION_JPN, + Usa = ctru_sys::CFG_REGION_USA, + Europe = ctru_sys::CFG_REGION_EUR, + Australia = ctru_sys::CFG_REGION_AUS, + China = ctru_sys::CFG_REGION_CHN, + Korea = ctru_sys::CFG_REGION_KOR, + Taiwan = ctru_sys::CFG_REGION_TWN, } #[derive(Debug)] -#[repr(u8)] +#[repr(u32)] pub enum Language { - Japan = ctru_sys::CFG_LANGUAGE_JP as u8, - English = ctru_sys::CFG_LANGUAGE_EN as u8, - French = ctru_sys::CFG_LANGUAGE_FR as u8, - German = ctru_sys::CFG_LANGUAGE_DE as u8, - Italian = ctru_sys::CFG_LANGUAGE_IT as u8, - Spanish = ctru_sys::CFG_LANGUAGE_ES as u8, - SimpChinese = ctru_sys::CFG_LANGUAGE_ZH as u8, - Korean = ctru_sys::CFG_LANGUAGE_KO as u8, - Dutch = ctru_sys::CFG_LANGUAGE_NL as u8, - Portuguese = ctru_sys::CFG_LANGUAGE_PT as u8, - Russian = ctru_sys::CFG_LANGUAGE_RU as u8, - TradChinese = ctru_sys::CFG_LANGUAGE_TW as u8, + Japan = ctru_sys::CFG_LANGUAGE_JP, + English = ctru_sys::CFG_LANGUAGE_EN, + French = ctru_sys::CFG_LANGUAGE_FR, + German = ctru_sys::CFG_LANGUAGE_DE, + Italian = ctru_sys::CFG_LANGUAGE_IT, + Spanish = ctru_sys::CFG_LANGUAGE_ES, + SimpChinese = ctru_sys::CFG_LANGUAGE_ZH, + Korean = ctru_sys::CFG_LANGUAGE_KO, + Dutch = ctru_sys::CFG_LANGUAGE_NL, + Portuguese = ctru_sys::CFG_LANGUAGE_PT, + Russian = ctru_sys::CFG_LANGUAGE_RU, + TradChinese = ctru_sys::CFG_LANGUAGE_TW, } #[derive(Debug)] -#[repr(u8)] +#[repr(u32)] pub enum SystemModel { - Model3DS = ctru_sys::CFG_MODEL_3DS as u8, - Model3DSXL = ctru_sys::CFG_MODEL_3DSXL as u8, - ModelNew3DS = ctru_sys::CFG_MODEL_N3DS as u8, - Model2DS = ctru_sys::CFG_MODEL_2DS as u8, - ModelNew3DSXL = ctru_sys::CFG_MODEL_N3DSXL as u8, - ModelNew2DSXL = ctru_sys::CFG_MODEL_N2DSXL as u8, + Model3DS = ctru_sys::CFG_MODEL_3DS, + Model3DSXL = ctru_sys::CFG_MODEL_3DSXL, + ModelNew3DS = ctru_sys::CFG_MODEL_N3DS, + Model2DS = ctru_sys::CFG_MODEL_2DS, + ModelNew3DSXL = ctru_sys::CFG_MODEL_N3DSXL, + ModelNew2DSXL = ctru_sys::CFG_MODEL_N2DSXL, } /// Represents the configuration service. No actions can be performed From 5ddc8e99061bafb65d3563f96f0ca37e6eb888cd Mon Sep 17 00:00:00 2001 From: TechiePi Date: Mon, 11 Jul 2022 17:03:12 +0200 Subject: [PATCH 08/13] Change example filename to ``system-configuration.rs`` --- .../{system-language-region-model.rs => system-configuration.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ctru-rs/examples/{system-language-region-model.rs => system-configuration.rs} (100%) diff --git a/ctru-rs/examples/system-language-region-model.rs b/ctru-rs/examples/system-configuration.rs similarity index 100% rename from ctru-rs/examples/system-language-region-model.rs rename to ctru-rs/examples/system-configuration.rs From 7fa7137b39e971401076852f2ef5d634e89ff99a Mon Sep 17 00:00:00 2001 From: TechiePi Date: Tue, 12 Jul 2022 14:24:59 +0200 Subject: [PATCH 09/13] Apply suggestions (III) --- ctru-rs/src/services/cfgu.rs | 119 +++++++++++++++-------------------- 1 file changed, 52 insertions(+), 67 deletions(-) diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index 11610ae..4ee0df5 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -2,11 +2,11 @@ //! //! This module contains basic methods to retrieve and change configuration from the console. -#[derive(Debug)] +#[derive(Clone, Debug)] #[repr(u32)] pub enum Region { Japan = ctru_sys::CFG_REGION_JPN, - Usa = ctru_sys::CFG_REGION_USA, + USA = ctru_sys::CFG_REGION_USA, Europe = ctru_sys::CFG_REGION_EUR, Australia = ctru_sys::CFG_REGION_AUS, China = ctru_sys::CFG_REGION_CHN, @@ -14,24 +14,24 @@ pub enum Region { Taiwan = ctru_sys::CFG_REGION_TWN, } -#[derive(Debug)] +#[derive(Clone, Debug)] #[repr(u32)] pub enum Language { - Japan = ctru_sys::CFG_LANGUAGE_JP, - English = ctru_sys::CFG_LANGUAGE_EN, - French = ctru_sys::CFG_LANGUAGE_FR, - German = ctru_sys::CFG_LANGUAGE_DE, - Italian = ctru_sys::CFG_LANGUAGE_IT, - Spanish = ctru_sys::CFG_LANGUAGE_ES, - SimpChinese = ctru_sys::CFG_LANGUAGE_ZH, - Korean = ctru_sys::CFG_LANGUAGE_KO, - Dutch = ctru_sys::CFG_LANGUAGE_NL, - Portuguese = ctru_sys::CFG_LANGUAGE_PT, - Russian = ctru_sys::CFG_LANGUAGE_RU, - TradChinese = ctru_sys::CFG_LANGUAGE_TW, + Japan = ctru_sys::CFG_LANGUAGE_JP, + English = ctru_sys::CFG_LANGUAGE_EN, + French = ctru_sys::CFG_LANGUAGE_FR, + German = ctru_sys::CFG_LANGUAGE_DE, + Italian = ctru_sys::CFG_LANGUAGE_IT, + Spanish = ctru_sys::CFG_LANGUAGE_ES, + SimplifiedChinese = ctru_sys::CFG_LANGUAGE_ZH, + Korean = ctru_sys::CFG_LANGUAGE_KO, + Dutch = ctru_sys::CFG_LANGUAGE_NL, + Portuguese = ctru_sys::CFG_LANGUAGE_PT, + Russian = ctru_sys::CFG_LANGUAGE_RU, + TraditionalChinese = ctru_sys::CFG_LANGUAGE_TW, } -#[derive(Debug)] +#[derive(Clone, Debug)] #[repr(u32)] pub enum SystemModel { Model3DS = ctru_sys::CFG_MODEL_3DS, @@ -73,78 +73,63 @@ impl Cfgu { /// Gets system region from secure info pub fn get_region(&self) -> crate::Result { let mut region: u8 = 0; - let region_pointer: *mut u8 = &mut region; - unsafe { - let r = ctru_sys::CFGU_SecureInfoGetRegion(region_pointer); - if r < 0 { - Err(r.into()) - } else { - // The system shouldn't give an invalid value - Ok(Region::try_from(region).unwrap()) - } + let r = unsafe { ctru_sys::CFGU_SecureInfoGetRegion(&mut region) }; + if r < 0 { + Err(r.into()) + } else { + // The system shouldn't give an invalid value + Ok(Region::try_from(region).unwrap()) } } /// Gets system's model pub fn get_model(&self) -> crate::Result { let mut model: u8 = 0; - let model_pointer: *mut u8 = &mut model; - unsafe { - let r = ctru_sys::CFGU_GetSystemModel(model_pointer); - if r < 0 { - Err(r.into()) - } else { - // The system shouldn't give an invalid value - Ok(SystemModel::try_from(model).unwrap()) - } + let r = unsafe { ctru_sys::CFGU_GetSystemModel(&mut model) }; + if r < 0 { + Err(r.into()) + } else { + // The system shouldn't give an invalid value + Ok(SystemModel::try_from(model).unwrap()) } } /// Gets system's language pub fn get_language(&self) -> crate::Result { let mut language: u8 = 0; - let language_pointer: *mut u8 = &mut language; - unsafe { - let r = ctru_sys::CFGU_GetSystemLanguage(language_pointer); - if r < 0 { - Err(r.into()) - } else { - // The system shouldn't give an invalid value - Ok(Language::try_from(language).unwrap()) - } + let r = unsafe { ctru_sys::CFGU_GetSystemLanguage(&mut language) }; + if r < 0 { + Err(r.into()) + } else { + // The system shouldn't give an invalid value + Ok(Language::try_from(language).unwrap()) } } /// Checks if NFC is supported by the console pub fn is_nfc_supported(&self) -> crate::Result { let mut supported: bool = false; - let supported_pointer: *mut bool = &mut supported; - unsafe { - let r = ctru_sys::CFGU_IsNFCSupported(supported_pointer); - if r < 0 { - Err(r.into()) - } else { - Ok(supported) - } + let r = unsafe { ctru_sys::CFGU_IsNFCSupported(&mut supported) }; + if r < 0 { + Err(r.into()) + } else { + Ok(supported) } } /// Check if the console is from the 2DS family (2DS, New2DS, New2DSXL) pub fn is_2ds_family(&self) -> crate::Result { let mut is_2ds_family: u8 = 0; - let is_2ds_family_pointer: *mut u8 = &mut is_2ds_family; - unsafe { - let r = ctru_sys::CFGU_GetModelNintendo2DS(is_2ds_family_pointer); - if r < 0 { - Err(r.into()) - } else { - Ok(is_2ds_family < 1) - } + let r = unsafe { ctru_sys::CFGU_GetModelNintendo2DS(&mut is_2ds_family) }; + if r < 0 { + Err(r.into()) + } else { + Ok(is_2ds_family == 0) } } } @@ -177,7 +162,7 @@ impl TryFrom for Region { fn try_from(value: u8) -> Result { match value as u32 { ctru_sys::CFG_REGION_JPN => Ok(Region::Japan), - ctru_sys::CFG_REGION_USA => Ok(Region::Usa), + ctru_sys::CFG_REGION_USA => Ok(Region::USA), ctru_sys::CFG_REGION_EUR => Ok(Region::Europe), ctru_sys::CFG_REGION_AUS => Ok(Region::Australia), ctru_sys::CFG_REGION_CHN => Ok(Region::China), @@ -199,12 +184,12 @@ impl TryFrom for Language { ctru_sys::CFG_LANGUAGE_DE => Ok(Language::German), ctru_sys::CFG_LANGUAGE_IT => Ok(Language::Italian), ctru_sys::CFG_LANGUAGE_ES => Ok(Language::Spanish), - ctru_sys::CFG_LANGUAGE_ZH => Ok(Language::SimpChinese), + ctru_sys::CFG_LANGUAGE_ZH => Ok(Language::SimplifiedChinese), ctru_sys::CFG_LANGUAGE_KO => Ok(Language::Korean), ctru_sys::CFG_LANGUAGE_NL => Ok(Language::Dutch), ctru_sys::CFG_LANGUAGE_PT => Ok(Language::Portuguese), - ctru_sys::CFG_LANGUAGE_RU => Ok(Language::Russian), - ctru_sys::CFG_LANGUAGE_TW => Ok(Language::TradChinese), + ctru_sys::CFG_LANGUAGE_RU => Ok(Language::Russian), + ctru_sys::CFG_LANGUAGE_TW => Ok(Language::TraditionalChinese), _ => Err(()) } } @@ -215,10 +200,10 @@ impl TryFrom for SystemModel { fn try_from(value: u8) -> Result { match value as u32 { - ctru_sys::CFG_MODEL_3DS => Ok(SystemModel::Model3DS), - ctru_sys::CFG_MODEL_3DSXL => Ok(SystemModel::Model3DSXL), - ctru_sys::CFG_MODEL_N3DS => Ok(SystemModel::ModelNew3DS), - ctru_sys::CFG_MODEL_2DS => Ok(SystemModel::Model2DS), + ctru_sys::CFG_MODEL_3DS => Ok(SystemModel::Model3DS), + ctru_sys::CFG_MODEL_3DSXL => Ok(SystemModel::Model3DSXL), + ctru_sys::CFG_MODEL_N3DS => Ok(SystemModel::ModelNew3DS), + ctru_sys::CFG_MODEL_2DS => Ok(SystemModel::Model2DS), ctru_sys::CFG_MODEL_N3DSXL => Ok(SystemModel::ModelNew3DSXL), ctru_sys::CFG_MODEL_N2DSXL => Ok(SystemModel::ModelNew2DSXL), _ => Err(()) From 1bf8c582d6d297bc547f4ce92af8bc552dd2c454 Mon Sep 17 00:00:00 2001 From: TechiePi Date: Tue, 12 Jul 2022 14:25:36 +0200 Subject: [PATCH 10/13] Rustfmt --- ctru-rs/src/services/cfgu.rs | 92 ++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index 4ee0df5..f8761be 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -5,41 +5,41 @@ #[derive(Clone, Debug)] #[repr(u32)] pub enum Region { - Japan = ctru_sys::CFG_REGION_JPN, - USA = ctru_sys::CFG_REGION_USA, - Europe = ctru_sys::CFG_REGION_EUR, - Australia = ctru_sys::CFG_REGION_AUS, - China = ctru_sys::CFG_REGION_CHN, - Korea = ctru_sys::CFG_REGION_KOR, - Taiwan = ctru_sys::CFG_REGION_TWN, + Japan = ctru_sys::CFG_REGION_JPN, + USA = ctru_sys::CFG_REGION_USA, + Europe = ctru_sys::CFG_REGION_EUR, + Australia = ctru_sys::CFG_REGION_AUS, + China = ctru_sys::CFG_REGION_CHN, + Korea = ctru_sys::CFG_REGION_KOR, + Taiwan = ctru_sys::CFG_REGION_TWN, } #[derive(Clone, Debug)] #[repr(u32)] pub enum Language { - Japan = ctru_sys::CFG_LANGUAGE_JP, - English = ctru_sys::CFG_LANGUAGE_EN, - French = ctru_sys::CFG_LANGUAGE_FR, - German = ctru_sys::CFG_LANGUAGE_DE, - Italian = ctru_sys::CFG_LANGUAGE_IT, - Spanish = ctru_sys::CFG_LANGUAGE_ES, - SimplifiedChinese = ctru_sys::CFG_LANGUAGE_ZH, - Korean = ctru_sys::CFG_LANGUAGE_KO, - Dutch = ctru_sys::CFG_LANGUAGE_NL, - Portuguese = ctru_sys::CFG_LANGUAGE_PT, - Russian = ctru_sys::CFG_LANGUAGE_RU, - TraditionalChinese = ctru_sys::CFG_LANGUAGE_TW, + Japan = ctru_sys::CFG_LANGUAGE_JP, + English = ctru_sys::CFG_LANGUAGE_EN, + French = ctru_sys::CFG_LANGUAGE_FR, + German = ctru_sys::CFG_LANGUAGE_DE, + Italian = ctru_sys::CFG_LANGUAGE_IT, + Spanish = ctru_sys::CFG_LANGUAGE_ES, + SimplifiedChinese = ctru_sys::CFG_LANGUAGE_ZH, + Korean = ctru_sys::CFG_LANGUAGE_KO, + Dutch = ctru_sys::CFG_LANGUAGE_NL, + Portuguese = ctru_sys::CFG_LANGUAGE_PT, + Russian = ctru_sys::CFG_LANGUAGE_RU, + TraditionalChinese = ctru_sys::CFG_LANGUAGE_TW, } #[derive(Clone, Debug)] #[repr(u32)] pub enum SystemModel { - Model3DS = ctru_sys::CFG_MODEL_3DS, - Model3DSXL = ctru_sys::CFG_MODEL_3DSXL, - ModelNew3DS = ctru_sys::CFG_MODEL_N3DS, - Model2DS = ctru_sys::CFG_MODEL_2DS, - ModelNew3DSXL = ctru_sys::CFG_MODEL_N3DSXL, - ModelNew2DSXL = ctru_sys::CFG_MODEL_N2DSXL, + Model3DS = ctru_sys::CFG_MODEL_3DS, + Model3DSXL = ctru_sys::CFG_MODEL_3DSXL, + ModelNew3DS = ctru_sys::CFG_MODEL_N3DS, + Model2DS = ctru_sys::CFG_MODEL_2DS, + ModelNew3DSXL = ctru_sys::CFG_MODEL_N3DSXL, + ModelNew2DSXL = ctru_sys::CFG_MODEL_N2DSXL, } /// Represents the configuration service. No actions can be performed @@ -149,7 +149,7 @@ macro_rules! from_type_to_u8 { v as u8 } } - } + }; } from_type_to_u8!(Region); @@ -168,7 +168,7 @@ impl TryFrom for Region { ctru_sys::CFG_REGION_CHN => Ok(Region::China), ctru_sys::CFG_REGION_KOR => Ok(Region::Korea), ctru_sys::CFG_REGION_TWN => Ok(Region::Taiwan), - _ => Err(()) + _ => Err(()), } } } @@ -178,19 +178,19 @@ impl TryFrom for Language { fn try_from(value: u8) -> Result { match value as u32 { - ctru_sys::CFG_LANGUAGE_JP => Ok(Language::Japan), - ctru_sys::CFG_LANGUAGE_EN => Ok(Language::English), - ctru_sys::CFG_LANGUAGE_FR => Ok(Language::French), - ctru_sys::CFG_LANGUAGE_DE => Ok(Language::German), - ctru_sys::CFG_LANGUAGE_IT => Ok(Language::Italian), - ctru_sys::CFG_LANGUAGE_ES => Ok(Language::Spanish), - ctru_sys::CFG_LANGUAGE_ZH => Ok(Language::SimplifiedChinese), - ctru_sys::CFG_LANGUAGE_KO => Ok(Language::Korean), - ctru_sys::CFG_LANGUAGE_NL => Ok(Language::Dutch), - ctru_sys::CFG_LANGUAGE_PT => Ok(Language::Portuguese), - ctru_sys::CFG_LANGUAGE_RU => Ok(Language::Russian), - ctru_sys::CFG_LANGUAGE_TW => Ok(Language::TraditionalChinese), - _ => Err(()) + ctru_sys::CFG_LANGUAGE_JP => Ok(Language::Japan), + ctru_sys::CFG_LANGUAGE_EN => Ok(Language::English), + ctru_sys::CFG_LANGUAGE_FR => Ok(Language::French), + ctru_sys::CFG_LANGUAGE_DE => Ok(Language::German), + ctru_sys::CFG_LANGUAGE_IT => Ok(Language::Italian), + ctru_sys::CFG_LANGUAGE_ES => Ok(Language::Spanish), + ctru_sys::CFG_LANGUAGE_ZH => Ok(Language::SimplifiedChinese), + ctru_sys::CFG_LANGUAGE_KO => Ok(Language::Korean), + ctru_sys::CFG_LANGUAGE_NL => Ok(Language::Dutch), + ctru_sys::CFG_LANGUAGE_PT => Ok(Language::Portuguese), + ctru_sys::CFG_LANGUAGE_RU => Ok(Language::Russian), + ctru_sys::CFG_LANGUAGE_TW => Ok(Language::TraditionalChinese), + _ => Err(()), } } } @@ -200,13 +200,13 @@ impl TryFrom for SystemModel { fn try_from(value: u8) -> Result { match value as u32 { - ctru_sys::CFG_MODEL_3DS => Ok(SystemModel::Model3DS), - ctru_sys::CFG_MODEL_3DSXL => Ok(SystemModel::Model3DSXL), - ctru_sys::CFG_MODEL_N3DS => Ok(SystemModel::ModelNew3DS), - ctru_sys::CFG_MODEL_2DS => Ok(SystemModel::Model2DS), + ctru_sys::CFG_MODEL_3DS => Ok(SystemModel::Model3DS), + ctru_sys::CFG_MODEL_3DSXL => Ok(SystemModel::Model3DSXL), + ctru_sys::CFG_MODEL_N3DS => Ok(SystemModel::ModelNew3DS), + ctru_sys::CFG_MODEL_2DS => Ok(SystemModel::Model2DS), ctru_sys::CFG_MODEL_N3DSXL => Ok(SystemModel::ModelNew3DSXL), ctru_sys::CFG_MODEL_N2DSXL => Ok(SystemModel::ModelNew2DSXL), - _ => Err(()) + _ => Err(()), } } -} \ No newline at end of file +} From 0dbf85db4c86f824da17c934a5fce1fccc24a467 Mon Sep 17 00:00:00 2001 From: TechiePi Date: Wed, 13 Jul 2022 11:07:40 +0200 Subject: [PATCH 11/13] Apply suggestions (IV) --- ctru-rs/src/services/cfgu.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index f8761be..96c3cd5 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -2,7 +2,7 @@ //! //! This module contains basic methods to retrieve and change configuration from the console. -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[repr(u32)] pub enum Region { Japan = ctru_sys::CFG_REGION_JPN, @@ -14,7 +14,7 @@ pub enum Region { Taiwan = ctru_sys::CFG_REGION_TWN, } -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[repr(u32)] pub enum Language { Japan = ctru_sys::CFG_LANGUAGE_JP, @@ -31,7 +31,7 @@ pub enum Language { TraditionalChinese = ctru_sys::CFG_LANGUAGE_TW, } -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[repr(u32)] pub enum SystemModel { Model3DS = ctru_sys::CFG_MODEL_3DS, From c130661da2b89673d14e8e549994ffdbff9ffe0d Mon Sep 17 00:00:00 2001 From: TechiePi Date: Wed, 13 Jul 2022 18:05:27 +0200 Subject: [PATCH 12/13] Apply suggestions (V) --- ctru-rs/src/services/cfgu.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index 96c3cd5..0ade817 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -17,7 +17,7 @@ pub enum Region { #[derive(Copy, Clone, Debug)] #[repr(u32)] pub enum Language { - Japan = ctru_sys::CFG_LANGUAGE_JP, + Japanese = ctru_sys::CFG_LANGUAGE_JP, English = ctru_sys::CFG_LANGUAGE_EN, French = ctru_sys::CFG_LANGUAGE_FR, German = ctru_sys::CFG_LANGUAGE_DE, @@ -178,7 +178,7 @@ impl TryFrom for Language { fn try_from(value: u8) -> Result { match value as u32 { - ctru_sys::CFG_LANGUAGE_JP => Ok(Language::Japan), + ctru_sys::CFG_LANGUAGE_JP => Ok(Language::Japanese), ctru_sys::CFG_LANGUAGE_EN => Ok(Language::English), ctru_sys::CFG_LANGUAGE_FR => Ok(Language::French), ctru_sys::CFG_LANGUAGE_DE => Ok(Language::German), From 990edffd215ba02263bba677076e7bf5cf3f88dd Mon Sep 17 00:00:00 2001 From: TechiePi Date: Sun, 17 Jul 2022 02:54:31 +0200 Subject: [PATCH 13/13] Apply suggestions (VI) --- ctru-rs/src/services/cfgu.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ctru-rs/src/services/cfgu.rs b/ctru-rs/src/services/cfgu.rs index 0ade817..bc8d986 100644 --- a/ctru-rs/src/services/cfgu.rs +++ b/ctru-rs/src/services/cfgu.rs @@ -58,7 +58,7 @@ impl Cfgu { /// /// ctrulib services are reference counted, so this function may be called /// as many times as desired and the service will not exit until all - /// instances of Fs drop out of scope. + /// instances of Cfgu drop out of scope. pub fn init() -> crate::Result { unsafe { let r = ctru_sys::cfguInit();