keyboard.rs 3.34 KB
Newer Older
Jeremy Soller's avatar
Jeremy Soller committed
1 2 3 4 5 6 7
use std::fs::File;
use std::io::{Read, Write};
use std::mem;
use std::thread;

use keymap;

8
pub fn keyboard() {
Jeremy Soller's avatar
Jeremy Soller committed
9
    let mut file = File::open("irq:1").expect("ps2d: failed to open irq:1");
10
    let mut input = File::open("display:input").expect("ps2d: failed to open display:input");
Jeremy Soller's avatar
Jeremy Soller committed
11

12
    let mut ctrl = false;
13 14
    let mut lshift = false;
    let mut rshift = false;
Jeremy Soller's avatar
Jeremy Soller committed
15 16 17 18 19 20 21 22
    loop {
        let mut irqs = [0; 8];
        if file.read(&mut irqs).expect("ps2d: failed to read irq:1") >= mem::size_of::<usize>() {
            let data: u8;
            unsafe {
                asm!("in al, dx" : "={al}"(data) : "{dx}"(0x60) : : "intel", "volatile");
            }

23 24
            file.write(&irqs).expect("ps2d: failed to write irq:1");

Jeremy Soller's avatar
Jeremy Soller committed
25 26 27 28 29 30
            let (scancode, pressed) = if data >= 0x80 {
                (data - 0x80, false)
            } else {
                (data, true)
            };

31 32 33
            if scancode == 0x1D {
                ctrl = pressed;
            } else if scancode == 0x2A {
34 35 36 37
                lshift = pressed;
            } else if scancode == 0x36 {
                rshift = pressed;
            } else if pressed {
38
                match scancode {
Jeremy Soller's avatar
Jeremy Soller committed
39 40 41 42 43 44 45 46 47
                    f @ 0x3B ... 0x44 => { // F1 through F10
                        input.write(&[(f - 0x3B) + 0xF4]).unwrap();
                    },
                    0x57 => { // F11
                        input.write(&[0xFE]).unwrap();
                    },
                    0x58 => { // F12
                        input.write(&[0xFF]).unwrap();
                    },
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
                    0x47 => { // Home
                        input.write(b"\x1B[H").unwrap();
                    },
                    0x48 => { // Up
                        input.write(b"\x1B[A").unwrap();
                    },
                    0x49 => { // Page up
                        input.write(b"\x1B[5~").unwrap();
                    },
                    0x4B => { // Left
                        input.write(b"\x1B[D").unwrap();
                    },
                    0x4D => { // Right
                        input.write(b"\x1B[C").unwrap();
                    },
                    0x4F => { // End
                        input.write(b"\x1B[F").unwrap();
                    },
                    0x50 => { // Down
                        input.write(b"\x1B[B").unwrap();
                    },
                    0x51 => { // Page down
                        input.write(b"\x1B[6~").unwrap();
                    },
Jeremy Soller's avatar
Jeremy Soller committed
72 73 74 75 76 77
                    0x52 => { // Insert
                        input.write(b"\x1B[2~").unwrap();
                    },
                    0x53 => { // Delete
                        input.write(b"\x1B[3~").unwrap();
                    },
78 79 80
                    _ => {
                        let c = if ctrl {
                            match keymap::get_char(scancode, false) {
Jeremy Soller's avatar
Jeremy Soller committed
81
                                c @ 'a' ... 'z' => ((c as u8 - b'a') + b'\x01') as char,
82 83 84 85 86 87 88 89 90 91
                                c => c
                            }
                        } else {
                            keymap::get_char(scancode, lshift || rshift)
                        };

                        if c != '\0' {
                            input.write(&[c as u8]).unwrap();
                        }
                    }
92
                }
Jeremy Soller's avatar
Jeremy Soller committed
93 94 95 96 97 98
            }
        } else {
            thread::yield_now();
        }
    }
}