From 6f84a43cabb1df04162b76ad824eea11646a1e55 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Sat, 4 Jun 2016 00:47:09 -0700 Subject: [PATCH 1/4] Re-introduced RAII semeantics for {Apt, Hid, Sdmc} --- src/sdmc.rs | 2 +- src/services/apt.rs | 20 +++++++++++++++++--- src/services/hid.rs | 19 ++++++++++++++++--- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/sdmc.rs b/src/sdmc.rs index 23a70ea..5e98552 100644 --- a/src/sdmc.rs +++ b/src/sdmc.rs @@ -7,7 +7,7 @@ pub struct Sdmc { } impl Sdmc { - pub fn new() -> Result { + pub fn new() -> Result { unsafe { let r = sdmcInit(); if r < 0 { diff --git a/src/services/apt.rs b/src/services/apt.rs index 8c891a6..612fdf7 100644 --- a/src/services/apt.rs +++ b/src/services/apt.rs @@ -51,14 +51,22 @@ impl From for AppStatus { } pub struct Apt { - pd: PhantomData<()>, + pd: PhantomData } impl Apt { - pub fn new() -> Apt { - Apt { pd: PhantomData } + pub fn new() -> Result { + unsafe { + let r = apt::aptInit(); + if r < 0 { + Err(r) + } else { + Ok(Apt { pd: PhantomData }) + } + } } + pub fn get_status(&self) -> AppStatus { unsafe { apt::aptGetStatus().into() } } @@ -88,3 +96,9 @@ impl Apt { } } } + +impl Drop for Apt { + fn drop(&mut self) { + unsafe { apt::aptExit() }; + } +} diff --git a/src/services/hid.rs b/src/services/hid.rs index 14dda12..a9e7954 100644 --- a/src/services/hid.rs +++ b/src/services/hid.rs @@ -74,12 +74,19 @@ impl From for u32 { } pub struct Hid { - pd: PhantomData<()>, + pd: PhantomData } impl Hid { - pub fn new() -> Hid { - Hid { pd: PhantomData } + pub fn new() -> Result { + unsafe { + let r = hid::hidInit(); + if r < 0 { + Err(r) + } else { + Ok(Hid { pd: PhantomData }) + } + } } pub fn scan_input(&mut self) { @@ -119,3 +126,9 @@ impl Hid { } } } + +impl Drop for Hid { + fn drop(&mut self) { + unsafe { hid::hidExit() }; + } +} From 9f8dc9fa5a5302eeb6e1c11ea55dcecc4e981516 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Tue, 7 Jun 2016 16:28:13 -0700 Subject: [PATCH 2/4] Remove extern crate alloc and core declarations --- src/lib.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2bec58d..43d72a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,8 @@ -#![feature(lang_items, alloc, collections, macro_reexport, allow_internal_unstable)] +#![feature(lang_items)] #![no_std] #![crate_type = "rlib"] #![crate_name = "ctru"] -extern crate alloc; -#[macro_reexport(format, vec)] -extern crate collections; - extern crate ctru_sys as libctru; pub mod console; From 51d59739a3558d83926e824bc4ddec2c8779d255 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Tue, 7 Jun 2016 16:51:40 -0700 Subject: [PATCH 3/4] Multiple console support --- src/console.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/console.rs b/src/console.rs index 0d0535a..123c067 100644 --- a/src/console.rs +++ b/src/console.rs @@ -1,17 +1,26 @@ -use libctru::console::{consoleInit, consoleClear}; -use libctru::gfx; +use libctru::console::*; use libctru::libc; +use gfx::Screen; + use core::fmt::{self, Write}; use core::default::Default; -use core::marker::PhantomData; use core::ptr; pub struct Console { - pd: PhantomData<()>, + context: PrintConsole, } 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) { unsafe { consoleClear() } } @@ -19,15 +28,14 @@ impl Console { impl Default for Console { fn default() -> Self { - unsafe { - consoleInit(gfx::gfxScreen_t::GFX_TOP, ptr::null_mut()); - } - Console { pd: PhantomData } + let ret = unsafe { *(consoleInit(Screen::Top.into(), ptr::null_mut())) }; + Console { context: ret } } } impl Write for Console { 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()) }; if ret == s.len() as isize { Ok(()) From 235eb9cc450eed53c0584eb848ae51222cb7d636 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Tue, 7 Jun 2016 16:54:28 -0700 Subject: [PATCH 4/4] Bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 54b043e..0aa1ecf 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.3.0" +version = "0.4.0" [dependencies.ctru-sys] path = "ctru-sys"