Browse Source

Added stdlib-like export module. Also added extremely basic console support

pull/10/head
Fenrir 9 years ago
parent
commit
d94fe0cb02
  1. 21
      Cargo.toml
  2. 17
      src/console.rs
  3. 54
      src/gfx.rs
  4. 51
      src/lib.rs
  5. 6
      src/sdmc.rs
  6. 22
      src/services/apt.rs
  7. 18
      src/services/gspgpu.rs
  8. 6
      src/services/hid.rs
  9. 2
      src/services/mod.rs
  10. 2
      src/srv.rs

21
Cargo.toml

@ -1,15 +1,18 @@
[package] [package]
name = "ctru-rs" authors = ["Ronald Kinard <furyhunter600@gmail.com>"]
version = "0.2.0"
description = "A safe wrapper around smealum's ctrulib."
authors = ["Ronald Kinard <furyhunter600@gmail.com>"]
links = "ctru"
build = "build.rs" build = "build.rs"
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"
name = "ctru-rs"
version = "0.2.1"
[dependencies]
rcstring = "0.2.1"
[dependencies.ctru-sys]
path = "ctru-sys"
[lib] [lib]
name = "ctru"
crate-type = ["rlib"] crate-type = ["rlib"]
name = "ctru"
[dependencies]
ctru-sys = { path = "ctru-sys" }

17
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()) }
}

54
src/gfx.rs

@ -4,65 +4,69 @@ use core::default::Default;
use core::marker::PhantomData; use core::marker::PhantomData;
use core::ops::Drop; use core::ops::Drop;
use ::services::gsp::FramebufferFormat; use services::gspgpu::FramebufferFormat;
pub struct Gfx { pub struct Gfx {
// we do this to prevent people from making a Gfx struct manually // we do this to prevent people from making a Gfx struct manually
pd: PhantomData<i32> pd: PhantomData<i32>,
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub enum Screen { pub enum Screen {
Top, Top,
Bottom Bottom,
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub enum Side { pub enum Side {
Left, Left,
Right Right,
} }
impl From<gfx::gfxScreen_t> for Screen { impl From<gfx::gfxScreen_t> for Screen {
#[inline] fn from(g: gfx::gfxScreen_t) -> Screen { #[inline]
fn from(g: gfx::gfxScreen_t) -> Screen {
use libctru::gfx::gfxScreen_t::*; use libctru::gfx::gfxScreen_t::*;
use self::Screen::*; use self::Screen::*;
match g { match g {
GFX_TOP => Top, GFX_TOP => Top,
GFX_BOTTOM => Bottom GFX_BOTTOM => Bottom,
} }
} }
} }
impl From<Screen> for gfx::gfxScreen_t { impl From<Screen> 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 libctru::gfx::gfxScreen_t::*;
use self::Screen::*; use self::Screen::*;
match g { match g {
Top => GFX_TOP, Top => GFX_TOP,
Bottom => GFX_BOTTOM Bottom => GFX_BOTTOM,
} }
} }
} }
impl From<gfx::gfx3dSide_t> for Side { impl From<gfx::gfx3dSide_t> for Side {
#[inline] fn from(s: gfx::gfx3dSide_t) -> Side { #[inline]
fn from(s: gfx::gfx3dSide_t) -> Side {
use libctru::gfx::gfx3dSide_t::*; use libctru::gfx::gfx3dSide_t::*;
use self::Side::*; use self::Side::*;
match s { match s {
GFX_LEFT => Left, GFX_LEFT => Left,
GFX_RIGHT => Right GFX_RIGHT => Right,
} }
} }
} }
impl From<Side> for gfx::gfx3dSide_t { impl From<Side> 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 libctru::gfx::gfx3dSide_t::*;
use self::Side::*; use self::Side::*;
match s { match s {
Left => GFX_LEFT, Left => GFX_LEFT,
Right => GFX_RIGHT Right => GFX_RIGHT,
} }
} }
} }
@ -70,18 +74,24 @@ impl From<Side> for gfx::gfx3dSide_t {
impl Gfx { impl Gfx {
pub fn set_3d_enabled(&mut self, enabled: bool) { pub fn set_3d_enabled(&mut self, enabled: bool) {
unsafe { 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; use core::convert::Into;
unsafe { unsafe {
use core::slice::from_raw_parts_mut; use core::slice::from_raw_parts_mut;
let mut w: u16 = 0; let mut w: u16 = 0;
let mut h: 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); let fbfmt = self.get_framebuffer_format(screen);
@ -103,21 +113,21 @@ impl Gfx {
pub fn get_framebuffer_format(&self, screen: Screen) -> FramebufferFormat { pub fn get_framebuffer_format(&self, screen: Screen) -> FramebufferFormat {
use core::convert::Into; use core::convert::Into;
unsafe { unsafe { gfx::gfxGetScreenFormat(screen.into()).into() }
gfx::gfxGetScreenFormat(screen.into()).into()
}
} }
pub fn set_framebuffer_format(&mut self, screen: Screen, fmt: FramebufferFormat) { pub fn set_framebuffer_format(&mut self, screen: Screen, fmt: FramebufferFormat) {
use core::convert::Into; use core::convert::Into;
unsafe { unsafe { gfx::gfxSetScreenFormat(screen.into(), fmt.into()) }
gfx::gfxSetScreenFormat(screen.into(), fmt.into())
}
} }
pub fn set_double_buffering(&mut self, screen: Screen, enabled: bool) { pub fn set_double_buffering(&mut self, screen: Screen, enabled: bool) {
unsafe { unsafe {
gfx::gfxSetDoubleBuffering(screen.into(), match enabled { true => 1u8, false => 0u8 }) gfx::gfxSetDoubleBuffering(screen.into(),
match enabled {
true => 1u8,
false => 0u8,
})
}; };
} }
} }

51
src/lib.rs

@ -1,10 +1,17 @@
#![no_std] #![no_std]
#![feature(lang_items)] #![feature(lang_items, alloc, collections, slice_concat_ext, macro_reexport, allow_internal_unstable)]
#![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;
#[macro_use]
extern crate rcstring;
pub mod console;
pub mod srv; pub mod srv;
pub mod gfx; pub mod gfx;
pub mod sdmc; pub mod sdmc;
@ -15,5 +22,43 @@ pub use srv::Srv;
pub use gfx::Gfx; pub use gfx::Gfx;
pub use sdmc::Sdmc; pub use sdmc::Sdmc;
#[lang = "eh_personality"] extern fn eh_personality() {} pub mod std {
#[lang = "panic_fmt"] fn panic_fmt() -> ! { loop {} } 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 {}
}

6
src/sdmc.rs

@ -3,7 +3,7 @@ use core::marker::PhantomData;
use libctru::sdmc::*; use libctru::sdmc::*;
pub struct Sdmc { pub struct Sdmc {
pd: PhantomData<i32> pd: PhantomData<i32>,
} }
impl Sdmc { impl Sdmc {
@ -21,8 +21,6 @@ impl Sdmc {
impl Drop for Sdmc { impl Drop for Sdmc {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe { sdmcExit() };
sdmcExit()
};
} }
} }

22
src/services/apt.rs

@ -11,7 +11,7 @@ pub enum AppStatus {
SleepMode, SleepMode,
PrepareSleepMode, PrepareSleepMode,
AppletStarted, AppletStarted,
AppletClosed AppletClosed,
} }
impl From<AppStatus> for apt::APT_AppStatus { impl From<AppStatus> for apt::APT_AppStatus {
@ -35,21 +35,21 @@ impl From<apt::APT_AppStatus> for AppStatus {
fn from(a: apt::APT_AppStatus) -> AppStatus { fn from(a: apt::APT_AppStatus) -> AppStatus {
use self::AppStatus::*; use self::AppStatus::*;
match a { match a {
apt::APT_AppStatus::APP_NOTINITIALIZED => NotInitialized, apt::APT_AppStatus::APP_NOTINITIALIZED => NotInitialized,
apt::APT_AppStatus::APP_RUNNING => Running, apt::APT_AppStatus::APP_RUNNING => Running,
apt::APT_AppStatus::APP_SUSPENDED => Suspended, apt::APT_AppStatus::APP_SUSPENDED => Suspended,
apt::APT_AppStatus::APP_EXITING => Exiting, apt::APT_AppStatus::APP_EXITING => Exiting,
apt::APT_AppStatus::APP_SUSPENDING => Suspending, apt::APT_AppStatus::APP_SUSPENDING => Suspending,
apt::APT_AppStatus::APP_SLEEPMODE => SleepMode, apt::APT_AppStatus::APP_SLEEPMODE => SleepMode,
apt::APT_AppStatus::APP_PREPARE_SLEEPMODE => PrepareSleepMode, apt::APT_AppStatus::APP_PREPARE_SLEEPMODE => PrepareSleepMode,
apt::APT_AppStatus::APP_APPLETSTARTED => AppletStarted, apt::APT_AppStatus::APP_APPLETSTARTED => AppletStarted,
apt::APT_AppStatus::APP_APPLETCLOSED => AppletClosed apt::APT_AppStatus::APP_APPLETCLOSED => AppletClosed,
} }
} }
} }
pub struct Apt { pub struct Apt {
pd: PhantomData<()> pd: PhantomData<()>,
} }
impl Apt { impl Apt {

18
src/services/gsp.rs → src/services/gspgpu.rs

@ -9,7 +9,7 @@ pub enum Event {
VBlank1, VBlank1,
PPF, PPF,
P3D, P3D,
DMA DMA,
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -18,7 +18,7 @@ pub enum FramebufferFormat {
Bgr8, Bgr8,
Rgb565, Rgb565,
Rgb5A1, Rgb5A1,
Rgba4 Rgba4,
} }
impl FramebufferFormat { impl FramebufferFormat {
@ -29,13 +29,14 @@ impl FramebufferFormat {
Bgr8 => 3usize, Bgr8 => 3usize,
Rgb565 => 2usize, Rgb565 => 2usize,
Rgb5A1 => 2usize, Rgb5A1 => 2usize,
Rgba4 => 2usize Rgba4 => 2usize,
} }
} }
} }
impl From<gspgpu::GSPGPU_FramebufferFormats> for FramebufferFormat { impl From<gspgpu::GSPGPU_FramebufferFormats> 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 libctru::services::gspgpu::GSPGPU_FramebufferFormats::*;
use self::FramebufferFormat::*; use self::FramebufferFormat::*;
match g { match g {
@ -43,13 +44,14 @@ impl From<gspgpu::GSPGPU_FramebufferFormats> for FramebufferFormat {
GSP_BGR8_OES => Bgr8, GSP_BGR8_OES => Bgr8,
GSP_RGB565_OES => Rgb565, GSP_RGB565_OES => Rgb565,
GSP_RGB5_A1_OES => Rgb5A1, GSP_RGB5_A1_OES => Rgb5A1,
GSP_RGBA4_OES => Rgba4 GSP_RGBA4_OES => Rgba4,
} }
} }
} }
impl From<FramebufferFormat> for gspgpu::GSPGPU_FramebufferFormats { impl From<FramebufferFormat> 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 libctru::services::gspgpu::GSPGPU_FramebufferFormats::*;
use self::FramebufferFormat::*; use self::FramebufferFormat::*;
match g { match g {
@ -57,7 +59,7 @@ impl From<FramebufferFormat> for gspgpu::GSPGPU_FramebufferFormats {
Bgr8 => GSP_BGR8_OES, Bgr8 => GSP_BGR8_OES,
Rgb565 => GSP_RGB565_OES, Rgb565 => GSP_RGB565_OES,
Rgb5A1 => GSP_RGB5_A1_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, VBlank1 => GSPGPU_EVENT_VBlank1,
PPF => GSPGPU_EVENT_PPF, PPF => GSPGPU_EVENT_PPF,
P3D => GSPGPU_EVENT_P3D, P3D => GSPGPU_EVENT_P3D,
DMA => GSPGPU_EVENT_DMA DMA => GSPGPU_EVENT_DMA,
} }
} }

6
src/services/hid.rs

@ -32,7 +32,7 @@ pub enum PadKey {
Up, Up,
Down, Down,
Left, Left,
Right Right,
} }
impl From<PadKey> for u32 { impl From<PadKey> for u32 {
@ -68,13 +68,13 @@ impl From<PadKey> for u32 {
DPadLeft => KEY_DLEFT as u32, DPadLeft => KEY_DLEFT as u32,
DPadRight => KEY_DRIGHT as u32, DPadRight => KEY_DRIGHT as u32,
DPadUp => KEY_DUP as u32, DPadUp => KEY_DUP as u32,
DPadDown => KEY_DDOWN as u32 DPadDown => KEY_DDOWN as u32,
} }
} }
} }
pub struct Hid { pub struct Hid {
pd: PhantomData<i32> pd: PhantomData<i32>,
} }
impl Hid { impl Hid {

2
src/services/mod.rs

@ -1,6 +1,6 @@
pub mod apt; pub mod apt;
pub mod hid; pub mod hid;
pub mod gsp; pub mod gspgpu;
pub use self::hid::Hid; pub use self::hid::Hid;
pub use self::apt::{Apt, Application}; pub use self::apt::{Apt, Application};

2
src/srv.rs

@ -3,7 +3,7 @@ use libctru::srv::*;
use core::marker::PhantomData; use core::marker::PhantomData;
pub struct Srv { pub struct Srv {
pd: PhantomData<i32> pd: PhantomData<i32>,
} }
impl Srv { impl Srv {

Loading…
Cancel
Save