Browse Source

Merge pull request #92 from rust3ds/refresh

Remove init function
pull/101/head
Meziu 2 years ago committed by GitHub
parent
commit
c8b9bf0592
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      ctru-rs/examples/audio-filters.rs
  2. 4
      ctru-rs/examples/buttons.rs
  3. 2
      ctru-rs/examples/camera-image.rs
  4. 3
      ctru-rs/examples/file-explorer.rs
  5. 3
      ctru-rs/examples/futures-basic.rs
  6. 3
      ctru-rs/examples/futures-tokio.rs
  7. 3
      ctru-rs/examples/gfx-3d-mode.rs
  8. 3
      ctru-rs/examples/gfx-wide-mode.rs
  9. 3
      ctru-rs/examples/graphics-bitmap.rs
  10. 5
      ctru-rs/examples/hashmaps.rs
  11. 4
      ctru-rs/examples/hello-both-screens.rs
  12. 3
      ctru-rs/examples/hello-world.rs
  13. 3
      ctru-rs/examples/linear-memory.rs
  14. 2
      ctru-rs/examples/mii-selector.rs
  15. 3
      ctru-rs/examples/network-sockets.rs
  16. 3
      ctru-rs/examples/output-3dslink.rs
  17. 3
      ctru-rs/examples/romfs.rs
  18. 3
      ctru-rs/examples/software-keyboard.rs
  19. 3
      ctru-rs/examples/system-configuration.rs
  20. 4
      ctru-rs/examples/thread-basic.rs
  21. 3
      ctru-rs/examples/thread-info.rs
  22. 3
      ctru-rs/examples/thread-locals.rs
  23. 2
      ctru-rs/examples/time-rtc.rs
  24. 7
      ctru-rs/src/applets/swkbd.rs
  25. 37
      ctru-rs/src/lib.rs
  26. 35
      ctru-rs/src/services/ps.rs
  27. 2
      ctru-rs/src/test_runner.rs

3
ctru-rs/examples/audio-filters.rs

@ -35,7 +35,8 @@ fn fill_buffer(audio_data: &mut [u8], frequency: f32) {
} }
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller"); let apt = Apt::init().expect("Couldn't obtain APT controller");

4
ctru-rs/examples/buttons.rs

@ -1,8 +1,8 @@
use ctru::prelude::*; use ctru::prelude::*;
fn main() { fn main() {
// Setup services ctru::use_panic_handler();
ctru::init();
let apt = Apt::init().unwrap(); let apt = Apt::init().unwrap();
let hid = Hid::init().unwrap(); let hid = Hid::init().unwrap();
let gfx = Gfx::init().unwrap(); let gfx = Gfx::init().unwrap();

2
ctru-rs/examples/camera-image.rs

@ -15,7 +15,7 @@ const BUF_SIZE: usize = WIDTH * HEIGHT * 2 * 2;
const WAIT_TIMEOUT: Duration = Duration::from_micros(300); const WAIT_TIMEOUT: Duration = Duration::from_micros(300);
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let apt = Apt::init().expect("Failed to initialize Apt service."); let apt = Apt::init().expect("Failed to initialize Apt service.");
let hid = Hid::init().expect("Failed to initialize Hid service."); let hid = Hid::init().expect("Failed to initialize Hid service.");

3
ctru-rs/examples/file-explorer.rs

@ -9,7 +9,8 @@ use std::os::horizon::fs::MetadataExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let apt = Apt::init().unwrap(); let apt = Apt::init().unwrap();
let hid = Hid::init().unwrap(); let hid = Hid::init().unwrap();
let gfx = Gfx::init().unwrap(); let gfx = Gfx::init().unwrap();

3
ctru-rs/examples/futures-basic.rs

@ -13,7 +13,8 @@ use futures::StreamExt;
use std::os::horizon::thread::BuilderExt; use std::os::horizon::thread::BuilderExt;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller"); let apt = Apt::init().expect("Couldn't obtain APT controller");

3
ctru-rs/examples/futures-tokio.rs

@ -6,7 +6,8 @@ use std::os::horizon::thread::BuilderExt;
use std::time::Duration; use std::time::Duration;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller"); let apt = Apt::init().expect("Couldn't obtain APT controller");

3
ctru-rs/examples/gfx-3d-mode.rs

@ -10,7 +10,8 @@ const IMAGE: &[u8] = include_bytes!("assets/ferris.rgb");
static ZERO: &[u8] = &[0; IMAGE.len()]; static ZERO: &[u8] = &[0; IMAGE.len()];
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller"); let apt = Apt::init().expect("Couldn't obtain APT controller");

3
ctru-rs/examples/gfx-wide-mode.rs

@ -1,7 +1,8 @@
use ctru::prelude::*; use ctru::prelude::*;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let apt = Apt::init().unwrap(); let apt = Apt::init().unwrap();
let hid = Hid::init().unwrap(); let hid = Hid::init().unwrap();
let gfx = Gfx::init().unwrap(); let gfx = Gfx::init().unwrap();

3
ctru-rs/examples/graphics-bitmap.rs

@ -15,7 +15,8 @@ use ctru::prelude::*;
static IMAGE: &[u8] = include_bytes!("assets/ferris.rgb"); static IMAGE: &[u8] = include_bytes!("assets/ferris.rgb");
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller"); let apt = Apt::init().expect("Couldn't obtain APT controller");

5
ctru-rs/examples/hashmaps.rs

@ -1,12 +1,13 @@
use ctru::prelude::*; use ctru::prelude::*;
fn main() { fn main() {
ctru::use_panic_handler();
// Initialize services // Initialize services
// //
// HashMaps generate hashes thanks to the 3DS' cryptografically secure generator. // HashMaps generate hashes thanks to the 3DS' cryptografically secure generator.
// This generator is only active when activating the `PS` service. // This generator is only active when activating the `PS` service.
// This service is automatically initialized in `ctru::init` // This service is automatically initialized.
ctru::init();
let apt = Apt::init().unwrap(); let apt = Apt::init().unwrap();
let hid = Hid::init().unwrap(); let hid = Hid::init().unwrap();
let gfx = Gfx::init().unwrap(); let gfx = Gfx::init().unwrap();

4
ctru-rs/examples/hello-both-screens.rs

@ -1,8 +1,8 @@
use ctru::prelude::*; use ctru::prelude::*;
fn main() { fn main() {
// Initialize services ctru::use_panic_handler();
ctru::init();
let apt = Apt::init().unwrap(); let apt = Apt::init().unwrap();
let hid = Hid::init().unwrap(); let hid = Hid::init().unwrap();
let gfx = Gfx::init().unwrap(); let gfx = Gfx::init().unwrap();

3
ctru-rs/examples/hello-world.rs

@ -3,7 +3,8 @@ use ctru::prelude::*;
use std::io::BufWriter; use std::io::BufWriter;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller"); let apt = Apt::init().expect("Couldn't obtain APT controller");

3
ctru-rs/examples/linear-memory.rs

@ -4,7 +4,8 @@ use ctru::linear::LinearAllocator;
use ctru::prelude::*; use ctru::prelude::*;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller"); let apt = Apt::init().expect("Couldn't obtain APT controller");

2
ctru-rs/examples/mii-selector.rs

@ -2,7 +2,7 @@ use ctru::applets::mii_selector::MiiSelector;
use ctru::prelude::*; use ctru::prelude::*;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");

3
ctru-rs/examples/network-sockets.rs

@ -5,7 +5,8 @@ use std::net::{Shutdown, TcpListener};
use std::time::Duration; use std::time::Duration;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().unwrap(); let gfx = Gfx::init().unwrap();
let _console = Console::init(gfx.top_screen.borrow_mut()); let _console = Console::init(gfx.top_screen.borrow_mut());
let hid = Hid::init().unwrap(); let hid = Hid::init().unwrap();

3
ctru-rs/examples/output-3dslink.rs

@ -11,7 +11,8 @@
use ctru::prelude::*; use ctru::prelude::*;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller"); let apt = Apt::init().expect("Couldn't obtain APT controller");

3
ctru-rs/examples/romfs.rs

@ -1,7 +1,8 @@
use ctru::prelude::*; use ctru::prelude::*;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller"); let apt = Apt::init().expect("Couldn't obtain APT controller");

3
ctru-rs/examples/software-keyboard.rs

@ -2,7 +2,8 @@ use ctru::applets::swkbd::{Button, Swkbd};
use ctru::prelude::*; use ctru::prelude::*;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let apt = Apt::init().unwrap(); let apt = Apt::init().unwrap();
let hid = Hid::init().unwrap(); let hid = Hid::init().unwrap();
let gfx = Gfx::init().unwrap(); let gfx = Gfx::init().unwrap();

3
ctru-rs/examples/system-configuration.rs

@ -2,7 +2,8 @@ use ctru::prelude::*;
use ctru::services::cfgu::Cfgu; use ctru::services::cfgu::Cfgu;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller"); let apt = Apt::init().expect("Couldn't obtain APT controller");

4
ctru-rs/examples/thread-basic.rs

@ -6,8 +6,8 @@ use std::os::horizon::thread::BuilderExt;
use std::time::Duration; use std::time::Duration;
fn main() { fn main() {
// Initialize services ctru::use_panic_handler();
ctru::init();
let apt = Apt::init().unwrap(); let apt = Apt::init().unwrap();
let hid = Hid::init().unwrap(); let hid = Hid::init().unwrap();
let gfx = Gfx::init().unwrap(); let gfx = Gfx::init().unwrap();

3
ctru-rs/examples/thread-info.rs

@ -7,7 +7,8 @@ use ctru::prelude::*;
use std::os::horizon::thread::BuilderExt; use std::os::horizon::thread::BuilderExt;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller"); let apt = Apt::init().expect("Couldn't obtain APT controller");

3
ctru-rs/examples/thread-locals.rs

@ -10,7 +10,8 @@ std::thread_local! {
} }
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
gfx.top_screen.borrow_mut().set_wide_mode(true); gfx.top_screen.borrow_mut().set_wide_mode(true);
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");

2
ctru-rs/examples/time-rtc.rs

@ -1,7 +1,7 @@
use ctru::prelude::*; use ctru::prelude::*;
fn main() { fn main() {
ctru::init(); ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller"); let hid = Hid::init().expect("Couldn't obtain HID controller");

7
ctru-rs/src/applets/swkbd.rs

@ -3,7 +3,6 @@ use ctru_sys::{
self, swkbdInit, swkbdInputText, swkbdSetButton, swkbdSetFeatures, swkbdSetHintText, SwkbdState, self, swkbdInit, swkbdInputText, swkbdSetButton, swkbdSetFeatures, swkbdSetHintText, SwkbdState,
}; };
use libc; use libc;
use std::convert::TryInto;
use std::iter::once; use std::iter::once;
use std::str; use std::str;
@ -127,11 +126,7 @@ impl Swkbd {
/// 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<Button, Error> { pub fn get_bytes(&mut self, buf: &mut [u8]) -> Result<Button, Error> {
unsafe { unsafe {
match swkbdInputText( match swkbdInputText(self.state.as_mut(), buf.as_mut_ptr(), buf.len()) {
self.state.as_mut(),
buf.as_mut_ptr(),
buf.len().try_into().unwrap(),
) {
ctru_sys::SWKBD_BUTTON_NONE => Err(self.parse_swkbd_error()), ctru_sys::SWKBD_BUTTON_NONE => Err(self.parse_swkbd_error()),
ctru_sys::SWKBD_BUTTON_LEFT => Ok(Button::Left), ctru_sys::SWKBD_BUTTON_LEFT => Ok(Button::Left),
ctru_sys::SWKBD_BUTTON_MIDDLE => Ok(Button::Middle), ctru_sys::SWKBD_BUTTON_MIDDLE => Ok(Button::Middle),

37
ctru-rs/src/lib.rs

@ -7,40 +7,25 @@
#![feature(nonnull_slice_from_raw_parts)] #![feature(nonnull_slice_from_raw_parts)]
#![test_runner(test_runner::run)] #![test_runner(test_runner::run)]
extern "C" fn services_deinit() { // Nothing is imported from these crates but their inclusion here assures correct linking of the missing implementations.
unsafe { extern crate linker_fix_3ds;
ctru_sys::psExit(); extern crate pthread_3ds;
}
}
#[no_mangle] #[no_mangle]
#[cfg(feature = "big-stack")] #[cfg(feature = "big-stack")]
static __stacksize__: usize = 2 * 1024 * 1024; // 2MB static __stacksize__: usize = 2 * 1024 * 1024; // 2MB
/// Call this somewhere to force Rust to link some required crates /// Activate ´ctru-rs´' default panic handler.
/// This is also a setup for some crate integration only available at runtime
/// ///
/// See <https://github.com/rust-lang/rust/issues/47384> /// With this implementation, the main thread will stop and try to print debug info to an available [console::Console].
pub fn init() { /// In case it fails to find an active [console::Console], the program will just exit.
linker_fix_3ds::init(); ///
pthread_3ds::init(); /// # Notes
///
/// When ´test´ is enabled, this function won't do anything, as it should be overridden by the ´test´ environment.
pub fn use_panic_handler() {
#[cfg(not(test))] #[cfg(not(test))]
panic_hook_setup(); panic_hook_setup();
// Initialize the PS service for random data generation
unsafe {
let ps_ret = ctru_sys::psInit();
if ctru_sys::R_FAILED(ps_ret) {
panic!(
"Failed to initialize random data generation: {:?}",
Error::from(ps_ret)
)
}
// Setup the deconstruction at the program's end
libc::atexit(services_deinit);
}
} }
#[cfg(not(test))] #[cfg(not(test))]

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

@ -1,10 +1,10 @@
//! Process Services (PS) module. This is used for miscellaneous utility tasks, but //! Process Services (PS) module. This is used for miscellaneous utility tasks, but
//! is particularly important because it is used to generate random data, which //! is particularly important because it is used to generate random data, which
//! is required for common things like [`HashMap`](std::collections::HashMap). //! is required for common things like [`HashMap`](std::collections::HashMap).
//! 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::ResultCode; use crate::error::ResultCode;
use crate::Result;
#[repr(u32)] #[repr(u32)]
pub enum AESAlgorithm { pub enum AESAlgorithm {
@ -30,23 +30,44 @@ pub enum AESKeyType {
Keyslot39Nfc, Keyslot39Nfc,
} }
pub fn local_friend_code_seed() -> crate::Result<u64> { pub struct Ps(());
impl Ps {
pub fn new() -> Result<Self> {
unsafe {
ResultCode(ctru_sys::psInit())?;
Ok(Ps(()))
}
}
pub fn local_friend_code_seed(&self) -> crate::Result<u64> {
let mut seed: u64 = 0; let mut seed: u64 = 0;
ResultCode(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(&self) -> crate::Result<u32> {
let mut id: u32 = 0; let mut id: u32 = 0;
ResultCode(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(&self, out: &mut [u8]) -> crate::Result<()> {
ResultCode(unsafe { ctru_sys::PS_GenerateRandomBytes(out as *mut _ as *mut _, out.len()) })?; ResultCode(unsafe {
ctru_sys::PS_GenerateRandomBytes(out as *mut _ as *mut _, out.len())
})?;
Ok(()) Ok(())
}
}
impl Drop for Ps {
fn drop(&mut self) {
unsafe {
ctru_sys::psExit();
}
}
} }
#[cfg(test)] #[cfg(test)]

2
ctru-rs/src/test_runner.rs

@ -15,8 +15,6 @@ use crate::services::Apt;
/// runs all tests in series, "failing" on the first one to panic (really, the /// runs all tests in series, "failing" on the first one to panic (really, the
/// panic is just treated the same as any normal application panic). /// panic is just treated the same as any normal application panic).
pub(crate) fn run(tests: &[&TestDescAndFn]) { pub(crate) fn run(tests: &[&TestDescAndFn]) {
crate::init();
let gfx = Gfx::init().unwrap(); let gfx = Gfx::init().unwrap();
let hid = Hid::init().unwrap(); let hid = Hid::init().unwrap();
let apt = Apt::init().unwrap(); let apt = Apt::init().unwrap();

Loading…
Cancel
Save