garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /hid/src/keyboard.rs

raw

//! A keyboard's boot report: the eight bytes every keyboard can send
//! (HID 1.11, appendix B.1), whether over USB or Bluetooth (HID over GATT's
//! Boot Keyboard Input Report). A byte of modifier bits, a reserved one,
//! then up to six keys held, as usages of the keyboard page; each report
//! says what is held now, and [`Keyboard`] turns the difference from the
//! last into presses and releases of Linux's key codes, which the display
//! server reads.

use alloc::vec::Vec;

/// Linux's key code for each keyboard usage (HID Usage Tables, page 7), as
/// its `usbkbd` driver has them.
const KEYS: [u8; 256] = [
    0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, //
    50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3, //
    4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26, //
    27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64, //
    65, 66, 67, 68, 87, 88, 99, 70, 119, 110, 102, 104, 111, 107, 109, 106, //
    105, 108, 103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71, //
    72, 73, 82, 83, 86, 127, 116, 117, 183, 184, 185, 186, 187, 188, 189, 190, //
    191, 192, 193, 194, 134, 138, 130, 132, 128, 129, 131, 137, 133, 135, 136, 113, //
    115, 114, 0, 0, 0, 121, 0, 89, 93, 124, 92, 94, 95, 0, 0, 0, //
    122, 123, 90, 91, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
    29, 42, 56, 125, 97, 54, 100, 126, 164, 166, 165, 163, 161, 115, 114, 113, //
    150, 158, 159, 128, 136, 177, 178, 176, 142, 152, 173, 140, 0, 0, 0, 0, //
];

/// The key codes of the modifier bits of a boot report's first byte: left
/// Ctrl, Shift, Alt, Meta, then the right ones.
const MODIFIERS: [u16; 8] = [29, 42, 56, 125, 97, 54, 100, 126];

/// Linux's key code for a keyboard usage, 0 for none.
pub fn key_code(usage: u8) -> u16 {
    u16::from(KEYS[usize::from(usage)])
}

/// The usage of a key code, for whoever makes boot reports (a simulated
/// keyboard): the first usage that maps to it.
pub fn usage(code: u16) -> Option<u8> {
    (4..=255u8).find(|&u| key_code(u) == code && code != 0)
}

/// A key pressed (`down`) or released, as a Linux key code.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Key {
    pub code: u16,
    pub down: bool,
}

/// A keyboard sending boot reports: what it held at its last.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Keyboard {
    previous: [u8; 8],
}

impl Keyboard {
    /// A report arrived: the keys that went down or up since the last,
    /// modifiers first, releases before presses.
    pub fn report(&mut self, report: &[u8]) -> Vec<Key> {
        let mut out = Vec::new();
        let mut now = [0u8; 8];
        let n = report.len().min(8);
        now[..n].copy_from_slice(&report[..n]);
        // A keyboard says it has too many keys down with all of them 1.
        if now[2..].iter().all(|&k| k == 1) {
            return out;
        }
        let before = self.previous;
        let changed = before[0] ^ now[0];
        for (bit, &code) in MODIFIERS.iter().enumerate() {
            if changed & (1 << bit) != 0 {
                out.push(Key {
                    code,
                    down: now[0] >> bit & 1 != 0,
                });
            }
        }
        for &k in before[2..].iter().filter(|&&k| k > 3 && !now[2..].contains(&k)) {
            out.push(Key {
                code: key_code(k),
                down: false,
            });
        }
        for &k in now[2..].iter().filter(|&&k| k > 3 && !before[2..].contains(&k)) {
            out.push(Key {
                code: key_code(k),
                down: true,
            });
        }
        self.previous = now;
        out
    }

    /// Everything held let go, as when the keyboard goes away.
    pub fn release_all(&mut self) -> Vec<Key> {
        self.report(&[0; 8])
    }
}