1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//! System Configuration service.
//!
//! This module contains basic methods to retrieve the console's system configuration.
#![doc(alias = "configuration")]

use crate::error::ResultCode;

/// Console region.
#[doc(alias = "CFG_Region")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum Region {
    /// Japan.
    Japan = ctru_sys::CFG_REGION_JPN,
    /// USA.
    USA = ctru_sys::CFG_REGION_USA,
    /// Europe.
    Europe = ctru_sys::CFG_REGION_EUR,
    /// Australia.
    Australia = ctru_sys::CFG_REGION_AUS,
    /// China.
    China = ctru_sys::CFG_REGION_CHN,
    /// Korea.
    Korea = ctru_sys::CFG_REGION_KOR,
    /// Taiwan.
    Taiwan = ctru_sys::CFG_REGION_TWN,
}

/// Language set for the console's OS.
#[doc(alias = "CFG_Language")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum Language {
    /// Japanese.
    Japanese = ctru_sys::CFG_LANGUAGE_JP,
    /// English.
    English = ctru_sys::CFG_LANGUAGE_EN,
    /// French.
    French = ctru_sys::CFG_LANGUAGE_FR,
    /// German.
    German = ctru_sys::CFG_LANGUAGE_DE,
    /// Italian.
    Italian = ctru_sys::CFG_LANGUAGE_IT,
    /// Spanish.
    Spanish = ctru_sys::CFG_LANGUAGE_ES,
    /// Korean.
    Korean = ctru_sys::CFG_LANGUAGE_KO,
    /// Dutch.
    Dutch = ctru_sys::CFG_LANGUAGE_NL,
    /// Portuguese.
    Portuguese = ctru_sys::CFG_LANGUAGE_PT,
    /// Russian.
    Russian = ctru_sys::CFG_LANGUAGE_RU,
    /// Simplified Chinese.
    SimplifiedChinese = ctru_sys::CFG_LANGUAGE_ZH,
    /// Traditional Chinese.
    TraditionalChinese = ctru_sys::CFG_LANGUAGE_TW,
}

/// Specific model of the console.
#[doc(alias = "CFG_SystemModel")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum SystemModel {
    /// Old Nintendo 3DS.
    Old3DS = ctru_sys::CFG_MODEL_3DS,
    /// Old Nintendo 3DS XL.
    Old3DSXL = ctru_sys::CFG_MODEL_3DSXL,
    /// New Nintendo 3DS.
    New3DS = ctru_sys::CFG_MODEL_N3DS,
    /// Old Nintendo 2DS.
    Old2DS = ctru_sys::CFG_MODEL_2DS,
    /// New Nintendo 3DS XL.
    New3DSXL = ctru_sys::CFG_MODEL_N3DSXL,
    /// New Nintendo 2DS XL.
    New2DSXL = ctru_sys::CFG_MODEL_N2DSXL,
}

/// Handle to the System Configuration service.
pub struct Cfgu(());

impl Cfgu {
    /// Initialize a new service handle.
    ///
    /// # Example
    ///
    /// ```
    /// # let _runner = test_runner::GdbRunner::default();
    /// # use std::error::Error;
    /// # fn main() -> Result<(), Box<dyn Error>> {
    /// #
    /// use ctru::services::cfgu::Cfgu;
    ///
    /// let cfgu = Cfgu::new()?;
    /// #
    /// # Ok(())
    /// # }
    /// ```
    #[doc(alias = "cfguInit")]
    pub fn new() -> crate::Result<Cfgu> {
        ResultCode(unsafe { ctru_sys::cfguInit() })?;
        Ok(Cfgu(()))
    }

    /// Returns the console's region from the system's secure info.
    ///
    /// # Example
    ///
    /// ```
    /// # let _runner = test_runner::GdbRunner::default();
    /// # use std::error::Error;
    /// # fn main() -> Result<(), Box<dyn Error>> {
    /// #
    /// use ctru::services::cfgu::Cfgu;
    /// let cfgu = Cfgu::new()?;
    ///
    /// let region = cfgu.region()?;
    /// #
    /// # Ok(())
    /// # }
    /// ```
    #[doc(alias = "CFGU_SecureInfoGetRegion")]
    pub fn region(&self) -> crate::Result<Region> {
        let mut region: u8 = 0;

        ResultCode(unsafe { ctru_sys::CFGU_SecureInfoGetRegion(&mut region) })?;
        Ok(Region::try_from(region).unwrap())
    }

    /// Returns the console's model.
    ///
    /// # Example
    ///
    /// ```
    /// # let _runner = test_runner::GdbRunner::default();
    /// # use std::error::Error;
    /// # fn main() -> Result<(), Box<dyn Error>> {
    /// #
    /// use ctru::services::cfgu::Cfgu;
    /// let cfgu = Cfgu::new()?;
    ///
    /// let model = cfgu.model()?;
    /// #
    /// # Ok(())
    /// # }
    /// ```
    #[doc(alias = "CFGU_GetSystemModel")]
    pub fn model(&self) -> crate::Result<SystemModel> {
        let mut model: u8 = 0;

        ResultCode(unsafe { ctru_sys::CFGU_GetSystemModel(&mut model) })?;
        Ok(SystemModel::try_from(model).unwrap())
    }

    /// Returns the system language set for the console.
    ///
    /// # Example
    ///
    /// ```
    /// # let _runner = test_runner::GdbRunner::default();
    /// # use std::error::Error;
    /// # fn main() -> Result<(), Box<dyn Error>> {
    /// #
    /// use ctru::services::cfgu::Cfgu;
    /// let cfgu = Cfgu::new()?;
    ///
    /// let language = cfgu.language()?;
    /// #
    /// # Ok(())
    /// # }
    /// ```
    #[doc(alias = "CFGU_GetSystemLanguage")]
    pub fn language(&self) -> crate::Result<Language> {
        let mut language: u8 = 0;

        ResultCode(unsafe { ctru_sys::CFGU_GetSystemLanguage(&mut language) })?;
        Ok(Language::try_from(language).unwrap())
    }

    /// Check if NFC is supported by the console.
    ///
    /// # Example
    ///
    /// ```
    /// # let _runner = test_runner::GdbRunner::default();
    /// # use std::error::Error;
    /// # fn main() -> Result<(), Box<dyn Error>> {
    /// #
    /// use ctru::services::cfgu::Cfgu;
    /// let cfgu = Cfgu::new()?;
    ///
    /// if cfgu.is_nfc_supported()? {
    ///     println!("NFC is available!");
    /// }
    /// #
    /// # Ok(())
    /// # }
    /// ```
    #[doc(alias = "CFGU_IsNFCSupported")]
    pub fn is_nfc_supported(&self) -> crate::Result<bool> {
        let mut supported: bool = false;

        ResultCode(unsafe { ctru_sys::CFGU_IsNFCSupported(&mut supported) })?;
        Ok(supported)
    }

    /// Check if the console is from the 2DS family ([`Old2DS`](SystemModel::Old2DS), [`New2DSXL`](SystemModel::New2DSXL)).
    ///
    /// Useful to avoid stereoscopic 3D rendering when working with 2DS consoles.
    ///
    /// # Example
    ///
    /// ```
    /// # let _runner = test_runner::GdbRunner::default();
    /// # use std::error::Error;
    /// # fn main() -> Result<(), Box<dyn Error>> {
    /// #
    /// use ctru::services::cfgu::Cfgu;
    /// let cfgu = Cfgu::new()?;
    ///
    /// if cfgu.is_2ds_family()? {
    ///     println!("Stereoscopic 3D is not supported.");
    /// }
    /// #
    /// # Ok(())
    /// # }
    /// ```
    #[doc(alias = "CFGU_GetModelNintendo2DS")]
    pub fn is_2ds_family(&self) -> crate::Result<bool> {
        let mut is_2ds_family: u8 = 0;

        ResultCode(unsafe { ctru_sys::CFGU_GetModelNintendo2DS(&mut is_2ds_family) })?;
        Ok(is_2ds_family == 0)
    }
}

impl Drop for Cfgu {
    #[doc(alias = "cfguExit")]
    fn drop(&mut self) {
        unsafe {
            ctru_sys::cfguExit();
        }
    }
}

from_impl!(Region, u8);
from_impl!(Language, u8);
from_impl!(SystemModel, u8);

impl TryFrom<u8> for Region {
    type Error = ();

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            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(()),
        }
    }
}

impl TryFrom<u8> for Language {
    type Error = ();

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            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),
            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(()),
        }
    }
}

impl TryFrom<u8> for SystemModel {
    type Error = ();

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            ctru_sys::CFG_MODEL_3DS => Ok(SystemModel::Old3DS),
            ctru_sys::CFG_MODEL_3DSXL => Ok(SystemModel::Old3DSXL),
            ctru_sys::CFG_MODEL_N3DS => Ok(SystemModel::New3DS),
            ctru_sys::CFG_MODEL_2DS => Ok(SystemModel::Old2DS),
            ctru_sys::CFG_MODEL_N3DSXL => Ok(SystemModel::New3DSXL),
            ctru_sys::CFG_MODEL_N2DSXL => Ok(SystemModel::New2DSXL),
            _ => Err(()),
        }
    }
}