From 2feaf8f636668641eb16e25208e53b718e191f66 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Fri, 3 Jun 2016 16:20:47 -0700 Subject: [PATCH] core::fmt::Write support for the console --- ctru-sys/src/console.rs | 2 +- ctru-sys/src/lib.rs | 9 ++------- ctru-sys/src/libc.rs | 11 ++++++++++ ctru-sys/src/os.rs | 7 ++++--- ctru-sys/src/services/apt.rs | 2 +- ctru-sys/src/services/cam.rs | 2 +- ctru-sys/src/services/dsp.rs | 2 +- ctru-sys/src/services/fs.rs | 29 +++++++++++++------------- ctru-sys/src/services/gspgpu.rs | 2 +- ctru-sys/src/services/hb.rs | 2 +- ctru-sys/src/services/news.rs | 2 +- ctru-sys/src/services/pm.rs | 2 +- ctru-sys/src/services/srvpm.rs | 2 +- ctru-sys/src/services/y2r.rs | 2 +- ctru-sys/src/srv.rs | 2 +- ctru-sys/src/svc.rs | 2 +- ctru-sys/src/thread.rs | 2 +- src/console.rs | 36 +++++++++++++++------------------ src/services/apt.rs | 2 +- 19 files changed, 62 insertions(+), 58 deletions(-) create mode 100644 ctru-sys/src/libc.rs diff --git a/ctru-sys/src/console.rs b/ctru-sys/src/console.rs index fcaa747..0fd4924 100644 --- a/ctru-sys/src/console.rs +++ b/ctru-sys/src/console.rs @@ -1,4 +1,4 @@ -use c_void; +use libc::c_void; use super::gfx::*; diff --git a/ctru-sys/src/lib.rs b/ctru-sys/src/lib.rs index 4b212a5..6676277 100644 --- a/ctru-sys/src/lib.rs +++ b/ctru-sys/src/lib.rs @@ -14,6 +14,7 @@ pub mod gfx; pub mod gpu; pub mod ipc; pub mod lock; +pub mod libc; pub mod os; pub mod sdmc; pub mod srv; @@ -29,10 +30,4 @@ pub use self::types::*; pub type Result = i32; pub type Handle = u32; -#[repr(u8)] -pub enum c_void { - __variant1, - __variant2, -} - -pub type ThreadFunc = Option ()>; +pub type ThreadFunc = Option ()>; diff --git a/ctru-sys/src/libc.rs b/ctru-sys/src/libc.rs new file mode 100644 index 0000000..95e37db --- /dev/null +++ b/ctru-sys/src/libc.rs @@ -0,0 +1,11 @@ +pub const STDOUT_FILENO: i32 = 1; + +#[repr(u8)] +pub enum c_void { + __variant1, + __variant2, +} + +extern "C" { + pub fn write(fd: i32, buf: *const c_void, count: usize) -> isize; +} diff --git a/ctru-sys/src/os.rs b/ctru-sys/src/os.rs index 7dbe3be..ce0df6c 100644 --- a/ctru-sys/src/os.rs +++ b/ctru-sys/src/os.rs @@ -1,6 +1,7 @@ //TODO: Fix Bindgen's issues again. use ::Result; +use libc::c_void; use types::*; #[inline] @@ -35,9 +36,9 @@ impl ::core::default::Default for Struct_Unnamed2 { } pub type OS_VersionBin = Struct_Unnamed2; extern "C" { - pub fn osConvertVirtToPhys(vaddr: *const ::c_void) -> u32; - pub fn osConvertOldLINEARMemToNew(vaddr: *const ::c_void) - -> *mut ::c_void; + pub fn osConvertVirtToPhys(vaddr: *const c_void) -> u32; + pub fn osConvertOldLINEARMemToNew(vaddr: *const c_void) + -> *mut c_void; pub fn osStrError(error: u32) -> *const u8; pub fn osGetMemRegionUsed(region: MemRegion) -> s64; pub fn osGetTime() -> u64; diff --git a/ctru-sys/src/services/apt.rs b/ctru-sys/src/services/apt.rs index a6ee0c0..8c32e91 100644 --- a/ctru-sys/src/services/apt.rs +++ b/ctru-sys/src/services/apt.rs @@ -1,5 +1,5 @@ use ::{Result, Handle}; -use ::c_void; +use ::libc::c_void; #[repr(C)] #[derive(Clone, Copy)] diff --git a/ctru-sys/src/services/cam.rs b/ctru-sys/src/services/cam.rs index e9ef4f6..811956b 100644 --- a/ctru-sys/src/services/cam.rs +++ b/ctru-sys/src/services/cam.rs @@ -1,7 +1,7 @@ // TODO: Determine if anonymous enums are properly represented (they probably aren't) use ::{Handle, Result}; -use ::c_void; +use libc::c_void; use ::types::*; #[derive(Clone, Copy)] diff --git a/ctru-sys/src/services/dsp.rs b/ctru-sys/src/services/dsp.rs index 12edfae..de8241e 100644 --- a/ctru-sys/src/services/dsp.rs +++ b/ctru-sys/src/services/dsp.rs @@ -1,5 +1,5 @@ use ::{Handle, Result}; -use ::c_void; +use ::libc::c_void; #[repr(C)] #[derive(Clone, Copy)] diff --git a/ctru-sys/src/services/fs.rs b/ctru-sys/src/services/fs.rs index 4cd767c..0608bb9 100644 --- a/ctru-sys/src/services/fs.rs +++ b/ctru-sys/src/services/fs.rs @@ -1,6 +1,7 @@ // TODO: Determine if anonymous enums are properly represented (they probably aren't) use ::{Handle, Result}; +use ::libc::c_void; #[derive(Clone, Copy)] #[repr(C)] @@ -276,7 +277,7 @@ pub type FS_DeviceMoveContext = Struct_Unnamed24; pub struct Struct_Unnamed25 { pub _type: FS_PathType, pub size: u32, - pub data: *const ::c_void, + pub data: *const c_void, } impl ::core::clone::Clone for Struct_Unnamed25 { fn clone(&self) -> Self { *self } @@ -305,12 +306,12 @@ extern "C" { pub fn fsExit(); pub fn fsUseSession(session: Handle, sdmc: u8); pub fn fsEndUseSession(); - pub fn fsMakePath(_type: FS_PathType, path: *const ::c_void) + pub fn fsMakePath(_type: FS_PathType, path: *const c_void) -> FS_Path; pub fn fsGetSessionHandle() -> *mut Handle; pub fn FSUSER_Control(action: FS_Action, - input: *mut ::c_void, inputSize: u32, - output: *mut ::c_void, + input: *mut c_void, inputSize: u32, + output: *mut c_void, outputSize: u32) -> Result; pub fn FSUSER_Initialize(session: Handle) -> Result; pub fn FSUSER_OpenFile(out: *mut Handle, archive: FS_Archive, @@ -339,9 +340,9 @@ extern "C" { pub fn FSUSER_OpenArchive(archive: *mut FS_Archive) -> Result; pub fn FSUSER_ControlArchive(archive: FS_Archive, action: FS_ArchiveAction, - input: *mut ::c_void, + input: *mut c_void, inputSize: u32, - output: *mut ::c_void, + output: *mut c_void, outputSize: u32) -> Result; pub fn FSUSER_CloseArchive(archive: *mut FS_Archive) -> Result; pub fn FSUSER_GetFreeBytes(freeBytes: *mut u64, archive: FS_Archive) @@ -495,22 +496,22 @@ extern "C" { titleUniqueId: u32, titleVariation: u8) -> Result; pub fn FSUSER_ControlSecureSave(action: FS_SecureSaveAction, - input: *mut ::c_void, + input: *mut c_void, inputSize: u32, - output: *mut ::c_void, + output: *mut c_void, outputSize: u32) -> Result; pub fn FSUSER_GetMediaType(mediaType: *mut FS_MediaType) -> Result; pub fn FSFILE_Control(handle: Handle, action: FS_FileAction, - input: *mut ::c_void, inputSize: u32, - output: *mut ::c_void, + input: *mut c_void, inputSize: u32, + output: *mut c_void, outputSize: u32) -> Result; pub fn FSFILE_OpenSubFile(handle: Handle, subFile: *mut Handle, offset: u64, size: u64) -> Result; pub fn FSFILE_Read(handle: Handle, bytesRead: *mut u32, offset: u64, - buffer: *mut ::c_void, size: u32) + buffer: *mut c_void, size: u32) -> Result; pub fn FSFILE_Write(handle: Handle, bytesWritten: *mut u32, offset: u64, - buffer: *const ::c_void, size: u32, + buffer: *const c_void, size: u32, flags: u32) -> Result; pub fn FSFILE_GetSize(handle: Handle, size: *mut u64) -> Result; pub fn FSFILE_SetSize(handle: Handle, size: u64) -> Result; @@ -524,8 +525,8 @@ extern "C" { pub fn FSFILE_OpenLinkFile(handle: Handle, linkFile: *mut Handle) -> Result; pub fn FSDIR_Control(handle: Handle, action: FS_DirectoryAction, - input: *mut ::c_void, inputSize: u32, - output: *mut ::c_void, + input: *mut c_void, inputSize: u32, + output: *mut c_void, outputSize: u32) -> Result; pub fn FSDIR_Read(handle: Handle, entriesRead: *mut u32, entryCount: u32, entries: *mut FS_DirectoryEntry) diff --git a/ctru-sys/src/services/gspgpu.rs b/ctru-sys/src/services/gspgpu.rs index 7265812..ffe4703 100644 --- a/ctru-sys/src/services/gspgpu.rs +++ b/ctru-sys/src/services/gspgpu.rs @@ -1,5 +1,5 @@ use ::{Handle, Result}; -use ::c_void; +use ::libc::c_void; use ::types::*; #[inline] diff --git a/ctru-sys/src/services/hb.rs b/ctru-sys/src/services/hb.rs index 7bfe5fb..6d6264e 100644 --- a/ctru-sys/src/services/hb.rs +++ b/ctru-sys/src/services/hb.rs @@ -1,5 +1,5 @@ use ::Result; -use ::c_void; +use ::libc::c_void; extern "C" { diff --git a/ctru-sys/src/services/news.rs b/ctru-sys/src/services/news.rs index fc82784..fb34f41 100644 --- a/ctru-sys/src/services/news.rs +++ b/ctru-sys/src/services/news.rs @@ -1,5 +1,5 @@ use ::Result; -use ::c_void; +use ::libc::c_void; extern "C" { pub fn newsInit() -> Result; diff --git a/ctru-sys/src/services/pm.rs b/ctru-sys/src/services/pm.rs index 971fbb3..958dbc4 100644 --- a/ctru-sys/src/services/pm.rs +++ b/ctru-sys/src/services/pm.rs @@ -1,5 +1,5 @@ use ::{Handle, Result}; -use ::c_void; +use ::libc::c_void; extern "C" { pub fn pmInit() -> Result; diff --git a/ctru-sys/src/services/srvpm.rs b/ctru-sys/src/services/srvpm.rs index 0141898..47ae21f 100644 --- a/ctru-sys/src/services/srvpm.rs +++ b/ctru-sys/src/services/srvpm.rs @@ -1,5 +1,5 @@ use ::{Handle, Result}; -use ::c_void; +use ::libc::c_void; extern "C" { pub fn srvPmInit() -> Result; diff --git a/ctru-sys/src/services/y2r.rs b/ctru-sys/src/services/y2r.rs index d3393d3..6554ad6 100644 --- a/ctru-sys/src/services/y2r.rs +++ b/ctru-sys/src/services/y2r.rs @@ -1,5 +1,5 @@ use ::{Handle, Result}; -use ::c_void; +use ::libc::c_void; use ::types::*; #[derive(Clone, Copy)] diff --git a/ctru-sys/src/srv.rs b/ctru-sys/src/srv.rs index e465b8a..050c32f 100644 --- a/ctru-sys/src/srv.rs +++ b/ctru-sys/src/srv.rs @@ -1,5 +1,5 @@ use {Result, Handle}; -use c_void; +use libc::c_void; extern "C" { pub fn srvInit() -> Result; diff --git a/ctru-sys/src/svc.rs b/ctru-sys/src/svc.rs index 0ad3fc9..12efcca 100644 --- a/ctru-sys/src/svc.rs +++ b/ctru-sys/src/svc.rs @@ -1,7 +1,7 @@ //TODO: Implement static functions use {Handle, Result}; -use c_void; +use libc::c_void; use ThreadFunc; use types::*; diff --git a/ctru-sys/src/thread.rs b/ctru-sys/src/thread.rs index 960807b..74a92e0 100644 --- a/ctru-sys/src/thread.rs +++ b/ctru-sys/src/thread.rs @@ -1,5 +1,5 @@ use {Handle, Result}; -use c_void; +use libc::c_void; use ThreadFunc; pub enum Struct_Thread_tag { } diff --git a/src/console.rs b/src/console.rs index a717aa5..0d0535a 100644 --- a/src/console.rs +++ b/src/console.rs @@ -1,34 +1,17 @@ use libctru::console::{consoleInit, consoleClear}; use libctru::gfx; +use libctru::libc; +use core::fmt::{self, Write}; use core::default::Default; use core::marker::PhantomData; use core::ptr; -extern "C" { - fn putchar(ch: u8) -> i32; -} - pub struct Console { pd: PhantomData<()>, } impl Console { - pub fn print<'a>(&mut self, s: &'a str) { - unsafe { - for ch in s.as_bytes().iter() { - putchar(*ch); - } - } - } - - pub fn println<'a>(&mut self, s: &'a str) { - unsafe { - self.print(s); - putchar('\n' as u8); - } - } - pub fn clear(&mut self) { unsafe { consoleClear() } } @@ -36,7 +19,20 @@ impl Console { impl Default for Console { fn default() -> Self { - unsafe { consoleInit(gfx::gfxScreen_t::GFX_TOP, ptr::null_mut()); } + unsafe { + consoleInit(gfx::gfxScreen_t::GFX_TOP, ptr::null_mut()); + } Console { pd: PhantomData } } } + +impl Write for Console { + fn write_str(&mut self, s: &str) -> fmt::Result { + let ret = unsafe { libc::write(libc::STDOUT_FILENO, s.as_ptr() as *const _, s.len()) }; + if ret == s.len() as isize { + Ok(()) + } else { + Err(fmt::Error) + } + } +} diff --git a/src/services/apt.rs b/src/services/apt.rs index b6efc86..8c891a6 100644 --- a/src/services/apt.rs +++ b/src/services/apt.rs @@ -79,7 +79,7 @@ impl Apt { } pub fn main_loop(&mut self) -> bool { - unsafe { + unsafe { match apt::aptMainLoop() { 1 => true, 0 => false,