Browse Source

Overhaul Apt service.

Applications should implement the Application trait and use that
to interface with apt. An example is provided in rust3ds-template.
pull/10/head
Ronald Kinard 9 years ago
parent
commit
5154868f04
  1. 89
      src/services/apt.rs
  2. 1
      src/services/mod.rs

89
src/services/apt.rs

@ -1,4 +1,4 @@
use ::Result; use core::marker::PhantomData;
use ::raw::services::apt; use ::raw::services::apt;
@ -14,10 +14,10 @@ pub enum AppStatus {
AppletClosed AppletClosed
} }
impl From<AppStatus> for apt::APP_STATUS {
fn to_raw_appstatus(status: AppStatus) -> apt::APP_STATUS { fn from(a: AppStatus) -> apt::APP_STATUS {
use self::AppStatus::*; use self::AppStatus::*;
match status { match a {
NotInitialized => apt::APP_STATUS::APP_NOTINITIALIZED, NotInitialized => apt::APP_STATUS::APP_NOTINITIALIZED,
Running => apt::APP_STATUS::APP_RUNNING, Running => apt::APP_STATUS::APP_RUNNING,
Suspended => apt::APP_STATUS::APP_SUSPENDED, Suspended => apt::APP_STATUS::APP_SUSPENDED,
@ -29,10 +29,12 @@ fn to_raw_appstatus(status: AppStatus) -> apt::APP_STATUS {
AppletClosed => apt::APP_STATUS::APP_APPLETCLOSED, AppletClosed => apt::APP_STATUS::APP_APPLETCLOSED,
} }
} }
}
fn from_raw_appstatus(status: apt::APP_STATUS) -> AppStatus { impl From<apt::APP_STATUS> for AppStatus {
fn from(a: apt::APP_STATUS) -> AppStatus {
use self::AppStatus::*; use self::AppStatus::*;
match status { match a {
apt::APP_STATUS::APP_NOTINITIALIZED => NotInitialized, apt::APP_STATUS::APP_NOTINITIALIZED => NotInitialized,
apt::APP_STATUS::APP_RUNNING => Running, apt::APP_STATUS::APP_RUNNING => Running,
apt::APP_STATUS::APP_SUSPENDED => Suspended, apt::APP_STATUS::APP_SUSPENDED => Suspended,
@ -44,29 +46,30 @@ fn from_raw_appstatus(status: apt::APP_STATUS) -> AppStatus {
apt::APP_STATUS::APP_APPLETCLOSED => AppletClosed apt::APP_STATUS::APP_APPLETCLOSED => AppletClosed
} }
} }
pub fn init() -> Result {
unsafe {
return apt::aptInit();
}
} }
pub fn exit() -> () { pub struct Apt {
unsafe { pd: PhantomData<()>
apt::aptExit();
}
} }
pub fn get_status() -> AppStatus { impl Apt {
pub fn new() -> Result<Apt, i32> {
unsafe { unsafe {
return from_raw_appstatus(apt::aptGetStatus()); let r = apt::aptInit();
if r < 0 {
Err(r)
} else {
Ok(Apt { pd: PhantomData })
}
} }
} }
pub fn set_status(status: AppStatus) -> () { pub fn get_status(&self) -> AppStatus {
unsafe { unsafe { apt::aptGetStatus().into() }
apt::aptSetStatus(to_raw_appstatus(status));
} }
pub fn set_status(&mut self, status: AppStatus) {
unsafe { apt::aptSetStatus(status.into()) };
} }
/// Return to the home menu. /// Return to the home menu.
@ -76,34 +79,32 @@ pub fn set_status(status: AppStatus) -> () {
/// ///
/// 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) {
/// # Examples unsafe { apt::aptReturnToMenu() };
///
/// ```
/// if get_status() == Suspending {
/// return_to_menu();
/// }
/// ```
pub fn return_to_menu() -> () {
unsafe {
apt::aptReturnToMenu();
}
} }
/// Execute a function repeatedly until the apt main loop is over. pub fn main_loop(&mut self, app: &mut Application) {
///
/// # Examples
///
/// ```rust
/// main_loop(|| {
/// // do things here
/// false
/// });
/// ```
pub fn main_loop<F>(mut f: F) -> () where F : FnMut() -> bool {
unsafe { unsafe {
while apt::aptMainLoop() != 0 { while apt::aptMainLoop() != 0 {
if !f() { break; } app.main_loop(self);
if app.ready_to_quit() {
self.set_status(AppStatus::Exiting)
}
}
};
} }
} }
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;
} }

1
src/services/mod.rs

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

Loading…
Cancel
Save