main.rs 7.94 KB
Newer Older
1
#![deny(warnings)]
2 3
#![feature(asm)]

Jeremy Soller's avatar
Jeremy Soller committed
4 5
#[macro_use]
extern crate bitflags;
6
extern crate event;
Jeremy Soller's avatar
Jeremy Soller committed
7
extern crate orbclient;
8 9
extern crate syscall;

10 11
use std::{cmp, env, process};
use std::cell::RefCell;
12 13 14
use std::fs::File;
use std::io::{Read, Write, Result};
use std::os::unix::io::AsRawFd;
15
use std::sync::Arc;
16

17
use event::EventQueue;
18
use orbclient::{KeyEvent, MouseEvent, ButtonEvent, ScrollEvent};
19 20
use syscall::iopl;

21 22
use controller::Ps2;

Jeremy Soller's avatar
Jeremy Soller committed
23
mod controller;
Jeremy Soller's avatar
Jeremy Soller committed
24
mod keymap;
25

26 27 28 29 30 31 32 33 34 35
bitflags! {
    flags MousePacketFlags: u8 {
        const LEFT_BUTTON = 1,
        const RIGHT_BUTTON = 1 << 1,
        const MIDDLE_BUTTON = 1 << 2,
        const ALWAYS_ON = 1 << 3,
        const X_SIGN = 1 << 4,
        const Y_SIGN = 1 << 5,
        const X_OVERFLOW = 1 << 6,
        const Y_OVERFLOW = 1 << 7
Jeremy Soller's avatar
Jeremy Soller committed
36
    }
37
}
38

39 40
struct Ps2d<F: Fn(u8,bool) -> char>  {
    ps2: Ps2,
Jeremy Soller's avatar
Jeremy Soller committed
41
    input: File,
42 43
    width: u32,
    height: u32,
Jeremy Soller's avatar
Jeremy Soller committed
44 45
    lshift: bool,
    rshift: bool,
46 47 48 49 50
    mouse_x: i32,
    mouse_y: i32,
    mouse_left: bool,
    mouse_middle: bool,
    mouse_right: bool,
Jeremy Soller's avatar
Jeremy Soller committed
51 52
    packets: [u8; 4],
    packet_i: usize,
Waylon Cude's avatar
Waylon Cude committed
53 54
    extra_packet: bool,
    //Keymap function
55
    get_char: F
Jeremy Soller's avatar
Jeremy Soller committed
56 57
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
impl<F: Fn(u8,bool) -> char> Ps2d<F> {
    fn new(input: File, keymap: F) -> Self {
        let mut ps2 = Ps2::new();
        let extra_packet = ps2.init();

        let mut width = 0;
        let mut height = 0;
        {
            let mut buf: [u8; 4096] = [0; 4096];
            if let Ok(count) = syscall::fpath(input.as_raw_fd() as usize, &mut buf) {
                let path = unsafe { String::from_utf8_unchecked(Vec::from(&buf[..count])) };
                let res = path.split(":").nth(1).unwrap_or("");
                width = res.split("/").nth(1).unwrap_or("").parse::<u32>().unwrap_or(0);
                height = res.split("/").nth(2).unwrap_or("").parse::<u32>().unwrap_or(0);
            }
        }

Jeremy Soller's avatar
Jeremy Soller committed
75
        Ps2d {
76
            ps2: ps2,
Jeremy Soller's avatar
Jeremy Soller committed
77
            input: input,
78 79
            width: width,
            height: height,
Jeremy Soller's avatar
Jeremy Soller committed
80 81
            lshift: false,
            rshift: false,
82 83 84 85 86
            mouse_x: 0,
            mouse_y: 0,
            mouse_left: false,
            mouse_middle: false,
            mouse_right: false,
Jeremy Soller's avatar
Jeremy Soller committed
87 88
            packets: [0; 4],
            packet_i: 0,
Waylon Cude's avatar
Waylon Cude committed
89 90
            extra_packet: extra_packet,
            get_char: keymap
Jeremy Soller's avatar
Jeremy Soller committed
91 92 93
        }
    }

94 95 96 97 98 99
    fn irq(&mut self) {
        while let Some((keyboard, data)) = self.ps2.next() {
            self.handle(keyboard, data);
        }
    }

Jeremy Soller's avatar
Jeremy Soller committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    fn handle(&mut self, keyboard: bool, data: u8) {
        if keyboard {
            let (scancode, pressed) = if data >= 0x80 {
                (data - 0x80, false)
            } else {
                (data, true)
            };

            if scancode == 0x2A {
                self.lshift = pressed;
            } else if scancode == 0x36 {
                self.rshift = pressed;
            }

            self.input.write(&KeyEvent {
Waylon Cude's avatar
Waylon Cude committed
115
                character: (self.get_char)(scancode, self.lshift || self.rshift),
Jeremy Soller's avatar
Jeremy Soller committed
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
                scancode: scancode,
                pressed: pressed
            }.to_event()).expect("ps2d: failed to write key event");
        } else {
            self.packets[self.packet_i] = data;
            self.packet_i += 1;

            let flags = MousePacketFlags::from_bits_truncate(self.packets[0]);
            if ! flags.contains(ALWAYS_ON) {
                println!("MOUSE MISALIGN {:X}", self.packets[0]);

                self.packets = [0; 4];
                self.packet_i = 0;
            } else if self.packet_i >= self.packets.len() || (!self.extra_packet && self.packet_i >= 3) {
                if ! flags.contains(X_OVERFLOW) && ! flags.contains(Y_OVERFLOW) {
                    let mut dx = self.packets[1] as i32;
                    if flags.contains(X_SIGN) {
                        dx -= 0x100;
                    }

                    let mut dy = -(self.packets[2] as i32);
                    if flags.contains(Y_SIGN) {
                        dy += 0x100;
                    }

141 142 143 144 145 146 147 148
                    let mut dz = 0;
                    if self.extra_packet {
                        let mut scroll = (self.packets[3] & 0xF) as i8;
                        if scroll & (1 << 3) == 1 << 3 {
                            scroll -= 16;
                        }
                        dz = -scroll as i32;
                    }
Jeremy Soller's avatar
Jeremy Soller committed
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
                    let x = cmp::max(0, cmp::min(self.width as i32, self.mouse_x + dx));
                    let y = cmp::max(0, cmp::min(self.height as i32, self.mouse_y + dy));
                    if x != self.mouse_x || y != self.mouse_y {
                        self.mouse_x = x;
                        self.mouse_y = y;
                        self.input.write(&MouseEvent {
                            x: x,
                            y: y,
                        }.to_event()).expect("ps2d: failed to write mouse event");
                    }

                    let left = flags.contains(LEFT_BUTTON);
                    let middle = flags.contains(MIDDLE_BUTTON);
                    let right = flags.contains(RIGHT_BUTTON);
                    if left != self.mouse_left || middle != self.mouse_middle || right != self.mouse_right {
                        self.mouse_left = left;
                        self.mouse_middle = middle;
                        self.mouse_right = right;
                        self.input.write(&ButtonEvent {
                            left: left,
                            middle: middle,
                            right: right,
                        }.to_event()).expect("ps2d: failed to write button event");
                    }
174 175 176 177 178 179 180

                    if dz != 0 {
                        self.input.write(&ScrollEvent {
                            x: 0,
                            y: dz,
                        }.to_event()).expect("ps2d: failed to write scroll event");
                    }
Jeremy Soller's avatar
Jeremy Soller committed
181 182 183 184 185 186 187 188 189 190 191
                } else {
                    println!("ps2d: overflow {:X} {:X} {:X} {:X}", self.packets[0], self.packets[1], self.packets[2], self.packets[3]);
                }

                self.packets = [0; 4];
                self.packet_i = 0;
            }
        }
    }
}

192 193 194 195
fn daemon(input: File) {
    unsafe {
        iopl(3).expect("ps2d: failed to get I/O permission");
    }
196

197 198 199 200 201 202 203 204
    let keymap = match env::args().skip(1).next() {
        Some(k) => match k.to_lowercase().as_ref() {
            "dvorak" => (keymap::dvorak::get_char),
            "english" => (keymap::english::get_char),
            &_ => (keymap::english::get_char)
        },
        None => (keymap::english::get_char)
    };
205
    let ps2d = Arc::new(RefCell::new(Ps2d::new(input, keymap)));
206

207
    let mut event_queue = EventQueue::<()>::new().expect("ps2d: failed to create event queue");
208 209

    let mut key_irq = File::open("irq:1").expect("ps2d: failed to open irq:1");
210 211
    let key_ps2d = ps2d.clone();
    event_queue.add(key_irq.as_raw_fd(), move |_count: usize| -> Result<Option<()>> {
212
        let mut irq = [0; 8];
213 214
        if key_irq.read(&mut irq)? >= irq.len() {
            key_ps2d.borrow_mut().irq();
215 216
            key_irq.write(&irq)?;
        }
217
        Ok(None)
218
    }).expect("ps2d: failed to poll irq:1");
219

220
    let mut mouse_irq = File::open("irq:12").expect("ps2d: failed to open irq:12");
221 222
    let mouse_ps2d = ps2d;
    event_queue.add(mouse_irq.as_raw_fd(), move |_count: usize| -> Result<Option<()>> {
223
        let mut irq = [0; 8];
224 225
        if mouse_irq.read(&mut irq)? >= irq.len() {
            mouse_ps2d.borrow_mut().irq();
226
            mouse_irq.write(&irq)?;
Jeremy Soller's avatar
Jeremy Soller committed
227
        }
228
        Ok(None)
229 230
    }).expect("ps2d: failed to poll irq:12");

231
    event_queue.trigger_all(0).expect("ps2d: failed to trigger events");
232

233
    event_queue.run().expect("ps2d: failed to handle events");
234 235 236 237 238 239 240 241 242 243 244 245 246
}

fn main() {
    match File::open("display:input") {
        Ok(input) => {
            // Daemonize
            if unsafe { syscall::clone(0).unwrap() } == 0 {
                daemon(input);
            }
        },
        Err(err) => {
            println!("ps2d: failed to open display: {}", err);
            process::exit(1);
247
        }
248
    }
249
}