main.rs 2.49 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
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 19
use syscall::iopl;

20
use state::Ps2d;
21

Jeremy Soller's avatar
Jeremy Soller committed
22
mod controller;
Jeremy Soller's avatar
Jeremy Soller committed
23
mod keymap;
24
mod state;
Jeremy Soller's avatar
Jeremy Soller committed
25
mod vm;
26

27 28 29 30
fn daemon(input: File) {
    unsafe {
        iopl(3).expect("ps2d: failed to get I/O permission");
    }
31

32 33 34
    let keymap = match env::args().skip(1).next() {
        Some(k) => match k.to_lowercase().as_ref() {
            "dvorak" => (keymap::dvorak::get_char),
Karl Hobley's avatar
Karl Hobley committed
35
            "us" => (keymap::us::get_char),
Karl Hobley's avatar
Karl Hobley committed
36
            "gb" => (keymap::gb::get_char),
37 38
            "azerty" => (keymap::azerty::get_char),
            "bepo" => (keymap::bepo::get_char),
Karl Hobley's avatar
Karl Hobley committed
39
            &_ => (keymap::us::get_char)
40
        },
Karl Hobley's avatar
Karl Hobley committed
41
        None => (keymap::us::get_char)
42
    };
43 44 45 46 47

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

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

48
    let ps2d = Arc::new(RefCell::new(Ps2d::new(input, keymap)));
49

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

52 53
    syscall::setrens(0, 0).expect("ps2d: failed to enter null namespace");

54 55
    let key_ps2d = ps2d.clone();
    event_queue.add(key_irq.as_raw_fd(), move |_count: usize| -> Result<Option<()>> {
56
        let mut irq = [0; 8];
57 58
        if key_irq.read(&mut irq)? >= irq.len() {
            key_ps2d.borrow_mut().irq();
59 60
            key_irq.write(&irq)?;
        }
61
        Ok(None)
62
    }).expect("ps2d: failed to poll irq:1");
63

64 65
    let mouse_ps2d = ps2d;
    event_queue.add(mouse_irq.as_raw_fd(), move |_count: usize| -> Result<Option<()>> {
66
        let mut irq = [0; 8];
67 68
        if mouse_irq.read(&mut irq)? >= irq.len() {
            mouse_ps2d.borrow_mut().irq();
69
            mouse_irq.write(&irq)?;
Jeremy Soller's avatar
Jeremy Soller committed
70
        }
71
        Ok(None)
72 73
    }).expect("ps2d: failed to poll irq:12");

74
    event_queue.trigger_all(0).expect("ps2d: failed to trigger events");
75

76
    event_queue.run().expect("ps2d: failed to handle events");
77 78 79 80 81 82 83 84 85 86 87 88 89
}

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);
90
        }
91
    }
92
}