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
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#![no_std]

#[macro_use]
extern crate bitflags;
extern crate libc;

use core::{cmp, mem, ptr, slice};

#[derive(Debug)]
#[repr(C)]
pub struct Screen(c::tsm_screen);

impl Screen {
    #[inline]
    pub fn new(x: uint, y: uint) -> Result<Self, usize> { unsafe {
        let mut s = mem::uninitialized();
        from_ret(c::tsm_screen_new(&mut s, x, y)).map(|_| Screen(s))
    } }

    #[inline]
    pub fn size(&self) -> (uint, uint) { (self.0.size_x, self.0.size_y) }

    #[inline]
    pub fn resize(&mut self, x: uint, y: uint) -> Result<(), usize> { unsafe {
        from_ret(c::tsm_screen_resize(&mut self.0, x as _, y as _)).map(|_| ())
    } }

    #[inline]
    pub fn set_margins(&mut self, top: uint, btm: uint) -> Result<(), usize> { unsafe {
        from_ret(c::tsm_screen_resize(&mut self.0, top as _, btm as _)).map(|_| ())
    } }

    #[inline]
    pub fn set_max_sb(&mut self, max: uint) { unsafe { c::tsm_screen_set_max_sb(&mut self.0, max) } }

    #[inline]
    pub fn clear_sb(&mut self) { unsafe { c::tsm_screen_clear_sb(&mut self.0) } }



    #[inline]
    pub fn sb_up(&mut self, n: uint) { unsafe { c::tsm_screen_sb_up(&mut self.0, n) } }

    #[inline]
    pub fn sb_dn(&mut self, n: uint) { unsafe { c::tsm_screen_sb_down(&mut self.0, n) } }

    #[inline]
    pub fn sb_pg_up(&mut self, n: uint) { unsafe { c::tsm_screen_sb_page_up(&mut self.0, n) } }

    #[inline]
    pub fn sb_pg_dn(&mut self, n: uint) { unsafe { c::tsm_screen_sb_page_down(&mut self.0, n) } }

    #[inline]
    pub fn sb_reset(&mut self) { unsafe { c::tsm_screen_sb_reset(&mut self.0) } }



    #[inline]
    pub fn def_attr(&self) -> &Attr { &self.0.def_attr }

    #[inline]
    pub fn def_attr_mut(&mut self) -> &mut Attr { &mut self.0.def_attr }

    #[inline]
    pub fn reset(&mut self) { unsafe { c::tsm_screen_reset(&mut self.0) } }

    #[inline]
    pub fn flags(&self) -> Flags { Flags { bits: self.0.flags } }

    #[inline]
    pub fn modify_flags<F: FnOnce(&mut Flags)>(&mut self, f: F) { unsafe {
        let mut flags = Flags { bits: self.0.flags };
        f(&mut flags);
        c::tsm_screen_set_flags(&mut self.0, flags.bits);
    } }



    #[inline]
    pub fn cursor_pos(&self) -> (uint, uint) { (self.0.cursor_x, self.0.cursor_y) }



    #[inline]
    pub fn write(&mut self, x: u32, attr: Attr) { unsafe {
        c::tsm_screen_write(&mut self.0, x, &attr)
    } }

    #[inline]
    pub fn newline(&mut self) { unsafe { c::tsm_screen_newline(&mut self.0) } }

    #[inline]
    pub fn scroll(&mut self, n: isize) { unsafe {
        if n < 0 { c::tsm_screen_scroll_up  (&mut self.0, -n as _) }
        else     { c::tsm_screen_scroll_down(&mut self.0,  n as _) }
    } }

    #[inline]
    pub fn move_to(&mut self, x: uint, y: uint) { unsafe {
        c::tsm_screen_move_to(&mut self.0, x, y)
    } }

    #[inline]
    pub fn move_y(&mut self, y: isize, scroll: bool) { unsafe {
        if y < 0 { c::tsm_screen_move_up  (&mut self.0, -y as _, scroll) }
        else     { c::tsm_screen_move_down(&mut self.0,  y as _, scroll) }
    } }

    #[inline]
    pub fn move_x(&mut self, x: isize) { unsafe {
        if x < 0 { c::tsm_screen_move_left (&mut self.0, -x as _) }
        else     { c::tsm_screen_move_right(&mut self.0,  x as _) }
    } }

    #[inline]
    pub fn move_line_home(&mut self) { unsafe {
        c::tsm_screen_move_line_home(&mut self.0)
    } }

    #[inline]
    pub fn move_line_end(&mut self) { unsafe {
        c::tsm_screen_move_line_end(&mut self.0)
    } }

    #[inline]
    fn erase_region(&mut self, from: (uint, uint), to: (uint, uint), protect: bool) { unsafe {
        self.inc_age();
        c::screen_erase_region(&mut self.0, from.0, from.1, to.0, to.1, protect);
    } }

    #[inline]
    pub fn erase(&mut self, erasure: Erasure, protect: bool) {
        use self::Erasure::*;
        let x = cmp::max(self.0.size_x - 1, self.0.cursor_x);
        let (xl, xu) = match erasure {
            Cursor => (x, x),
            LineToCursor | ScreenToCursor => (0, self.0.cursor_x),
            LineFromCursor | ScreenFromCursor => (x, self.0.size_x - 1),
            Line | Screen => (0, self.0.size_x - 1),
        };
        let (yl, yu) = match erasure {
            Cursor | LineToCursor | LineFromCursor | Line => (self.0.cursor_y, self.0.cursor_y),
            ScreenToCursor => (0, self.0.cursor_y),
            ScreenFromCursor => (self.0.cursor_y, self.0.size_y - 1),
            Screen => (0, self.0.size_y - 1),
        };
        self.erase_region((xl, yl), (xu, yu), protect);
    }

    #[inline]
    pub fn draw<F: FnOnce(Draw1) -> Result<(), ()>>(&self, f: F) -> Age { unsafe {
        unsafe extern "C"
        fn do_draw<F>(_con: *mut c::tsm_screen, id: u32, xs: *const u32, len: usize,
                      w: uint, x: uint, y: uint, attr: *const c::tsm_screen_attr,
                      age: c::tsm_age_t, f: *mut c::c_void) -> c::c_int
          where F: FnOnce(Draw1) -> Result<(), ()> {
            match ptr::read(f as *mut F)(Draw1 {
                id: id, xs: slice::from_raw_parts(xs, len),
                width: w, pos: (x, y), attr: *attr, age: age,
            }) { Ok(()) => 0, Err(()) => -1 }
        }

        let mut f = mem::ManuallyDrop::new(f);
        c::tsm_screen_draw(&self.0 as *const _ as *mut _,
                           Some(do_draw::<F>), &mut f as *mut _ as *mut c::c_void)
    } }

    #[inline(always)]
    fn inc_age(&mut self) {
        use core::num::Wrapping as w;
        self.0.age_cnt += w(1);
        if 0 == self.0.age_cnt.0 {
            self.0.set_age_reset(1);
            self.0.age_cnt += w(1);
        }
    }
}

impl Drop for Screen {
    #[inline]
    fn drop(&mut self) { unsafe { c::tsm_screen_old(&mut self.0) } }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Erasure {
    Cursor,
    LineToCursor, LineFromCursor, Line,
    ScreenToCursor, ScreenFromCursor, Screen,
}

bitflags! {
    pub struct Flags: uint {
        const InsertMode = c::TSM_SCREEN_INSERT_MODE;
        const AutoWrap   = c::TSM_SCREEN_AUTO_WRAP;
        const RelOrigin  = c::TSM_SCREEN_REL_ORIGIN;
        const Inverse    = c::TSM_SCREEN_INVERSE;
        const HideCursor = c::TSM_SCREEN_HIDE_CURSOR;
        const FixedPos   = c::TSM_SCREEN_FIXED_POS;
        const Alternate  = c::TSM_SCREEN_ALTERNATE;
    }
}

bitflags! {
    #[repr(C)]
    pub struct AttrFlags: uint {
        const Bold      = c::TSM_ATTR_BOLD;
        const Underline = c::TSM_ATTR_UNDERLINE;
        const Inverse   = c::TSM_ATTR_INVERSE;
        const Protect   = c::TSM_ATTR_PROTECT;
        const Blink     = c::TSM_ATTR_BLINK;
    }
}

pub use c::tsm_age_t as Age;
pub use c::tsm_screen_attr as Attr;

#[derive(Debug, Clone, Copy)]
pub struct Draw1<'a> {
    pub id: u32,
    pub xs: &'a [u32],
    pub width: uint,
    pub pos: (uint, uint),
    pub attr: Attr,
    pub age: Age,
}

#[allow(dead_code)]
mod c { pub use libc::*; include!(concat!(env!("OUT_DIR"), "/raw.rs")); }

use c::{c_uint as uint};

fn from_ret(r: c::c_int) -> Result<usize, usize> {
    if r < 0 { Err(-r as _) } else { Ok(r as _) }
}