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." @@ -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"

8
src/console.rs

@ -10,11 +10,11 @@ extern "C" { @@ -10,11 +10,11 @@ extern "C" {
}
pub struct Console {
pd: PhantomData<i32>,
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 { @@ -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);
}
}

78
src/services/apt.rs

@ -17,16 +17,17 @@ pub enum AppStatus { @@ -17,16 +17,17 @@ pub enum AppStatus {
impl From<AppStatus> 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<AppStatus> for apt::APT_AppStatus { @@ -34,16 +35,17 @@ impl From<AppStatus> for apt::APT_AppStatus {
impl From<apt::APT_AppStatus> 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 { @@ -53,15 +55,8 @@ pub struct Apt {
}
impl Apt {
pub fn new() -> Result<Apt, i32> {
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 { @@ -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) {
pub fn main_loop(&mut self) -> bool {
unsafe {
while apt::aptMainLoop() != 0 {
app.main_loop(self);
if app.ready_to_quit() {
self.set_status(AppStatus::Exiting)
}
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;
}

19
src/services/hid.rs

@ -74,19 +74,12 @@ impl From<PadKey> for u32 { @@ -74,19 +74,12 @@ impl From<PadKey> for u32 {
}
pub struct Hid {
pd: PhantomData<i32>,
pd: PhantomData<()>,
}
impl Hid {
pub fn new() -> Result<Hid, i32> {
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 { @@ -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; @@ -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;

Loading…
Cancel
Save