Browse Source

Improve implementation and improve wording

Co-Authored-By: Ian Chamberlain <11131775+ian-h-chamberlain@users.noreply.github.com>
pull/74/head
TechiePi 2 years ago
parent
commit
f896871b73
  1. 27
      ctru-rs/src/error.rs
  2. 4
      ctru-rs/src/romfs.rs
  3. 6
      ctru-rs/src/services/apt.rs
  4. 88
      ctru-rs/src/services/cam.rs
  5. 14
      ctru-rs/src/services/cfgu.rs
  6. 4
      ctru-rs/src/services/hid.rs
  7. 8
      ctru-rs/src/services/ps.rs
  8. 4
      ctru-rs/src/services/soc.rs
  9. 6
      ctru-rs/src/services/sslc.rs

27
ctru-rs/src/error.rs

@ -9,14 +9,11 @@ pub type Result<T> = ::std::result::Result<T, Error>;
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord)] #[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
#[repr(transparent)] #[repr(transparent)]
pub(crate) struct LibCtruResult(pub i32); pub(crate) struct ResultCode(pub i32);
impl Try for LibCtruResult { impl Try for ResultCode {
type Output = (); type Output = ();
// This type is passed to [FromResidual::from_residual] when the LibCtruResult is an error, type Residual = Error;
// so this type implies "this is a result than CAN'T be `Ok`" (Infallible is the same as !)
// The purpose of this type is to _only_ bring information about the *Error*
type Residual = crate::Result<core::convert::Infallible>;
fn from_output(_: Self::Output) -> Self { fn from_output(_: Self::Output) -> Self {
Self(0) Self(0)
@ -24,22 +21,28 @@ impl Try for LibCtruResult {
fn branch(self) -> ControlFlow<Self::Residual, Self::Output> { fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
if self.0 < 0 { if self.0 < 0 {
ControlFlow::Break(Err(self.into())) ControlFlow::Break(self.into())
} else { } else {
ControlFlow::Continue(()) ControlFlow::Continue(())
} }
} }
} }
impl FromResidual for LibCtruResult { impl FromResidual for ResultCode {
fn from_residual(e: <Self as Try>::Residual) -> Self { fn from_residual(e: <Self as Try>::Residual) -> Self {
match e.err().unwrap() { match e {
Error::Os(result) => Self(result), Error::Os(result) => Self(result),
_ => Self(-1), _ => unreachable!(),
} }
} }
} }
impl<T> FromResidual<Error> for Result<T> {
fn from_residual(e: Error) -> Self {
Err(e)
}
}
/// The error type returned by all libctru functions. /// The error type returned by all libctru functions.
#[non_exhaustive] #[non_exhaustive]
pub enum Error { pub enum Error {
@ -73,8 +76,8 @@ impl From<ctru_sys::Result> for Error {
} }
} }
impl From<LibCtruResult> for Error { impl From<ResultCode> for Error {
fn from(err: LibCtruResult) -> Self { fn from(err: ResultCode) -> Self {
Self::Os(err.0) Self::Os(err.0)
} }
} }

4
ctru-rs/src/romfs.rs

@ -10,7 +10,7 @@
//! romfs_dir = "romfs" //! romfs_dir = "romfs"
//! ``` //! ```
use crate::error::LibCtruResult; use crate::error::ResultCode;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::ffi::CStr; use std::ffi::CStr;
use std::sync::Mutex; use std::sync::Mutex;
@ -31,7 +31,7 @@ impl RomFS {
true, true,
|| { || {
let mount_name = CStr::from_bytes_with_nul(b"romfs\0").unwrap(); let mount_name = CStr::from_bytes_with_nul(b"romfs\0").unwrap();
LibCtruResult(unsafe { ctru_sys::romfsMountSelf(mount_name.as_ptr()) })?; ResultCode(unsafe { ctru_sys::romfsMountSelf(mount_name.as_ptr()) })?;
Ok(()) Ok(())
}, },
|| { || {

6
ctru-rs/src/services/apt.rs

@ -1,11 +1,11 @@
use crate::error::LibCtruResult; use crate::error::ResultCode;
pub struct Apt(()); pub struct Apt(());
impl Apt { impl Apt {
pub fn init() -> crate::Result<Apt> { pub fn init() -> crate::Result<Apt> {
unsafe { unsafe {
LibCtruResult(ctru_sys::aptInit())?; ResultCode(ctru_sys::aptInit())?;
Ok(Apt(())) Ok(Apt(()))
} }
} }
@ -16,7 +16,7 @@ impl Apt {
pub fn set_app_cpu_time_limit(&self, percent: u32) -> crate::Result<()> { pub fn set_app_cpu_time_limit(&self, percent: u32) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::APT_SetAppCpuTimeLimit(percent))?; ResultCode(ctru_sys::APT_SetAppCpuTimeLimit(percent))?;
Ok(()) Ok(())
} }
} }

88
ctru-rs/src/services/cam.rs

@ -3,7 +3,7 @@
//! The CAM service provides access to the cameras. Cameras can return 2D images //! The CAM service provides access to the cameras. Cameras can return 2D images
//! in the form of byte vectors which can be used for display or other usages. //! in the form of byte vectors which can be used for display or other usages.
use crate::error::LibCtruResult; use crate::error::ResultCode;
use crate::services::gspgpu::FramebufferFormat; use crate::services::gspgpu::FramebufferFormat;
use bitflags::bitflags; use bitflags::bitflags;
use ctru_sys::Handle; use ctru_sys::Handle;
@ -268,7 +268,7 @@ impl BothOutwardCam {
brightness_synchronization: bool, brightness_synchronization: bool,
) -> crate::Result<()> { ) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetBrightnessSynchronization( ResultCode(ctru_sys::CAMU_SetBrightnessSynchronization(
brightness_synchronization, brightness_synchronization,
))?; ))?;
Ok(()) Ok(())
@ -300,7 +300,7 @@ pub trait Camera {
fn is_busy(&self) -> crate::Result<bool> { fn is_busy(&self) -> crate::Result<bool> {
unsafe { unsafe {
let mut is_busy = false; let mut is_busy = false;
LibCtruResult(ctru_sys::CAMU_IsBusy(&mut is_busy, self.port_as_raw()))?; ResultCode(ctru_sys::CAMU_IsBusy(&mut is_busy, self.port_as_raw()))?;
Ok(is_busy) Ok(is_busy)
} }
} }
@ -310,7 +310,7 @@ pub trait Camera {
fn get_transfer_bytes(&self) -> crate::Result<u32> { fn get_transfer_bytes(&self) -> crate::Result<u32> {
unsafe { unsafe {
let mut transfer_bytes = 0; let mut transfer_bytes = 0;
LibCtruResult(ctru_sys::CAMU_GetTransferBytes( ResultCode(ctru_sys::CAMU_GetTransferBytes(
&mut transfer_bytes, &mut transfer_bytes,
self.port_as_raw(), self.port_as_raw(),
))?; ))?;
@ -322,7 +322,7 @@ pub trait Camera {
/// [Camera::set_trimming_params] /// [Camera::set_trimming_params]
fn set_trimming(&mut self, enabled: bool) -> crate::Result<()> { fn set_trimming(&mut self, enabled: bool) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetTrimming(self.port_as_raw(), enabled))?; ResultCode(ctru_sys::CAMU_SetTrimming(self.port_as_raw(), enabled))?;
Ok(()) Ok(())
} }
} }
@ -331,7 +331,7 @@ pub trait Camera {
fn is_trimming_enabled(&self) -> crate::Result<bool> { fn is_trimming_enabled(&self) -> crate::Result<bool> {
unsafe { unsafe {
let mut trimming = false; let mut trimming = false;
LibCtruResult(ctru_sys::CAMU_IsTrimming(&mut trimming, self.port_as_raw()))?; ResultCode(ctru_sys::CAMU_IsTrimming(&mut trimming, self.port_as_raw()))?;
Ok(trimming) Ok(trimming)
} }
} }
@ -339,7 +339,7 @@ pub trait Camera {
/// Sets trimming parameters based on coordinates specified inside a [CamTrimmingParams] /// Sets trimming parameters based on coordinates specified inside a [CamTrimmingParams]
fn set_trimming_params(&mut self, params: CamTrimmingParams) -> crate::Result<()> { fn set_trimming_params(&mut self, params: CamTrimmingParams) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetTrimmingParams( ResultCode(ctru_sys::CAMU_SetTrimmingParams(
self.port_as_raw(), self.port_as_raw(),
params.x_start, params.x_start,
params.y_start, params.y_start,
@ -357,7 +357,7 @@ pub trait Camera {
let mut y_start = 0; let mut y_start = 0;
let mut x_end = 0; let mut x_end = 0;
let mut y_end = 0; let mut y_end = 0;
LibCtruResult(ctru_sys::CAMU_GetTrimmingParams( ResultCode(ctru_sys::CAMU_GetTrimmingParams(
&mut x_start, &mut x_start,
&mut y_start, &mut y_start,
&mut x_end, &mut x_end,
@ -385,7 +385,7 @@ pub trait Camera {
cam_height: i16, cam_height: i16,
) -> crate::Result<()> { ) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetTrimmingParamsCenter( ResultCode(ctru_sys::CAMU_SetTrimmingParamsCenter(
self.port_as_raw(), self.port_as_raw(),
trim_width, trim_width,
trim_height, trim_height,
@ -399,7 +399,7 @@ pub trait Camera {
/// Sets the exposure level of the camera /// Sets the exposure level of the camera
fn set_exposure(&mut self, exposure: i8) -> crate::Result<()> { fn set_exposure(&mut self, exposure: i8) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetExposure(self.camera_as_raw(), exposure))?; ResultCode(ctru_sys::CAMU_SetExposure(self.camera_as_raw(), exposure))?;
Ok(()) Ok(())
} }
} }
@ -407,7 +407,7 @@ pub trait Camera {
/// Sets the white balance mod of the camera based on the passed [CamWhiteBalance] argument /// Sets the white balance mod of the camera based on the passed [CamWhiteBalance] argument
fn set_white_balance(&mut self, white_balance: CamWhiteBalance) -> crate::Result<()> { fn set_white_balance(&mut self, white_balance: CamWhiteBalance) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetWhiteBalance( ResultCode(ctru_sys::CAMU_SetWhiteBalance(
self.camera_as_raw(), self.camera_as_raw(),
white_balance.bits(), white_balance.bits(),
))?; ))?;
@ -422,7 +422,7 @@ pub trait Camera {
white_balance: CamWhiteBalance, white_balance: CamWhiteBalance,
) -> crate::Result<()> { ) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetWhiteBalanceWithoutBaseUp( ResultCode(ctru_sys::CAMU_SetWhiteBalanceWithoutBaseUp(
self.camera_as_raw(), self.camera_as_raw(),
white_balance.bits(), white_balance.bits(),
))?; ))?;
@ -433,7 +433,7 @@ pub trait Camera {
/// Sets the sharpness of the camera /// Sets the sharpness of the camera
fn set_sharpness(&mut self, sharpness: i8) -> crate::Result<()> { fn set_sharpness(&mut self, sharpness: i8) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetSharpness(self.camera_as_raw(), sharpness))?; ResultCode(ctru_sys::CAMU_SetSharpness(self.camera_as_raw(), sharpness))?;
Ok(()) Ok(())
} }
} }
@ -441,7 +441,7 @@ pub trait Camera {
/// Sets whether auto exposure is enabled or disabled for the camera /// Sets whether auto exposure is enabled or disabled for the camera
fn set_auto_exposure(&mut self, enabled: bool) -> crate::Result<()> { fn set_auto_exposure(&mut self, enabled: bool) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetAutoExposure( ResultCode(ctru_sys::CAMU_SetAutoExposure(
self.camera_as_raw(), self.camera_as_raw(),
enabled, enabled,
))?; ))?;
@ -453,7 +453,7 @@ pub trait Camera {
fn is_auto_exposure_enabled(&self) -> crate::Result<bool> { fn is_auto_exposure_enabled(&self) -> crate::Result<bool> {
unsafe { unsafe {
let mut enabled = false; let mut enabled = false;
LibCtruResult(ctru_sys::CAMU_IsAutoExposure( ResultCode(ctru_sys::CAMU_IsAutoExposure(
&mut enabled, &mut enabled,
self.camera_as_raw(), self.camera_as_raw(),
))?; ))?;
@ -464,7 +464,7 @@ pub trait Camera {
/// Sets whether auto white balance is enabled or disabled for the camera /// Sets whether auto white balance is enabled or disabled for the camera
fn set_auto_white_balance(&mut self, enabled: bool) -> crate::Result<()> { fn set_auto_white_balance(&mut self, enabled: bool) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetAutoWhiteBalance( ResultCode(ctru_sys::CAMU_SetAutoWhiteBalance(
self.camera_as_raw(), self.camera_as_raw(),
enabled, enabled,
))?; ))?;
@ -476,7 +476,7 @@ pub trait Camera {
fn is_auto_white_balance_enabled(&self) -> crate::Result<bool> { fn is_auto_white_balance_enabled(&self) -> crate::Result<bool> {
unsafe { unsafe {
let mut enabled = false; let mut enabled = false;
LibCtruResult(ctru_sys::CAMU_IsAutoWhiteBalance( ResultCode(ctru_sys::CAMU_IsAutoWhiteBalance(
&mut enabled, &mut enabled,
self.camera_as_raw(), self.camera_as_raw(),
))?; ))?;
@ -487,7 +487,7 @@ pub trait Camera {
/// Sets the flip direction of the camera's image based on the passed [CamFlip] argument /// Sets the flip direction of the camera's image based on the passed [CamFlip] argument
fn flip_image(&mut self, flip: CamFlip) -> crate::Result<()> { fn flip_image(&mut self, flip: CamFlip) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_FlipImage( ResultCode(ctru_sys::CAMU_FlipImage(
self.camera_as_raw(), self.camera_as_raw(),
flip.bits(), flip.bits(),
ctru_sys::CONTEXT_A, ctru_sys::CONTEXT_A,
@ -516,7 +516,7 @@ pub trait Camera {
crop_1: (i16, i16), crop_1: (i16, i16),
) -> crate::Result<()> { ) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetDetailSize( ResultCode(ctru_sys::CAMU_SetDetailSize(
self.camera_as_raw(), self.camera_as_raw(),
width, width,
height, height,
@ -533,7 +533,7 @@ pub trait Camera {
/// Sets the view size of the camera based on the passed [CamSize] argument. /// Sets the view size of the camera based on the passed [CamSize] argument.
fn set_view_size(&mut self, size: CamSize) -> crate::Result<()> { fn set_view_size(&mut self, size: CamSize) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetSize( ResultCode(ctru_sys::CAMU_SetSize(
self.camera_as_raw(), self.camera_as_raw(),
size.bits(), size.bits(),
ctru_sys::CONTEXT_A, ctru_sys::CONTEXT_A,
@ -545,7 +545,7 @@ pub trait Camera {
/// Sets the frame rate of the camera based on the passed [CamFrameRate] argument. /// Sets the frame rate of the camera based on the passed [CamFrameRate] argument.
fn set_frame_rate(&mut self, frame_rate: CamFrameRate) -> crate::Result<()> { fn set_frame_rate(&mut self, frame_rate: CamFrameRate) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetFrameRate( ResultCode(ctru_sys::CAMU_SetFrameRate(
self.camera_as_raw(), self.camera_as_raw(),
frame_rate.bits(), frame_rate.bits(),
))?; ))?;
@ -556,7 +556,7 @@ pub trait Camera {
/// Sets the photo mode of the camera based on the passed [CamPhotoMode] argument. /// Sets the photo mode of the camera based on the passed [CamPhotoMode] argument.
fn set_photo_mode(&mut self, photo_mode: CamPhotoMode) -> crate::Result<()> { fn set_photo_mode(&mut self, photo_mode: CamPhotoMode) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetPhotoMode( ResultCode(ctru_sys::CAMU_SetPhotoMode(
self.camera_as_raw(), self.camera_as_raw(),
photo_mode.bits(), photo_mode.bits(),
))?; ))?;
@ -569,7 +569,7 @@ pub trait Camera {
/// Multiple effects can be set at once by combining the bitflags of [CamEffect] /// Multiple effects can be set at once by combining the bitflags of [CamEffect]
fn set_effect(&mut self, effect: CamEffect) -> crate::Result<()> { fn set_effect(&mut self, effect: CamEffect) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetEffect( ResultCode(ctru_sys::CAMU_SetEffect(
self.camera_as_raw(), self.camera_as_raw(),
effect.bits(), effect.bits(),
ctru_sys::CONTEXT_A, ctru_sys::CONTEXT_A,
@ -581,7 +581,7 @@ pub trait Camera {
/// Sets the contrast of the camera based on the passed [CamContrast] argument. /// Sets the contrast of the camera based on the passed [CamContrast] argument.
fn set_contrast(&mut self, contrast: CamContrast) -> crate::Result<()> { fn set_contrast(&mut self, contrast: CamContrast) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetContrast( ResultCode(ctru_sys::CAMU_SetContrast(
self.camera_as_raw(), self.camera_as_raw(),
contrast.bits(), contrast.bits(),
))?; ))?;
@ -592,7 +592,7 @@ pub trait Camera {
/// Sets the lens correction of the camera based on the passed [CamLensCorrection] argument. /// Sets the lens correction of the camera based on the passed [CamLensCorrection] argument.
fn set_lens_correction(&mut self, lens_correction: CamLensCorrection) -> crate::Result<()> { fn set_lens_correction(&mut self, lens_correction: CamLensCorrection) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetLensCorrection( ResultCode(ctru_sys::CAMU_SetLensCorrection(
self.camera_as_raw(), self.camera_as_raw(),
lens_correction.bits(), lens_correction.bits(),
))?; ))?;
@ -603,7 +603,7 @@ pub trait Camera {
/// Sets the output format of the camera based on the passed [CamOutputFormat] argument. /// Sets the output format of the camera based on the passed [CamOutputFormat] argument.
fn set_output_format(&mut self, format: CamOutputFormat) -> crate::Result<()> { fn set_output_format(&mut self, format: CamOutputFormat) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetOutputFormat( ResultCode(ctru_sys::CAMU_SetOutputFormat(
self.camera_as_raw(), self.camera_as_raw(),
format.bits(), format.bits(),
ctru_sys::CONTEXT_A, ctru_sys::CONTEXT_A,
@ -628,7 +628,7 @@ pub trait Camera {
height: i16, height: i16,
) -> crate::Result<()> { ) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetAutoExposureWindow( ResultCode(ctru_sys::CAMU_SetAutoExposureWindow(
self.camera_as_raw(), self.camera_as_raw(),
x, x,
y, y,
@ -655,7 +655,7 @@ pub trait Camera {
height: i16, height: i16,
) -> crate::Result<()> { ) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetAutoWhiteBalanceWindow( ResultCode(ctru_sys::CAMU_SetAutoWhiteBalanceWindow(
self.camera_as_raw(), self.camera_as_raw(),
x, x,
y, y,
@ -669,7 +669,7 @@ pub trait Camera {
/// Sets whether the noise filter should be enabled or disabled for the camera /// Sets whether the noise filter should be enabled or disabled for the camera
fn set_noise_filter(&mut self, enabled: bool) -> crate::Result<()> { fn set_noise_filter(&mut self, enabled: bool) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetNoiseFilter(self.camera_as_raw(), enabled))?; ResultCode(ctru_sys::CAMU_SetNoiseFilter(self.camera_as_raw(), enabled))?;
Ok(()) Ok(())
} }
} }
@ -681,7 +681,7 @@ pub trait Camera {
data: ImageQualityCalibrationData, data: ImageQualityCalibrationData,
) -> crate::Result<()> { ) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetImageQualityCalibrationData(data.0))?; ResultCode(ctru_sys::CAMU_SetImageQualityCalibrationData(data.0))?;
Ok(()) Ok(())
} }
} }
@ -690,7 +690,7 @@ pub trait Camera {
fn get_image_quality_calibration_data(&self) -> crate::Result<ImageQualityCalibrationData> { fn get_image_quality_calibration_data(&self) -> crate::Result<ImageQualityCalibrationData> {
unsafe { unsafe {
let mut data = ImageQualityCalibrationData::default(); let mut data = ImageQualityCalibrationData::default();
LibCtruResult(ctru_sys::CAMU_GetImageQualityCalibrationData(&mut data.0))?; ResultCode(ctru_sys::CAMU_GetImageQualityCalibrationData(&mut data.0))?;
Ok(data) Ok(data)
} }
} }
@ -699,7 +699,7 @@ pub trait Camera {
// TODO: Explain sleep camera // TODO: Explain sleep camera
fn set_sleep_camera(&mut self) -> crate::Result<()> { fn set_sleep_camera(&mut self) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetSleepCamera(self.camera_as_raw()))?; ResultCode(ctru_sys::CAMU_SetSleepCamera(self.camera_as_raw()))?;
Ok(()) Ok(())
} }
} }
@ -723,7 +723,7 @@ pub trait Camera {
) -> crate::Result<Vec<u8>> { ) -> crate::Result<Vec<u8>> {
let transfer_unit = unsafe { let transfer_unit = unsafe {
let mut buf_size = 0; let mut buf_size = 0;
LibCtruResult(ctru_sys::CAMU_GetMaxBytes( ResultCode(ctru_sys::CAMU_GetMaxBytes(
&mut buf_size, &mut buf_size,
width as i16, width as i16,
height as i16, height as i16,
@ -736,7 +736,7 @@ pub trait Camera {
let mut buf = vec![0u8; usize::try_from(screen_size).unwrap()]; let mut buf = vec![0u8; usize::try_from(screen_size).unwrap()];
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_SetTransferBytes( ResultCode(ctru_sys::CAMU_SetTransferBytes(
self.port_as_raw(), self.port_as_raw(),
transfer_unit, transfer_unit,
width as i16, width as i16,
@ -745,14 +745,14 @@ pub trait Camera {
}; };
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_Activate(self.camera_as_raw()))?; ResultCode(ctru_sys::CAMU_Activate(self.camera_as_raw()))?;
LibCtruResult(ctru_sys::CAMU_ClearBuffer(self.port_as_raw()))?; ResultCode(ctru_sys::CAMU_ClearBuffer(self.port_as_raw()))?;
LibCtruResult(ctru_sys::CAMU_StartCapture(self.port_as_raw()))?; ResultCode(ctru_sys::CAMU_StartCapture(self.port_as_raw()))?;
}; };
let receive_event = unsafe { let receive_event = unsafe {
let mut completion_handle: Handle = 0; let mut completion_handle: Handle = 0;
LibCtruResult(ctru_sys::CAMU_SetReceiving( ResultCode(ctru_sys::CAMU_SetReceiving(
&mut completion_handle, &mut completion_handle,
buf.as_mut_ptr() as *mut ::libc::c_void, buf.as_mut_ptr() as *mut ::libc::c_void,
self.port_as_raw(), self.port_as_raw(),
@ -763,13 +763,13 @@ pub trait Camera {
}?; }?;
unsafe { unsafe {
LibCtruResult(ctru_sys::svcWaitSynchronization( ResultCode(ctru_sys::svcWaitSynchronization(
receive_event, receive_event,
timeout.as_nanos().try_into().unwrap(), timeout.as_nanos().try_into().unwrap(),
))?; ))?;
LibCtruResult(ctru_sys::CAMU_StopCapture(self.port_as_raw()))?; ResultCode(ctru_sys::CAMU_StopCapture(self.port_as_raw()))?;
LibCtruResult(ctru_sys::svcCloseHandle(receive_event))?; ResultCode(ctru_sys::svcCloseHandle(receive_event))?;
LibCtruResult(ctru_sys::CAMU_Activate(ctru_sys::SELECT_NONE))?; ResultCode(ctru_sys::CAMU_Activate(ctru_sys::SELECT_NONE))?;
}; };
Ok(buf) Ok(buf)
@ -786,7 +786,7 @@ impl Cam {
/// rare in practice. /// rare in practice.
pub fn init() -> crate::Result<Cam> { pub fn init() -> crate::Result<Cam> {
unsafe { unsafe {
LibCtruResult(ctru_sys::camInit())?; ResultCode(ctru_sys::camInit())?;
Ok(Cam { Ok(Cam {
inner_cam: InwardCam, inner_cam: InwardCam,
outer_right_cam: OutwardRightCam, outer_right_cam: OutwardRightCam,
@ -799,7 +799,7 @@ impl Cam {
/// Plays the specified sound based on the [CamShutterSoundType] argument /// Plays the specified sound based on the [CamShutterSoundType] argument
pub fn play_shutter_sound(&self, sound: CamShutterSoundType) -> crate::Result<()> { pub fn play_shutter_sound(&self, sound: CamShutterSoundType) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::CAMU_PlayShutterSound(sound.bits()))?; ResultCode(ctru_sys::CAMU_PlayShutterSound(sound.bits()))?;
Ok(()) Ok(())
} }
} }

14
ctru-rs/src/services/cfgu.rs

@ -2,7 +2,7 @@
//! //!
//! This module contains basic methods to retrieve and change configuration from the console. //! This module contains basic methods to retrieve and change configuration from the console.
use crate::error::LibCtruResult; use crate::error::ResultCode;
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
#[repr(u32)] #[repr(u32)]
@ -62,7 +62,7 @@ impl Cfgu {
/// as many times as desired and the service will not exit until all /// as many times as desired and the service will not exit until all
/// instances of Cfgu drop out of scope. /// instances of Cfgu drop out of scope.
pub fn init() -> crate::Result<Cfgu> { pub fn init() -> crate::Result<Cfgu> {
LibCtruResult(unsafe { ctru_sys::cfguInit() })?; ResultCode(unsafe { ctru_sys::cfguInit() })?;
Ok(Cfgu(())) Ok(Cfgu(()))
} }
@ -70,7 +70,7 @@ impl Cfgu {
pub fn get_region(&self) -> crate::Result<Region> { pub fn get_region(&self) -> crate::Result<Region> {
let mut region: u8 = 0; let mut region: u8 = 0;
LibCtruResult(unsafe { ctru_sys::CFGU_SecureInfoGetRegion(&mut region) })?; ResultCode(unsafe { ctru_sys::CFGU_SecureInfoGetRegion(&mut region) })?;
Ok(Region::try_from(region).unwrap()) Ok(Region::try_from(region).unwrap())
} }
@ -78,7 +78,7 @@ impl Cfgu {
pub fn get_model(&self) -> crate::Result<SystemModel> { pub fn get_model(&self) -> crate::Result<SystemModel> {
let mut model: u8 = 0; let mut model: u8 = 0;
LibCtruResult(unsafe { ctru_sys::CFGU_GetSystemModel(&mut model) })?; ResultCode(unsafe { ctru_sys::CFGU_GetSystemModel(&mut model) })?;
Ok(SystemModel::try_from(model).unwrap()) Ok(SystemModel::try_from(model).unwrap())
} }
@ -86,7 +86,7 @@ impl Cfgu {
pub fn get_language(&self) -> crate::Result<Language> { pub fn get_language(&self) -> crate::Result<Language> {
let mut language: u8 = 0; let mut language: u8 = 0;
LibCtruResult(unsafe { ctru_sys::CFGU_GetSystemLanguage(&mut language) })?; ResultCode(unsafe { ctru_sys::CFGU_GetSystemLanguage(&mut language) })?;
Ok(Language::try_from(language).unwrap()) Ok(Language::try_from(language).unwrap())
} }
@ -94,7 +94,7 @@ impl Cfgu {
pub fn is_nfc_supported(&self) -> crate::Result<bool> { pub fn is_nfc_supported(&self) -> crate::Result<bool> {
let mut supported: bool = false; let mut supported: bool = false;
LibCtruResult(unsafe { ctru_sys::CFGU_IsNFCSupported(&mut supported) })?; ResultCode(unsafe { ctru_sys::CFGU_IsNFCSupported(&mut supported) })?;
Ok(supported) Ok(supported)
} }
@ -102,7 +102,7 @@ impl Cfgu {
pub fn is_2ds_family(&self) -> crate::Result<bool> { pub fn is_2ds_family(&self) -> crate::Result<bool> {
let mut is_2ds_family: u8 = 0; let mut is_2ds_family: u8 = 0;
LibCtruResult(unsafe { ctru_sys::CFGU_GetModelNintendo2DS(&mut is_2ds_family) })?; ResultCode(unsafe { ctru_sys::CFGU_GetModelNintendo2DS(&mut is_2ds_family) })?;
Ok(is_2ds_family == 0) Ok(is_2ds_family == 0)
} }
} }

4
ctru-rs/src/services/hid.rs

@ -4,7 +4,7 @@
//! and circle pad information. It also provides information from the sound volume slider, //! and circle pad information. It also provides information from the sound volume slider,
//! the accelerometer, and the gyroscope. //! the accelerometer, and the gyroscope.
use crate::error::LibCtruResult; use crate::error::ResultCode;
bitflags::bitflags! { bitflags::bitflags! {
/// A set of flags corresponding to the button and directional pad /// A set of flags corresponding to the button and directional pad
/// inputs on the 3DS /// inputs on the 3DS
@ -63,7 +63,7 @@ pub struct CirclePosition(ctru_sys::circlePosition);
impl Hid { impl Hid {
pub fn init() -> crate::Result<Hid> { pub fn init() -> crate::Result<Hid> {
unsafe { unsafe {
LibCtruResult(ctru_sys::hidInit())?; ResultCode(ctru_sys::hidInit())?;
Ok(Hid(())) Ok(Hid(()))
} }
} }

8
ctru-rs/src/services/ps.rs

@ -4,7 +4,7 @@
//! As such, it is initialized by default in `ctru::init` instead of having a safety handler //! As such, it is initialized by default in `ctru::init` instead of having a safety handler
//! See also <https://www.3dbrew.org/wiki/Process_Services> //! See also <https://www.3dbrew.org/wiki/Process_Services>
use crate::error::LibCtruResult; use crate::error::ResultCode;
#[repr(u32)] #[repr(u32)]
pub enum AESAlgorithm { pub enum AESAlgorithm {
@ -33,19 +33,19 @@ pub enum AESKeyType {
pub fn local_friend_code_seed() -> crate::Result<u64> { pub fn local_friend_code_seed() -> crate::Result<u64> {
let mut seed: u64 = 0; let mut seed: u64 = 0;
LibCtruResult(unsafe { ctru_sys::PS_GetLocalFriendCodeSeed(&mut seed) })?; ResultCode(unsafe { ctru_sys::PS_GetLocalFriendCodeSeed(&mut seed) })?;
Ok(seed) Ok(seed)
} }
pub fn device_id() -> crate::Result<u32> { pub fn device_id() -> crate::Result<u32> {
let mut id: u32 = 0; let mut id: u32 = 0;
LibCtruResult(unsafe { ctru_sys::PS_GetDeviceId(&mut id) })?; ResultCode(unsafe { ctru_sys::PS_GetDeviceId(&mut id) })?;
Ok(id) Ok(id)
} }
pub fn generate_random_bytes(out: &mut [u8]) -> crate::Result<()> { pub fn generate_random_bytes(out: &mut [u8]) -> crate::Result<()> {
LibCtruResult(unsafe { ResultCode(unsafe {
ctru_sys::PS_GenerateRandomBytes(out as *mut _ as *mut _, out.len() as u32) ctru_sys::PS_GenerateRandomBytes(out as *mut _ as *mut _, out.len() as u32)
})?; })?;
Ok(()) Ok(())

4
ctru-rs/src/services/soc.rs

@ -3,7 +3,7 @@ use once_cell::sync::Lazy;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::sync::Mutex; use std::sync::Mutex;
use crate::error::LibCtruResult; use crate::error::ResultCode;
use crate::services::ServiceReference; use crate::services::ServiceReference;
use crate::Error; use crate::Error;
@ -39,7 +39,7 @@ impl Soc {
false, false,
|| { || {
let soc_mem = unsafe { memalign(0x1000, num_bytes) } as *mut u32; let soc_mem = unsafe { memalign(0x1000, num_bytes) } as *mut u32;
LibCtruResult(unsafe { ctru_sys::socInit(soc_mem, num_bytes as u32) })?; ResultCode(unsafe { ctru_sys::socInit(soc_mem, num_bytes as u32) })?;
Ok(()) Ok(())
}, },

6
ctru-rs/src/services/sslc.rs

@ -1,6 +1,6 @@
// TODO: Implement remaining functions // TODO: Implement remaining functions
use crate::error::LibCtruResult; use crate::error::ResultCode;
pub struct SslC(()); pub struct SslC(());
@ -8,7 +8,7 @@ impl SslC {
/// Initialize sslc /// Initialize sslc
pub fn init() -> crate::Result<Self> { pub fn init() -> crate::Result<Self> {
unsafe { unsafe {
LibCtruResult(ctru_sys::sslcInit(0))?; ResultCode(ctru_sys::sslcInit(0))?;
Ok(SslC(())) Ok(SslC(()))
} }
} }
@ -16,7 +16,7 @@ impl SslC {
/// Fill `buf` with `buf.len()` random bytes /// Fill `buf` with `buf.len()` random bytes
pub fn generate_random_data(&self, buf: &mut [u8]) -> crate::Result<()> { pub fn generate_random_data(&self, buf: &mut [u8]) -> crate::Result<()> {
unsafe { unsafe {
LibCtruResult(ctru_sys::sslcGenerateRandomData( ResultCode(ctru_sys::sslcGenerateRandomData(
buf.as_ptr() as _, buf.as_ptr() as _,
buf.len() as u32, buf.len() as u32,
))?; ))?;

Loading…
Cancel
Save