scheme.rs 9.17 KB
Newer Older
Jeremy Soller's avatar
Jeremy Soller committed
1
use std::collections::BTreeMap;
Jeremy Soller's avatar
Jeremy Soller committed
2
use std::{mem, slice, str};
Jeremy Soller's avatar
Jeremy Soller committed
3

Jeremy Soller's avatar
Jeremy Soller committed
4
use orbclient::{Event, EventOption};
5
use syscall::{Result, Error, EACCES, EBADF, ENOENT, O_NONBLOCK, SchemeMut};
Jeremy Soller's avatar
Jeremy Soller committed
6 7

use display::Display;
Jeremy Soller's avatar
Jeremy Soller committed
8
use screen::{Screen, GraphicScreen, TextScreen};
Jeremy Soller's avatar
Jeremy Soller committed
9

10 11 12 13 14 15 16 17 18 19 20 21
#[derive(Clone)]
enum HandleKind {
    Input,
    Screen(usize),
}

#[derive(Clone)]
struct Handle {
    kind: HandleKind,
    flags: usize,
}

Jeremy Soller's avatar
Jeremy Soller committed
22
pub struct DisplayScheme {
23 24
    width: usize,
    height: usize,
25
    active: usize,
26 27 28
    pub screens: BTreeMap<usize, Box<Screen>>,
    next_id: usize,
    handles: BTreeMap<usize, Handle>,
Jeremy Soller's avatar
Jeremy Soller committed
29 30 31
}

impl DisplayScheme {
Jeremy Soller's avatar
Jeremy Soller committed
32
    pub fn new(width: usize, height: usize, onscreen: usize, spec: &[bool]) -> DisplayScheme {
Jeremy Soller's avatar
Jeremy Soller committed
33
        let mut screens: BTreeMap<usize, Box<Screen>> = BTreeMap::new();
Jeremy Soller's avatar
Jeremy Soller committed
34 35 36 37 38 39 40 41 42 43

        let mut screen_i = 1;
        for &screen_type in spec.iter() {
            if screen_type {
                screens.insert(screen_i, Box::new(GraphicScreen::new(Display::new(width, height, onscreen))));
            } else {
                screens.insert(screen_i, Box::new(TextScreen::new(Display::new(width, height, onscreen))));
            }
            screen_i += 1;
        }
Jeremy Soller's avatar
Jeremy Soller committed
44 45

        DisplayScheme {
46 47
            width: width,
            height: height,
48
            active: 1,
49 50 51
            screens: screens,
            next_id: 0,
            handles: BTreeMap::new(),
Jeremy Soller's avatar
Jeremy Soller committed
52 53 54
        }
    }

55 56 57 58 59 60 61 62 63 64 65 66 67 68
    pub fn can_read(&self, id: usize) -> Option<usize> {
        if let Some(handle) = self.handles.get(&id) {
            if let HandleKind::Screen(screen_i) = handle.kind {
                if let Some(screen) = self.screens.get(&screen_i) {
                    match screen.can_read() {
                        Some(count) => return Some(count),
                        None => if handle.flags & O_NONBLOCK == O_NONBLOCK {
                            return Some(0);
                        } else {
                            return None;
                        }
                    }
                }
            }
Jeremy Soller's avatar
Jeremy Soller committed
69
        }
70 71

        Some(0)
Jeremy Soller's avatar
Jeremy Soller committed
72 73 74
    }
}

75
impl SchemeMut for DisplayScheme {
76
    fn open(&mut self, path: &[u8], flags: usize, uid: u32, _gid: u32) -> Result<usize> {
Jeremy Soller's avatar
Jeremy Soller committed
77
        if path == b"input" {
Jeremy Soller's avatar
Jeremy Soller committed
78
            if uid == 0 {
79 80 81 82 83 84 85 86 87
                let id = self.next_id;
                self.next_id += 1;

                self.handles.insert(id, Handle {
                    kind: HandleKind::Input,
                    flags: flags
                });

                Ok(id)
Jeremy Soller's avatar
Jeremy Soller committed
88 89 90
            } else {
                Err(Error::new(EACCES))
            }
Jeremy Soller's avatar
Jeremy Soller committed
91
        } else {
92 93
            let path_str = str::from_utf8(path).unwrap_or("").trim_matches('/');
            let mut parts = path_str.split('/');
94 95
            let screen_i = parts.next().unwrap_or("").parse::<usize>().unwrap_or(0);
            if self.screens.contains_key(&screen_i) {
96 97
                for cmd in parts {
                    if cmd == "activate" {
98
                        self.active = screen_i;
99 100
                    }
                }
101 102 103 104 105 106 107 108 109

                let id = self.next_id;
                self.next_id += 1;

                self.handles.insert(id, Handle {
                    kind: HandleKind::Screen(screen_i),
                    flags: flags
                });

Jeremy Soller's avatar
Jeremy Soller committed
110 111 112 113 114 115 116
                Ok(id)
            } else {
                Err(Error::new(ENOENT))
            }
        }
    }

Jeremy Soller's avatar
Jeremy Soller committed
117
    fn dup(&mut self, id: usize, _buf: &[u8]) -> Result<usize> {
118 119 120 121 122 123 124 125
        let handle = self.handles.get(&id).map(|handle| handle.clone()).ok_or(Error::new(EBADF))?;

        let new_id = self.next_id;
        self.next_id += 1;

        self.handles.insert(new_id, handle.clone());

        Ok(new_id)
Jeremy Soller's avatar
Jeremy Soller committed
126 127
    }

128
    fn fevent(&mut self, id: usize, flags: usize) -> Result<usize> {
129 130 131 132 133 134
        let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;

        if let HandleKind::Screen(screen_i) = handle.kind {
            if let Some(mut screen) = self.screens.get_mut(&screen_i) {
                return screen.event(flags).and(Ok(screen_i));
            }
Jeremy Soller's avatar
Jeremy Soller committed
135
        }
136 137

        Err(Error::new(EBADF))
Jeremy Soller's avatar
Jeremy Soller committed
138 139
    }

140
    fn fmap(&mut self, id: usize, offset: usize, size: usize) -> Result<usize> {
141 142 143 144 145 146
        let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;

        if let HandleKind::Screen(screen_i) = handle.kind {
            if let Some(screen) = self.screens.get(&screen_i) {
                return screen.map(offset, size);
            }
147
        }
148 149

        Err(Error::new(EBADF))
150 151
    }

152
    fn fpath(&mut self, id: usize, buf: &mut [u8]) -> Result<usize> {
153 154 155 156 157 158 159 160 161 162 163
        let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;

        let path_str = match handle.kind {
            HandleKind::Input => {
                format!("display:input/{}/{}", self.width, self.height)
            },
            HandleKind::Screen(screen_i) => if let Some(screen) = self.screens.get(&screen_i) {
                format!("display:{}/{}/{}", screen_i, screen.width(), screen.height())
            } else {
                return Err(Error::new(EBADF));
            }
Jeremy Soller's avatar
Jeremy Soller committed
164 165 166 167 168 169 170 171 172 173 174 175 176
        };

        let path = path_str.as_bytes();

        let mut i = 0;
        while i < buf.len() && i < path.len() {
            buf[i] = path[i];
            i += 1;
        }

        Ok(i)
    }

177
    fn fsync(&mut self, id: usize) -> Result<usize> {
178 179 180 181 182 183 184 185
        let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;

        if let HandleKind::Screen(screen_i) = handle.kind {
            if let Some(mut screen) = self.screens.get_mut(&screen_i) {
                if screen_i == self.active {
                    screen.sync();
                }
                return Ok(0);
Jeremy Soller's avatar
Jeremy Soller committed
186 187
            }
        }
188 189

        Err(Error::new(EBADF))
Jeremy Soller's avatar
Jeremy Soller committed
190 191
    }

192
    fn read(&mut self, id: usize, buf: &mut [u8]) -> Result<usize> {
193 194 195 196 197 198
        let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;

        if let HandleKind::Screen(screen_i) = handle.kind {
            if let Some(mut screen) = self.screens.get_mut(&screen_i) {
                return screen.read(buf);
            }
Jeremy Soller's avatar
Jeremy Soller committed
199
        }
200 201

        Err(Error::new(EBADF))
Jeremy Soller's avatar
Jeremy Soller committed
202 203
    }

204
    fn write(&mut self, id: usize, buf: &[u8]) -> Result<usize> {
205 206 207 208
        let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;

        match handle.kind {
            HandleKind::Input => if buf.len() == 1 && buf[0] >= 0xF4 {
Jeremy Soller's avatar
Jeremy Soller committed
209
                let new_active = (buf[0] - 0xF4) as usize + 1;
210 211
                if let Some(mut screen) = self.screens.get_mut(&new_active) {
                    self.active = new_active;
Jeremy Soller's avatar
Jeremy Soller committed
212 213 214 215
                    screen.redraw();
                }
                Ok(1)
            } else {
Jeremy Soller's avatar
Jeremy Soller committed
216 217 218
                let events = unsafe { slice::from_raw_parts(buf.as_ptr() as *const Event, buf.len()/mem::size_of::<Event>()) };

                for event in events.iter() {
Jeremy Soller's avatar
Jeremy Soller committed
219 220 221
                    let mut new_active_opt = None;
                    match event.to_option() {
                        EventOption::Key(key_event) => match key_event.scancode {
Jeremy Soller's avatar
Jeremy Soller committed
222
                            f @ 0x3B ... 0x44 => { // F1 through F10
Jeremy Soller's avatar
Jeremy Soller committed
223
                                new_active_opt = Some((f - 0x3A) as usize);
Jeremy Soller's avatar
Jeremy Soller committed
224 225
                            },
                            0x57 => { // F11
Jeremy Soller's avatar
Jeremy Soller committed
226
                                new_active_opt = Some(11);
Jeremy Soller's avatar
Jeremy Soller committed
227 228
                            },
                            0x58 => { // F12
Jeremy Soller's avatar
Jeremy Soller committed
229
                                new_active_opt = Some(12);
Jeremy Soller's avatar
Jeremy Soller committed
230
                            },
Jeremy Soller's avatar
Jeremy Soller committed
231 232 233 234
                            _ => ()
                        },
                        EventOption::Resize(resize_event) => {
                            println!("Resizing to {}, {}", resize_event.width, resize_event.height);
235
                            for (screen_i, screen) in self.screens.iter_mut() {
Jeremy Soller's avatar
Jeremy Soller committed
236
                                screen.resize(resize_event.width as usize, resize_event.height as usize);
237
                                if *screen_i == self.active {
Jeremy Soller's avatar
Jeremy Soller committed
238 239
                                    screen.redraw();
                                }
Jeremy Soller's avatar
Jeremy Soller committed
240 241 242
                            }
                        },
                        _ => ()
Jeremy Soller's avatar
Jeremy Soller committed
243 244 245
                    };

                    if let Some(new_active) = new_active_opt {
246 247
                        if let Some(mut screen) = self.screens.get_mut(&new_active) {
                            self.active = new_active;
Jeremy Soller's avatar
Jeremy Soller committed
248 249 250
                            screen.redraw();
                        }
                    } else {
251
                        if let Some(mut screen) = self.screens.get_mut(&self.active) {
Jeremy Soller's avatar
Jeremy Soller committed
252 253 254
                            screen.input(event);
                        }
                    }
Jeremy Soller's avatar
Jeremy Soller committed
255
                }
Jeremy Soller's avatar
Jeremy Soller committed
256 257

                Ok(events.len() * mem::size_of::<Event>())
258 259 260 261 262
            },
            HandleKind::Screen(screen_i) => if let Some(mut screen) = self.screens.get_mut(&screen_i) {
                screen.write(buf, screen_i == self.active)
            } else {
                Err(Error::new(EBADF))
Jeremy Soller's avatar
Jeremy Soller committed
263 264 265 266
            }
        }
    }

267
    fn seek(&mut self, id: usize, pos: usize, whence: usize) -> Result<usize> {
268 269 270 271 272 273
        let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;

        if let HandleKind::Screen(screen_i) = handle.kind {
            if let Some(mut screen) = self.screens.get_mut(&screen_i) {
                return screen.seek(pos, whence);
            }
Jeremy Soller's avatar
Jeremy Soller committed
274
        }
275 276

        Err(Error::new(EBADF))
Jeremy Soller's avatar
Jeremy Soller committed
277 278
    }

279 280
    fn close(&mut self, id: usize) -> Result<usize> {
        self.handles.remove(&id).ok_or(Error::new(EBADF))?;
Jeremy Soller's avatar
Jeremy Soller committed
281 282 283
        Ok(0)
    }
}