Browse Source

Overhauled and simplified API. Now apps no longer hang on exit!

pull/10/head
Fenrir 9 years ago
parent
commit
5e5a88fa46
  1. 2
      Cargo.toml
  2. 8
      src/console.rs
  3. 78
      src/services/apt.rs
  4. 19
      src/services/hid.rs
  5. 2
      src/services/mod.rs

2
Cargo.toml

@ -5,7 +5,7 @@ description = "A safe wrapper around smealum's ctrulib."
license = "https://en.wikipedia.org/wiki/Zlib_License" license = "https://en.wikipedia.org/wiki/Zlib_License"
links = "ctru" links = "ctru"
name = "ctru-rs" name = "ctru-rs"
version = "0.2.1" version = "0.3.0"
[dependencies.ctru-sys] [dependencies.ctru-sys]
path = "ctru-sys" path = "ctru-sys"

8
src/console.rs

@ -10,11 +10,11 @@ extern "C" {
} }
pub struct Console { pub struct Console {
pd: PhantomData<i32>, pd: PhantomData<()>,
} }
impl Console { impl Console {
pub fn write<'a>(&mut self, s: &'a str) { pub fn print<'a>(&mut self, s: &'a str) {
unsafe { unsafe {
for ch in s.as_bytes().iter() { for ch in s.as_bytes().iter() {
putchar(*ch); 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 { unsafe {
self.write(s); self.print(s);
putchar('\n' as u8); putchar('\n' as u8);
} }
} }

78
src/services/apt.rs

@ -17,16 +17,17 @@ pub enum AppStatus {
impl From<AppStatus> for apt::APT_AppStatus { impl From<AppStatus> for apt::APT_AppStatus {
fn from(a: AppStatus) -> apt::APT_AppStatus { fn from(a: AppStatus) -> apt::APT_AppStatus {
use self::AppStatus::*; use self::AppStatus::*;
use libctru::services::apt::APT_AppStatus::*;
match a { match a {
NotInitialized => apt::APT_AppStatus::APP_NOTINITIALIZED, NotInitialized => APP_NOTINITIALIZED,
Running => apt::APT_AppStatus::APP_RUNNING, Running => APP_RUNNING,
Suspended => apt::APT_AppStatus::APP_SUSPENDED, Suspended => APP_SUSPENDED,
Exiting => apt::APT_AppStatus::APP_EXITING, Exiting => APP_EXITING,
Suspending => apt::APT_AppStatus::APP_SUSPENDING, Suspending => APP_SUSPENDING,
SleepMode => apt::APT_AppStatus::APP_SLEEPMODE, SleepMode => APP_SLEEPMODE,
PrepareSleepMode => apt::APT_AppStatus::APP_PREPARE_SLEEPMODE, PrepareSleepMode => APP_PREPARE_SLEEPMODE,
AppletStarted => apt::APT_AppStatus::APP_APPLETSTARTED, AppletStarted => APP_APPLETSTARTED,
AppletClosed => apt::APT_AppStatus::APP_APPLETCLOSED, AppletClosed => APP_APPLETCLOSED,
} }
} }
} }
@ -34,16 +35,17 @@ impl From<AppStatus> for apt::APT_AppStatus {
impl From<apt::APT_AppStatus> for AppStatus { impl From<apt::APT_AppStatus> for AppStatus {
fn from(a: apt::APT_AppStatus) -> AppStatus { fn from(a: apt::APT_AppStatus) -> AppStatus {
use self::AppStatus::*; use self::AppStatus::*;
use libctru::services::apt::APT_AppStatus::*;
match a { match a {
apt::APT_AppStatus::APP_NOTINITIALIZED => NotInitialized, APP_NOTINITIALIZED => NotInitialized,
apt::APT_AppStatus::APP_RUNNING => Running, APP_RUNNING => Running,
apt::APT_AppStatus::APP_SUSPENDED => Suspended, APP_SUSPENDED => Suspended,
apt::APT_AppStatus::APP_EXITING => Exiting, APP_EXITING => Exiting,
apt::APT_AppStatus::APP_SUSPENDING => Suspending, APP_SUSPENDING => Suspending,
apt::APT_AppStatus::APP_SLEEPMODE => SleepMode, APP_SLEEPMODE => SleepMode,
apt::APT_AppStatus::APP_PREPARE_SLEEPMODE => PrepareSleepMode, APP_PREPARE_SLEEPMODE => PrepareSleepMode,
apt::APT_AppStatus::APP_APPLETSTARTED => AppletStarted, APP_APPLETSTARTED => AppletStarted,
apt::APT_AppStatus::APP_APPLETCLOSED => AppletClosed, APP_APPLETCLOSED => AppletClosed,
} }
} }
} }
@ -53,15 +55,8 @@ pub struct Apt {
} }
impl Apt { impl Apt {
pub fn new() -> Result<Apt, i32> { pub fn new() -> Apt {
unsafe { Apt { pd: PhantomData }
let r = apt::aptInit();
if r < 0 {
Err(r)
} else {
Ok(Apt { pd: PhantomData })
}
}
} }
pub fn get_status(&self) -> AppStatus { pub fn get_status(&self) -> AppStatus {
@ -80,31 +75,16 @@ impl Apt {
/// The program will not return from this function until the system returns /// The program will not return from this function until the system returns
/// to the application, or when the status changes to `AppStatus::Exiting`. /// to the application, or when the status changes to `AppStatus::Exiting`.
pub fn return_to_menu(&mut self) { pub fn return_to_menu(&mut self) {
unsafe { apt::aptReturnToMenu() }; unsafe { apt::aptReturnToMenu() }
} }
pub fn main_loop(&mut self, app: &mut Application) { pub fn main_loop(&mut self) -> bool {
unsafe { unsafe {
while apt::aptMainLoop() != 0 { match apt::aptMainLoop() {
app.main_loop(self); 1 => true,
if app.ready_to_quit() { 0 => false,
self.set_status(AppStatus::Exiting) _ => 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;
}

19
src/services/hid.rs

@ -74,19 +74,12 @@ impl From<PadKey> for u32 {
} }
pub struct Hid { pub struct Hid {
pd: PhantomData<i32>, pd: PhantomData<()>,
} }
impl Hid { impl Hid {
pub fn new() -> Result<Hid, i32> { pub fn new() -> Hid {
unsafe { Hid { pd: PhantomData }
let r = hid::hidInit();
if r < 0 {
Err(r)
} else {
Ok(Hid { pd: PhantomData })
}
}
} }
pub fn scan_input(&mut self) { pub fn scan_input(&mut self) {
@ -126,9 +119,3 @@ impl Hid {
} }
} }
} }
impl Drop for Hid {
fn drop(&mut self) {
unsafe { hid::hidExit() };
}
}

2
src/services/mod.rs

@ -3,4 +3,4 @@ pub mod hid;
pub mod gspgpu; pub mod gspgpu;
pub use self::hid::Hid; pub use self::hid::Hid;
pub use self::apt::{Apt, Application}; pub use self::apt::Apt;

Loading…
Cancel
Save