graphic.rs 3.37 KB
Newer Older
Jeremy Soller's avatar
Jeremy Soller committed
1 2 3
use std::collections::VecDeque;
use std::{cmp, mem, slice};

Jeremy Soller's avatar
Jeremy Soller committed
4
use orbclient::{Event, ResizeEvent};
Jeremy Soller's avatar
Jeremy Soller committed
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
use syscall::error::*;
use syscall::flag::{SEEK_SET, SEEK_CUR, SEEK_END};

use display::Display;
use primitive::fast_copy;
use screen::Screen;

pub struct GraphicScreen {
    pub display: Display,
    pub seek: usize,
    pub input: VecDeque<Event>,
    pub requested: usize
}

impl GraphicScreen {
    pub fn new(display: Display) -> GraphicScreen {
        GraphicScreen {
            display: display,
            seek: 0,
            input: VecDeque::new(),
            requested: 0
        }
    }
}

impl Screen for GraphicScreen {
    fn width(&self) -> usize {
        self.display.width
    }

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

Jeremy Soller's avatar
Jeremy Soller committed
39 40 41 42 43 44 45 46 47
    fn resize(&mut self, width: usize, height: usize) {
        //TODO: Fix issue with mapped screens
        self.display.resize(width, height);
        self.input.push_back(ResizeEvent {
            width: width as u32,
            height: height as u32,
        }.to_event());
    }

Jeremy Soller's avatar
Jeremy Soller committed
48 49 50 51 52
    fn event(&mut self, flags: usize) -> Result<usize> {
        self.requested = flags;
        Ok(0)
    }

53 54 55 56 57 58 59 60
    fn map(&self, offset: usize, size: usize) -> Result<usize> {
        if offset + size <= self.display.offscreen.len() * 4 {
            Ok(self.display.offscreen.as_ptr() as usize + offset)
        } else {
            Err(Error::new(EINVAL))
        }
    }

Jeremy Soller's avatar
Jeremy Soller committed
61
    fn input(&mut self, event: &Event) {
62
        self.input.push_back(*event);
Jeremy Soller's avatar
Jeremy Soller committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    }

    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        let mut i = 0;

        let event_buf = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr() as *mut Event, buf.len()/mem::size_of::<Event>()) };

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

        Ok(i * mem::size_of::<Event>())
    }

78 79 80 81 82 83
    fn can_read(&self) -> Option<usize> {
        if self.input.is_empty() {
            None
        } else {
            Some(self.input.len() * mem::size_of::<Event>())
        }
Jeremy Soller's avatar
Jeremy Soller committed
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
    }

    fn write(&mut self, buf: &[u8], sync: bool) -> Result<usize> {
        let size = cmp::max(0, cmp::min(self.display.offscreen.len() as isize - self.seek as isize, (buf.len()/4) as isize)) as usize;

        if size > 0 {
            unsafe {
                fast_copy(self.display.offscreen.as_mut_ptr().offset(self.seek as isize) as *mut u8, buf.as_ptr(), size * 4);
                if sync {
                    fast_copy(self.display.onscreen.as_mut_ptr().offset(self.seek as isize) as *mut u8, buf.as_ptr(), size * 4);
                }
            }
        }

        Ok(size * 4)
    }

    fn seek(&mut self, pos: usize, whence: usize) -> Result<usize> {
        let size = self.display.offscreen.len();

        self.seek = match whence {
            SEEK_SET => cmp::min(size, (pos/4)),
            SEEK_CUR => cmp::max(0, cmp::min(size as isize, self.seek as isize + (pos/4) as isize)) as usize,
            SEEK_END => cmp::max(0, cmp::min(size as isize, size as isize + (pos/4) as isize)) as usize,
            _ => return Err(Error::new(EINVAL))
        };

        Ok(self.seek * 4)
    }

    fn sync(&mut self) {
        self.redraw();
    }

    fn redraw(&mut self) {
        let width = self.display.width;
        let height = self.display.height;
        self.display.sync(0, 0, width, height);
    }
}