aboutsummaryrefslogtreecommitdiff
path: root/src/console.rs
blob: 78a88a2930c6160cdf3c2fe9ae4ffceef704c482 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use libctru::console::*;
use libctru::libc;

use gfx::Screen;

use std::fmt::{self, Write};
use std::default::Default;
use std::ptr;

pub struct Console {
    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() }
    }
}

impl Default for Console {
    fn default() -> Self {
        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 {
        // Writing 0 bytes to the console fails
        if s.is_empty() {
            return Ok(())
        }
        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(())
        } else {
            Err(fmt::Error)
        }
    }
}