main.rs 6.02 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
use std::{env, process};
11 12 13
use std::fs::File;
use std::io::{Read, Write, Result};
use std::os::unix::io::AsRawFd;
14
use std::mem;
15

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

Jeremy Soller's avatar
Jeremy Soller committed
20
mod controller;
Jeremy Soller's avatar
Jeremy Soller committed
21
mod keymap;
22

23 24 25 26 27 28 29 30 31 32
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
33
    }
34
}
35

Waylon Cude's avatar
Waylon Cude committed
36
struct Ps2d<'a> {
Jeremy Soller's avatar
Jeremy Soller committed
37 38 39 40 41
    input: File,
    lshift: bool,
    rshift: bool,
    packets: [u8; 4],
    packet_i: usize,
Waylon Cude's avatar
Waylon Cude committed
42 43 44
    extra_packet: bool,
    //Keymap function
    get_char: &'a Fn(u8,bool) -> char
Jeremy Soller's avatar
Jeremy Soller committed
45 46
}

Waylon Cude's avatar
Waylon Cude committed
47 48
impl<'a> Ps2d<'a> {
    fn new(input: File, extra_packet: bool, keymap: &'a Fn(u8,bool) -> char) -> Self {
Jeremy Soller's avatar
Jeremy Soller committed
49 50 51 52 53 54
        Ps2d {
            input: input,
            lshift: false,
            rshift: false,
            packets: [0; 4],
            packet_i: 0,
Waylon Cude's avatar
Waylon Cude committed
55 56
            extra_packet: extra_packet,
            get_char: keymap
Jeremy Soller's avatar
Jeremy Soller committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        }
    }

    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
75
                character: (self.get_char)(scancode, self.lshift || self.rshift),
Jeremy Soller's avatar
Jeremy Soller committed
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 119 120 121 122 123 124
                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;
                    }

                    let _extra = if self.extra_packet {
                        self.packets[3]
                    } else {
                        0
                    };

                    self.input.write(&MouseEvent {
                        x: dx,
                        y: dy,
                        left_button: flags.contains(LEFT_BUTTON),
                        middle_button: flags.contains(MIDDLE_BUTTON),
                        right_button: flags.contains(RIGHT_BUTTON)
                    }.to_event()).expect("ps2d: failed to write mouse event");
                } 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;
            }
        }
    }
}

125 126 127 128 129
fn daemon(input: File) {
    unsafe {
        iopl(3).expect("ps2d: failed to get I/O permission");
        asm!("cli" : : : : "intel", "volatile");
    }
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    let extra_packet = controller::Ps2::new().init();
    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)
    };
    let mut ps2d = Ps2d::new(input, extra_packet,&keymap);

    let mut event_queue = EventQueue::<(bool, u8)>::new().expect("ps2d: failed to create event queue");

    let mut key_irq = File::open("irq:1").expect("ps2d: failed to open irq:1");

    event_queue.add(key_irq.as_raw_fd(), move |_count: usize| -> Result<Option<(bool, u8)>> {
        let mut irq = [0; 8];
        if key_irq.read(&mut irq)? >= mem::size_of::<usize>() {
            let data: u8;
            unsafe {
                asm!("in al, dx" : "={al}"(data) : "{dx}"(0x60) : : "intel", "volatile");
152 153
            }

154
            key_irq.write(&irq)?;
155

156 157 158 159 160
            Ok(Some((true, data)))
        } else {
            Ok(None)
        }
    }).expect("ps2d: failed to poll irq:1");
161

162
    let mut mouse_irq = File::open("irq:12").expect("ps2d: failed to open irq:12");
163

164 165 166 167 168 169
    event_queue.add(mouse_irq.as_raw_fd(), move |_count: usize| -> Result<Option<(bool, u8)>> {
        let mut irq = [0; 8];
        if mouse_irq.read(&mut irq)? >= mem::size_of::<usize>() {
            let data: u8;
            unsafe {
                asm!("in al, dx" : "={al}"(data) : "{dx}"(0x60) : : "intel", "volatile");
170 171
            }

172 173 174 175 176
            mouse_irq.write(&irq)?;

            Ok(Some((false, data)))
        } else {
            Ok(None)
Jeremy Soller's avatar
Jeremy Soller committed
177
        }
178 179 180 181 182
    }).expect("ps2d: failed to poll irq:12");

    for (keyboard, data) in event_queue.trigger_all(0).expect("ps2d: failed to trigger events") {
        ps2d.handle(keyboard, data);
    }
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    loop {
        let (keyboard, data) = event_queue.run().expect("ps2d: failed to handle events");
        ps2d.handle(keyboard, data);
    }
}

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);
201
        }
202
    }
203
}