scheme.rs 9.41 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};
Jeremy Soller's avatar
Jeremy Soller committed
5
use syscall::{Result, Error, EACCES, EBADF, EINVAL, 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
#[derive(Clone)]
11
pub enum HandleKind {
12
13
14
15
16
    Input,
    Screen(usize),
}

#[derive(Clone)]
17
18
19
20
pub struct Handle {
    pub kind: HandleKind,
    pub flags: usize,
    pub events: usize,
21
22
}

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

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

        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
45
46

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

56
57
58
59
60
61
62
63
64
65
66
67
68
69
    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
70
        }
71
72

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

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

                self.handles.insert(id, Handle {
                    kind: HandleKind::Input,
85
86
                    flags: flags,
                    events: 0,
87
88
89
                });

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

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

                self.handles.insert(id, Handle {
                    kind: HandleKind::Screen(screen_i),
109
110
                    flags: flags,
                    events: 0,
111
112
                });

Jeremy Soller's avatar
Jeremy Soller committed
113
114
115
116
117
118
119
                Ok(id)
            } else {
                Err(Error::new(ENOENT))
            }
        }
    }

120
121
122
123
124
    fn dup(&mut self, id: usize, buf: &[u8]) -> Result<usize> {
        if ! buf.is_empty() {
            return Err(Error::new(EINVAL));
        }

125
126
127
128
129
130
131
132
        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
133
134
    }

135
    fn fevent(&mut self, id: usize, flags: usize) -> Result<usize> {
136
        let handle = self.handles.get_mut(&id).ok_or(Error::new(EBADF))?;
137

138
139
140
141
142
        if let HandleKind::Screen(_screen_i) = handle.kind {
            handle.events = flags;
            Ok(id)
        } else {
            Err(Error::new(EBADF))
Jeremy Soller's avatar
Jeremy Soller committed
143
144
145
        }
    }

146
    fn fmap(&mut self, id: usize, offset: usize, size: usize) -> Result<usize> {
147
148
149
150
151
152
        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);
            }
153
        }
154
155

        Err(Error::new(EBADF))
156
157
    }

158
    fn fpath(&mut self, id: usize, buf: &mut [u8]) -> Result<usize> {
159
160
161
162
163
164
165
166
167
168
169
        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
170
171
172
173
174
175
176
177
178
179
180
181
182
        };

        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)
    }

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

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

        Err(Error::new(EBADF))
Jeremy Soller's avatar
Jeremy Soller committed
196
197
    }

198
    fn read(&mut self, id: usize, buf: &mut [u8]) -> Result<usize> {
199
200
201
        let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;

        if let HandleKind::Screen(screen_i) = handle.kind {
Jeremy Soller's avatar
Jeremy Soller committed
202
            if let Some(screen) = self.screens.get_mut(&screen_i) {
203
204
                return screen.read(buf);
            }
Jeremy Soller's avatar
Jeremy Soller committed
205
        }
206
207

        Err(Error::new(EBADF))
Jeremy Soller's avatar
Jeremy Soller committed
208
209
    }

210
    fn write(&mut self, id: usize, buf: &[u8]) -> Result<usize> {
211
212
213
214
        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
215
                let new_active = (buf[0] - 0xF4) as usize + 1;
Jeremy Soller's avatar
Jeremy Soller committed
216
                if let Some(screen) = self.screens.get_mut(&new_active) {
217
                    self.active = new_active;
Jeremy Soller's avatar
Jeremy Soller committed
218
219
220
221
                    screen.redraw();
                }
                Ok(1)
            } else {
Jeremy Soller's avatar
Jeremy Soller committed
222
223
224
                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
225
226
227
                    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
228
                            f @ 0x3B ... 0x44 => { // F1 through F10
Jeremy Soller's avatar
Jeremy Soller committed
229
                                new_active_opt = Some((f - 0x3A) as usize);
Jeremy Soller's avatar
Jeremy Soller committed
230
231
                            },
                            0x57 => { // F11
Jeremy Soller's avatar
Jeremy Soller committed
232
                                new_active_opt = Some(11);
Jeremy Soller's avatar
Jeremy Soller committed
233
234
                            },
                            0x58 => { // F12
Jeremy Soller's avatar
Jeremy Soller committed
235
                                new_active_opt = Some(12);
Jeremy Soller's avatar
Jeremy Soller committed
236
                            },
Jeremy Soller's avatar
Jeremy Soller committed
237
238
239
240
                            _ => ()
                        },
                        EventOption::Resize(resize_event) => {
                            println!("Resizing to {}, {}", resize_event.width, resize_event.height);
241
242
                            self.width = resize_event.width as usize;
                            self.height = resize_event.height as usize;
243
                            for (screen_i, screen) in self.screens.iter_mut() {
Jeremy Soller's avatar
Jeremy Soller committed
244
                                screen.resize(resize_event.width as usize, resize_event.height as usize);
245
                                if *screen_i == self.active {
Jeremy Soller's avatar
Jeremy Soller committed
246
247
                                    screen.redraw();
                                }
Jeremy Soller's avatar
Jeremy Soller committed
248
249
250
                            }
                        },
                        _ => ()
Jeremy Soller's avatar
Jeremy Soller committed
251
252
253
                    };

                    if let Some(new_active) = new_active_opt {
Jeremy Soller's avatar
Jeremy Soller committed
254
                        if let Some(screen) = self.screens.get_mut(&new_active) {
255
                            self.active = new_active;
Jeremy Soller's avatar
Jeremy Soller committed
256
257
258
                            screen.redraw();
                        }
                    } else {
Jeremy Soller's avatar
Jeremy Soller committed
259
                        if let Some(screen) = self.screens.get_mut(&self.active) {
Jeremy Soller's avatar
Jeremy Soller committed
260
261
262
                            screen.input(event);
                        }
                    }
Jeremy Soller's avatar
Jeremy Soller committed
263
                }
Jeremy Soller's avatar
Jeremy Soller committed
264
265

                Ok(events.len() * mem::size_of::<Event>())
266
            },
Jeremy Soller's avatar
Jeremy Soller committed
267
            HandleKind::Screen(screen_i) => if let Some(screen) = self.screens.get_mut(&screen_i) {
268
269
270
                screen.write(buf, screen_i == self.active)
            } else {
                Err(Error::new(EBADF))
Jeremy Soller's avatar
Jeremy Soller committed
271
272
273
274
            }
        }
    }

275
    fn seek(&mut self, id: usize, pos: usize, whence: usize) -> Result<usize> {
276
277
278
        let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;

        if let HandleKind::Screen(screen_i) = handle.kind {
Jeremy Soller's avatar
Jeremy Soller committed
279
            if let Some(screen) = self.screens.get_mut(&screen_i) {
280
281
                return screen.seek(pos, whence);
            }
Jeremy Soller's avatar
Jeremy Soller committed
282
        }
283
284

        Err(Error::new(EBADF))
Jeremy Soller's avatar
Jeremy Soller committed
285
286
    }

287
288
    fn close(&mut self, id: usize) -> Result<usize> {
        self.handles.remove(&id).ok_or(Error::new(EBADF))?;
Jeremy Soller's avatar
Jeremy Soller committed
289
290
291
        Ok(0)
    }
}