text.rs 7.8 KB
Newer Older
Jeremy Soller's avatar
Jeremy Soller committed
1 2 3 4
extern crate ransid;

use std::collections::{BTreeSet, VecDeque};

Jeremy Soller's avatar
Jeremy Soller committed
5
use orbclient::{Event, EventOption};
6
use syscall::error::*;
Jeremy Soller's avatar
Jeremy Soller committed
7 8

use display::Display;
Jeremy Soller's avatar
Jeremy Soller committed
9
use screen::Screen;
Jeremy Soller's avatar
Jeremy Soller committed
10 11

pub struct TextScreen {
Jeremy Soller's avatar
Jeremy Soller committed
12
    pub console: ransid::Console,
Jeremy Soller's avatar
Jeremy Soller committed
13 14
    pub display: Display,
    pub changed: BTreeSet<usize>,
Jeremy Soller's avatar
Jeremy Soller committed
15
    pub ctrl: bool,
Jeremy Soller's avatar
Jeremy Soller committed
16 17 18 19 20 21 22 23 24
    pub input: VecDeque<u8>,
    pub end_of_input: bool,
    pub cooked: VecDeque<u8>,
    pub requested: usize
}

impl TextScreen {
    pub fn new(display: Display) -> TextScreen {
        TextScreen {
Jeremy Soller's avatar
Jeremy Soller committed
25
            console: ransid::Console::new(display.width/8, display.height/16),
Jeremy Soller's avatar
Jeremy Soller committed
26 27
            display: display,
            changed: BTreeSet::new(),
Jeremy Soller's avatar
Jeremy Soller committed
28
            ctrl: false,
Jeremy Soller's avatar
Jeremy Soller committed
29 30 31 32 33 34
            input: VecDeque::new(),
            end_of_input: false,
            cooked: VecDeque::new(),
            requested: 0
        }
    }
Jeremy Soller's avatar
Jeremy Soller committed
35 36 37 38 39 40 41 42 43 44 45
}

impl Screen for TextScreen {
    fn width(&self) -> usize {
        self.console.w
    }

    fn height(&self) -> usize {
        self.console.h
    }

Jeremy Soller's avatar
Jeremy Soller committed
46 47
    fn resize(&mut self, width: usize, height: usize) {
        self.display.resize(width, height);
Jeremy Soller's avatar
Jeremy Soller committed
48 49
        self.console.w = width / 8;
        self.console.h = height / 16;
Jeremy Soller's avatar
Jeremy Soller committed
50 51
    }

Jeremy Soller's avatar
Jeremy Soller committed
52 53 54 55 56
    fn event(&mut self, flags: usize) -> Result<usize> {
        self.requested = flags;
        Ok(0)
    }

57
    fn map(&self, _offset: usize, _size: usize) -> Result<usize> {
58 59 60
        Err(Error::new(EBADF))
    }

Jeremy Soller's avatar
Jeremy Soller committed
61 62 63 64 65 66 67 68 69
    fn input(&mut self, event: &Event) {
        let mut buf = vec![];

        match event.to_option() {
            EventOption::Key(key_event) => {
                if key_event.scancode == 0x1D {
                    self.ctrl = key_event.pressed;
                } else if key_event.pressed {
                    match key_event.scancode {
Jeremy Soller's avatar
Jeremy Soller committed
70 71 72
                        0x0E => { // Backspace
                            buf.extend_from_slice(b"\x7F");
                        },
Jeremy Soller's avatar
Jeremy Soller committed
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
                        0x47 => { // Home
                            buf.extend_from_slice(b"\x1B[H");
                        },
                        0x48 => { // Up
                            buf.extend_from_slice(b"\x1B[A");
                        },
                        0x49 => { // Page up
                            buf.extend_from_slice(b"\x1B[5~");
                        },
                        0x4B => { // Left
                            buf.extend_from_slice(b"\x1B[D");
                        },
                        0x4D => { // Right
                            buf.extend_from_slice(b"\x1B[C");
                        },
                        0x4F => { // End
                            buf.extend_from_slice(b"\x1B[F");
                        },
                        0x50 => { // Down
                            buf.extend_from_slice(b"\x1B[B");
                        },
                        0x51 => { // Page down
                            buf.extend_from_slice(b"\x1B[6~");
                        },
                        0x52 => { // Insert
                            buf.extend_from_slice(b"\x1B[2~");
                        },
                        0x53 => { // Delete
                            buf.extend_from_slice(b"\x1B[3~");
                        },
                        _ => {
                            let c = match key_event.character {
                                c @ 'A' ... 'Z' if self.ctrl => ((c as u8 - b'A') + b'\x01') as char,
                                c @ 'a' ... 'z' if self.ctrl => ((c as u8 - b'a') + b'\x01') as char,
                                c => c
                            };

                            if c != '\0' {
                                buf.extend_from_slice(&[c as u8]);
                            }
                        }
                    }
                }
            },
            _ => () //TODO: Mouse in terminal
        }
Jeremy Soller's avatar
Jeremy Soller committed
119 120 121 122 123 124 125 126 127 128

        if self.console.raw_mode {
            for &b in buf.iter() {
                self.input.push_back(b);
            }
        } else {
            for &b in buf.iter() {
                match b {
                    b'\x03' => {
                        self.end_of_input = true;
Jeremy Soller's avatar
Jeremy Soller committed
129
                        let _ = self.write(b"^C\n", true);
Jeremy Soller's avatar
Jeremy Soller committed
130 131 132
                    },
                    b'\x08' | b'\x7F' => {
                        if let Some(_c) = self.cooked.pop_back() {
Jeremy Soller's avatar
Jeremy Soller committed
133
                            let _ = self.write(b"\x08", true);
Jeremy Soller's avatar
Jeremy Soller committed
134 135
                        }
                    },
Jeremy Soller's avatar
Jeremy Soller committed
136 137 138
                    b'\x1B' => {
                        let _ = self.write(b"^[", true);
                    },
Jeremy Soller's avatar
Jeremy Soller committed
139 140 141 142 143
                    b'\n' | b'\r' => {
                        self.cooked.push_back(b);
                        while let Some(c) = self.cooked.pop_front() {
                            self.input.push_back(c);
                        }
Jeremy Soller's avatar
Jeremy Soller committed
144
                        let _ = self.write(b"\n", true);
Jeremy Soller's avatar
Jeremy Soller committed
145 146 147
                    },
                    _ => {
                        self.cooked.push_back(b);
Jeremy Soller's avatar
Jeremy Soller committed
148
                        let _ = self.write(&[b], true);
Jeremy Soller's avatar
Jeremy Soller committed
149 150 151 152 153 154
                    }
                }
            }
        }
    }

Jeremy Soller's avatar
Jeremy Soller committed
155
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
Jeremy Soller's avatar
Jeremy Soller committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169
        let mut i = 0;

        while i < buf.len() && ! self.input.is_empty() {
            buf[i] = self.input.pop_front().unwrap();
            i += 1;
        }

        if i == 0 {
            self.end_of_input = false;
        }

        Ok(i)
    }

Jeremy Soller's avatar
Jeremy Soller committed
170
    fn will_block(&self) -> bool {
Jeremy Soller's avatar
Jeremy Soller committed
171 172 173
        self.input.is_empty() && ! self.end_of_input
    }

Jeremy Soller's avatar
Jeremy Soller committed
174
    fn write(&mut self, buf: &[u8], sync: bool) -> Result<usize> {
Jeremy Soller's avatar
Jeremy Soller committed
175 176 177
        if self.console.cursor && self.console.x < self.console.w && self.console.y < self.console.h {
            let x = self.console.x;
            let y = self.console.y;
Jeremy Soller's avatar
Jeremy Soller committed
178
            self.display.invert(x * 8, y * 16, 8, 16);
Jeremy Soller's avatar
Jeremy Soller committed
179 180 181 182 183 184
            self.changed.insert(y);
        }

        {
            let display = &mut self.display;
            let changed = &mut self.changed;
Jeremy Soller's avatar
Jeremy Soller committed
185
            let input = &mut self.input;
Jeremy Soller's avatar
Jeremy Soller committed
186 187
            self.console.write(buf, |event| {
                match event {
Jeremy Soller's avatar
Jeremy Soller committed
188
                    ransid::Event::Char { x, y, c, color, bold, .. } => {
Jeremy Soller's avatar
Jeremy Soller committed
189 190 191
                        display.char(x * 8, y * 16, c, color.data, bold, false);
                        changed.insert(y);
                    },
Jeremy Soller's avatar
Jeremy Soller committed
192 193 194
                    ransid::Event::Input { data } => {
                        input.extend(data);
                    },
Jeremy Soller's avatar
Jeremy Soller committed
195
                    ransid::Event::Rect { x, y, w, h, color } => {
Jeremy Soller's avatar
Jeremy Soller committed
196 197 198 199 200
                        display.rect(x * 8, y * 16, w * 8, h * 16, color.data);
                        for y2 in y..y + h {
                            changed.insert(y2);
                        }
                    },
Jeremy Soller's avatar
Jeremy Soller committed
201
                    ransid::Event::ScreenBuffer { .. } => (),
Jeremy Soller's avatar
Jeremy Soller committed
202
                    ransid::Event::Scroll { rows, color } => {
Jeremy Soller's avatar
Jeremy Soller committed
203 204 205 206
                        display.scroll(rows * 16, color.data);
                        for y in 0..display.height/16 {
                            changed.insert(y);
                        }
Jeremy Soller's avatar
Jeremy Soller committed
207 208
                    },
                    ransid::Event::Title { .. } => ()
Jeremy Soller's avatar
Jeremy Soller committed
209 210 211 212 213 214 215
                }
            });
        }

        if self.console.cursor && self.console.x < self.console.w && self.console.y < self.console.h {
            let x = self.console.x;
            let y = self.console.y;
Jeremy Soller's avatar
Jeremy Soller committed
216
            self.display.invert(x * 8, y * 16, 8, 16);
Jeremy Soller's avatar
Jeremy Soller committed
217 218 219 220 221 222 223 224 225 226
            self.changed.insert(y);
        }

        if ! self.console.raw_mode && sync {
            self.sync();
        }

        Ok(buf.len())
    }

227
    fn seek(&mut self, _pos: usize, _whence: usize) -> Result<usize> {
Jeremy Soller's avatar
Jeremy Soller committed
228 229 230 231
        Ok(0)
    }

    fn sync(&mut self) {
Jeremy Soller's avatar
Jeremy Soller committed
232 233 234 235 236 237 238
        let width = self.display.width;
        for change in self.changed.iter() {
            self.display.sync(0, change * 16, width, 16);
        }
        self.changed.clear();
    }

Jeremy Soller's avatar
Jeremy Soller committed
239
    fn redraw(&mut self) {
Jeremy Soller's avatar
Jeremy Soller committed
240 241 242 243 244 245
        let width = self.display.width;
        let height = self.display.height;
        self.display.sync(0, 0, width, height);
        self.changed.clear();
    }
}