Browse Source

core::fmt::Write support for the console

pull/10/head
Fenrir 9 years ago
parent
commit
2feaf8f636
  1. 2
      ctru-sys/src/console.rs
  2. 9
      ctru-sys/src/lib.rs
  3. 11
      ctru-sys/src/libc.rs
  4. 7
      ctru-sys/src/os.rs
  5. 2
      ctru-sys/src/services/apt.rs
  6. 2
      ctru-sys/src/services/cam.rs
  7. 2
      ctru-sys/src/services/dsp.rs
  8. 29
      ctru-sys/src/services/fs.rs
  9. 2
      ctru-sys/src/services/gspgpu.rs
  10. 2
      ctru-sys/src/services/hb.rs
  11. 2
      ctru-sys/src/services/news.rs
  12. 2
      ctru-sys/src/services/pm.rs
  13. 2
      ctru-sys/src/services/srvpm.rs
  14. 2
      ctru-sys/src/services/y2r.rs
  15. 2
      ctru-sys/src/srv.rs
  16. 2
      ctru-sys/src/svc.rs
  17. 2
      ctru-sys/src/thread.rs
  18. 36
      src/console.rs

2
ctru-sys/src/console.rs

@ -1,4 +1,4 @@
use c_void; use libc::c_void;
use super::gfx::*; use super::gfx::*;

9
ctru-sys/src/lib.rs

@ -14,6 +14,7 @@ pub mod gfx;
pub mod gpu; pub mod gpu;
pub mod ipc; pub mod ipc;
pub mod lock; pub mod lock;
pub mod libc;
pub mod os; pub mod os;
pub mod sdmc; pub mod sdmc;
pub mod srv; pub mod srv;
@ -29,10 +30,4 @@ pub use self::types::*;
pub type Result = i32; pub type Result = i32;
pub type Handle = u32; pub type Handle = u32;
#[repr(u8)] pub type ThreadFunc = Option<extern "C" fn(arg1: *mut libc::c_void) -> ()>;
pub enum c_void {
__variant1,
__variant2,
}
pub type ThreadFunc = Option<extern "C" fn(arg1: *mut c_void) -> ()>;

11
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;
}

7
ctru-sys/src/os.rs

@ -1,6 +1,7 @@
//TODO: Fix Bindgen's issues again. //TODO: Fix Bindgen's issues again.
use ::Result; use ::Result;
use libc::c_void;
use types::*; use types::*;
#[inline] #[inline]
@ -35,9 +36,9 @@ impl ::core::default::Default for Struct_Unnamed2 {
} }
pub type OS_VersionBin = Struct_Unnamed2; pub type OS_VersionBin = Struct_Unnamed2;
extern "C" { extern "C" {
pub fn osConvertVirtToPhys(vaddr: *const ::c_void) -> u32; pub fn osConvertVirtToPhys(vaddr: *const c_void) -> u32;
pub fn osConvertOldLINEARMemToNew(vaddr: *const ::c_void) pub fn osConvertOldLINEARMemToNew(vaddr: *const c_void)
-> *mut ::c_void; -> *mut c_void;
pub fn osStrError(error: u32) -> *const u8; pub fn osStrError(error: u32) -> *const u8;
pub fn osGetMemRegionUsed(region: MemRegion) -> s64; pub fn osGetMemRegionUsed(region: MemRegion) -> s64;
pub fn osGetTime() -> u64; pub fn osGetTime() -> u64;

2
ctru-sys/src/services/apt.rs

@ -1,5 +1,5 @@
use ::{Result, Handle}; use ::{Result, Handle};
use ::c_void; use ::libc::c_void;
#[repr(C)] #[repr(C)]
#[derive(Clone, Copy)] #[derive(Clone, Copy)]

2
ctru-sys/src/services/cam.rs

@ -1,7 +1,7 @@
// TODO: Determine if anonymous enums are properly represented (they probably aren't) // TODO: Determine if anonymous enums are properly represented (they probably aren't)
use ::{Handle, Result}; use ::{Handle, Result};
use ::c_void; use libc::c_void;
use ::types::*; use ::types::*;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]

2
ctru-sys/src/services/dsp.rs

@ -1,5 +1,5 @@
use ::{Handle, Result}; use ::{Handle, Result};
use ::c_void; use ::libc::c_void;
#[repr(C)] #[repr(C)]
#[derive(Clone, Copy)] #[derive(Clone, Copy)]

29
ctru-sys/src/services/fs.rs

@ -1,6 +1,7 @@
// TODO: Determine if anonymous enums are properly represented (they probably aren't) // TODO: Determine if anonymous enums are properly represented (they probably aren't)
use ::{Handle, Result}; use ::{Handle, Result};
use ::libc::c_void;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
#[repr(C)] #[repr(C)]
@ -276,7 +277,7 @@ pub type FS_DeviceMoveContext = Struct_Unnamed24;
pub struct Struct_Unnamed25 { pub struct Struct_Unnamed25 {
pub _type: FS_PathType, pub _type: FS_PathType,
pub size: u32, pub size: u32,
pub data: *const ::c_void, pub data: *const c_void,
} }
impl ::core::clone::Clone for Struct_Unnamed25 { impl ::core::clone::Clone for Struct_Unnamed25 {
fn clone(&self) -> Self { *self } fn clone(&self) -> Self { *self }
@ -305,12 +306,12 @@ extern "C" {
pub fn fsExit(); pub fn fsExit();
pub fn fsUseSession(session: Handle, sdmc: u8); pub fn fsUseSession(session: Handle, sdmc: u8);
pub fn fsEndUseSession(); 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; -> FS_Path;
pub fn fsGetSessionHandle() -> *mut Handle; pub fn fsGetSessionHandle() -> *mut Handle;
pub fn FSUSER_Control(action: FS_Action, pub fn FSUSER_Control(action: FS_Action,
input: *mut ::c_void, inputSize: u32, input: *mut c_void, inputSize: u32,
output: *mut ::c_void, output: *mut c_void,
outputSize: u32) -> Result; outputSize: u32) -> Result;
pub fn FSUSER_Initialize(session: Handle) -> Result; pub fn FSUSER_Initialize(session: Handle) -> Result;
pub fn FSUSER_OpenFile(out: *mut Handle, archive: FS_Archive, 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_OpenArchive(archive: *mut FS_Archive) -> Result;
pub fn FSUSER_ControlArchive(archive: FS_Archive, pub fn FSUSER_ControlArchive(archive: FS_Archive,
action: FS_ArchiveAction, action: FS_ArchiveAction,
input: *mut ::c_void, input: *mut c_void,
inputSize: u32, inputSize: u32,
output: *mut ::c_void, output: *mut c_void,
outputSize: u32) -> Result; outputSize: u32) -> Result;
pub fn FSUSER_CloseArchive(archive: *mut FS_Archive) -> Result; pub fn FSUSER_CloseArchive(archive: *mut FS_Archive) -> Result;
pub fn FSUSER_GetFreeBytes(freeBytes: *mut u64, archive: FS_Archive) pub fn FSUSER_GetFreeBytes(freeBytes: *mut u64, archive: FS_Archive)
@ -495,22 +496,22 @@ extern "C" {
titleUniqueId: u32, titleUniqueId: u32,
titleVariation: u8) -> Result; titleVariation: u8) -> Result;
pub fn FSUSER_ControlSecureSave(action: FS_SecureSaveAction, pub fn FSUSER_ControlSecureSave(action: FS_SecureSaveAction,
input: *mut ::c_void, input: *mut c_void,
inputSize: u32, inputSize: u32,
output: *mut ::c_void, output: *mut c_void,
outputSize: u32) -> Result; outputSize: u32) -> Result;
pub fn FSUSER_GetMediaType(mediaType: *mut FS_MediaType) -> Result; pub fn FSUSER_GetMediaType(mediaType: *mut FS_MediaType) -> Result;
pub fn FSFILE_Control(handle: Handle, action: FS_FileAction, pub fn FSFILE_Control(handle: Handle, action: FS_FileAction,
input: *mut ::c_void, inputSize: u32, input: *mut c_void, inputSize: u32,
output: *mut ::c_void, output: *mut c_void,
outputSize: u32) -> Result; outputSize: u32) -> Result;
pub fn FSFILE_OpenSubFile(handle: Handle, subFile: *mut Handle, pub fn FSFILE_OpenSubFile(handle: Handle, subFile: *mut Handle,
offset: u64, size: u64) -> Result; offset: u64, size: u64) -> Result;
pub fn FSFILE_Read(handle: Handle, bytesRead: *mut u32, offset: u64, pub fn FSFILE_Read(handle: Handle, bytesRead: *mut u32, offset: u64,
buffer: *mut ::c_void, size: u32) buffer: *mut c_void, size: u32)
-> Result; -> Result;
pub fn FSFILE_Write(handle: Handle, bytesWritten: *mut u32, offset: u64, 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; flags: u32) -> Result;
pub fn FSFILE_GetSize(handle: Handle, size: *mut u64) -> Result; pub fn FSFILE_GetSize(handle: Handle, size: *mut u64) -> Result;
pub fn FSFILE_SetSize(handle: Handle, size: 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) pub fn FSFILE_OpenLinkFile(handle: Handle, linkFile: *mut Handle)
-> Result; -> Result;
pub fn FSDIR_Control(handle: Handle, action: FS_DirectoryAction, pub fn FSDIR_Control(handle: Handle, action: FS_DirectoryAction,
input: *mut ::c_void, inputSize: u32, input: *mut c_void, inputSize: u32,
output: *mut ::c_void, output: *mut c_void,
outputSize: u32) -> Result; outputSize: u32) -> Result;
pub fn FSDIR_Read(handle: Handle, entriesRead: *mut u32, pub fn FSDIR_Read(handle: Handle, entriesRead: *mut u32,
entryCount: u32, entries: *mut FS_DirectoryEntry) entryCount: u32, entries: *mut FS_DirectoryEntry)

2
ctru-sys/src/services/gspgpu.rs

@ -1,5 +1,5 @@
use ::{Handle, Result}; use ::{Handle, Result};
use ::c_void; use ::libc::c_void;
use ::types::*; use ::types::*;
#[inline] #[inline]

2
ctru-sys/src/services/hb.rs

@ -1,5 +1,5 @@
use ::Result; use ::Result;
use ::c_void; use ::libc::c_void;
extern "C" { extern "C" {

2
ctru-sys/src/services/news.rs

@ -1,5 +1,5 @@
use ::Result; use ::Result;
use ::c_void; use ::libc::c_void;
extern "C" { extern "C" {
pub fn newsInit() -> Result; pub fn newsInit() -> Result;

2
ctru-sys/src/services/pm.rs

@ -1,5 +1,5 @@
use ::{Handle, Result}; use ::{Handle, Result};
use ::c_void; use ::libc::c_void;
extern "C" { extern "C" {
pub fn pmInit() -> Result; pub fn pmInit() -> Result;

2
ctru-sys/src/services/srvpm.rs

@ -1,5 +1,5 @@
use ::{Handle, Result}; use ::{Handle, Result};
use ::c_void; use ::libc::c_void;
extern "C" { extern "C" {
pub fn srvPmInit() -> Result; pub fn srvPmInit() -> Result;

2
ctru-sys/src/services/y2r.rs

@ -1,5 +1,5 @@
use ::{Handle, Result}; use ::{Handle, Result};
use ::c_void; use ::libc::c_void;
use ::types::*; use ::types::*;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]

2
ctru-sys/src/srv.rs

@ -1,5 +1,5 @@
use {Result, Handle}; use {Result, Handle};
use c_void; use libc::c_void;
extern "C" { extern "C" {
pub fn srvInit() -> Result; pub fn srvInit() -> Result;

2
ctru-sys/src/svc.rs

@ -1,7 +1,7 @@
//TODO: Implement static functions //TODO: Implement static functions
use {Handle, Result}; use {Handle, Result};
use c_void; use libc::c_void;
use ThreadFunc; use ThreadFunc;
use types::*; use types::*;

2
ctru-sys/src/thread.rs

@ -1,5 +1,5 @@
use {Handle, Result}; use {Handle, Result};
use c_void; use libc::c_void;
use ThreadFunc; use ThreadFunc;
pub enum Struct_Thread_tag { } pub enum Struct_Thread_tag { }

36
src/console.rs

@ -1,34 +1,17 @@
use libctru::console::{consoleInit, consoleClear}; use libctru::console::{consoleInit, consoleClear};
use libctru::gfx; use libctru::gfx;
use libctru::libc;
use core::fmt::{self, Write};
use core::default::Default; use core::default::Default;
use core::marker::PhantomData; use core::marker::PhantomData;
use core::ptr; use core::ptr;
extern "C" {
fn putchar(ch: u8) -> i32;
}
pub struct Console { pub struct Console {
pd: PhantomData<()>, pd: PhantomData<()>,
} }
impl Console { 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) { pub fn clear(&mut self) {
unsafe { consoleClear() } unsafe { consoleClear() }
} }
@ -36,7 +19,20 @@ impl Console {
impl Default for Console { impl Default for Console {
fn default() -> Self { 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 } 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)
}
}
}

Loading…
Cancel
Save