From d94fe0cb029195086a59f07a07a1b1ffe216b498 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Wed, 18 May 2016 01:02:41 -0700 Subject: [PATCH] Added stdlib-like export module. Also added extremely basic console support --- Cargo.toml | 21 +++++++----- src/console.rs | 17 ++++++++++ src/gfx.rs | 54 ++++++++++++++++++------------ src/lib.rs | 51 ++++++++++++++++++++++++++-- src/sdmc.rs | 6 ++-- src/services/apt.rs | 22 ++++++------ src/services/{gsp.rs => gspgpu.rs} | 18 +++++----- src/services/hid.rs | 6 ++-- src/services/mod.rs | 2 +- src/srv.rs | 2 +- 10 files changed, 137 insertions(+), 62 deletions(-) create mode 100644 src/console.rs rename src/services/{gsp.rs => gspgpu.rs} (85%) diff --git a/Cargo.toml b/Cargo.toml index 0316b5c..d691e86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,15 +1,18 @@ [package] -name = "ctru-rs" -version = "0.2.0" -description = "A safe wrapper around smealum's ctrulib." -authors = ["Ronald Kinard "] -links = "ctru" +authors = ["Ronald Kinard "] build = "build.rs" +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" + +[dependencies] +rcstring = "0.2.1" + +[dependencies.ctru-sys] +path = "ctru-sys" [lib] -name = "ctru" crate-type = ["rlib"] - -[dependencies] -ctru-sys = { path = "ctru-sys" } +name = "ctru" diff --git a/src/console.rs b/src/console.rs new file mode 100644 index 0000000..98090d5 --- /dev/null +++ b/src/console.rs @@ -0,0 +1,17 @@ +use libctru::console::{PrintConsole, consoleInit}; +use libctru::gfx; +use rcstring::CString; + +use core::ptr; + +extern "C" { + fn puts(cstr: *const u8) -> u8; +} + +pub fn console_default_init() -> *mut PrintConsole { + unsafe { consoleInit(gfx::gfxScreen_t::GFX_TOP, ptr::null_mut()) } +} + +pub fn console_write<'a>(s: &'a str) -> u8 { + unsafe { puts(CString::new(s).unwrap().into_raw()) } +} diff --git a/src/gfx.rs b/src/gfx.rs index 7417a7a..7504632 100644 --- a/src/gfx.rs +++ b/src/gfx.rs @@ -4,65 +4,69 @@ use core::default::Default; use core::marker::PhantomData; use core::ops::Drop; -use ::services::gsp::FramebufferFormat; +use services::gspgpu::FramebufferFormat; pub struct Gfx { // we do this to prevent people from making a Gfx struct manually - pd: PhantomData + pd: PhantomData, } #[derive(Copy, Clone)] pub enum Screen { Top, - Bottom + Bottom, } #[derive(Copy, Clone)] pub enum Side { Left, - Right + Right, } impl From for Screen { - #[inline] fn from(g: gfx::gfxScreen_t) -> Screen { + #[inline] + fn from(g: gfx::gfxScreen_t) -> Screen { use libctru::gfx::gfxScreen_t::*; use self::Screen::*; match g { GFX_TOP => Top, - GFX_BOTTOM => Bottom + GFX_BOTTOM => Bottom, } } } impl From for gfx::gfxScreen_t { - #[inline] fn from(g: Screen) -> gfx::gfxScreen_t { + #[inline] + fn from(g: Screen) -> gfx::gfxScreen_t { use libctru::gfx::gfxScreen_t::*; use self::Screen::*; match g { Top => GFX_TOP, - Bottom => GFX_BOTTOM + Bottom => GFX_BOTTOM, } } } impl From for Side { - #[inline] fn from(s: gfx::gfx3dSide_t) -> Side { + #[inline] + fn from(s: gfx::gfx3dSide_t) -> Side { use libctru::gfx::gfx3dSide_t::*; use self::Side::*; match s { GFX_LEFT => Left, - GFX_RIGHT => Right + GFX_RIGHT => Right, } } } impl From for gfx::gfx3dSide_t { - #[inline] fn from(s: Side) -> gfx::gfx3dSide_t { + #[inline] + fn from(s: Side) -> gfx::gfx3dSide_t { use libctru::gfx::gfx3dSide_t::*; use self::Side::*; match s { Left => GFX_LEFT, - Right => GFX_RIGHT + Right => GFX_RIGHT, } } } @@ -70,18 +74,24 @@ impl From for gfx::gfx3dSide_t { impl Gfx { pub fn set_3d_enabled(&mut self, enabled: bool) { unsafe { - gfx::gfxSet3D(match enabled { true => 1u8, false => 0u8 }); + gfx::gfxSet3D(match enabled { + true => 1u8, + false => 0u8, + }); } } - pub fn get_framebuffer(& mut self, screen: Screen, side: Side) -> (&'static mut [u8], u16, u16) { + pub fn get_framebuffer(&mut self, screen: Screen, side: Side) -> (&'static mut [u8], u16, u16) { use core::convert::Into; unsafe { use core::slice::from_raw_parts_mut; let mut w: u16 = 0; let mut h: u16 = 0; - let buf: *mut u8 = gfx::gfxGetFramebuffer(screen.into(), side.into(), &mut w as *mut u16, &mut h as &mut u16); + let buf: *mut u8 = gfx::gfxGetFramebuffer(screen.into(), + side.into(), + &mut w as *mut u16, + &mut h as &mut u16); let fbfmt = self.get_framebuffer_format(screen); @@ -103,21 +113,21 @@ impl Gfx { pub fn get_framebuffer_format(&self, screen: Screen) -> FramebufferFormat { use core::convert::Into; - unsafe { - gfx::gfxGetScreenFormat(screen.into()).into() - } + unsafe { gfx::gfxGetScreenFormat(screen.into()).into() } } pub fn set_framebuffer_format(&mut self, screen: Screen, fmt: FramebufferFormat) { use core::convert::Into; - unsafe { - gfx::gfxSetScreenFormat(screen.into(), fmt.into()) - } + unsafe { gfx::gfxSetScreenFormat(screen.into(), fmt.into()) } } pub fn set_double_buffering(&mut self, screen: Screen, enabled: bool) { unsafe { - gfx::gfxSetDoubleBuffering(screen.into(), match enabled { true => 1u8, false => 0u8 }) + gfx::gfxSetDoubleBuffering(screen.into(), + match enabled { + true => 1u8, + false => 0u8, + }) }; } } diff --git a/src/lib.rs b/src/lib.rs index 9d9cbd5..70de26c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,17 @@ #![no_std] -#![feature(lang_items)] +#![feature(lang_items, alloc, collections, slice_concat_ext, macro_reexport, allow_internal_unstable)] #![crate_type = "rlib"] #![crate_name = "ctru"] +extern crate alloc; +#[macro_reexport(format, vec)] +extern crate collections; + extern crate ctru_sys as libctru; +#[macro_use] +extern crate rcstring; +pub mod console; pub mod srv; pub mod gfx; pub mod sdmc; @@ -15,5 +22,43 @@ pub use srv::Srv; pub use gfx::Gfx; pub use sdmc::Sdmc; -#[lang = "eh_personality"] extern fn eh_personality() {} -#[lang = "panic_fmt"] fn panic_fmt() -> ! { loop {} } +pub mod std { + pub use core::{any, cell, char, clone, cmp, convert, default, f32, f64, hash, i16, i32, i64, + i8, isize, iter, marker, mem, num, ops, option, ptr, result, sync, u16, u32, + u64, u8, usize}; + pub use alloc::{arc, rc}; + pub use collections::{borrow, boxed, fmt, slice, str, string, vec}; + + pub mod collections { + pub use collections::{binary_heap, btree_map, btree_set, linked_list, vec_deque, + BinaryHeap, LinkedList, VecDeque, String, Vec, BTreeMap, BTreeSet}; + } +} + +pub mod prelude { + pub use std; + pub use std::marker::{Copy, Send, Sized, Sync}; + pub use std::ops::{Drop, Fn, FnMut, FnOnce}; + pub use std::mem::drop; + pub use std::boxed::Box; + pub use std::borrow::ToOwned; + pub use std::clone::Clone; + pub use std::cmp::{PartialEq, PartialOrd, Eq, Ord}; + pub use std::convert::{AsRef, AsMut, Into, From}; + pub use std::default::Default; + pub use std::iter::{Iterator, Extend, IntoIterator}; + pub use std::iter::{DoubleEndedIterator, ExactSizeIterator}; + pub use std::option::Option::{self, Some, None}; + pub use std::result::Result::{self, Ok, Err}; + pub use std::slice::SliceConcatExt; + pub use std::string::{String, ToString}; + pub use std::vec::Vec; + pub use std::fmt::Write; +} + +#[lang = "eh_personality"] +extern "C" fn eh_personality() {} +#[lang = "panic_fmt"] +fn panic_fmt() -> ! { + loop {} +} diff --git a/src/sdmc.rs b/src/sdmc.rs index 4579789..23a70ea 100644 --- a/src/sdmc.rs +++ b/src/sdmc.rs @@ -3,7 +3,7 @@ use core::marker::PhantomData; use libctru::sdmc::*; pub struct Sdmc { - pd: PhantomData + pd: PhantomData, } impl Sdmc { @@ -21,8 +21,6 @@ impl Sdmc { impl Drop for Sdmc { fn drop(&mut self) { - unsafe { - sdmcExit() - }; + unsafe { sdmcExit() }; } } diff --git a/src/services/apt.rs b/src/services/apt.rs index 5d3f04f..3c9a9fc 100644 --- a/src/services/apt.rs +++ b/src/services/apt.rs @@ -11,7 +11,7 @@ pub enum AppStatus { SleepMode, PrepareSleepMode, AppletStarted, - AppletClosed + AppletClosed, } impl From for apt::APT_AppStatus { @@ -35,21 +35,21 @@ impl From for AppStatus { fn from(a: apt::APT_AppStatus) -> AppStatus { use self::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 + 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, } } } pub struct Apt { - pd: PhantomData<()> + pd: PhantomData<()>, } impl Apt { diff --git a/src/services/gsp.rs b/src/services/gspgpu.rs similarity index 85% rename from src/services/gsp.rs rename to src/services/gspgpu.rs index 722bab6..efa153d 100644 --- a/src/services/gsp.rs +++ b/src/services/gspgpu.rs @@ -9,7 +9,7 @@ pub enum Event { VBlank1, PPF, P3D, - DMA + DMA, } #[derive(Copy, Clone)] @@ -18,7 +18,7 @@ pub enum FramebufferFormat { Bgr8, Rgb565, Rgb5A1, - Rgba4 + Rgba4, } impl FramebufferFormat { @@ -29,13 +29,14 @@ impl FramebufferFormat { Bgr8 => 3usize, Rgb565 => 2usize, Rgb5A1 => 2usize, - Rgba4 => 2usize + Rgba4 => 2usize, } } } impl From for FramebufferFormat { - #[inline] fn from(g: gspgpu::GSPGPU_FramebufferFormats) -> FramebufferFormat { + #[inline] + fn from(g: gspgpu::GSPGPU_FramebufferFormats) -> FramebufferFormat { use libctru::services::gspgpu::GSPGPU_FramebufferFormats::*; use self::FramebufferFormat::*; match g { @@ -43,13 +44,14 @@ impl From for FramebufferFormat { GSP_BGR8_OES => Bgr8, GSP_RGB565_OES => Rgb565, GSP_RGB5_A1_OES => Rgb5A1, - GSP_RGBA4_OES => Rgba4 + GSP_RGBA4_OES => Rgba4, } } } impl From for gspgpu::GSPGPU_FramebufferFormats { - #[inline] fn from(g: FramebufferFormat) -> gspgpu::GSPGPU_FramebufferFormats { + #[inline] + fn from(g: FramebufferFormat) -> gspgpu::GSPGPU_FramebufferFormats { use libctru::services::gspgpu::GSPGPU_FramebufferFormats::*; use self::FramebufferFormat::*; match g { @@ -57,7 +59,7 @@ impl From for gspgpu::GSPGPU_FramebufferFormats { Bgr8 => GSP_BGR8_OES, Rgb565 => GSP_RGB565_OES, Rgb5A1 => GSP_RGB5_A1_OES, - Rgba4 => GSP_RGBA4_OES + Rgba4 => GSP_RGBA4_OES, } } } @@ -73,7 +75,7 @@ fn to_raw_event(ev: Event) -> gspgpu::GSPGPU_Event { VBlank1 => GSPGPU_EVENT_VBlank1, PPF => GSPGPU_EVENT_PPF, P3D => GSPGPU_EVENT_P3D, - DMA => GSPGPU_EVENT_DMA + DMA => GSPGPU_EVENT_DMA, } } diff --git a/src/services/hid.rs b/src/services/hid.rs index cb96c3a..0cca5cf 100644 --- a/src/services/hid.rs +++ b/src/services/hid.rs @@ -32,7 +32,7 @@ pub enum PadKey { Up, Down, Left, - Right + Right, } impl From for u32 { @@ -68,13 +68,13 @@ impl From for u32 { DPadLeft => KEY_DLEFT as u32, DPadRight => KEY_DRIGHT as u32, DPadUp => KEY_DUP as u32, - DPadDown => KEY_DDOWN as u32 + DPadDown => KEY_DDOWN as u32, } } } pub struct Hid { - pd: PhantomData + pd: PhantomData, } impl Hid { diff --git a/src/services/mod.rs b/src/services/mod.rs index 0757352..6df9e8c 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -1,6 +1,6 @@ pub mod apt; pub mod hid; -pub mod gsp; +pub mod gspgpu; pub use self::hid::Hid; pub use self::apt::{Apt, Application}; diff --git a/src/srv.rs b/src/srv.rs index 5cafed3..bb1bb59 100644 --- a/src/srv.rs +++ b/src/srv.rs @@ -3,7 +3,7 @@ use libctru::srv::*; use core::marker::PhantomData; pub struct Srv { - pd: PhantomData + pd: PhantomData, } impl Srv {