diff --git a/ctru-rs/examples/buttons.rs b/ctru-rs/examples/buttons.rs index ca4ba3a..e3250c4 100644 --- a/ctru-rs/examples/buttons.rs +++ b/ctru-rs/examples/buttons.rs @@ -8,7 +8,7 @@ fn main() { ctru::init(); let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); - let gfx = Gfx::init_default().unwrap(); + let gfx = Gfx::init().unwrap(); let console = Console::init(gfx.top_screen.borrow_mut()); println!("Hi there! Try pressing a button"); diff --git a/ctru-rs/examples/file-explorer.rs b/ctru-rs/examples/file-explorer.rs index 0451989..181c846 100644 --- a/ctru-rs/examples/file-explorer.rs +++ b/ctru-rs/examples/file-explorer.rs @@ -14,7 +14,7 @@ fn main() { ctru::init(); let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); - let gfx = Gfx::init_default().unwrap(); + let gfx = Gfx::init().unwrap(); #[cfg(all(feature = "romfs", romfs_exists))] let _romfs = ctru::romfs::RomFS::init().unwrap(); diff --git a/ctru-rs/examples/futures-basic.rs b/ctru-rs/examples/futures-basic.rs index dd78795..f027372 100644 --- a/ctru-rs/examples/futures-basic.rs +++ b/ctru-rs/examples/futures-basic.rs @@ -13,7 +13,7 @@ use futures::StreamExt; fn main() { ctru::init(); - let gfx = Gfx::init_default().expect("Couldn't obtaint GFX controller"); + 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 _console = Console::init(gfx.top_screen.borrow_mut()); diff --git a/ctru-rs/examples/futures-tokio.rs b/ctru-rs/examples/futures-tokio.rs index a478603..91f821e 100644 --- a/ctru-rs/examples/futures-tokio.rs +++ b/ctru-rs/examples/futures-tokio.rs @@ -6,7 +6,7 @@ use std::time::Duration; fn main() { ctru::init(); - let gfx = Gfx::init_default().expect("Couldn't obtaint GFX controller"); + 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 _console = Console::init(gfx.top_screen.borrow_mut()); diff --git a/ctru-rs/examples/gfx-wide-mode.rs b/ctru-rs/examples/gfx-wide-mode.rs index 1d39a2f..f84f5c4 100644 --- a/ctru-rs/examples/gfx-wide-mode.rs +++ b/ctru-rs/examples/gfx-wide-mode.rs @@ -7,7 +7,7 @@ fn main() { ctru::init(); let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); - let gfx = Gfx::init_default().unwrap(); + let gfx = Gfx::init().unwrap(); let mut console = Console::init(gfx.top_screen.borrow_mut()); println!("Press A to enable/disable wide screen mode."); diff --git a/ctru-rs/examples/graphics-bitmap.rs b/ctru-rs/examples/graphics-bitmap.rs index 822d03d..0d82ecf 100644 --- a/ctru-rs/examples/graphics-bitmap.rs +++ b/ctru-rs/examples/graphics-bitmap.rs @@ -19,7 +19,7 @@ static IMAGE: &[u8] = include_bytes!("assets/ferris.rgb"); fn main() { ctru::init(); - let gfx = Gfx::init_default().expect("Couldn't obtaint GFX controller"); + 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 _console = Console::init(gfx.top_screen.borrow_mut()); diff --git a/ctru-rs/examples/hashmaps.rs b/ctru-rs/examples/hashmaps.rs index 8dc26e2..0b07939 100644 --- a/ctru-rs/examples/hashmaps.rs +++ b/ctru-rs/examples/hashmaps.rs @@ -12,7 +12,7 @@ fn main() { ctru::init(); let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); - let gfx = Gfx::init_default().unwrap(); + let gfx = Gfx::init().unwrap(); let _console = Console::init(gfx.top_screen.borrow_mut()); let mut map = std::collections::HashMap::new(); diff --git a/ctru-rs/examples/hello-both-screens.rs b/ctru-rs/examples/hello-both-screens.rs index 5a96187..483e5c3 100644 --- a/ctru-rs/examples/hello-both-screens.rs +++ b/ctru-rs/examples/hello-both-screens.rs @@ -8,7 +8,7 @@ fn main() { ctru::init(); let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); - let gfx = Gfx::init_default().unwrap(); + let gfx = Gfx::init().unwrap(); // Start a console on the top screen let top_screen = Console::init(gfx.top_screen.borrow_mut()); diff --git a/ctru-rs/examples/hello-world.rs b/ctru-rs/examples/hello-world.rs index b620025..2fcd7fb 100644 --- a/ctru-rs/examples/hello-world.rs +++ b/ctru-rs/examples/hello-world.rs @@ -7,7 +7,7 @@ use std::io::BufWriter; fn main() { ctru::init(); - let gfx = Gfx::init_default().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 apt = Apt::init().expect("Couldn't obtain APT controller"); let _console = Console::init(gfx.top_screen.borrow_mut()); diff --git a/ctru-rs/examples/network-sockets.rs b/ctru-rs/examples/network-sockets.rs index c459619..2172d81 100644 --- a/ctru-rs/examples/network-sockets.rs +++ b/ctru-rs/examples/network-sockets.rs @@ -10,7 +10,7 @@ use std::time::Duration; fn main() { ctru::init(); - let gfx = Gfx::init_default().unwrap(); + let gfx = Gfx::init().unwrap(); let _console = Console::init(gfx.top_screen.borrow_mut()); let hid = Hid::init().unwrap(); let apt = Apt::init().unwrap(); diff --git a/ctru-rs/examples/romfs.rs b/ctru-rs/examples/romfs.rs index 6e7ece9..dc1d133 100644 --- a/ctru-rs/examples/romfs.rs +++ b/ctru-rs/examples/romfs.rs @@ -5,7 +5,7 @@ use ctru::services::hid::{Hid, KeyPad}; fn main() { ctru::init(); - let gfx = Gfx::init_default().expect("Couldn't obtaint GFX controller"); + 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 _console = Console::init(gfx.top_screen.borrow_mut()); diff --git a/ctru-rs/examples/software-keyboard.rs b/ctru-rs/examples/software-keyboard.rs index 6be9e81..799d65d 100644 --- a/ctru-rs/examples/software-keyboard.rs +++ b/ctru-rs/examples/software-keyboard.rs @@ -8,7 +8,7 @@ fn main() { ctru::init(); let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); - let gfx = Gfx::default(); + let gfx = Gfx::init().unwrap(); let _console = Console::init(gfx.top_screen.borrow_mut()); println!("Press A to enter some text or press Start to quit"); diff --git a/ctru-rs/examples/thread-basic.rs b/ctru-rs/examples/thread-basic.rs index 5da5560..09002a1 100644 --- a/ctru-rs/examples/thread-basic.rs +++ b/ctru-rs/examples/thread-basic.rs @@ -11,7 +11,7 @@ fn main() { ctru::init(); let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); - let gfx = Gfx::init_default().unwrap(); + let gfx = Gfx::init().unwrap(); let _console = Console::init(gfx.top_screen.borrow_mut()); let prio = thread::current().priority(); diff --git a/ctru-rs/examples/thread-locals.rs b/ctru-rs/examples/thread-locals.rs index a6618a1..593f90a 100644 --- a/ctru-rs/examples/thread-locals.rs +++ b/ctru-rs/examples/thread-locals.rs @@ -10,7 +10,7 @@ std::thread_local! { fn main() { ctru::init(); - let gfx = Gfx::init_default().expect("Couldn't obtaint GFX controller"); + let gfx = Gfx::init().expect("Couldn't obtain GFX controller"); gfx.top_screen.borrow_mut().set_wide_mode(true); let hid = Hid::init().expect("Couldn't obtain HID controller"); let apt = Apt::init().expect("Couldn't obtain APT controller"); diff --git a/ctru-rs/examples/time-rtc.rs b/ctru-rs/examples/time-rtc.rs index 696a7f0..b77615a 100644 --- a/ctru-rs/examples/time-rtc.rs +++ b/ctru-rs/examples/time-rtc.rs @@ -6,7 +6,7 @@ use ctru::services::hid::{Hid, KeyPad}; fn main() { ctru::init(); - let gfx = Gfx::init_default().expect("Couldn't obtaint GFX controller"); + 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"); diff --git a/ctru-rs/src/error.rs b/ctru-rs/src/error.rs index 790866c..2cfe55a 100644 --- a/ctru-rs/src/error.rs +++ b/ctru-rs/src/error.rs @@ -29,7 +29,10 @@ impl fmt::Debug for Error { .field("summary", &R_SUMMARY(err)) .field("level", &R_LEVEL(err)) .finish(), - Error::ServiceAlreadyActive(service) => write!(f, "Service {service} already active"), + Error::ServiceAlreadyActive(service) => f + .debug_tuple("ServiceAlreadyActive") + .field(&String::from(service)) + .finish(), } } } diff --git a/ctru-rs/src/gfx.rs b/ctru-rs/src/gfx.rs index b38419f..b03e64d 100644 --- a/ctru-rs/src/gfx.rs +++ b/ctru-rs/src/gfx.rs @@ -80,8 +80,8 @@ impl Gfx { /// Initialize the Gfx module with the chosen framebuffer formats for the top and bottom /// screens /// - /// Use `Gfx::init_default()` instead of this function to initialize the module with default parameters - pub fn init( + /// Use `Gfx::init()` instead of this function to initialize the module with default parameters + pub fn with_formats( top_fb_fmt: FramebufferFormat, bottom_fb_fmt: FramebufferFormat, use_vram_buffers: bool, @@ -102,9 +102,9 @@ impl Gfx { } /// Creates a new Gfx instance with default init values - /// It's the same as calling: `Gfx::init(FramebufferFormat::Bgr8, FramebufferFormat::Bgr8, false) - pub fn init_default() -> Result { - Gfx::init(FramebufferFormat::Bgr8, FramebufferFormat::Bgr8, false) + /// It's the same as calling: `Gfx::with_formats(FramebufferFormat::Bgr8, FramebufferFormat::Bgr8, false) + pub fn init() -> Result { + Gfx::with_formats(FramebufferFormat::Bgr8, FramebufferFormat::Bgr8, false) } /// Flushes the current framebuffers @@ -216,7 +216,7 @@ impl Drop for Gfx { fn drop(&mut self) { unsafe { ctru_sys::gfxExit() }; - GFX_ACTIVE.store(false, Ordering::Release); + GFX_ACTIVE.store(false, Ordering::SeqCst); } } @@ -227,6 +227,9 @@ mod tests { #[test] fn gfx_duplicate() { // We don't need to build a `Gfx` because the test runner has one already - assert!(Gfx::init_default().is_err()); + match Gfx::init() { + Err(Error::ServiceAlreadyActive("Gfx")) => return, + _ => panic!(), + } } } diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 85b9c63..8cad7a9 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -45,7 +45,7 @@ pub fn init() { // The problem here is that the `panic_hook` is run _before_ the app cleanup, // so an `Hid` stuct may still be alive and thus make the `panic_hook` panic. // If that were to happen, the system would have to reboot to properly close the app. - // + // // Using `hidInit` is safe when another instance is open, and we can do safe operations afterwards. // The only (probably) unsafe part of this system is the `hidExit`, since in a multithreaded // environment some other threads may still be doing operations on the service diff --git a/ctru-rs/src/romfs.rs b/ctru-rs/src/romfs.rs index 19b0990..f09c25e 100644 --- a/ctru-rs/src/romfs.rs +++ b/ctru-rs/src/romfs.rs @@ -43,7 +43,7 @@ impl Drop for RomFS { let mount_name = CStr::from_bytes_with_nul(b"romfs\0").unwrap(); unsafe { ctru_sys::romfsUnmount(mount_name.as_ptr()) }; - ROMFS_ACTIVE.store(false, Ordering::Release); + ROMFS_ACTIVE.store(false, Ordering::SeqCst); } } @@ -55,6 +55,9 @@ mod tests { fn romfs_duplicate() { let _romfs = RomFS::init().unwrap(); - assert!(RomFS::init().is_err()); + match RomFS::init() { + Err(Error::ServiceAlreadyActive("RomFS")) => return, + _ => panic!(), + } } } diff --git a/ctru-rs/src/services/apt.rs b/ctru-rs/src/services/apt.rs index 2e425ff..85fb710 100644 --- a/ctru-rs/src/services/apt.rs +++ b/ctru-rs/src/services/apt.rs @@ -40,7 +40,7 @@ impl Drop for Apt { fn drop(&mut self) { unsafe { ctru_sys::aptExit() }; - APT_ACTIVE.store(false, Ordering::Release); + APT_ACTIVE.store(false, Ordering::SeqCst); } } @@ -49,9 +49,11 @@ mod tests { use super::*; #[test] - fn gfx_duplicate() { + fn apt_duplicate() { // We don't need to build a `Apt` because the test runner has one already - assert!(Apt::init().is_err()); + match Apt::init() { + Err(Error::ServiceAlreadyActive("Apt")) => return, + _ => panic!(), + } } } - diff --git a/ctru-rs/src/services/fs.rs b/ctru-rs/src/services/fs.rs index cc9bbe7..b4c7cfa 100644 --- a/ctru-rs/src/services/fs.rs +++ b/ctru-rs/src/services/fs.rs @@ -1004,7 +1004,7 @@ impl Drop for Fs { ctru_sys::fsExit(); } - FS_ACTIVE.store(false, Ordering::Release); + FS_ACTIVE.store(false, Ordering::SeqCst); } } @@ -1083,7 +1083,9 @@ mod tests { fn fs_duplicate() { let _fs = Fs::init().unwrap(); - assert!(Fs::init().is_err()); + match Fs::init() { + Err(Error::ServiceAlreadyActive("Fs")) => return, + _ => panic!(), + } } } - diff --git a/ctru-rs/src/services/hid.rs b/ctru-rs/src/services/hid.rs index 6ad67fb..bbeefb4 100644 --- a/ctru-rs/src/services/hid.rs +++ b/ctru-rs/src/services/hid.rs @@ -164,7 +164,7 @@ impl Drop for Hid { fn drop(&mut self) { unsafe { ctru_sys::hidExit() }; - HID_ACTIVE.store(false, Ordering::Release); + HID_ACTIVE.store(false, Ordering::SeqCst); } } @@ -175,6 +175,9 @@ mod tests { #[test] fn hid_duplicate() { // We don't need to build a `Hid` because the test runner has one already - assert!(Hid::init().is_err()); + match Hid::init() { + Err(Error::ServiceAlreadyActive("Hid")) => return, + _ => panic!(), + } } } diff --git a/ctru-rs/src/services/soc.rs b/ctru-rs/src/services/soc.rs index a15541a..3bad215 100644 --- a/ctru-rs/src/services/soc.rs +++ b/ctru-rs/src/services/soc.rs @@ -61,7 +61,7 @@ impl Drop for Soc { free(self.soc_mem as *mut _); } - SOC_ACTIVE.store(false, Ordering::Release); + SOC_ACTIVE.store(false, Ordering::SeqCst); } } @@ -73,6 +73,9 @@ mod tests { fn soc_duplicate() { let _soc = Soc::init().unwrap(); - assert!(Soc::init().is_err()); + match Soc::init() { + Err(Error::ServiceAlreadyActive("Soc")) => return, + _ => panic!(), + } } } diff --git a/ctru-rs/src/services/sslc.rs b/ctru-rs/src/services/sslc.rs index 0b2192e..4778819 100644 --- a/ctru-rs/src/services/sslc.rs +++ b/ctru-rs/src/services/sslc.rs @@ -40,7 +40,7 @@ impl Drop for SslC { fn drop(&mut self) { unsafe { ctru_sys::sslcExit() }; - SSLC_ACTIVE.store(false, Ordering::Release); + SSLC_ACTIVE.store(false, Ordering::SeqCst); } } @@ -52,7 +52,9 @@ mod tests { fn sslc_duplicate() { let _sslc = SslC::init().unwrap(); - assert!(SslC::init().is_err()); + match SslC::init() { + Err(Error::ServiceAlreadyActive("SslC")) => return, + _ => panic!(), + } } } - diff --git a/ctru-rs/src/srv.rs b/ctru-rs/src/srv.rs index 8cca5c3..d817b94 100644 --- a/ctru-rs/src/srv.rs +++ b/ctru-rs/src/srv.rs @@ -27,7 +27,7 @@ impl Drop for Srv { fn drop(&mut self) { unsafe { ctru_sys::srvExit() }; - SRV_ACTIVE.store(false, Ordering::Release); + SRV_ACTIVE.store(false, Ordering::SeqCst); } } @@ -39,6 +39,9 @@ mod tests { fn srv_duplicate() { let _srv = Srv::init().unwrap(); - assert!(Srv::init().is_err()); + match Srv::init() { + Err(Error::ServiceAlreadyActive("Srv")) => return, + _ => panic!(), + } } } diff --git a/ctru-rs/src/test_runner.rs b/ctru-rs/src/test_runner.rs index 0e9ab06..ab4c60f 100644 --- a/ctru-rs/src/test_runner.rs +++ b/ctru-rs/src/test_runner.rs @@ -17,7 +17,7 @@ use crate::services::Apt; pub(crate) fn run(tests: &[&TestDescAndFn]) { crate::init(); - let gfx = Gfx::init_default().unwrap(); + let gfx = Gfx::init().unwrap(); let hid = Hid::init().unwrap(); let apt = Apt::init().unwrap();