Browse Source

Merge pull request #6 from FenrirWolf/master

Multiple console support + Bringing RAII back
pull/10/head
Ronald Kinard 9 years ago
parent
commit
c2c030f726
  1. 2
      Cargo.toml
  2. 24
      src/console.rs
  3. 6
      src/lib.rs
  4. 2
      src/sdmc.rs
  5. 20
      src/services/apt.rs
  6. 19
      src/services/hid.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.3.0" version = "0.4.0"
[dependencies.ctru-sys] [dependencies.ctru-sys]
path = "ctru-sys" path = "ctru-sys"

24
src/console.rs

@ -1,17 +1,26 @@
use libctru::console::{consoleInit, consoleClear}; use libctru::console::*;
use libctru::gfx;
use libctru::libc; use libctru::libc;
use gfx::Screen;
use core::fmt::{self, Write}; use core::fmt::{self, Write};
use core::default::Default; use core::default::Default;
use core::marker::PhantomData;
use core::ptr; use core::ptr;
pub struct Console { pub struct Console {
pd: PhantomData<()>, context: PrintConsole,
} }
impl Console { impl Console {
pub fn init(screen: Screen) -> Self {
let ret = unsafe { *(consoleInit(screen.into(), ptr::null_mut())) };
Console { context: ret }
}
pub fn set_window(&mut self, x: i32, y: i32, width: i32, height: i32) {
unsafe { consoleSetWindow(&mut self.context, x, y, width, height) }
}
pub fn clear(&mut self) { pub fn clear(&mut self) {
unsafe { consoleClear() } unsafe { consoleClear() }
} }
@ -19,15 +28,14 @@ impl Console {
impl Default for Console { impl Default for Console {
fn default() -> Self { fn default() -> Self {
unsafe { let ret = unsafe { *(consoleInit(Screen::Top.into(), ptr::null_mut())) };
consoleInit(gfx::gfxScreen_t::GFX_TOP, ptr::null_mut()); Console { context: ret }
}
Console { pd: PhantomData }
} }
} }
impl Write for Console { impl Write for Console {
fn write_str(&mut self, s: &str) -> fmt::Result { fn write_str(&mut self, s: &str) -> fmt::Result {
unsafe { consoleSelect(&mut self.context); }
let ret = unsafe { libc::write(libc::STDOUT_FILENO, s.as_ptr() as *const _, s.len()) }; let ret = unsafe { libc::write(libc::STDOUT_FILENO, s.as_ptr() as *const _, s.len()) };
if ret == s.len() as isize { if ret == s.len() as isize {
Ok(()) Ok(())

6
src/lib.rs

@ -1,12 +1,8 @@
#![feature(lang_items, alloc, collections, macro_reexport, allow_internal_unstable)] #![feature(lang_items)]
#![no_std] #![no_std]
#![crate_type = "rlib"] #![crate_type = "rlib"]
#![crate_name = "ctru"] #![crate_name = "ctru"]
extern crate alloc;
#[macro_reexport(format, vec)]
extern crate collections;
extern crate ctru_sys as libctru; extern crate ctru_sys as libctru;
pub mod console; pub mod console;

2
src/sdmc.rs

@ -7,7 +7,7 @@ pub struct Sdmc {
} }
impl Sdmc { impl Sdmc {
pub fn new() -> Result<Self, i32> { pub fn new() -> Result<Sdmc, i32> {
unsafe { unsafe {
let r = sdmcInit(); let r = sdmcInit();
if r < 0 { if r < 0 {

20
src/services/apt.rs

@ -51,13 +51,21 @@ impl From<apt::APT_AppStatus> for AppStatus {
} }
pub struct Apt { pub struct Apt {
pd: PhantomData<()>, pd: PhantomData<i32>
} }
impl Apt { impl Apt {
pub fn new() -> Apt { pub fn new() -> Result<Apt, i32> {
Apt { pd: PhantomData } unsafe {
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 {
unsafe { apt::aptGetStatus().into() } unsafe { apt::aptGetStatus().into() }
@ -88,3 +96,9 @@ impl Apt {
} }
} }
} }
impl Drop for Apt {
fn drop(&mut self) {
unsafe { apt::aptExit() };
}
}

19
src/services/hid.rs

@ -74,12 +74,19 @@ impl From<PadKey> for u32 {
} }
pub struct Hid { pub struct Hid {
pd: PhantomData<()>, pd: PhantomData<i32>
} }
impl Hid { impl Hid {
pub fn new() -> Hid { pub fn new() -> Result<Hid, i32> {
Hid { pd: PhantomData } unsafe {
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) {
@ -119,3 +126,9 @@ impl Hid {
} }
} }
} }
impl Drop for Hid {
fn drop(&mut self) {
unsafe { hid::hidExit() };
}
}

Loading…
Cancel
Save