From 5e5a88fa46964aa4785b4bcc8a0ac49c05f8deb6 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Thu, 2 Jun 2016 00:38:17 -0700 Subject: [PATCH] Overhauled and simplified API. Now apps no longer hang on exit! --- Cargo.toml | 2 +- src/console.rs | 8 ++--- src/services/apt.rs | 80 +++++++++++++++++---------------------------- src/services/hid.rs | 19 ++--------- src/services/mod.rs | 2 +- 5 files changed, 39 insertions(+), 72 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3418dff..54b043e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ description = "A safe wrapper around smealum's ctrulib." license = "https://en.wikipedia.org/wiki/Zlib_License" links = "ctru" name = "ctru-rs" -version = "0.2.1" +version = "0.3.0" [dependencies.ctru-sys] path = "ctru-sys" diff --git a/src/console.rs b/src/console.rs index 6531c6d..a717aa5 100644 --- a/src/console.rs +++ b/src/console.rs @@ -10,11 +10,11 @@ extern "C" { } pub struct Console { - pd: PhantomData, + pd: PhantomData<()>, } impl Console { - pub fn write<'a>(&mut self, s: &'a str) { + pub fn print<'a>(&mut self, s: &'a str) { unsafe { for ch in s.as_bytes().iter() { putchar(*ch); @@ -22,9 +22,9 @@ impl Console { } } - pub fn writeln<'a>(&mut self, s: &'a str) { + pub fn println<'a>(&mut self, s: &'a str) { unsafe { - self.write(s); + self.print(s); putchar('\n' as u8); } } diff --git a/src/services/apt.rs b/src/services/apt.rs index 3c9a9fc..b6efc86 100644 --- a/src/services/apt.rs +++ b/src/services/apt.rs @@ -17,16 +17,17 @@ pub enum AppStatus { impl From for apt::APT_AppStatus { fn from(a: AppStatus) -> apt::APT_AppStatus { use self::AppStatus::*; + use libctru::services::apt::APT_AppStatus::*; match a { - NotInitialized => apt::APT_AppStatus::APP_NOTINITIALIZED, - Running => apt::APT_AppStatus::APP_RUNNING, - Suspended => apt::APT_AppStatus::APP_SUSPENDED, - Exiting => apt::APT_AppStatus::APP_EXITING, - Suspending => apt::APT_AppStatus::APP_SUSPENDING, - SleepMode => apt::APT_AppStatus::APP_SLEEPMODE, - PrepareSleepMode => apt::APT_AppStatus::APP_PREPARE_SLEEPMODE, - AppletStarted => apt::APT_AppStatus::APP_APPLETSTARTED, - AppletClosed => apt::APT_AppStatus::APP_APPLETCLOSED, + NotInitialized => APP_NOTINITIALIZED, + Running => APP_RUNNING, + Suspended => APP_SUSPENDED, + Exiting => APP_EXITING, + Suspending => APP_SUSPENDING, + SleepMode => APP_SLEEPMODE, + PrepareSleepMode => APP_PREPARE_SLEEPMODE, + AppletStarted => APP_APPLETSTARTED, + AppletClosed => APP_APPLETCLOSED, } } } @@ -34,16 +35,17 @@ impl From for apt::APT_AppStatus { impl From for AppStatus { fn from(a: apt::APT_AppStatus) -> AppStatus { use self::AppStatus::*; + use libctru::services::apt::APT_AppStatus::*; match a { - apt::APT_AppStatus::APP_NOTINITIALIZED => NotInitialized, - apt::APT_AppStatus::APP_RUNNING => Running, - apt::APT_AppStatus::APP_SUSPENDED => Suspended, - apt::APT_AppStatus::APP_EXITING => Exiting, - apt::APT_AppStatus::APP_SUSPENDING => Suspending, - apt::APT_AppStatus::APP_SLEEPMODE => SleepMode, - apt::APT_AppStatus::APP_PREPARE_SLEEPMODE => PrepareSleepMode, - apt::APT_AppStatus::APP_APPLETSTARTED => AppletStarted, - apt::APT_AppStatus::APP_APPLETCLOSED => AppletClosed, + APP_NOTINITIALIZED => NotInitialized, + APP_RUNNING => Running, + APP_SUSPENDED => Suspended, + APP_EXITING => Exiting, + APP_SUSPENDING => Suspending, + APP_SLEEPMODE => SleepMode, + APP_PREPARE_SLEEPMODE => PrepareSleepMode, + APP_APPLETSTARTED => AppletStarted, + APP_APPLETCLOSED => AppletClosed, } } } @@ -53,15 +55,8 @@ pub struct Apt { } impl Apt { - pub fn new() -> Result { - unsafe { - let r = apt::aptInit(); - if r < 0 { - Err(r) - } else { - Ok(Apt { pd: PhantomData }) - } - } + pub fn new() -> Apt { + Apt { pd: PhantomData } } pub fn get_status(&self) -> AppStatus { @@ -80,31 +75,16 @@ impl Apt { /// The program will not return from this function until the system returns /// to the application, or when the status changes to `AppStatus::Exiting`. pub fn return_to_menu(&mut self) { - unsafe { apt::aptReturnToMenu() }; + unsafe { apt::aptReturnToMenu() } } - pub fn main_loop(&mut self, app: &mut Application) { - unsafe { - while apt::aptMainLoop() != 0 { - app.main_loop(self); - if app.ready_to_quit() { - self.set_status(AppStatus::Exiting) - } + pub fn main_loop(&mut self) -> bool { + unsafe { + match apt::aptMainLoop() { + 1 => true, + 0 => false, + _ => unreachable!(), } - }; - } -} - -impl Drop for Apt { - fn drop(&mut self) { - unsafe { apt::aptExit() }; + } } } - -pub trait Application { - /// Program app loop body. - fn main_loop(&mut self, apt: &mut Apt); - - /// True if the application is ready to quit. - fn ready_to_quit(&self) -> bool; -} diff --git a/src/services/hid.rs b/src/services/hid.rs index 0cca5cf..14dda12 100644 --- a/src/services/hid.rs +++ b/src/services/hid.rs @@ -74,19 +74,12 @@ impl From for u32 { } pub struct Hid { - pd: PhantomData, + pd: PhantomData<()>, } impl Hid { - pub fn new() -> Result { - unsafe { - let r = hid::hidInit(); - if r < 0 { - Err(r) - } else { - Ok(Hid { pd: PhantomData }) - } - } + pub fn new() -> Hid { + Hid { pd: PhantomData } } pub fn scan_input(&mut self) { @@ -126,9 +119,3 @@ impl Hid { } } } - -impl Drop for Hid { - fn drop(&mut self) { - unsafe { hid::hidExit() }; - } -} diff --git a/src/services/mod.rs b/src/services/mod.rs index 6df9e8c..734cac0 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -3,4 +3,4 @@ pub mod hid; pub mod gspgpu; pub use self::hid::Hid; -pub use self::apt::{Apt, Application}; +pub use self::apt::Apt;